2010-01-09 20:20:14 +01:00
|
|
|
function ProcessCommand(player, cmd)
|
|
|
|
{
|
|
|
|
// print("command: " + player + " " + uneval(cmd) + "\n");
|
|
|
|
|
|
|
|
switch (cmd.type)
|
|
|
|
{
|
2010-02-05 23:00:39 +01:00
|
|
|
case "walk":
|
2010-01-09 20:20:14 +01:00
|
|
|
for each (var ent in cmd.entities)
|
|
|
|
{
|
2010-02-05 23:00:39 +01:00
|
|
|
var ai = Engine.QueryInterface(ent, IID_UnitAI);
|
|
|
|
if (!ai)
|
2010-01-09 20:20:14 +01:00
|
|
|
continue;
|
2010-02-05 23:00:39 +01:00
|
|
|
ai.Walk(cmd.x, cmd.z);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
break;
|
2010-01-24 18:24:35 +01:00
|
|
|
|
2010-02-05 23:00:39 +01:00
|
|
|
case "attack":
|
2010-01-09 20:20:14 +01:00
|
|
|
for each (var ent in cmd.entities)
|
|
|
|
{
|
2010-02-05 23:00:39 +01:00
|
|
|
var ai = Engine.QueryInterface(ent, IID_UnitAI);
|
|
|
|
if (!ai)
|
2010-01-09 20:20:14 +01:00
|
|
|
continue;
|
2010-02-05 23:00:39 +01:00
|
|
|
ai.Attack(cmd.target);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
break;
|
2010-01-24 18:24:35 +01:00
|
|
|
|
2010-03-12 22:41:40 +01:00
|
|
|
case "repair":
|
|
|
|
// This covers both repairing damaged buildings, and constructing unfinished foundations
|
|
|
|
for each (var ent in cmd.entities)
|
|
|
|
{
|
|
|
|
var ai = Engine.QueryInterface(ent, IID_UnitAI);
|
|
|
|
if (!ai)
|
|
|
|
continue;
|
|
|
|
ai.Repair(cmd.target);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-02-12 23:46:53 +01:00
|
|
|
case "gather":
|
|
|
|
for each (var ent in cmd.entities)
|
|
|
|
{
|
|
|
|
var ai = Engine.QueryInterface(ent, IID_UnitAI);
|
|
|
|
if (!ai)
|
|
|
|
continue;
|
|
|
|
ai.Gather(cmd.target);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-01-24 18:24:35 +01:00
|
|
|
case "construct":
|
2010-03-12 22:41:40 +01:00
|
|
|
/*
|
|
|
|
* Construction process:
|
|
|
|
* . Take resources away immediately.
|
|
|
|
* . Create a foundation entity with 1hp, 0% build progress.
|
|
|
|
* . Increase hp and build progress up to 100% when people work on it.
|
|
|
|
* . If it's destroyed, an appropriate fraction of the resource cost is refunded.
|
|
|
|
* . If it's completed, it gets replaced with the real building.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Find the player
|
|
|
|
var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
|
|
|
|
var playerEnt = cmpPlayerMan.GetPlayerByID(player);
|
|
|
|
var cmpPlayer = Engine.QueryInterface(playerEnt, IID_Player);
|
|
|
|
|
|
|
|
// Tentatively create the foundation (we don't know yet if the player can really afford it)
|
|
|
|
var ent = Engine.AddEntity("foundation|" + cmd.template);
|
|
|
|
// TODO: report errors (e.g. invalid template names)
|
|
|
|
|
|
|
|
var cmpCost = Engine.QueryInterface(ent, IID_Cost);
|
|
|
|
if (!cmpPlayer.TrySubtractResources(cmpCost.GetResourceCosts()))
|
2010-01-24 18:24:35 +01:00
|
|
|
{
|
2010-03-12 22:41:40 +01:00
|
|
|
// TODO: report error to player (they ran out of resources)
|
|
|
|
|
|
|
|
// Remove the foundation because the construction was aborted
|
|
|
|
Engine.DestroyEntity(ent);
|
|
|
|
|
|
|
|
break;
|
2010-01-24 18:24:35 +01:00
|
|
|
}
|
2010-03-12 22:41:40 +01:00
|
|
|
|
|
|
|
// Move the foundation to the right place
|
|
|
|
var cmpPosition = Engine.QueryInterface(ent, IID_Position);
|
|
|
|
cmpPosition.JumpTo(cmd.x, cmd.z);
|
|
|
|
cmpPosition.SetYRotation(cmd.angle);
|
|
|
|
|
|
|
|
// Make it owned by the current player
|
|
|
|
var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
|
|
|
|
cmpOwnership.SetOwner(player);
|
|
|
|
|
|
|
|
// Initialise the foundation
|
|
|
|
var cmpFoundation = Engine.QueryInterface(ent, IID_Foundation);
|
|
|
|
cmpFoundation.InitialiseConstruction(player, cmd.template);
|
|
|
|
|
|
|
|
// Tell the units to start building this new entity
|
|
|
|
ProcessCommand(player, {
|
|
|
|
"type": "repair",
|
|
|
|
"entities": cmd.entities,
|
|
|
|
"target": ent
|
|
|
|
});
|
|
|
|
|
2010-01-24 18:24:35 +01:00
|
|
|
break;
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
default:
|
|
|
|
print("Ignoring unrecognised command type '" + cmd.type + "'\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine.RegisterGlobal("ProcessCommand", ProcessCommand);
|