-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.h
285 lines (242 loc) · 6.4 KB
/
State.h
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
276
277
278
279
280
281
282
283
284
285
#ifndef STATE_H_
#define STATE_H_
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdint.h>
#include "Timer.h"
#include "Bug.h"
#include "Visualizer.h"
#include "Square.h"
#include "Location.h"
#include "grid.h"
/*
constants
*/
const int TDIRECTIONS = 4;
const char CDIRECTIONS[5] = {'N', 'E', 'S', 'W', 'x'};
const int DIRECTIONS[5][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1}, {0, 0} }; //{N, E, S, W}
const int AHEAD[5] = { 0, 1, 2, 3, 4 };
const int BEHIND[5] = { 2, 3, 0, 1, 4 };
const int RIGHT[5] = { 1, 2, 3, 0, 4 };
const int LEFT[5] = { 3, 0, 1, 2, 4 };
#define PURE __attribute__((__pure__))
template <size_t N>
class Mod
{
public:
Mod() : modulus(a + N / 2) { }
void init(int n)
{
for (int i = -int(N) / 2; i < int(N) / 2; ++i)
modulus[i] = (i + 10 * n) % n;
}
inline int operator () (const int &i) const PURE
{
return modulus[i];
}
protected:
int16_t *modulus;
int16_t a[N]; __attribute__((aligned(64)));
};
/*
struct to store current state information
*/
struct State
{
/*
Variables
*/
int rows, cols,
turn, turns,
noPlayers, noHills;
double attackradius, spawnradius, viewradius;
double avgHillSpacing;
double loadtime, turntime;
std::vector<double> scores;
bool gameover;
int64_t seed;
Grid<Square> grid;
std::vector<int> diedByPlayer, ateByPlayer;
std::vector<Location> myAnts, enemyAnts, myHills, enemyHills, food;
LocationSet allMyHills, allEnemyHills, allFood;
std::vector<Location> visionNeighborhood, combatNeighborhood;
std::vector<std::vector<Location> > visionArc;
int visibleSquares;
typedef std::vector<Location>::iterator iterator;
Mod<512> _row, _col;
Timer timer;
mutable Bug bug;
mutable Visualizer v;
/*
Functions
*/
State();
~State();
void setup();
void reset();
void makeMove(const Location &loc, int direction);
double distance(const Location &loc1, const Location &loc2) const;
//returns the new location from moving in a given direction with the edges wrapped
Location getLocation(const Location &loc, int direction) const PURE
{
return Location(_row(loc.row + DIRECTIONS[direction][0]),
_col(loc.col + DIRECTIONS[direction][1]));
}
//returns the new location from moving in a given direction with the edges wrapped
Location normLocation(const Location &loc) const PURE
{
return Location(_row(loc.row), _col(loc.col));
}
//returns the new location from moving in a given direction with the edges wrapped
Location deltaLocation(const Location &loc, int dr, int dc) const PURE
{
return Location(_row(loc.row + dr), _col(loc.col + dc));
}
int shortRowDiff(int r1, int r2) const PURE
{
int d = r1 - r2;
if (d > rows / 2)
d -= rows;
return d;
}
int shortColDiff(int c1, int c2) const PURE
{
int d = c1 - c2;
if (d > cols / 2)
d -= cols;
return d;
}
void updateVisionInformation();
std::vector<Location> neighborhood_offsets(double max_dist) const;
std::vector<Location> dialate_neighborhood(const std::vector<Location> &orig, int n) const;
std::vector<Location> neighborhood_arc(double max_dist, int direction) const;
};
extern State state;
struct AllPassable
{
bool operator () (const Location &loc) const
{
return true;
}
};
struct Passable
{
bool operator () (const Location &loc) const
{
const Square &square = state.grid(loc);
return !square.isWater;
}
};
struct PassableButMyHills
{
bool operator () (const Location &loc) const
{
const Square &square = state.grid(loc);
return !(square.isWater || square.hillPlayer == 0);
}
};
template <typename P = Passable>
struct Unidirectional : public P
{
Unidirectional(const Grid<int> &f) : flow(f) { }
bool operator () (const Location &loc) const
{
return P::operator()(loc) && flow(loc) <= origin;
}
void setOrigin(const Location &loc)
{
origin = flow(loc);
}
private:
const Grid<int> &flow;
int origin;
};
template <int B, typename P = Passable>
struct ForwardBiased : public P
{
ForwardBiased(const Grid<int> &f) : flow(f) { }
int operator () (const Location &loc) const
{
if (!P::operator()(loc))
return 0;
else if (flow(loc) <= origin)
return 1;
else
return B;
}
void setOrigin(const Location &loc)
{
origin = flow(loc);
}
private:
const Grid<int> &flow;
int origin;
};
struct UnitCost
{
inline bool operator () (const Location &loc) const
{
return 1;
}
};
struct NarrowCost
{
inline int operator () (const Location &loc) const
{
// OPEN WALL GAP/CORNER NICHE STUCK
const int costs[] = { 1, 2, 20, 50, 1000 };
const Square &square = state.grid(loc);
return costs[int(square.byWater)];
}
};
struct NarrowBusyCost : NarrowCost
{
inline int operator () (const Location &loc) const
{
const Square &square = state.grid(loc);
if (square.stationary)
return NarrowCost::operator()(loc) + 4;
else
return 1;
}
};
std::ostream& operator<<(std::ostream &os, const State &state);
std::istream& operator>>(std::istream &is, State &state);
#include <iostream>
#include <iomanip>
template <typename T>
std::ostream& operator<<(std::ostream &os, const Grid<T> &grid)
{
os << std::hex;
for (int r = 0; r < state.rows; ++r) {
for (int c = 0; c < state.cols; ++c) {
if (grid(r,c) == 9999)
os << " []";
else if (grid(r,c) >= 1000)
os << " __";
else if (grid(r,c) > 0xff)
os << " xx";
else
os << std::setw(3) << grid(r,c);
}
os << std::endl;
}
os << std::dec;
return os;
}
template <typename I, typename J>
void paint(Grid<bool> &grid, I begin, I end, J nbegin, J nend)
{
for (; begin != end; ++begin)
for (J n = nbegin; n != nend; ++n)
grid(state.deltaLocation(*begin, (*n).row, (*n).col)) = true;
}
#endif //STATE_H_