-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.h
More file actions
170 lines (149 loc) · 4.01 KB
/
State.h
File metadata and controls
170 lines (149 loc) · 4.01 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
#ifndef STATE_H_
#define STATE_H_
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#ifndef _WIN32
# include <tr1/memory>
#endif
//#include <xutility>
#include "Timer.h"
#include "Bug.h"
#include "Square.h"
#include "Location.h"
#include "pathfind/tiling.h"
/*
constants
*/
enum TDIRECTIONS
{
e_north = 0
, e_east = 1
, e_south = 2
, e_west = 3
, TDIRECTIONS_SIZE = 4
};
const char CDIRECTIONS[4] = {'N', 'E', 'S', 'W'};
//{N, E, S, W}
const int DIRECTIONS[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
const int COST_ONE = 100;
const int COST_SQRT2 = 142;
struct moves
{
Location * from;
Location * to;
TDIRECTIONS d;
int ant;
};
typedef std::list<moves> t_moves;
/*
struct to store current state information
*/
struct State
{
/*
Variables
*/
int rows, cols, turn, turns, noPlayers;
double attackradius, spawnradius, viewradius;
double loadtime, turntime;
std::vector<double> scores;
bool gameover;
t_moves _moves;
std::vector<std::vector<Square> > grid;
t_location_vector
myAnts
, enemyAnts
, myHills
, enemyHills
, food;
Location * seenHill;
std::tr1::shared_ptr<PathFind::Tiling> tiling;
Timer timer;
Bug bug;
/*
Functions
*/
State();
~State();
void setup();
void reset();
void makeMoves(Location &loc, Location &dest, TDIRECTIONS direction);
void endMoves();
inline int getHeuristic(const Location & start, const Location & target) const
{
int colStart = start.col;
int colTarget = target.col;
int rowStart = start.row;
int rowTarget = target.row;
int diffCol = abs(colTarget - colStart);
int diffRow = abs(rowTarget - rowStart);
// Vancouver distance
// See P.Yap: Grid-based Path-Finding (LNAI 2338 pp.44-55)
{
int correction = 0;
if (diffCol % 2 != 0)
{
if (rowTarget < rowStart)
correction = colTarget % 2;
else if (rowTarget > rowStart)
correction = colStart % 2;
}
// Note: formula in paper is wrong, corrected below.
int dist = std::max(0, diffRow - diffCol / 2 - correction) + diffCol;
return dist * COST_ONE;
}
return 1;
}
/// returns the euclidean distance between two locations
/// with the edges wrapped
inline double distance(const Location &loc1, const Location &loc2) const
{
int d1 = abs(loc1.row-loc2.row),
d2 = abs(loc1.col-loc2.col),
dr = std::min(d1, rows-d1),
dc = std::min(d2, cols-d2);
return sqrt(static_cast<double>(dr*dr + dc*dc));
}
inline int manhattan_method(const Location & l1, const Location & l2) const
{ return 2 * (abs(l1.row - l2.row) + abs(l1.col - l2.col)); }
//inline int manhattan_method(const Location & l, const Location & dest)
//{
// static int D(2)
// , D2(static_cast<int>(sqrt(2.0)*static_cast<double>(D)));
// int h_d = std::min(std::abs(l.col-dest.col), std::abs(l.row-dest.row));
// int h_s = (std::abs(l.col-dest.col) + std::abs(l.row-dest.row));
// return (D2 * h_d) + (D * (h_s - 2*h_d));
//}
/// returns the distance between two locations
/// with the edges wrapped
//inline int manhattan_method(const Location & loc1, const Location & loc2)
//{
// int d1 = abs(loc1.row-loc2.row),
// d2 = abs(loc1.col-loc2.col),
// dr = std::min(d1, rows-d1),
// dc = std::min(d2, cols-d2);
// return static_cast<int>(sqrt(static_cast<double>(dr*dr + dc*dc))*10.0);
//}
/// returns the new location from moving in a
/// given direction with the edges wrapped
//inline Location* State::getLocation(const Location &loc, int direction)
//{
// return &grid
// [((loc.row + DIRECTIONS[direction][0] + rows) % rows)]
// [((loc.col + DIRECTIONS[direction][1] + cols) % cols)].loc;
//}
inline Location* getLocation(const Location &startLoc, int direction)
{ return startLoc.around[direction]; }
TDIRECTIONS get_direction(const Location &start, const Location &dest);
void updateVisionInformation();
};
std::ostream& operator<<(std::ostream &os, const State &state);
std::istream& operator>>(std::istream &is, State &state);
#endif //STATE_H_