Index: ps/trunk/binaries/data/mods/public/gui/summary/counters.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/summary/counters.js (revision 20270) +++ ps/trunk/binaries/data/mods/public/gui/summary/counters.js (revision 20271) @@ -1,335 +1,346 @@ var g_TeamHelperData = []; function calculatePercent(divident, divisor) { return { "percent": divisor ? Math.floor(100 * divident / divisor) : 0 }; } function calculateRatio(divident, divisor) { return divident ? +((divident / divisor).toFixed(2)) : 0; } function formatSummaryValue(values) { if (typeof values != "object") return values === Infinity ? g_InfinitySymbol : values; let ret = ""; for (let type in values) ret += (g_SummaryTypes[type].color ? '[color="' + g_SummaryTypes[type].color + '"]' + values[type] + '[/color]' : values[type]) + g_SummaryTypes[type].postfix; return ret; } function getPlayerValuesPerTeam(team, index, type, counters, headings) { let fn = counters[headings.map(heading => heading.identifier).indexOf(type) - 1].fn; return g_Teams[team].map(player => fn(g_GameData.sim.playerStates[player], index, type)); } function updateCountersPlayer(playerState, counters, headings, idGUI, index) { for (let n in counters) { let fn = counters[n].fn; Engine.GetGUIObjectByName(idGUI + "[" + n + "]").caption = formatSummaryValue(fn && fn(playerState, index, headings[+n + 1].identifier)); } } function updateCountersTeam(teamFn, counters, headings, index) { for (let team in g_Teams) { if (team == -1) continue; for (let n in counters) Engine.GetGUIObjectByName("valueDataTeam[" + team + "][" + n + "]").caption = formatSummaryValue(teamFn(team, index, headings[+n + 1].identifier, counters, headings)); } } /** * Add two objects property-wise and writes the result to obj1. * So summaryAddObject([1, 2], [7, 42]) will result in [8, 44] and * summaryAddObject({ "f": 3, "o", 5 }, { "f": -1, "o", 42 }) will result in { "f": 2, "o", 47 }. * * @param {Object} obj1 - First summand object. This will be set to the sum of both. * @param {Object} obj2 - Second summand object. */ function summaryAddObject(obj1, obj2) { for (let p in obj1) obj1[p] += obj2[p]; } /** * The sum of all elements of an array. So summaryArraySum([1, 2]) will be 3 and * summaryArraySum([{ "f": 3, "o", 5 }, { "f": -1, "o", 42 }]) will be { "f": 2, "o", 47 }. * * @param {Array} array - The array to sum up. * @returns the sum of all elements. */ function summaryArraySum(array) { return array.reduce((sum, val) => { if (typeof sum != "object") return sum + val; summaryAddObject(sum, val); return sum; }); } function calculateTeamCounterDataHelper() { for (let i = 0; i < g_PlayerCount; ++i) { let playerState = g_GameData.sim.playerStates[i + 1]; if (!g_TeamHelperData[playerState.team]) { g_TeamHelperData[playerState.team] = {}; for (let value of ["food", "vegetarianFood", "femaleCitizen", "worker", "enemyUnitsKilled", "unitsLost", "mapControl", "mapControlPeak", "mapExploration", "totalBought", "totalSold"]) g_TeamHelperData[playerState.team][value] = new Array(playerState.sequences.time.length).fill(0); } summaryAddObject(g_TeamHelperData[playerState.team].food, playerState.sequences.resourcesGathered.food); summaryAddObject(g_TeamHelperData[playerState.team].vegetarianFood, playerState.sequences.resourcesGathered.vegetarianFood); summaryAddObject(g_TeamHelperData[playerState.team].femaleCitizen, playerState.sequences.unitsTrained.FemaleCitizen); summaryAddObject(g_TeamHelperData[playerState.team].worker, playerState.sequences.unitsTrained.Worker); summaryAddObject(g_TeamHelperData[playerState.team].enemyUnitsKilled, playerState.sequences.enemyUnitsKilled.total); summaryAddObject(g_TeamHelperData[playerState.team].unitsLost, playerState.sequences.unitsLost.total); g_TeamHelperData[playerState.team].mapControl = playerState.sequences.teamPercentMapControlled; g_TeamHelperData[playerState.team].mapControlPeak = playerState.sequences.teamPeakPercentMapControlled; g_TeamHelperData[playerState.team].mapExploration = playerState.sequences.teamPercentMapExplored; for (let type in playerState.sequences.resourcesBought) summaryAddObject(g_TeamHelperData[playerState.team].totalBought, playerState.sequences.resourcesBought[type]); for (let type in playerState.sequences.resourcesSold) summaryAddObject(g_TeamHelperData[playerState.team].totalSold, playerState.sequences.resourcesSold[type]); } } function calculateEconomyScore(playerState, index) { let total = 0; for (let type of g_ResourceData.GetCodes()) total += playerState.sequences.resourcesGathered[type][index]; // Subtract costs for sheep/goats/pigs to get the net food gain for corralling total -= playerState.sequences.domesticUnitsTrainedValue[index]; total += playerState.sequences.tradeIncome[index]; return Math.round(total / 10); } function calculateMilitaryScore(playerState, index) { return Math.round((playerState.sequences.enemyUnitsKilledValue[index] + playerState.sequences.unitsCapturedValue[index] + playerState.sequences.enemyBuildingsDestroyedValue[index] + playerState.sequences.buildingsCapturedValue[index]) / 10); } function calculateExplorationScore(playerState, index) { return playerState.sequences.percentMapExplored[index] * 10; } function calculateScoreTotal(playerState, index) { return calculateEconomyScore(playerState, index) + calculateMilitaryScore(playerState, index) + calculateExplorationScore(playerState, index); } function calculateScoreTeam(team, index, type, counters, headings) { if (type == "explorationScore") return g_TeamHelperData[team].mapExploration[index] * 10; if (type == "totalScore") return ["economyScore", "militaryScore", "explorationScore"].map( t => calculateScoreTeam(team, index, t, counters, headings)).reduce( (sum, value) => sum + value); return summaryArraySum(getPlayerValuesPerTeam(team, index, type, counters, headings)); } function calculateBuildings(playerState, index, type) { return { "constructed": playerState.sequences.buildingsConstructed[type][index], "destroyed": playerState.sequences.enemyBuildingsDestroyed[type][index], "captured": playerState.sequences.buildingsCaptured[type][index], "lost": playerState.sequences.buildingsLost[type][index] }; } function calculateBuildingsTeam(team, index, type, counters, headings) { return summaryArraySum(getPlayerValuesPerTeam(team, index, type, counters, headings)); } function calculateUnitsTeam(team, index, type, counters, headings) { return summaryArraySum(getPlayerValuesPerTeam(team, index, type, counters, headings)); } function calculateUnitsWithCaptured(playerState, index, type) { return { "trained": playerState.sequences.unitsTrained[type][index], "killed": playerState.sequences.enemyUnitsKilled[type][index], "captured": playerState.sequences.unitsCaptured[type][index], "lost": playerState.sequences.unitsLost[type][index] }; } function calculateUnits(playerState, index, type) { return { "trained": playerState.sequences.unitsTrained[type][index], "killed": playerState.sequences.enemyUnitsKilled[type][index], "lost": playerState.sequences.unitsLost[type][index] }; } function calculateResources(playerState, index, type) { return { "gathered": playerState.sequences.resourcesGathered[type][index], "used": playerState.sequences.resourcesUsed[type][index] - playerState.sequences.resourcesSold[type][index] }; } function calculateTotalResources(playerState, index) { let totalGathered = 0; let totalUsed = 0; for (let type of g_ResourceData.GetCodes()) { totalGathered += playerState.sequences.resourcesGathered[type][index]; totalUsed += playerState.sequences.resourcesUsed[type][index] - playerState.sequences.resourcesSold[type][index]; } return { "gathered": totalGathered, "used": totalUsed }; } function calculateTreasureCollected(playerState, index) { return playerState.sequences.treasuresCollected[index]; } function calculateLootCollected(playerState, index) { return playerState.sequences.lootCollected[index]; } function calculateTributeSent(playerState, index) { return { "sent": playerState.sequences.tributesSent[index], "received": playerState.sequences.tributesReceived[index] }; } function calculateResourcesTeam(team, index, type, counters, headings) { return summaryArraySum(getPlayerValuesPerTeam(team, index, type, counters, headings)); } function calculateResourceExchanged(playerState, index, type) { return { "bought": playerState.sequences.resourcesBought[type][index], "sold": playerState.sequences.resourcesSold[type][index] }; } function calculateBarterEfficiency(playerState, index) { let totalBought = 0; let totalSold = 0; for (let type in playerState.sequences.resourcesBought) totalBought += playerState.sequences.resourcesBought[type][index]; for (let type in playerState.sequences.resourcesSold) totalSold += playerState.sequences.resourcesSold[type][index]; return calculatePercent(totalBought, totalSold); } function calculateTradeIncome(playerState, index) { return playerState.sequences.tradeIncome[index]; } function calculateMarketTeam(team, index, type, counters, headings) { if (type == "barterEfficency") return calculatePercent(g_TeamHelperData[team].totalBought[index], g_TeamHelperData[team].totalSold[index]); return summaryArraySum(getPlayerValuesPerTeam(team, index, type, counters, headings)); } function calculateVegetarianRatio(playerState, index) { return calculatePercent( playerState.sequences.resourcesGathered.vegetarianFood[index], playerState.sequences.resourcesGathered.food[index]); } function calculateFeminization(playerState, index) { return calculatePercent( playerState.sequences.unitsTrained.FemaleCitizen[index], playerState.sequences.unitsTrained.Worker[index]); } function calculateKillDeathRatio(playerState, index) { return calculateRatio( playerState.sequences.enemyUnitsKilled.total[index], playerState.sequences.unitsLost.total[index]); } function calculateMapExploration(playerState, index) { return { "percent": playerState.sequences.percentMapExplored[index] }; } function calculateMapFinalControl(playerState, index) { return { "percent": playerState.sequences.percentMapControlled[index] }; } function calculateMapPeakControl(playerState, index) { return { "percent": playerState.sequences.peakPercentMapControlled[index] }; } -function calculateMiscellaneousTeam(team, index, type) +function calculateMiscellaneousTeam(team, index, type, counters, headings) { if (type == "vegetarianRatio") return calculatePercent(g_TeamHelperData[team].vegetarianFood[index], g_TeamHelperData[team].food[index]); if (type == "feminization") return calculatePercent(g_TeamHelperData[team].femaleCitizen[index], g_TeamHelperData[team].worker[index]); if (type == "killDeath") return calculateRatio(g_TeamHelperData[team].enemyUnitsKilled[index], g_TeamHelperData[team].unitsLost[index]); + if (type == "bribes") + return summaryArraySum(getPlayerValuesPerTeam(team, index, type, counters, headings)); + return { "percent": g_TeamHelperData[team][type][index] }; } + +function calculateBribes(playerState, index, type) +{ + return { + "succeeded": playerState.sequences.successfulBribes[index], + "failed": playerState.sequences.failedBribes[index] + }; +} Index: ps/trunk/binaries/data/mods/public/gui/summary/layout.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/summary/layout.js (revision 20270) +++ ps/trunk/binaries/data/mods/public/gui/summary/layout.js (revision 20271) @@ -1,367 +1,378 @@ var g_ScorePanelsData = { "score": { "caption": translate("Score"), "headings": [ { "identifier": "playername", "caption": translate("Player name"), "yStart": 26, "width": 200 }, { "identifier": "economyScore", "caption": translate("Economy score"), "yStart": 16, "width": 100 }, { "identifier": "militaryScore", "caption": translate("Military score"), "yStart": 16, "width": 100 }, { "identifier": "explorationScore", "caption": translate("Exploration score"), "yStart": 16, "width": 100 }, { "identifier": "totalScore", "caption": translate("Total score"), "yStart": 16, "width": 100 } ], "titleHeadings": [], "counters": [ { "width": 100, "fn": calculateEconomyScore, "verticalOffset": 12 }, { "width": 100, "fn": calculateMilitaryScore, "verticalOffset": 12 }, { "width": 100, "fn": calculateExplorationScore, "verticalOffset": 12 }, { "width": 100, "fn": calculateScoreTotal, "verticalOffset": 12 } ], "teamCounterFn": calculateScoreTeam }, "buildings": { "caption": translate("Buildings"), "headings": [ { "identifier": "playername", "caption": translate("Player name"), "yStart": 26, "width": 200 }, { "identifier": "total", "caption": translate("Total"), "yStart": 34, "width": 105 }, { "identifier": "House", "caption": translate("Houses"), "yStart": 34, "width": 85 }, { "identifier": "Economic", "caption": translate("Economic"), "yStart": 34, "width": 85 }, { "identifier": "Outpost", "caption": translate("Outposts"), "yStart": 34, "width": 85 }, { "identifier": "Military", "caption": translate("Military"), "yStart": 34, "width": 85 }, { "identifier": "Fortress", "caption": translate("Fortresses"), "yStart": 34, "width": 85 }, { "identifier": "CivCentre", "caption": translate("Civ centers"), "yStart": 34, "width": 85 }, { "identifier": "Wonder", "caption": translate("Wonders"), "yStart": 34, "width": 85 } ], "titleHeadings": [ { "caption": sprintf(translate("Buildings Statistics (%(constructed)s / %(destroyed)s / %(captured)s / %(lost)s)"), { "constructed": getColoredTypeTranslation("constructed"), "destroyed": getColoredTypeTranslation("destroyed"), "captured": getColoredTypeTranslation("captured"), "lost": getColoredTypeTranslation("lost") }), "yStart": 16, "width": 85 * 7 + 105 }, // width = 700 ], "counters": [ { "width": 105, "fn": calculateBuildings, "verticalOffset": 3 }, { "width": 85, "fn": calculateBuildings, "verticalOffset": 3 }, { "width": 85, "fn": calculateBuildings, "verticalOffset": 3 }, { "width": 85, "fn": calculateBuildings, "verticalOffset": 3 }, { "width": 85, "fn": calculateBuildings, "verticalOffset": 3 }, { "width": 85, "fn": calculateBuildings, "verticalOffset": 3 }, { "width": 85, "fn": calculateBuildings, "verticalOffset": 3 }, { "width": 85, "fn": calculateBuildings, "verticalOffset": 3 } ], "teamCounterFn": calculateBuildingsTeam }, "units": { "caption": translate("Units"), "headings": [ { "identifier": "playername", "caption": translate("Player name"), "yStart": 26, "width": 200 }, { "identifier": "total", "caption": translate("Total"), "yStart": 34, "width": 105 }, { "identifier": "Infantry", "caption": translate("Infantry"), "yStart": 34, "width": 85 }, { "identifier": "Worker", "caption": translate("Worker"), "yStart": 34, "width": 85 }, { "identifier": "Cavalry", "caption": translate("Cavalry"), "yStart": 34, "width": 85 }, { "identifier": "Champion", "caption": translate("Champion"), "yStart": 34, "width": 85 }, { "identifier": "Hero", "caption": translate("Heroes"), "yStart": 34, "width": 85 }, { "identifier": "Siege", "caption": translate("Siege"), "yStart": 34, "width": 85 }, { "identifier": "Ship", "caption": translate("Navy"), "yStart": 34, "width": 85 }, { "identifier": "Trader", "caption": translate("Traders"), "yStart": 34, "width": 85 } ], "titleHeadings": [ { "caption": sprintf(translate("Units Statistics (%(trained)s / %(killed)s / %(captured)s / %(lost)s)"), { "trained": getColoredTypeTranslation("trained"), "killed": getColoredTypeTranslation("killed"), "captured": getColoredTypeTranslation("captured"), "lost": getColoredTypeTranslation("lost") }), "yStart": 16, "width": 85 * 8 + 105 }, // width = 785 ], "counters": [ { "width": 105, "fn": calculateUnitsWithCaptured, "verticalOffset": 3 }, { "width": 85, "fn": calculateUnits, "verticalOffset": 3 }, { "width": 85, "fn": calculateUnits, "verticalOffset": 3 }, { "width": 85, "fn": calculateUnits, "verticalOffset": 3 }, { "width": 85, "fn": calculateUnits, "verticalOffset": 3 }, { "width": 85, "fn": calculateUnits, "verticalOffset": 3 }, { "width": 85, "fn": calculateUnitsWithCaptured, "verticalOffset": 3 }, { "width": 85, "fn": calculateUnits, "verticalOffset": 3 }, { "width": 85, "fn": calculateUnits, "verticalOffset": 3 } ], "teamCounterFn": calculateUnitsTeam }, "resources": { "caption": translate("Resources"), "headings": [ { "identifier": "playername", "caption": translate("Player name"), "yStart": 26, "width": 200 }, { "identifier": "total", "caption": translate("Total"), "yStart": 34, "width": 110 }, ...g_ResourceData.GetResources().map(res => ({ "identifier": res.code, "caption": resourceNameFirstWord(res.code), "yStart": 34, "width": 100 })), { "identifier": "tributes", "caption": sprintf(translate("Tributes \n(%(sent)s / %(received)s)"), { "sent": getColoredTypeTranslation("sent"), "received": getColoredTypeTranslation("received") }), "yStart": 16, "width": 121 }, { "identifier": "treasuresCollected", "caption": translate("Treasures collected"), "yStart": 16, "width": 100 }, { "identifier": "loot", "caption": translate("Loot"), "yStart": 16, "width": 100 } ], "titleHeadings": [ { "caption": sprintf(translate("Resource Statistics (%(gathered)s / %(used)s)"), { "gathered": getColoredTypeTranslation("gathered"), "used": getColoredTypeTranslation("used") }), "yStart": 16, "width": 100 * g_ResourceData.GetCodes().length + 110 }, ], "counters": [ { "width": 110, "fn": calculateTotalResources, "verticalOffset": 12 }, ...g_ResourceData.GetCodes().map(code => ({ "fn": calculateResources, "verticalOffset": 12, "width": 100 })), { "width": 121, "fn": calculateTributeSent, "verticalOffset": 12 }, { "width": 100, "fn": calculateTreasureCollected, "verticalOffset": 12 }, { "width": 100, "fn": calculateLootCollected, "verticalOffset": 12 } ], "teamCounterFn": calculateResourcesTeam }, "market": { "caption": translate("Market"), "headings": [ { "identifier": "playername", "caption": translate("Player name"), "yStart": 26, "width": 200 }, ...g_ResourceData.GetResources().map(res => { return { "identifier": res.code, "caption": // Translation: use %(resourceWithinSentence)s if needed sprintf(translate("%(resourceFirstWord)s exchanged"), { "resourceFirstWord": resourceNameFirstWord(res.code), "resourceWithinSentence": resourceNameWithinSentence(res.code) }), "yStart": 16, "width": 100 }; }), { "identifier": "barterEfficency", "caption": translate("Barter efficiency"), "yStart": 16, "width": 100 }, { "identifier": "tradeIncome", "caption": translate("Trade income"), "yStart": 16, "width": 100 } ], "titleHeadings": [], "counters": [ ...g_ResourceData.GetCodes().map(code => ({ "width": 100, "fn": calculateResourceExchanged, "verticalOffset": 12 })), { "width": 100, "fn": calculateBarterEfficiency, "verticalOffset": 12 }, { "width": 100, "fn": calculateTradeIncome, "verticalOffset": 12 } ], "teamCounterFn": calculateMarketTeam }, "misc": { "caption": translate("Miscellaneous"), "headings": [ { "identifier": "playername", "caption": translate("Player name"), "yStart": 26, "width": 200 }, { "identifier": "vegetarianRatio", "caption": translate("Vegetarian ratio"), "yStart": 16, "width": 100 }, { "identifier": "feminization", "caption": translate("Feminization"), "yStart": 16, "width": 100 }, { "identifier": "killDeath", "caption": translate("Kill / Death ratio"), "yStart": 16, "width": 100 }, + { + "identifier": "bribes", + "caption": sprintf(translate("Bribes\n(%(succeeded)s / %(failed)s)"), + { + "succeeded": getColoredTypeTranslation("succeeded"), + "failed": getColoredTypeTranslation("failed") + }), + "yStart": 16, + "width": 139 + }, { "identifier": "mapExploration", "caption": translate("Map exploration"), "yStart": 16, "width": 100 }, { "identifier": "mapControlPeak", "caption": translate("Map control (peak)"), "yStart": 16, "width": 100 }, { "identifier": "mapControl", "caption": translate("Map control (finish)"), "yStart": 16, "width": 100 } ], "titleHeadings": [], "counters": [ { "width": 100, "fn": calculateVegetarianRatio, "verticalOffset": 12 }, { "width": 100, "fn": calculateFeminization, "verticalOffset": 12 }, { "width": 100, "fn": calculateKillDeathRatio, "verticalOffset": 12 }, + { "width": 139, "fn": calculateBribes, "verticalOffset": 12 }, { "width": 100, "fn": calculateMapExploration, "verticalOffset": 12 }, { "width": 100, "fn": calculateMapPeakControl, "verticalOffset": 12 }, { "width": 100, "fn": calculateMapFinalControl, "verticalOffset": 12 } ], "teamCounterFn": calculateMiscellaneousTeam } }; function getColoredTypeTranslation(type) { return g_SummaryTypes[type].color ? '[color="' + g_SummaryTypes[type].color + '"]' + g_SummaryTypes[type].caption + '[/color]' : g_SummaryTypes[type].caption; } function resetGeneralPanel() { for (let h = 0; h < g_MaxHeadingTitle; ++h) { Engine.GetGUIObjectByName("titleHeading["+ h +"]").hidden = true; Engine.GetGUIObjectByName("Heading[" + h + "]").hidden = true; for (let p = 0; p < g_MaxPlayers; ++p) { Engine.GetGUIObjectByName("valueData[" + p + "][" + h + "]").hidden = true; for (let t = 0; t < g_MaxTeams; ++t) { Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + p + "][" + h + "]").hidden = true; Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + h + "]").hidden = true; } } } } function updateGeneralPanelHeadings(headings) { let left = 50; for (let h in headings) { let headerGUIName = "playerNameHeading"; if (h > 0) headerGUIName = "Heading[" + (h - 1) + "]"; let headerGUI = Engine.GetGUIObjectByName(headerGUIName); headerGUI.caption = headings[h].caption; headerGUI.size = left + " " + headings[h].yStart + " " + (left + headings[h].width) + " 100%"; headerGUI.hidden = false; if (headings[h].width < g_LongHeadingWidth) left += headings[h].width; } } function updateGeneralPanelTitles(titleHeadings) { let left = 250; for (let th in titleHeadings) { if (th >= g_MaxHeadingTitle) break; if (titleHeadings[th].xOffset) left += titleHeadings[th].xOffset; let headerGUI = Engine.GetGUIObjectByName("titleHeading["+ th +"]"); headerGUI.caption = titleHeadings[th].caption; headerGUI.size = left + " " + titleHeadings[th].yStart + " " + (left + titleHeadings[th].width) + " 100%"; headerGUI.hidden = false; if (titleHeadings[th].width < g_LongHeadingWidth) left += titleHeadings[th].width; } } function updateGeneralPanelCounter(counters) { let rowPlayerObjectWidth = 0; let left = 0; for (let p = 0; p < g_MaxPlayers; ++p) { left = 240; let counterObject; for (let w in counters) { counterObject = Engine.GetGUIObjectByName("valueData[" + p + "][" + w + "]"); counterObject.size = left + " " + counters[w].verticalOffset + " " + (left + counters[w].width) + " 100%"; counterObject.hidden = false; left += counters[w].width; } if (rowPlayerObjectWidth == 0) rowPlayerObjectWidth = left; let counterTotalObject; for (let t = 0; t < g_MaxTeams; ++t) { left = 240; for (let w in counters) { counterObject = Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + p + "][" + w + "]"); counterObject.size = left + " " + counters[w].verticalOffset + " " + (left + counters[w].width) + " 100%"; counterObject.hidden = false; if (g_Teams[t]) { let yStart = 25 + g_Teams[t].length * (g_PlayerBoxYSize + g_PlayerBoxGap) + 3 + counters[w].verticalOffset; counterTotalObject = Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + w + "]"); counterTotalObject.size = (left + 20) + " " + yStart + " " + (left + counters[w].width) + " 100%"; counterTotalObject.hidden = false; } left += counters[w].width; } } } return rowPlayerObjectWidth; } function updateGeneralPanelTeams() { let withoutTeam = !g_Teams[-1] ? 0 : g_Teams[-1].length; if (!g_Teams || withoutTeam > 0) Engine.GetGUIObjectByName("noTeamsBox").hidden = false; if (!g_Teams) return; let yStart = g_TeamsBoxYStart + withoutTeam * (g_PlayerBoxYSize + g_PlayerBoxGap) + (withoutTeam ? 30 : 0); for (let i in g_Teams) { if (i == -1) continue; let teamBox = Engine.GetGUIObjectByName("teamBoxt["+i+"]"); teamBox.hidden = false; let teamBoxSize = teamBox.size; teamBoxSize.top = yStart; teamBox.size = teamBoxSize; yStart += 30 + g_Teams[i].length * (g_PlayerBoxYSize + g_PlayerBoxGap) + 32; Engine.GetGUIObjectByName("teamNameHeadingt["+i+"]").caption = "Team " + (+i + 1); let teamHeading = Engine.GetGUIObjectByName("teamHeadingt["+i+"]"); let yStartTotal = 30 + g_Teams[i].length * (g_PlayerBoxYSize + g_PlayerBoxGap) + 10; teamHeading.size = "50 " + yStartTotal + " 100% " + (yStartTotal + 20); teamHeading.caption = translate("Team total"); } // If there are no players without team, hide "player name" heading if (!withoutTeam) Engine.GetGUIObjectByName("playerNameHeading").caption = ""; } function initPlayerBoxPositions() { for (let h = 0; h < g_MaxPlayers; ++h) { let playerBox = Engine.GetGUIObjectByName("playerBox[" + h + "]"); let boxSize = playerBox.size; boxSize.top += h * (g_PlayerBoxYSize + g_PlayerBoxGap); boxSize.bottom = boxSize.top + g_PlayerBoxYSize; playerBox.size = boxSize; for (let i = 0; i < g_MaxTeams; ++i) { let playerBoxt = Engine.GetGUIObjectByName("playerBoxt[" + i + "][" + h + "]"); boxSize = playerBoxt.size; boxSize.top += h * (g_PlayerBoxYSize + g_PlayerBoxGap); boxSize.bottom = boxSize.top + g_PlayerBoxYSize; playerBoxt.size = boxSize; } } } Index: ps/trunk/binaries/data/mods/public/gui/summary/summary.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/summary/summary.js (revision 20270) +++ ps/trunk/binaries/data/mods/public/gui/summary/summary.js (revision 20271) @@ -1,479 +1,489 @@ const g_CivData = loadCivData(false, false); var g_MaxHeadingTitle = 9; var g_LongHeadingWidth = 250; var g_PlayerBoxYSize = 40; var g_PlayerBoxGap = 2; var g_PlayerBoxAlpha = " 50"; var g_TeamsBoxYStart = 40; var g_TypeColors = { "blue": "196 198 255", "green": "201 255 200", "red": "255 213 213", "yellow": "255 255 157" } /** * Colors, captions and format used for units, buildings, etc. types */ var g_SummaryTypes = { "percent": { "color": "", "caption": "%", "postfix": "%" }, "trained": { "color": g_TypeColors.green, "caption": translate("Trained"), "postfix": " / " }, "constructed": { "color": g_TypeColors.green, "caption": translate("Constructed"), "postfix": " / " }, "gathered": { "color": g_TypeColors.green, "caption": translate("Gathered"), "postfix": " / " }, "sent": { "color": g_TypeColors.green, "caption": translate("Sent"), "postfix": " / " }, "bought": { "color": g_TypeColors.green, "caption": translate("Bought"), "postfix": " / " }, "income": { "color": g_TypeColors.green, "caption": translate("Income"), "postfix": " / " }, "captured": { "color": g_TypeColors.yellow, "caption": translate("Captured"), "postfix": " / " }, + "succeeded": { + "color": g_TypeColors.green, + "caption": translate("Succeeded"), + "postfix": " / " + }, "destroyed": { "color": g_TypeColors.blue, "caption": translate("Destroyed"), "postfix": "\n" }, "killed": { "color": g_TypeColors.blue, "caption": translate("Killed"), "postfix": "\n" }, "lost": { "color": g_TypeColors.red, "caption": translate("Lost"), "postfix": "\n" }, "used": { "color": g_TypeColors.red, "caption": translate("Used"), "postfix": "\n" }, "received": { "color": g_TypeColors.red, "caption": translate("Received"), "postfix": "\n" }, "sold": { "color": g_TypeColors.red, "caption": translate("Sold"), "postfix": "\n" }, "outcome": { "color": g_TypeColors.red, "caption": translate("Outcome"), "postfix": "\n" + }, + "failed": { + "color": g_TypeColors.red, + "caption": translate("Failed"), + "postfix": "\n" } }; /** * Translation: Unicode encoded infinity symbol indicating a division by zero in the summary screen. */ var g_InfinitySymbol = translate("\u221E"); var g_Teams = []; // TODO set g_PlayerCount as playerCounters.length var g_PlayerCount = 0; var g_GameData; var g_ResourceData = new Resources(); // Selected chart indexes var g_SelectedChart = { "category": [0, 0], "value": [0, 0], "type": [0, 0] }; function selectPanel(panel) { // TODO: move panel buttons to a custom parent object for (let button of Engine.GetGUIObjectByName("summaryWindow").children) if (button.name.endsWith("PanelButton")) button.sprite = "ModernTabHorizontalBackground"; panel.sprite = "ModernTabHorizontalForeground"; adjustTabDividers(panel.size); let generalPanel = Engine.GetGUIObjectByName("generalPanel"); let chartsPanel = Engine.GetGUIObjectByName("chartsPanel"); let chartsHidden = panel.name != "chartsPanelButton"; generalPanel.hidden = !chartsHidden; chartsPanel.hidden = chartsHidden; if (chartsHidden) updatePanelData(g_ScorePanelsData[panel.name.substr(0, panel.name.length - "PanelButton".length)]); else [0, 1].forEach(updateCategoryDropdown); } function initCharts() { let player_colors = []; for (let i = 1; i <= g_PlayerCount; ++i) { let playerState = g_GameData.sim.playerStates[i]; player_colors.push( Math.floor(playerState.color.r * 255) + " " + Math.floor(playerState.color.g * 255) + " " + Math.floor(playerState.color.b * 255) ); } [0, 1].forEach(i => Engine.GetGUIObjectByName("chart[" + i + "]").series_color = player_colors); let chartLegend = Engine.GetGUIObjectByName("chartLegend"); chartLegend.caption = g_GameData.sim.playerStates.slice(1).map( (state, index) => '[color="' + player_colors[index] + '"]■[/color] ' + state.name ).join(" "); let chart1Part = Engine.GetGUIObjectByName("chart[1]Part"); let chart1PartSize = chart1Part.size; chart1PartSize.rright += 50; chart1PartSize.rleft += 50; chart1PartSize.right -= 5; chart1PartSize.left -= 5; chart1Part.size = chart1PartSize; } function resizeDropdown(dropdown) { let size = dropdown.size; size.bottom = dropdown.size.top + (Engine.GetTextWidth(dropdown.font, dropdown.list[dropdown.selected]) > dropdown.size.right - dropdown.size.left - 32 ? 42 : 27); dropdown.size = size; } function updateCategoryDropdown(number) { let chartCategory = Engine.GetGUIObjectByName("chart[" + number + "]CategorySelection"); chartCategory.list_data = Object.keys(g_ScorePanelsData); chartCategory.list = Object.keys(g_ScorePanelsData).map(panel => g_ScorePanelsData[panel].caption); chartCategory.onSelectionChange = function() { if (!this.list_data[this.selected]) return; if (g_SelectedChart.category[number] != this.selected) { g_SelectedChart.category[number] = this.selected; g_SelectedChart.value[number] = 0; g_SelectedChart.type[number] = 0; } resizeDropdown(this); updateValueDropdown(number, this.list_data[this.selected]); }; chartCategory.selected = g_SelectedChart.category[number]; } function updateValueDropdown(number, category) { let chartValue = Engine.GetGUIObjectByName("chart[" + number + "]ValueSelection"); let list = g_ScorePanelsData[category].headings.map(heading => heading.caption); list.shift(); chartValue.list = list; let list_data = g_ScorePanelsData[category].headings.map(heading => heading.identifier); list_data.shift(); chartValue.list_data = list_data; chartValue.onSelectionChange = function() { if (!this.list_data[this.selected]) return; if (g_SelectedChart.value[number] != this.selected) { g_SelectedChart.value[number] = this.selected; g_SelectedChart.type[number] = 0; } resizeDropdown(this); updateTypeDropdown(number, category, this.list_data[this.selected], this.selected); }; chartValue.selected = g_SelectedChart.value[number]; } function updateTypeDropdown(number, category, item, itemNumber) { let testValue = g_ScorePanelsData[category].counters[itemNumber].fn(g_GameData.sim.playerStates[1], 0, item); let hide = !g_ScorePanelsData[category].counters[itemNumber].fn || typeof testValue != "object" || Object.keys(testValue).length < 2; Engine.GetGUIObjectByName("chart[" + number + "]TypeLabel").hidden = hide; let chartType = Engine.GetGUIObjectByName("chart[" + number + "]TypeSelection"); chartType.hidden = hide; if (hide) { updateChart(number, category, item, itemNumber, Object.keys(testValue)[0] || undefined); return; } chartType.list = Object.keys(testValue).map(type => g_SummaryTypes[type].caption); chartType.list_data = Object.keys(testValue); chartType.onSelectionChange = function() { if (!this.list_data[this.selected]) return; g_SelectedChart.type[number] = this.selected; resizeDropdown(this); updateChart(number, category, item, itemNumber, this.list_data[this.selected]); }; chartType.selected = g_SelectedChart.type[number]; } function updateChart(number, category, item, itemNumber, type) { if (!g_ScorePanelsData[category].counters[itemNumber].fn) return; let chart = Engine.GetGUIObjectByName("chart[" + number + "]"); let series = []; for (let j = 1; j <= g_PlayerCount; ++j) { let playerState = g_GameData.sim.playerStates[j]; let data = []; for (let index in playerState.sequences.time) { let value = g_ScorePanelsData[category].counters[itemNumber].fn(playerState, index, item); if (type) value = value[type]; data.push([playerState.sequences.time[index], value]); } series.push(data); } chart.series = series; } function adjustTabDividers(tabSize) { let leftSpacer = Engine.GetGUIObjectByName("tabDividerLeft"); let rightSpacer = Engine.GetGUIObjectByName("tabDividerRight"); leftSpacer.size = [ 20, leftSpacer.size.top, tabSize.left + 2, leftSpacer.size.bottom ].join(" "); rightSpacer.size = [ tabSize.right - 2, rightSpacer.size.top, "100%-20", rightSpacer.size.bottom ].join(" "); } function updatePanelData(panelInfo) { resetGeneralPanel(); updateGeneralPanelHeadings(panelInfo.headings); updateGeneralPanelTitles(panelInfo.titleHeadings); let rowPlayerObjectWidth = updateGeneralPanelCounter(panelInfo.counters); updateGeneralPanelTeams(); let index = g_GameData.sim.playerStates[1].sequences.time.length - 1; let playerBoxesCounts = []; for (let i = 0; i < g_PlayerCount; ++i) { let playerState = g_GameData.sim.playerStates[i+1]; if (!playerBoxesCounts[playerState.team+1]) playerBoxesCounts[playerState.team+1] = 1; else playerBoxesCounts[playerState.team+1] += 1; let positionObject = playerBoxesCounts[playerState.team+1] - 1; let rowPlayer = "playerBox[" + positionObject + "]"; let playerOutcome = "playerOutcome[" + positionObject + "]"; let playerNameColumn = "playerName[" + positionObject + "]"; let playerCivicBoxColumn = "civIcon[" + positionObject + "]"; let playerCounterValue = "valueData[" + positionObject + "]"; if (playerState.team != -1) { rowPlayer = "playerBoxt[" + playerState.team + "][" + positionObject + "]"; playerOutcome = "playerOutcomet[" + playerState.team + "][" + positionObject + "]"; playerNameColumn = "playerNamet[" + playerState.team + "][" + positionObject + "]"; playerCivicBoxColumn = "civIcont[" + playerState.team + "][" + positionObject + "]"; playerCounterValue = "valueDataTeam[" + playerState.team + "][" + positionObject + "]"; } let colorString = "color: " + Math.floor(playerState.color.r * 255) + " " + Math.floor(playerState.color.g * 255) + " " + Math.floor(playerState.color.b * 255); let rowPlayerObject = Engine.GetGUIObjectByName(rowPlayer); rowPlayerObject.hidden = false; rowPlayerObject.sprite = colorString + g_PlayerBoxAlpha; let boxSize = rowPlayerObject.size; boxSize.right = rowPlayerObjectWidth; rowPlayerObject.size = boxSize; setOutcomeIcon(playerState.state, playerOutcome); Engine.GetGUIObjectByName(playerNameColumn).caption = g_GameData.sim.playerStates[i+1].name; let civIcon = Engine.GetGUIObjectByName(playerCivicBoxColumn); civIcon.sprite = "stretched:" + g_CivData[playerState.civ].Emblem; civIcon.tooltip = g_CivData[playerState.civ].Name; updateCountersPlayer(playerState, panelInfo.counters, panelInfo.headings, playerCounterValue, index); } let teamCounterFn = panelInfo.teamCounterFn; if (g_Teams && teamCounterFn) updateCountersTeam(teamCounterFn, panelInfo.counters, panelInfo.headings, index); } function confirmStartReplay() { if (Engine.HasXmppClient()) messageBox( 400, 200, translate("Are you sure you want to quit the lobby?"), translate("Confirmation"), [translate("No"), translate("Yes")], [null, startReplay] ); else startReplay(); } function continueButton() { if (g_GameData.gui.isInGame) Engine.PopGuiPageCB(0); else if (g_GameData.gui.isReplay) Engine.SwitchGuiPage("page_replaymenu.xml", { "replaySelectionData": g_GameData.gui.replaySelectionData }); else if (Engine.HasXmppClient()) Engine.SwitchGuiPage("page_lobby.xml"); else Engine.SwitchGuiPage("page_pregame.xml"); } function startReplay() { if (Engine.HasXmppClient()) Engine.StopXmppClient(); if (!Engine.StartVisualReplay(g_GameData.gui.replayDirectory)) { warn("Replay file not found!"); return; } Engine.SwitchGuiPage("page_loading.xml", { "attribs": Engine.GetReplayAttributes(g_GameData.gui.replayDirectory), "isNetworked": false, "playerAssignments": { "local": { "name": singleplayerName(), "player": -1 } }, "savedGUIData": "", "isReplay": true, "replaySelectionData": g_GameData.gui.replaySelectionData }); } function init(data) { g_GameData = data; let assignedState = g_GameData.sim.playerStates[g_GameData.gui.assignedPlayer || -1]; Engine.GetGUIObjectByName("summaryText").caption = g_GameData.gui.isInGame ? translate("Current Scores") : g_GameData.gui.isReplay ? translate("Scores at the end of the game.") : g_GameData.gui.disconnected ? translate("You have been disconnected.") : !assignedState ? translate("You have left the game.") : assignedState.state == "won" ? translate("You have won the battle!") : assignedState.state == "defeated" ? translate("You have been defeated...") : translate("You have abandoned the game."); initPlayerBoxPositions(); Engine.GetGUIObjectByName("timeElapsed").caption = sprintf( translate("Game time elapsed: %(time)s"), { "time": timeToString(g_GameData.sim.timeElapsed) }); let mapType = g_Settings.MapTypes.find(mapType => mapType.Name == g_GameData.sim.mapSettings.mapType); let mapSize = g_Settings.MapSizes.find(size => size.Tiles == g_GameData.sim.mapSettings.Size || 0); Engine.GetGUIObjectByName("mapName").caption = sprintf( translate("%(mapName)s - %(mapType)s"), { "mapName": translate(g_GameData.sim.mapSettings.Name), "mapType": mapSize ? mapSize.Name : (mapType ? mapType.Title : "") }); Engine.GetGUIObjectByName("replayButton").hidden = g_GameData.gui.isInGame || !g_GameData.gui.replayDirectory; // Panels g_PlayerCount = g_GameData.sim.playerStates.length - 1; if (g_GameData.sim.mapSettings.LockTeams) { // Count teams for (let player = 1; player <= g_PlayerCount; ++player) { let playerTeam = g_GameData.sim.playerStates[player].team; if (!g_Teams[playerTeam]) g_Teams[playerTeam] = []; g_Teams[playerTeam].push(player); } if (g_Teams.every(team => team && team.length < 2)) g_Teams = false; // Each player has his own team. Displaying teams makes no sense. } else g_Teams = false; // Erase teams data if teams are not displayed if (!g_Teams) { for (let p = 0; p < g_PlayerCount; ++p) g_GameData.sim.playerStates[p+1].team = -1; } calculateTeamCounterDataHelper(); initCharts(); selectPanel(Engine.GetGUIObjectByName("scorePanelButton")); } Index: ps/trunk/binaries/data/mods/public/simulation/components/StatisticsTracker.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/StatisticsTracker.js (revision 20270) +++ ps/trunk/binaries/data/mods/public/simulation/components/StatisticsTracker.js (revision 20271) @@ -1,573 +1,587 @@ function StatisticsTracker() {} const g_UpdateSequenceInterval = 30 * 1000; StatisticsTracker.prototype.Schema = ""; StatisticsTracker.prototype.Init = function() { this.unitsClasses = [ "Infantry", "Worker", "FemaleCitizen", "Cavalry", "Champion", "Hero", "Siege", "Ship", "Trader" ]; this.unitsTrained = { "Infantry": 0, "Worker": 0, "FemaleCitizen": 0, "Cavalry": 0, "Champion": 0, "Hero": 0, "Siege": 0, "Ship": 0, "Trader": 0, "total": 0 }; this.domesticUnitsTrainedValue = 0; this.unitsLost = { "Infantry": 0, "Worker": 0, "FemaleCitizen": 0, "Cavalry": 0, "Champion": 0, "Hero": 0, "Siege": 0, "Ship": 0, "Trader": 0, "total": 0 }; this.unitsLostValue = 0; this.enemyUnitsKilled = { "Infantry": 0, "Worker": 0, "FemaleCitizen": 0, "Cavalry": 0, "Champion": 0, "Hero": 0, "Siege": 0, "Ship": 0, "Trader": 0, "total": 0 }; this.enemyUnitsKilledValue = 0; this.unitsCaptured = { "Infantry": 0, "Worker": 0, "FemaleCitizen": 0, "Cavalry": 0, "Champion": 0, "Hero": 0, "Siege": 0, "Ship": 0, "Trader": 0, "total": 0 }; this.unitsCapturedValue = 0; this.buildingsClasses = [ "House", "Economic", "Outpost", "Military", "Fortress", "CivCentre", "Wonder" ]; this.buildingsConstructed = { "House": 0, "Economic": 0, "Outpost": 0, "Military": 0, "Fortress": 0, "CivCentre": 0, "Wonder": 0, "total": 0 }; this.buildingsLost = { "House": 0, "Economic": 0, "Outpost": 0, "Military": 0, "Fortress": 0, "CivCentre": 0, "Wonder": 0, "total": 0 }; this.buildingsLostValue = 0; this.enemyBuildingsDestroyed = { "House": 0, "Economic": 0, "Outpost": 0, "Military": 0, "Fortress": 0, "CivCentre": 0, "Wonder": 0, "total": 0 }; this.enemyBuildingsDestroyedValue = 0; this.buildingsCaptured = { "House": 0, "Economic": 0, "Outpost": 0, "Military": 0, "Fortress": 0, "CivCentre": 0, "Wonder": 0, "total": 0 }; this.buildingsCapturedValue = 0; this.resourcesGathered = { "vegetarianFood": 0 }; this.resourcesUsed = {}; this.resourcesSold = {}; this.resourcesBought = {}; for (let res of Resources.GetCodes()) { this.resourcesGathered[res] = 0; this.resourcesUsed[res] = 0; this.resourcesSold[res] = 0; this.resourcesBought[res] = 0; } this.tributesSent = 0; this.tributesReceived = 0; this.tradeIncome = 0; this.treasuresCollected = 0; this.lootCollected = 0; this.peakPercentMapControlled = 0; this.teamPeakPercentMapControlled = 0; + this.successfulBribes = 0; + this.failedBribes = 0; let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); this.updateTimer = cmpTimer.SetInterval( this.entity, IID_StatisticsTracker, "UpdateSequences", 0, g_UpdateSequenceInterval); }; StatisticsTracker.prototype.OnGlobalInitGame = function() { this.sequences = clone(this.GetStatistics()); this.sequences.time = []; }; /** * Returns a subset of statistics that will be added to the simulation state, * thus called each turn. Basic statistics should not contain data that would * be expensive to compute. * * Note: as of now, nothing in the game needs that, but some AIs developed by * modders need it in the API. */ StatisticsTracker.prototype.GetBasicStatistics = function() { return { "resourcesGathered": this.resourcesGathered, "percentMapExplored": this.GetPercentMapExplored() }; }; StatisticsTracker.prototype.GetStatistics = function() { return { "unitsTrained": this.unitsTrained, "domesticUnitsTrainedValue": this.domesticUnitsTrainedValue, "unitsLost": this.unitsLost, "unitsLostValue": this.unitsLostValue, "enemyUnitsKilled": this.enemyUnitsKilled, "enemyUnitsKilledValue": this.enemyUnitsKilledValue, "unitsCaptured": this.unitsCaptured, "unitsCapturedValue": this.unitsCapturedValue, "buildingsConstructed": this.buildingsConstructed, "buildingsLost": this.buildingsLost, "buildingsLostValue": this.buildingsLostValue, "enemyBuildingsDestroyed": this.enemyBuildingsDestroyed, "enemyBuildingsDestroyedValue": this.enemyBuildingsDestroyedValue, "buildingsCaptured": this.buildingsCaptured, "buildingsCapturedValue": this.buildingsCapturedValue, "resourcesGathered": this.resourcesGathered, "resourcesUsed": this.resourcesUsed, "resourcesSold": this.resourcesSold, "resourcesBought": this.resourcesBought, "tributesSent": this.tributesSent, "tributesReceived": this.tributesReceived, "tradeIncome": this.tradeIncome, "treasuresCollected": this.treasuresCollected, "lootCollected": this.lootCollected, "percentMapExplored": this.GetPercentMapExplored(), "teamPercentMapExplored": this.GetTeamPercentMapExplored(), "percentMapControlled": this.GetPercentMapControlled(), "teamPercentMapControlled": this.GetTeamPercentMapControlled(), "peakPercentMapControlled": this.peakPercentMapControlled, - "teamPeakPercentMapControlled": this.teamPeakPercentMapControlled + "teamPeakPercentMapControlled": this.teamPeakPercentMapControlled, + "successfulBribes": this.successfulBribes, + "failedBribes": this.failedBribes }; }; StatisticsTracker.prototype.GetSequences = function() { let ret = clone(this.sequences); let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); ret.time.push(cmpTimer.GetTime() / 1000); this.PushValue(this.GetStatistics(), ret); return ret; }; /** * Used to print statistics for non-visual autostart games. * @return The player's statistics as a JSON string beautified with some indentations. */ StatisticsTracker.prototype.GetStatisticsJSON = function() { let cmpPlayer = Engine.QueryInterface(this.entity, IID_Player); let playerStatistics = { "playerID": cmpPlayer.GetPlayerID(), "playerState": cmpPlayer.GetState(), "statistics": this.GetStatistics() }; return JSON.stringify(playerStatistics, null, "\t"); }; /** * Increments counter associated with certain entity/counter and type of given entity. * @param cmpIdentity - the entity identity component. * @param counter - the name of the counter to increment (e.g. "unitsTrained"). * @param type - the type of the counter (e.g. "workers"). */ StatisticsTracker.prototype.CounterIncrement = function(cmpIdentity, counter, type) { var classes = cmpIdentity.GetClassesList(); if (!classes) return; if (classes.indexOf(type) != -1) ++this[counter][type]; }; /** * Counts the total number of units trained as well as an individual count for * each unit type. Based on templates. */ StatisticsTracker.prototype.IncreaseTrainedUnitsCounter = function(trainedUnit) { let cmpUnitEntityIdentity = Engine.QueryInterface(trainedUnit, IID_Identity); if (!cmpUnitEntityIdentity) return; let cmpCost = Engine.QueryInterface(trainedUnit, IID_Cost); let costs = cmpCost && cmpCost.GetResourceCosts(); for (let type of this.unitsClasses) this.CounterIncrement(cmpUnitEntityIdentity, "unitsTrained", type); ++this.unitsTrained.total; if (cmpUnitEntityIdentity.HasClass("Domestic") && costs) for (let type in costs) this.domesticUnitsTrainedValue += costs[type]; }; /** * Counts the total number of buildings constructed as well as an individual count for * each building type. Based on templates. */ StatisticsTracker.prototype.IncreaseConstructedBuildingsCounter = function(constructedBuilding) { var cmpBuildingEntityIdentity = Engine.QueryInterface(constructedBuilding, IID_Identity); if (!cmpBuildingEntityIdentity) return; for (let type of this.buildingsClasses) this.CounterIncrement(cmpBuildingEntityIdentity, "buildingsConstructed", type); ++this.buildingsConstructed.total; }; StatisticsTracker.prototype.KilledEntity = function(targetEntity) { var cmpTargetEntityIdentity = Engine.QueryInterface(targetEntity, IID_Identity); if (!cmpTargetEntityIdentity) return; var cmpCost = Engine.QueryInterface(targetEntity, IID_Cost); var costs = cmpCost && cmpCost.GetResourceCosts(); var cmpTargetOwnership = Engine.QueryInterface(targetEntity, IID_Ownership); // Ignore gaia if (cmpTargetOwnership.GetOwner() == 0) return; if (cmpTargetEntityIdentity.HasClass("Unit") && !cmpTargetEntityIdentity.HasClass("Domestic")) { for (let type of this.unitsClasses) this.CounterIncrement(cmpTargetEntityIdentity, "enemyUnitsKilled", type); ++this.enemyUnitsKilled.total; if (costs) for (let type in costs) this.enemyUnitsKilledValue += costs[type]; } let cmpFoundation = Engine.QueryInterface(targetEntity, IID_Foundation); if (cmpTargetEntityIdentity.HasClass("Structure") && !cmpFoundation) { for (let type of this.buildingsClasses) this.CounterIncrement(cmpTargetEntityIdentity, "enemyBuildingsDestroyed", type); ++this.enemyBuildingsDestroyed.total; if (costs) for (let type in costs) this.enemyBuildingsDestroyedValue += costs[type]; } }; StatisticsTracker.prototype.LostEntity = function(lostEntity) { var cmpLostEntityIdentity = Engine.QueryInterface(lostEntity, IID_Identity); if (!cmpLostEntityIdentity) return; var cmpCost = Engine.QueryInterface(lostEntity, IID_Cost); var costs = cmpCost && cmpCost.GetResourceCosts(); if (cmpLostEntityIdentity.HasClass("Unit") && !cmpLostEntityIdentity.HasClass("Domestic")) { for (let type of this.unitsClasses) this.CounterIncrement(cmpLostEntityIdentity, "unitsLost", type); ++this.unitsLost.total; if (costs) for (let type in costs) this.unitsLostValue += costs[type]; } let cmpFoundation = Engine.QueryInterface(lostEntity, IID_Foundation); if (cmpLostEntityIdentity.HasClass("Structure") && !cmpFoundation) { for (let type of this.buildingsClasses) this.CounterIncrement(cmpLostEntityIdentity, "buildingsLost", type); ++this.buildingsLost.total; if (costs) for (let type in costs) this.buildingsLostValue += costs[type]; } }; StatisticsTracker.prototype.CapturedEntity = function(capturedEntity) { let cmpCapturedEntityIdentity = Engine.QueryInterface(capturedEntity, IID_Identity); if (!cmpCapturedEntityIdentity) return; let cmpCost = Engine.QueryInterface(capturedEntity, IID_Cost); let costs = cmpCost && cmpCost.GetResourceCosts(); if (cmpCapturedEntityIdentity.HasClass("Unit")) { for (let type of this.unitsClasses) this.CounterIncrement(cmpCapturedEntityIdentity, "unitsCaptured", type); ++this.unitsCaptured.total; if (costs) for (let type in costs) this.unitsCapturedValue += costs[type]; } if (cmpCapturedEntityIdentity.HasClass("Structure")) { for (let type of this.buildingsClasses) this.CounterIncrement(cmpCapturedEntityIdentity, "buildingsCaptured", type); ++this.buildingsCaptured.total; if (costs) for (let type in costs) this.buildingsCapturedValue += costs[type]; } }; /** * @param {string} type - generic type of resource. * @param {number} amount - amount of resource, whick should be added. * @param {string} specificType - specific type of resource. */ StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amount, specificType) { this.resourcesGathered[type] += amount; if (type == "food" && (specificType == "fruit" || specificType == "grain")) this.resourcesGathered.vegetarianFood += amount; }; /** * @param {string} type - generic type of resource. * @param {number} amount - amount of resource, which should be added. */ StatisticsTracker.prototype.IncreaseResourceUsedCounter = function(type, amount) { this.resourcesUsed[type] += amount; }; StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function() { ++this.treasuresCollected; }; StatisticsTracker.prototype.IncreaseLootCollectedCounter = function(amount) { for (let type in amount) this.lootCollected += amount[type]; }; StatisticsTracker.prototype.IncreaseResourcesSoldCounter = function(type, amount) { this.resourcesSold[type] += amount; }; StatisticsTracker.prototype.IncreaseResourcesBoughtCounter = function(type, amount) { this.resourcesBought[type] += amount; }; StatisticsTracker.prototype.IncreaseTributesSentCounter = function(amount) { this.tributesSent += amount; }; StatisticsTracker.prototype.IncreaseTributesReceivedCounter = function(amount) { this.tributesReceived += amount; }; StatisticsTracker.prototype.IncreaseTradeIncomeCounter = function(amount) { this.tradeIncome += amount; }; +StatisticsTracker.prototype.IncreaseSuccessfulBribesCounter = function() +{ + ++this.successfulBribes; +}; + +StatisticsTracker.prototype.IncreaseFailedBribesCounter = function() +{ + ++this.failedBribes; +}; + StatisticsTracker.prototype.GetPercentMapExplored = function() { var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player); return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID()); }; /** * Note: cmpRangeManager.GetUnionPercentMapExplored computes statistics from scratch! * As a consequence, this function should not be called too often. */ StatisticsTracker.prototype.GetTeamPercentMapExplored = function() { var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player); if (!cmpPlayer) return 0; var team = cmpPlayer.GetTeam(); // If teams are not locked, this statistic won't be displayed, so don't bother computing if (team == -1 || !cmpPlayer.GetLockTeams()) return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID()); var teamPlayers = []; for (var i = 1; i < cmpPlayerManager.GetNumPlayers(); ++i) { let cmpOtherPlayer = QueryPlayerIDInterface(i); if (cmpOtherPlayer && cmpOtherPlayer.GetTeam() == team) teamPlayers.push(i); } return cmpRangeManager.GetUnionPercentMapExplored(teamPlayers); }; StatisticsTracker.prototype.GetPercentMapControlled = function() { var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player); var cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager); if (!cmpPlayer || !cmpTerritoryManager) return 0; return cmpTerritoryManager.GetTerritoryPercentage(cmpPlayer.GetPlayerID()); }; StatisticsTracker.prototype.GetTeamPercentMapControlled = function() { var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player); var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); var cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager); if (!cmpPlayer || !cmpTerritoryManager) return 0; var team = cmpPlayer.GetTeam(); if (team == -1 || !cmpPlayer.GetLockTeams()) return cmpTerritoryManager.GetTerritoryPercentage(cmpPlayer.GetPlayerID()); var teamPercent = 0; for (let i = 1; i < cmpPlayerManager.GetNumPlayers(); ++i) { let cmpOtherPlayer = QueryPlayerIDInterface(i); if (cmpOtherPlayer && cmpOtherPlayer.GetTeam() == team) teamPercent += cmpTerritoryManager.GetTerritoryPercentage(i); } return teamPercent; }; StatisticsTracker.prototype.OnTerritoriesChanged = function(msg) { var newPercent = this.GetPercentMapControlled(); if (newPercent > this.peakPercentMapControlled) this.peakPercentMapControlled = newPercent; newPercent = this.GetTeamPercentMapControlled(); if (newPercent > this.teamPeakPercentMapControlled) this.teamPeakPercentMapControlled = newPercent; }; /** * Adds the values of fromData to the end of the arrays of toData. * If toData misses the needed array, one will be created. * * @param fromData - an object of values or a value. * @param toData - an object of arrays or an array. **/ StatisticsTracker.prototype.PushValue = function(fromData, toData) { if (typeof fromData == "object") for (let prop in fromData) { if (typeof toData[prop] != "object") toData[prop] = [fromData[prop]]; else this.PushValue(fromData[prop], toData[prop]); } else toData.push(fromData); }; StatisticsTracker.prototype.UpdateSequences = function() { let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); this.sequences.time.push(cmpTimer.GetTime() / 1000); this.PushValue(this.GetStatistics(), this.sequences); }; Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker); Index: ps/trunk/binaries/data/mods/public/simulation/components/VisionSharing.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/VisionSharing.js (revision 20270) +++ ps/trunk/binaries/data/mods/public/simulation/components/VisionSharing.js (revision 20271) @@ -1,173 +1,178 @@ function VisionSharing() {} VisionSharing.prototype.Schema = "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + ""; VisionSharing.prototype.Init = function() { this.activated = false; this.shared = undefined; this.spyId = 0; this.spies = undefined; }; /** * As entities have not necessarily the VisionSharing component, it has to be activated * before use so that the rangeManager can register it */ VisionSharing.prototype.Activate = function() { if (this.activated) return; let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (!cmpOwnership || cmpOwnership.GetOwner() <= 0) return; this.shared = new Set([cmpOwnership.GetOwner()]); Engine.PostMessage(this.entity, MT_VisionSharingChanged, { "entity": this.entity, "player": cmpOwnership.GetOwner(), "add": true }); this.activated = true; }; VisionSharing.prototype.CheckVisionSharings = function() { let shared = new Set(); let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); let owner = cmpOwnership ? cmpOwnership.GetOwner() : -1; if (owner >= 0) { // The owner has vision if (owner > 0) shared.add(owner); // Vision sharing due to garrisoned units let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder); if (cmpGarrisonHolder) { for (let ent of cmpGarrisonHolder.GetEntities()) { let cmpEntOwnership = Engine.QueryInterface(ent, IID_Ownership); if (!cmpEntOwnership) continue; let entOwner = cmpEntOwnership.GetOwner(); if (entOwner > 0 && entOwner != owner) { shared.add(entOwner); // if shared by another player than the owner and not yet activated, do it this.Activate(); } } } // vision sharing due to spies if (this.spies) for (let spy of this.spies.values()) if (spy > 0 && spy != owner) shared.add(spy); } if (!this.activated) return; // compare with previous vision sharing, and update if needed for (let player of shared) if (!this.shared.has(player)) Engine.PostMessage(this.entity, MT_VisionSharingChanged, { "entity": this.entity, "player": player, "add": true }); for (let player of this.shared) if (!shared.has(player)) Engine.PostMessage(this.entity, MT_VisionSharingChanged, { "entity": this.entity, "player": player, "add": false }); this.shared = shared; }; VisionSharing.prototype.IsBribable = function() { return this.template.Bribable == "true"; }; VisionSharing.prototype.OnGarrisonedUnitsChanged = function(msg) { this.CheckVisionSharings(); }; VisionSharing.prototype.OnOwnershipChanged = function(msg) { if (this.activated) this.CheckVisionSharings(); }; VisionSharing.prototype.AddSpy = function(player, timeLength) { if (!this.IsBribable()) return 0; let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (!cmpOwnership || cmpOwnership.GetOwner() == player || player <= 0) return 0; let cmpTechnologyManager = QueryPlayerIDInterface(player, IID_TechnologyManager); if (!cmpTechnologyManager || !cmpTechnologyManager.CanProduce("special/spy")) return 0; let template = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).GetTemplate("special/spy"); if (!IncurBribeCost(template, player, cmpOwnership.GetOwner(), false)) return 0; // If no duration given, take it from the spy template and scale it with the ent vision // When no duration argument nor in spy template, it is a permanent spy let duration = timeLength; if (!duration && template.VisionSharing && template.VisionSharing.Duration) { duration = ApplyValueModificationsToTemplate("VisionSharing/Duration", +template.VisionSharing.Duration, player, template); let cmpVision = Engine.QueryInterface(this.entity, IID_Vision); if (cmpVision) duration *= 60 / Math.max(30, cmpVision.GetRange()); } if (!this.spies) this.spies = new Map(); this.spies.set(++this.spyId, player); if (duration) { let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); cmpTimer.SetTimeout(this.entity, IID_VisionSharing, "RemoveSpy", duration * 1000, { "id": this.spyId }); } this.Activate(); this.CheckVisionSharings(); + // update statistics for successful bribes + let cmpBribesStatisticsTracker = QueryPlayerIDInterface(player, IID_StatisticsTracker); + if (cmpBribesStatisticsTracker) + cmpBribesStatisticsTracker.IncreaseSuccessfulBribesCounter(); + return this.spyId; }; VisionSharing.prototype.RemoveSpy = function(data) { this.spies.delete(data.id); this.CheckVisionSharings(); }; /** * Returns true if this entity share its vision with player */ VisionSharing.prototype.ShareVisionWith = function(player) { if (this.activated) return this.shared.has(player); let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); return cmpOwnership && cmpOwnership.GetOwner() == player; }; Engine.RegisterComponentType(IID_VisionSharing, "VisionSharing", VisionSharing); Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_VisionSharing.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_VisionSharing.js (revision 20270) +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_VisionSharing.js (revision 20271) @@ -1,177 +1,182 @@ Engine.LoadHelperScript("Player.js"); Engine.LoadHelperScript("ValueModification.js"); Engine.LoadHelperScript("Commands.js"); Engine.LoadComponentScript("interfaces/GarrisonHolder.js"); Engine.LoadComponentScript("interfaces/TechnologyManager.js"); Engine.LoadComponentScript("interfaces/AuraManager.js"); Engine.LoadComponentScript("interfaces/Timer.js"); Engine.LoadComponentScript("interfaces/VisionSharing.js"); +Engine.LoadComponentScript("interfaces/StatisticsTracker.js"); Engine.LoadComponentScript("VisionSharing.js"); const ent = 170; let template = { "Bribable": "true" }; AddMock(SYSTEM_ENTITY, IID_TemplateManager, { "GetTemplate": (name) => name == "special/spy" ? ({ "Cost": { "Resources": { "wood": 1000 } }, "VisionSharing": { "Duration": 15 } }) : ({}) }); AddMock(ent, IID_GarrisonHolder, { "GetEntities": () => [] }); AddMock(ent, IID_Ownership, { "GetOwner": () => 1 }); let cmpVisionSharing = ConstructComponent(ent, "VisionSharing", template); // Add some entities AddMock(180, IID_Ownership, { "GetOwner": () => 2 }); AddMock(181, IID_Ownership, { "GetOwner": () => 1 }); AddMock(182, IID_Ownership, { "GetOwner": () => 8 }); AddMock(183, IID_Ownership, { "GetOwner": () => 2 }); TS_ASSERT_EQUALS(cmpVisionSharing.activated, false); // Test Activate cmpVisionSharing.activated = false; cmpVisionSharing.Activate(); TS_ASSERT_EQUALS(cmpVisionSharing.activated, true); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1]); // Test CheckVisionSharings cmpVisionSharing.activated = true; cmpVisionSharing.shared = new Set([1]); AddMock(ent, IID_GarrisonHolder, { "GetEntities": () => [181] }); Engine.PostMessage = function(id, iid, message) { TS_ASSERT(false); // One doesn't send message }; cmpVisionSharing.CheckVisionSharings(); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1]); cmpVisionSharing.shared = new Set([1, 2, 8]); AddMock(ent, IID_GarrisonHolder, { "GetEntities": () => [180] }); Engine.PostMessage = function(id, iid, message) { TS_ASSERT_UNEVAL_EQUALS({ "entity": ent, "player": 8, "add": false }, message); }; cmpVisionSharing.CheckVisionSharings(); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1, 2]); cmpVisionSharing.shared = new Set([1, 8]); AddMock(ent, IID_GarrisonHolder, { "GetEntities": () => [181, 182, 183] }); Engine.PostMessage = function(id, iid, message) { TS_ASSERT_UNEVAL_EQUALS({ "entity": ent, "player": 2, "add": true }, message); }; cmpVisionSharing.CheckVisionSharings(); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1, 8, 2]); // take care of order or sort them // Test IsBribable TS_ASSERT(cmpVisionSharing.IsBribable()); // Test RemoveSpy AddMock(ent, IID_GarrisonHolder, { "GetEntities": () => [] }); cmpVisionSharing.spies = new Map([[5, 2], [17, 5]]); cmpVisionSharing.shared = new Set([1, 2, 5]); Engine.PostMessage = function(id, iid, message) { TS_ASSERT_UNEVAL_EQUALS({ "entity": ent, "player": 2, "add": false }, message); }; cmpVisionSharing.RemoveSpy({ "id": 5 }); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1, 5]); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.spies], [[17, 5]]); Engine.PostMessage = function(id, iid, message) {}; // Test AddSpy cmpVisionSharing.spies = new Map([[5, 2], [17, 5]]); cmpVisionSharing.shared = new Set([1, 2, 5]); cmpVisionSharing.spyId = 20; AddMock(SYSTEM_ENTITY, IID_PlayerManager, { "GetPlayerByID": id => 14 }); AddMock(14, IID_TechnologyManager, { "CanProduce": entity => false, "ApplyModificationsTemplate": (valueName, curValue, template) => curValue }); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1, 2, 5]); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.spies], [[5, 2], [17, 5]]); TS_ASSERT_EQUALS(cmpVisionSharing.spyId, 20); AddMock(14, IID_TechnologyManager, { "CanProduce": entity => entity == "special/spy", "ApplyModificationsTemplate": (valueName, curValue, template) => curValue }); AddMock(14, IID_Player, { "GetSpyCostMultiplier": () => 1, "TrySubtractResources": costs => false }); +AddMock(4, IID_StatisticsTracker, { + "IncreaseSuccessfulBribesCounter": () => {}, + "IncreaseFailedBribesCounter": () => {} +}); cmpVisionSharing.AddSpy(4, 25); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1, 2, 5]); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.spies], [[5, 2], [17, 5]]); TS_ASSERT_EQUALS(cmpVisionSharing.spyId, 20); AddMock(14, IID_Player, { "GetSpyCostMultiplier": () => 1, "TrySubtractResources": costs => true }); AddMock(SYSTEM_ENTITY, IID_Timer, { "SetTimeout": (ent, iid, funcname, time, data) => TS_ASSERT_EQUALS(time, 25 * 1000) }); cmpVisionSharing.AddSpy(4, 25); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1, 2, 5, 4]); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.spies], [[5, 2], [17, 5], [21, 4]]); TS_ASSERT_EQUALS(cmpVisionSharing.spyId, 21); cmpVisionSharing.spies = new Map([[5, 2], [17, 5]]); cmpVisionSharing.shared = new Set([1, 2, 5]); cmpVisionSharing.spyId = 20; AddMock(ent, IID_Vision, { "GetRange": () => 48 }); AddMock(SYSTEM_ENTITY, IID_Timer, { "SetTimeout": (ent, iid, funcname, time, data) => TS_ASSERT_EQUALS(time, 15 * 1000 * 60 / 48) }); cmpVisionSharing.AddSpy(4); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.shared], [1, 2, 5, 4]); TS_ASSERT_UNEVAL_EQUALS([...cmpVisionSharing.spies], [[5, 2], [17, 5], [21, 4]]); TS_ASSERT_EQUALS(cmpVisionSharing.spyId, 21); // Test ShareVisionWith cmpVisionSharing.activated = false; cmpVisionSharing.shared = undefined; TS_ASSERT(cmpVisionSharing.ShareVisionWith(1)); TS_ASSERT(!cmpVisionSharing.ShareVisionWith(2)); cmpVisionSharing.activated = true; cmpVisionSharing.shared = new Set([1, 2, 8]); TS_ASSERT(cmpVisionSharing.ShareVisionWith(1)); TS_ASSERT(cmpVisionSharing.ShareVisionWith(2)); TS_ASSERT(!cmpVisionSharing.ShareVisionWith(3)); TS_ASSERT(!cmpVisionSharing.ShareVisionWith(0)); Index: ps/trunk/binaries/data/mods/public/simulation/helpers/Commands.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/helpers/Commands.js (revision 20270) +++ ps/trunk/binaries/data/mods/public/simulation/helpers/Commands.js (revision 20271) @@ -1,1729 +1,1733 @@ // 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; function ProcessCommand(player, cmd) { let data = { "cmpPlayerManager": Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager) }; if (!data.cmpPlayerManager || player < 0) return; data.playerEnt = data.cmpPlayerManager.GetPlayerByID(player); if (data.playerEnt == INVALID_ENTITY) return; data.cmpPlayer = Engine.QueryInterface(data.playerEnt, IID_Player); if (!data.cmpPlayer) return; data.controlAllUnits = data.cmpPlayer.CanControlAllUnits(); if (cmd.entities) data.entities = FilterEntityList(cmd.entities, player, data.controlAllUnits); // Allow focusing the camera on recent commands let commandData = { "type": "playercommand", "players": [player], "cmd": cmd }; // Save the position, since the GUI event is received after the unit died if (cmd.type == "delete-entities") { let cmpPosition = cmd.entities[0] && Engine.QueryInterface(cmd.entities[0], IID_Position); commandData.position = cmpPosition && cmpPosition.IsInWorld() && cmpPosition.GetPosition2D(); } let cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGuiInterface.PushNotification(commandData); // 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.) // Now handle various commands if (g_Commands[cmd.type]) { var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); cmpTrigger.CallEvent("PlayerCommand", { "player": player, "cmd": cmd }); g_Commands[cmd.type](player, cmd, data); } else error("Invalid command: unknown command type: "+uneval(cmd)); } var g_Commands = { "debug-print": function(player, cmd, data) { print(cmd.message); }, "chat": function(player, cmd, data) { var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGuiInterface.PushNotification({ "type": cmd.type, "players": [player], "message": cmd.message }); }, "aichat": function(player, cmd, data) { var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); var notification = { "players": [player] }; for (var key in cmd) notification[key] = cmd[key]; cmpGuiInterface.PushNotification(notification); }, "cheat": function(player, cmd, data) { Cheat(cmd); }, "diplomacy": function(player, cmd, data) { let cmpCeasefireManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_CeasefireManager); if (data.cmpPlayer.GetLockTeams() || cmpCeasefireManager && cmpCeasefireManager.IsCeasefireActive()) return; switch(cmd.to) { case "ally": data.cmpPlayer.SetAlly(cmd.player); break; case "neutral": data.cmpPlayer.SetNeutral(cmd.player); break; case "enemy": data.cmpPlayer.SetEnemy(cmd.player); break; default: warn("Invalid command: Could not set "+player+" diplomacy status of player "+cmd.player+" to "+cmd.to); } var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGuiInterface.PushNotification({ "type": "diplomacy", "players": [player], "targetPlayer": cmd.player, "status": cmd.to }); }, "tribute": function(player, cmd, data) { data.cmpPlayer.TributeResource(cmd.player, cmd.amounts); }, "control-all": function(player, cmd, data) { if (!data.cmpPlayer.GetCheatsEnabled()) return; var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGuiInterface.PushNotification({ "type": "aichat", "players": [player], "message": markForTranslation("(Cheat - control all units)") }); data.cmpPlayer.SetControlAllUnits(cmd.flag); }, "reveal-map": function(player, cmd, data) { if (!data.cmpPlayer.GetCheatsEnabled()) return; var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGuiInterface.PushNotification({ "type": "aichat", "players": [player], "message": markForTranslation("(Cheat - reveal map)") }); // Reveal the map for all players, not just the current player, // primarily to make it obvious to everyone that the player is cheating var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); cmpRangeManager.SetLosRevealAll(-1, cmd.enable); }, "walk": function(player, cmd, data) { GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.Walk(cmd.x, cmd.z, cmd.queued); }); }, "walk-to-range": function(player, cmd, data) { // Only used by the AI for (let ent of data.entities) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); if (cmpUnitAI) cmpUnitAI.WalkToPointRange(cmd.x, cmd.z, cmd.min, cmd.max, cmd.queued); } }, "attack-walk": function(player, cmd, data) { GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.WalkAndFight(cmd.x, cmd.z, cmd.targetClasses, cmd.queued); }); }, "attack": function(player, cmd, data) { let allowCapture = cmd.allowCapture || cmd.allowCapture == null; if (g_DebugCommands && !allowCapture && !(IsOwnedByEnemyOfPlayer(player, cmd.target) || IsOwnedByNeutralOfPlayer(player, cmd.target))) warn("Invalid command: attack target is not owned by enemy of player "+player+": "+uneval(cmd)); GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.Attack(cmd.target, cmd.queued, allowCapture); }); }, "patrol": function(player, cmd, data) { GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => cmpUnitAI.Patrol(cmd.x, cmd.z, cmd.targetClasses, cmd.queued) ); }, "heal": function(player, cmd, data) { if (g_DebugCommands && !(IsOwnedByPlayer(player, cmd.target) || IsOwnedByAllyOfPlayer(player, cmd.target))) warn("Invalid command: heal target is not owned by player "+player+" or their ally: "+uneval(cmd)); GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.Heal(cmd.target, cmd.queued); }); }, "repair": function(player, cmd, data) { // This covers both repairing damaged buildings, and constructing unfinished foundations if (g_DebugCommands && !IsOwnedByAllyOfPlayer(player, cmd.target)) warn("Invalid command: repair target is not owned by ally of player "+player+": "+uneval(cmd)); GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.Repair(cmd.target, cmd.autocontinue, cmd.queued); }); }, "gather": function(player, cmd, data) { if (g_DebugCommands && !(IsOwnedByPlayer(player, cmd.target) || IsOwnedByGaia(cmd.target))) warn("Invalid command: resource is not owned by gaia or player "+player+": "+uneval(cmd)); GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.Gather(cmd.target, cmd.queued); }); }, "gather-near-position": function(player, cmd, data) { GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.GatherNearPosition(cmd.x, cmd.z, cmd.resourceType, cmd.resourceTemplate, cmd.queued); }); }, "returnresource": function(player, cmd, data) { if (g_DebugCommands && !IsOwnedByPlayer(player, cmd.target)) warn("Invalid command: dropsite is not owned by player "+player+": "+uneval(cmd)); GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.ReturnResource(cmd.target, cmd.queued); }); }, "back-to-work": function(player, cmd, data) { for (let ent of data.entities) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); if(!cmpUnitAI || !cmpUnitAI.BackToWork()) notifyBackToWorkFailure(player); } }, "remove-guard": function(player, cmd, data) { for (let ent of data.entities) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); if (cmpUnitAI) cmpUnitAI.RemoveGuard(); } }, "train": function(player, cmd, data) { if (!Number.isInteger(cmd.count) || cmd.count <= 0) { warn("Invalid command: can't train " + uneval(cmd.count) + " units"); return; } // Check entity limits var template = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).GetTemplate(cmd.template); var unitCategory = null; if (template.TrainingRestrictions) unitCategory = template.TrainingRestrictions.Category; // Verify that the building(s) can be controlled by the player if (data.entities.length <= 0) { if (g_DebugCommands) warn("Invalid command: training building(s) cannot be controlled by player "+player+": "+uneval(cmd)); return; } for (let ent of data.entities) { if (unitCategory) { var cmpPlayerEntityLimits = QueryOwnerInterface(ent, IID_EntityLimits); if (!cmpPlayerEntityLimits.AllowedToTrain(unitCategory, cmd.count)) { if (g_DebugCommands) warn(unitCategory + " train limit is reached: " + uneval(cmd)); continue; } } var cmpTechnologyManager = QueryOwnerInterface(ent, IID_TechnologyManager); if (!cmpTechnologyManager.CanProduce(cmd.template)) { if (g_DebugCommands) warn("Invalid command: training requires unresearched technology: " + uneval(cmd)); continue; } var queue = Engine.QueryInterface(ent, IID_ProductionQueue); // Check if the building can train the unit // 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. if (queue && data.cmpPlayer.IsAI()) { var list = queue.GetEntitiesList(); if (list.indexOf(cmd.template) === -1 && cmd.promoted) { for (var promoted of cmd.promoted) { if (list.indexOf(promoted) === -1) continue; cmd.template = promoted; break; } } } if (queue && queue.GetEntitiesList().indexOf(cmd.template) != -1) if ("metadata" in cmd) queue.AddBatch(cmd.template, "unit", +cmd.count, cmd.metadata); else queue.AddBatch(cmd.template, "unit", +cmd.count); } }, "research": function(player, cmd, data) { if (!CanControlUnit(cmd.entity, player, data.controlAllUnits)) { if (g_DebugCommands) warn("Invalid command: research building cannot be controlled by player "+player+": "+uneval(cmd)); return; } var cmpTechnologyManager = QueryOwnerInterface(cmd.entity, IID_TechnologyManager); if (!cmpTechnologyManager.CanResearch(cmd.template)) { if (g_DebugCommands) warn("Invalid command: Requirements to research technology are not met: " + uneval(cmd)); return; } var queue = Engine.QueryInterface(cmd.entity, IID_ProductionQueue); if (queue) queue.AddBatch(cmd.template, "technology"); }, "stop-production": function(player, cmd, data) { if (!CanControlUnit(cmd.entity, player, data.controlAllUnits)) { if (g_DebugCommands) warn("Invalid command: production building cannot be controlled by player "+player+": "+uneval(cmd)); return; } var queue = Engine.QueryInterface(cmd.entity, IID_ProductionQueue); if (queue) queue.RemoveBatch(cmd.id); }, "construct": function(player, cmd, data) { TryConstructBuilding(player, data.cmpPlayer, data.controlAllUnits, cmd); }, "construct-wall": function(player, cmd, data) { TryConstructWall(player, data.cmpPlayer, data.controlAllUnits, cmd); }, "delete-entities": function(player, cmd, data) { for (let ent of data.entities) { let cmpHealth = QueryMiragedInterface(ent, IID_Health); if (!data.controlAllUnits) { if (cmpHealth && cmpHealth.IsUndeletable()) continue; let cmpCapturable = QueryMiragedInterface(ent, IID_Capturable); if (cmpCapturable && cmpCapturable.GetCapturePoints()[player] < cmpCapturable.GetMaxCapturePoints() / 2) continue; let cmpResourceSupply = QueryMiragedInterface(ent, IID_ResourceSupply); if (cmpResourceSupply && cmpResourceSupply.GetKillBeforeGather()) continue; } let cmpMirage = Engine.QueryInterface(ent, IID_Mirage); if (cmpMirage) { let cmpMiragedHealth = Engine.QueryInterface(cmpMirage.parent, IID_Health); if (cmpMiragedHealth) cmpMiragedHealth.Kill(); else Engine.DestroyEntity(cmpMirage.parent); Engine.DestroyEntity(ent); } else if (cmpHealth) cmpHealth.Kill(); else Engine.DestroyEntity(ent); } }, "set-rallypoint": function(player, cmd, data) { for (let ent of data.entities) { var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint); if (cmpRallyPoint) { if (!cmd.queued) cmpRallyPoint.Unset(); cmpRallyPoint.AddPosition(cmd.x, cmd.z); cmpRallyPoint.AddData(clone(cmd.data)); } } }, "unset-rallypoint": function(player, cmd, data) { for (let ent of data.entities) { var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint); if (cmpRallyPoint) cmpRallyPoint.Reset(); } }, "resign": function(player, cmd, data) { let cmpPlayer = QueryPlayerIDInterface(player); if (cmpPlayer) cmpPlayer.SetState("defeated", markForTranslation("%(player)s has resigned.")); }, "garrison": function(player, cmd, data) { // Verify that the building can be controlled by the player or is mutualAlly if (!CanControlUnitOrIsAlly(cmd.target, player, data.controlAllUnits)) { if (g_DebugCommands) warn("Invalid command: garrison target cannot be controlled by player "+player+" (or ally): "+uneval(cmd)); return; } GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.Garrison(cmd.target, cmd.queued); }); }, "guard": function(player, cmd, data) { // Verify that the target can be controlled by the player or is mutualAlly if (!CanControlUnitOrIsAlly(cmd.target, player, data.controlAllUnits)) { if (g_DebugCommands) warn("Invalid command: guard/escort target cannot be controlled by player "+player+": "+uneval(cmd)); return; } GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.Guard(cmd.target, cmd.queued); }); }, "stop": function(player, cmd, data) { GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.Stop(cmd.queued); }); }, "unload": function(player, cmd, data) { // Verify that the building can be controlled by the player or is mutualAlly if (!CanControlUnitOrIsAlly(cmd.garrisonHolder, player, data.controlAllUnits)) { if (g_DebugCommands) warn("Invalid command: unload target cannot be controlled by player "+player+" (or ally): "+uneval(cmd)); return; } var cmpGarrisonHolder = Engine.QueryInterface(cmd.garrisonHolder, IID_GarrisonHolder); var notUngarrisoned = 0; // The owner can ungarrison every garrisoned unit if (IsOwnedByPlayer(player, cmd.garrisonHolder)) data.entities = cmd.entities; for (let ent of data.entities) if (!cmpGarrisonHolder || !cmpGarrisonHolder.Unload(ent)) ++notUngarrisoned; if (notUngarrisoned != 0) notifyUnloadFailure(player, cmd.garrisonHolder); }, "unload-template": function(player, cmd, data) { var entities = FilterEntityListWithAllies(cmd.garrisonHolders, player, data.controlAllUnits); for (let garrisonHolder of entities) { var cmpGarrisonHolder = Engine.QueryInterface(garrisonHolder, IID_GarrisonHolder); if (cmpGarrisonHolder) { // Only the owner of the garrisonHolder may unload entities from any owners if (!IsOwnedByPlayer(player, garrisonHolder) && !data.controlAllUnits && player != +cmd.owner) continue; if (!cmpGarrisonHolder.UnloadTemplate(cmd.template, cmd.owner, cmd.all)) notifyUnloadFailure(player, garrisonHolder); } } }, "unload-all-by-owner": function(player, cmd, data) { var entities = FilterEntityListWithAllies(cmd.garrisonHolders, player, data.controlAllUnits); for (let garrisonHolder of entities) { var cmpGarrisonHolder = Engine.QueryInterface(garrisonHolder, IID_GarrisonHolder); if (!cmpGarrisonHolder || !cmpGarrisonHolder.UnloadAllByOwner(player)) notifyUnloadFailure(player, garrisonHolder); } }, "unload-all": function(player, cmd, data) { var entities = FilterEntityList(cmd.garrisonHolders, player, data.controlAllUnits); for (let garrisonHolder of entities) { var cmpGarrisonHolder = Engine.QueryInterface(garrisonHolder, IID_GarrisonHolder); if (!cmpGarrisonHolder || !cmpGarrisonHolder.UnloadAll()) notifyUnloadFailure(player, garrisonHolder); } }, "increase-alert-level": function(player, cmd, data) { for (let ent of data.entities) { var cmpAlertRaiser = Engine.QueryInterface(ent, IID_AlertRaiser); if (!cmpAlertRaiser || !cmpAlertRaiser.IncreaseAlertLevel()) notifyAlertFailure(player); } }, "alert-end": function(player, cmd, data) { for (let ent of data.entities) { var cmpAlertRaiser = Engine.QueryInterface(ent, IID_AlertRaiser); if (cmpAlertRaiser) cmpAlertRaiser.EndOfAlert(); } }, "formation": function(player, cmd, data) { GetFormationUnitAIs(data.entities, player, cmd.name).forEach(cmpUnitAI => { cmpUnitAI.MoveIntoFormation(cmd); }); }, "promote": function(player, cmd, data) { if (!data.cmpPlayer.GetCheatsEnabled()) return; var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGuiInterface.PushNotification({ "type": "aichat", "players": [player], "message": markForTranslation("(Cheat - promoted units)"), "translateMessage": true }); for (let ent of cmd.entities) { var cmpPromotion = Engine.QueryInterface(ent, IID_Promotion); if (cmpPromotion) cmpPromotion.IncreaseXp(cmpPromotion.GetRequiredXp() - cmpPromotion.GetCurrentXp()); } }, "stance": function(player, cmd, data) { for (let ent of data.entities) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); if (cmpUnitAI && !cmpUnitAI.IsTurret()) cmpUnitAI.SwitchToStance(cmd.name); } }, "lock-gate": function(player, cmd, data) { for (let ent of data.entities) { var cmpGate = Engine.QueryInterface(ent, IID_Gate); if (!cmpGate) continue; if (cmd.lock) cmpGate.LockGate(); else cmpGate.UnlockGate(); } }, "setup-trade-route": function(player, cmd, data) { GetFormationUnitAIs(data.entities, player).forEach(cmpUnitAI => { cmpUnitAI.SetupTradeRoute(cmd.target, cmd.source, cmd.route, cmd.queued); }); }, "set-trading-goods": function(player, cmd, data) { data.cmpPlayer.SetTradingGoods(cmd.tradingGoods); }, "barter": function(player, cmd, data) { var cmpBarter = Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter); cmpBarter.ExchangeResources(data.playerEnt, cmd.sell, cmd.buy, cmd.amount); }, "set-shading-color": function(player, cmd, data) { // Prevent multiplayer abuse if (!data.cmpPlayer.IsAI()) return; // Debug command to make an entity brightly colored for (let ent of cmd.entities) { var cmpVisual = Engine.QueryInterface(ent, IID_Visual); if (cmpVisual) cmpVisual.SetShadingColor(cmd.rgb[0], cmd.rgb[1], cmd.rgb[2], 0); // alpha isn't used so just send 0 } }, "pack": function(player, cmd, data) { for (let ent of data.entities) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); if (!cmpUnitAI) continue; if (cmd.pack) cmpUnitAI.Pack(cmd.queued); else cmpUnitAI.Unpack(cmd.queued); } }, "cancel-pack": function(player, cmd, data) { for (let ent of data.entities) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); if (!cmpUnitAI) continue; if (cmd.pack) cmpUnitAI.CancelPack(cmd.queued); else cmpUnitAI.CancelUnpack(cmd.queued); } }, "upgrade": function(player, cmd, data) { for (let ent of data.entities) { var cmpUpgrade = Engine.QueryInterface(ent, IID_Upgrade); if (!cmpUpgrade || !cmpUpgrade.CanUpgradeTo(cmd.template)) continue; if (cmpUpgrade.WillCheckPlacementRestrictions(cmd.template) && ObstructionsBlockingTemplateChange(ent, cmd.template)) { var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGUIInterface.PushNotification({ "players": [data.cmpPlayer.GetPlayerID()], "message": markForTranslation("Cannot upgrade as distance requirements are not verified or terrain is obstructed.") }); continue; } if (!CanGarrisonedChangeTemplate(ent, cmd.template)) { var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGUIInterface.PushNotification({ "players": [data.cmpPlayer.GetPlayerID()], "message": markForTranslation("Cannot upgrade a garrisoned entity.") }); continue; } // Check entity limits var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); var template = cmpTemplateManager.GetTemplate(cmd.template); var cmpEntityLimits = QueryPlayerIDInterface(player, IID_EntityLimits); if (template.TrainingRestrictions && !cmpEntityLimits.AllowedToTrain(template.TrainingRestrictions.Category, 1) || template.BuildRestrictions && !cmpEntityLimits.AllowedToBuild(template.BuildRestrictions.Category)) { if (g_DebugCommands) warn("Invalid command: build limits check failed for player " + player + ": " + uneval(cmd)); continue; } var cmpTechnologyManager = QueryOwnerInterface(ent, IID_TechnologyManager); if (cmpUpgrade.GetRequiredTechnology(cmd.template) && !cmpTechnologyManager.IsTechnologyResearched(cmpUpgrade.GetRequiredTechnology(cmd.template))) { if (g_DebugCommands) warn("Invalid command: upgrading requires unresearched technology: " + uneval(cmd)); continue; } cmpUpgrade.Upgrade(cmd.template, data.cmpPlayer); } }, "cancel-upgrade": function(player, cmd, data) { for (let ent of data.entities) { let cmpUpgrade = Engine.QueryInterface(ent, IID_Upgrade); if (cmpUpgrade) cmpUpgrade.CancelUpgrade(data.cmpPlayer.playerID); } }, "attack-request": function(player, cmd, data) { // Send a chat message to human players var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGuiInterface.PushNotification({ "type": "aichat", "players": [player], "message": "/allies " + markForTranslation("Attack against %(_player_)s requested."), "translateParameters": ["_player_"], "parameters": { "_player_": cmd.player } }); // And send an attackRequest event to the AIs let cmpAIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_AIInterface); if (cmpAIInterface) cmpAIInterface.PushEvent("AttackRequest", cmd); }, "spy-request": function(player, cmd, data) { let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); let ent = pickRandom(cmpRangeManager.GetEntitiesByPlayer(cmd.player).filter(ent => { let cmpVisionSharing = Engine.QueryInterface(ent, IID_VisionSharing); return cmpVisionSharing && cmpVisionSharing.IsBribable() && !cmpVisionSharing.ShareVisionWith(player); })); let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); if (ent) { Engine.QueryInterface(ent, IID_VisionSharing).AddSpy(cmd.source); cmpGUIInterface.PushNotification({ "type": "spy-response", "players": [player], "entity": ent }); } else { let template = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).GetTemplate("special/spy"); IncurBribeCost(template, player, cmd.player, true); + // update statistics for failed bribes + let cmpBribesStatisticsTracker = QueryPlayerIDInterface(player, IID_StatisticsTracker); + if (cmpBribesStatisticsTracker) + cmpBribesStatisticsTracker.IncreaseFailedBribesCounter(); cmpGUIInterface.PushNotification({ "type": "text", "players": [player], "message": markForTranslation("There are no bribable units"), "translateMessage": true }); } }, "diplomacy-request": function(player, cmd, data) { let cmpAIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_AIInterface); if (cmpAIInterface) cmpAIInterface.PushEvent("DiplomacyRequest", cmd); }, "tribute-request": function(player, cmd, data) { let cmpAIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_AIInterface); if (cmpAIInterface) cmpAIInterface.PushEvent("TributeRequest", cmd); }, "dialog-answer": function(player, cmd, data) { // Currently nothing. Triggers can read it anyway, and send this // message to any component you like. }, "set-dropsite-sharing": function(player, cmd, data) { for (let ent of data.entities) { let cmpResourceDropsite = Engine.QueryInterface(ent, IID_ResourceDropsite); if (cmpResourceDropsite && cmpResourceDropsite.IsSharable()) cmpResourceDropsite.SetSharing(cmd.shared); } }, }; /** * Sends a GUI notification about unit(s) that failed to ungarrison. */ function notifyUnloadFailure(player, garrisonHolder) { var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGUIInterface.PushNotification({ "type": "text", "players": [player], "message": markForTranslation("Unable to ungarrison unit(s)"), "translateMessage": true }); } /** * Sends a GUI notification about worker(s) that failed to go back to work. */ function notifyBackToWorkFailure(player) { var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGUIInterface.PushNotification({ "type": "text", "players": [player], "message": markForTranslation("Some unit(s) can't go back to work"), "translateMessage": true }); } /** * Sends a GUI notification about Alerts that failed to be raised */ function notifyAlertFailure(player) { var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGUIInterface.PushNotification({ "type": "text", "players": [player], "message": "You can't raise the alert to a higher level!", "translateMessage": true }); } /** * Get some information about the formations used by entities. * The entities must have a UnitAI component. */ function ExtractFormations(ents) { var entities = []; // subset of ents that have UnitAI var members = {}; // { formationentity: [ent, ent, ...], ... } for (let ent of ents) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); var fid = cmpUnitAI.GetFormationController(); if (fid != INVALID_ENTITY) { if (!members[fid]) members[fid] = []; members[fid].push(ent); } entities.push(ent); } var ids = [ id for (id in members) ]; return { "entities": entities, "members": members, "ids": ids }; } /** * Tries to find the best angle to put a dock at a given position * Taken from GuiInterface.js */ function GetDockAngle(template, x, z) { var cmpTerrain = Engine.QueryInterface(SYSTEM_ENTITY, IID_Terrain); var cmpWaterManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_WaterManager); if (!cmpTerrain || !cmpWaterManager) return undefined; // Get footprint size var halfSize = 0; if (template.Footprint.Square) halfSize = Math.max(template.Footprint.Square["@depth"], template.Footprint.Square["@width"])/2; else if (template.Footprint.Circle) halfSize = template.Footprint.Circle["@radius"]; /* Find direction of most open water, algorithm: * 1. Pick points in a circle around dock * 2. If point is in water, add to array * 3. Scan array looking for consecutive points * 4. Find longest sequence of consecutive points * 5. If sequence equals all points, no direction can be determined, * expand search outward and try (1) again * 6. Calculate angle using average of sequence */ const numPoints = 16; for (var dist = 0; dist < 4; ++dist) { var waterPoints = []; for (var i = 0; i < numPoints; ++i) { var angle = (i/numPoints)*2*Math.PI; var d = halfSize*(dist+1); var nx = x - d*Math.sin(angle); var nz = z + d*Math.cos(angle); if (cmpTerrain.GetGroundLevel(nx, nz) < cmpWaterManager.GetWaterLevel(nx, nz)) waterPoints.push(i); } var consec = []; var length = waterPoints.length; if (!length) continue; for (var i = 0; i < length; ++i) { var count = 0; for (let j = 0; j < length - 1; ++j) { if ((waterPoints[(i + j) % length] + 1) % numPoints == waterPoints[(i + j + 1) % length]) ++count; else break; } consec[i] = count; } var start = 0; var count = 0; for (var c in consec) { if (consec[c] > count) { start = c; count = consec[c]; } } // If we've found a shoreline, stop searching if (count != numPoints-1) return -((waterPoints[start] + consec[start]/2) % numPoints) / numPoints * 2 * Math.PI; } return undefined; } /** * 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": ..., // "metadata": "...", // AI metadata of the building // "actorSeed": ..., // "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; var foundationTemplate = "foundation|" + cmd.template; // Tentatively create the foundation (we might find later that it's a invalid build command) var ent = Engine.AddEntity(foundationTemplate); if (ent == INVALID_ENTITY) { // Error (e.g. invalid template names) error("Error creating foundation entity for '" + cmd.template + "'"); return false; } // If it's a dock, get the right angle. var template = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).GetTemplate(cmd.template); var angle = cmd.angle; if (template.BuildRestrictions.PlacementType === "shore") { let angleDock = GetDockAngle(template, cmd.x, cmd.z); if (angleDock !== undefined) angle = angleDock; } // Move the foundation to the right place var cmpPosition = Engine.QueryInterface(ent, IID_Position); cmpPosition.JumpTo(cmd.x, cmd.z); cmpPosition.SetYRotation(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); } // Make it owned by the current player var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership); cmpOwnership.SetOwner(player); // Check whether building placement is valid var cmpBuildRestrictions = Engine.QueryInterface(ent, IID_BuildRestrictions); if (cmpBuildRestrictions) { var ret = cmpBuildRestrictions.CheckPlacement(); if (!ret.success) { if (g_DebugCommands) warn("Invalid command: build restrictions check failed with '"+ret.message+"' for player "+player+": "+uneval(cmd)); var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); ret.players = [player]; cmpGuiInterface.PushNotification(ret); // Remove the foundation because the construction was aborted // move it out of world because it's not destroyed immediately. cmpPosition.MoveOutOfWorld(); Engine.DestroyEntity(ent); return false; } } else error("cmpBuildRestrictions not defined"); // Check entity limits var cmpEntityLimits = QueryPlayerIDInterface(player, IID_EntityLimits); if (cmpEntityLimits && !cmpEntityLimits.AllowedToBuild(cmpBuildRestrictions.GetCategory())) { if (g_DebugCommands) warn("Invalid command: build limits check failed for player "+player+": "+uneval(cmd)); // Remove the foundation because the construction was aborted cmpPosition.MoveOutOfWorld(); Engine.DestroyEntity(ent); return false; } var cmpTechnologyManager = QueryPlayerIDInterface(player, IID_TechnologyManager); if (cmpTechnologyManager && !cmpTechnologyManager.CanProduce(cmd.template)) { 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({ "type": "text", "players": [player], "message": markForTranslation("The building's technology requirements are not met."), "translateMessage": true }); // Remove the foundation because the construction was aborted cmpPosition.MoveOutOfWorld(); Engine.DestroyEntity(ent); } // We need the cost after tech and aura modifications // To calculate this with an entity requires ownership, so use the template instead let cmpCost = Engine.QueryInterface(ent, IID_Cost); let costs = cmpCost.GetResourceCosts(player); if (!cmpPlayer.TrySubtractResources(costs)) { if (g_DebugCommands) warn("Invalid command: building cost check failed for player "+player+": "+uneval(cmd)); Engine.DestroyEntity(ent); cmpPosition.MoveOutOfWorld(); return false; } var cmpVisual = Engine.QueryInterface(ent, IID_Visual); if (cmpVisual && cmd.actorSeed !== undefined) cmpVisual.SetActorSeed(cmd.actorSeed); // Initialise the foundation var cmpFoundation = Engine.QueryInterface(ent, IID_Foundation); cmpFoundation.InitialiseConstruction(player, cmd.template); // send Metadata info if any if (cmd.metadata) Engine.PostMessage(ent, MT_AIMetadata, { "id": ent, "metadata" : cmd.metadata, "owner" : player } ); // 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; var queued = cmd.queued; var pieces = clone(cmd.pieces); for (; i < pieces.length; ++i) { var piece = pieces[i]; // All wall pieces after the first must be queued. if (i > 0 && !queued) queued = true; // '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": 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 == 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(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 break; } var lastBuiltPieceIndex = i - 1; var wallComplete = (lastBuiltPieceIndex == 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 = 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; } } } } /** * 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]); } } /** * Returns a list of UnitAI components, each belonging either to a * selected unit or to a formation entity for groups of the selected units. */ function GetFormationUnitAIs(ents, player, formationTemplate) { // If an individual was selected, remove it from any formation // and command it individually if (ents.length == 1) { // Skip unit if it has no UnitAI var cmpUnitAI = Engine.QueryInterface(ents[0], IID_UnitAI); if (!cmpUnitAI) return []; RemoveFromFormation(ents); cmpUnitAI.SetLastFormationTemplate("special/formations/null"); return [ cmpUnitAI ]; } // Separate out the units that don't support the chosen formation var formedEnts = []; var nonformedUnitAIs = []; for (let ent of ents) { // Skip units with no UnitAI or no position var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); var cmpPosition = Engine.QueryInterface(ent, IID_Position); if (!cmpUnitAI || !cmpPosition || !cmpPosition.IsInWorld()) continue; var cmpIdentity = Engine.QueryInterface(ent, IID_Identity); // 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. var nullFormation = (formationTemplate || cmpUnitAI.GetLastFormationTemplate()) == "special/formations/null"; if (!nullFormation && cmpIdentity && cmpIdentity.CanUseFormation(formationTemplate || "special/formations/null")) formedEnts.push(ent); else { if (nullFormation) { RemoveFromFormation([ent]); cmpUnitAI.SetLastFormationTemplate("special/formations/null"); } nonformedUnitAIs.push(cmpUnitAI); } } if (formedEnts.length == 0) { // No units support the foundation - return all the others return nonformedUnitAIs; } // Find what formations the formationable selected entities are currently in var formation = ExtractFormations(formedEnts); var formationUnitAIs = []; if (formation.ids.length == 1) { // Selected units either belong to this formation or have no formation // Check that all its members are selected var fid = formation.ids[0]; var cmpFormation = Engine.QueryInterface(+fid, IID_Formation); if (cmpFormation && cmpFormation.GetMemberCount() == formation.members[fid].length && cmpFormation.GetMemberCount() == formation.entities.length) { cmpFormation.DeleteTwinFormations(); // The whole formation was selected, so reuse its controller for this command formationUnitAIs = [Engine.QueryInterface(+fid, IID_UnitAI)]; if (formationTemplate && CanMoveEntsIntoFormation(formation.entities, formationTemplate)) cmpFormation.LoadFormation(formationTemplate); } } if (!formationUnitAIs.length) { // 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]); } // TODO replace the fixed 60 with something sensible, based on vision range f.e. var formationSeparation = 60; var clusters = ClusterEntities(formation.entities, formationSeparation); var formationEnts = []; for (let cluster of clusters) { if (!formationTemplate || !CanMoveEntsIntoFormation(cluster, formationTemplate)) { // get the most recently used formation, or default to line closed var lastFormationTemplate = undefined; for (let ent of cluster) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); if (cmpUnitAI) { var template = cmpUnitAI.GetLastFormationTemplate(); if (lastFormationTemplate === undefined) { lastFormationTemplate = template; } else if (lastFormationTemplate != template) { lastFormationTemplate = undefined; break; } } } if (lastFormationTemplate && CanMoveEntsIntoFormation(cluster, lastFormationTemplate)) formationTemplate = lastFormationTemplate; else formationTemplate = "special/formations/null"; } // Create the new controller var formationEnt = Engine.AddEntity(formationTemplate); var cmpFormation = Engine.QueryInterface(formationEnt, IID_Formation); formationUnitAIs.push(Engine.QueryInterface(formationEnt, IID_UnitAI)); cmpFormation.SetFormationSeparation(formationSeparation); cmpFormation.SetMembers(cluster); for (let ent of formationEnts) cmpFormation.RegisterTwinFormation(ent); formationEnts.push(formationEnt); var cmpOwnership = Engine.QueryInterface(formationEnt, IID_Ownership); cmpOwnership.SetOwner(player); } } return nonformedUnitAIs.concat(formationUnitAIs); } /** * Group a list of entities in clusters via single-links */ function ClusterEntities(ents, separationDistance) { var clusters = []; if (!ents.length) return clusters; var distSq = separationDistance * separationDistance; var positions = []; // triangular matrix with the (squared) distances between the different clusters // the other half is not initialised var matrix = []; for (let i = 0; i < ents.length; ++i) { matrix[i] = []; clusters.push([ents[i]]); var cmpPosition = Engine.QueryInterface(ents[i], IID_Position); positions.push(cmpPosition.GetPosition2D()); for (let j = 0; j < i; ++j) matrix[i][j] = positions[i].distanceToSquared(positions[j]); } while (clusters.length > 1) { // search two clusters that are closer than the required distance var closeClusters = undefined; for (var i = matrix.length - 1; i >= 0 && !closeClusters; --i) for (var j = i - 1; j >= 0 && !closeClusters; --j) if (matrix[i][j] < distSq) closeClusters = [i,j]; // if no more close clusters found, just return all found clusters so far if (!closeClusters) return clusters; // make a new cluster with the entities from the two found clusters var newCluster = clusters[closeClusters[0]].concat(clusters[closeClusters[1]]); // calculate the minimum distance between the new cluster and all other remaining // clusters by taking the minimum of the two distances. var distances = []; for (let i = 0; i < clusters.length; ++i) { if (i == closeClusters[1] || i == closeClusters[0]) continue; var dist1 = matrix[closeClusters[1]][i] || matrix[i][closeClusters[1]]; var dist2 = matrix[closeClusters[0]][i] || matrix[i][closeClusters[0]]; distances.push(Math.min(dist1, dist2)); } // remove the rows and columns in the matrix for the merged clusters, // and the clusters themselves from the cluster list clusters.splice(closeClusters[0],1); clusters.splice(closeClusters[1],1); matrix.splice(closeClusters[0],1); matrix.splice(closeClusters[1],1); for (let i = 0; i < matrix.length; ++i) { if (matrix[i].length > closeClusters[0]) matrix[i].splice(closeClusters[0],1); if (matrix[i].length > closeClusters[1]) matrix[i].splice(closeClusters[1],1); } // add a new row of distances to the matrix and the new cluster clusters.push(newCluster); matrix.push(distances); } return clusters; } function GetFormationRequirements(formationTemplate) { var template = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).GetTemplate(formationTemplate); if (!template.Formation) return false; return { "minCount": +template.Formation.RequiredMemberCount }; } function CanMoveEntsIntoFormation(ents, formationTemplate) { // TODO: should check the player's civ is allowed to use this formation // See simulation/components/Player.js GetFormations() for a list of all allowed formations var requirements = GetFormationRequirements(formationTemplate); if (!requirements) return false; var count = 0; for (let ent of ents) { var cmpIdentity = Engine.QueryInterface(ent, IID_Identity); if (!cmpIdentity || !cmpIdentity.CanUseFormation(formationTemplate)) continue; ++count; } return count >= requirements.minCount; } /** * Check if player can control this entity * returns: true if the entity is valid and owned by the player * or control all units is activated, else false */ function CanControlUnit(entity, player, controlAll) { return IsOwnedByPlayer(player, entity) || controlAll; } /** * Check if player can control this entity * returns: true if the entity is valid and owned by the player * or the entity is owned by an mutualAlly * or control all units is activated, else false */ function CanControlUnitOrIsAlly(entity, player, controlAll) { return IsOwnedByPlayer(player, entity) || IsOwnedByMutualAllyOfPlayer(player, entity) || controlAll; } /** * Filter entities which the player can control */ function FilterEntityList(entities, player, controlAll) { return entities.filter(ent => CanControlUnit(ent, player, controlAll)); } /** * Filter entities which the player can control or are mutualAlly */ function FilterEntityListWithAllies(entities, player, controlAll) { return entities.filter(ent => CanControlUnitOrIsAlly(ent, player, controlAll)); } /** * Incur the player with the cost of a bribe, optionally multiply the cost with * the additionalMultiplier */ function IncurBribeCost(template, player, playerBribed, failedBribe) { let cmpPlayerBribed = QueryPlayerIDInterface(playerBribed); if (!cmpPlayerBribed) return false; let costs = {}; // Additional cost for this owner let multiplier = cmpPlayerBribed.GetSpyCostMultiplier(); if (failedBribe) multiplier *= template.VisionSharing.FailureCostRatio; for (let res in template.Cost.Resources) costs[res] = Math.floor(multiplier * ApplyValueModificationsToTemplate("Cost/Resources/" + res, +template.Cost.Resources[res], player, template)); let cmpPlayer = QueryPlayerIDInterface(player); return cmpPlayer && cmpPlayer.TrySubtractResources(costs); } Engine.RegisterGlobal("GetFormationRequirements", GetFormationRequirements); Engine.RegisterGlobal("CanMoveEntsIntoFormation", CanMoveEntsIntoFormation); Engine.RegisterGlobal("GetDockAngle", GetDockAngle); Engine.RegisterGlobal("ProcessCommand", ProcessCommand); Engine.RegisterGlobal("g_Commands", g_Commands); Engine.RegisterGlobal("IncurBribeCost", IncurBribeCost);