nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
paths.h
Go to the documentation of this file.
1#ifndef NANOMMO_TERRAIN_PATHS_H_
2#define NANOMMO_TERRAIN_PATHS_H_
3
4#include <cstdint>
5#include <vector>
6
7#include "terrain/lands.h"
8#include "terrain/terrain.h"
9
10namespace nanommo {
11
12/// The ways across a land: which of its metre squares a player can walk on,
13/// and the shortest way from one place to another over them, which A* finds.
14/// A world finds a way each time a player asks to walk somewhere, between
15/// ticks, so a search looks at kMostSearched squares at most, and then goes as
16/// near as it got.
17class Paths {
18 public:
19 /// In metres along each side.
20 static constexpr float kSquare = 1.0f;
21
22 /// A little under a sixth of the island's squares.
23 static constexpr int kMostSearched = 40000;
24
25 explicit Paths(const Terrain& terrain);
26
27 /// The way from one place to the other: the places to walk to straight,
28 /// one after the other, the last of them to, or the place nearest it that
29 /// the search reached. Empty when there's no way to go: to isn't somewhere
30 /// a player can stand, or from is off the land.
31 std::vector<Place> Find(Place from, Place to);
32
33 /// Whether a straight walk from one place to the other stays on squares a
34 /// player can walk on.
35 bool Clear(Place from, Place to) const;
36
37 /// Whether the square the place is in is one a player can walk on.
38 bool Open(Place place) const;
39
40 /// How many squares the last search looked at.
41 int searched() const { return searched_; }
42
43 private:
44 /// The square the place is in, by its index, or -1 off the land.
45 int SquareOf(Place place) const;
46 Place Middle(int square) const;
47 /// Along the shortest way there could be, around nothing.
48 float Estimate(int square, int goal) const;
49 /// The squares, as places, cut down to the places where the way turns, so
50 /// that each is in sight of the one before.
51 std::vector<Place> Straighten(Place from,
52 const std::vector<Place>& squares) const;
53
54 int size_ = 0;
55 float half_ = 0.0f;
56 std::vector<bool> open_;
57 // What the searches work with, kept from one to the next, so that a search
58 // doesn't clear or allocate a quarter of a million of each: a square's cost
59 // and where it was reached from count only while its stamp is the number of
60 // the search going on.
61 std::vector<float> cost_;
62 std::vector<int> came_from_;
63 std::vector<uint32_t> stamp_;
64 uint32_t search_ = 0;
65 int searched_ = 0;
66};
67
68} // namespace nanommo
69
70#endif // NANOMMO_TERRAIN_PATHS_H_
static constexpr float kSquare
In metres along each side.
Definition paths.h:20
bool Clear(Place from, Place to) const
Definition paths.cc:75
int searched() const
How many squares the last search looked at.
Definition paths.h:41
std::vector< Place > Find(Place from, Place to)
Definition paths.cc:100
bool Open(Place place) const
Whether the square the place is in is one a player can walk on.
Definition paths.cc:70
static constexpr int kMostSearched
A little under a sixth of the island's squares.
Definition paths.h:23
Paths(const Terrain &terrain)
Definition paths.cc:30
Definition chat.cc:9
In metres from the middle of a land.
Definition lands.h:13