2010-01-09 20:20:14 +01:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "simulation2/system/Component.h"
|
|
|
|
#include "ICmpCommandQueue.h"
|
|
|
|
|
|
|
|
#include "ps/CLogger.h"
|
2010-05-20 02:59:01 +02:00
|
|
|
#include "ps/Game.h"
|
|
|
|
#include "network/NetTurnManager.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
class CCmpCommandQueue : public ICmpCommandQueue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void ClassInit(CComponentManager& UNUSED(componentManager))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(CommandQueue)
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
std::vector<SimulationCommand> m_LocalQueue;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-04-23 18:09:03 +02:00
|
|
|
static std::string GetSchema()
|
|
|
|
{
|
|
|
|
return "<a:component type='system'/><empty/>";
|
|
|
|
}
|
|
|
|
|
2010-05-01 11:48:39 +02:00
|
|
|
virtual void Init(const CSimContext& UNUSED(context), const CParamNode& UNUSED(paramNode))
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Deinit(const CSimContext& UNUSED(context))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Serialize(ISerializer& serialize)
|
|
|
|
{
|
2010-05-20 02:59:01 +02:00
|
|
|
serialize.NumberU32_Unbounded("num commands", (u32)m_LocalQueue.size());
|
|
|
|
for (size_t i = 0; i < m_LocalQueue.size(); ++i)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-20 02:59:01 +02:00
|
|
|
serialize.NumberI32_Unbounded("player", m_LocalQueue[i].player);
|
|
|
|
serialize.ScriptVal("data", m_LocalQueue[i].data.get());
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Deserialize(const CSimContext& context, const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
|
|
|
|
{
|
2010-05-01 11:48:39 +02:00
|
|
|
JSContext* cx = context.GetScriptInterface().GetContext();
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
u32 numCmds;
|
2010-09-17 19:53:26 +02:00
|
|
|
deserialize.NumberU32_Unbounded("num commands", numCmds);
|
2010-01-09 20:20:14 +01:00
|
|
|
for (size_t i = 0; i < numCmds; ++i)
|
|
|
|
{
|
|
|
|
i32 player;
|
|
|
|
jsval data;
|
2010-09-17 19:53:26 +02:00
|
|
|
deserialize.NumberI32_Unbounded("player", player);
|
|
|
|
deserialize.ScriptVal("data", data);
|
2010-05-20 02:59:01 +02:00
|
|
|
SimulationCommand c = { player, CScriptValRooted(cx, data) };
|
|
|
|
m_LocalQueue.push_back(c);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
virtual void PushLocalCommand(int player, CScriptVal cmd)
|
|
|
|
{
|
|
|
|
JSContext* cx = GetSimContext().GetScriptInterface().GetContext();
|
|
|
|
|
|
|
|
SimulationCommand c = { player, CScriptValRooted(cx, cmd) };
|
|
|
|
m_LocalQueue.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void PostNetworkCommand(CScriptVal cmd)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-01 11:48:39 +02:00
|
|
|
JSContext* cx = GetSimContext().GetScriptInterface().GetContext();
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
// TODO: would be nicer to not use globals
|
|
|
|
g_Game->GetTurnManager()->PostCommand(CScriptValRooted(cx, cmd));
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
virtual void FlushTurn(const std::vector<SimulationCommand>& commands)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-01 11:48:39 +02:00
|
|
|
ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface();
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
std::vector<SimulationCommand> localCommands;
|
|
|
|
m_LocalQueue.swap(localCommands);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < localCommands.size(); ++i)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-20 02:59:01 +02:00
|
|
|
bool ok = scriptInterface.CallFunctionVoid(scriptInterface.GetGlobalObject(), "ProcessCommand", localCommands[i].player, localCommands[i].data);
|
2010-01-09 20:20:14 +01:00
|
|
|
if (!ok)
|
|
|
|
LOGERROR(L"Failed to call ProcessCommand() global script function");
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
for (size_t i = 0; i < commands.size(); ++i)
|
|
|
|
{
|
|
|
|
bool ok = scriptInterface.CallFunctionVoid(scriptInterface.GetGlobalObject(), "ProcessCommand", commands[i].player, commands[i].data);
|
|
|
|
if (!ok)
|
|
|
|
LOGERROR(L"Failed to call ProcessCommand() global script function");
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(CommandQueue)
|