Pioneer
Loading...
Searching...
No Matches
SystemPath.h
Go to the documentation of this file.
1// Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifndef _SYSTEMPATH_H
5#define _SYSTEMPATH_H
6
7#include "JsonFwd.h"
8#include "lua/LuaWrappable.h"
9#include <SDL_stdinc.h>
10#include <cassert>
11#include <stdexcept>
12
13class SystemPath : public LuaWrappable {
14public:
15 struct ParseFailure : public std::invalid_argument {
17 std::invalid_argument("invalid SystemPath format") {}
18 };
19 static SystemPath Parse(const char *const str);
20
22 sectorX(0),
23 sectorY(0),
24 sectorZ(0),
25 systemIndex(Uint32(-1)),
26 bodyIndex(Uint32(-1)) {}
27
28 SystemPath(Sint32 x, Sint32 y, Sint32 z) :
29 sectorX(x),
30 sectorY(y),
31 sectorZ(z),
32 systemIndex(Uint32(-1)),
33 bodyIndex(Uint32(-1)) {}
34 SystemPath(Sint32 x, Sint32 y, Sint32 z, Uint32 si) :
35 sectorX(x),
36 sectorY(y),
37 sectorZ(z),
38 systemIndex(si),
39 bodyIndex(Uint32(-1)) {}
40 SystemPath(Sint32 x, Sint32 y, Sint32 z, Uint32 si, Uint32 bi) :
41 sectorX(x),
42 sectorY(y),
43 sectorZ(z),
44 systemIndex(si),
45 bodyIndex(bi) {}
46
47 SystemPath(const SystemPath *path) :
48 sectorX(path->sectorX),
49 sectorY(path->sectorY),
50 sectorZ(path->sectorZ),
52 bodyIndex(path->bodyIndex) {}
53
54 Sint32 sectorX;
55 Sint32 sectorY;
56 Sint32 sectorZ;
58 Uint32 bodyIndex;
59
60 friend bool operator==(const SystemPath &a, const SystemPath &b)
61 {
62 if (a.sectorX != b.sectorX) return false;
63 if (a.sectorY != b.sectorY) return false;
64 if (a.sectorZ != b.sectorZ) return false;
65 if (a.systemIndex != b.systemIndex) return false;
66 if (a.bodyIndex != b.bodyIndex) return false;
67 return true;
68 }
69
70 friend bool operator!=(const SystemPath &a, const SystemPath &b)
71 {
72 return !(a == b);
73 }
74
75 friend bool operator<(const SystemPath &a, const SystemPath &b)
76 {
77 if (a.sectorX != b.sectorX) return (a.sectorX < b.sectorX);
78 if (a.sectorY != b.sectorY) return (a.sectorY < b.sectorY);
79 if (a.sectorZ != b.sectorZ) return (a.sectorZ < b.sectorZ);
80 if (a.systemIndex != b.systemIndex) return (a.systemIndex < b.systemIndex);
81 return (a.bodyIndex < b.bodyIndex);
82 }
83
84 static inline double SectorDistance(const SystemPath &a, const SystemPath &b)
85 {
86 const Sint32 x = b.sectorX - a.sectorX;
87 const Sint32 y = b.sectorY - a.sectorY;
88 const Sint32 z = b.sectorZ - b.sectorZ;
89 return sqrt(x * x + y * y + z * z); // sqrt is slow
90 }
91
92 static inline double SectorDistanceSqr(const SystemPath &a, const SystemPath &b)
93 {
94 const Sint32 x = b.sectorX - a.sectorX;
95 const Sint32 y = b.sectorY - a.sectorY;
96 const Sint32 z = b.sectorZ - b.sectorZ;
97 return (x * x + y * y + z * z); // return the square of the distance
98 }
99
101 public:
102 bool operator()(const SystemPath &a, const SystemPath &b) const
103 {
104 if (a.sectorX != b.sectorX) return (a.sectorX < b.sectorX);
105 if (a.sectorY != b.sectorY) return (a.sectorY < b.sectorY);
106 return (a.sectorZ < b.sectorZ);
107 }
108 };
109
111 public:
112 bool operator()(const SystemPath &a, const SystemPath &b) const
113 {
114 if (a.sectorX != b.sectorX) return (a.sectorX < b.sectorX);
115 if (a.sectorY != b.sectorY) return (a.sectorY < b.sectorY);
116 if (a.sectorZ != b.sectorZ) return (a.sectorZ < b.sectorZ);
117 return (a.systemIndex < b.systemIndex);
118 }
119 };
120
121 bool IsSectorPath() const
122 {
123 return (systemIndex == Uint32(-1) && bodyIndex == Uint32(-1));
124 }
125
126 bool IsSystemPath() const
127 {
128 return (systemIndex != Uint32(-1) && bodyIndex == Uint32(-1));
129 }
130 bool HasValidSystem() const
131 {
132 return (systemIndex != Uint32(-1));
133 }
134
135 bool IsBodyPath() const
136 {
137 return (systemIndex != Uint32(-1) && bodyIndex != Uint32(-1));
138 }
139 bool HasValidBody() const
140 {
141 assert((bodyIndex == Uint32(-1)) || (systemIndex != Uint32(-1)));
142 return (bodyIndex != Uint32(-1));
143 }
144
145 bool IsSameSector(const SystemPath &b) const
146 {
147 if (sectorX != b.sectorX) return false;
148 if (sectorY != b.sectorY) return false;
149 if (sectorZ != b.sectorZ) return false;
150 return true;
151 }
152
153 bool IsSameSystem(const SystemPath &b) const
154 {
155 assert(HasValidSystem());
156 assert(b.HasValidSystem());
157 if (sectorX != b.sectorX) return false;
158 if (sectorY != b.sectorY) return false;
159 if (sectorZ != b.sectorZ) return false;
160 if (systemIndex != b.systemIndex) return false;
161 return true;
162 }
163
165 {
167 }
168
170 {
171 assert(systemIndex != Uint32(-1));
173 }
174
175 void ToJson(Json &jsonObj) const;
176 static SystemPath FromJson(const Json &jsonObj);
177
178 // sometimes it's useful to be able to get the SystemPath data as a blob
179 // (for example, to be used for hashing)
180 // see, LuaObject<SystemPath>::PushToLua in LuaSystemPath.cpp
181 static_assert(sizeof(Sint32) == sizeof(Uint32), "something crazy is going on!");
182 static const size_t SizeAsBlob = 5 * sizeof(Uint32);
183 void SerializeToBlob(char *blob) const
184 {
185 // could just memcpy(blob, this, sizeof(SystemPath))
186 // but that might include packing and/or vtable pointer
187 memcpy(blob + 0 * sizeof(Uint32), &sectorX, sizeof(Uint32));
188 memcpy(blob + 1 * sizeof(Uint32), &sectorY, sizeof(Uint32));
189 memcpy(blob + 2 * sizeof(Uint32), &sectorZ, sizeof(Uint32));
190 memcpy(blob + 3 * sizeof(Uint32), &systemIndex, sizeof(Uint32));
191 memcpy(blob + 4 * sizeof(Uint32), &bodyIndex, sizeof(Uint32));
192 }
193};
194
195std::string to_string(const SystemPath &path);
196
197#endif
nlohmann::json Json
Definition Json.h:8
std::string to_string(const SystemPath &path)
Definition SystemPath.cpp:97
Definition LuaWrappable.h:13
Definition SystemPath.h:100
bool operator()(const SystemPath &a, const SystemPath &b) const
Definition SystemPath.h:102
Definition SystemPath.h:110
bool operator()(const SystemPath &a, const SystemPath &b) const
Definition SystemPath.h:112
Definition SystemPath.h:13
SystemPath(Sint32 x, Sint32 y, Sint32 z)
Definition SystemPath.h:28
static SystemPath Parse(const char *const str)
Definition SystemPath.cpp:36
static double SectorDistanceSqr(const SystemPath &a, const SystemPath &b)
Definition SystemPath.h:92
SystemPath SystemOnly() const
Definition SystemPath.h:169
SystemPath()
Definition SystemPath.h:21
static double SectorDistance(const SystemPath &a, const SystemPath &b)
Definition SystemPath.h:84
SystemPath(Sint32 x, Sint32 y, Sint32 z, Uint32 si)
Definition SystemPath.h:34
bool IsSameSector(const SystemPath &b) const
Definition SystemPath.h:145
Sint32 sectorZ
Definition SystemPath.h:56
friend bool operator==(const SystemPath &a, const SystemPath &b)
Definition SystemPath.h:60
Uint32 systemIndex
Definition SystemPath.h:57
bool HasValidSystem() const
Definition SystemPath.h:130
Sint32 sectorX
Definition SystemPath.h:54
friend bool operator!=(const SystemPath &a, const SystemPath &b)
Definition SystemPath.h:70
static SystemPath FromJson(const Json &jsonObj)
Definition SystemPath.cpp:80
SystemPath(const SystemPath *path)
Definition SystemPath.h:47
bool IsSystemPath() const
Definition SystemPath.h:126
Uint32 bodyIndex
Definition SystemPath.h:58
bool IsBodyPath() const
Definition SystemPath.h:135
bool IsSameSystem(const SystemPath &b) const
Definition SystemPath.h:153
SystemPath SectorOnly() const
Definition SystemPath.h:164
void SerializeToBlob(char *blob) const
Definition SystemPath.h:183
friend bool operator<(const SystemPath &a, const SystemPath &b)
Definition SystemPath.h:75
SystemPath(Sint32 x, Sint32 y, Sint32 z, Uint32 si, Uint32 bi)
Definition SystemPath.h:40
static const size_t SizeAsBlob
Definition SystemPath.h:182
Sint32 sectorY
Definition SystemPath.h:55
bool HasValidBody() const
Definition SystemPath.h:139
void ToJson(Json &jsonObj) const
Definition SystemPath.cpp:69
bool IsSectorPath() const
Definition SystemPath.h:121
Definition SystemPath.h:15
ParseFailure()
Definition SystemPath.h:16