Line data Source code
1 : /** 2 : * Checks for emergencies and acts accordingly 3 : */ 4 0 : PETRA.EmergencyManager = function(Config) 5 : { 6 0 : this.Config = Config; 7 0 : this.referencePopulation = 0; 8 0 : this.referenceStructureCount = 0; 9 0 : this.numRoots = 0; 10 0 : this.hasEmergency = false; 11 : }; 12 : 13 0 : PETRA.EmergencyManager.prototype.init = function(gameState) 14 : { 15 0 : this.referencePopulation = gameState.getPopulation(); 16 0 : this.referenceStructureCount = gameState.getOwnStructures().length; 17 0 : this.numRoots = this.rootCount(gameState); 18 : }; 19 : 20 0 : PETRA.EmergencyManager.prototype.update = function(gameState) 21 : { 22 0 : if (this.hasEmergency) 23 : { 24 0 : this.emergencyUpdate(gameState); 25 0 : return; 26 : } 27 0 : const pop = gameState.getPopulation(); 28 0 : const nStructures = gameState.getOwnStructures().length; 29 0 : const nRoots = this.rootCount(gameState); 30 0 : const factors = this.Config.emergencyValues; 31 0 : if (((pop / this.referencePopulation) < factors.population || pop == 0) && 32 : ((nStructures / this.referenceStructureCount) < factors.structures || nStructures == 0)) 33 0 : this.setEmergency(gameState, true); 34 0 : else if ((nRoots / this.numRoots) <= factors.roots || (nRoots == 0 && this.numRoots != 0)) 35 0 : this.setEmergency(gameState, true); 36 : 37 0 : if (pop > this.referencePopulation || this.hasEmergency) 38 0 : this.referencePopulation = pop; 39 0 : if (nStructures > this.referenceStructureCount || this.hasEmergency) 40 0 : this.referenceStructureCount = nStructures; 41 0 : if (nRoots > this.numRoots || this.hasEmergency) 42 0 : this.numRoots = nRoots; 43 : }; 44 : 45 0 : PETRA.EmergencyManager.prototype.emergencyUpdate = function(gameState) 46 : { 47 0 : const pop = gameState.getPopulation(); 48 0 : const nStructures = gameState.getOwnStructures().length; 49 0 : const nRoots = this.rootCount(gameState); 50 0 : const factors = this.Config.emergencyValues; 51 : 52 0 : if ((pop > this.referencePopulation * 1.2 && 53 : nStructures > this.referenceStructureCount * 1.2) || 54 : nRoots > this.numRoots) 55 : { 56 0 : this.setEmergency(gameState, false); 57 0 : this.referencePopulation = pop; 58 0 : this.referenceStructureCount = nStructures; 59 0 : this.numRoots = nRoots; 60 : } 61 : }; 62 : 63 0 : PETRA.EmergencyManager.prototype.rootCount = function(gameState) 64 : { 65 0 : let roots = 0; 66 0 : gameState.getOwnStructures().toEntityArray().forEach(ent => { 67 0 : if (ent?.get("TerritoryInfluence")?.Root === "true") 68 0 : roots++; 69 : }); 70 0 : return roots; 71 : }; 72 : 73 0 : PETRA.EmergencyManager.prototype.setEmergency = function(gameState, enable) 74 : { 75 0 : this.hasEmergency = enable; 76 0 : PETRA.chatEmergency(gameState, enable); 77 : }; 78 : 79 0 : PETRA.EmergencyManager.prototype.Serialize = function() 80 : { 81 0 : return { 82 : "referencePopulation": this.referencePopulation, 83 : "referenceStructureCount": this.referenceStructureCount, 84 : "numRoots": this.numRoots, 85 : "hasEmergency": this.hasEmergency 86 : }; 87 : }; 88 : 89 0 : PETRA.EmergencyManager.prototype.Deserialize = function(data) 90 : { 91 0 : for (const key in data) 92 0 : this[key] = data[key]; 93 : };