LCOV - code coverage report
Current view: top level - simulation/ai/petra - _petrabot.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 84 0.0 %
Date: 2023-04-02 12:52:40 Functions: 0 5 0.0 %

          Line data    Source code
       1           0 : Engine.IncludeModule("common-api");
       2             : 
       3           0 : var PETRA = {};
       4             : 
       5           0 : PETRA.PetraBot = function(settings)
       6             : {
       7           0 :         API3.BaseAI.call(this, settings);
       8             : 
       9           0 :         this.playedTurn = 0;
      10           0 :         this.elapsedTime = 0;
      11             : 
      12           0 :         this.uniqueIDs = {
      13             :                 "armies": 1,  // starts at 1 to allow easier tests on armies ID existence
      14             :                 "bases": 1,   // base manager ID starts at one because "0" means "no base" on the map
      15             :                 "plans": 0,   // training/building/research plans
      16             :                 "transports": 1       // transport plans start at 1 because 0 might be used as none
      17             :         };
      18             : 
      19           0 :         this.Config = new PETRA.Config(settings.difficulty, settings.behavior);
      20             : 
      21           0 :         this.savedEvents = {};
      22             : };
      23             : 
      24           0 : PETRA.PetraBot.prototype = Object.create(API3.BaseAI.prototype);
      25             : 
      26           0 : PETRA.PetraBot.prototype.CustomInit = function(gameState)
      27             : {
      28           0 :         if (this.isDeserialized)
      29             :         {
      30             :                 // WARNING: the deserializations should not modify the metadatas infos inside their init functions
      31           0 :                 this.turn = this.data.turn;
      32           0 :                 this.playedTurn = this.data.playedTurn;
      33           0 :                 this.elapsedTime = this.data.elapsedTime;
      34           0 :                 this.savedEvents = this.data.savedEvents;
      35           0 :                 for (let key in this.savedEvents)
      36             :                 {
      37           0 :                         for (let i in this.savedEvents[key])
      38             :                         {
      39           0 :                                 if (!this.savedEvents[key][i].entityObj)
      40           0 :                                         continue;
      41           0 :                                 let evt = this.savedEvents[key][i];
      42           0 :                                 let evtmod = {};
      43           0 :                                 for (let keyevt in evt)
      44             :                                 {
      45           0 :                                         evtmod[keyevt] = evt[keyevt];
      46           0 :                                         evtmod.entityObj = new API3.Entity(gameState.sharedScript, evt.entityObj);
      47           0 :                                         this.savedEvents[key][i] = evtmod;
      48             :                                 }
      49             :                         }
      50             :                 }
      51             : 
      52           0 :                 this.Config.Deserialize(this.data.config);
      53             : 
      54           0 :                 this.queueManager = new PETRA.QueueManager(this.Config, {});
      55           0 :                 this.queueManager.Deserialize(gameState, this.data.queueManager);
      56           0 :                 this.queues = this.queueManager.queues;
      57             : 
      58           0 :                 this.HQ = new PETRA.HQ(this.Config);
      59           0 :                 this.HQ.init(gameState, this.queues);
      60           0 :                 this.HQ.Deserialize(gameState, this.data.HQ);
      61             : 
      62           0 :                 this.uniqueIDs = this.data.uniqueIDs;
      63           0 :                 this.isDeserialized = false;
      64           0 :                 this.data = undefined;
      65             : 
      66             :                 // initialisation needed after the completion of the deserialization
      67           0 :                 this.HQ.postinit(gameState);
      68             :         }
      69             :         else
      70             :         {
      71           0 :                 this.Config.setConfig(gameState);
      72             : 
      73             :                 // this.queues can only be modified by the queue manager or things will go awry.
      74           0 :                 this.queues = {};
      75           0 :                 for (let i in this.Config.priorities)
      76           0 :                         this.queues[i] = new PETRA.Queue();
      77             : 
      78           0 :                 this.queueManager = new PETRA.QueueManager(this.Config, this.queues);
      79             : 
      80           0 :                 this.HQ = new PETRA.HQ(this.Config);
      81             : 
      82           0 :                 this.HQ.init(gameState, this.queues);
      83             : 
      84             :                 // Analyze our starting position and set a strategy
      85           0 :                 this.HQ.gameAnalysis(gameState);
      86             :         }
      87             : };
      88             : 
      89           0 : PETRA.PetraBot.prototype.OnUpdate = function(sharedScript)
      90             : {
      91           0 :         if (this.gameFinished)
      92           0 :                 return;
      93             : 
      94           0 :         for (let i in this.events)
      95             :         {
      96           0 :                 if (i == "AIMetadata")   // not used inside petra
      97           0 :                         continue;
      98           0 :                 if(this.savedEvents[i] !== undefined)
      99           0 :                         this.savedEvents[i] = this.savedEvents[i].concat(this.events[i]);
     100             :                 else
     101           0 :                         this.savedEvents[i] = this.events[i];
     102             :         }
     103             : 
     104             :         // Run the update every n turns, offset depending on player ID to balance the load
     105           0 :         this.elapsedTime = this.gameState.getTimeElapsed() / 1000;
     106           0 :         if (!this.playedTurn || (this.turn + this.player) % 8 == 5)
     107             :         {
     108           0 :                 Engine.ProfileStart("PetraBot bot (player " + this.player +")");
     109             : 
     110           0 :                 this.playedTurn++;
     111             : 
     112           0 :                 if (this.gameState.getOwnEntities().length === 0)
     113             :                 {
     114           0 :                         Engine.ProfileStop();
     115           0 :                         return; // With no entities to control the AI cannot do anything
     116             :                 }
     117             : 
     118           0 :                 this.HQ.update(this.gameState, this.queues, this.savedEvents);
     119             : 
     120           0 :                 this.queueManager.update(this.gameState);
     121             : 
     122           0 :                 for (let i in this.savedEvents)
     123           0 :                         this.savedEvents[i] = [];
     124             : 
     125           0 :                 Engine.ProfileStop();
     126             :         }
     127             : 
     128           0 :         this.turn++;
     129             : };
     130             : 
     131           0 : PETRA.PetraBot.prototype.Serialize = function()
     132             : {
     133           0 :         let savedEvents = {};
     134           0 :         for (let key in this.savedEvents)
     135             :         {
     136           0 :                 savedEvents[key] = this.savedEvents[key].slice();
     137           0 :                 for (let i in savedEvents[key])
     138             :                 {
     139           0 :                         if (!savedEvents[key][i] || !savedEvents[key][i].entityObj)
     140           0 :                                 continue;
     141           0 :                         let evt = savedEvents[key][i];
     142           0 :                         let evtmod = {};
     143           0 :                         for (let keyevt in evt)
     144           0 :                                 evtmod[keyevt] = evt[keyevt];
     145           0 :                         evtmod.entityObj = evt.entityObj._entity;
     146           0 :                         savedEvents[key][i] = evtmod;
     147             :                 }
     148             :         }
     149             : 
     150           0 :         return {
     151             :                 "uniqueIDs": this.uniqueIDs,
     152             :                 "turn": this.turn,
     153             :                 "playedTurn": this.playedTurn,
     154             :                 "elapsedTime": this.elapsedTime,
     155             :                 "savedEvents": savedEvents,
     156             :                 "config": this.Config.Serialize(),
     157             :                 "queueManager": this.queueManager.Serialize(),
     158             :                 "HQ": this.HQ.Serialize()
     159             :         };
     160             : };
     161             : 
     162           0 : PETRA.PetraBot.prototype.Deserialize = function(data, sharedScript)
     163             : {
     164           0 :         this.isDeserialized = true;
     165           0 :         this.data = data;
     166             : };

Generated by: LCOV version 1.14