Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/helpers/Commands.js
Show All 40 Lines | function ProcessCommand(player, cmd) | ||||
// Note: checks of UnitAI targets are not robust enough here, as ownership | // 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 | // can change after the order is issued, they should be checked by UnitAI | ||||
// when the specific behavior (e.g. attack, garrison) is performed. | // 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 | // (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.) | // moves the entities closer to the target before giving up.) | ||||
// Now handle various commands | // Now handle various commands | ||||
if (g_Commands[cmd.type]) | if (g_Commands[cmd.type]) | ||||
Lint: no-use-before-define: 'g_Commands' was used before it was defined. | |||||
{ | { | ||||
var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); | var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); | ||||
cmpTrigger.CallEvent("OnPlayerCommand", { "player": player, "cmd": cmd }); | cmpTrigger.CallEvent("OnPlayerCommand", { "player": player, "cmd": cmd }); | ||||
g_Commands[cmd.type](player, cmd, data); | g_Commands[cmd.type](player, cmd, data); | ||||
Lint: no-use-before-define 'g_Commands' was used before it was defined. Lint: no-use-before-define: 'g_Commands' was used before it was defined. | |||||
} | } | ||||
else | else | ||||
error("Invalid command: unknown command type: "+uneval(cmd)); | error("Invalid command: unknown command type: "+uneval(cmd)); | ||||
} | } | ||||
var g_Commands = { | var g_Commands = { | ||||
"aichat": function(player, cmd, data) | "aichat": function(player, cmd, data) | ||||
▲ Show 20 Lines • Show All 286 Lines • ▼ Show 20 Lines | for (let ent of data.entities) | ||||
var cmpTechnologyManager = QueryOwnerInterface(ent, IID_TechnologyManager); | var cmpTechnologyManager = QueryOwnerInterface(ent, IID_TechnologyManager); | ||||
if (cmpTechnologyManager && !cmpTechnologyManager.CanProduce(cmd.template)) | if (cmpTechnologyManager && !cmpTechnologyManager.CanProduce(cmd.template)) | ||||
{ | { | ||||
if (g_DebugCommands) | if (g_DebugCommands) | ||||
warn("Invalid command: training requires unresearched technology: " + uneval(cmd)); | warn("Invalid command: training requires unresearched technology: " + uneval(cmd)); | ||||
continue; | continue; | ||||
} | } | ||||
var queue = Engine.QueryInterface(ent, IID_ProductionQueue); | const cmpTrainer = Engine.QueryInterface(ent, IID_Trainer); | ||||
if (!cmpTrainer) | |||||
continue; | |||||
let templateName = cmd.template; | |||||
// Check if the building can train the unit | // Check if the building can train the unit | ||||
// TODO: the AI API does not take promotion technologies into account for the list | // TODO: the AI API does not take promotion technologies into account for the list | ||||
// of trainable units (taken directly from the unit template). Here is a temporary fix. | // of trainable units (taken directly from the unit template). Here is a temporary fix. | ||||
if (queue && data.cmpPlayer.IsAI()) | if (data.cmpPlayer.IsAI()) | ||||
{ | templateName = cmpTrainer.GetUpgradedTemplate(cmd.template); | ||||
var list = queue.GetEntitiesList(); | |||||
if (list.indexOf(cmd.template) === -1 && cmd.promoted) | if (cmpTrainer.CanTrain(templateName)) | ||||
{ | Engine.QueryInterface(ent, IID_ProductionQueue)?.AddItem(templateName, "unit", +cmd.count, cmd.metadata, cmd.pushFront); | ||||
for (var promoted of cmd.promoted) | |||||
{ | |||||
if (list.indexOf(promoted) === -1) | |||||
continue; | |||||
cmd.template = promoted; | |||||
break; | |||||
} | |||||
} | |||||
} | |||||
if (queue && queue.GetEntitiesList().indexOf(cmd.template) != -1) | |||||
queue.AddItem(cmd.template, "unit", +cmd.count, cmd.metadata, cmd.pushFront); | |||||
} | } | ||||
}, | }, | ||||
"research": function(player, cmd, data) | "research": function(player, cmd, data) | ||||
{ | { | ||||
var cmpTechnologyManager = QueryOwnerInterface(cmd.entity, IID_TechnologyManager); | var cmpTechnologyManager = QueryOwnerInterface(cmd.entity, IID_TechnologyManager); | ||||
if (cmpTechnologyManager && !cmpTechnologyManager.CanResearch(cmd.template)) | if (cmpTechnologyManager && !cmpTechnologyManager.CanResearch(cmd.template)) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 1,481 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
'g_Commands' was used before it was defined.