-
Notifications
You must be signed in to change notification settings - Fork 0
/
observations.js
190 lines (165 loc) · 4.97 KB
/
observations.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
const GAME_CONSTANTS = require("./lux/game_constants");
const DIRECTIONS = GAME_CONSTANTS.DIRECTIONS;
const { getPositionHash } = require("./utils.js");
const getIsUnitCurrentlySharingTileWithOtherUnit = (unit, player) => {
const unitsOnTile =
player.units.filter((u) => {
return u.pos.x === unit.pos.x && u.pos.y === unit.pos.y;
}).length || 0;
return unitsOnTile > 1;
};
const getClosestUnclaimedResourceTile = (
resourceTiles,
player,
unit,
claimedTiles
) => {
let closestResourceTile = null;
let closestDist = 9999999;
resourceTiles
.filter((tile) => {
return claimedTiles.indexOf(getPositionHash(tile.pos)) < 0;
})
.forEach((cell) => {
if (
cell.resource.type === GAME_CONSTANTS.RESOURCE_TYPES.COAL &&
!player.researchedCoal()
)
return;
if (
cell.resource.type === GAME_CONSTANTS.RESOURCE_TYPES.URANIUM &&
!player.researchedUranium()
)
return;
const dist = cell.pos.distanceTo(unit.pos);
if (
dist < closestDist &&
(cell.resource.type !== "wood" || cell.resource.amount > 50)
) {
closestDist = dist;
closestResourceTile = cell;
}
});
return closestResourceTile;
};
const getNearestUnclaimedEmptyTile = (gameMap, unit, claimedTiles) => {
const emptyTiles = getAllEmptyTiles(gameMap);
let closestEmptyTile = null;
let closestDist = 9999999;
emptyTiles
.filter((tile) => {
return claimedTiles.indexOf(getPositionHash(tile.pos)) < 0;
})
.forEach((cell) => {
const dist = cell.pos.distanceTo(unit.pos);
if (dist < closestDist) {
closestDist = dist;
closestEmptyTile = cell;
}
});
return closestEmptyTile;
};
const getClosestUnclaimedCityTileNeedingFuel = (player, unit, claimedTiles) => {
const citiesArr = Object.values(Object.fromEntries(player.cities));
let closestDist = 999999;
let closestCityTile = null;
citiesArr.forEach((city) => {
if (city.lightUpkeep * 10 > city.fuel) {
city.citytiles
.filter((citytile) => {
return claimedTiles.indexOf(getPositionHash(citytile.pos)) < 0;
})
.forEach((citytile) => {
const dist = citytile.pos.distanceTo(unit.pos);
if (dist < closestDist) {
closestCityTile = citytile;
closestDist = dist;
}
});
}
});
return closestCityTile;
};
const getClosestCityTileWithLeastFuel = (player, unit, claimedTiles) => {
const citiesArr = Object.values(Object.fromEntries(player.cities));
let closestDist = 999999;
let leastFuel = 999999;
let closestCityTile = null;
citiesArr.forEach((city) => {
if (city.fuel / city.citytiles.length < leastFuel) {
city.citytiles
.filter((citytile) => {
return claimedTiles.indexOf(getPositionHash(citytile.pos)) < 0;
})
.forEach((citytile) => {
const dist = citytile.pos.distanceTo(unit.pos);
if (dist < closestDist) {
closestCityTile = citytile;
closestDist = dist;
}
});
}
});
return closestCityTile;
};
const getAllEmptyTiles = (gameMap) => {
let emptyTiles = [];
for (let y = 0; y < gameMap.height; y++) {
for (let x = 0; x < gameMap.width; x++) {
const cell = gameMap.getCell(x, y);
if (!cell.citytile && !cell.resource) {
// logs.push("empty tile?: " + JSON.stringify(cell));
emptyTiles.push(cell);
}
}
}
return emptyTiles;
};
const getAllResourceTiles = (gameMap) => {
let resourceTiles = [];
for (let y = 0; y < gameMap.height; y++) {
for (let x = 0; x < gameMap.width; x++) {
const cell = gameMap.getCell(x, y);
if (cell.hasResource()) {
resourceTiles.push(cell);
}
}
}
return resourceTiles;
};
const getCountOwnedCityTiles = (cities) => {
return cities.reduce((acc, currentCity) => {
return acc + currentCity.citytiles.length;
}, 0);
};
const getDoAnyCitiesNeedFuel = (player) => {
const cities = Object.values(Object.fromEntries(player.cities));
return cities.filter((city) => city.lightUpkeep * 10 > city.fuel).length > 0;
};
const getCanUnitBuildCityRightNow = (unit, gameMap) => {
return (
unit.canBuild(gameMap) && unit.canAct() && unit.getCargoSpaceLeft() < 1
);
};
const getMyCityTileCount = (player) => {
const citiesArr = Object.values(Object.fromEntries(player.cities));
return getCountOwnedCityTiles(citiesArr);
};
const getOpponentCityTileCount = (opponent) => {
const citiesArr = Object.values(Object.fromEntries(opponent.cities));
return getCountOwnedCityTiles(citiesArr);
};
module.exports = {
getIsUnitCurrentlySharingTileWithOtherUnit,
getClosestUnclaimedResourceTile,
getNearestUnclaimedEmptyTile,
getClosestUnclaimedCityTileNeedingFuel,
getClosestCityTileWithLeastFuel,
getAllEmptyTiles,
getAllResourceTiles,
getCountOwnedCityTiles,
getDoAnyCitiesNeedFuel,
getCanUnitBuildCityRightNow,
getMyCityTileCount,
getOpponentCityTileCount,
};