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 2987d2904e..d372eea2d8 100644 --- a/binaries/data/mods/public/simulation/ai/common-api/gamestate.js +++ b/binaries/data/mods/public/simulation/ai/common-api/gamestate.js @@ -454,13 +454,16 @@ m.GameState.prototype.countEntitiesByType = function(type, maintain) { }; m.GameState.prototype.countEntitiesAndQueuedByType = function(type, maintain) { + var template = this.getTemplate(type); + if (!template) + return 0; + var count = this.countEntitiesByType(type, maintain); // Count building foundations - var template = this.getTemplate(type); - if (template && template.hasClass("Structure") === true) + if (template.hasClass("Structure") === true) count += this.countFoundationsByType(type, true); - else if (template && template.resourceSupplyType() !== undefined) // animal resources + else if (template.resourceSupplyType() !== undefined) // animal resources count += this.countEntitiesByType("resource|" + type, true); else { diff --git a/binaries/data/mods/public/simulation/ai/petra/attackManager.js b/binaries/data/mods/public/simulation/ai/petra/attackManager.js index 19fe2ef5b3..f31ae7dd34 100644 --- a/binaries/data/mods/public/simulation/ai/petra/attackManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/attackManager.js @@ -240,21 +240,15 @@ m.AttackManager.prototype.getPlan = function(planName) { for (var attackType in this.upcomingAttacks) { - for (var i in this.upcomingAttacks[attackType]) - { - var attack = this.upcomingAttacks[attackType][i]; + for (var attack of this.upcomingAttacks[attackType]) if (attack.getName() == planName) return attack; - } } for (var attackType in this.startedAttacks) { - for (var i in this.startedAttacks[attackType]) - { - var attack = this.startedAttacks[attackType][i]; + for (var attack of this.startedAttacks[attackType]) if (attack.getName() == planName) return attack; - } } return undefined; }; @@ -276,23 +270,23 @@ m.AttackManager.prototype.unpausePlan = function(planName) m.AttackManager.prototype.pauseAllPlans = function() { for (var attackType in this.upcomingAttacks) - for (var i in this.upcomingAttacks[attackType]) - this.upcomingAttacks[attackType][i].setPaused(true); + for (var attack of this.upcomingAttacks[attackType]) + attack.setPaused(true); for (var attackType in this.startedAttacks) - for (var i in this.startedAttacks[attackType]) - this.startedAttacks[attackType][i].setPaused(true); + for (var attack of this.startedAttacks[attackType]) + attack.setPaused(true); }; m.AttackManager.prototype.unpauseAllPlans = function() { for (var attackType in this.upcomingAttacks) - for (var i in this.upcomingAttacks[attackType]) - this.upcomingAttacks[attackType][i].setPaused(false); + for (var attack of this.upcomingAttacks[attackType]) + attack.setPaused(false); for (var attackType in this.startedAttacks) - for (var i in this.startedAttacks[attackType]) - this.startedAttacks[attackType][i].setPaused(false); + for (var attack of this.startedAttacks[attackType]) + attack.setPaused(false); }; return m; diff --git a/binaries/data/mods/public/simulation/ai/petra/attackPlan.js b/binaries/data/mods/public/simulation/ai/petra/attackPlan.js index 54d61b8387..5654401138 100644 --- a/binaries/data/mods/public/simulation/ai/petra/attackPlan.js +++ b/binaries/data/mods/public/simulation/ai/petra/attackPlan.js @@ -1426,10 +1426,34 @@ m.AttackPlan.prototype.update = function(gameState, events) this.target = this.getNearestTarget(gameState, this.position, true); if (!this.target) { - if (this.Config.debug > 0) - API3.warn("No new target found. Remaining units " + this.unitCollection.length); - Engine.ProfileStop(); - return false; + // Check if we could help any current attack + var attackManager = gameState.ai.HQ.attackManager; + var accessIndex = gameState.ai.accessibility.getAccessValue(this.targetPos); + for (var attackType in attackManager.startedAttacks) + { + if (this.target) + break; + for (var attack of attackManager.startedAttacks[attackType]) + { + if (attack.name === this.name) + continue; + if (accessIndex !== gameState.ai.accessibility.getAccessValue(attack.targetPos)) + continue; + this.target = attack.target; + this.targetPlayer = attack.targetPlayer; + break; + } + } + + if (!this.target) + { + if (this.Config.debug > 0) + API3.warn("No new target found. Remaining units " + this.unitCollection.length); + Engine.ProfileStop(); + return false; + } + else if (this.Config.debug > 0) + API3.warn("We will help one of our other attacks"); } this.targetPos = this.target.position(); } diff --git a/binaries/data/mods/public/simulation/ai/petra/baseManager.js b/binaries/data/mods/public/simulation/ai/petra/baseManager.js index 3e3270076a..120b2d6295 100644 --- a/binaries/data/mods/public/simulation/ai/petra/baseManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/baseManager.js @@ -109,13 +109,30 @@ m.BaseManager.prototype.checkEvents = function (gameState, events, queues) this.removeDropsite(gameState, ent); if (evt.metadata[PlayerID]["baseAnchor"] && evt.metadata[PlayerID]["baseAnchor"] == true) { - // sounds like we lost our anchor. Let's try rebuilding it. - // TODO: currently the HQ manager sets us as initgathering, we probably ouht to do it + // sounds like we lost our anchor. Let's reaffect our units and buildings this.anchor = undefined; - - this.constructing = true; // let's switch mode. - this.workers.forEach( function (worker) { worker.stopMoving(); }); - queues.civilCentre.addItem(new m.ConstructionPlan(gameState, gameState.ai.HQ.bBase[0], { "base": this.ID, "baseAnchor": true }, ent.position())); + var distmin = Math.min(); + var basemin = undefined; + for each (var base in gameState.ai.HQ.baseManagers) + { + if (!base.anchor) + continue; + var dist = API3.SquareVectorDistance(base.anchor.position(), ent.position()); + if (base.accessIndex !== this.accessIndex) + dist += 100000000; + if (dist > distmin) + continue; + distmin = dist; + basemin = base; + } + if (this.Config.debug > 0) + API3.warn(" base " + this.ID + " detruite"); + if (!basemin) + continue; + if (this.Config.debug > 0) + API3.warn(" >>> on echange units/buildings vers base " + basemin.ID); + this.units.forEach( function (ent) { ent.setMetadata(PlayerID, "base", basemin.ID); }); + this.buildings.forEach( function (ent) { ent.setMetadata(PlayerID, "base", basemin.ID); }); } } @@ -840,6 +857,9 @@ m.BaseManager.prototype.assignToFoundations = function(gameState, noRepair) m.BaseManager.prototype.update = function(gameState, queues, events) { + if (!this.anchor) // this base has been destroyed + return; + if (this.anchor && this.anchor.getMetadata(PlayerID, "access") !== this.accessIndex) API3.warn("Petra baseManager " + this.ID + " problem with accessIndex " + this.accessIndex + " while metadata access is " + this.anchor.getMetadata(PlayerID, "access")); diff --git a/binaries/data/mods/public/simulation/ai/petra/config.js b/binaries/data/mods/public/simulation/ai/petra/config.js index d2f8988662..a4fceaf25f 100644 --- a/binaries/data/mods/public/simulation/ai/petra/config.js +++ b/binaries/data/mods/public/simulation/ai/petra/config.js @@ -102,7 +102,7 @@ m.Config.prototype.updateDifficulty = function(difficulty) { this.difficulty = difficulty; // changing settings based on difficulty. - this.targetNumTraders = 2 * this.difficulty; + this.Economy.targetNumTraders = 2 * this.difficulty; if (this.difficulty === 1) { this.Military.popForBarracks1 = 35; diff --git a/binaries/data/mods/public/simulation/ai/petra/headquarters.js b/binaries/data/mods/public/simulation/ai/petra/headquarters.js index 7e9e616c30..8d2e6466ef 100644 --- a/binaries/data/mods/public/simulation/ai/petra/headquarters.js +++ b/binaries/data/mods/public/simulation/ai/petra/headquarters.js @@ -1334,7 +1334,7 @@ m.HQ.prototype.buildMoreHouses = function(gameState,queues) }; // checks the status of the territory expansion. If no new economic bases created, build some strategic ones. -m.HQ.prototype.checkBaseExpansion = function(gameState,queues) +m.HQ.prototype.checkBaseExpansion = function(gameState, queues) { if (queues.civilCentre.length() > 0) return;