FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
extra_pathFinding.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_EXTRA_PATHFINDING_HPP_INCLUDED
18#define _FGE_EXTRA_PATHFINDING_HPP_INCLUDED
19
20/*
21 * Original from : https://github.com/daancode/a-star
22 * Copyright (c) 2015, Damian Barczynski <daan.net@wp.eu>
23 *
24 * Altered/Modified by Guillaume Guillet
25 */
26
27#include "FastEngine/fge_extern.hpp"
28#include "FastEngine/C_vector.hpp"
29#include <array>
30#include <cstdint>
31#include <optional>
32#include <unordered_map>
33#include <unordered_set>
34#include <vector>
35
36namespace fge::AStar
37{
38
39using HeuristicFunction = unsigned int (*)(fge::Vector2i, fge::Vector2i);
40using CoordinateList = std::vector<fge::Vector2i>;
41
43{
44 static_assert(sizeof(fge::Vector2i) == 8, "bad fge::Vector2i size, should be 8 !");
45
46 inline std::size_t operator()(fge::Vector2i const& coord) const
47 {
48 return std::hash<uint64_t>()(*reinterpret_cast<uint64_t const*>(&coord));
49 }
50};
51
52using CoordinateSet = std::unordered_set<fge::Vector2i, fge::AStar::Vector2iHash>;
53
54struct FGE_API Node
55{
56 explicit Node(std::optional<fge::Vector2i> parent = std::nullopt);
57 [[nodiscard]] unsigned int getScore() const;
58
59 unsigned int _costScore;
60 unsigned int _heuristicScore;
61 std::optional<fge::Vector2i> _parent;
62};
63
64using NodeMap = std::unordered_map<fge::Vector2i, fge::AStar::Node, fge::AStar::Vector2iHash>;
65
66class FGE_API Generator
67{
68public:
69 Generator();
70 ~Generator() = default;
71
72 void setWorldSize(fge::Vector2i worldSize);
73 [[nodiscard]] fge::Vector2i const& getWorldSize() const;
74
75 void setDiagonalMovement(bool enable);
76 void setHeuristic(HeuristicFunction heuristic);
77 CoordinateList findPath(fge::Vector2i source, fge::Vector2i target);
78 void addCollision(fge::Vector2i coord);
79 void removeCollision(fge::Vector2i coord);
80 void clearCollisions();
81
82private:
83 bool detectCollision(fge::Vector2i coord);
84
85 HeuristicFunction g_heuristic;
86 CoordinateSet g_walls;
87 fge::Vector2i g_worldSize;
88
89 std::array<fge::Vector2i, 8> const g_directions;
90 std::size_t g_directionsCount;
91};
92
93class FGE_API Heuristic
94{
95private:
96 static fge::Vector2i getDelta(fge::Vector2i source, fge::Vector2i target);
97
98public:
99 static unsigned int manhattan(fge::Vector2i source, fge::Vector2i target);
100 static unsigned int euclidean(fge::Vector2i source, fge::Vector2i target);
101 static unsigned int octagonal(fge::Vector2i source, fge::Vector2i target);
102};
103
104} // namespace fge::AStar
105
106#endif //_FGE_EXTRA_PATHFINDING_HPP_INCLUDED
Definition extra_pathFinding.hpp:67
Definition extra_pathFinding.hpp:94
Definition extra_pathFinding.hpp:55
Definition extra_pathFinding.hpp:43