Ticket #3858: AITerritory-v2.patch

File AITerritory-v2.patch, 5.5 KB (added by mimo, 8 years ago)

fixed version taking DiplomacyChanged events into account

  • 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    if (events.TerritoriesChanged.length || events.DiplomacyChanged.length)
     122        this.updateTerritories(gameState);
     123
    120124    for (let evt of events.Create)
    121125    {
    122126        // Let's check if we have a building set to create a new base.
     
    178182                base.anchor = ent;
    179183                base.anchorId = evt.newentity;
    180184                base.buildings.updateEnt(ent);
    181                 this.updateTerritories(gameState);
    182185                if (base.ID === this.baseManagers[1].ID)
    183186                {
    184187                    // this is our first base, let us configure our starting resources
     
    191194                    this.saveSpace = undefined;
    192195                }
    193196            }
    194             else if (ent.hasTerritoryInfluence())
    195                 this.updateTerritories(gameState);
    196197        }
    197198    }
    198199
     
    237238                newbase.init(gameState, "captured");
    238239            newbase.setAnchor(gameState, ent);
    239240            this.baseManagers.push(newbase);
    240             this.updateTerritories(gameState);
    241241            newbase.assignEntity(gameState, ent);
    242242        }
    243243        else
     
    244244        {
    245245            // TODO should be reassigned later if a better base is captured
    246246            m.getBestBase(gameState, ent).assignEntity(gameState, ent);
    247             if (ent.hasTerritoryInfluence())
    248                 this.updateTerritories(gameState);
    249247            if (ent.decaying())
    250248            {
    251249                if (ent.isGarrisonHolder() && this.garrisonManager.addDecayingStructure(gameState, evt.entity, true))
     
    18241822
    18251823m.HQ.prototype.updateTerritories = function(gameState)
    18261824{
    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 
    18321825    var passabilityMap = gameState.getMap();
    18331826    var width = this.territoryMap.width;
    18341827    var cellSize = this.territoryMap.cellSize;
     
    20662059            phaseName = gameState.getTemplate(gameState.cityPhase()).name();
    20672060
    20682061        m.chatNewPhase(gameState, phaseName, false);
    2069         this.updateTerritories(gameState);
    20702062    }
    2071     else if (gameState.ai.playedTurn - this.lastTerritoryUpdate > 100)
    2072         this.updateTerritories(gameState);
    20732063
    20742064    if (gameState.getGameType() === "wonder")
    20752065        this.buildWonder(gameState, queues);
     
    21452135        "lastFailedGather": this.lastFailedGather,
    21462136        "supportRatio": this.supportRatio,
    21472137        "targetNumWorkers": this.targetNumWorkers,
    2148         "lastTerritoryUpdate": this.lastTerritoryUpdate,
    21492138        "stopBuilding": this.stopBuilding,
    21502139        "fortStartTime": this.fortStartTime,
    21512140        "towerStartTime": this.towerStartTime,
  • binaries/data/mods/public/simulation/components/AIInterface.js

     
    88    "Destroy",
    99    "Attacked",
    1010    "ConstructionFinished",
     11    "DiplomacyChanged",
    1112    "TrainingStarted",
    1213    "TrainingFinished",
    1314    "AIMetadata",
     
    1617    "OwnershipChanged",
    1718    "Garrison",
    1819    "UnGarrison",
     20    "TerritoriesChanged",
    1921    "TerritoryDecayChanged",
    2022    "TributeExchanged",
    2123    "AttackRequest"
     
    164166    this.events[type].push(msg);
    165167};
    166168
     169AIInterface.prototype.OnDiplomacyChanged = function(msg)
     170{
     171    this.events.DiplomacyChanged.push(msg);
     172};
     173
    167174AIInterface.prototype.OnGlobalPlayerDefeated = function(msg)
    168175{
    169     this.events["PlayerDefeated"].push(msg);
     176    this.events.PlayerDefeated.push(msg);
    170177};
    171178
    172179AIInterface.prototype.OnGlobalEntityRenamed = function(msg)
     
    173180{
    174181    var cmpMirage = Engine.QueryInterface(msg.entity, IID_Mirage);
    175182    if (!cmpMirage)
    176         this.events["EntityRenamed"].push(msg);
     183        this.events.EntityRenamed.push(msg);
    177184};
    178185
    179186AIInterface.prototype.OnGlobalTributeExchanged = function(msg)
    180187{
    181     this.events["TributeExchanged"].push(msg);
     188    this.events.TributeExchanged.push(msg);
    182189};
    183190
     191AIInterface.prototype.OnTerritoriesChanged = function(msg)
     192{
     193    this.events.TerritoriesChanged.push(msg);
     194};
     195
    184196// When a new technology is researched, check which templates it affects,
    185197// and send the updated values to the AI.
    186198// this relies on the fact that any "value" in a technology can only ever change