Line data Source code
1 : import { ResourcesManager } from "simulation/ai/common-api/resources.js";
2 : import { QueuePlan } from "simulation/ai/petra/queueplan.js";
3 :
4 : export class ResearchPlan extends QueuePlan
5 : {
6 : constructor(gameState, type, rush = false)
7 : {
8 0 : super(gameState, type, {});
9 :
10 0 : if (this.template.researchTime === undefined)
11 0 : throw new Error();
12 :
13 : // Refine the estimated cost
14 0 : const researchers = this.getBestResearchers(gameState, true);
15 0 : if (researchers)
16 0 : this.cost = new ResourcesManager(this.template.cost(researchers[0]));
17 :
18 0 : this.category = "technology";
19 0 : this.rush = rush;
20 : }
21 :
22 : canStart(gameState)
23 : {
24 0 : this.researchers = this.getBestResearchers(gameState);
25 0 : if (!this.researchers)
26 0 : return false;
27 0 : this.cost = new ResourcesManager(this.template.cost(this.researchers[0]));
28 0 : return true;
29 : }
30 :
31 : getBestResearchers(gameState, noRequirementCheck = false)
32 : {
33 0 : const 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 (const ent of allResearchers.values())
41 : {
42 0 : const 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 : isInvalid(gameState)
55 : {
56 0 : return gameState.isResearched(this.type) || gameState.isResearching(this.type);
57 : }
58 :
59 : start(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 : onStart(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 : Serialize()
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 : Deserialize(gameState, data)
101 : {
102 0 : for (const key in data)
103 0 : this[key] = data[key];
104 :
105 0 : this.cost = new ResourcesManager();
106 0 : this.cost.Deserialize(data.cost);
107 : }
108 : }
|