/
Tree.hpp
83 lines (62 loc) · 1.66 KB
/
Tree.hpp
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
/// Author: Matthew Lowe
/// Copyright (c) 2018
#pragma once
#include <memory>
#include <vector>
#include <list>
#include <set>
#include <map>
#include "Vec3.hpp"
/*
Map node
Stores values for use in the algorithms as well as it's world position
*/
struct SNode
{
Vec2<> mPos;
int mCost;
int mScore;
int mEstimate;
SNode(Vec2<> p, int c) : mPos(p), mCost(c) {};
};
/*
Tree class to store the nodes and do operations on them
*/
class CTree
{
public:
typedef std::shared_ptr<SNode> Node;
CTree(Vec2<size_t> dims, std::vector<std::vector<Node>> &grid);
Node SetNode(unsigned x, unsigned y, Vec2<> pos, int cost = 1);
Node GetNode(unsigned x, unsigned y);
Node FindNode(Vec2<> pos);
Vec2<size_t> GetGridSize() { return Vec2<size_t>(mWidth, mHeight); }
private:
std::vector<std::vector<Node>> &mGrid;
size_t mWidth, mHeight;
};
/*
Abstract class for a search algorithm
*/
class CSearchAlgorithm
{
public:
enum EStatus { Found, Searching, Failed };
CSearchAlgorithm(CTree &t, bool diag = false) : mTree(t), mUseDiag(diag) {}
virtual void Start(CTree::Node start, CTree::Node goal) = 0;
virtual int Step() = 0;
std::vector<Vec2<>> GetPath() { return mPath; }
int GetNumSearches() { return mNumSearches; }
void UseDiagonals(bool diag) { mUseDiag = diag; }
std::set<CTree::Node> GetOpenList() { return mOpenList; }
std::set<CTree::Node> GetClosedList() { return mClosedList; }
protected:
CTree &mTree;
CTree::Node mStartNode, mGoalNode;
std::vector<Vec2<>> mPath;
std::set<CTree::Node> mOpenList;
std::set<CTree::Node> mClosedList;
int mNumSearches;
bool mUseDiag;
int Heuristic(CTree::Node start, CTree::Node end);
};