Unit selection and ordering, setTimeout, scheduler, fixed timestep code, various fixes and reinforcement of the JS<->Native code.

This was SVN commit r783.
This commit is contained in:
MarkT
2004-07-20 19:30:35 +00:00
parent be9d6d032f
commit 194fdf9b4b
41 changed files with 2087 additions and 242 deletions
+182
View File
@@ -8,6 +8,8 @@
#include "Entity.h"
#include "EntityManager.h"
#include "BaseEntityCollection.h"
#include "Scheduler.h"
#include "Interact.h"
#include "scripting/JSInterface_Entity.h"
#include "scripting/JSInterface_BaseEntity.h"
#include "scripting/JSInterface_Vector3D.h"
@@ -29,6 +31,9 @@ JSFunctionSpec ScriptFunctionTable[] =
{"writeConsole", writeConsole, 1, 0, 0 },
{"getEntityByHandle", getEntityByHandle, 1, 0, 0 },
{"getEntityTemplate", getEntityTemplate, 1, 0, 0 },
{"setTimeout", setTimeout, 2, 0, 0 },
{"setInterval", setInterval, 2, 0, 0 },
{"cancelInterval", cancelInterval, 0, 0, 0 },
{"getGUIObjectByName", JSI_IGUIObject::getByName, 1, 0, 0 },
{"getGlobal", getGlobal, 0, 0, 0 },
{"getGUIGlobal", getGUIGlobal, 0, 0, 0 },
@@ -37,6 +42,19 @@ JSFunctionSpec ScriptFunctionTable[] =
{0, 0, 0, 0, 0},
};
enum ScriptGlobalTinyIDs
{
GLOBAL_SELECTED,
GLOBAL_SELECTIONARRAY
};
JSPropertySpec ScriptGlobalTable[] =
{
{ "selected", GLOBAL_SELECTED, JSPROP_PERMANENT, getSelected, setSelected },
{ "selection", GLOBAL_SELECTIONARRAY, JSPROP_PERMANENT, getSelection, setSelection },
{ 0, 0, 0, 0, 0 },
};
// Allow scripts to output to the global log file
JSBool WriteLog(JSContext * context, JSObject * globalObject, unsigned int argc, jsval * argv, jsval * rval)
{
@@ -88,11 +106,13 @@ JSBool getEntityByHandle( JSContext* context, JSObject* globalObject, unsigned i
catch( PSERROR_Scripting_ConversionFailed )
{
*rval = JSVAL_NULL;
JS_ReportError( context, "Invalid handle" );
return( JS_TRUE );
}
HEntity* v = g_EntityManager.getByHandle( (u16)handle );
if( !v )
{
JS_ReportError( context, "No entity occupying handle: %d", handle );
*rval = JSVAL_NULL;
return( JS_TRUE );
}
@@ -113,12 +133,14 @@ JSBool getEntityTemplate( JSContext* context, JSObject* globalObject, unsigned i
catch( PSERROR_Scripting_ConversionFailed )
{
*rval = JSVAL_NULL;
JS_ReportError( context, "Invalid template identifier" );
return( JS_TRUE );
}
CBaseEntity* v = g_EntityTemplateCollection.getTemplate( templateName );
if( !v )
{
*rval = JSVAL_NULL;
JS_ReportError( context, "No such template: %s", (const char*)templateName );
return( JS_TRUE );
}
JSObject* baseEntity = JS_NewObject( context, &JSI_BaseEntity::JSI_class, NULL, NULL );
@@ -127,6 +149,166 @@ JSBool getEntityTemplate( JSContext* context, JSObject* globalObject, unsigned i
return( JS_TRUE );
}
JSBool setTimeout( JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval )
{
assert( argc >= 2 );
size_t delay;
try
{
delay = g_ScriptingHost.ValueToInt( argv[1] );
}
catch( ... )
{
JS_ReportError( context, "Invalid timer parameters" );
return( JS_TRUE );
}
switch( JS_TypeOfValue( context, argv[0] ) )
{
case JSTYPE_STRING:
{
CStr16 fragment = g_ScriptingHost.ValueToUCString( argv[0] );
g_Scheduler.pushTime( delay, fragment, JS_GetScopeChain( context ) );
return( JS_TRUE );
}
case JSTYPE_FUNCTION:
{
JSFunction* fn = JS_ValueToFunction( context, argv[0] );
g_Scheduler.pushTime( delay, fn, JS_GetScopeChain( context ) );
return( JS_TRUE );
}
default:
JS_ReportError( context, "Invalid timer script" );
return( JS_TRUE );
}
}
JSBool setInterval( JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval )
{
assert( argc >= 2 );
size_t first, interval;
try
{
first = g_ScriptingHost.ValueToInt( argv[1] );
if( argc == 3 )
{
// toDo, first, interval
interval = g_ScriptingHost.ValueToInt( argv[2] );
}
else
{
// toDo, interval (first = interval)
interval = first;
}
}
catch( ... )
{
JS_ReportError( context, "Invalid timer parameters" );
return( JS_TRUE );
}
switch( JS_TypeOfValue( context, argv[0] ) )
{
case JSTYPE_STRING:
{
CStr16 fragment = g_ScriptingHost.ValueToUCString( argv[0] );
g_Scheduler.pushInterval( first, interval, fragment, JS_GetScopeChain( context ) );
return( JS_TRUE );
}
case JSTYPE_FUNCTION:
{
JSFunction* fn = JS_ValueToFunction( context, argv[0] );
g_Scheduler.pushInterval( first, interval, fn, JS_GetScopeChain( context ) );
return( JS_TRUE );
}
default:
JS_ReportError( context, "Invalid timer script" );
return( JS_TRUE );
}
}
JSBool cancelInterval( JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval )
{
g_Scheduler.m_abortInterval = true;
return( JS_TRUE );
}
JSBool getSelected( JSContext* context, JSObject* globalObject, jsval id, jsval* vp )
{
if( g_Selection.m_selected.size() )
{
JSObject* entity = JS_NewObject( context, &JSI_Entity::JSI_class, NULL, NULL );
JS_SetPrivate( context, entity, new HEntity( g_Selection.m_selected[0]->me ) );
*vp = OBJECT_TO_JSVAL( entity );
return( JS_TRUE );
}
else
{
*vp = JSVAL_NULL;
return( JS_TRUE );
}
}
JSBool setSelected( JSContext* context, JSObject* globalObject, jsval id, jsval* vp )
{
g_Selection.clearSelection();
JSObject* selection = JSVAL_TO_OBJECT( *vp );
if( !JSVAL_IS_NULL( *vp ) && JSVAL_IS_OBJECT( *vp ) && ( JS_GetClass( selection ) == &JSI_Entity::JSI_class ) )
{
HEntity* entity = (HEntity*)JS_GetPrivate( context, selection );
g_Selection.addSelection( *entity );
}
else
JS_ReportError( context, "[Entity] Invalid reference" );
return( JS_TRUE );
}
JSBool getSelection( JSContext* context, JSObject* globalObject, jsval id, jsval* vp )
{
JSObject* selectionArray = JS_NewArrayObject( context, 0, NULL );
std::vector<CEntity*>::iterator it; int i;
for( it = g_Selection.m_selected.begin(), i = 0; it < g_Selection.m_selected.end(); it++, i++ )
{
JSObject* entity = JS_NewObject( context, &JSI_Entity::JSI_class, NULL, NULL );
JS_SetPrivate( context, entity, new HEntity( (*it)->me ) );
jsval j = OBJECT_TO_JSVAL( entity );
JS_SetElement( context, selectionArray, i, &j );
}
*vp = OBJECT_TO_JSVAL( selectionArray );
return( JS_TRUE );
}
JSBool setSelection( JSContext* context, JSObject* globalObject, jsval id, jsval* vp )
{
g_Selection.clearSelection();
JSObject* selectionArray = JSVAL_TO_OBJECT( *vp );
if( JSVAL_IS_NULL( *vp ) || !JSVAL_IS_OBJECT( *vp ) || !JS_IsArrayObject( context, selectionArray ) )
{
JS_ReportError( context, "Not an array" );
return( JS_TRUE );
}
jsuint selectionCount;
if( !JS_GetArrayLength( context, selectionArray, &selectionCount ) )
{
JS_ReportError( context, "Not an array" );
return( JS_TRUE );
}
for( jsuint i = 0; i < selectionCount; i++ )
{
jsval entry;
JS_GetElement( context, selectionArray, i, &entry );
JSObject* selection = JSVAL_TO_OBJECT( entry );
if( !JSVAL_IS_NULL( entry ) && JSVAL_IS_OBJECT( entry ) && ( JS_GetClass( selection ) == &JSI_Entity::JSI_class ) )
{
HEntity* entity = (HEntity*)JS_GetPrivate( context, JSVAL_TO_OBJECT( entry ) );
g_Selection.addSelection( &(**entity) );
}
else
JS_ReportError( context, "[Entity] Invalid reference" );
}
return( JS_TRUE );
}
JSBool getGUIGlobal( JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval )
{
*rval = OBJECT_TO_JSVAL( g_GUI.GetScriptObject() );