Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/GuiInterface.js
Show All 27 Lines | GuiInterface.prototype.Init = function() | ||||
this.placementWallLastAngle = 0; | this.placementWallLastAngle = 0; | ||||
this.notifications = []; | this.notifications = []; | ||||
this.renamedEntities = []; | this.renamedEntities = []; | ||||
this.miragedEntities = []; | this.miragedEntities = []; | ||||
this.timeNotificationID = 1; | this.timeNotificationID = 1; | ||||
this.timeNotifications = []; | this.timeNotifications = []; | ||||
this.entsRallyPointsDisplayed = []; | this.entsRallyPointsDisplayed = []; | ||||
this.entsWithAuraAndStatusBars = new Set(); | this.entsWithAuraAndStatusBars = new Set(); | ||||
this.enabledVisualRangeOverlayTypes = {}; | |||||
}; | }; | ||||
/* | /* | ||||
* All of the functions defined below are called via Engine.GuiInterfaceCall(name, arg) | * All of the functions defined below are called via Engine.GuiInterfaceCall(name, arg) | ||||
* from GUI scripts, and executed here with arguments (player, arg). | * from GUI scripts, and executed here with arguments (player, arg). | ||||
* | * | ||||
* CAUTION: The input to the functions in this module is not network-synchronised, so it | * CAUTION: The input to the functions in this module is not network-synchronised, so it | ||||
* mustn't affect the simulation state (i.e. the data that is serialised and can affect | * mustn't affect the simulation state (i.e. the data that is serialised and can affect | ||||
▲ Show 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | GuiInterface.prototype.GetExtendedSimulationState = function() | ||||
// Add statistics to each player | // Add statistics to each player | ||||
let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); | let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); | ||||
let n = cmpPlayerManager.GetNumPlayers(); | let n = cmpPlayerManager.GetNumPlayers(); | ||||
for (let i = 0; i < n; ++i) | for (let i = 0; i < n; ++i) | ||||
{ | { | ||||
let playerEnt = cmpPlayerManager.GetPlayerByID(i); | let playerEnt = cmpPlayerManager.GetPlayerByID(i); | ||||
let cmpPlayerStatisticsTracker = Engine.QueryInterface(playerEnt, IID_StatisticsTracker); | let cmpPlayerStatisticsTracker = Engine.QueryInterface(playerEnt, IID_StatisticsTracker); | ||||
if (cmpPlayerStatisticsTracker) | if (cmpPlayerStatisticsTracker) | ||||
ret.players[i].statistics = cmpPlayerStatisticsTracker.GetStatistics(); | ret.players[i].sequences = cmpPlayerStatisticsTracker.GetSequences(); | ||||
} | } | ||||
return ret; | return ret; | ||||
}; | }; | ||||
GuiInterface.prototype.GetRenamedEntities = function(player) | GuiInterface.prototype.GetRenamedEntities = function(player) | ||||
{ | { | ||||
if (this.miragedEntities[player]) | if (this.miragedEntities[player]) | ||||
▲ Show 20 Lines • Show All 238 Lines • ▼ Show 20 Lines | GuiInterface.prototype.GetExtendedEntityState = function(player, ent) | ||||
let cmpIdentity = Engine.QueryInterface(ent, IID_Identity); | let cmpIdentity = Engine.QueryInterface(ent, IID_Identity); | ||||
let cmpAttack = Engine.QueryInterface(ent, IID_Attack); | let cmpAttack = Engine.QueryInterface(ent, IID_Attack); | ||||
if (cmpAttack) | if (cmpAttack) | ||||
{ | { | ||||
let types = cmpAttack.GetAttackTypes(); | let types = cmpAttack.GetAttackTypes(); | ||||
if (types.length) | if (types.length) | ||||
ret.attack = {}; | ret.attack = {}; | ||||
for (let type of types) | for (let type of types) | ||||
{ | { | ||||
ret.attack[type] = cmpAttack.GetAttackStrengths(type); | ret.attack[type] = cmpAttack.GetAttackStrengths(type); | ||||
ret.attack[type].splash = cmpAttack.GetSplashDamage(type); | ret.attack[type].splash = cmpAttack.GetSplashDamage(type); | ||||
let range = cmpAttack.GetRange(type); | let range = cmpAttack.GetRange(type); | ||||
ret.attack[type].minRange = range.min; | ret.attack[type].minRange = range.min; | ||||
ret.attack[type].maxRange = range.max; | ret.attack[type].maxRange = range.max; | ||||
▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | GuiInterface.prototype.GetTemplateData = function(player, name) | ||||
if (!template.Auras) | if (!template.Auras) | ||||
return GetTemplateDataHelper(template, player, aurasTemplate, Resources); | return GetTemplateDataHelper(template, player, aurasTemplate, Resources); | ||||
// Add aura name and description loaded from JSON file | // Add aura name and description loaded from JSON file | ||||
let auraNames = template.Auras._string.split(/\s+/); | let auraNames = template.Auras._string.split(/\s+/); | ||||
let cmpDataTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager); | let cmpDataTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager); | ||||
for (let name of auraNames) | for (let name of auraNames) | ||||
{ | aurasTemplate[name] = cmpDataTemplateManager.GetAuraTemplate(name); | ||||
let auraTemplate = cmpDataTemplateManager.GetAuraTemplate(name); | |||||
if (!auraTemplate) | |||||
{ | |||||
// The following warning is perhaps useless since it's yet done in DataTemplateManager | |||||
warn("Tried to get data for invalid aura: " + name); | |||||
continue; | |||||
} | |||||
aurasTemplate[name] = {}; | |||||
aurasTemplate[name].auraName = auraTemplate.auraName || null; | |||||
aurasTemplate[name].auraDescription = auraTemplate.auraDescription || null; | |||||
} | |||||
return GetTemplateDataHelper(template, player, aurasTemplate, Resources); | return GetTemplateDataHelper(template, player, aurasTemplate, Resources); | ||||
}; | }; | ||||
GuiInterface.prototype.GetTechnologyData = function(player, data) | GuiInterface.prototype.GetTechnologyData = function(player, data) | ||||
{ | { | ||||
let cmpDataTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager); | let cmpDataTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager); | ||||
let template = cmpDataTemplateManager.GetTechnologyTemplate(data.name); | let template = cmpDataTemplateManager.GetTechnologyTemplate(data.name); | ||||
▲ Show 20 Lines • Show All 223 Lines • ▼ Show 20 Lines | if (!color) | ||||
color = { "r":1, "g":1, "b":1 }; | color = { "r":1, "g":1, "b":1 }; | ||||
let cmpPlayer = QueryPlayerIDInterface(owner); | let cmpPlayer = QueryPlayerIDInterface(owner); | ||||
if (cmpPlayer) | if (cmpPlayer) | ||||
color = cmpPlayer.GetColor(); | color = cmpPlayer.GetColor(); | ||||
playerColors[owner] = color; | playerColors[owner] = color; | ||||
} | } | ||||
cmpSelectable.SetSelectionHighlight({ "r": color.r, "g": color.g, "b": color.b, "a": cmd.alpha }, cmd.selected); | cmpSelectable.SetSelectionHighlight({ "r": color.r, "g": color.g, "b": color.b, "a": cmd.alpha }, cmd.selected); | ||||
let cmpRangeVisualization = Engine.QueryInterface(ent, IID_RangeVisualization); | |||||
if (!cmpRangeVisualization || player != owner && player != -1) | |||||
continue; | |||||
cmpRangeVisualization.SetEnabled(cmd.selected, this.enabledVisualRangeOverlayTypes); | |||||
} | } | ||||
}; | }; | ||||
GuiInterface.prototype.EnableVisualRangeOverlayType = function(player, data) | |||||
{ | |||||
this.enabledVisualRangeOverlayTypes[data.type] = data.enabled; | |||||
}; | |||||
GuiInterface.prototype.GetEntitiesWithStatusBars = function() | GuiInterface.prototype.GetEntitiesWithStatusBars = function() | ||||
{ | { | ||||
return [...this.entsWithAuraAndStatusBars]; | return [...this.entsWithAuraAndStatusBars]; | ||||
}; | }; | ||||
GuiInterface.prototype.SetStatusBars = function(player, cmd) | GuiInterface.prototype.SetStatusBars = function(player, cmd) | ||||
{ | { | ||||
let affectedEnts = new Set(); | let affectedEnts = new Set(); | ||||
Show All 24 Lines | GuiInterface.prototype.SetStatusBars = function(player, cmd) | ||||
for (let ent of affectedEnts) | for (let ent of affectedEnts) | ||||
{ | { | ||||
let cmpStatusBars = Engine.QueryInterface(ent, IID_StatusBars); | let cmpStatusBars = Engine.QueryInterface(ent, IID_StatusBars); | ||||
if (cmpStatusBars) | if (cmpStatusBars) | ||||
cmpStatusBars.RegenerateSprites(); | cmpStatusBars.RegenerateSprites(); | ||||
} | } | ||||
}; | }; | ||||
GuiInterface.prototype.SetRangeOverlays = function(player, cmd) | |||||
{ | |||||
for (let ent of cmd.entities) | |||||
{ | |||||
let cmpRangeVisualization = Engine.QueryInterface(ent, IID_RangeVisualization); | |||||
if (cmpRangeVisualization) | |||||
cmpRangeVisualization.SetEnabled(cmd.enabled, this.enabledVisualRangeOverlayTypes); | |||||
} | |||||
}; | |||||
GuiInterface.prototype.GetPlayerEntities = function(player) | GuiInterface.prototype.GetPlayerEntities = function(player) | ||||
{ | { | ||||
return Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).GetEntitiesByPlayer(player); | return Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).GetEntitiesByPlayer(player); | ||||
}; | }; | ||||
GuiInterface.prototype.GetNonGaiaEntities = function() | GuiInterface.prototype.GetNonGaiaEntities = function() | ||||
{ | { | ||||
return Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).GetNonGaiaEntities(); | return Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).GetNonGaiaEntities(); | ||||
▲ Show 20 Lines • Show All 878 Lines • ▼ Show 20 Lines | GuiInterface.prototype.GetTradingDetails = function(player, data) | ||||
else | else | ||||
{ | { | ||||
// Else both markets are not null and target is different from them | // Else both markets are not null and target is different from them | ||||
result = { "type": "set first" }; | result = { "type": "set first" }; | ||||
} | } | ||||
return result; | return result; | ||||
}; | }; | ||||
GuiInterface.prototype.CanCapture = function(player, data) | |||||
{ | |||||
let cmpAttack = Engine.QueryInterface(data.entity, IID_Attack); | |||||
if (!cmpAttack) | |||||
return false; | |||||
let owner = QueryOwnerInterface(data.entity).GetPlayerID(); | |||||
let cmpCapturable = QueryMiragedInterface(data.target, IID_Capturable); | |||||
if (cmpCapturable && cmpCapturable.CanCapture(owner) && cmpAttack.GetAttackTypes().indexOf("Capture") != -1) | |||||
return cmpAttack.CanAttack(data.target); | |||||
return false; | |||||
}; | |||||
GuiInterface.prototype.CanAttack = function(player, data) | GuiInterface.prototype.CanAttack = function(player, data) | ||||
{ | { | ||||
let cmpAttack = Engine.QueryInterface(data.entity, IID_Attack); | let cmpAttack = Engine.QueryInterface(data.entity, IID_Attack); | ||||
if (!cmpAttack) | return cmpAttack && cmpAttack.CanAttack(data.target, data.types || undefined); | ||||
return false; | |||||
let cmpEntityPlayer = QueryOwnerInterface(data.entity, IID_Player); | |||||
let cmpTargetPlayer = QueryOwnerInterface(data.target, IID_Player); | |||||
if (!cmpEntityPlayer || !cmpTargetPlayer) | |||||
return false; | |||||
// if the owner is an enemy, it's up to the attack component to decide | |||||
if (cmpEntityPlayer.IsEnemy(cmpTargetPlayer.GetPlayerID())) | |||||
return cmpAttack.CanAttack(data.target); | |||||
return false; | |||||
}; | }; | ||||
/* | /* | ||||
* Returns batch build time. | * Returns batch build time. | ||||
*/ | */ | ||||
GuiInterface.prototype.GetBatchTime = function(player, data) | GuiInterface.prototype.GetBatchTime = function(player, data) | ||||
{ | { | ||||
let cmpProductionQueue = Engine.QueryInterface(data.entity, IID_ProductionQueue); | let cmpProductionQueue = Engine.QueryInterface(data.entity, IID_ProductionQueue); | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | let exposedFunctions = { | ||||
"SetBuildingPlacementPreview": 1, | "SetBuildingPlacementPreview": 1, | ||||
"SetWallPlacementPreview": 1, | "SetWallPlacementPreview": 1, | ||||
"GetFoundationSnapData": 1, | "GetFoundationSnapData": 1, | ||||
"PlaySound": 1, | "PlaySound": 1, | ||||
"FindIdleUnits": 1, | "FindIdleUnits": 1, | ||||
"HasIdleUnits": 1, | "HasIdleUnits": 1, | ||||
"GetTradingRouteGain": 1, | "GetTradingRouteGain": 1, | ||||
"GetTradingDetails": 1, | "GetTradingDetails": 1, | ||||
"CanCapture": 1, | |||||
"CanAttack": 1, | "CanAttack": 1, | ||||
"GetBatchTime": 1, | "GetBatchTime": 1, | ||||
"IsMapRevealed": 1, | "IsMapRevealed": 1, | ||||
"SetPathfinderDebugOverlay": 1, | "SetPathfinderDebugOverlay": 1, | ||||
"SetPathfinderHierDebugOverlay": 1, | "SetPathfinderHierDebugOverlay": 1, | ||||
"SetObstructionDebugOverlay": 1, | "SetObstructionDebugOverlay": 1, | ||||
"SetMotionDebugOverlay": 1, | "SetMotionDebugOverlay": 1, | ||||
"SetRangeDebugOverlay": 1, | "SetRangeDebugOverlay": 1, | ||||
"EnableVisualRangeOverlayType": 1, | |||||
"SetRangeOverlays": 1, | |||||
"GetTraderNumber": 1, | "GetTraderNumber": 1, | ||||
"GetTradingGoods": 1, | "GetTradingGoods": 1, | ||||
}; | }; | ||||
GuiInterface.prototype.ScriptCall = function(player, name, args) | GuiInterface.prototype.ScriptCall = function(player, name, args) | ||||
{ | { | ||||
if (exposedFunctions[name]) | if (exposedFunctions[name]) | ||||
return this[name](player, args); | return this[name](player, args); | ||||
else | else | ||||
throw new Error("Invalid GuiInterface Call name \""+name+"\""); | throw new Error("Invalid GuiInterface Call name \""+name+"\""); | ||||
}; | }; | ||||
Engine.RegisterSystemComponentType(IID_GuiInterface, "GuiInterface", GuiInterface); | Engine.RegisterSystemComponentType(IID_GuiInterface, "GuiInterface", GuiInterface); |
Wildfire Games · Phabricator