blob: 95ac3b131a7e1a17c8dfce0b3e82d27fa8a53057 (
plain)
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
|
#include "PathfindingContext.h"
#include "XY.h"
#include "Museum.h"
#include "NullTileBehavior.h"
#include "ToggleMuseumPauseCommand.h"
#include "DijkstraPathfinder.h"
#include "BreadthFirstPathfinder.h"
using namespace std;
PathfindingContext::PathfindingContext(Museum & m) : museum(m) {
this->solvers.push_back(unique_ptr<Pathfinder>(new DijkstraPathfinder(m)));
this->solvers.push_back(unique_ptr<Pathfinder>(new BreadthFirstPathfinder(m)));
}
void PathfindingContext::cycle_solver() {
this->solver_index = (this->solver_index + 1) % this->solvers.size();
this->update();
}
Pathfinder & PathfindingContext::get_solver() {
return *this->solvers[this->solver_index];
}
void PathfindingContext::set_start(const XY & point) {
if (this->empty_point(point)) return;
this->start_point = point;
this->update();
}
void PathfindingContext::set_end(const XY & point) {
if (this->empty_point(point)) return;
this->end_point = point;
this->update();
}
bool PathfindingContext::empty_point(const XY & point) {
try {
// check if square is empty (has null behavior)
Tile & tile = this->museum.canvas.get_tile(point);
TileBehavior * behavior = tile.behavior.get();
if (dynamic_cast<NullTileBehavior *>(behavior) != nullptr) return true;
} catch (...) {
// get_tile throws an exception if the point is outside the canvas bounds
return true;
}
return false;
}
void PathfindingContext::update() {
bool valid = true;
if (this->empty_point(this->start_point)) {
this->start_point = { -1, -1 };
valid = false;
}
if (this->empty_point(this->end_point)) {
this->end_point = { -1, -1 };
valid = false;
}
if (!valid) return;
ToggleMuseumPauseCommand(this->museum, true).execute();
Pathfinder & solver = this->get_solver();
solver.find_between(this->start_point, this->end_point);
}
void PathfindingContext::register_weight(const string & type, unsigned int weight) {
this->weight_map[type] = weight;
}
unsigned int PathfindingContext::get_weight(const string & type) {
if (this->weight_map.contains(type))
return this->weight_map[type];
return 0;
}
|