Line data Source code
1 0 : PETRA.ResearchPlan = function(gameState, type, rush = false) 2 : { 3 0 : if (!PETRA.QueuePlan.call(this, gameState, type, {})) 4 0 : return false; 5 : 6 0 : if (this.template.researchTime === undefined) 7 0 : return false; 8 : 9 : // Refine the estimated cost 10 0 : let researchers = this.getBestResearchers(gameState, true); 11 0 : if (researchers) 12 0 : this.cost = new API3.Resources(this.template.cost(researchers[0])); 13 : 14 0 : this.category = "technology"; 15 0 : this.rush = rush; 16 : 17 0 : return true; 18 : }; 19 : 20 0 : PETRA.ResearchPlan.prototype = Object.create(PETRA.QueuePlan.prototype); 21 : 22 0 : PETRA.ResearchPlan.prototype.canStart = function(gameState) 23 : { 24 0 : this.researchers = this.getBestResearchers(gameState); 25 0 : if (!this.researchers) 26 0 : return false; 27 0 : this.cost = new API3.Resources(this.template.cost(this.researchers[0])); 28 0 : return true; 29 : }; 30 : 31 0 : PETRA.ResearchPlan.prototype.getBestResearchers = function(gameState, noRequirementCheck = false) 32 : { 33 0 : let allResearchers = gameState.findResearchers(this.type, noRequirementCheck); 34 0 : if (!allResearchers || !allResearchers.hasEntities()) 35 0 : return undefined; 36 : 37 : // Keep only researchers with smallest cost 38 0 : let costMin = Math.min(); 39 : let researchers; 40 0 : for (let ent of allResearchers.values()) 41 : { 42 0 : let cost = this.template.costSum(ent); 43 0 : if (cost === costMin) 44 0 : researchers.push(ent); 45 0 : else if (cost < costMin) 46 : { 47 0 : costMin = cost; 48 0 : researchers = [ent]; 49 : } 50 : } 51 0 : return researchers; 52 : }; 53 : 54 0 : PETRA.ResearchPlan.prototype.isInvalid = function(gameState) 55 : { 56 0 : return gameState.isResearched(this.type) || gameState.isResearching(this.type); 57 : }; 58 : 59 0 : PETRA.ResearchPlan.prototype.start = function(gameState) 60 : { 61 : // Prefer researcher with shortest queues (no need to serialize this.researchers 62 : // as the functions canStart and start are always called on the same turn) 63 0 : this.researchers.sort((a, b) => a.trainingQueueTime() - b.trainingQueueTime()); 64 : // Drop anything in the queue if we rush it. 65 0 : if (this.rush) 66 0 : this.researchers[0].stopAllProduction(0.45); 67 0 : this.researchers[0].research(this.type); 68 0 : this.onStart(gameState); 69 : }; 70 : 71 0 : PETRA.ResearchPlan.prototype.onStart = function(gameState) 72 : { 73 0 : if (this.queueToReset) 74 0 : gameState.ai.queueManager.changePriority(this.queueToReset, gameState.ai.Config.priorities[this.queueToReset]); 75 : 76 0 : for (let i = gameState.getNumberOfPhases(); i > 0; --i) 77 : { 78 0 : if (this.type != gameState.getPhaseName(i)) 79 0 : continue; 80 0 : gameState.ai.HQ.phasing = 0; 81 0 : gameState.ai.HQ.OnPhaseUp(gameState, i); 82 0 : break; 83 : } 84 : }; 85 : 86 0 : PETRA.ResearchPlan.prototype.Serialize = function() 87 : { 88 0 : return { 89 : "category": this.category, 90 : "type": this.type, 91 : "ID": this.ID, 92 : "metadata": this.metadata, 93 : "cost": this.cost.Serialize(), 94 : "number": this.number, 95 : "rush": this.rush, 96 : "queueToReset": this.queueToReset || undefined 97 : }; 98 : }; 99 : 100 0 : PETRA.ResearchPlan.prototype.Deserialize = function(gameState, data) 101 : { 102 0 : for (let key in data) 103 0 : this[key] = data[key]; 104 : 105 0 : this.cost = new API3.Resources(); 106 0 : this.cost.Deserialize(data.cost); 107 : };