Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpCommandQueue.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2010 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
21 #include "ICmpCommandQueue.h"
22 
23 #include "ps/CLogger.h"
24 #include "ps/Game.h"
25 #include "network/NetTurnManager.h"
26 
28 {
29 public:
30  static void ClassInit(CComponentManager& UNUSED(componentManager))
31  {
32  }
33 
35 
37 
38  static std::string GetSchema()
39  {
40  return "<a:component type='system'/><empty/>";
41  }
42 
43  virtual void Init(const CParamNode& UNUSED(paramNode))
44  {
45  }
46 
47  virtual void Deinit()
48  {
49  }
50 
51  virtual void Serialize(ISerializer& serialize)
52  {
53  serialize.NumberU32_Unbounded("num commands", (u32)m_LocalQueue.size());
54  for (size_t i = 0; i < m_LocalQueue.size(); ++i)
55  {
56  serialize.NumberI32_Unbounded("player", m_LocalQueue[i].player);
57  serialize.ScriptVal("data", m_LocalQueue[i].data.get());
58  }
59  }
60 
61  virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
62  {
63  u32 numCmds;
64  deserialize.NumberU32_Unbounded("num commands", numCmds);
65  for (size_t i = 0; i < numCmds; ++i)
66  {
67  i32 player;
68  CScriptValRooted data;
69  deserialize.NumberI32_Unbounded("player", player);
70  deserialize.ScriptVal("data", data);
71  SimulationCommand c = { player, data };
72  m_LocalQueue.push_back(c);
73  }
74  }
75 
76  virtual void PushLocalCommand(player_id_t player, CScriptVal cmd)
77  {
78  JSContext* cx = GetSimContext().GetScriptInterface().GetContext();
79 
80  SimulationCommand c = { player, CScriptValRooted(cx, cmd) };
81  m_LocalQueue.push_back(c);
82  }
83 
84  virtual void PostNetworkCommand(CScriptVal cmd)
85  {
86  JSContext* cx = GetSimContext().GetScriptInterface().GetContext();
87 
88  PROFILE2_EVENT("post net command");
89  PROFILE2_ATTR("command: %s", GetSimContext().GetScriptInterface().StringifyJSON(cmd.get(), false).c_str());
90 
91  // TODO: would be nicer to not use globals
92  if (g_Game && g_Game->GetTurnManager())
94  }
95 
96  virtual void FlushTurn(const std::vector<SimulationCommand>& commands)
97  {
98  ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface();
99 
100  std::vector<SimulationCommand> localCommands;
101  m_LocalQueue.swap(localCommands);
102 
103  for (size_t i = 0; i < localCommands.size(); ++i)
104  {
105  bool ok = scriptInterface.CallFunctionVoid(scriptInterface.GetGlobalObject(), "ProcessCommand", localCommands[i].player, localCommands[i].data);
106  if (!ok)
107  LOGERROR(L"Failed to call ProcessCommand() global script function");
108  }
109 
110  for (size_t i = 0; i < commands.size(); ++i)
111  {
112  bool ok = scriptInterface.CallFunctionVoid(scriptInterface.GetGlobalObject(), "ProcessCommand", commands[i].player, commands[i].data);
113  if (!ok)
114  LOGERROR(L"Failed to call ProcessCommand() global script function");
115  }
116  }
117 };
118 
119 REGISTER_COMPONENT_TYPE(CommandQueue)
An entity initialisation parameter node.
Definition: ParamNode.h:112
#define PROFILE2_EVENT(name)
Record the named event at the current time.
Definition: Profiler2.h:453
virtual void FlushTurn(const std::vector< SimulationCommand > &commands)
Calls the ProcessCommand(player, cmd) global script function for each command in the local queue and ...
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
virtual void Deinit()
#define LOGERROR
Definition: CLogger.h:35
static void ClassInit(CComponentManager &componentManager)
#define i32
Definition: types.h:36
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
bool CallFunctionVoid(jsval val, const char *name)
Call the named property on the given object, with void return type and 0 arguments.
virtual void PostNetworkCommand(CScriptVal cmd)
Send a command associated with the current player to the networking system.
const jsval & get() const
Returns the current value.
Definition: ScriptVal.h:38
virtual void NumberU32_Unbounded(const char *name, uint32_t &out)
A trivial wrapper around a jsval.
Definition: ScriptVal.h:29
void ScriptVal(const char *name, jsval value)
Serialize a jsval.
Definition: ISerializer.cpp:95
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
ScriptInterface & GetScriptInterface() const
Definition: SimContext.cpp:63
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
Simulation command, typically received over the network in multiplayer games.
CNetTurnManager * GetTurnManager() const
Definition: Game.h:159
virtual void PushLocalCommand(player_id_t player, CScriptVal cmd)
Pushes a new command onto the local queue.
Command queue, for sending orders to entities.
CGame * g_Game
Globally accessible pointer to the CGame object.
Definition: Game.cpp:56
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
const CSimContext & GetSimContext() const
Definition: IComponent.h:52
void NumberU32_Unbounded(const char *name, uint32_t value)
Serialize a number.
Definition: ISerializer.h:171
#define PROFILE2_ATTR
Associates a string (with printf-style formatting) with the current region or event.
Definition: Profiler2.h:461
#define u32
Definition: types.h:41
virtual void ScriptVal(const char *name, jsval &out)=0
Deserialize a jsval, replacing &#39;out&#39;.
virtual void Init(const CParamNode &paramNode)
static std::string GetSchema()
Abstraction around a SpiderMonkey JSContext.
void NumberI32_Unbounded(const char *name, int32_t value)
Serialize a number.
Definition: ISerializer.h:176
std::vector< SimulationCommand > m_LocalQueue
virtual void PostCommand(CScriptValRooted data)=0
Called by simulation code, to add a new command to be distributed to all clients and executed soon...
virtual void Serialize(ISerializer &serialize)
JSContext * GetContext() const
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34
virtual void NumberI32_Unbounded(const char *name, int32_t &out)