Horizon Official Technical Documentation
AStarTest.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <cstring>
#include <fstream>
#include <cstdint>
#include <cmath>
#include "Server/Zone/Game/Map/Path/AStar.hpp"
#include "Server/Zone/Game/Map/Grid/Cell/Cell.hpp"
#include "Server/Zone/Game/Map/Grid/GridDefinitions.hpp"
#include "prontera.cpp"
+ Include dependency graph for AStarTest.cpp:

Macros

#define BOOST_TEST_MODULE   "AStarTest"
 
#define MAP_WIDTH   268
 
#define MAP_HEIGHT   300
 

Functions

bool check_collision (int16_t x, int16_t y)
 
 BOOST_AUTO_TEST_CASE (AStarTest)
 

Variables

Cell cell [MAP_WIDTH][MAP_HEIGHT]
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   "AStarTest"

◆ MAP_HEIGHT

#define MAP_HEIGHT   300

◆ MAP_WIDTH

#define MAP_WIDTH   268

Function Documentation

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( AStarTest  )
77{
79 astar.setHeuristic(&AStar::Heuristic::manhattan);
80
81 std::srand(std::time(nullptr));
82
83 int total_time = 0;
84 int idx = 0;
85 // maps are stored from max-y,x down to 0,0
86 // we store in cell[][] starting from 0,0 to max-y 0
87 for (int y = MAP_HEIGHT - 1; y >= 0; y--) {
88 for (int x = 0; x < MAP_WIDTH; ++x) {
89 cell[x][y] = Cell(prontera[idx++]);
90 }
91 }
92
93 std::vector<std::pair<MapCoords, MapCoords>> start_end;
94 for (int i = 0; i < 10000; i++) {
95 MapCoords start, end;
96
97 do {
98 start = MapCoords( rand() % MAP_WIDTH - 1, rand() % MAP_HEIGHT - 1 );
99 } while(check_collision(start.x(), start.y()));
100
101 std::vector<MapCoords> path;
102 do {
103 do {
104 end = MapCoords( rand() % MAP_WIDTH - 1, rand() % MAP_HEIGHT - 1 );
105 } while(check_collision(end.x(), end.y()));
106 } while (!start.is_within_range(end, MAX_VIEW_RANGE) && ((path = astar.findPath(start, end)).size() == 0 || (path.at(0).x() != end.x() && path.at(0).y() != end.y())));
107
108 start_end.push_back(std::make_pair(start, end));
109 //std::cout << "Generated " << i << " start: " << start.x() << ", " << start.y() << " end: " << end.x() << ", " << end.y() << std::endl;
110 }
111
112 for (int i = 0; i < 10000; i++) {
113 MapCoords start = start_end[i].first;
114 MapCoords end = start_end[i].second;
115
116 auto start_time = std::chrono::high_resolution_clock::now();
117 auto path = astar.findPath(start, end);
118 auto finish_time = std::chrono::high_resolution_clock::now();
119 auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(finish_time - start_time);
120 bool path_found = path.size() != 0;
121 //printf("Manhattan: %lldus %d %s size: %lld (%d, %d) -> (%d, %d)\n", elapsed.count(), i, path_found ? "path found" : "path not found", path.size(), start.x(), start.y(), end.x(), end.y());
122 total_time += elapsed.count();
123 if (path.size() == 0)
124 BOOST_FAIL("Path not found");
125#ifdef PRINT_FILE
126 std::ofstream mapfile;
127 char filename[100];
128 snprintf(filename, 100, "izlude-%d.txt", i);
129 mapfile.open(filename);
130 for (int y = MAP_HEIGHT - 1; y >= 0; --y) {
131 for (int x = 0; x < MAP_WIDTH; ++x) {
132 MapCoords coords = MapCoords(x, y);
133 bool found = false;
134
135 for (auto c : path) {
136 if (coords == c) {
137 if (start.x() == x && start.y() == y)
138 mapfile << "@ (" << x << ", " << y << ")";
139 else if (end.x() == x && end.y() == y)
140 mapfile << "T (" << x << ", " << y << ")";
141 else if (c.x() == x && c.y() == y)
142 mapfile << "^";
143 found = true;
144 }
145 }
146
147 if (!found) {
148 if (start.x() == x && start.y() == y)
149 mapfile << "@ (" << x << ", " << y << ")";
150 else if (end.x() == x && end.y() == y)
151 mapfile << "T (" << x << ", " << y << ")";
152 else if (cell[x][y].isWalkable())
153 mapfile << " ";
154 else
155 mapfile << "|";
156 }
157 }
158
159 mapfile << "\n";
160 }
161
162 mapfile.close();
163#endif
164 }
165 std::cout << "total time: " << total_time << std::endl;
166
167// start_time = std::chrono::high_resolution_clock::now();
168// astar.setHeuristic(&AStar::Heuristic::octagonal);
169// astar.navigate(start, end);
170// finish_time = std::chrono::high_resolution_clock::now();
171// elapsed = finish_time - start_time;
172// printf("Octogonal: %.2fs\n", elapsed.count());
173
174 //BOOST_ASSERT(path->size() > 1);
175}
#define MAP_HEIGHT
Definition: AStarTest.cpp:48
Cell cell[MAP_WIDTH][MAP_HEIGHT]
Definition: AStarTest.cpp:52
#define MAP_WIDTH
Definition: AStarTest.cpp:47
bool check_collision(int16_t x, int16_t y)
Definition: AStarTest.cpp:54
Coordinates< MAX_CELLS_PER_MAP > MapCoords
Definition: GridDefinitions.hpp:83
#define MAX_VIEW_RANGE
Definition: Horizon.hpp:59
int16_t y() const
Definition: Coordinates.hpp:120
bool is_within_range(Coordinates< BOUNDS > const &bounds, int range) const
Definition: Coordinates.hpp:82
int16_t x() const
Definition: Coordinates.hpp:119
Definition: AStar.hpp:99
void setHeuristic(const HeuristicFunction &heuristic_)
Definition: AStar.hpp:137
static uint32_t manhattan(MapCoords source_, MapCoords target_)
Definition: AStar.hpp:55
uint8_t prontera[122304]
Definition: prontera.cpp:4
Definition: Cell.hpp:42

References cell, check_collision(), Coordinates< MAX_COORDINATES >::is_within_range(), Horizon::Zone::AStar::Heuristic::manhattan(), MAP_HEIGHT, MAP_WIDTH, MAX_VIEW_RANGE, prontera, Horizon::Zone::AStar::Generator::setHeuristic(), Coordinates< MAX_COORDINATES >::x(), and Coordinates< MAX_COORDINATES >::y().

+ Here is the call graph for this function:

◆ check_collision()

bool check_collision ( int16_t  x,
int16_t  y 
)
55{
56 if (x < 0 || y < 0 ||
57 x >= MAP_WIDTH || y >= MAP_HEIGHT)
58 return true;
59
60 return cell[x][y].isWalkable() ? false : true;
61}
bool isWalkable()
Definition: Cell.hpp:87

References cell, Horizon::Zone::Cell::isWalkable(), MAP_HEIGHT, and MAP_WIDTH.

Referenced by BOOST_AUTO_TEST_CASE().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ cell