2011-11-22 01:16:35 +01:00
|
|
|
// Setting this to true will display some warnings when commands
|
|
|
|
// are likely to fail, which may be useful for debugging AIs
|
|
|
|
var g_DebugCommands = false;
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
function ProcessCommand(player, cmd)
|
|
|
|
{
|
2011-07-02 02:06:39 +02:00
|
|
|
// Do some basic checks here that commanding player is valid
|
|
|
|
var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
|
|
|
|
if (!cmpPlayerMan || player < 0)
|
|
|
|
return;
|
2012-05-05 21:22:22 +02:00
|
|
|
|
2011-07-02 02:06:39 +02:00
|
|
|
var playerEnt = cmpPlayerMan.GetPlayerByID(player);
|
|
|
|
if (playerEnt == INVALID_ENTITY)
|
|
|
|
return;
|
2012-05-05 21:22:22 +02:00
|
|
|
|
2011-07-02 02:06:39 +02:00
|
|
|
var cmpPlayer = Engine.QueryInterface(playerEnt, IID_Player);
|
|
|
|
if (!cmpPlayer)
|
|
|
|
return;
|
2012-05-05 21:22:22 +02:00
|
|
|
|
2011-07-02 02:06:39 +02:00
|
|
|
var controlAllUnits = cmpPlayer.CanControlAllUnits();
|
|
|
|
|
2011-11-22 01:16:35 +01:00
|
|
|
// Note: checks of UnitAI targets are not robust enough here, as ownership
|
|
|
|
// can change after the order is issued, they should be checked by UnitAI
|
|
|
|
// when the specific behavior (e.g. attack, garrison) is performed.
|
|
|
|
// (Also it's not ideal if a command silently fails, it's nicer if UnitAI
|
|
|
|
// moves the entities closer to the target before giving up.)
|
|
|
|
|
2011-07-02 02:06:39 +02:00
|
|
|
// Now handle various commands
|
2010-01-09 20:20:14 +01:00
|
|
|
switch (cmd.type)
|
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
case "debug-print":
|
|
|
|
print(cmd.message);
|
|
|
|
break;
|
2012-08-05 16:01:05 +02:00
|
|
|
|
2011-01-12 13:21:41 +01:00
|
|
|
case "chat":
|
|
|
|
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
cmpGuiInterface.PushNotification({"type": "chat", "player": player, "message": cmd.message});
|
|
|
|
break;
|
2012-04-25 22:56:35 +02:00
|
|
|
|
2012-08-05 16:01:05 +02:00
|
|
|
case "cheat":
|
|
|
|
Cheat(cmd);
|
|
|
|
break;
|
|
|
|
|
2012-04-25 22:56:35 +02:00
|
|
|
case "quit":
|
|
|
|
// Let the AI exit the game for testing purposes
|
|
|
|
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
cmpGuiInterface.PushNotification({"type": "quit"});
|
|
|
|
break;
|
2011-01-12 13:21:41 +01:00
|
|
|
|
2011-07-02 02:06:39 +02:00
|
|
|
case "control-all":
|
|
|
|
cmpPlayer.SetControlAllUnits(cmd.flag);
|
|
|
|
break;
|
2011-11-22 01:16:35 +01:00
|
|
|
|
2010-10-31 23:00:28 +01:00
|
|
|
case "reveal-map":
|
2011-06-29 01:24:42 +02:00
|
|
|
// Reveal the map for all players, not just the current player,
|
|
|
|
// primarily to make it obvious to everyone that the player is cheating
|
2010-10-31 23:00:28 +01:00
|
|
|
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
|
2011-06-29 01:24:42 +02:00
|
|
|
cmpRangeManager.SetLosRevealAll(-1, cmd.enable);
|
2010-10-31 23:00:28 +01:00
|
|
|
break;
|
|
|
|
|
2010-02-05 23:00:39 +01:00
|
|
|
case "walk":
|
2011-07-02 02:06:39 +02:00
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
|
2010-09-03 11:55:14 +02:00
|
|
|
cmpUnitAI.Walk(cmd.x, cmd.z, cmd.queued);
|
2011-07-07 19:05:22 +02:00
|
|
|
});
|
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":
|
2011-11-22 01:16:35 +01:00
|
|
|
if (g_DebugCommands && !IsOwnedByEnemyOfPlayer(player, cmd.target))
|
2011-07-02 02:06:39 +02:00
|
|
|
{
|
2011-11-22 01:16:35 +01:00
|
|
|
// This check is for debugging only!
|
|
|
|
warn("Invalid command: attack target is not owned by enemy of player "+player+": "+uneval(cmd));
|
2011-07-02 02:06:39 +02:00
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
|
|
|
|
// See UnitAI.CanAttack for target checks
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
|
2011-11-22 01:16:35 +01:00
|
|
|
cmpUnitAI.Attack(cmd.target, cmd.queued);
|
|
|
|
});
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
2010-01-24 18:24:35 +01:00
|
|
|
|
2012-04-17 22:22:13 +02:00
|
|
|
case "heal":
|
|
|
|
if (g_DebugCommands && !(IsOwnedByPlayer(player, cmd.target) || IsOwnedByAllyOfPlayer(player, cmd.target)))
|
|
|
|
{
|
|
|
|
// This check is for debugging only!
|
|
|
|
warn("Invalid command: heal target is not owned by player "+player+" or their ally: "+uneval(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
// See UnitAI.CanHeal for target checks
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
|
2012-04-17 22:22:13 +02:00
|
|
|
cmpUnitAI.Heal(cmd.target, cmd.queued);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2010-03-12 22:41:40 +01:00
|
|
|
case "repair":
|
|
|
|
// This covers both repairing damaged buildings, and constructing unfinished foundations
|
2011-11-22 01:16:35 +01:00
|
|
|
if (g_DebugCommands && !IsOwnedByAllyOfPlayer(player, cmd.target))
|
2011-07-02 02:06:39 +02:00
|
|
|
{
|
2011-11-22 01:16:35 +01:00
|
|
|
// This check is for debugging only!
|
|
|
|
warn("Invalid command: repair target is not owned by ally of player "+player+": "+uneval(cmd));
|
2011-07-02 02:06:39 +02:00
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
|
|
|
|
// See UnitAI.CanRepair for target checks
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
|
2011-11-22 01:16:35 +01:00
|
|
|
cmpUnitAI.Repair(cmd.target, cmd.autocontinue, cmd.queued);
|
|
|
|
});
|
2010-03-12 22:41:40 +01:00
|
|
|
break;
|
|
|
|
|
2010-02-12 23:46:53 +01:00
|
|
|
case "gather":
|
2011-11-22 01:16:35 +01:00
|
|
|
if (g_DebugCommands && !(IsOwnedByPlayer(player, cmd.target) || IsOwnedByGaia(cmd.target)))
|
2011-09-30 21:54:04 +02:00
|
|
|
{
|
2011-11-22 01:16:35 +01:00
|
|
|
// This check is for debugging only!
|
|
|
|
warn("Invalid command: resource is not owned by gaia or player "+player+": "+uneval(cmd));
|
2011-09-30 21:54:04 +02:00
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
|
|
|
|
// See UnitAI.CanGather for target checks
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
|
2011-11-22 01:16:35 +01:00
|
|
|
cmpUnitAI.Gather(cmd.target, cmd.queued);
|
|
|
|
});
|
2010-02-12 23:46:53 +01:00
|
|
|
break;
|
2011-12-16 17:08:26 +01:00
|
|
|
|
2012-03-02 00:16:01 +01:00
|
|
|
case "gather-near-position":
|
2011-12-16 17:08:26 +01:00
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
|
2012-05-27 22:23:08 +02:00
|
|
|
cmpUnitAI.GatherNearPosition(cmd.x, cmd.z, cmd.resourceType, cmd.resourceTemplate, cmd.queued);
|
2011-12-16 17:08:26 +01:00
|
|
|
});
|
|
|
|
break;
|
2010-02-12 23:46:53 +01:00
|
|
|
|
2010-11-13 20:15:29 +01:00
|
|
|
case "returnresource":
|
2011-07-02 02:06:39 +02:00
|
|
|
// Check dropsite is owned by player
|
2011-11-22 01:16:35 +01:00
|
|
|
if (g_DebugCommands && IsOwnedByPlayer(player, cmd.target))
|
2011-07-02 02:06:39 +02:00
|
|
|
{
|
2011-11-22 01:16:35 +01:00
|
|
|
// This check is for debugging only!
|
|
|
|
warn("Invalid command: dropsite is not owned by player "+player+": "+uneval(cmd));
|
2011-07-02 02:06:39 +02:00
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
|
|
|
|
// See UnitAI.CanReturnResource for target checks
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
|
2011-11-22 01:16:35 +01:00
|
|
|
cmpUnitAI.ReturnResource(cmd.target, cmd.queued);
|
|
|
|
});
|
2010-11-13 20:15:29 +01:00
|
|
|
break;
|
|
|
|
|
2010-04-19 21:47:23 +02:00
|
|
|
case "train":
|
2012-04-21 02:21:01 +02:00
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
|
|
|
// Verify that the building(s) can be controlled by the player
|
|
|
|
if (entities.length > 0)
|
2011-07-02 02:06:39 +02:00
|
|
|
{
|
2012-04-21 02:21:01 +02:00
|
|
|
for each (var ent in entities)
|
2012-04-20 19:21:04 +02:00
|
|
|
{
|
2012-07-20 03:54:24 +02:00
|
|
|
var cmpTechnologyManager = QueryOwnerInterface(ent, IID_TechnologyManager);
|
2012-04-21 02:21:01 +02:00
|
|
|
// TODO: Enable this check once the AI gets technology support
|
2012-07-20 03:54:24 +02:00
|
|
|
if (cmpTechnologyManager.CanProduce(cmd.template) || true)
|
2012-04-21 02:21:01 +02:00
|
|
|
{
|
|
|
|
var queue = Engine.QueryInterface(ent, IID_ProductionQueue);
|
|
|
|
// Check if the building can train the unit
|
|
|
|
if (queue && queue.GetEntitiesList().indexOf(cmd.template) != -1)
|
|
|
|
queue.AddBatch(cmd.template, "unit", +cmd.count, cmd.metadata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
warn("Invalid command: training requires unresearched technology: " + uneval(cmd));
|
|
|
|
}
|
2012-04-20 19:21:04 +02:00
|
|
|
}
|
2011-07-02 02:06:39 +02:00
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
else if (g_DebugCommands)
|
|
|
|
{
|
2012-04-21 02:21:01 +02:00
|
|
|
warn("Invalid command: training building(s) cannot be controlled by player "+player+": "+uneval(cmd));
|
2011-11-22 01:16:35 +01:00
|
|
|
}
|
2010-04-19 21:47:23 +02:00
|
|
|
break;
|
|
|
|
|
2012-04-20 19:21:04 +02:00
|
|
|
case "research":
|
|
|
|
// Verify that the building can be controlled by the player
|
|
|
|
if (CanControlUnit(cmd.entity, player, controlAllUnits))
|
|
|
|
{
|
2012-07-20 03:54:24 +02:00
|
|
|
var cmpTechnologyManager = QueryOwnerInterface(cmd.entity, IID_TechnologyManager);
|
2012-04-20 19:21:04 +02:00
|
|
|
// TODO: Enable this check once the AI gets technology support
|
2012-07-20 03:54:24 +02:00
|
|
|
if (cmpTechnologyManager.CanResearch(cmd.template) || true)
|
2012-04-20 19:21:04 +02:00
|
|
|
{
|
|
|
|
var queue = Engine.QueryInterface(cmd.entity, IID_ProductionQueue);
|
|
|
|
if (queue)
|
|
|
|
queue.AddBatch(cmd.template, "technology");
|
|
|
|
}
|
|
|
|
else if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: Requirements to research technology are not met: " + uneval(cmd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: research building cannot be controlled by player "+player+": "+uneval(cmd));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "stop-production":
|
2011-11-22 01:16:35 +01:00
|
|
|
// Verify that the building can be controlled by the player
|
2011-07-02 02:06:39 +02:00
|
|
|
if (CanControlUnit(cmd.entity, player, controlAllUnits))
|
|
|
|
{
|
2012-04-20 19:21:04 +02:00
|
|
|
var queue = Engine.QueryInterface(cmd.entity, IID_ProductionQueue);
|
2011-07-02 02:06:39 +02:00
|
|
|
if (queue)
|
|
|
|
queue.RemoveBatch(cmd.id);
|
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
else if (g_DebugCommands)
|
|
|
|
{
|
2012-04-20 19:21:04 +02:00
|
|
|
warn("Invalid command: production building cannot be controlled by player "+player+": "+uneval(cmd));
|
2011-11-22 01:16:35 +01:00
|
|
|
}
|
2010-04-19 21:47:23 +02:00
|
|
|
break;
|
|
|
|
|
2010-01-24 18:24:35 +01:00
|
|
|
case "construct":
|
2012-05-05 21:22:22 +02:00
|
|
|
TryConstructBuilding(player, cmpPlayer, controlAllUnits, cmd);
|
|
|
|
break;
|
2010-03-12 22:41:40 +01:00
|
|
|
|
2012-05-05 21:22:22 +02:00
|
|
|
case "construct-wall":
|
|
|
|
TryConstructWall(player, cmpPlayer, controlAllUnits, cmd);
|
2010-01-24 18:24:35 +01:00
|
|
|
break;
|
2011-11-22 01:16:35 +01:00
|
|
|
|
2011-01-12 13:21:41 +01:00
|
|
|
case "delete-entities":
|
2011-07-02 02:06:39 +02:00
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
|
|
|
for each (var ent in entities)
|
2011-01-12 13:21:41 +01:00
|
|
|
{
|
|
|
|
var cmpHealth = Engine.QueryInterface(ent, IID_Health);
|
|
|
|
if (cmpHealth)
|
|
|
|
cmpHealth.Kill();
|
|
|
|
else
|
|
|
|
Engine.DestroyEntity(ent);
|
|
|
|
}
|
2010-08-01 07:09:30 +02:00
|
|
|
break;
|
2010-01-24 18:24:35 +01:00
|
|
|
|
2010-08-05 12:20:47 +02:00
|
|
|
case "set-rallypoint":
|
2011-07-02 02:06:39 +02:00
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
|
|
|
for each (var ent in entities)
|
2010-08-05 12:20:47 +02:00
|
|
|
{
|
|
|
|
var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
|
|
|
|
if (cmpRallyPoint)
|
2011-12-16 17:08:26 +01:00
|
|
|
{
|
2012-05-24 20:25:31 +02:00
|
|
|
if (!cmd.queued)
|
|
|
|
cmpRallyPoint.Unset();
|
|
|
|
|
|
|
|
cmpRallyPoint.AddPosition(cmd.x, cmd.z);
|
|
|
|
cmpRallyPoint.AddData(cmd.data);
|
2011-12-16 17:08:26 +01:00
|
|
|
}
|
2010-08-05 12:20:47 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "unset-rallypoint":
|
2011-07-02 02:06:39 +02:00
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
|
|
|
for each (var ent in entities)
|
2010-08-05 12:20:47 +02:00
|
|
|
{
|
|
|
|
var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
|
|
|
|
if (cmpRallyPoint)
|
|
|
|
cmpRallyPoint.Unset();
|
|
|
|
}
|
|
|
|
break;
|
2011-11-22 01:16:35 +01:00
|
|
|
|
2010-10-01 22:51:21 +02:00
|
|
|
case "defeat-player":
|
|
|
|
// Send "OnPlayerDefeated" message to player
|
2012-08-05 20:07:56 +02:00
|
|
|
Engine.PostMessage(playerEnt, MT_PlayerDefeated, { "playerId": player, "destroy": cmd.destroy } );
|
2010-10-01 22:51:21 +02:00
|
|
|
break;
|
2010-08-05 12:20:47 +02:00
|
|
|
|
2010-10-24 00:43:15 +02:00
|
|
|
case "garrison":
|
2011-11-22 01:16:35 +01:00
|
|
|
// Verify that the building can be controlled by the player
|
2011-07-02 02:06:39 +02:00
|
|
|
if (CanControlUnit(cmd.target, player, controlAllUnits))
|
|
|
|
{
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player).forEach(function(cmpUnitAI) {
|
2011-07-02 02:06:39 +02:00
|
|
|
cmpUnitAI.Garrison(cmd.target);
|
2011-07-07 19:05:22 +02:00
|
|
|
});
|
2011-07-02 02:06:39 +02:00
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
else if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: garrison target cannot be controlled by player "+player+": "+uneval(cmd));
|
|
|
|
}
|
2010-10-24 00:43:15 +02:00
|
|
|
break;
|
2011-11-22 01:16:35 +01:00
|
|
|
|
2010-10-24 00:43:15 +02:00
|
|
|
case "unload":
|
2011-11-22 01:16:35 +01:00
|
|
|
// Verify that the building can be controlled by the player
|
2011-07-02 02:06:39 +02:00
|
|
|
if (CanControlUnit(cmd.garrisonHolder, player, controlAllUnits))
|
|
|
|
{
|
|
|
|
var cmpGarrisonHolder = Engine.QueryInterface(cmd.garrisonHolder, IID_GarrisonHolder);
|
2012-03-21 16:18:31 +01:00
|
|
|
var notUngarrisoned = 0;
|
|
|
|
for each (ent in cmd.entities)
|
|
|
|
{
|
|
|
|
if (!cmpGarrisonHolder || !cmpGarrisonHolder.Unload(ent))
|
|
|
|
{
|
|
|
|
notUngarrisoned++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (notUngarrisoned != 0)
|
2011-08-17 03:43:23 +02:00
|
|
|
{
|
|
|
|
var cmpPlayer = QueryPlayerIDInterface(player, IID_Player);
|
2012-03-21 16:18:31 +01:00
|
|
|
var notification = {"player": cmpPlayer.GetPlayerID(), "message": (notUngarrisoned == 1 ? "Unable to ungarrison unit" : "Unable to ungarrison units")};
|
2011-08-17 03:43:23 +02:00
|
|
|
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
cmpGUIInterface.PushNotification(notification);
|
2012-03-21 16:18:31 +01:00
|
|
|
}
|
2011-07-02 02:06:39 +02:00
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
else if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: unload target cannot be controlled by player "+player+": "+uneval(cmd));
|
|
|
|
}
|
2010-10-24 00:43:15 +02:00
|
|
|
break;
|
2011-11-22 01:16:35 +01:00
|
|
|
|
2010-10-24 00:43:15 +02:00
|
|
|
case "unload-all":
|
2011-11-22 01:16:35 +01:00
|
|
|
// Verify that the building can be controlled by the player
|
2011-07-02 02:06:39 +02:00
|
|
|
if (CanControlUnit(cmd.garrisonHolder, player, controlAllUnits))
|
|
|
|
{
|
|
|
|
var cmpGarrisonHolder = Engine.QueryInterface(cmd.garrisonHolder, IID_GarrisonHolder);
|
2011-08-17 03:43:23 +02:00
|
|
|
if (!cmpGarrisonHolder || !cmpGarrisonHolder.UnloadAll())
|
|
|
|
{
|
|
|
|
var cmpPlayer = QueryPlayerIDInterface(player, IID_Player);
|
|
|
|
var notification = {"player": cmpPlayer.GetPlayerID(), "message": "Unable to ungarrison all units"};
|
|
|
|
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
cmpGUIInterface.PushNotification(notification);
|
|
|
|
}
|
2011-07-02 02:06:39 +02:00
|
|
|
}
|
2011-11-22 01:16:35 +01:00
|
|
|
else if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: unload-all target cannot be controlled by player "+player+": "+uneval(cmd));
|
|
|
|
}
|
2010-10-24 00:43:15 +02:00
|
|
|
break;
|
2011-05-01 22:40:53 +02:00
|
|
|
|
|
|
|
case "formation":
|
2011-07-02 02:06:39 +02:00
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
2012-05-18 23:31:57 +02:00
|
|
|
GetFormationUnitAIs(entities, player, cmd.name).forEach(function(cmpUnitAI) {
|
2011-07-07 19:05:22 +02:00
|
|
|
var cmpFormation = Engine.QueryInterface(cmpUnitAI.entity, IID_Formation);
|
|
|
|
if (!cmpFormation)
|
|
|
|
return;
|
|
|
|
cmpFormation.LoadFormation(cmd.name);
|
|
|
|
cmpFormation.MoveMembersIntoFormation(true);
|
|
|
|
});
|
2011-05-01 22:40:53 +02:00
|
|
|
break;
|
2011-05-15 23:53:24 +02:00
|
|
|
|
|
|
|
case "promote":
|
2011-11-22 01:16:35 +01:00
|
|
|
// No need to do checks here since this is a cheat anyway
|
2011-05-15 23:53:24 +02:00
|
|
|
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
cmpGuiInterface.PushNotification({"type": "chat", "player": player, "message": "(Cheat - promoted units)"});
|
|
|
|
|
|
|
|
for each (var ent in cmd.entities)
|
|
|
|
{
|
|
|
|
var cmpPromotion = Engine.QueryInterface(ent, IID_Promotion);
|
|
|
|
if (cmpPromotion)
|
|
|
|
cmpPromotion.IncreaseXp(cmpPromotion.GetRequiredXp() - cmpPromotion.GetCurrentXp());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-06-18 00:13:39 +02:00
|
|
|
case "stance":
|
2011-07-02 02:06:39 +02:00
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
|
|
|
for each (var ent in entities)
|
2011-06-18 00:13:39 +02:00
|
|
|
{
|
|
|
|
var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
|
|
|
if (cmpUnitAI)
|
|
|
|
cmpUnitAI.SwitchToStance(cmd.name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-07-08 18:25:33 +02:00
|
|
|
case "wall-to-gate":
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
|
|
|
for each (var ent in entities)
|
|
|
|
{
|
2012-07-13 00:49:40 +02:00
|
|
|
TryTransformWallToGate(ent, cmpPlayer, cmd.template);
|
2012-07-08 18:25:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "lock-gate":
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
|
|
|
for each (var ent in entities)
|
|
|
|
{
|
|
|
|
var cmpGate = Engine.QueryInterface(ent, IID_Gate);
|
|
|
|
if (cmpGate)
|
|
|
|
{
|
|
|
|
if (cmd.lock)
|
|
|
|
cmpGate.LockGate();
|
|
|
|
else
|
|
|
|
cmpGate.UnlockGate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-03-08 21:42:28 +01:00
|
|
|
case "setup-trade-route":
|
|
|
|
for each (var ent in cmd.entities)
|
|
|
|
{
|
|
|
|
var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
|
|
|
if (cmpUnitAI)
|
2012-07-25 23:38:56 +02:00
|
|
|
cmpUnitAI.SetupTradeRoute(cmd.target, cmd.source);
|
2012-03-08 21:42:28 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "select-trading-goods":
|
2012-04-06 17:07:04 +02:00
|
|
|
for each (var ent in cmd.entities)
|
|
|
|
{
|
|
|
|
var cmpTrader = Engine.QueryInterface(ent, IID_Trader);
|
|
|
|
if (cmpTrader)
|
|
|
|
cmpTrader.SetPreferredGoods(cmd.preferredGoods);
|
|
|
|
}
|
2012-03-08 21:42:28 +01:00
|
|
|
break;
|
|
|
|
|
2011-11-24 16:43:32 +01:00
|
|
|
case "barter":
|
|
|
|
var cmpBarter = Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter);
|
|
|
|
cmpBarter.ExchangeResources(playerEnt, cmd.sell, cmd.buy, cmd.amount);
|
|
|
|
break;
|
2012-04-25 22:56:35 +02:00
|
|
|
|
|
|
|
case "set-shading-color":
|
|
|
|
// Debug command to make an entity brightly colored
|
|
|
|
for each (var ent in cmd.entities)
|
|
|
|
{
|
|
|
|
var cmpVisual = Engine.QueryInterface(ent, IID_Visual)
|
|
|
|
if (cmpVisual)
|
|
|
|
cmpVisual.SetShadingColour(cmd.rgb[0], cmd.rgb[1], cmd.rgb[2], 0) // alpha isn't used so just send 0
|
|
|
|
}
|
|
|
|
break;
|
2011-11-24 16:43:32 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
default:
|
2011-11-22 01:16:35 +01:00
|
|
|
error("Invalid command: unknown command type: "+uneval(cmd));
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
/**
|
|
|
|
* Get some information about the formations used by entities.
|
2011-07-07 19:05:22 +02:00
|
|
|
* The entities must have a UnitAI component.
|
2010-09-03 11:55:14 +02:00
|
|
|
*/
|
|
|
|
function ExtractFormations(ents)
|
|
|
|
{
|
|
|
|
var entities = []; // subset of ents that have UnitAI
|
|
|
|
var members = {}; // { formationentity: [ent, ent, ...], ... }
|
|
|
|
for each (var ent in ents)
|
|
|
|
{
|
|
|
|
var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
2011-07-07 19:05:22 +02:00
|
|
|
var fid = cmpUnitAI.GetFormationController();
|
|
|
|
if (fid != INVALID_ENTITY)
|
2010-09-03 11:55:14 +02:00
|
|
|
{
|
2011-07-07 19:05:22 +02:00
|
|
|
if (!members[fid])
|
|
|
|
members[fid] = [];
|
|
|
|
members[fid].push(ent);
|
2010-09-03 11:55:14 +02:00
|
|
|
}
|
2011-07-07 19:05:22 +02:00
|
|
|
entities.push(ent);
|
2010-09-03 11:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ids = [ id for (id in members) ];
|
|
|
|
|
|
|
|
return { "entities": entities, "members": members, "ids": ids };
|
|
|
|
}
|
|
|
|
|
2012-05-05 21:22:22 +02:00
|
|
|
/**
|
|
|
|
* Attempts to construct a building using the specified parameters.
|
|
|
|
* Returns true on success, false on failure.
|
|
|
|
*/
|
|
|
|
function TryConstructBuilding(player, cmpPlayer, controlAllUnits, cmd)
|
|
|
|
{
|
|
|
|
// Message structure:
|
|
|
|
// {
|
|
|
|
// "type": "construct",
|
|
|
|
// "entities": [...], // entities that will be ordered to construct the building (if applicable)
|
|
|
|
// "template": "...", // template name of the entity being constructed
|
|
|
|
// "x": ...,
|
|
|
|
// "z": ...,
|
|
|
|
// "angle": ...,
|
|
|
|
// "autorepair": true, // whether to automatically start constructing/repairing the new foundation
|
|
|
|
// "autocontinue": true, // whether to automatically gather/build/etc after finishing this
|
|
|
|
// "queued": true, // whether to add the construction/repairing of this foundation to entities' queue (if applicable)
|
|
|
|
// "obstructionControlGroup": ..., // Optional; the obstruction control group ID that should be set for this building prior to obstruction
|
|
|
|
// // testing to determine placement validity. If specified, must be a valid control group ID (> 0).
|
|
|
|
// "obstructionControlGroup2": ..., // Optional; secondary obstruction control group ID that should be set for this building prior to obstruction
|
|
|
|
// // testing to determine placement validity. May be INVALID_ENTITY.
|
|
|
|
// }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Check whether we can control these units
|
|
|
|
var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
|
|
|
|
if (!entities.length)
|
|
|
|
return false;
|
|
|
|
|
2012-07-20 03:54:24 +02:00
|
|
|
var foundationTemplate = "foundation|" + cmd.template;
|
|
|
|
|
2012-05-05 21:22:22 +02:00
|
|
|
// Tentatively create the foundation (we might find later that it's a invalid build command)
|
2012-07-20 03:54:24 +02:00
|
|
|
var ent = Engine.AddEntity(foundationTemplate);
|
2012-05-05 21:22:22 +02:00
|
|
|
if (ent == INVALID_ENTITY)
|
|
|
|
{
|
|
|
|
// Error (e.g. invalid template names)
|
|
|
|
error("Error creating foundation entity for '" + cmd.template + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the foundation to the right place
|
|
|
|
var cmpPosition = Engine.QueryInterface(ent, IID_Position);
|
|
|
|
cmpPosition.JumpTo(cmd.x, cmd.z);
|
|
|
|
cmpPosition.SetYRotation(cmd.angle);
|
|
|
|
|
|
|
|
// Set the obstruction control group if needed
|
|
|
|
if (cmd.obstructionControlGroup || cmd.obstructionControlGroup2)
|
|
|
|
{
|
|
|
|
var cmpObstruction = Engine.QueryInterface(ent, IID_Obstruction);
|
|
|
|
|
|
|
|
// primary control group must always be valid
|
|
|
|
if (cmd.obstructionControlGroup)
|
|
|
|
{
|
|
|
|
if (cmd.obstructionControlGroup <= 0)
|
|
|
|
warn("[TryConstructBuilding] Invalid primary obstruction control group " + cmd.obstructionControlGroup + " received; must be > 0");
|
|
|
|
|
|
|
|
cmpObstruction.SetControlGroup(cmd.obstructionControlGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.obstructionControlGroup2)
|
|
|
|
cmpObstruction.SetControlGroup2(cmd.obstructionControlGroup2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether it's obstructed by other entities or invalid terrain
|
|
|
|
var cmpBuildRestrictions = Engine.QueryInterface(ent, IID_BuildRestrictions);
|
|
|
|
if (!cmpBuildRestrictions || !cmpBuildRestrictions.CheckPlacement(player))
|
|
|
|
{
|
|
|
|
if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: build restrictions check failed for player "+player+": "+uneval(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
cmpGuiInterface.PushNotification({ "player": player, "message": "Building site was obstructed" });
|
|
|
|
|
|
|
|
// Remove the foundation because the construction was aborted
|
|
|
|
Engine.DestroyEntity(ent);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check build limits
|
|
|
|
var cmpBuildLimits = QueryPlayerIDInterface(player, IID_BuildLimits);
|
|
|
|
if (!cmpBuildLimits || !cmpBuildLimits.AllowedToBuild(cmpBuildRestrictions.GetCategory()))
|
|
|
|
{
|
|
|
|
if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: build limits check failed for player "+player+": "+uneval(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: The UI should tell the user they can't build this (but we still need this check)
|
|
|
|
|
|
|
|
// Remove the foundation because the construction was aborted
|
|
|
|
Engine.DestroyEntity(ent);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-20 04:50:43 +02:00
|
|
|
var cmpTechnologyManager = QueryPlayerIDInterface(player, IID_TechnologyManager);
|
|
|
|
|
2012-05-05 21:22:22 +02:00
|
|
|
// TODO: Enable this check once the AI gets technology support
|
2012-07-20 03:54:24 +02:00
|
|
|
if (!cmpTechnologyManager.CanProduce(cmd.template) && false)
|
2012-05-05 21:22:22 +02:00
|
|
|
{
|
|
|
|
if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: required technology check failed for player "+player+": "+uneval(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
cmpGuiInterface.PushNotification({ "player": player, "message": "Building's technology requirements are not met." });
|
|
|
|
|
|
|
|
// Remove the foundation because the construction was aborted
|
|
|
|
Engine.DestroyEntity(ent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: AI has no visibility info
|
|
|
|
if (!cmpPlayer.IsAI())
|
|
|
|
{
|
|
|
|
// Check whether it's in a visible or fogged region
|
|
|
|
// tell GetLosVisibility to force RetainInFog because preview entities set this to false,
|
|
|
|
// which would show them as hidden instead of fogged
|
|
|
|
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
|
|
|
|
var visible = (cmpRangeManager && cmpRangeManager.GetLosVisibility(ent, player, true) != "hidden");
|
|
|
|
if (!visible)
|
|
|
|
{
|
|
|
|
if (g_DebugCommands)
|
|
|
|
{
|
|
|
|
warn("Invalid command: foundation visibility check failed for player "+player+": "+uneval(cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
cmpGuiInterface.PushNotification({ "player": player, "message": "Building site was not visible" });
|
|
|
|
|
|
|
|
Engine.DestroyEntity(ent);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 04:50:43 +02:00
|
|
|
// We need the cost after tech modifications
|
|
|
|
// To calculate this with an entity requires ownership, so use the template instead
|
|
|
|
var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
|
|
|
|
var template = cmpTemplateManager.GetTemplate(foundationTemplate);
|
|
|
|
var costs = {};
|
|
|
|
for (var r in template.Cost.Resources)
|
|
|
|
{
|
|
|
|
costs[r] = +template.Cost.Resources[r];
|
|
|
|
if (cmpTechnologyManager)
|
|
|
|
costs[r] = cmpTechnologyManager.ApplyModificationsTemplate("Cost/Resources/"+r, costs[r], template);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cmpPlayer.TrySubtractResources(costs))
|
|
|
|
{
|
|
|
|
if (g_DebugCommands)
|
|
|
|
warn("Invalid command: building cost check failed for player "+player+": "+uneval(cmd));
|
|
|
|
|
|
|
|
Engine.DestroyEntity(ent);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-05 21:22:22 +02:00
|
|
|
// 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
|
|
|
|
if (cmd.autorepair)
|
|
|
|
{
|
|
|
|
ProcessCommand(player, {
|
|
|
|
"type": "repair",
|
|
|
|
"entities": entities,
|
|
|
|
"target": ent,
|
|
|
|
"autocontinue": cmd.autocontinue,
|
|
|
|
"queued": cmd.queued
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
function TryConstructWall(player, cmpPlayer, controlAllUnits, cmd)
|
|
|
|
{
|
|
|
|
// 'cmd' message structure:
|
|
|
|
// {
|
|
|
|
// "type": "construct-wall",
|
|
|
|
// "entities": [...], // entities that will be ordered to construct the wall (if applicable)
|
|
|
|
// "pieces": [ // ordered list of information about the pieces making up the wall (towers, wall segments, ...)
|
|
|
|
// {
|
|
|
|
// "template": "...", // one of the templates from the wallset
|
|
|
|
// "x": ...,
|
|
|
|
// "z": ...,
|
|
|
|
// "angle": ...,
|
|
|
|
// },
|
|
|
|
// ...
|
|
|
|
// ],
|
|
|
|
// "wallSet": {
|
|
|
|
// "templates": {
|
|
|
|
// "tower": // tower template name
|
|
|
|
// "long": // long wall segment template name
|
|
|
|
// ... // etc.
|
|
|
|
// },
|
|
|
|
// "maxTowerOverlap": ...,
|
|
|
|
// "minTowerOverlap": ...,
|
|
|
|
// },
|
|
|
|
// "startSnappedEntity": // optional; entity ID of tower being snapped to at the starting side of the wall
|
|
|
|
// "endSnappedEntity": // optional; entity ID of tower being snapped to at the ending side of the wall
|
|
|
|
// "autorepair": true, // whether to automatically start constructing/repairing the new foundation
|
|
|
|
// "autocontinue": true, // whether to automatically gather/build/etc after finishing this
|
|
|
|
// "queued": true, // whether to add the construction/repairing of this wall's pieces to entities' queue (if applicable)
|
|
|
|
// }
|
|
|
|
|
|
|
|
if (cmd.pieces.length <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cmd.startSnappedEntity && cmd.pieces[0].template == cmd.wallSet.templates.tower)
|
|
|
|
{
|
|
|
|
error("[TryConstructWall] Starting wall piece cannot be a tower (" + cmd.wallSet.templates.tower + ") when snapping at the starting side");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.endSnappedEntity && cmd.pieces[cmd.pieces.length - 1].template == cmd.wallSet.templates.tower)
|
|
|
|
{
|
|
|
|
error("[TryConstructWall] Ending wall piece cannot be a tower (" + cmd.wallSet.templates.tower + ") when snapping at the ending side");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign obstruction control groups to allow the wall pieces to mutually overlap during foundation placement
|
|
|
|
// and during construction. The scheme here is that whatever wall pieces are inbetween two towers inherit the control
|
|
|
|
// groups of both of the towers they are connected to (either newly constructed ones as part of the wall, or existing
|
|
|
|
// towers in the case of snapping). The towers themselves all keep their default unique control groups.
|
|
|
|
|
|
|
|
// To support this, every non-tower piece registers the entity ID of the towers (or foundations thereof) that neighbour
|
|
|
|
// it on either side. Specifically, each non-tower wall piece has its primary control group set equal to that of the
|
|
|
|
// first tower encountered towards the starting side of the wall, and its secondary control group set equal to that of
|
|
|
|
// the first tower encountered towards the ending side of the wall (if any).
|
|
|
|
|
|
|
|
// We can't build the whole wall at once by linearly stepping through the wall pieces and build them, because the
|
|
|
|
// wall segments may/will need the entity IDs of towers that come afterwards. So, build it in two passes:
|
|
|
|
//
|
|
|
|
// FIRST PASS:
|
|
|
|
// - Go from start to end and construct wall piece foundations as far as we can without running into a piece that
|
|
|
|
// cannot be built (e.g. because it is obstructed). At each non-tower, set the most recently built tower's ID
|
|
|
|
// as the primary control group, thus allowing it to be built overlapping the previous piece.
|
|
|
|
// - If we encounter a new tower along the way (which will gain its own control group), do the following:
|
|
|
|
// o First build it using temporarily the same control group of the previous (non-tower) piece
|
|
|
|
// o Set the previous piece's secondary control group to the tower's entity ID
|
|
|
|
// o Restore the primary control group of the constructed tower back its original (unique) value.
|
|
|
|
// The temporary control group is necessary to allow the newer tower with its unique control group ID to be able
|
|
|
|
// to be placed while overlapping the previous piece.
|
|
|
|
//
|
|
|
|
// SECOND PASS:
|
|
|
|
// - Go end to start from the last successfully placed wall piece (which might be a tower we backtracked to), this
|
|
|
|
// time registering the right neighbouring tower in each non-tower piece.
|
|
|
|
|
|
|
|
// first pass; L -> R
|
|
|
|
|
|
|
|
var lastTowerIndex = -1; // index of the last tower we've encountered in cmd.pieces
|
|
|
|
var lastTowerControlGroup = null; // control group of the last tower we've encountered, to assign to non-tower pieces
|
|
|
|
|
|
|
|
// If we're snapping to an existing entity at the starting end, set lastTowerControlGroup to its control group ID so that
|
|
|
|
// the first wall piece can be built while overlapping it.
|
|
|
|
if (cmd.startSnappedEntity)
|
|
|
|
{
|
|
|
|
var cmpSnappedStartObstruction = Engine.QueryInterface(cmd.startSnappedEntity, IID_Obstruction);
|
|
|
|
if (!cmpSnappedStartObstruction)
|
|
|
|
{
|
|
|
|
error("[TryConstructWall] Snapped entity on starting side does not have an obstruction component");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastTowerControlGroup = cmpSnappedStartObstruction.GetControlGroup();
|
|
|
|
//warn("setting lastTowerControlGroup to control group of start snapped entity " + cmd.startSnappedEntity + ": " + lastTowerControlGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
for (; i < cmd.pieces.length; ++i)
|
|
|
|
{
|
|
|
|
var piece = cmd.pieces[i];
|
|
|
|
|
|
|
|
// 'lastTowerControlGroup' must always be defined and valid here, except if we're at the first piece and we didn't do
|
|
|
|
// start position snapping (implying that the first entity we build must be a tower)
|
|
|
|
if (lastTowerControlGroup === null || lastTowerControlGroup == INVALID_ENTITY)
|
|
|
|
{
|
|
|
|
if (!(i == 0 && piece.template == cmd.wallSet.templates.tower && !cmd.startSnappedEntity))
|
|
|
|
{
|
|
|
|
error("[TryConstructWall] Expected last tower control group to be available, none found (1st pass, iteration " + i + ")");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var constructPieceCmd = {
|
|
|
|
"type": "construct",
|
|
|
|
"entities": cmd.entities,
|
|
|
|
"template": piece.template,
|
|
|
|
"x": piece.x,
|
|
|
|
"z": piece.z,
|
|
|
|
"angle": piece.angle,
|
|
|
|
"autorepair": cmd.autorepair,
|
|
|
|
"autocontinue": cmd.autocontinue,
|
|
|
|
"queued": cmd.queued,
|
|
|
|
// Regardless of whether we're building a tower or an intermediate wall piece, it is always (first) constructed
|
|
|
|
// using the control group of the last tower (see comments above).
|
|
|
|
"obstructionControlGroup": lastTowerControlGroup,
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we're building the last piece and we're attaching to a snapped entity, we need to add in the snapped entity's
|
|
|
|
// control group directly at construction time (instead of setting it in the second pass) to allow it to be built
|
|
|
|
// while overlapping the snapped entity.
|
|
|
|
if (i == cmd.pieces.length - 1 && cmd.endSnappedEntity)
|
|
|
|
{
|
|
|
|
var cmpEndSnappedObstruction = Engine.QueryInterface(cmd.endSnappedEntity, IID_Obstruction);
|
|
|
|
if (cmpEndSnappedObstruction)
|
|
|
|
constructPieceCmd.obstructionControlGroup2 = cmpEndSnappedObstruction.GetControlGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
var pieceEntityId = TryConstructBuilding(player, cmpPlayer, controlAllUnits, constructPieceCmd);
|
|
|
|
if (pieceEntityId)
|
|
|
|
{
|
|
|
|
// wall piece foundation successfully built, save the entity ID in the piece info object so we can reference it later
|
|
|
|
piece.ent = pieceEntityId;
|
|
|
|
|
|
|
|
// if we built a tower, do the control group dance (see outline above) and update lastTowerControlGroup and lastTowerIndex
|
|
|
|
if (piece.template == cmd.wallSet.templates.tower)
|
|
|
|
{
|
|
|
|
var cmpTowerObstruction = Engine.QueryInterface(pieceEntityId, IID_Obstruction);
|
|
|
|
var newTowerControlGroup = pieceEntityId;
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
//warn(" updating previous wall piece's secondary control group to " + newTowerControlGroup);
|
|
|
|
var cmpPreviousObstruction = Engine.QueryInterface(cmd.pieces[i-1].ent, IID_Obstruction);
|
|
|
|
// TODO: ensure that cmpPreviousObstruction exists
|
|
|
|
// TODO: ensure that the previous obstruction does not yet have a secondary control group set
|
|
|
|
cmpPreviousObstruction.SetControlGroup2(newTowerControlGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ensure that cmpTowerObstruction exists
|
|
|
|
cmpTowerObstruction.SetControlGroup(newTowerControlGroup); // give the tower its own unique control group
|
|
|
|
|
|
|
|
lastTowerIndex = i;
|
|
|
|
lastTowerControlGroup = newTowerControlGroup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// failed to build wall piece, abort
|
|
|
|
i = j + 1; // compensate for the -1 subtracted by lastBuiltPieceIndex below
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastBuiltPieceIndex = i - 1;
|
|
|
|
var wallComplete = (lastBuiltPieceIndex == cmd.pieces.length - 1);
|
|
|
|
|
|
|
|
// At this point, 'i' is the index of the last wall piece that was successfully constructed (which may or may not be a tower).
|
|
|
|
// Now do the second pass going right-to-left, registering the control groups of the towers to the right of each piece (if any)
|
|
|
|
// as their secondary control groups.
|
|
|
|
|
|
|
|
lastTowerControlGroup = null; // control group of the last tower we've encountered, to assign to non-tower pieces
|
|
|
|
|
|
|
|
// only start off with the ending side's snapped tower's control group if we were able to build the entire wall
|
|
|
|
if (cmd.endSnappedEntity && wallComplete)
|
|
|
|
{
|
|
|
|
var cmpSnappedEndObstruction = Engine.QueryInterface(cmd.endSnappedEntity, IID_Obstruction);
|
|
|
|
if (!cmpSnappedEndObstruction)
|
|
|
|
{
|
|
|
|
error("[TryConstructWall] Snapped entity on ending side does not have an obstruction component");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastTowerControlGroup = cmpSnappedEndObstruction.GetControlGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var j = lastBuiltPieceIndex; j >= 0; --j)
|
|
|
|
{
|
|
|
|
var piece = cmd.pieces[j];
|
|
|
|
|
|
|
|
if (!piece.ent)
|
|
|
|
{
|
|
|
|
error("[TryConstructWall] No entity ID set for constructed entity of template '" + piece.template + "'");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmpPieceObstruction = Engine.QueryInterface(piece.ent, IID_Obstruction);
|
|
|
|
if (!cmpPieceObstruction)
|
|
|
|
{
|
|
|
|
error("[TryConstructWall] Wall piece of template '" + piece.template + "' has no Obstruction component");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (piece.template == cmd.wallSet.templates.tower)
|
|
|
|
{
|
|
|
|
// encountered a tower entity, update the last tower control group
|
|
|
|
lastTowerControlGroup = cmpPieceObstruction.GetControlGroup();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Encountered a non-tower entity, update its secondary control group to 'lastTowerControlGroup'.
|
|
|
|
// Note that the wall piece may already have its secondary control group set to the tower's entity ID from a control group
|
|
|
|
// dance during the first pass, in which case we should validate it against 'lastTowerControlGroup'.
|
|
|
|
|
|
|
|
var existingSecondaryControlGroup = cmpPieceObstruction.GetControlGroup2();
|
|
|
|
if (existingSecondaryControlGroup == INVALID_ENTITY)
|
|
|
|
{
|
|
|
|
if (lastTowerControlGroup != null && lastTowerControlGroup != INVALID_ENTITY)
|
|
|
|
{
|
|
|
|
cmpPieceObstruction.SetControlGroup2(lastTowerControlGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (existingSecondaryControlGroup != lastTowerControlGroup)
|
|
|
|
{
|
|
|
|
error("[TryConstructWall] Existing secondary control group of non-tower entity does not match expected value (2nd pass, iteration " + j + ")");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
/**
|
|
|
|
* Remove the given list of entities from their current formations.
|
|
|
|
*/
|
|
|
|
function RemoveFromFormation(ents)
|
|
|
|
{
|
|
|
|
var formation = ExtractFormations(ents);
|
|
|
|
for (var fid in formation.members)
|
|
|
|
{
|
|
|
|
var cmpFormation = Engine.QueryInterface(+fid, IID_Formation);
|
|
|
|
if (cmpFormation)
|
|
|
|
cmpFormation.RemoveMembers(formation.members[fid]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-07 19:05:22 +02:00
|
|
|
* Returns a list of UnitAI components, each belonging either to a
|
|
|
|
* selected unit or to a formation entity for groups of the selected units.
|
2010-09-03 11:55:14 +02:00
|
|
|
*/
|
2012-05-18 23:31:57 +02:00
|
|
|
function GetFormationUnitAIs(ents, player, formName)
|
2010-09-03 11:55:14 +02:00
|
|
|
{
|
|
|
|
// If an individual was selected, remove it from any formation
|
|
|
|
// and command it individually
|
|
|
|
if (ents.length == 1)
|
|
|
|
{
|
2011-07-07 19:05:22 +02:00
|
|
|
// Skip unit if it has no UnitAI
|
|
|
|
var cmpUnitAI = Engine.QueryInterface(ents[0], IID_UnitAI);
|
|
|
|
if (!cmpUnitAI)
|
|
|
|
return [];
|
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
RemoveFromFormation(ents);
|
|
|
|
|
2011-07-07 19:05:22 +02:00
|
|
|
return [ cmpUnitAI ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Separate out the units that don't support the chosen formation
|
|
|
|
var formedEnts = [];
|
|
|
|
var nonformedUnitAIs = [];
|
|
|
|
for each (var ent in ents)
|
|
|
|
{
|
|
|
|
// Skip units with no UnitAI
|
|
|
|
var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
|
|
|
if (!cmpUnitAI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
|
2012-05-04 01:32:10 +02:00
|
|
|
// TODO: We only check if the formation is usable by some units
|
|
|
|
// if we move them to it. We should check if we can use formations
|
|
|
|
// for the other cases.
|
|
|
|
// We only use "LineClosed" instead of "Line Closed" to access the templates.
|
|
|
|
if (cmpIdentity && cmpIdentity.CanUseFormation(formName === undefined ? "LineClosed" : formName.replace(/\s+/,'')))
|
2011-07-07 19:05:22 +02:00
|
|
|
formedEnts.push(ent);
|
|
|
|
else
|
|
|
|
nonformedUnitAIs.push(cmpUnitAI);
|
2010-09-03 11:55:14 +02:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:05:22 +02:00
|
|
|
if (formedEnts.length == 0)
|
2010-09-03 11:55:14 +02:00
|
|
|
{
|
2011-07-07 19:05:22 +02:00
|
|
|
// No units support the foundation - return all the others
|
|
|
|
return nonformedUnitAIs;
|
2010-09-03 11:55:14 +02:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:05:22 +02:00
|
|
|
// Find what formations the formationable selected entities are currently in
|
|
|
|
var formation = ExtractFormations(formedEnts);
|
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
var formationEnt = undefined;
|
|
|
|
if (formation.ids.length == 1)
|
|
|
|
{
|
2012-03-30 22:09:50 +02:00
|
|
|
// Selected units either belong to this formation or have no formation
|
|
|
|
// Check that all its members are selected
|
2010-09-03 11:55:14 +02:00
|
|
|
var fid = formation.ids[0];
|
|
|
|
var cmpFormation = Engine.QueryInterface(+fid, IID_Formation);
|
2012-03-30 22:09:50 +02:00
|
|
|
if (cmpFormation && cmpFormation.GetMemberCount() == formation.members[fid].length
|
|
|
|
&& cmpFormation.GetMemberCount() == formation.entities.length)
|
2010-09-03 11:55:14 +02:00
|
|
|
{
|
|
|
|
// The whole formation was selected, so reuse its controller for this command
|
|
|
|
formationEnt = +fid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!formationEnt)
|
|
|
|
{
|
|
|
|
// We need to give the selected units a new formation controller
|
|
|
|
|
|
|
|
// Remove selected units from their current formation
|
|
|
|
for (var fid in formation.members)
|
|
|
|
{
|
|
|
|
var cmpFormation = Engine.QueryInterface(+fid, IID_Formation);
|
|
|
|
if (cmpFormation)
|
|
|
|
cmpFormation.RemoveMembers(formation.members[fid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new controller
|
|
|
|
formationEnt = Engine.AddEntity("special/formation");
|
|
|
|
var cmpFormation = Engine.QueryInterface(formationEnt, IID_Formation);
|
|
|
|
cmpFormation.SetMembers(formation.entities);
|
2011-05-01 22:40:53 +02:00
|
|
|
|
2012-05-18 23:31:57 +02:00
|
|
|
var cmpOwnership = Engine.QueryInterface(formationEnt, IID_Ownership);
|
|
|
|
cmpOwnership.SetOwner(player);
|
|
|
|
|
2011-05-01 22:40:53 +02:00
|
|
|
// If all the selected units were previously in formations of the same shape,
|
|
|
|
// then set this new formation to that shape too; otherwise use the default shape
|
|
|
|
var lastFormationName = undefined;
|
|
|
|
for each (var ent in formation.entities)
|
|
|
|
{
|
|
|
|
var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
|
|
|
if (cmpUnitAI)
|
|
|
|
{
|
|
|
|
var name = cmpUnitAI.GetLastFormationName();
|
|
|
|
if (lastFormationName === undefined)
|
|
|
|
{
|
|
|
|
lastFormationName = name;
|
|
|
|
}
|
|
|
|
else if (lastFormationName != name)
|
|
|
|
{
|
|
|
|
lastFormationName = undefined;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var formationName;
|
|
|
|
if (lastFormationName)
|
|
|
|
formationName = lastFormationName;
|
|
|
|
else
|
|
|
|
formationName = "Line Closed";
|
|
|
|
|
|
|
|
if (CanMoveEntsIntoFormation(formation.entities, formationName))
|
|
|
|
{
|
|
|
|
cmpFormation.LoadFormation(formationName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-21 17:45:02 +01:00
|
|
|
cmpFormation.LoadFormation("Scatter");
|
2011-05-01 22:40:53 +02:00
|
|
|
}
|
2010-09-03 11:55:14 +02:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:05:22 +02:00
|
|
|
return nonformedUnitAIs.concat(Engine.QueryInterface(formationEnt, IID_UnitAI));
|
2010-09-03 11:55:14 +02:00
|
|
|
}
|
|
|
|
|
2012-03-21 17:45:02 +01:00
|
|
|
function GetFormationRequirements(formationName)
|
|
|
|
{
|
|
|
|
var countRequired = 1;
|
|
|
|
var classesRequired;
|
|
|
|
switch(formationName)
|
|
|
|
{
|
|
|
|
case "Scatter":
|
|
|
|
case "Column Closed":
|
|
|
|
case "Line Closed":
|
|
|
|
case "Column Open":
|
|
|
|
case "Line Open":
|
|
|
|
case "Battle Line":
|
|
|
|
break;
|
|
|
|
case "Box":
|
|
|
|
countRequired = 4;
|
|
|
|
break;
|
|
|
|
case "Flank":
|
|
|
|
countRequired = 8;
|
|
|
|
break;
|
|
|
|
case "Skirmish":
|
|
|
|
classesRequired = ["Ranged"];
|
|
|
|
break;
|
|
|
|
case "Wedge":
|
|
|
|
countRequired = 3;
|
|
|
|
classesRequired = ["Cavalry"];
|
|
|
|
break;
|
|
|
|
case "Phalanx":
|
|
|
|
countRequired = 10;
|
|
|
|
classesRequired = ["Melee", "Infantry"];
|
|
|
|
break;
|
|
|
|
case "Syntagma":
|
|
|
|
countRequired = 9;
|
|
|
|
classesRequired = ["Melee", "Infantry"]; // TODO: pike only
|
|
|
|
break;
|
|
|
|
case "Testudo":
|
|
|
|
countRequired = 9;
|
|
|
|
classesRequired = ["Melee", "Infantry"];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// We encountered a unknown formation -> warn the user
|
|
|
|
warn("Commands.js: GetFormationRequirements: unknown formation: " + formationName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return { "count": countRequired, "classesRequired": classesRequired };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-01 22:40:53 +02:00
|
|
|
function CanMoveEntsIntoFormation(ents, formationName)
|
|
|
|
{
|
|
|
|
var count = ents.length;
|
|
|
|
|
|
|
|
// TODO: should check the player's civ is allowed to use this formation
|
2012-05-04 01:32:10 +02:00
|
|
|
// See simulation/components/Player.js GetFormations() for a list of all allowed formations
|
2011-05-01 22:40:53 +02:00
|
|
|
|
2012-03-21 17:45:02 +01:00
|
|
|
var requirements = GetFormationRequirements(formationName);
|
|
|
|
if (!requirements)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (count < requirements.count)
|
2011-05-01 22:40:53 +02:00
|
|
|
return false;
|
|
|
|
|
2012-03-21 17:45:02 +01:00
|
|
|
var scatterOnlyUnits = true;
|
2011-05-01 22:40:53 +02:00
|
|
|
for each (var ent in ents)
|
|
|
|
{
|
|
|
|
var cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
|
|
|
|
if (cmpIdentity)
|
|
|
|
{
|
|
|
|
var classes = cmpIdentity.GetClassesList();
|
2012-03-21 17:45:02 +01:00
|
|
|
if (scatterOnlyUnits && (classes.indexOf("Worker") == -1 || classes.indexOf("Support") == -1))
|
|
|
|
scatterOnlyUnits = false;
|
|
|
|
for each (var classRequired in requirements.classesRequired)
|
2011-05-01 22:40:53 +02:00
|
|
|
{
|
|
|
|
if (classes.indexOf(classRequired) == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 17:45:02 +01:00
|
|
|
if (scatterOnlyUnits)
|
2011-05-01 22:40:53 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-02 02:06:39 +02:00
|
|
|
/**
|
|
|
|
* Check if player can control this entity
|
|
|
|
* returns: true if the entity is valid and owned by the player if
|
|
|
|
* or control all units is activated for the player, else false
|
|
|
|
*/
|
|
|
|
function CanControlUnit(entity, player, controlAll)
|
|
|
|
{
|
2011-08-06 10:11:05 +02:00
|
|
|
return (IsOwnedByPlayer(player, entity) || controlAll);
|
2011-07-02 02:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter entities which the player can control
|
|
|
|
*/
|
|
|
|
function FilterEntityList(entities, player, controlAll)
|
|
|
|
{
|
|
|
|
return entities.filter(function(ent) { return CanControlUnit(ent, player, controlAll);} );
|
|
|
|
}
|
|
|
|
|
2012-07-08 18:25:33 +02:00
|
|
|
/**
|
|
|
|
* Try to transform a wall to a gate
|
|
|
|
*/
|
2012-07-13 00:49:40 +02:00
|
|
|
function TryTransformWallToGate(ent, cmpPlayer, template)
|
2012-07-08 18:25:33 +02:00
|
|
|
{
|
|
|
|
var cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
|
|
|
|
if (!cmpIdentity)
|
|
|
|
return;
|
2012-07-18 05:56:18 +02:00
|
|
|
|
|
|
|
// Check if this is a valid long wall segment
|
|
|
|
if (!cmpIdentity.HasClass("LongWall"))
|
|
|
|
{
|
|
|
|
if (g_DebugCommands)
|
|
|
|
warn("Invalid command: invalid wall conversion to gate for player "+player+": "+uneval(cmd));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-08 18:25:33 +02:00
|
|
|
var civ = cmpIdentity.GetCiv();
|
|
|
|
var gate = Engine.AddEntity(template);
|
|
|
|
|
|
|
|
var cmpCost = Engine.QueryInterface(gate, IID_Cost);
|
|
|
|
if (!cmpPlayer.TrySubtractResources(cmpCost.GetResourceCosts()))
|
|
|
|
{
|
|
|
|
if (g_DebugCommands)
|
2012-07-18 05:56:18 +02:00
|
|
|
warn("Invalid command: convert gate cost check failed for player "+player+": "+uneval(cmd));
|
|
|
|
|
2012-07-08 18:25:33 +02:00
|
|
|
Engine.DestroyEntity(gate);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplaceBuildingWith(ent, gate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unconditionally replace a building with another one
|
|
|
|
*/
|
|
|
|
function ReplaceBuildingWith(ent, building)
|
|
|
|
{
|
|
|
|
// Move the building to the right place
|
|
|
|
var cmpPosition = Engine.QueryInterface(ent, IID_Position);
|
|
|
|
var cmpBuildingPosition = Engine.QueryInterface(building, IID_Position);
|
|
|
|
var pos = cmpPosition.GetPosition2D();
|
|
|
|
cmpBuildingPosition.JumpTo(pos.x, pos.y);
|
|
|
|
var rot = cmpPosition.GetRotation();
|
|
|
|
cmpBuildingPosition.SetYRotation(rot.y);
|
|
|
|
cmpBuildingPosition.SetXZRotation(rot.x, rot.z);
|
|
|
|
|
|
|
|
// Copy ownership
|
|
|
|
var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
|
|
|
|
var cmpBuildingOwnership = Engine.QueryInterface(building, IID_Ownership);
|
|
|
|
cmpBuildingOwnership.SetOwner(cmpOwnership.GetOwner());
|
|
|
|
|
|
|
|
// Copy control groups
|
|
|
|
var cmpObstruction = Engine.QueryInterface(ent, IID_Obstruction);
|
|
|
|
var cmpBuildingObstruction = Engine.QueryInterface(building, IID_Obstruction);
|
|
|
|
cmpBuildingObstruction.SetControlGroup(cmpObstruction.GetControlGroup());
|
|
|
|
cmpBuildingObstruction.SetControlGroup2(cmpObstruction.GetControlGroup2());
|
|
|
|
|
|
|
|
PlaySound("constructed", building);
|
|
|
|
|
|
|
|
Engine.PostMessage(ent, MT_ConstructionFinished,
|
|
|
|
{ "entity": ent, "newentity": building });
|
|
|
|
Engine.BroadcastMessage(MT_EntityRenamed, { entity: ent, newentity: building });
|
|
|
|
|
|
|
|
Engine.DestroyEntity(ent);
|
|
|
|
}
|
|
|
|
|
2012-03-21 17:45:02 +01:00
|
|
|
Engine.RegisterGlobal("GetFormationRequirements", GetFormationRequirements);
|
2011-05-01 22:40:53 +02:00
|
|
|
Engine.RegisterGlobal("CanMoveEntsIntoFormation", CanMoveEntsIntoFormation);
|
2010-01-09 20:20:14 +01:00
|
|
|
Engine.RegisterGlobal("ProcessCommand", ProcessCommand);
|