Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarPlayerUniverseMap.hpp
blob: bf8efecabaf1fc6cdaec86c19e550e80e5548e3f (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once

#include "StarJson.hpp"
#include "StarWarping.hpp"
#include "StarCelestialCoordinate.hpp"
#include "StarSystemWorld.hpp"

namespace Star {

STAR_CLASS(PlayerUniverseMap);

template <typename T>
Json jsonFromBookmarkTarget(T const& target);

template <typename T>
T jsonToBookmarkTarget(Json const& json);

// Bookmark<T> requires T to implement jsonToBookmarkTarget<T> and jsonFromBookmarkTarget<T>
// also operator== and operator!=
template<typename T>
struct Bookmark {
  T target;
  String targetName;
  String bookmarkName;
  String icon;

  static Bookmark fromJson(Json const& json);
  Json toJson() const;

  bool operator==(Bookmark<T> const& rhs) const;
  bool operator!=(Bookmark<T> const& rhs) const;
  bool operator<(Bookmark<T> const& rhs) const;
};

typedef Variant<CelestialCoordinate, Uuid> OrbitTarget;
typedef pair<WorldId, SpawnTarget> TeleportTarget;

typedef Bookmark<OrbitTarget> OrbitBookmark;
typedef Bookmark<TeleportTarget> TeleportBookmark;


class PlayerUniverseMap {
public:
  struct MappedObject {
    String typeName;
    Maybe<CelestialOrbit> orbit;
    JsonObject parameters;
  };

  PlayerUniverseMap(Json const& json = {});

  Json toJson() const;

  // pair of system location and bookmark, not all orbit bookmarks include the system
  List<pair<Vec3I, OrbitBookmark>> orbitBookmarks() const;
  bool addOrbitBookmark(CelestialCoordinate const& system, OrbitBookmark const& bookmark);
  bool removeOrbitBookmark(CelestialCoordinate const& system, OrbitBookmark const& bookmark);

  List<TeleportBookmark> teleportBookmarks() const;
  bool addTeleportBookmark(TeleportBookmark bookmark);
  bool removeTeleportBookmark(TeleportBookmark const& bookmark);
  void invalidateWarpAction(WarpAction const& bookmark);

  Maybe<OrbitBookmark> worldBookmark(CelestialCoordinate const& world) const;
  List<OrbitBookmark> systemBookmarks(CelestialCoordinate const& system) const;
  List<OrbitBookmark> planetBookmarks(CelestialCoordinate const& planet) const;

  bool isMapped(CelestialCoordinate const& coordinate);
  HashMap<Uuid, MappedObject> mappedObjects(CelestialCoordinate const& system);

  void addMappedCoordinate(CelestialCoordinate const& coordinate);
  void addMappedObject(CelestialCoordinate const& system, Uuid const& uuid, String const& typeName, Maybe<CelestialOrbit> const& orbit = {}, JsonObject parameters = {});
  void removeMappedObject(CelestialCoordinate const& system, Uuid const& uuid);
  void filterMappedObjects(CelestialCoordinate const& system, List<Uuid> const& allowed);

  void setServerUuid(Maybe<Uuid> serverUuid);

private:
  struct SystemMap {
    Set<CelestialCoordinate> mappedPlanets;
    HashMap<Uuid, MappedObject> mappedObjects;
    Set<OrbitBookmark> bookmarks;

    static SystemMap fromJson(Json const& json);
    Json toJson() const;
  };
  struct UniverseMap {
    HashMap<Vec3I, SystemMap> systems;
    Set<TeleportBookmark> teleportBookmarks;

    static UniverseMap fromJson(Json const& json);
    Json toJson() const;
  };

  UniverseMap const& universeMap() const;
  UniverseMap& universeMap();

  Maybe<Uuid> m_serverUuid;
  HashMap<Uuid, UniverseMap> m_universeMaps;
};

template <typename T>
bool Bookmark<T>::operator==(Bookmark<T> const& rhs) const {
  return target == rhs.target;
}

template <typename T>
bool Bookmark<T>::operator!=(Bookmark<T> const& rhs) const {
  return target != rhs.target;
}

template <typename T>
bool Bookmark<T>::operator<(Bookmark<T> const& rhs) const {
  return target < rhs.target;
}

}