-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
138 lines (127 loc) · 3.55 KB
/
actions.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
const {
getPositionHash,
modelPosMoveByDirection,
randomIntFromInterval,
} = require("./utils.js");
const {
getClosestUnclaimedResourceTile,
getNearestUnclaimedEmptyTile,
getClosestUnclaimedCityTileNeedingFuel,
getClosestCityTileWithLeastFuel,
} = require("./observations.js");
const goToNearestMineableResource = (
unit,
actions,
resourceTiles,
player,
claimedTiles,
logs
) => {
const closestUnclaimedResourceTile = getClosestUnclaimedResourceTile(
resourceTiles,
player,
unit,
claimedTiles
);
if (closestUnclaimedResourceTile != null) {
const dir = unit.pos.directionTo(closestUnclaimedResourceTile.pos);
// move the unit in the direction towards the closest resource tile's position.
actions.push(unit.move(dir));
claimedTiles.push(getPositionHash(modelPosMoveByDirection(unit.pos, dir)));
}
};
const goToNearestCityNeedingFuel = (
player,
unit,
claimedTiles,
actions,
logs
) => {
const closestCityTileNeedingFuel = getClosestUnclaimedCityTileNeedingFuel(
player,
unit,
claimedTiles
);
if (closestCityTileNeedingFuel != null) {
const dir = unit.pos.directionTo(closestCityTileNeedingFuel.pos);
if (
claimedTiles.indexOf(
getPositionHash(modelPosMoveByDirection(unit.pos, dir))
) > -1
) {
moveRandomDirection(unit, actions, logs);
} else {
actions.push(unit.move(dir));
claimedTiles.push(
getPositionHash(modelPosMoveByDirection(unit.pos, dir))
);
logs.push(
"heading to the closest city needing fuel" +
JSON.stringify(closestCityTileNeedingFuel.pos)
);
}
} else {
//no cities needing fuel immediately, go to lowest fuel per tile then
const cityTileWithLeastFuel = getClosestCityTileWithLeastFuel(
player,
unit,
claimedTiles
);
if (!cityTileWithLeastFuel) {
logs.push(
"SOMETHING WENT WRONG, COULDNT FIND ANY CITY WHEN LOOKING FOR LEAST FUEL???"
);
return;
}
const dir = unit.pos.directionTo(cityTileWithLeastFuel.pos);
if (
claimedTiles.indexOf(
getPositionHash(modelPosMoveByDirection(unit.pos, dir))
) > -1
) {
moveRandomDirection(unit, actions, logs);
} else {
logs.push(
"heading to the city with lowest fuel (though doesn't need it)" +
JSON.stringify(cityTileWithLeastFuel.pos)
);
actions.push(unit.move(dir));
claimedTiles.push(
getPositionHash(modelPosMoveByDirection(unit.pos, dir))
);
}
}
};
const buildCity = (unit, actions, logs) => {
logs.push("TRIED TO BUILD CITY");
actions.push(unit.buildCity());
};
const moveToNearestEmptyTile = (gameMap, unit, claimedTiles, actions, logs) => {
const nearestEmptyTile = getNearestUnclaimedEmptyTile(
gameMap,
unit,
claimedTiles
);
if (nearestEmptyTile) {
logs.push("trying to move to" + JSON.stringify(nearestEmptyTile.pos));
const dir = unit.pos.directionTo(nearestEmptyTile.pos);
actions.push(unit.move(dir));
claimedTiles.push(getPositionHash(modelPosMoveByDirection(unit.pos, dir)));
} else {
logs.push(
"fell into the else, something went wrong or there are literally no empty tiles"
);
}
};
const DIRS = ["n", "s", "e", "w"];
const moveRandomDirection = (unit, actions, logs) => {
logs.push(unit.id + " - moving random direction");
actions.push(unit.move(DIRS[randomIntFromInterval(0, 3)]));
};
module.exports = {
goToNearestMineableResource,
goToNearestCityNeedingFuel,
buildCity,
moveToNearestEmptyTile,
moveRandomDirection,
};