-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.ts
More file actions
207 lines (181 loc) · 6.1 KB
/
Copy pathengine.ts
File metadata and controls
207 lines (181 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { ResourceDefinition, Turn, ActiveResource } from "./types";
export class GameEngine {
budget: number;
turns: Turn[];
resources: ResourceDefinition[];
activeResources: ActiveResource[] = [];
currentTurn: number = 0;
totalScore: number = 0;
totalStoredBuildings: number = 0;
totalAccumulatorCapacity: number = 0;
constructor(budget: number, turns: Turn[], resources: ResourceDefinition[]) {
this.budget = budget;
this.turns = turns;
this.resources = resources;
}
purchaseResources(resourceIds: number[]): boolean {
const cost = resourceIds.reduce((sum, id) => {
const def = this.resources.find((r) => r.id === id)!;
return sum + def.activationCost;
}, 0);
if (cost > this.budget) return false;
this.budget -= cost;
for (const id of resourceIds) {
const def = this.resources.find((r) => r.id === id)!;
const newResource: ActiveResource = {
definition: def,
remainingLife: def.lifecycle,
cooldownRemaining: 0,
turnsRemainingActive: def.activeTurns,
};
this.applyCTypeEffects(newResource);
this.activeResources.push(newResource);
if (def.effectType === "E") {
this.totalAccumulatorCapacity += def.effectValue || 0;
}
}
return true;
}
private applyCTypeEffects(newResource: ActiveResource) {
const cResources = this.activeResources.filter(
(r) => r.definition.effectType === "C" && r.turnsRemainingActive > 0
);
const totalCPercentage = cResources.reduce(
(sum, c) => sum + (c.definition.effectValue || 0),
0
);
const factor = 1 + totalCPercentage / 100;
newResource.remainingLife = Math.max(
1,
Math.floor(newResource.definition.lifecycle * factor)
);
}
runTurn(): number {
const turn = this.turns[this.currentTurn];
let buildingsPowered = 0;
let maintenanceCost = 0;
let profitPerBuilding = turn.profitPerBuilding;
let minBuildings = turn.minBuildings;
let maxBuildings = turn.maxBuildings;
// Apply special effects
const activeEffects = this.activeResources
.filter((r) => r.turnsRemainingActive > 0 && r.remainingLife > 0)
.reduce((acc, r) => {
acc[r.definition.effectType] = acc[r.definition.effectType] || [];
acc[r.definition.effectType].push(r);
return acc;
}, {} as Record<string, ActiveResource[]>);
// A: Smart Meter
const totalAPercentage = activeEffects["A"]
? activeEffects["A"].reduce(
(sum, r) => sum + (r.definition.effectValue || 0),
0
)
: 0;
const aFactor = 1 + totalAPercentage / 100;
let baseBuildings = this.activeResources.reduce(
(sum, r) => sum + (r.definition.buildingsPowered || 0),
0
);
// Apply A-type effects AFTER base calculation
const adjustedBuildings = Math.floor(baseBuildings * aFactor);
buildingsPowered = adjustedBuildings;
// B: Distribution Facility
const totalBPercentage = activeEffects["B"]
? activeEffects["B"].reduce(
(sum, r) => sum + (r.definition.effectValue || 0),
0
)
: 0;
const bFactor = 1 + totalBPercentage / 100;
minBuildings = Math.max(0, Math.floor(turn.minBuildings * bFactor));
maxBuildings = Math.max(0, Math.floor(turn.maxBuildings * bFactor));
// D: Renewable Plant
const totalDPercentage = activeEffects["D"]
? activeEffects["D"].reduce(
(sum, r) => sum + (r.definition.effectValue || 0),
0
)
: 0;
const dFactor = 1 + totalDPercentage / 100;
profitPerBuilding = Math.max(
0,
Math.floor(turn.profitPerBuilding * dFactor)
);
// Update resources and calculate buildings powered
this.activeResources.forEach((res) => {
if (res.remainingLife <= 0) return;
if (res.cooldownRemaining > 0) {
res.cooldownRemaining--;
if (res.cooldownRemaining === 0 && res.remainingLife > 0) {
res.turnsRemainingActive = res.definition.activeTurns;
}
} else if (res.turnsRemainingActive > 0) {
const adjustedRU = Math.max(
0,
Math.floor(res.definition.buildingsPowered * aFactor)
);
buildingsPowered += adjustedRU;
maintenanceCost += res.definition.periodicCost;
res.turnsRemainingActive--;
if (
res.turnsRemainingActive === 0 &&
res.definition.maintenanceTurns > 0
) {
res.cooldownRemaining = res.definition.maintenanceTurns;
}
}
res.remainingLife--;
});
// E: Accumulator handling
const surplus = buildingsPowered - maxBuildings;
if (surplus > 0) {
this.totalStoredBuildings = Math.min(
this.totalAccumulatorCapacity,
this.totalStoredBuildings + surplus
);
buildingsPowered = maxBuildings;
}
if (buildingsPowered < minBuildings) {
const needed = minBuildings - buildingsPowered;
if (this.totalStoredBuildings >= needed) {
this.totalStoredBuildings -= needed;
buildingsPowered = minBuildings;
} else {
buildingsPowered += this.totalStoredBuildings;
this.totalStoredBuildings = 0;
}
}
// Remove expired E capacity
const activeEResources = this.activeResources.filter(
(r) => r.definition.effectType === "E" && r.remainingLife > 0
);
this.totalAccumulatorCapacity = activeEResources.reduce(
(sum, e) => sum + (e.definition.effectValue || 0),
0
);
this.totalStoredBuildings = Math.min(
this.totalStoredBuildings,
this.totalAccumulatorCapacity
);
// Calculate profit
let profit = 0;
if (buildingsPowered >= minBuildings) {
profit = Math.min(buildingsPowered, maxBuildings) * profitPerBuilding;
}
this.budget += profit - maintenanceCost;
this.totalScore += profit;
this.currentTurn++;
// Remove expired resources
this.activeResources = this.activeResources.filter(
(r) => r.remainingLife > 0
);
return profit;
}
isGameOver(): boolean {
return this.currentTurn >= this.turns.length;
}
getTotalScore(): number {
return this.totalScore;
}
}