From e2d893103b49a2daa498eca75adaecc51efe361c Mon Sep 17 00:00:00 2001 From: mimo Date: Wed, 9 Jul 2014 21:41:14 +0000 Subject: [PATCH] make the AI aware of the game type and start interfacing with wonder victory condition + some fixes This was SVN commit r15505. --- .../public/simulation/ai/common-api/baseAI.js | 2 ++ .../simulation/ai/common-api/gamestate.js | 6 +++++ .../public/simulation/ai/common-api/shared.js | 1 + .../public/simulation/ai/petra/baseManager.js | 5 ++-- .../public/simulation/ai/petra/defenseArmy.js | 7 +++++- .../simulation/ai/petra/headquarters.js | 5 +++- .../simulation/ai/petra/navalManager.js | 23 +++++++++++++++++++ .../simulation/ai/petra/transportPlan.js | 12 +++++++--- .../simulation/components/GuiInterface.js | 4 ++++ 9 files changed, 58 insertions(+), 7 deletions(-) diff --git a/binaries/data/mods/public/simulation/ai/common-api/baseAI.js b/binaries/data/mods/public/simulation/ai/common-api/baseAI.js index 8de2b746b4..e1890586de 100644 --- a/binaries/data/mods/public/simulation/ai/common-api/baseAI.js +++ b/binaries/data/mods/public/simulation/ai/common-api/baseAI.js @@ -57,6 +57,8 @@ m.BaseAI.prototype.Init = function(state, playerID, sharedAI) this.circularMap = sharedAI.circularMap; + this.gameType = sharedAI.gameType; + this.barterPrices = sharedAI.barterPrices; this.CustomInit(this.gameState, this.sharedScript); diff --git a/binaries/data/mods/public/simulation/ai/common-api/gamestate.js b/binaries/data/mods/public/simulation/ai/common-api/gamestate.js index 7b91d25104..2d609dca10 100644 --- a/binaries/data/mods/public/simulation/ai/common-api/gamestate.js +++ b/binaries/data/mods/public/simulation/ai/common-api/gamestate.js @@ -27,6 +27,7 @@ m.GameState.prototype.init = function(SharedScript, state, player) { this.playerData = this.sharedScript.playersData[this.player]; this.techModifications = SharedScript._techModifications[this.player]; this.barterPrices = SharedScript.barterPrices; + this.gameType = SharedScript.gameType; }; m.GameState.prototype.update = function(SharedScript, state) { @@ -119,6 +120,11 @@ m.GameState.prototype.getBarterPrices = function() return this.barterPrices; }; +m.GameState.prototype.getGameType = function() +{ + return this.gameType; +}; + m.GameState.prototype.getTemplate = function(type) { if (this.techTemplates[type] !== undefined) diff --git a/binaries/data/mods/public/simulation/ai/common-api/shared.js b/binaries/data/mods/public/simulation/ai/common-api/shared.js index 723279d475..1cb4f0fddd 100644 --- a/binaries/data/mods/public/simulation/ai/common-api/shared.js +++ b/binaries/data/mods/public/simulation/ai/common-api/shared.js @@ -128,6 +128,7 @@ m.SharedScript.prototype.init = function(state) { this.territoryMap = state.territoryMap; this.timeElapsed = state.timeElapsed; this.circularMap = state.circularMap; + this.gameType = state.gameType; this.barterPrices = state.barterPrices; this._entities = {}; diff --git a/binaries/data/mods/public/simulation/ai/petra/baseManager.js b/binaries/data/mods/public/simulation/ai/petra/baseManager.js index 9a1acda95e..2c8ba532a8 100644 --- a/binaries/data/mods/public/simulation/ai/petra/baseManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/baseManager.js @@ -422,7 +422,7 @@ m.BaseManager.prototype.checkResourceLevels = function (gameState, queues) for (var i in queues.field.queue) queues.field.queue[i].isGo = function() { return true; }; // start them } - else if(gameState.ai.HQ.canBuild(gameState, "structures/{civ}_field")) // let's see if we need to add new farms. + else if (gameState.ai.HQ.canBuild(gameState, "structures/{civ}_field")) // let's see if we need to add new farms. { if ((!gameState.ai.HQ.saveResources && numFound < 2 && numFound + numQueue < 3) || (gameState.ai.HQ.saveResources && numFound < 1 && numFound + numQueue < 2)) @@ -843,7 +843,8 @@ m.BaseManager.prototype.assignToFoundations = function(gameState, noRepair) m.BaseManager.prototype.update = function(gameState, queues, events) { if (this.anchor && this.anchor.getMetadata(PlayerID, "access") !== this.accessIndex) - warn(" probleme avec accessIndex " + this.accessIndex + " et metadata " + this.anchor.getMetadata(PlayerID, "access")); + warn("Petra baseManager problem with accessIndex " + this.accessIndex + + " while metadata acess is " + this.anchor.getMetadata(PlayerID, "access")); Engine.ProfileStart("Base update - base " + this.ID); diff --git a/binaries/data/mods/public/simulation/ai/petra/defenseArmy.js b/binaries/data/mods/public/simulation/ai/petra/defenseArmy.js index e8d8748731..a28cc38c20 100644 --- a/binaries/data/mods/public/simulation/ai/petra/defenseArmy.js +++ b/binaries/data/mods/public/simulation/ai/petra/defenseArmy.js @@ -38,13 +38,18 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID) break; } + // already enough units against it + if (this.assignedAgainst[id].length > 8 + || (this.assignedAgainst[id].length > 5 && !eEnt.hasClass("Hero") && !eEnt.hasClass("Siege"))) + continue; + var dist = API3.SquareVectorDistance(ent.position(), eEnt.position()); if (idMinAll === undefined || dist < distMinAll) { idMinAll = id; distMinAll = dist; } - if (this.assignedAgainst[id].length > 2) // already enough units against it + if (this.assignedAgainst[id].length > 2) continue; if (idMin === undefined || dist < distMin) { diff --git a/binaries/data/mods/public/simulation/ai/petra/headquarters.js b/binaries/data/mods/public/simulation/ai/petra/headquarters.js index a61e703089..d1492cad93 100644 --- a/binaries/data/mods/public/simulation/ai/petra/headquarters.js +++ b/binaries/data/mods/public/simulation/ai/petra/headquarters.js @@ -757,7 +757,7 @@ m.HQ.prototype.findEconomicCCLocation = function(gameState, template, resource, if (fromStrategic) // be less restrictive cut = 30; if (this.Config.debug) - warn("on a trouve une base avec best (cut=" + cut + ") = " + bestVal); + warn("we have found a base for " + resource + " with best (cut=" + cut + ") = " + bestVal); // not good enough. if (bestVal < cut) return false; @@ -1758,6 +1758,9 @@ m.HQ.prototype.update = function(gameState, queues, events) else if (gameState.ai.playedTurn - this.lastTerritoryUpdate > 100) this.updateTerritories(gameState); + if (gameState.getGameType() === "wonder") + this.buildWonder(gameState, queues); + if (this.baseManagers[1]) { this.trainMoreWorkers(gameState, queues); diff --git a/binaries/data/mods/public/simulation/ai/petra/navalManager.js b/binaries/data/mods/public/simulation/ai/petra/navalManager.js index ed3dffb31b..c27d391375 100644 --- a/binaries/data/mods/public/simulation/ai/petra/navalManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/navalManager.js @@ -307,6 +307,29 @@ m.NavalManager.prototype.requireTransport = function(gameState, entity, startInd return true; }; +// split a transport plan in two, moving all entities not yet affected to a ship in the new plan +m.NavalManager.prototype.splitTransport = function(gameState, plan) +{ + var newplan = new m.TransportPlan(gameState, [], plan.startIndex, plan.endIndex, plan.endPos, false); + if (newplan.failed) + { + if (this.Config.debug > 0) + warn(">>>> split of transport plan aborted <<<<"); + return false; + } + + var nbUnits = 0; + plan.units.forEach(function (ent) { + if (ent.setMetadata(PlayerID, "onBoard")) + return; + ++nbUnits; + newplan.addUnit(ent, ent.getMetadata(PlayerID, "endPos")); + }); + if (nbUnits) + this.transportPlans.push(newplan); + return (nbUnits !== 0); +}; + // set minimal number of needed ships when a new event (new base or new attack plan) m.NavalManager.prototype.setMinimalTransportShips = function(gameState, sea, number) { diff --git a/binaries/data/mods/public/simulation/ai/petra/transportPlan.js b/binaries/data/mods/public/simulation/ai/petra/transportPlan.js index 790f125a3b..696c452071 100644 --- a/binaries/data/mods/public/simulation/ai/petra/transportPlan.js +++ b/binaries/data/mods/public/simulation/ai/petra/transportPlan.js @@ -27,6 +27,7 @@ m.TransportPlan = function(gameState, units, startIndex, endIndex, endPos) { this.ID = m.playerGlobals[PlayerID].uniqueIDTPlans++; this.debug = gameState.ai.HQ.Config.debug; + this.flotilla = false; // when false, only one ship per transport ... not yet tested when true this.endPos = endPos; this.endIndex = endIndex @@ -84,7 +85,7 @@ m.TransportPlan.prototype.countFreeSlotsOnShip = function(ship) return ship.garrisonMax() - occupied; }; -m.TransportPlan.prototype.assignUnitToShip = function(ent) +m.TransportPlan.prototype.assignUnitToShip = function(gameState, ent) { var self = this; var done = false; @@ -104,8 +105,13 @@ m.TransportPlan.prototype.assignUnitToShip = function(ent) } } }); - if (!done) + if (done) + return; + + if (this.flotilla) this.needTransportShips = true; + else + gameState.ai.HQ.navalManager.splitTransport(gameState, this); }; m.TransportPlan.prototype.assignShip = function(ship) @@ -190,7 +196,7 @@ m.TransportPlan.prototype.onBoarding = function(gameState) if (!ent.getMetadata(PlayerID, "onBoard")) { ready = false; - self.assignUnitToShip(ent); + self.assignUnitToShip(gameState, ent); if (ent.getMetadata(PlayerID, "onBoard")) { var shipId = ent.getMetadata(PlayerID, "onBoard"); diff --git a/binaries/data/mods/public/simulation/components/GuiInterface.js b/binaries/data/mods/public/simulation/components/GuiInterface.js index 0f9bb6c47b..fb23cd2c3a 100644 --- a/binaries/data/mods/public/simulation/components/GuiInterface.js +++ b/binaries/data/mods/public/simulation/components/GuiInterface.js @@ -117,6 +117,10 @@ GuiInterface.prototype.GetSimulationState = function(player) var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); ret.timeElapsed = cmpTimer.GetTime(); + // and the game type + var cmpEndGameManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_EndGameManager); + ret.gameType = cmpEndGameManager.gameType; + return ret; };