Ticket #3858: AITerritory.patch

File AITerritory.patch, 5.2 KB (added by mimo, 8 years ago)

let the AI recomputes territories only when receiving TerritoriesChanged events, to cure one of the nonreproducibilities

  • binaries/data/mods/public/simulation/ai/petra/baseManager.js

     
    172172    for (let entity of this.buildings.values())
    173173        bestbase.assignEntity(gameState, entity);
    174174    gameState.ai.HQ.resetActiveBase();
    175     gameState.ai.HQ.updateTerritories(gameState);
    176175};
    177176
    178177/**
  • binaries/data/mods/public/simulation/ai/petra/headquarters.js

     
    3131    this.targetNumWorkers = this.Config.Economy.targetNumWorkers;
    3232    this.supportRatio = this.Config.Economy.supportRatio;
    3333
    34     this.lastTerritoryUpdate = -1;
    3534    this.stopBuilding = new Map(); // list of buildings to stop (temporarily) production because no room
    3635
    3736    this.fortStartTime = 180;   // wooden defense towers, will start at fortStartTime + towerLapseTime
     
    9594        let base = this.getBaseByID(ent.getMetadata(PlayerID, "base"));
    9695        base.assignResourceToDropsite(gameState, ent);
    9796    }
     97
     98    this.updateTerritories(gameState);
    9899};
    99100
    100101// returns the sea index linking regions 1 and region 2 (supposed to be different land region)
     
    117118
    118119m.HQ.prototype.checkEvents = function (gameState, events, queues)
    119120{
     121    for (let evt of events.TerritoriesChanged)
     122    {
     123        this.updateTerritories(gameState);
     124        break;
     125    }
     126
    120127    for (let evt of events.Create)
    121128    {
    122129        // Let's check if we have a building set to create a new base.
     
    178185                base.anchor = ent;
    179186                base.anchorId = evt.newentity;
    180187                base.buildings.updateEnt(ent);
    181                 this.updateTerritories(gameState);
    182188                if (base.ID === this.baseManagers[1].ID)
    183189                {
    184190                    // this is our first base, let us configure our starting resources
     
    191197                    this.saveSpace = undefined;
    192198                }
    193199            }
    194             else if (ent.hasTerritoryInfluence())
    195                 this.updateTerritories(gameState);
    196200        }
    197201    }
    198202
     
    237241                newbase.init(gameState, "captured");
    238242            newbase.setAnchor(gameState, ent);
    239243            this.baseManagers.push(newbase);
    240             this.updateTerritories(gameState);
    241244            newbase.assignEntity(gameState, ent);
    242245        }
    243246        else
     
    244247        {
    245248            // TODO should be reassigned later if a better base is captured
    246249            m.getBestBase(gameState, ent).assignEntity(gameState, ent);
    247             if (ent.hasTerritoryInfluence())
    248                 this.updateTerritories(gameState);
    249250            if (ent.decaying())
    250251            {
    251252                if (ent.isGarrisonHolder() && this.garrisonManager.addDecayingStructure(gameState, evt.entity, true))
     
    18241825
    18251826m.HQ.prototype.updateTerritories = function(gameState)
    18261827{
    1827     // TODO may-be update also when territory decreases. For the moment, only increases are taking into account
    1828     if (this.lastTerritoryUpdate == gameState.ai.playedTurn)
    1829         return;
    1830     this.lastTerritoryUpdate = gameState.ai.playedTurn;
    1831 
    18321828    var passabilityMap = gameState.getMap();
    18331829    var width = this.territoryMap.width;
    18341830    var cellSize = this.territoryMap.cellSize;
     
    20662062            phaseName = gameState.getTemplate(gameState.cityPhase()).name();
    20672063
    20682064        m.chatNewPhase(gameState, phaseName, false);
    2069         this.updateTerritories(gameState);
    20702065    }
    2071     else if (gameState.ai.playedTurn - this.lastTerritoryUpdate > 100)
    2072         this.updateTerritories(gameState);
    20732066
    20742067    if (gameState.getGameType() === "wonder")
    20752068        this.buildWonder(gameState, queues);
     
    21452138        "lastFailedGather": this.lastFailedGather,
    21462139        "supportRatio": this.supportRatio,
    21472140        "targetNumWorkers": this.targetNumWorkers,
    2148         "lastTerritoryUpdate": this.lastTerritoryUpdate,
    21492141        "stopBuilding": this.stopBuilding,
    21502142        "fortStartTime": this.fortStartTime,
    21512143        "towerStartTime": this.towerStartTime,
  • binaries/data/mods/public/simulation/components/AIInterface.js

     
    1616    "OwnershipChanged",
    1717    "Garrison",
    1818    "UnGarrison",
     19    "TerritoriesChanged",
    1920    "TerritoryDecayChanged",
    2021    "TributeExchanged",
    2122    "AttackRequest"
     
    166167
    167168AIInterface.prototype.OnGlobalPlayerDefeated = function(msg)
    168169{
    169     this.events["PlayerDefeated"].push(msg);
     170    this.events.PlayerDefeated.push(msg);
    170171};
    171172
    172173AIInterface.prototype.OnGlobalEntityRenamed = function(msg)
     
    173174{
    174175    var cmpMirage = Engine.QueryInterface(msg.entity, IID_Mirage);
    175176    if (!cmpMirage)
    176         this.events["EntityRenamed"].push(msg);
     177        this.events.EntityRenamed.push(msg);
    177178};
    178179
    179180AIInterface.prototype.OnGlobalTributeExchanged = function(msg)
    180181{
    181     this.events["TributeExchanged"].push(msg);
     182    this.events.TributeExchanged.push(msg);
    182183};
    183184
     185AIInterface.prototype.OnTerritoriesChanged = function(msg)
     186{
     187    this.events.TerritoriesChanged.push(msg);
     188};
     189
    184190// When a new technology is researched, check which templates it affects,
    185191// and send the updated values to the AI.
    186192// this relies on the fact that any "value" in a technology can only ever change