better UniverseMap checks

This commit is contained in:
Kae 2024-03-18 17:34:02 +11:00
parent 7d717489a9
commit 5d1e118a19

View File

@ -103,23 +103,30 @@ void PlayerUniverseMap::invalidateWarpAction(WarpAction const& warpAction) {
} }
Maybe<OrbitBookmark> PlayerUniverseMap::worldBookmark(CelestialCoordinate const& world) const { Maybe<OrbitBookmark> PlayerUniverseMap::worldBookmark(CelestialCoordinate const& world) const {
for (auto bookmark : universeMap().systems.get(world.location()).bookmarks) { if (auto systemMap = universeMap().systems.ptr(world.location())) {
if (bookmark.target == world) for (auto& bookmark : systemMap->bookmarks) {
return bookmark; if (bookmark.target == world)
return bookmark;
}
} }
return {}; return {};
} }
List<OrbitBookmark> PlayerUniverseMap::systemBookmarks(CelestialCoordinate const& system) const { List<OrbitBookmark> PlayerUniverseMap::systemBookmarks(CelestialCoordinate const& system) const {
return universeMap().systems.get(system.location()).bookmarks.values(); if (auto systemMap = universeMap().systems.ptr(system.location()))
return systemMap->bookmarks.values();
return {};
} }
List<OrbitBookmark> PlayerUniverseMap::planetBookmarks(CelestialCoordinate const& planet) const { List<OrbitBookmark> PlayerUniverseMap::planetBookmarks(CelestialCoordinate const& planet) const {
return universeMap().systems.get(planet.location()).bookmarks.values().filtered([planet] (OrbitBookmark const& bookmark) { if (auto systemMap = universeMap().systems.ptr(planet.location())) {
return systemMap->bookmarks.values().filtered([planet](OrbitBookmark const& bookmark) {
if (auto coordinate = bookmark.target.maybe<CelestialCoordinate>()) if (auto coordinate = bookmark.target.maybe<CelestialCoordinate>())
return coordinate->planet().orbitNumber() == planet.planet().orbitNumber(); return coordinate->planet().orbitNumber() == planet.planet().orbitNumber();
return false; return false;
}); });
}
return {};
} }
bool PlayerUniverseMap::isMapped(CelestialCoordinate const& coordinate) { bool PlayerUniverseMap::isMapped(CelestialCoordinate const& coordinate) {
@ -127,67 +134,52 @@ bool PlayerUniverseMap::isMapped(CelestialCoordinate const& coordinate) {
return false; return false;
auto& universeMap = m_universeMaps[*m_serverUuid]; auto& universeMap = m_universeMaps[*m_serverUuid];
if (!universeMap.systems.contains(coordinate.location())) if (auto systemMap = universeMap.systems.ptr(coordinate.location()))
return coordinate.isSystem() || systemMap->mappedPlanets.contains(coordinate.planet());
else
return false; return false;
if (coordinate.isSystem())
return true;
return universeMap.systems[coordinate.location()].mappedPlanets.contains(coordinate.planet());
} }
HashMap<Uuid, PlayerUniverseMap::MappedObject> PlayerUniverseMap::mappedObjects(CelestialCoordinate const& system) { HashMap<Uuid, PlayerUniverseMap::MappedObject> PlayerUniverseMap::mappedObjects(CelestialCoordinate const& system) {
auto& universeMap = m_universeMaps[*m_serverUuid]; auto& universeMap = m_universeMaps[*m_serverUuid];
if (!universeMap.systems.contains(system.location())) if (auto systemMap = universeMap.systems.ptr(system.location()))
return systemMap->mappedObjects;
else
return {}; return {};
return universeMap.systems[system.location()].mappedObjects;
} }
void PlayerUniverseMap::addMappedCoordinate(CelestialCoordinate const& coordinate) { void PlayerUniverseMap::addMappedCoordinate(CelestialCoordinate const& coordinate) {
if (coordinate.isNull()) if (coordinate.isNull() || coordinate.isSystem())
return; return;
auto& universeMap = m_universeMaps[*m_serverUuid]; auto& universeMap = m_universeMaps[*m_serverUuid];
if (!universeMap.systems.contains(coordinate.location()))
universeMap.systems.set(coordinate.location(), SystemMap());
if (coordinate.isSystem())
return;
universeMap.systems[coordinate.location()].mappedPlanets.add(coordinate.planet()); universeMap.systems[coordinate.location()].mappedPlanets.add(coordinate.planet());
} }
void PlayerUniverseMap::addMappedObject(CelestialCoordinate const& system, Uuid const& uuid, String const& typeName, Maybe<CelestialOrbit> const& orbit, JsonObject parameters) { void PlayerUniverseMap::addMappedObject(CelestialCoordinate const& system, Uuid const& uuid, String const& typeName, Maybe<CelestialOrbit> const& orbit, JsonObject parameters) {
auto& universeMap = m_universeMaps[*m_serverUuid];
if (!universeMap.systems.contains(system.location()))
universeMap.systems.set(system.location(), SystemMap());
MappedObject object { MappedObject object {
typeName, typeName,
orbit, orbit,
parameters parameters
}; };
auto& universeMap = m_universeMaps[*m_serverUuid];
universeMap.systems[system.location()].mappedObjects.set(uuid, object); universeMap.systems[system.location()].mappedObjects.set(uuid, object);
} }
void PlayerUniverseMap::removeMappedObject(CelestialCoordinate const& system, Uuid const& uuid) { void PlayerUniverseMap::removeMappedObject(CelestialCoordinate const& system, Uuid const& uuid) {
auto& universeMap = m_universeMaps[*m_serverUuid]; auto& universeMap = m_universeMaps[*m_serverUuid];
if (!universeMap.systems.contains(system.location())) if (auto systemMap = universeMap.systems.ptr(system.location()))
return; systemMap->mappedObjects.remove(uuid);
universeMap.systems[system.location()].mappedObjects.remove(uuid);
} }
void PlayerUniverseMap::filterMappedObjects(CelestialCoordinate const& system, List<Uuid> const& allowed) { void PlayerUniverseMap::filterMappedObjects(CelestialCoordinate const& system, List<Uuid> const& allowed) {
auto& universeMap = m_universeMaps[*m_serverUuid]; auto& universeMap = m_universeMaps[*m_serverUuid];
if (!universeMap.systems.contains(system.location())) if (auto systemMap = universeMap.systems.ptr(system.location())) {
return; auto& objects = systemMap->mappedObjects;
for (auto& uuid : objects.keys()) {
auto& objects = universeMap.systems[system.location()].mappedObjects; if (!allowed.contains(uuid))
for (auto uuid : objects.keys()) { objects.remove(uuid);
if (!allowed.contains(uuid)) }
objects.remove(uuid);
} }
} }