2005-07-20 02:57:17 +02:00
|
|
|
// This module defines the table of all functions callable from JS.
|
|
|
|
// it's required by the interpreter; we make use of the opportunity to
|
|
|
|
// document them all in one spot. we thus obviate having to dig through
|
|
|
|
// all the other headers. most of the functions are implemented here;
|
|
|
|
// as for the rest, we only link to their docs (duplication is bad).
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
#include "ScriptGlue.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "JSConversions.h"
|
|
|
|
#include "GameEvents.h"
|
2007-02-10 04:09:52 +01:00
|
|
|
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "graphics/GameView.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "graphics/LightEnv.h"
|
|
|
|
#include "graphics/MapWriter.h"
|
2007-02-10 04:09:52 +01:00
|
|
|
#include "graphics/Unit.h"
|
|
|
|
#include "graphics/UnitManager.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "graphics/scripting/JSInterface_Camera.h"
|
|
|
|
#include "graphics/scripting/JSInterface_LightEnv.h"
|
|
|
|
#include "gui/CGUI.h"
|
|
|
|
#include "lib/timer.h"
|
|
|
|
#include "maths/scripting/JSInterface_Vector3D.h"
|
2007-02-10 04:09:52 +01:00
|
|
|
#include "network/Client.h"
|
|
|
|
#include "network/Server.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "ps/CConsole.h"
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/CStr.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Game.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "ps/GameSetup/GameSetup.h"
|
2007-02-10 04:09:52 +01:00
|
|
|
#include "ps/Hotkey.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "ps/Interact.h"
|
2007-02-10 04:09:52 +01:00
|
|
|
#include "ps/ProfileViewer.h"
|
2004-08-21 13:45:01 +02:00
|
|
|
#include "ps/i18n.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/scripting/JSCollection.h"
|
|
|
|
#include "ps/scripting/JSInterface_Console.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "ps/scripting/JSInterface_Selection.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/scripting/JSInterface_VFS.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "renderer/Renderer.h"
|
2006-05-16 06:41:37 +02:00
|
|
|
#include "renderer/SkyManager.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "renderer/WaterManager.h"
|
|
|
|
#include "simulation/Entity.h"
|
|
|
|
#include "simulation/EntityFormation.h"
|
|
|
|
#include "simulation/EntityHandles.h"
|
|
|
|
#include "simulation/EntityManager.h"
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
|
|
|
#include "simulation/EntityTemplate.h"
|
2007-02-10 04:09:52 +01:00
|
|
|
#include "simulation/EntityTemplateCollection.h"
|
2006-03-31 05:30:34 +02:00
|
|
|
#include "simulation/FormationManager.h"
|
2006-06-04 01:02:37 +02:00
|
|
|
#include "simulation/LOSManager.h"
|
|
|
|
#include "simulation/Scheduler.h"
|
|
|
|
#include "simulation/Simulation.h"
|
2007-02-10 04:09:52 +01:00
|
|
|
#include "simulation/TechnologyCollection.h"
|
|
|
|
#include "simulation/TriggerManager.h"
|
2006-03-31 05:30:34 +02:00
|
|
|
|
2005-01-10 23:13:18 +01:00
|
|
|
#ifndef NO_GUI
|
|
|
|
# include "gui/scripting/JSInterface_IGUIObject.h"
|
|
|
|
#endif
|
|
|
|
|
2005-10-19 05:02:51 +02:00
|
|
|
extern bool g_TerrainModified;
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// rationale: the function table is now at the end of the source file to
|
|
|
|
// avoid the need for forward declarations for every function.
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// all normal function wrappers have the following signature:
|
|
|
|
// JSBool func(JSContext* cx, JSObject* globalObject, uint argc, jsval* argv, jsval* rval);
|
|
|
|
// all property accessors have the following signature:
|
|
|
|
// JSBool accessor(JSContext* cx, JSObject* globalObject, jsval id, jsval* vp);
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Output
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Write values to the log file.
|
|
|
|
// params: any number of any type.
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - Each argument is converted to a string and then written to the log.
|
|
|
|
// - Output is in NORMAL style (see LOG).
|
2006-07-26 15:10:13 +02:00
|
|
|
JSBool WriteLog(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
CStr logMessage;
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
for (int i = 0; i < (int)argc; i++)
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
try
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
CStr arg = g_ScriptingHost.ValueToString( argv[i] );
|
|
|
|
logMessage += arg;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
// Do nothing.
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// We should perhaps unicodify (?) the logger at some point.
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-08-15 22:57:31 +02:00
|
|
|
LOG( NORMAL, "script", logMessage );
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2006-07-26 15:10:13 +02:00
|
|
|
*rval = JSVAL_TRUE;
|
2004-08-03 01:14:54 +02:00
|
|
|
return JS_TRUE;
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Entity
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2005-07-20 06:53:58 +02:00
|
|
|
// Retrieve the entity currently occupying the specified handle.
|
2005-07-20 02:57:17 +02:00
|
|
|
// params: handle [int]
|
|
|
|
// returns: entity
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetEntityByUnitID( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
|
2007-01-30 04:50:28 +01:00
|
|
|
int uid;
|
2004-06-11 00:24:03 +02:00
|
|
|
try
|
|
|
|
{
|
2007-01-30 04:50:28 +01:00
|
|
|
uid = ToPrimitive<int>( argv[0] );
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|
2004-07-17 19:09:33 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2007-01-30 04:50:28 +01:00
|
|
|
JS_ReportError( cx, "Invalid parameter" );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2007-01-30 04:50:28 +01:00
|
|
|
|
|
|
|
CUnit* unit = g_Game->GetWorld()->GetUnitManager().FindByID( uid );
|
|
|
|
if( !unit || !unit->GetEntity() )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2007-01-30 04:50:28 +01:00
|
|
|
*rval = JSVAL_NULL;
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2007-01-30 04:50:28 +01:00
|
|
|
*rval = OBJECT_TO_JSVAL( unit->GetEntity()->GetScript() );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Look up an EntityTemplate by name.
|
|
|
|
// params: template name [wstring]
|
|
|
|
// returns: entity template object
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetEntityTemplate( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAM_RANGE(1, 2);
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
CStrW templateName;
|
2006-05-14 00:11:46 +02:00
|
|
|
CPlayer* player = 0;
|
|
|
|
|
2004-06-11 00:24:03 +02:00
|
|
|
try
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
templateName = g_ScriptingHost.ValueToUCString( argv[0] );
|
2006-05-14 00:11:46 +02:00
|
|
|
if( argc == 2 )
|
|
|
|
{
|
|
|
|
player = ToNative<CPlayer>( argv[1] );
|
|
|
|
}
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|
2004-07-17 19:09:33 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2005-07-20 02:57:17 +02:00
|
|
|
JS_ReportError( cx, "Invalid template identifier" );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2006-05-14 00:11:46 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
CEntityTemplate* v = g_EntityTemplateCollection.GetTemplate( templateName, player );
|
2004-06-11 00:24:03 +02:00
|
|
|
if( !v )
|
|
|
|
{
|
2007-01-17 04:25:20 +01:00
|
|
|
JS_ReportError( cx, "No such template: %s", CStr(templateName).c_str() );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
*rval = OBJECT_TO_JSVAL( v->GetScript() );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2007-01-17 00:22:25 +01:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetPlayerUnitCount( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2007-01-17 00:22:25 +01:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_PARAMS(2);
|
|
|
|
int unitCount, playerNum = ToPrimitive<int>( argv[0] );
|
|
|
|
CStrW unitName = ToPrimitive<CStrW>( argv[1] );
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
unitCount = g_EntityManager.GetPlayerUnitCount((size_t)playerNum, unitName);
|
2007-04-27 05:05:26 +02:00
|
|
|
*rval = ToJSVal( unitCount );
|
2007-01-17 00:22:25 +01:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
//Used to create net messages for formations--msgList.front() is the original message. see IssueCommand
|
2006-07-06 05:17:44 +02:00
|
|
|
void CreateFormationMessage( std::vector<CNetMessage*>& msgList, CNetMessage* msg, CEntityList& formation )
|
2006-03-31 05:30:34 +02:00
|
|
|
{
|
2006-07-06 05:17:44 +02:00
|
|
|
CNetMessage* retMsg;
|
|
|
|
const int type = msg->GetType();
|
|
|
|
|
|
|
|
if ( type == NMT_Goto )
|
2006-03-31 05:30:34 +02:00
|
|
|
{
|
|
|
|
//formationEnt->GetFormation()->BaseToMovement();
|
2006-07-06 05:17:44 +02:00
|
|
|
CGoto* tmp = static_cast<CGoto*>(msg);
|
|
|
|
retMsg = CNetMessage::CreatePositionMessage( formation, NMT_FormationGoto,
|
|
|
|
CVector2D(tmp->m_TargetX, tmp->m_TargetY) );
|
|
|
|
}
|
|
|
|
else if( type == NMT_Run )
|
|
|
|
{
|
|
|
|
CGoto* tmp = static_cast<CGoto*>(msg);
|
|
|
|
retMsg = CNetMessage::CreatePositionMessage( formation, NMT_FormationGoto,
|
|
|
|
CVector2D(tmp->m_TargetX, tmp->m_TargetY) );
|
2006-03-31 05:30:34 +02:00
|
|
|
}
|
|
|
|
else if ( type == NMT_Generic )
|
2006-07-06 05:17:44 +02:00
|
|
|
{
|
|
|
|
CGeneric* tmp = static_cast<CGeneric*>(msg);
|
|
|
|
retMsg = CNetMessage::CreateEntityIntMessage(formation, NMT_FormationGeneric,
|
|
|
|
tmp->m_Target, tmp->m_Action);
|
|
|
|
}
|
2006-03-31 05:30:34 +02:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
2006-07-06 05:17:44 +02:00
|
|
|
msgList.push_back(retMsg);
|
2006-03-31 05:30:34 +02:00
|
|
|
}
|
2005-01-18 01:46:18 +01:00
|
|
|
|
2005-07-20 06:53:58 +02:00
|
|
|
// Issue a command (network message) to an entity or collection.
|
|
|
|
// params: either an entity- or entity collection object, message ID [int],
|
|
|
|
// any further params needed by CNetMessage::CommandFromJSArgs
|
|
|
|
// returns: command in serialized form [string]
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool IssueCommand( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2005-07-20 06:53:58 +02:00
|
|
|
{
|
2006-08-25 06:24:06 +02:00
|
|
|
// at least one for target object, one for isQueued, and then 1 or more for the CommandFromJSArgs
|
|
|
|
JSU_REQUIRE_MIN_PARAMS(3);
|
2006-07-26 15:10:13 +02:00
|
|
|
|
2006-08-25 06:24:06 +02:00
|
|
|
JSU_ASSERT(JSVAL_IS_OBJECT(argv[0]), "Argument 0 must be an entity collection.");
|
2005-07-20 06:53:58 +02:00
|
|
|
*rval = JSVAL_NULL;
|
2006-03-31 05:30:34 +02:00
|
|
|
|
2006-07-06 05:17:44 +02:00
|
|
|
CEntityList entities, msgEntities;
|
2005-07-20 06:53:58 +02:00
|
|
|
|
2007-06-09 00:56:01 +02:00
|
|
|
if (JS_GetClass(cx, JSVAL_TO_OBJECT(argv[0])) == &CEntity::JSI_class)
|
2005-07-20 06:53:58 +02:00
|
|
|
entities.push_back( (ToNative<CEntity>(argv[0])) ->me);
|
2006-01-07 02:04:26 +01:00
|
|
|
else
|
2005-07-20 06:53:58 +02:00
|
|
|
entities = *EntityCollection::RetrieveSet(cx, JSVAL_TO_OBJECT(argv[0]));
|
2007-06-04 09:41:05 +02:00
|
|
|
|
2006-07-06 05:17:44 +02:00
|
|
|
std::map<int, CEntityList> entityStore;
|
2006-08-25 06:24:06 +02:00
|
|
|
|
|
|
|
bool isQueued = ToPrimitive<bool>(argv[1]);
|
2007-06-04 09:41:05 +02:00
|
|
|
|
2006-04-09 00:34:54 +02:00
|
|
|
//Destroy old notifiers if we're explicitly being reassigned
|
2006-02-13 04:31:45 +01:00
|
|
|
for ( size_t i=0; i < entities.size(); i++)
|
|
|
|
{
|
2006-07-12 18:18:31 +02:00
|
|
|
if ( entities[i]->entf_get(ENTF_DESTROY_NOTIFIERS))
|
2006-04-09 00:34:54 +02:00
|
|
|
entities[i]->DestroyAllNotifiers();
|
2006-02-13 04:31:45 +01:00
|
|
|
}
|
2007-06-04 09:41:05 +02:00
|
|
|
|
2006-03-31 05:30:34 +02:00
|
|
|
std::vector<CNetMessage*> messages;
|
|
|
|
|
|
|
|
//Generate messages for formations
|
|
|
|
for (size_t i=0; i < entities.size(); i++ )
|
2005-07-20 06:53:58 +02:00
|
|
|
{
|
2006-03-31 05:30:34 +02:00
|
|
|
if ( entities[i]->m_formation >= 0)
|
|
|
|
{
|
|
|
|
CEntityFormation* formation = entities[i]->GetFormation();
|
|
|
|
bool duplicate = formation->IsDuplication();
|
|
|
|
|
|
|
|
if ( formation->IsLocked() && !duplicate)
|
|
|
|
{
|
|
|
|
formation->SelectAllUnits();
|
2006-07-06 05:17:44 +02:00
|
|
|
entityStore[entities[i]->m_formation] = formation->GetEntityList();
|
2006-03-31 05:30:34 +02:00
|
|
|
formation->SetDuplication(true);
|
2006-06-06 08:31:17 +02:00
|
|
|
}
|
2006-03-31 05:30:34 +02:00
|
|
|
}
|
2006-07-06 05:17:44 +02:00
|
|
|
else
|
|
|
|
msgEntities.push_back( entities[i] );
|
|
|
|
}
|
2006-08-25 06:24:06 +02:00
|
|
|
CNetMessage* msg = CNetMessage::CommandFromJSArgs(msgEntities, cx, argc-2, argv+2, isQueued);
|
2006-07-06 05:17:44 +02:00
|
|
|
if (!msg)
|
|
|
|
{
|
|
|
|
delete msg;
|
2006-07-10 01:12:37 +02:00
|
|
|
return JS_TRUE;
|
2006-03-31 05:30:34 +02:00
|
|
|
}
|
2006-07-06 05:17:44 +02:00
|
|
|
messages.push_back(msg);
|
|
|
|
|
|
|
|
for ( std::map<int, CEntityList>::iterator it=entityStore.begin(); it!=entityStore.end(); it++)
|
|
|
|
CreateFormationMessage(messages, msg, it->second);
|
2006-03-31 05:30:34 +02:00
|
|
|
|
|
|
|
for ( std::vector<CNetMessage*>::iterator it=messages.begin(); it != messages.end(); it++ )
|
|
|
|
{
|
2007-06-04 09:41:05 +02:00
|
|
|
g_Console->InsertMessage(L"IssueCommand: %hs", (*it)->GetString().c_str());
|
2006-03-31 05:30:34 +02:00
|
|
|
*rval = g_ScriptingHost.UCStringToValue((*it)->GetString());
|
2007-06-04 09:41:05 +02:00
|
|
|
g_Game->GetSimulation()->QueueLocalCommand(*it);
|
2005-07-20 06:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Get the state of a given hotkey (from the hotkeys file)
|
|
|
|
JSBool isOrderQueued( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
|
|
|
{
|
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
|
|
|
|
|
|
|
*rval = ToJSVal(hotkeys[HOTKEY_ORDER_QUEUE]);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// formations
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
JSBool CreateEntityFormation( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2006-04-09 00:34:54 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(2);
|
2006-04-09 00:34:54 +02:00
|
|
|
|
|
|
|
CEntityList entities = *EntityCollection::RetrieveSet(cx, JSVAL_TO_OBJECT(argv[0]));
|
|
|
|
CStrW name = ToPrimitive<CStrW>( argv[1] );
|
|
|
|
g_FormationManager.CreateFormation( entities, name );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
JSBool RemoveFromFormation( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2006-04-09 00:34:54 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2006-04-09 00:34:54 +02:00
|
|
|
|
|
|
|
CEntityList entities;
|
2007-06-09 00:56:01 +02:00
|
|
|
if (JS_GetClass(cx, JSVAL_TO_OBJECT(argv[0])) == &CEntity::JSI_class)
|
2006-04-09 00:34:54 +02:00
|
|
|
entities.push_back( (ToNative<CEntity>(argv[0])) ->me);
|
|
|
|
else
|
|
|
|
entities = *EntityCollection::RetrieveSet(cx, JSVAL_TO_OBJECT(argv[0]));
|
|
|
|
|
|
|
|
*rval = g_FormationManager.RemoveUnitList(entities) ? JS_TRUE : JS_FALSE;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
JSBool LockEntityFormation( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2006-04-09 00:34:54 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2006-04-09 00:34:54 +02:00
|
|
|
|
|
|
|
CEntity* entity = ToNative<CEntity>( argv[0] );
|
|
|
|
entity->GetFormation()->SetLock( ToPrimitive<bool>( argv[1] ) );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
JSBool IsFormationLocked( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2006-04-09 00:34:54 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2006-04-09 00:34:54 +02:00
|
|
|
|
|
|
|
CEntity* entity = ToNative<CEntity>( argv[0] );
|
|
|
|
*rval = entity->GetFormation()->IsLocked() ? JS_TRUE : JS_FALSE;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2006-08-25 06:24:06 +02:00
|
|
|
|
|
|
|
|
2006-06-06 08:31:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Techs
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetTechnology( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2006-06-06 08:31:17 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(2);
|
2006-06-06 08:31:17 +02:00
|
|
|
|
2006-07-18 06:17:46 +02:00
|
|
|
CStrW name;
|
|
|
|
CPlayer* player;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
name = g_ScriptingHost.ValueToUCString( argv[0] );
|
|
|
|
player = ToNative<CPlayer>( argv[1] );
|
|
|
|
}
|
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_ReportError( cx, "Invalid parameters for GetTechnology (expected name and player)" );
|
2006-07-18 06:17:46 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2006-06-07 03:04:41 +02:00
|
|
|
*rval = JSVAL_NULL;
|
2006-06-06 08:31:17 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
CTechnology* tech = g_TechnologyCollection.GetTechnology( name, player );
|
2006-06-10 01:07:11 +02:00
|
|
|
if ( tech )
|
|
|
|
*rval = ToJSVal( tech );
|
2006-06-06 08:31:17 +02:00
|
|
|
else
|
2007-05-02 14:07:08 +02:00
|
|
|
g_Console->InsertMessage( L"Warning: Invalid tech template name \"%ls\" passed for GetTechnology()", name.c_str() );
|
2006-06-06 08:31:17 +02:00
|
|
|
|
2006-06-10 05:05:16 +02:00
|
|
|
return JS_TRUE;
|
2006-06-06 08:31:17 +02:00
|
|
|
}
|
2005-07-20 06:53:58 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Events
|
|
|
|
//-----------------------------------------------------------------------------
|
2005-01-18 01:46:18 +01:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Register a global handler for the specified DOM event.
|
|
|
|
// params: event type name [wstring], handler [fragment or function]
|
|
|
|
// returns: whether it was actually newly registered [bool]
|
2005-08-09 18:02:15 +02:00
|
|
|
JSBool AddGlobalHandler( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2005-03-29 00:13:47 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(2);
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = BOOLEAN_TO_JSVAL( g_JSGameEvents.AddHandlerJS( cx, argc, argv ) );
|
2005-03-29 00:13:47 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Remove a previously registered global handler for the specified DOM event.
|
|
|
|
// params: event type name [wstring], handler [fragment or function]
|
|
|
|
// returns: whether it was successfully removed [bool]
|
2005-08-09 18:02:15 +02:00
|
|
|
JSBool RemoveGlobalHandler( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2005-04-22 09:12:55 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(2);
|
2005-04-22 09:12:55 +02:00
|
|
|
|
|
|
|
*rval = BOOLEAN_TO_JSVAL( g_JSGameEvents.RemoveHandlerJS( cx, argc, argv ) );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2005-03-29 00:13:47 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Timer
|
|
|
|
//-----------------------------------------------------------------------------
|
2005-03-29 00:13:47 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Request a callback be executed after the specified delay.
|
|
|
|
// params: callback [fragment or function], delay in milliseconds [int]
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - Scripts and functions registered this way are called on the first
|
|
|
|
// simulation frame after the specified period has elapsed. If this causes
|
|
|
|
// multiple segments of code to be executed in the same frame,
|
|
|
|
// relative timing is maintained. Delays of 0 milliseconds cause code to be
|
|
|
|
// executed on the following simulation frame. If more than one script or
|
|
|
|
// function is scheduled to execute in the same millisecond, the order of
|
|
|
|
// execution is undefined. Code is scheduled in simulation time, and is
|
|
|
|
// therefore suspended while the game is paused or frozen. Granularity of
|
|
|
|
// timing is also limited to 1/(Simulation frame rate); currently 100ms.
|
|
|
|
// The called function or script executes in the same scope as the
|
2007-05-02 14:07:08 +02:00
|
|
|
// code that called SetTimeout (amongst other things, the
|
2005-07-20 02:57:17 +02:00
|
|
|
// 'this' reference is usually maintained)
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetTimeout( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAM_RANGE(2, 3);
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
size_t delay;
|
|
|
|
try
|
|
|
|
{
|
2005-08-09 18:02:15 +02:00
|
|
|
delay = ToPrimitive<int>( argv[1] );
|
2004-07-20 21:30:35 +02:00
|
|
|
}
|
2005-07-20 02:57:17 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2005-07-20 02:57:17 +02:00
|
|
|
JS_ReportError( cx, "Invalid timer parameters" );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2005-12-18 08:33:16 +01:00
|
|
|
JSObject* scope;
|
|
|
|
if( argc == 3 )
|
|
|
|
{
|
|
|
|
if( !JSVAL_IS_OBJECT( argv[2] ) )
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid timer parameters" );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
scope = JSVAL_TO_OBJECT( argv[2] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scope = JS_GetScopeChain( cx );
|
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
switch( JS_TypeOfValue( cx, argv[0] ) )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
{
|
2004-09-03 16:08:12 +02:00
|
|
|
CStrW fragment = g_ScriptingHost.ValueToUCString( argv[0] );
|
2007-05-02 14:07:08 +02:00
|
|
|
int id = g_Scheduler.PushTime( delay, fragment, scope );
|
2005-12-18 08:33:16 +01:00
|
|
|
*rval = INT_TO_JSVAL( id );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
{
|
2005-07-20 02:57:17 +02:00
|
|
|
JSFunction* fn = JS_ValueToFunction( cx, argv[0] );
|
2007-05-02 14:07:08 +02:00
|
|
|
int id = g_Scheduler.PushTime( delay, fn, scope );
|
2005-12-18 08:33:16 +01:00
|
|
|
*rval = INT_TO_JSVAL( id );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
default:
|
2005-07-20 02:57:17 +02:00
|
|
|
JS_ReportError( cx, "Invalid timer script" );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Request a callback be executed periodically.
|
|
|
|
// params: callback [fragment or function], initial delay in ms [int], period in ms [int]
|
|
|
|
// OR callback [fragment or function], period in ms [int] (initial delay = period)
|
|
|
|
// returns:
|
|
|
|
// notes:
|
2007-05-02 14:07:08 +02:00
|
|
|
// - SetTimeout's notes apply here as well.
|
|
|
|
JSBool SetInterval( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAM_RANGE(2, 3);
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
size_t first, interval;
|
|
|
|
try
|
|
|
|
{
|
2005-08-09 18:02:15 +02:00
|
|
|
first = ToPrimitive<int>( argv[1] );
|
2004-07-20 21:30:35 +02:00
|
|
|
if( argc == 3 )
|
|
|
|
{
|
|
|
|
// toDo, first, interval
|
2005-08-09 18:02:15 +02:00
|
|
|
interval = ToPrimitive<int>( argv[2] );
|
2004-07-20 21:30:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// toDo, interval (first = interval)
|
|
|
|
interval = first;
|
|
|
|
}
|
|
|
|
}
|
2005-07-20 02:57:17 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2005-07-20 02:57:17 +02:00
|
|
|
JS_ReportError( cx, "Invalid timer parameters" );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
switch( JS_TypeOfValue( cx, argv[0] ) )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
{
|
2004-09-03 16:08:12 +02:00
|
|
|
CStrW fragment = g_ScriptingHost.ValueToUCString( argv[0] );
|
2007-05-02 14:07:08 +02:00
|
|
|
int id = g_Scheduler.PushInterval( first, interval, fragment, JS_GetScopeChain( cx ) );
|
2005-12-18 08:33:16 +01:00
|
|
|
*rval = INT_TO_JSVAL( id );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
{
|
2005-07-20 02:57:17 +02:00
|
|
|
JSFunction* fn = JS_ValueToFunction( cx, argv[0] );
|
2007-05-02 14:07:08 +02:00
|
|
|
int id = g_Scheduler.PushInterval( first, interval, fn, JS_GetScopeChain( cx ) );
|
2005-12-18 08:33:16 +01:00
|
|
|
*rval = INT_TO_JSVAL( id );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
default:
|
2005-07-20 02:57:17 +02:00
|
|
|
JS_ReportError( cx, "Invalid timer script" );
|
2004-07-20 21:30:35 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
// Cause all periodic functions registered via SetInterval to
|
2005-07-20 02:57:17 +02:00
|
|
|
// no longer be called.
|
|
|
|
// params:
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - Execution continues until the end of the triggered function or
|
|
|
|
// script fragment, but is not triggered again.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool CancelInterval( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
g_Scheduler.m_abortInterval = true;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
// Cause the scheduled task (timeout or interval) with the given ID to
|
2005-12-18 08:33:16 +01:00
|
|
|
// no longer be called.
|
|
|
|
// params:
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - Execution continues until the end of the triggered function or
|
|
|
|
// script fragment, but is not triggered again.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool CancelTimer( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2005-12-18 08:33:16 +01:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2005-12-18 08:33:16 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int id = ToPrimitive<int>( argv[0] );
|
2007-05-02 14:07:08 +02:00
|
|
|
g_Scheduler.CancelTask( id );
|
2005-12-18 08:33:16 +01:00
|
|
|
}
|
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid ID parameter" );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2006-07-30 02:24:04 +02:00
|
|
|
|
2006-06-06 08:31:17 +02:00
|
|
|
//Set the simulation rate scalar-time becomes time * SimRate.
|
|
|
|
//Params: rate [float] : sets SimRate
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetSimRate(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2006-06-06 08:31:17 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2006-06-06 08:31:17 +02:00
|
|
|
|
|
|
|
g_Game->SetSimRate( ToPrimitive<float>(argv[0]) );
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2005-12-18 08:33:16 +01:00
|
|
|
|
2006-07-30 02:24:04 +02:00
|
|
|
//Generate a random float in [0, 1) using the simulation's random generator
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SimRand(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2006-07-30 02:24:04 +02:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
|
|
|
|
|
|
|
*rval = ToJSVal( g_Game->GetSimulation()->RandFloat() );
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Generate a random float int between 0 and the given number - 1 using the simulation's RNG
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SimRandInt(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2006-07-30 02:24:04 +02:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2007-05-02 14:07:08 +02:00
|
|
|
JSU_ASSERT(JSVAL_IS_INT(argv[0]), "SimRandInt(): first parameter must be an int");
|
2006-07-30 02:24:04 +02:00
|
|
|
|
|
|
|
*rval = ToJSVal( g_Game->GetSimulation()->RandInt(ToPrimitive<int>(argv[0])) );
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
// Script profiling functions: Begin timing a piece of code with StartJsTimer(num)
|
|
|
|
// and stop timing with StopJsTimer(num). The results will be printed to stdout
|
2006-07-30 02:24:04 +02:00
|
|
|
// when the game exits.
|
2005-12-18 08:33:16 +01:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
static const uint MAX_JS_TIMERS = 20;
|
|
|
|
static Timer js_timer;
|
|
|
|
static TimerUnit js_start_times[MAX_JS_TIMERS];
|
|
|
|
static TimerUnit js_timer_overhead;
|
|
|
|
static TimerClient js_timer_clients[MAX_JS_TIMERS];
|
|
|
|
static char js_timer_descriptions_buf[MAX_JS_TIMERS * 12]; // depends on MAX_JS_TIMERS and format string below
|
2006-07-13 22:09:20 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
static void InitJsTimers()
|
2006-07-13 22:09:20 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
char* pos = js_timer_descriptions_buf;
|
|
|
|
for(uint i = 0; i < MAX_JS_TIMERS; i++)
|
2006-07-13 22:09:20 +02:00
|
|
|
{
|
|
|
|
const char* description = pos;
|
2007-05-02 14:07:08 +02:00
|
|
|
pos += sprintf(pos, "js_timer %d", i)+1;
|
|
|
|
timer_add_client(&js_timer_clients[i], description);
|
2006-07-13 22:09:20 +02:00
|
|
|
}
|
2006-07-13 23:04:06 +02:00
|
|
|
|
2006-07-14 16:00:00 +02:00
|
|
|
// call several times to get a good approximation of 'hot' performance.
|
|
|
|
// note: don't use a separate timer slot to warm up and then judge
|
|
|
|
// overhead from another: that causes worse results (probably some
|
|
|
|
// caching effects inside JS, but I don't entirely understand why).
|
2006-07-13 23:04:06 +02:00
|
|
|
static const char* calibration_script =
|
|
|
|
"startXTimer(0);\n"
|
2006-07-14 16:00:00 +02:00
|
|
|
"stopXTimer (0);\n"
|
2006-07-13 23:04:06 +02:00
|
|
|
"startXTimer(0);\n"
|
2006-07-14 16:00:00 +02:00
|
|
|
"stopXTimer (0);\n"
|
2006-07-13 23:04:06 +02:00
|
|
|
"startXTimer(0);\n"
|
2006-07-14 16:00:00 +02:00
|
|
|
"stopXTimer (0);\n"
|
|
|
|
"startXTimer(0);\n"
|
|
|
|
"stopXTimer (0);\n"
|
2006-07-13 23:04:06 +02:00
|
|
|
"\n";
|
|
|
|
g_ScriptingHost.RunMemScript(calibration_script, strlen(calibration_script));
|
2007-05-02 14:07:08 +02:00
|
|
|
js_timer_overhead = js_timer_clients[0].sum/4;
|
2006-07-13 22:09:20 +02:00
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool StartJsTimer(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2006-07-13 22:09:20 +02:00
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
ONCE(InitJsTimers());
|
2006-07-13 22:09:20 +02:00
|
|
|
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2006-07-13 22:09:20 +02:00
|
|
|
uint slot = ToPrimitive<uint>(argv[0]);
|
2007-05-02 14:07:08 +02:00
|
|
|
if (slot >= MAX_JS_TIMERS)
|
2006-11-07 22:03:13 +01:00
|
|
|
return JS_FALSE;
|
2006-07-13 22:09:20 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
debug_assert(js_start_times[slot] == 0);
|
|
|
|
js_start_times[slot] = js_timer.get_timestamp();
|
2006-11-07 22:03:13 +01:00
|
|
|
return JS_TRUE;
|
2006-07-13 22:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool StopJsTimer(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2006-07-13 22:09:20 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2006-07-13 22:09:20 +02:00
|
|
|
uint slot = ToPrimitive<uint>(argv[0]);
|
2007-05-02 14:07:08 +02:00
|
|
|
if (slot >= MAX_JS_TIMERS)
|
2006-11-07 22:03:13 +01:00
|
|
|
return JS_FALSE;
|
2006-07-13 22:09:20 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
debug_assert(js_start_times[slot] != 0);
|
|
|
|
TimerUnit dt = js_timer.get_timestamp() - js_start_times[slot] - js_timer_overhead;
|
|
|
|
js_start_times[slot] = 0;
|
|
|
|
timer_bill_client(&js_timer_clients[slot], dt);
|
2006-11-07 22:03:13 +01:00
|
|
|
return JS_TRUE;
|
2006-07-13 22:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Game Setup
|
|
|
|
//-----------------------------------------------------------------------------
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Create a new network server object.
|
|
|
|
// params:
|
|
|
|
// returns: net server object
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool CreateServer(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-07-11 18:22:35 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2004-07-11 18:22:35 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
if( !g_Game )
|
|
|
|
g_Game = new CGame();
|
|
|
|
if( !g_NetServer )
|
|
|
|
g_NetServer = new CNetServer(g_Game, &g_GameAttributes);
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = OBJECT_TO_JSVAL(g_NetServer->GetScript());
|
2004-07-07 20:27:25 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-07-09 14:44:12 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Create a new network client object.
|
|
|
|
// params:
|
|
|
|
// returns: net client object
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool CreateClient(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-08-05 15:07:51 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2004-08-16 17:19:17 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
if( !g_Game )
|
|
|
|
g_Game = new CGame();
|
|
|
|
if( !g_NetClient )
|
|
|
|
g_NetClient = new CNetClient(g_Game, &g_GameAttributes);
|
2004-08-16 17:19:17 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = OBJECT_TO_JSVAL(g_NetClient->GetScript());
|
|
|
|
return( JS_TRUE );
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Begin the process of starting a game.
|
|
|
|
// params:
|
|
|
|
// returns: success [bool]
|
|
|
|
// notes:
|
|
|
|
// - Performs necessary initialization while calling back into the
|
|
|
|
// main loop, so the game remains responsive to display+user input.
|
|
|
|
// - When complete, the engine calls the reallyStartGame JS function.
|
2007-05-02 14:07:08 +02:00
|
|
|
// TODO: Replace StartGame with Create(Game|Server|Client)/game.start() -
|
2005-07-20 02:57:17 +02:00
|
|
|
// after merging CGame and CGameAttributes
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool StartGame(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-02-02 18:03:37 +01:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = BOOLEAN_TO_JSVAL(JS_TRUE);
|
2005-02-02 18:03:37 +01:00
|
|
|
|
2005-01-17 05:52:02 +01:00
|
|
|
// Hosted MP Game
|
2007-06-04 09:41:05 +02:00
|
|
|
if (g_NetServer)
|
|
|
|
{
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = BOOLEAN_TO_JSVAL(g_NetServer->StartGame() == 0);
|
2007-06-04 09:41:05 +02:00
|
|
|
}
|
|
|
|
// Joined MP Game
|
2005-01-17 05:52:02 +01:00
|
|
|
else if (g_NetClient)
|
2004-09-21 18:10:46 +02:00
|
|
|
{
|
2007-06-04 09:41:05 +02:00
|
|
|
*rval = BOOLEAN_TO_JSVAL(g_NetClient->StartGame() == 0);
|
2004-09-21 18:10:46 +02:00
|
|
|
}
|
2005-01-17 05:52:02 +01:00
|
|
|
// Start an SP Game Session
|
2004-09-21 18:10:46 +02:00
|
|
|
else if (!g_Game)
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2005-07-20 02:57:17 +02:00
|
|
|
g_Game = new CGame();
|
2005-01-15 18:20:51 +01:00
|
|
|
PSRETURN ret = g_Game->StartGame(&g_GameAttributes);
|
|
|
|
if (ret != PSRETURN_OK)
|
|
|
|
{
|
|
|
|
// Failed to start the game - destroy it, and return false
|
|
|
|
|
|
|
|
delete g_Game;
|
|
|
|
g_Game = NULL;
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
|
|
|
|
return( JS_TRUE );
|
2005-01-15 18:20:51 +01:00
|
|
|
}
|
2004-08-05 15:07:51 +02:00
|
|
|
}
|
2007-06-04 09:41:05 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
|
|
|
|
}
|
2005-01-15 18:20:51 +01:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
return( JS_TRUE );
|
2004-08-05 15:07:51 +02:00
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Immediately ends the current game (if any).
|
|
|
|
// params:
|
|
|
|
// returns:
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool EndGame(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-08-05 15:07:51 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2004-08-05 15:07:51 +02:00
|
|
|
EndGame();
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2004-07-31 14:45:30 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetGameMode(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2007-04-27 05:05:26 +02:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
|
|
|
|
|
|
|
*rval = ToJSVal( g_GameAttributes.GetGameMode() );
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2004-08-21 13:45:01 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Internationalization
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2005-07-20 06:53:58 +02:00
|
|
|
// these remain here instead of in the i18n tree because they are
|
|
|
|
// really related to the engine's use of them, as opposed to i18n itself.
|
|
|
|
// contrariwise, translate() cannot be moved here because that would
|
|
|
|
// make i18n dependent on this code and therefore harder to reuse.
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Replaces the current language (locale) with a new one.
|
|
|
|
// params: language id [string] as in I18n::LoadLanguage
|
|
|
|
// returns:
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool LoadLanguage(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-08-21 13:45:01 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2004-08-21 13:45:01 +02:00
|
|
|
CStr lang = g_ScriptingHost.ValueToString(argv[0]);
|
|
|
|
I18n::LoadLanguage(lang);
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Return identifier of the current language (locale) in use.
|
|
|
|
// params:
|
|
|
|
// returns: language id [string] as in I18n::LoadLanguage
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetLanguageID(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-09-05 13:28:59 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
|
2004-09-05 13:28:59 +02:00
|
|
|
JSString* s = JS_NewStringCopyZ(cx, I18n::CurrentLanguageName());
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
JS_ReportError(cx, "Error creating string");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
*rval = STRING_TO_JSVAL(s);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Debug
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// Deliberately cause the game to crash.
|
|
|
|
// params:
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - currently implemented via access violation (read of address 0)
|
|
|
|
// - useful for testing the crashlog/stack trace code.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool ProvokeCrash(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-09-05 13:28:59 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
MICROLOG(L"Crashing at user's request.");
|
|
|
|
return *(JSBool*)0;
|
2004-09-05 13:28:59 +02:00
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
// Force a JS garbage collection cycle to take place immediately.
|
2005-07-20 02:57:17 +02:00
|
|
|
// params:
|
|
|
|
// returns: true [bool]
|
|
|
|
// notes:
|
|
|
|
// - writes an indication of how long this took to the console.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool ForceGarbageCollection(JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval)
|
2004-08-30 13:20:59 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
double time = get_time();
|
2006-06-04 01:02:37 +02:00
|
|
|
JS_GC(cx);
|
2005-07-20 02:57:17 +02:00
|
|
|
time = get_time() - time;
|
2006-06-04 01:02:37 +02:00
|
|
|
g_Console->InsertMessage(L"Garbage collection completed in: %f", time);
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = JSVAL_TRUE;
|
2006-06-04 01:02:37 +02:00
|
|
|
return JS_TRUE ;
|
2004-08-30 13:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-21 13:45:01 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-07-20 06:53:58 +02:00
|
|
|
// GUI
|
2005-07-20 02:57:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2004-07-09 14:44:12 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Returns the sort-of-global object associated with the current GUI.
|
|
|
|
// params:
|
|
|
|
// returns: global object
|
|
|
|
// notes:
|
|
|
|
// - Useful for accessing an object from another scope.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetGuiGlobal(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2004-07-09 14:44:12 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2006-06-04 01:02:37 +02:00
|
|
|
*rval = OBJECT_TO_JSVAL(g_GUI.GetScriptObject());
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resets the entire GUI state and reloads the XML files.
|
|
|
|
// params:
|
|
|
|
// returns:
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool ResetGui(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval)
|
2006-06-04 01:02:37 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2006-06-04 01:02:37 +02:00
|
|
|
|
|
|
|
// Slightly unpleasant code, because CGUI is a Singleton but we don't really
|
|
|
|
// want it to be
|
|
|
|
g_GUI.Destroy();
|
|
|
|
delete &g_GUI;
|
|
|
|
new CGUI;
|
|
|
|
GUI_Init();
|
|
|
|
g_GUI.SendEventToAll("load");
|
|
|
|
|
|
|
|
return JS_TRUE;
|
2005-07-20 02:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Misc. Engine Interface
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Return the global frames-per-second value.
|
|
|
|
// params:
|
|
|
|
// returns: FPS [int]
|
|
|
|
// notes:
|
|
|
|
// - This value is recalculated once a frame. We take special care to
|
|
|
|
// filter it, so it is both accurate and free of jitter.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetFps( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
*rval = INT_TO_JSVAL(fps);
|
2004-07-09 14:44:12 +02:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2004-07-15 21:29:56 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Cause the game to exit gracefully.
|
|
|
|
// params:
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - Exit happens after the current main loop iteration ends
|
|
|
|
// (since this only sets a flag telling it to end)
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool ExitProgram( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2004-07-15 21:29:56 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
kill_mainloop();
|
|
|
|
return JS_TRUE;
|
2004-07-15 21:29:56 +02:00
|
|
|
}
|
2004-08-14 13:21:29 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Write an indication of total/available video RAM to console.
|
|
|
|
// params:
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - Not supported on all platforms.
|
|
|
|
// - Only a rough approximation; do not base low-level decisions
|
|
|
|
// ("should I allocate one more texture?") on this.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool WriteVideoMemToConsole( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2004-08-14 13:21:29 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
#if OS_WIN
|
2004-08-14 13:21:29 +02:00
|
|
|
int left, total;
|
|
|
|
if (GetVRAMInfo(left, total))
|
2004-08-15 22:57:31 +02:00
|
|
|
g_Console->InsertMessage(L"VRAM: used %d, total %d, free %d", total-left, total, left);
|
2004-08-14 13:21:29 +02:00
|
|
|
else
|
2004-08-15 22:57:31 +02:00
|
|
|
g_Console->InsertMessage(L"VRAM: failed to detect");
|
2004-08-14 13:21:29 +02:00
|
|
|
#else
|
2004-08-15 22:57:31 +02:00
|
|
|
g_Console->InsertMessage(L"VRAM: [not available on non-Windows]");
|
2004-08-14 13:21:29 +02:00
|
|
|
#endif
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2005-03-29 22:50:04 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Change the mouse cursor.
|
|
|
|
// params: cursor name [string] (i.e. basename of definition file and texture)
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - Cursors are stored in "art\textures\cursors"
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetCursor( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2005-07-20 02:57:17 +02:00
|
|
|
g_CursorName = g_ScriptingHost.ValueToString(argv[0]);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
JSBool GetCursorName( JSContext* UNUSED(cx), JSObject*, uint UNUSED(argc), jsval* UNUSED(argv), jsval* rval )
|
2006-06-22 00:37:31 +02:00
|
|
|
{
|
|
|
|
*rval = ToJSVal(g_CursorName);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Trigger a rewrite of all maps.
|
|
|
|
// params:
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - Usefulness is unclear. If you need it, consider renaming this and updating the docs.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool _RewriteMaps( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2005-03-29 22:50:04 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
g_Game->GetWorld()->RewriteMap();
|
2005-03-29 22:50:04 +02:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Change the LOD bias.
|
|
|
|
// params: LOD bias [float]
|
|
|
|
// returns:
|
|
|
|
// notes:
|
|
|
|
// - value is as required by GL_TEXTURE_LOD_BIAS.
|
|
|
|
// - useful for adjusting image "sharpness" (since it affects which mipmap level is chosen)
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool _LodBias( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2005-05-20 19:09:47 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
g_Renderer.SetOptionFloat(CRenderer::OPT_LODBIAS, ToPrimitive<float>(argv[0]));
|
2005-05-20 19:09:47 +02:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2005-07-20 06:53:58 +02:00
|
|
|
// Focus the game camera on a given position.
|
2005-07-20 02:57:17 +02:00
|
|
|
// params: target position vector [CVector3D]
|
|
|
|
// returns: success [bool]
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetCameraTarget( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(1);
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
CVector3D* target = ToNative<CVector3D>( argv[0] );
|
|
|
|
if(!target)
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid camera target" );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
g_Game->GetView()->SetCameraTarget( *target );
|
|
|
|
|
|
|
|
*rval = JSVAL_TRUE;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Miscellany
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Return the date/time at which the current executable was compiled.
|
|
|
|
// params: none (-> "date time") OR
|
|
|
|
// what to display [int]: 0 (-> "date"); 1 (-> "time")
|
|
|
|
// returns: date and/or time [string]
|
|
|
|
// notes:
|
|
|
|
// - Displayed on main menu screen; tells non-programmers which auto-build
|
|
|
|
// they are running. Could also be determined via .EXE file properties,
|
|
|
|
// but that's a bit more trouble.
|
|
|
|
// - To be exact, the date/time returned is when scriptglue.cpp was
|
|
|
|
// last compiled; since the auto-build does full rebuilds, that is moot.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetBuildTimestamp( JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* rval )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAM_RANGE(0, 1);
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
// no param => "date time"
|
|
|
|
// param = 0 => "date"
|
|
|
|
// param = 1 => "time"
|
2005-07-20 02:57:17 +02:00
|
|
|
JSString* s = JS_NewStringCopyZ(cx,
|
|
|
|
argc && argv[0]==JSVAL_ONE ? __TIME__
|
|
|
|
: argc ? __DATE__
|
|
|
|
: __DATE__" "__TIME__
|
|
|
|
);
|
|
|
|
*rval = STRING_TO_JSVAL(s);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return distance between 2 points.
|
|
|
|
// params: 2 position vectors [CVector3D]
|
|
|
|
// returns: Euclidean distance [float]
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool ComputeDistanceBetweenTwoPoints( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS(2);
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
CVector3D* a = ToNative<CVector3D>( argv[0] );
|
|
|
|
CVector3D* b = ToNative<CVector3D>( argv[1] );
|
2007-05-02 14:07:08 +02:00
|
|
|
float dist = ( *a - *b ).Length();
|
2005-07-20 02:57:17 +02:00
|
|
|
*rval = ToJSVal( dist );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-20 06:53:58 +02:00
|
|
|
// Returns the global object.
|
|
|
|
// params:
|
|
|
|
// returns: global object
|
|
|
|
// notes:
|
|
|
|
// - Useful for accessing an object from another scope.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetGlobal( JSContext* cx, JSObject* globalObject, uint argc, jsval* argv, jsval* rval )
|
2005-07-20 06:53:58 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2005-07-20 06:53:58 +02:00
|
|
|
|
|
|
|
*rval = OBJECT_TO_JSVAL( globalObject );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:09:52 +01:00
|
|
|
// Saves the current profiling data to the logs/profile.txt file
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SaveProfileData( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2007-02-10 04:09:52 +01:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
|
|
|
g_ProfileViewer.SaveToFile();
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2006-05-16 06:41:37 +02:00
|
|
|
// Activates the building placement cursor for placing a building. The currently selected units
|
|
|
|
// are then ordered to construct the building if it is placed.
|
2005-09-05 21:48:28 +02:00
|
|
|
// params: templateName - the name of the entity to place.
|
|
|
|
// returns: true if cursor was activated, false if cursor was already active.
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool StartPlacing( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-09-05 21:48:28 +02:00
|
|
|
{
|
|
|
|
CStrW name;
|
|
|
|
if(argc == 0) {
|
|
|
|
name = L"hele_ho"; // save some typing during testing
|
|
|
|
}
|
|
|
|
else {
|
2005-12-15 22:59:48 +01:00
|
|
|
if(!ToPrimitive<CStrW>( g_ScriptingHost.GetContext(), argv[0], name ))
|
2005-09-05 21:48:28 +02:00
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid template name argument" );
|
2005-09-18 05:47:15 +02:00
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
return( JS_FALSE );
|
2005-09-05 21:48:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
*rval = g_BuildingPlacer.Activate(name) ? JS_TRUE : JS_FALSE;
|
2005-09-05 21:48:28 +02:00
|
|
|
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2006-05-16 06:41:37 +02:00
|
|
|
// Toggles drawing the sky
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool ToggleSky( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-05-16 06:41:37 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2006-05-16 06:41:37 +02:00
|
|
|
g_Renderer.GetSkyManager()->m_RenderSky = !g_Renderer.GetSkyManager()->m_RenderSky;
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2006-12-05 03:08:52 +01:00
|
|
|
// Toggles drawing territory outlines
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool ToggleTerritoryRendering( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-12-05 03:08:52 +01:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
|
|
|
g_Renderer.m_RenderTerritories = !g_Renderer.m_RenderTerritories;
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// water
|
|
|
|
|
2005-09-18 05:47:15 +02:00
|
|
|
// Toggles drawing the water plane
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool ToggleWater( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-09-18 05:47:15 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2006-01-07 02:04:26 +01:00
|
|
|
g_Renderer.GetWaterManager()->m_RenderWater = !g_Renderer.GetWaterManager()->m_RenderWater;
|
2005-09-18 05:47:15 +02:00
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the water plane height
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetWaterHeight( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-09-18 05:47:15 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS( 1 );
|
2005-09-18 05:47:15 +02:00
|
|
|
float newHeight;
|
|
|
|
if(!ToPrimitive( g_ScriptingHost.GetContext(), argv[0], newHeight ))
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid water height argument" );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_FALSE );
|
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
g_Renderer.GetWaterManager()->m_WaterHeight = newHeight;
|
2005-10-19 05:02:51 +02:00
|
|
|
g_TerrainModified = true;
|
2005-09-18 05:47:15 +02:00
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the water plane height
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetWaterHeight( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-09-18 05:47:15 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2006-01-07 02:04:26 +01:00
|
|
|
*rval = ToJSVal(g_Renderer.GetWaterManager()->m_WaterHeight);
|
2005-09-18 05:47:15 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2005-07-20 06:53:58 +02:00
|
|
|
|
2005-09-18 08:28:23 +02:00
|
|
|
// Sets the water color
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetWaterColor( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-09-18 08:28:23 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS( 3 );
|
2005-09-18 08:28:23 +02:00
|
|
|
float r,g,b;
|
|
|
|
if(!ToPrimitive( g_ScriptingHost.GetContext(), argv[0], r )
|
|
|
|
|| !ToPrimitive( g_ScriptingHost.GetContext(), argv[1], g )
|
|
|
|
|| !ToPrimitive( g_ScriptingHost.GetContext(), argv[2], b ))
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid arguments" );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_FALSE );
|
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
g_Renderer.GetWaterManager()->m_WaterColor = CColor(r, g, b, 1.0f);
|
2005-09-18 08:28:23 +02:00
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2006-06-22 23:11:18 +02:00
|
|
|
// Sets the water tint (used to tint reflections in fancy water)
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetWaterTint( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-06-22 23:11:18 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS( 3 );
|
2006-06-22 23:11:18 +02:00
|
|
|
float r,g,b;
|
|
|
|
if(!ToPrimitive( g_ScriptingHost.GetContext(), argv[0], r )
|
|
|
|
|| !ToPrimitive( g_ScriptingHost.GetContext(), argv[1], g )
|
|
|
|
|| !ToPrimitive( g_ScriptingHost.GetContext(), argv[2], b ))
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid arguments" );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_FALSE );
|
|
|
|
}
|
|
|
|
g_Renderer.GetWaterManager()->m_WaterTint = CColor(r, g, b, 1.0f);
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2006-09-10 07:31:41 +02:00
|
|
|
// Sets the water tint (used to tint reflections in fancy water)
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetReflectionTint( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-09-10 07:31:41 +02:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_PARAMS( 3 );
|
|
|
|
float r,g,b;
|
|
|
|
if(!ToPrimitive( g_ScriptingHost.GetContext(), argv[0], r )
|
|
|
|
|| !ToPrimitive( g_ScriptingHost.GetContext(), argv[1], g )
|
|
|
|
|| !ToPrimitive( g_ScriptingHost.GetContext(), argv[2], b ))
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid arguments" );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_FALSE );
|
|
|
|
}
|
|
|
|
g_Renderer.GetWaterManager()->m_ReflectionTint = CColor(r, g, b, 1.0f);
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2005-09-18 08:28:23 +02:00
|
|
|
// Sets the max water alpha (achieved when it is at WaterFullDepth or deeper)
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetWaterMaxAlpha( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-09-18 08:28:23 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS( 1 );
|
2005-09-18 08:28:23 +02:00
|
|
|
float val;
|
|
|
|
if(!ToPrimitive( g_ScriptingHost.GetContext(), argv[0], val ))
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid argument" );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_FALSE );
|
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
g_Renderer.GetWaterManager()->m_WaterMaxAlpha = val;
|
2005-09-18 08:28:23 +02:00
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the water full depth (when it is colored WaterMaxAlpha)
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetWaterFullDepth( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-09-18 08:28:23 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS( 1 );
|
2005-09-18 08:28:23 +02:00
|
|
|
float val;
|
|
|
|
if(!ToPrimitive( g_ScriptingHost.GetContext(), argv[0], val ))
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid argument" );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_FALSE );
|
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
g_Renderer.GetWaterManager()->m_WaterFullDepth = val;
|
2005-09-18 08:28:23 +02:00
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the water alpha offset (added to tweak water alpha near the shore)
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetWaterAlphaOffset( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-09-18 08:28:23 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS( 1 );
|
2005-09-18 08:28:23 +02:00
|
|
|
float val;
|
|
|
|
if(!ToPrimitive( g_ScriptingHost.GetContext(), argv[0], val ))
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Invalid argument" );
|
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_FALSE );
|
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
g_Renderer.GetWaterManager()->m_WaterAlphaOffset = val;
|
2005-09-18 08:28:23 +02:00
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2006-01-02 04:07:29 +01:00
|
|
|
// Is the game paused?
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool IsPaused( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-01-02 04:07:29 +01:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2006-01-23 04:56:48 +01:00
|
|
|
if( !g_Game )
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Game is not started" );
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2006-01-02 04:07:29 +01:00
|
|
|
*rval = g_Game->m_Paused ? JSVAL_TRUE : JSVAL_FALSE;
|
2006-01-23 04:56:48 +01:00
|
|
|
return JS_TRUE ;
|
2006-01-02 04:07:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pause/unpause the game
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool SetPaused( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-01-02 04:07:29 +01:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAMS( 1 );
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2006-01-23 04:56:48 +01:00
|
|
|
if( !g_Game )
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Game is not started" );
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2006-02-15 01:45:16 +01:00
|
|
|
try
|
2006-01-02 04:07:29 +01:00
|
|
|
{
|
|
|
|
g_Game->m_Paused = ToPrimitive<bool>( argv[0] );
|
2006-02-15 01:45:16 +01:00
|
|
|
}
|
2006-01-02 04:07:29 +01:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_ReportError( cx, "Invalid parameter to SetPaused" );
|
2006-01-02 04:07:29 +01:00
|
|
|
}
|
2006-01-23 04:56:48 +01:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get game time
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool GetGameTime( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-01-23 04:56:48 +01:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_NO_PARAMS();
|
2006-02-15 01:45:16 +01:00
|
|
|
|
2006-01-23 04:56:48 +01:00
|
|
|
if( !g_Game )
|
|
|
|
{
|
|
|
|
JS_ReportError( cx, "Game is not started" );
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2007-10-23 08:52:23 +02:00
|
|
|
*rval = ToJSVal(g_Game->GetSimulation()->GetTime());
|
2006-01-23 04:56:48 +01:00
|
|
|
return JS_TRUE;
|
2006-01-02 04:07:29 +01:00
|
|
|
}
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool RegisterTrigger( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-11-24 20:45:04 +01:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_PARAMS_CPP(1);
|
|
|
|
CTrigger* trigger = ToNative<CTrigger>( argv[0] );
|
|
|
|
debug_assert( trigger );
|
|
|
|
|
|
|
|
g_TriggerManager.AddTrigger( trigger );
|
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
|
|
|
|
JSBool GetTrigger( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2006-11-24 20:45:04 +01:00
|
|
|
{
|
|
|
|
JSU_REQUIRE_PARAMS_CPP(1);
|
|
|
|
CStrW name = ToPrimitive<CStrW>( argv[0] );
|
|
|
|
|
|
|
|
if ( g_TriggerManager.m_TriggerMap.find(name) != g_TriggerManager.m_TriggerMap.end() )
|
|
|
|
*rval = ToJSVal( g_TriggerManager.m_TriggerMap[name] );
|
|
|
|
else
|
|
|
|
{
|
2006-11-30 00:37:10 +01:00
|
|
|
debug_printf("Invalid trigger name %ws", name.c_str());
|
2006-11-24 20:45:04 +01:00
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2005-10-09 05:43:03 +02:00
|
|
|
// Reveal map
|
2007-05-02 14:07:08 +02:00
|
|
|
JSBool RevealMap( JSContext* cx, JSObject* UNUSED(globalObject), uint argc, jsval* argv, jsval* rval )
|
2005-10-09 05:43:03 +02:00
|
|
|
{
|
2006-07-26 15:10:13 +02:00
|
|
|
JSU_REQUIRE_PARAM_RANGE(0, 1);
|
2005-10-09 05:43:03 +02:00
|
|
|
|
2005-10-10 05:35:32 +02:00
|
|
|
uint newValue;
|
2005-10-09 05:43:03 +02:00
|
|
|
if(argc == 0)
|
|
|
|
{
|
2005-10-10 05:35:32 +02:00
|
|
|
newValue = 2;
|
2005-10-09 05:43:03 +02:00
|
|
|
}
|
2006-12-17 01:49:09 +01:00
|
|
|
else if(!ToPrimitive( g_ScriptingHost.GetContext(), argv[0], newValue ) || newValue > 2)
|
2005-10-09 05:43:03 +02:00
|
|
|
{
|
2005-10-10 05:35:32 +02:00
|
|
|
JS_ReportError( cx, "Invalid argument (should be 0, 1 or 2)" );
|
2005-10-09 05:43:03 +02:00
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_FALSE );
|
|
|
|
}
|
|
|
|
|
2005-10-10 05:35:32 +02:00
|
|
|
g_Game->GetWorld()->GetLOSManager()->m_LOSSetting = newValue;
|
2005-10-09 05:43:03 +02:00
|
|
|
*rval = JSVAL_VOID;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// function table
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the JS interpreter expects the table to contain 5-tuples as follows:
|
|
|
|
// - name the function will be called as from script;
|
|
|
|
// - function which will be called;
|
|
|
|
// - number of arguments this function expects
|
|
|
|
// - Flags (deprecated, always zero)
|
|
|
|
// - Extra (reserved for future use, always zero)
|
|
|
|
//
|
|
|
|
// we simplify this a bit with a macro:
|
2007-05-02 14:07:08 +02:00
|
|
|
#define JS_FUNC(script_name, cpp_function, min_params) { script_name, cpp_function, min_params, 0, 0 },
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
JSFunctionSpec ScriptFunctionTable[] =
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
2005-07-20 06:53:58 +02:00
|
|
|
// Console
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("writeConsole", JSI_Console::writeConsole, 1) // external
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Entity
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("getEntityByUnitID", GetEntityByUnitID, 1)
|
|
|
|
JS_FUNC("GetPlayerUnitCount", GetPlayerUnitCount, 1)
|
|
|
|
JS_FUNC("getEntityTemplate", GetEntityTemplate, 1)
|
|
|
|
JS_FUNC("issueCommand", IssueCommand, 2)
|
|
|
|
JS_FUNC("startPlacing", StartPlacing, 1)
|
|
|
|
|
|
|
|
// Formation
|
|
|
|
JS_FUNC("createEntityFormation", CreateEntityFormation, 2)
|
|
|
|
JS_FUNC("removeFromFormation", RemoveFromFormation, 1)
|
|
|
|
JS_FUNC("lockEntityFormation", LockEntityFormation, 1)
|
|
|
|
JS_FUNC("isFormationLocked", IsFormationLocked, 1)
|
|
|
|
|
|
|
|
// Trigger
|
|
|
|
JS_FUNC("registerTrigger", RegisterTrigger, 1)
|
2006-06-06 08:31:17 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
// Tech
|
|
|
|
JS_FUNC("getTechnology", GetTechnology, 2)
|
|
|
|
|
2005-07-20 06:53:58 +02:00
|
|
|
// Camera
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("setCameraTarget", SetCameraTarget, 1)
|
2005-07-20 06:53:58 +02:00
|
|
|
|
2006-05-16 06:41:37 +02:00
|
|
|
// Sky
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("toggleSky", ToggleSky, 0)
|
2006-05-16 06:41:37 +02:00
|
|
|
|
2005-09-18 05:47:15 +02:00
|
|
|
// Water
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("toggleWater", ToggleWater, 0)
|
|
|
|
JS_FUNC("setWaterHeight", SetWaterHeight, 1)
|
|
|
|
JS_FUNC("getWaterHeight", GetWaterHeight, 0)
|
|
|
|
JS_FUNC("setWaterColor", SetWaterColor, 3)
|
|
|
|
JS_FUNC("setWaterTint", SetWaterTint, 3)
|
|
|
|
JS_FUNC("setReflectionTint", SetReflectionTint, 3)
|
|
|
|
JS_FUNC("setWaterMaxAlpha", SetWaterMaxAlpha, 0)
|
|
|
|
JS_FUNC("setWaterFullDepth", SetWaterFullDepth, 0)
|
|
|
|
JS_FUNC("setWaterAlphaOffset", SetWaterAlphaOffset, 0)
|
2005-09-18 05:47:15 +02:00
|
|
|
|
2006-12-05 03:08:52 +01:00
|
|
|
// Territory rendering
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("toggleTerritoryRendering", ToggleTerritoryRendering, 0)
|
2006-12-05 03:08:52 +01:00
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
// GUI
|
2005-07-20 06:53:58 +02:00
|
|
|
#ifndef NO_GUI
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("getGUIObjectByName", JSI_IGUIObject::getByName, 1) // external
|
|
|
|
JS_FUNC("getGUIGlobal", GetGuiGlobal, 0)
|
|
|
|
JS_FUNC("resetGUI", ResetGui, 0)
|
2005-07-20 06:53:58 +02:00
|
|
|
#endif
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Events
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("addGlobalHandler", AddGlobalHandler, 2)
|
|
|
|
JS_FUNC("removeGlobalHandler", RemoveGlobalHandler, 2)
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Timer
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("setTimeout", SetTimeout, 2)
|
|
|
|
JS_FUNC("setInterval", SetInterval, 2)
|
|
|
|
JS_FUNC("cancelInterval", CancelInterval, 0)
|
|
|
|
JS_FUNC("cancelTimer", CancelTimer, 0)
|
|
|
|
JS_FUNC("setSimRate", SetSimRate, 1)
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2006-07-30 02:24:04 +02:00
|
|
|
// Random number generator
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("simRand", SimRand, 0)
|
|
|
|
JS_FUNC("simRandInt", SimRandInt, 1)
|
2006-07-30 02:24:04 +02:00
|
|
|
|
|
|
|
// Profiling
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("startXTimer", StartJsTimer, 1)
|
|
|
|
JS_FUNC("stopXTimer", StopJsTimer, 1)
|
2006-07-13 22:09:20 +02:00
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
// Game Setup
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("startGame", StartGame, 0)
|
|
|
|
JS_FUNC("endGame", EndGame, 0)
|
|
|
|
JS_FUNC("getGameMode", GetGameMode, 0)
|
|
|
|
JS_FUNC("createClient", CreateClient, 0)
|
|
|
|
JS_FUNC("createServer", CreateServer, 0)
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// VFS (external)
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("buildDirEntList", JSI_VFS::BuildDirEntList, 1)
|
|
|
|
JS_FUNC("getFileMTime", JSI_VFS::GetFileMTime, 1)
|
|
|
|
JS_FUNC("getFileSize", JSI_VFS::GetFileSize, 1)
|
|
|
|
JS_FUNC("readFile", JSI_VFS::ReadFile, 1)
|
|
|
|
JS_FUNC("readFileLines", JSI_VFS::ReadFileLines, 1)
|
|
|
|
JS_FUNC("archiveBuilderCancel", JSI_VFS::ArchiveBuilderCancel, 1)
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Internationalization
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("loadLanguage", LoadLanguage, 1)
|
|
|
|
JS_FUNC("getLanguageID", GetLanguageID, 0)
|
2005-07-20 06:53:58 +02:00
|
|
|
// note: i18n/ScriptInterface.cpp registers translate() itself.
|
|
|
|
// rationale: see implementation section above.
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Debug
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("crash", ProvokeCrash, 0)
|
|
|
|
JS_FUNC("forceGC", ForceGarbageCollection, 0)
|
|
|
|
JS_FUNC("revealMap", RevealMap, 1)
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Misc. Engine Interface
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("writeLog", WriteLog, 1)
|
|
|
|
JS_FUNC("exit", ExitProgram, 0)
|
|
|
|
JS_FUNC("isPaused", IsPaused, 0)
|
|
|
|
JS_FUNC("setPaused", SetPaused, 1)
|
|
|
|
JS_FUNC("getGameTime", GetGameTime, 0)
|
|
|
|
JS_FUNC("vmem", WriteVideoMemToConsole, 0)
|
|
|
|
JS_FUNC("_rewriteMaps", _RewriteMaps, 0)
|
|
|
|
JS_FUNC("_lodBias", _LodBias, 0)
|
|
|
|
JS_FUNC("setCursor", SetCursor, 1)
|
|
|
|
JS_FUNC("getCursorName", GetCursorName, 0)
|
|
|
|
JS_FUNC("getFPS", GetFps, 0)
|
|
|
|
JS_FUNC("isOrderQueued", isOrderQueued, 1)
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// Miscellany
|
2007-05-02 14:07:08 +02:00
|
|
|
JS_FUNC("v3dist", ComputeDistanceBetweenTwoPoints, 2)
|
|
|
|
JS_FUNC("buildTime", GetBuildTimestamp, 0)
|
|
|
|
JS_FUNC("getGlobal", GetGlobal, 0)
|
|
|
|
JS_FUNC("saveProfileData", SaveProfileData, 0)
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// end of table marker
|
|
|
|
{0, 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
#undef JS_FUNC
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// property accessors
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
JSBool GetEntitySet( JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsval UNUSED(argv), jsval* vp )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
2007-04-30 21:58:04 +02:00
|
|
|
std::vector<HEntity> extant;
|
|
|
|
g_EntityManager.GetExtantAsHandles(extant);
|
|
|
|
*vp = OBJECT_TO_JSVAL(EntityCollection::Create(extant));
|
2005-07-20 02:57:17 +02:00
|
|
|
|
2007-04-30 21:58:04 +02:00
|
|
|
return JS_TRUE;
|
2005-07-20 02:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
JSBool GetPlayerSet( JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsval UNUSED(id), jsval* vp )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
|
|
|
std::vector<CPlayer*>* players = g_Game->GetPlayers();
|
|
|
|
|
|
|
|
*vp = OBJECT_TO_JSVAL( PlayerCollection::Create( *players ) );
|
|
|
|
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
JSBool GetLocalPlayer( JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsval UNUSED(id), jsval* vp )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
|
|
|
*vp = OBJECT_TO_JSVAL( g_Game->GetLocalPlayer()->GetScript() );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
JSBool GetGaiaPlayer( JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsval UNUSED(id), jsval* vp )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
|
|
|
*vp = OBJECT_TO_JSVAL( g_Game->GetPlayer( 0 )->GetScript() );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
JSBool SetLocalPlayer( JSContext* cx, JSObject* UNUSED(obj), jsval UNUSED(id), jsval* vp )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
|
|
|
CPlayer* newLocalPlayer = ToNative<CPlayer>( *vp );
|
|
|
|
|
|
|
|
if( !newLocalPlayer )
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
2005-07-20 02:57:17 +02:00
|
|
|
JS_ReportError( cx, "Not a valid Player." );
|
|
|
|
return( JS_TRUE );
|
2005-05-18 07:32:09 +02:00
|
|
|
}
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
g_Game->SetLocalPlayer( newLocalPlayer );
|
|
|
|
return( JS_TRUE );
|
2005-05-18 07:32:09 +02:00
|
|
|
}
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
|
2005-08-09 18:02:15 +02:00
|
|
|
JSBool GetGameView( JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsval UNUSED(id), jsval* vp )
|
2005-07-20 02:57:17 +02:00
|
|
|
{
|
|
|
|
if (g_Game)
|
|
|
|
*vp = OBJECT_TO_JSVAL( g_Game->GetView()->GetScript() );
|
|
|
|
else
|
|
|
|
*vp = JSVAL_NULL;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-10 16:15:28 +02:00
|
|
|
JSBool GetRenderer( JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsval UNUSED(id), jsval* vp )
|
|
|
|
{
|
|
|
|
if (CRenderer::IsInitialised())
|
|
|
|
*vp = OBJECT_TO_JSVAL( g_Renderer.GetScript() );
|
|
|
|
else
|
|
|
|
*vp = JSVAL_NULL;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-20 02:57:17 +02:00
|
|
|
enum ScriptGlobalTinyIDs
|
|
|
|
{
|
|
|
|
GLOBAL_SELECTION,
|
|
|
|
GLOBAL_GROUPSARRAY,
|
|
|
|
GLOBAL_CAMERA,
|
|
|
|
GLOBAL_CONSOLE,
|
2006-02-15 01:45:16 +01:00
|
|
|
GLOBAL_LIGHTENV
|
2005-07-20 02:57:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// shorthand
|
|
|
|
#define PERM JSPROP_PERMANENT
|
|
|
|
#define CONST JSPROP_READONLY
|
|
|
|
|
|
|
|
JSPropertySpec ScriptGlobalTable[] =
|
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
{ "selection" , GLOBAL_SELECTION, PERM, JSI_Selection::getSelection, JSI_Selection::SetSelection },
|
2005-07-20 02:57:17 +02:00
|
|
|
{ "groups" , GLOBAL_GROUPSARRAY, PERM, JSI_Selection::getGroups, JSI_Selection::setGroups },
|
|
|
|
{ "camera" , GLOBAL_CAMERA, PERM, JSI_Camera::getCamera, JSI_Camera::setCamera },
|
|
|
|
{ "console" , GLOBAL_CONSOLE, PERM | CONST, JSI_Console::getConsole, 0 },
|
2006-02-15 01:45:16 +01:00
|
|
|
{ "lightenv" , GLOBAL_LIGHTENV, PERM, JSI_LightEnv::getLightEnv, JSI_LightEnv::setLightEnv },
|
2005-07-20 02:57:17 +02:00
|
|
|
{ "entities" , 0, PERM | CONST, GetEntitySet, 0 },
|
|
|
|
{ "players" , 0, PERM | CONST, GetPlayerSet, 0 },
|
|
|
|
{ "localPlayer", 0, PERM , GetLocalPlayer, SetLocalPlayer },
|
|
|
|
{ "gaiaPlayer" , 0, PERM | CONST, GetGaiaPlayer, 0 },
|
|
|
|
{ "gameView" , 0, PERM | CONST, GetGameView, 0 },
|
2005-10-10 16:15:28 +02:00
|
|
|
{ "renderer" , 0, PERM | CONST, GetRenderer, 0 },
|
2005-07-20 02:57:17 +02:00
|
|
|
|
|
|
|
// end of table marker
|
|
|
|
{ 0, 0, 0, 0, 0 },
|
|
|
|
};
|