-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOctileGrid.cpp
More file actions
executable file
·107 lines (101 loc) · 2.51 KB
/
OctileGrid.cpp
File metadata and controls
executable file
·107 lines (101 loc) · 2.51 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
#include "OctileGrid.h"
#include <fstream>
#include <iostream>
#include <string>
OctileGrid::OctileGrid(int width, int height)
{
this->width = width;
this->height = height;
const char* filename1 = "/Users/dcruwys/ClionProjects/pathfinder_skynet/lak303d.map";
std::string buffer;
std::ifstream read;
read.open(filename1);
if (read.is_open())
{
buffer.clear();
int i = 0;
int k = 0;
while (getline(read, buffer))
{
if (i > 3)
{
for (int j = 0; j < 194; j++)
{
map[k][j] = buffer[j];
}
k++;
buffer.clear();
}
i++;
}
}
read.close();
}
void OctileGrid::GetActions(Coordinate &nodeID, std::vector<Action> &actions)
{
actions.clear();
if (nodeID.x < width - 1 && map[nodeID.y][nodeID.x + 1] == '.')
{
actions.push_back(Action::RIGHT);
}
if (nodeID.x > 0 && map[nodeID.y][nodeID.x - 1] == '.')
{
actions.push_back(Action::LEFT);
}
if (nodeID.y < height - 1 && map[nodeID.y - 1][nodeID.x] == '.')
{
actions.push_back(Action::UP);
}
if (nodeID.y > 0 && map[nodeID.y + 1][nodeID.x] == '.')
{
actions.push_back(Action::DOWN);
}
if (nodeID.x < width - 1 && nodeID.y < height && map[nodeID.y - 1][nodeID.x + 1] == '.')
{
actions.push_back(Action::UPRIGHT);
}
if (nodeID.x > 0 && nodeID.y < height - 1 && map[nodeID.y - 1][nodeID.x - 1] == '.')
{
actions.push_back(Action::UPLEFT);
}
if (nodeID.x < width - 1 && nodeID.y > 0 && map[nodeID.y + 1][nodeID.x + 1] == '.')
{
actions.push_back(Action::DOWNRIGHT);
}
if (nodeID.x > 0 && nodeID.y > 0 && map[nodeID.y + 1][nodeID.x - 1] == '.')
{
actions.push_back(Action::DOWNLEFT);
}
}
void OctileGrid::ApplyAction(Coordinate &s, Action a)
{
switch (a)
{
case Action::RIGHT: s.x++; break;
case Action::LEFT: s.x--; break;
case Action::UP: s.y--; break;
case Action::DOWN: s.y++; break;
case Action::UPRIGHT: s.x++; s.y--; break;
case Action::UPLEFT: s.x--; s.y--; break;
case Action::DOWNRIGHT: s.x++; s.y++; break;
case Action::DOWNLEFT: s.x--; s.y++; break;
}
}
void OctileGrid::UndoAction(Coordinate &s, Action a)
{
switch (a)
{
case Action::RIGHT: s.x--; break;
case Action::LEFT: s.x++; break;
case Action::UP: s.y++; break;
case Action::DOWN: s.y--; break;
case Action::UPRIGHT: s.x--; s.y++; break;
case Action::UPLEFT: s.x++; s.y++; break;
case Action::DOWNRIGHT: s.x--; s.y--; break;
case Action::DOWNLEFT: s.x++; s.y--; break;
}
}
uint64_t OctileGrid::Rank(Coordinate &s)
{
return s.x * height + s.y;
}