diff --git a/binaries/data/mods/public/simulation/ai/petra/entityExtend.js b/binaries/data/mods/public/simulation/ai/petra/entityExtend.js index 01eba2ed29..5720564b6b 100644 --- a/binaries/data/mods/public/simulation/ai/petra/entityExtend.js +++ b/binaries/data/mods/public/simulation/ai/petra/entityExtend.js @@ -94,37 +94,63 @@ m.getLandAccess = function(gameState, ent) if (!access) { access = gameState.ai.accessibility.getAccessValue(ent.position()); + // Docks are sometimes not as expected + if (access < 2 && ent.buildPlacementType() == "shore") + { + let entPos = ent.position(); + let cosa = Math.cos(ent.angle()); + let sina = Math.sin(ent.angle()); + for (let d = 3; d < 15; d += 3) + { + let pos = [ entPos[0] - d * sina, + entPos[1] - d * cosa]; + access = gameState.ai.accessibility.getAccessValue(pos); + if (access > 1) + break; + } + } ent.setMetadata(PlayerID, "access", access); } return access; }; -m.getSeaAccess = function(gameState, ent, warning = true) +m.getSeaAccess = function(gameState, ent) { let sea = ent.getMetadata(PlayerID, "sea"); if (!sea) { sea = gameState.ai.accessibility.getAccessValue(ent.position(), true); - if (sea < 2) // pre-positioned docks are sometimes not well positionned + // Docks are sometimes not as expected + if (sea < 2 && ent.buildPlacementType() == "shore") { let entPos = ent.position(); - let radius = ent.footprintRadius(); - for (let i = 0; i < 16; ++i) + let cosa = Math.cos(ent.angle()); + let sina = Math.sin(ent.angle()); + for (let d = 3; d < 15; d += 3) { - let pos = [ entPos[0] + radius*Math.cos(i*Math.PI/8), - entPos[1] + radius*Math.sin(i*Math.PI/8) ]; + let pos = [ entPos[0] + d * sina, + entPos[1] + d * cosa]; sea = gameState.ai.accessibility.getAccessValue(pos, true); - if (sea >= 2) + if (sea > 1) break; } } - if (warning && sea < 2) - API3.warn("ERROR in Petra getSeaAccess because of position with sea index " + sea); ent.setMetadata(PlayerID, "sea", sea); } return sea; }; +m.setAccessIndices = function(gameState, ent) +{ + m.getLandAccess(gameState, ent); + m.getSeaAccess(gameState, ent); +}; + +m.setLandAccess = function(gameState, ent) +{ + m.getLandAccess(gameState, ent); +}; + /** Decide if we should try to capture (returns true) or destroy (return false) */ m.allowCapture = function(gameState, ent, target) { @@ -332,15 +358,15 @@ m.gatherTreasure = function(gameState, ent, water = false) let lastGathered = treasure.getMetadata(PlayerID, "lastGathered"); if (lastGathered && gameState.ai.elapsedTime - lastGathered < 20) continue; - if (!water && access !== m.getLandAccess(gameState, treasure)) + if (!water && access != m.getLandAccess(gameState, treasure)) continue; - if (water && access !== m.getSeaAccess(gameState, treasure, false)) + if (water && access != m.getSeaAccess(gameState, treasure)) continue; let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(treasure.position()); - if (territoryOwner !== 0 && !gameState.isPlayerAlly(territoryOwner)) + if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) continue; let dist = API3.SquareVectorDistance(ent.position(), treasure.position()); - if (dist > 120000 || territoryOwner !== PlayerID && dist > 14000) // AI has no LOS, so restrict it a bit + if (dist > 120000 || territoryOwner != PlayerID && dist > 14000) // AI has no LOS, so restrict it a bit continue; if (dist > distmin) continue; diff --git a/binaries/data/mods/public/simulation/ai/petra/navalManager.js b/binaries/data/mods/public/simulation/ai/petra/navalManager.js index 1317295cfb..0e6ed754e9 100644 --- a/binaries/data/mods/public/simulation/ai/petra/navalManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/navalManager.js @@ -158,7 +158,7 @@ m.NavalManager.prototype.init = function(gameState, deserializing) for (let ship of this.ships.values()) this.setShipIndex(gameState, ship); for (let dock of this.docks.values()) - this.setAccessIndices(gameState, dock); + m.setAccessIndices(gameState, dock); }; m.NavalManager.prototype.updateFishingBoats = function(sea, num) @@ -175,12 +175,6 @@ m.NavalManager.prototype.resetFishingBoats = function(gameState, sea) this.wantedFishShips.fill(0); }; -m.NavalManager.prototype.setAccessIndices = function(gameState, ent) -{ - m.getLandAccess(gameState, ent); - m.getSeaAccess(gameState, ent); -}; - m.NavalManager.prototype.setShipIndex = function(gameState, ship) { let sea = gameState.ai.accessibility.getAccessValue(ship.position(), true); @@ -274,7 +268,7 @@ m.NavalManager.prototype.checkEvents = function(gameState, queues, events) continue; let ent = gameState.getEntityById(evt.entity); if (ent && ent.isOwn(PlayerID) && ent.foundationProgress() !== undefined && (ent.hasClass("Dock") || ent.hasClass("Shipyard"))) - this.setAccessIndices(gameState, ent); + m.setAccessIndices(gameState, ent); } for (let evt of events.TrainingFinished) @@ -341,7 +335,7 @@ m.NavalManager.prototype.checkEvents = function(gameState, queues, events) continue; let ent = gameState.getEntityById(evt.entity); if (ent && (ent.hasClass("Dock") || ent.hasClass("Shipyard"))) - this.setAccessIndices(gameState, ent); + m.setAccessIndices(gameState, ent); } }; @@ -648,6 +642,10 @@ m.NavalManager.prototype.buildNavalStructures = function(gameState, queues) if (!this.docks.filter(API3.Filters.byClass("Dock")).hasEntities() || this.docks.filter(API3.Filters.byClass("Shipyard")).hasEntities()) return; + // Use in priority resources to build a market + if (!gameState.getOwnEntitiesByClass("BarterMarket", true).hasEntities() && + gameState.ai.HQ.canBuild(gameState, "structures/{civ}_market")) + return; let template; if (gameState.ai.HQ.canBuild(gameState, "structures/{civ}_super_dock")) template = "structures/{civ}_super_dock"; diff --git a/binaries/data/mods/public/simulation/ai/petra/startingStrategy.js b/binaries/data/mods/public/simulation/ai/petra/startingStrategy.js index fb630ed98a..a949d40d1d 100644 --- a/binaries/data/mods/public/simulation/ai/petra/startingStrategy.js +++ b/binaries/data/mods/public/simulation/ai/petra/startingStrategy.js @@ -97,7 +97,7 @@ m.HQ.prototype.assignStartingEntities = function(gameState) for (let id of ent.garrisoned()) ent.unload(id); - ent.setMetadata(PlayerID, "access", gameState.ai.accessibility.getAccessValue(pos)); + m.setLandAccess(gameState, ent); let bestbase; let territorypos = this.territoryMap.gamePosToMapPos(pos); let territoryIndex = territorypos[0] + territorypos[1]*this.territoryMap.width;