18 #include "precompiled.h"
51 #include "tools/atlas/GameInterface/GameLoop.h"
105 if (!cmpGuiInterface)
127 if (!cmpCommandQueue)
185 std::wstring name = playername.FromUTF8();
200 return server.FromUTF8();
205 g_ConfigDB.CreateValue(
CFG_USER,
"playername")->m_String = CStrW(playerName).ToUTF8();
206 g_ConfigDB.CreateValue(
CFG_USER,
"multiplayerserver")->m_String = CStrW(server).ToUTF8();
246 std::string savedState;
268 return metadata.
get();
271 void SaveGame(
void* cbdata, std::wstring filename, std::wstring description)
379 std::vector<CScriptValRooted>
GetAIs(
void* cbdata)
532 bool splashScreenEnable =
true;
533 CFG_GET_VAL(
"splashscreenenable", Bool, splashScreenEnable);
534 return splashScreenEnable;
539 CStr val = (enabled ?
"true" :
"false");
577 LOGERROR(L
"Only network host can change turn length");
592 return *(
volatile int*)0;
621 VfsPath filename(L
"screenshots/terrainmipmap.png");
624 g_VFS->GetRealPath(filename, realPath);
665 scriptInterface.RegisterFunction<void, std::wstring, CScriptVal, &
SwitchGuiPage>(
"SwitchGuiPage");
666 scriptInterface.RegisterFunction<void, &
PopGuiPage>(
"PopGuiPage");
669 scriptInterface.RegisterFunction<CScriptVal, std::wstring, CScriptVal, &
GuiInterfaceCall>(
"GuiInterfaceCall");
670 scriptInterface.RegisterFunction<void, CScriptVal, &
PostNetworkCommand>(
"PostNetworkCommand");
673 scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, &
PickEntitiesAtPoint>(
"PickEntitiesAtPoint");
674 scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &
PickFriendlyEntitiesInRect>(
"PickFriendlyEntitiesInRect");
676 scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &
PickSimilarFriendlyEntities>(
"PickSimilarFriendlyEntities");
680 scriptInterface.RegisterFunction<void, &
StartNetworkGame>(
"StartNetworkGame");
681 scriptInterface.RegisterFunction<void, CScriptVal, int, &
StartGame>(
"StartGame");
682 scriptInterface.RegisterFunction<void, std::wstring, &
StartNetworkHost>(
"StartNetworkHost");
683 scriptInterface.RegisterFunction<void, std::wstring, std::string, &
StartNetworkJoin>(
"StartNetworkJoin");
685 scriptInterface.RegisterFunction<CScriptVal, &
PollNetworkClient>(
"PollNetworkClient");
687 scriptInterface.RegisterFunction<void, int, std::string, &
AssignNetworkPlayer>(
"AssignNetworkPlayer");
688 scriptInterface.RegisterFunction<void, std::wstring, &
SendNetworkChat>(
"SendNetworkChat");
689 scriptInterface.RegisterFunction<std::vector<CScriptValRooted>, &
GetAIs>(
"GetAIs");
692 scriptInterface.RegisterFunction<CScriptVal, std::wstring, &
StartSavedGame>(
"StartSavedGame");
693 scriptInterface.RegisterFunction<std::vector<CScriptValRooted>, &
GetSavedGames>(
"GetSavedGames");
694 scriptInterface.RegisterFunction<bool, std::wstring, &
DeleteSavedGame>(
"DeleteSavedGame");
695 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, &
SaveGame>(
"SaveGame");
696 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, &
SaveGamePrefix>(
"SaveGamePrefix");
697 scriptInterface.RegisterFunction<void, &
QuickSave>(
"QuickSave");
698 scriptInterface.RegisterFunction<void, &
QuickLoad>(
"QuickLoad");
701 scriptInterface.RegisterFunction<std::wstring, std::wstring, &
SetCursor>(
"SetCursor");
702 scriptInterface.RegisterFunction<int, &
GetPlayerID>(
"GetPlayerID");
703 scriptInterface.RegisterFunction<void, int, &
SetPlayerID>(
"SetPlayerID");
705 scriptInterface.RegisterFunction<std::wstring, &
GetDefaultMPServer>(
"GetDefaultMPServer");
706 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, &
SaveMPConfig>(
"SaveMPConfig");
707 scriptInterface.RegisterFunction<void, std::string, &
OpenURL>(
"OpenURL");
708 scriptInterface.RegisterFunction<void, &
RestartInAtlas>(
"RestartInAtlas");
709 scriptInterface.RegisterFunction<bool, &
AtlasIsAvailable>(
"AtlasIsAvailable");
710 scriptInterface.RegisterFunction<bool, &
IsAtlasRunning>(
"IsAtlasRunning");
712 scriptInterface.RegisterFunction<CScriptVal, &
GetMapSettings>(
"GetMapSettings");
713 scriptInterface.RegisterFunction<float, &
CameraGetX>(
"CameraGetX");
714 scriptInterface.RegisterFunction<float, &
CameraGetZ>(
"CameraGetZ");
719 scriptInterface.RegisterFunction<bool, std::string, &
HotkeyIsPressed_>(
"HotkeyIsPressed");
720 scriptInterface.RegisterFunction<void, std::wstring, &
DisplayErrorDialog>(
"DisplayErrorDialog");
721 scriptInterface.RegisterFunction<CScriptVal, &
GetProfilerState>(
"GetProfilerState");
726 scriptInterface.RegisterFunction<std::string, &
GetUserReportStatus>(
"GetUserReportStatus");
727 scriptInterface.RegisterFunction<void, std::string, int, std::wstring, &
SubmitUserReport>(
"SubmitUserReport");
734 scriptInterface.RegisterFunction<void, float, &
SetSimRate>(
"SetSimRate");
735 scriptInterface.RegisterFunction<float, &
GetSimRate>(
"GetSimRate");
736 scriptInterface.RegisterFunction<void, int, &
SetTurnLength>(
"SetTurnLength");
737 scriptInterface.RegisterFunction<void, float, float, float, &
SetCameraTarget>(
"SetCameraTarget");
738 scriptInterface.RegisterFunction<int, &
Crash>(
"Crash");
739 scriptInterface.RegisterFunction<void, &
DebugWarn>(
"DebugWarn");
740 scriptInterface.RegisterFunction<void, &
ForceGC>(
"ForceGC");
741 scriptInterface.RegisterFunction<void, &
DumpSimState>(
"DumpSimState");
744 scriptInterface.RegisterFunction<void, &
RewindTimeWarp>(
"RewindTimeWarp");
CScriptVal GetMapSettings(void *cbdata)
void RestartInAtlas(void *cbdata)
The container that holds the rules, resources and attributes of the game.
ScriptInterface & GetScriptInterface()
Get the script interface associated with this network client, which is equivalent to the one used by ...
A simple fixed-point number class.
std::vector< CScriptValRooted > GetSavedGames(ScriptInterface &scriptInterface)
Get list of saved games for GUI script usage.
std::wstring sys_get_user_name()
Get the current user's login name.
void SetTurnLength(void *cbdata, int length)
void StartGame()
Call from the GUI to asynchronously notify all clients that they should start loading the game...
CScriptValRooted GetMapSettings(ScriptInterface &scriptInterface)
Returns a value of the form:
JSBool SetCursor(JSContext *cx, uintN argc, jsval *vp)
void SendNetworkChat(void *cbdata, std::wstring message)
CUserReporter g_UserReporter
void SetPlayerID(void *cbdata, int id)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
void DumpToDisk(const VfsPath &path) const
void PopPage()
Unload the currently active GUI page, and make the previous page active.
Path VfsPath
VFS path of the form "(dir/)*file?".
CNetClient * g_NetClient
Global network client for the standard game.
void DebugWarn(void *cbdata)
bool DeleteSavedGame(const std::wstring &name)
Permanently deletes the saved game archive with the given name.
void PushGuiPage(void *cbdata, std::wstring name, CScriptVal initData)
static std::vector< CScriptValRooted > GetAIs(ScriptInterface &scriptInterface)
Returns a vector of {"id":"value-for-AddPlayer", "name":"Human readable name"} objects, based on all the available AI scripts.
bool HotkeyIsPressed_(void *cbdata, std::string hotkeyName)
void QuickLoad(void *cbdata)
void RegisterScriptFunctions(ScriptInterface &scriptInterface)
const OsPath & psLogDir()
static bool ms_EnableDebugOverlays
std::wstring GetDefaultMPServer(void *cbdata)
Status Save(const std::wstring &name, const std::wstring &description, CSimulation2 &simulation, CGUIManager *gui, int playerID)
Create new saved game archive with given name and simulation data.
void SaveGame(void *cbdata, std::wstring filename, std::wstring description)
ErrorReaction debug_DisplayError(const wchar_t *description, size_t flags, void *context, const wchar_t *lastFuncToSkip, const wchar_t *pathname, int line, const char *func, atomic_bool *suppress)
display an error dialog with a message and stack trace.
void DumpSimState(void *cbdata)
const PSRETURN PSRETURN_OK
bool SetupConnection()
Begin listening for network connections.
void ForceGC(void *cbdata)
std::string utf8_from_wstring(const std::wstring &src, Status *err)
opposite of wstring_from_utf8
void RegisterScriptFunctions(ScriptInterface &scriptInterface)
std::string GetUserReportStatus(void *cbdata)
#define CFG_GET_VAL(name, type, destination)
void CameraFollow(entity_id_t entity, bool firstPerson)
static void out(const wchar_t *fmt,...)
void SubmitUserReport(void *cbdata, std::string type, int version, std::wstring data)
void StartNetworkJoin(void *cbdata, std::wstring playerName, std::string serverAddress)
std::vector< entity_id_t > PickSimilarEntities(CSimulation2 &simulation, const CCamera &camera, const std::string &templateName, player_id_t owner, bool includeOffScreen, bool matchRank, bool allowEditorSelectables, bool allowFoundations)
Finds all entities with the given entity template name, belonging to the given player.
void EnableTimeWarpRecording(size_t numTurns)
Enables the recording of state snapshots every numTurns, which can be jumped back to via RewindTimeWa...
CFixedVector3D GetTerrainAtScreenPoint(void *cbdata, int x, int y)
CScriptVal GuiInterfaceCall(void *cbdata, std::wstring name, CScriptVal data)
void PushPage(const CStrW &name, CScriptVal initData)
Load a new GUI page and make it active.
const jsval & get() const
Returns the current value.
void GuiScriptingInit(ScriptInterface &scriptInterface)
std::vector< entity_id_t > PickEntitiesAtPoint(CSimulation2 &simulation, const CCamera &camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables)
Finds all selectable entities under the given screen coordinates.
CNetServer * g_NetServer
Global network server for the standard game.
A trivial wrapper around a jsval.
GameLoopState * g_AtlasGameLoop
CScriptValRooted GuiPoll()
Retrieves the next queued GUI message, and removes it from the queue.
float GetExactGroundLevel(float x, float z) const
CVector3D GetWorldCoordinates(int px, int py, bool aboveWater=false) const
std::vector< entity_id_t > PickFriendlyEntitiesInRect(void *cbdata, int x0, int y0, int x1, int y1, int player)
void SetBoundingBoxDebugOverlay(void *cbdata, bool enabled)
const entity_id_t SYSTEM_ENTITY
Entity ID for singleton 'system' components.
Public API for simulation system.
float GetSimRate(void *cbdata)
Status SavePrefix(const std::wstring &prefix, const std::wstring &description, CSimulation2 &simulation, CGUIManager *gui, int playerID)
Create new saved game archive with given prefix and simulation data.
void RewindTimeWarp(void *cbdata)
CTerrain * GetTerrain()
Get the pointer to the terrain object.
void QuickSave(void *cbdata)
CFixed_15_16 entity_pos_t
Positions and distances (measured in metres)
void CameraFollow(void *cbdata, entity_id_t entityid)
Start / stop camera following mode.
std::vector< CScriptValRooted > GetSavedGames(void *cbdata)
void SetSplashScreenEnabled(void *cbdata, bool enabled)
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
void DumpTerrainMipmap(void *cbdata)
void SetTurnLength(u32 msecs)
Set the turn length to a fixed value.
void SetPlayerID(int playerID)
void SubmitReport(const char *type, int version, const std::string &data)
Submit a report to be transmitted to the online server.
void AssignPlayer(int playerID, const CStr &guid)
Call from the GUI to update the player assignments.
std::vector< CScriptValRooted > GetAIs(void *cbdata)
void OpenURL(void *cbdata, std::string url)
display just the given message; do not add any information about the call stack, do not write crashlo...
Status sys_open_url(const std::string &url)
Open the user's default web browser to the given URL.
virtual void PostNetworkCommand(CScriptVal cmd)=0
Send a command associated with the current player to the networking system.
CScriptVal GetActiveGui(void *cbdata)
const String & string() const
Contains functions for managing saved game archives.
float ToFloat() const
Convert to float. May be lossy - float can't represent all values.
CScriptVal StartSavedGame(void *cbdata, std::wstring name)
void UpdateGameAttributes(const CScriptVal &attrs, ScriptInterface &scriptInterface)
Call from the GUI to update the game setup attributes.
void StartGame(const CScriptValRooted &attribs, const std::string &savedState)
void SetUserName(const CStrW &username)
Set the user's name that will be displayed to all players.
CNetTurnManager * GetTurnManager() const
JSObject * GetScriptObject()
See CGUI::GetScriptObject; applies to the currently active page.
ScriptInterface & GetScriptInterface() const
#define SAFE_DELETE(p)
delete memory ensuing from new and set the pointer to zero (thus making double-frees safe / a no-op) ...
void DisconnectNetworkGame(void *cbdata)
void SaveMPConfig(void *cbdata, std::wstring playerName, std::wstring server)
CGame * g_Game
Globally accessible pointer to the CGame object.
void AssignNetworkPlayer(void *cbdata, int playerID, std::string guid)
Network server interface.
void SetSimRate(float simRate)
Set the simulation scale multiplier.
i64 Status
Error handling system.
bool IsAtlasRunning(void *cbdata)
A simplified syntax for accessing entity components.
jsval CloneValueFromOtherContext(ScriptInterface &otherContext, jsval val)
Construct a new value (usable in this ScriptInterface's context) by cloning a value from a different ...
std::vector< entity_id_t > PickEntitiesAtPoint(void *cbdata, int x, int y)
CSimulation2 * GetSimulation2()
Get the pointer to the simulation2 object.
ScriptInterface & GetScriptInterface()
bool GetProperty(jsval obj, const char *name, T &out)
Get the named property on the given object.
CScriptVal PollNetworkClient(void *cbdata)
entity_id_t GetFollowedEntity()
CGameView * GetView()
Get the pointer to the game view object.
float CameraGetX(void *cbdata)
Get the current X coordinate of the camera.
bool SetupConnection(const CStr &server)
Set up a connection to the remote networked server.
void CameraMoveTo(void *cbdata, entity_pos_t x, entity_pos_t z)
Move camera to a 2D location.
PSRETURN LoadMap(const VfsPath &pathname)
Try to load a map file.
void PopGuiPage(void *cbdata)
virtual CScriptVal ScriptCall(int player, const std::wstring &cmd, CScriptVal data)=0
Generic call function, for use by GUI scripts to talk to the GuiInterface script. ...
void SwitchPage(const CStrW &name, CScriptVal initData)
Load a new GUI page and make it active.
void SetReportingEnabled(bool enabled)
std::vector< entity_id_t > PickFriendlyEntitiesOnScreen(void *cbdata, int player)
void SetUserReportEnabled(void *cbdata, bool enabled)
void PostNetworkCommand(void *cbdata, CScriptVal cmd)
void StartNetworkGame(void *cbdata)
entity_id_t GetFollowedEntity(void *cbdata)
void SendChatMessage(const std::wstring &text)
void CameraFollowFPS(void *cbdata, entity_id_t entityid)
Start / stop first-person camera following mode.
std::wstring GetDefaultPlayerName(void *cbdata)
CScriptVal GetMapSettings()
Get the current map settings.
Status Load(const std::wstring &name, ScriptInterface &scriptInterface, CScriptValRooted &metadata, std::string &savedState)
Load saved game archive with the given name.
void SwitchGuiPage(void *cbdata, std::wstring name, CScriptVal initData)
void RegisterScriptFunctions(ScriptInterface &ScriptInterface)
void StartNetworkHost(void *cbdata, std::wstring playerName)
std::vector< entity_id_t > PickSimilarFriendlyEntities(void *cbdata, std::string templateName, bool includeOffScreen, bool matchRank, bool allowFoundations)
static CFixed FromFloat(float n)
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
CWorld * GetWorld()
Get the pointer to the game world object.
void restart_mainloop_in_atlas()
Abstraction around a SpiderMonkey JSContext.
bool HotkeyIsPressed(const CStr &keyname)
CScriptVal GetProfilerState(void *cbdata)
int GetPlayerID(void *cbdata)
#define debug_warn(expr)
display the error dialog with the given text.
void RegisterScriptFunctions(ScriptInterface &scriptInterface)
External interface to the GUI system.
void EnableTimeWarpRecording(void *cbdata, unsigned int numTurns)
bool DeleteSavedGame(void *cbdata, std::wstring name)
CScriptVal LoadMapSettings(void *cbdata, VfsPath pathname)
float CameraGetZ(void *cbdata)
Get the current Z coordinate of the camera.
const entity_id_t INVALID_ENTITY
Invalid entity ID.
bool AtlasIsAvailable(void *cbdata)
void SetNetworkGameAttributes(void *cbdata, CScriptVal attribs)
u32 entity_id_t
Entity ID type.
void RewindTimeWarp()
Jumps back to the latest recorded state snapshot (if any).
A restricted map reader that returns various summary information for use by scripts (particularly the...
void MoveCameraTarget(const CVector3D &target)
bool IsUserReportEnabled(void *cbdata)
bool DumpDebugState(std::ostream &stream)
void InsertMessage(const wchar_t *szMessage,...) WPRINTF_ARGS(2)
void SetCameraTarget(void *cbdata, float x, float y, float z)
Helper functions related to entity selection.
JSContext * GetContext() const
void SaveGamePrefix(void *cbdata, std::wstring prefix, std::wstring description)
void ResetCameraTarget(const CVector3D &target)
const CHeightMipmap & GetHeightMipmap() const
void ReportError(const char *msg)
Report the given error message through the JS error reporting mechanism, and throw a JS exception...
void DisplayErrorDialog(void *cbdata, std::wstring msg)
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
void SetSimRate(void *cbdata, float rate)
bool IsSplashScreenEnabled(void *cbdata)
bool IsReportingEnabled()
static std::string OsString(const OsPath &path)
std::vector< entity_id_t > PickEntitiesInRect(CSimulation2 &simulation, const CCamera &camera, int sx0, int sy0, int sx1, int sy1, player_id_t owner, bool allowEditorSelectables)
Finds all selectable entities within the given screen coordinate rectangle, belonging to the given pl...