-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.js
275 lines (250 loc) · 10.1 KB
/
state.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
console.time("state generation")
const DEFENSE = 0
const ATTACK = 1
const ENERGY = 2
const DECONSTRUCT = 3
const TESLA = 4
const IRON_CURTAIN = 5
let stateFile = require("./state.json")
let currentRound = stateFile.gameDetails.round
let { flatMap, unique, most, least, allHighest } = require("./helpers.js")
let myself = stateFile.players.find(player => player.playerType === "A")
let opponent = stateFile.players.find(player => player.playerType === "B")
let mapSize = {
x: stateFile.gameDetails.mapWidth,
y: stateFile.gameDetails.mapHeight
}
let buildingStats = []
buildingStats[DEFENSE] = stateFile.gameDetails.buildingsStats.DEFENSE
buildingStats[ATTACK] = stateFile.gameDetails.buildingsStats.ATTACK
buildingStats[ENERGY] = stateFile.gameDetails.buildingsStats.ENERGY
buildingStats[DECONSTRUCT] = null
buildingStats[TESLA] = stateFile.gameDetails.buildingsStats.TESLA
buildingStats[IRON_CURTAIN] = stateFile.gameDetails.ironCurtainStats
let gameMap = stateFile.gameMap
let cells = flatMap(gameMap)
let buildingsBeforeDestroyedRemoved = flatMap(
cells.filter(cell => cell.buildings.length > 0).map(cell => cell.buildings)
)
setNewCellPropertyDefaults()
let [missiles, turrets] = addImpactInXRounds()
let [emptyCells, buildings] = removeDestroyedBuildings()
addImpactInXRoundsForNewTurrets()
let myBuildings = buildings.filter(building => building.playerType == "A")
let myEnergyBuilings = myBuildings.filter(building => building.buildingType == "ENERGY")
let myTurrets = myBuildings.filter(building => building.buildingType == "ATTACK")
let enemyBuildings = buildings.filter(building => building.playerType == "B")
let enemyEnergyBuildings = enemyBuildings.filter(building => building.buildingType == "ENERGY")
let enemyWalls = enemyBuildings.filter(building => building.buildingType == "DEFENSE")
let enemyTurrets = enemyBuildings.filter(building => building.buildingType == "ATTACK")
let enemyMissiles = missiles.filter(missile => missile.playerType == "B")
let myMissiles = missiles.filter(missile => missile.playerType == "A")
let myEnergyProduction = myEnergyBuilings.length * buildingStats[ENERGY].energyGeneratedPerTurn
addEmptyCellsAdditionalStats()
function setNewCellPropertyDefaults() {
cells.forEach(cell => {
cell.impactInXRounds = 0
})
}
function addImpactInXRounds() {
return [addImpactInXRoundsBasedOnCurrentMissiles(), addImpactInXRoundsBasedOnWeaponCooldown()]
}
function addImpactInXRoundsBasedOnCurrentMissiles() {
let missiles = flatMap(cells.filter(cell => cell.missiles.length > 0).map(cell => cell.missiles))
missiles.forEach(missile => {
addMissileLocation(missile)
})
return missiles
}
function addImpactInXRoundsBasedOnWeaponCooldown() {
// don't have to worry about excluding turrets that will be destroyed next round because
// game engine fires missiles before destroying buildings, so they will still fire
let turrets = buildingsBeforeDestroyedRemoved.filter(building => building.buildingType == "ATTACK")
turrets.forEach(turret => {
if (aboutToFireAndNotUnderConstuction(turret)) {
addMissileLocation(turret)
}
})
return turrets
}
function aboutToFireAndNotUnderConstuction(turret) {
// Turrets that finish construction the following round are added only after destroyed buildings have been
// removed in addImpactInXRoundsForNewTurrets() otherwise it will think the missile hit
// something when actually that building has been destroyed and the missile is still flying
return [0, 1].includes(turret.weaponCooldownTimeLeft && turret.constructionTimeLeft == -1)
}
function addMissileLocation(missileOrTurret) {
let isFriendly = missileOrTurret.playerType == "A"
let numberOfColumnsToCheck
if (isFriendly) {
numberOfColumnsToCheck = gameMap[0].length - 1 - missileOrTurret.x
} else {
numberOfColumnsToCheck = missileOrTurret.x
}
for (let index = 1; index <= numberOfColumnsToCheck; index++) {
let cellToCheck
if (isFriendly) {
cellToCheck = cells.find(cell => cell.y == missileOrTurret.y && cell.x == missileOrTurret.x + index)
} else {
cellToCheck = cells.find(cell => cell.y == missileOrTurret.y && cell.x == missileOrTurret.x - index)
}
if (cellToCheck.impactInXRounds > 0) {
return // only calculate impact for first missile to hit. Otherwise it will add second incoming missile on top.
}
if (!cellToCheck) {
throw new Error("trying to check invalid cell")
}
cellToCheck.impactInXRounds += roundsUntilImpact(missileOrTurret, cellToCheck)
if (cellToCheck.buildings.length) {
break // stop checking if it hits something
}
}
}
function roundsUntilImpact(missileOrTurret, cellToCheck) {
return Math.round(
Math.abs(missileOrTurret.x - cellToCheck.x) / (missileOrTurret.speed || missileOrTurret.weaponSpeed) // depending on if it's a turret or a missile
)
}
function addImpactInXRoundsForNewTurrets() {
// Have to do it seperate from WeaponCooldown ones so that it first removes destroyed ones.
// Otherwise it will think the missile hit a building which is not there because it was destroyed.
let newTurrets = buildings.filter(building => building.buildingType == "ATTACK" && building.constructionTimeLeft == 0)
newTurrets.forEach(turret => {
addMissileLocation(turret, turret.playerType == "A")
})
}
function removeDestroyedBuildings() {
let emptyCells = cells.filter(cell => cell.buildings.length == 0 && cell.x <= mapSize.x / 2 - 1)
// no need to add destroyed buildings to emptyCells as game engen does player commands before
// destroying buildings so even though it's destroyed that cell is not available yet to build on.
unDestroyedBuildings = buildingsBeforeDestroyedRemoved.filter(building => {
let cell = cells.find(cell => cell.x == building.x && cell.y == building.y)
return cell.impactInXRounds != 1 || building.health - buildingStats[ATTACK].weaponDamage > 0
})
return [emptyCells, unDestroyedBuildings]
}
function addEmptyCellsAdditionalStats() {
emptyCells.forEach(emptyCell => {
emptyCell.potentialCoverProvided = potentialCoverProvidedRating(emptyCell.y)
addWallViability(emptyCell)
addEnergyViability(emptyCell)
addTurretViability(emptyCell)
emptyCell.enemyRowDefenseRating = calcRowDefenseRating(emptyCell.y)
emptyCell.rowAttackingTurretsCount = rowAttackingTurretsCount(emptyCell.y)
})
}
function potentialCoverProvidedRating(yCoord) {
const coverProvidedToRating = {
ENERGY: 1,
ATTACK: 2,
DEFENSE: 0
}
let buildingsBehind = myBuildings.filter(building => building.y == yCoord)
let potentialCoverProvided = 0
buildingsBehind.forEach(building => {
potentialCoverProvided += coverProvidedToRating[building.buildingType]
})
return potentialCoverProvided
}
function addWallViability(emptyCell) {
emptyCell.wallViability = 0
emptyCell.wallViability += emptyCell.potentialCoverProvided
}
function addEnergyViability(emptyCell) {
emptyCell.energyViability = 0
emptyCell.energyViability -= energyOnSameRowCount(emptyCell.y) // spread out is better
emptyCell.energyViability -= enemyRowAttackRating(emptyCell.y) // better to build where being less attacked // SG_TODO:(unless health gets low???)
}
function energyOnSameRowCount(yCoord) {
return myEnergyBuilings.filter(building => building.y == yCoord).length
}
function enemyRowAttackRating(yCoord) {
// only for purpose of optimising energy placement (not go for kill strat)
// so not worrying about walls that don't have turrets behind them.
let rowAttackRating = 0
let enemyTurretsOnSameRow = enemyTurrets.filter(turret => turret.y == yCoord)
enemyTurretsOnSameRow.forEach(enemyTurret => {
rowAttackRating++
rowAttackRating += wallsInFrontOfTurret(enemyTurret)
})
return rowAttackRating
}
function wallsInFrontOfTurret(enemyTurret) {
let wallsInFrontOfTurret = enemyBuildings.filter(
building => building.buildingType == "DEFENSE" && building.y == enemyTurret.y && building.x < enemyTurret.x
)
return wallsInFrontOfTurret.length
}
function addTurretViability(emptyCell) {
emptyCell.turretViability = 0
addTurretViabilityByInitialTargetType(emptyCell)
addTurretViabilityByEnemyEnergyBuildingsOnSameRow(emptyCell)
}
function addTurretViabilityByInitialTargetType(emptyCell) {
const targetPriorityRating = {
ENERGY: 1,
ATTACK: 5,
DEFENSE: 0
}
let enemyBuildingsOnSameRow = enemyBuildings.filter(building => building.y == emptyCell.y)
if (enemyBuildingsOnSameRow.length) {
let initialTarget = least(enemyBuildingsOnSameRow, "x")
// emptyCell.turretViability += targetPriorityRating[initialTarget.buildingType] // SG_TODO: fix duplication
if (initialTarget.buildingType == "ATTACK" && missileWillDestroyTargetBeforeItFires(emptyCell, initialTarget)) {
emptyCell.turretViability += 5
} else {
emptyCell.turretViability += targetPriorityRating[initialTarget.buildingType]
}
}
}
function missileWillDestroyTargetBeforeItFires(emptyCell, initialTarget) {
// if 1 then it's already too late as it also destroyes my turret when it fires
let roundsUntilImpact = Math.round(Math.abs(initialTarget.x - emptyCell.x) / buildingStats[ATTACK].weaponSpeed)
return initialTarget.weaponCooldownTimeLeft > roundsUntilImpact
}
function addTurretViabilityByEnemyEnergyBuildingsOnSameRow(emptyCell) {
let enemyEnergyBuildingsOnSameRow = enemyEnergyBuildings.filter(building => building.y == emptyCell.y)
enemyEnergyBuildingsOnSameRow.forEach(building => {
emptyCell.turretViability++
})
}
function calcRowDefenseRating(yCoord) {
const defenseRating = {
ENERGY: 1,
ATTACK: 2,
DEFENSE: 5
}
enemyBuildingsOnSameRow = enemyBuildings.filter(building => building.y == yCoord)
rowDefenseRating = 0
enemyBuildingsOnSameRow.forEach(building => {
rowDefenseRating += defenseRating[building.buildingType]
})
return rowDefenseRating
}
function rowAttackingTurretsCount(yCoord) {
return myTurrets.filter(turret => turret.y == yCoord).length
}
module.exports = {
ATTACK,
DEFENSE,
ENERGY,
TESLA,
IRON_CURTAIN,
myself,
opponent,
mapSize,
buildingStats,
cells,
buildings,
myBuildings,
myEnergyBuilings,
enemyBuildings,
emptyCells,
enemyEnergyBuildings,
enemyWalls,
enemyTurrets,
myEnergyProduction,
myTurrets,
enemyTurrets,
currentRound
}