Page MenuHomeWildfire Games
Paste P147

Petra: Fix cases where trade routes can go through enemy territory
ActivePublic

Authored by smiley on Dec 28 2018, 6:32 AM.
Index: binaries/data/mods/public/simulation/ai/petra/entityExtend.js
===================================================================
--- binaries/data/mods/public/simulation/ai/petra/entityExtend.js (revision 21929)
+++ binaries/data/mods/public/simulation/ai/petra/entityExtend.js (working copy)
@@ -374,6 +374,23 @@
return false;
};
+/**
+ * Check if the path between the two positions crosses an enemy territory
+ */
+m.isPathInsideEnemyTerritory = function(gameState, pos1, pos2, passClass, step)
+{
+ let path = Engine.ComputePath(pos1, pos2, gameState.getPassabilityClassMask(passClass));
+
+ for (let i = 0; i < path.length; i += step)
+ {
+ let owner = gameState.ai.HQ.territoryMap.getOwner(path[i]);
+ if (owner && gameState.isPlayerEnemy(owner))
+ return true;
+ }
+
+ return false;
+};
+
m.gatherTreasure = function(gameState, ent, water = false)
{
if (!gameState.ai.HQ.treasures.hasEntities())
Index: binaries/data/mods/public/simulation/ai/petra/headquarters.js
===================================================================
--- binaries/data/mods/public/simulation/ai/petra/headquarters.js (revision 21929)
+++ binaries/data/mods/public/simulation/ai/petra/headquarters.js (working copy)
@@ -1283,6 +1283,7 @@
*/
m.HQ.prototype.findMarketLocation = function(gameState, template)
{
+ Engine.ProfileStart("market-loc");
let markets = gameState.updatingCollection("diplo-ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()).toEntityArray();
if (!markets.length)
markets = gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()).toEntityArray();
@@ -1335,12 +1336,13 @@
{
if (isNavalMarket && market.hasClass("NavalMarket"))
{
- if (m.getSeaAccess(gameState, market) != gameState.ai.accessibility.getAccessValue(pos, true))
+ if (m.getSeaAccess(gameState, market) != gameState.ai.accessibility.getAccessValue(pos, true) &&
+ !m.isPathInsideEnemyTerritory(gameState, market.position(), pos, "ship-terrain-only", 15))
continue;
gainMultiplier = traderTemplatesGains.navalGainMultiplier;
}
else if (m.getLandAccess(gameState, market) == index &&
- !m.isLineInsideEnemyTerritory(gameState, market.position(), pos))
+ !m.isPathInsideEnemyTerritory(gameState, market.position(), pos, "default", 5))
gainMultiplier = traderTemplatesGains.landGainMultiplier;
else
continue;
@@ -1390,6 +1392,7 @@
let x = (bestIdx % obstructions.width + 0.5) * obstructions.cellSize;
let z = (Math.floor(bestIdx / obstructions.width) + 0.5) * obstructions.cellSize;
+ Engine.ProfileStop();
return [x, z, idx, expectedGain];
};
Index: binaries/data/mods/public/simulation/ai/petra/tradeManager.js
===================================================================
--- binaries/data/mods/public/simulation/ai/petra/tradeManager.js (revision 21929)
+++ binaries/data/mods/public/simulation/ai/petra/tradeManager.js (working copy)
@@ -447,8 +447,10 @@
let sea = sea1 && sea1 == sea2 ? sea1 : undefined;
if (!land && !sea)
continue;
- if (land && m.isLineInsideEnemyTerritory(gameState, m1.position(), m2.position()))
+ if (land && m.isPathInsideEnemyTerritory(gameState, m1.position(), m2.position(), "default", 5))
continue;
+ if (sea && m.isPathInsideEnemyTerritory(gameState, m1.position(), m2.position(), "ship-terrain-only", 15))
+ continue;
let gainMultiplier;
if (land && traderTemplatesGains.landGainMultiplier)
gainMultiplier = traderTemplatesGains.landGainMultiplier;

Event Timeline

smiley created this paste.Dec 28 2018, 6:32 AM

I havent checked any performance but there is a chance the changes in headquaters.js could be bad.

smiley changed the visibility from "All Users" to "Public (No Login Required)".Dec 29 2018, 9:50 AM