Lodestone.Level
Loading...
Searching...
No Matches
World.h
Go to the documentation of this file.
1//
2// Created by DexrnZacAttack on 10/15/25 using zPc-i2.
3//
4#ifndef LODESTONE_WORLD_H
5#define LODESTONE_WORLD_H
7
8#include <unordered_map>
9
11
15
17 class WorldIO;
18}
19
23 public:
24 class Dimension {
25 // just a class full of constants for now
26 public:
27 static constexpr const common::registry::Identifier
28 OVERWORLD = {"lodestone", "overworld"};
29 static constexpr const common::registry::Identifier
30 NETHER = {"lodestone", "nether"};
31 static constexpr const common::registry::Identifier END =
32 {"lodestone", "end"};
33 static constexpr const common::registry::Identifier
34 UNKNOWN = {"lodestone",
35 "unknown"}; // todo we can make level type registry
36 };
37
38 explicit World(const std::string &name = "New World") : m_name(name) {}
39
40 World(std::unique_ptr<Level> overworldLevel,
41 const std::string &name = "New World")
42 : World(name) {
43 this->addLevel(Dimension::OVERWORLD, std::move(overworldLevel));
44 }
45
46 World(const std::string &name,
48 std::unique_ptr<Level>> &&levels)
49 : m_name(std::move(name)), m_levels(std::move(levels)) {}
50
51 // TODO world constructor with abstract generator class that provides level
52
54 std::unique_ptr<Level>> &
55 getLevels() const;
56
58 std::unique_ptr<Level> level);
59 Level *
62 bool hasLevel(const common::registry::Identifier &id) const;
63
64 std::string toString() const override {
65 return std::format("World[name={}]", this->getName());
66 };
67
68 std::string getName() const;
69 void setName(const std::string &n);
70
71 virtual Level *getDefaultLevel() const;
72
74 getPlayers() const;
75 size_t getPlayerCount() const;
76
77 entity::Player *addPlayer(std::unique_ptr<entity::Player> player,
78 bool resetCoords = true);
79 entity::Player *getPlayer(const std::string &id) const;
80 std::unique_ptr<entity::Player> removePlayer(const std::string &id);
81 bool hasPlayer(const std::string &id) const;
82
83
84 void movePlayerToLevel(const std::string &id,
86 const bool resetCoords = true) {
87 const auto it = m_players.find(id);
88 if (it == m_players.end())
89 throw std::runtime_error(std::format(
90 "Attempted to move nonexistent player '{}' to level '{}'",
91 id, level));
92
93 entity::Player &plr = *it->second;
94
95 Level *lvl = getLevel(level);
96 if (!lvl)
97 throw std::runtime_error(std::format(
98 "Tried to move player '{}' to nonexistent level '{}'",
99 plr.getId(), level));
100
101 plr.setLevel(lvl, resetCoords);
102 }
103
104 void movePlayerToWorld(const std::string &id, World *target) {
105 std::unique_ptr<entity::Player> plr = removePlayer(id);
106 if (!plr)
107 throw std::runtime_error(std::format(
108 "Attempted to move nonexistent player '{}' to world '{}'",
109 id, target->toString()));
110
111 target->addPlayer(std::move(plr));
112 }
113
115 uint64_t getCreationTime();
116
117 std::unique_ptr<properties::AbstractProperty>
118 getProperty(const std::string &name) override;
119 protected:
121 std::string m_name;
122
130
132 };
133} // namespace lodestone::level::world
134
135#endif // LODESTONE_WORLD_H
gtl::flat_hash_map< T... > map_t
A level holds Chunks.
Definition Level.h:26
Definition Player.h:14
virtual const std::string & getId() const =0
Can return a UUID, player name, or other depending on the version.
void setLevel(level::Level *level, bool resetCoords=true)
Sets the player's level.
Definition Player.cpp:14
Definition ReflectiveProperties.h:15
static constexpr const common::registry::Identifier UNKNOWN
Definition World.h:34
static constexpr const common::registry::Identifier NETHER
Definition World.h:30
static constexpr const common::registry::Identifier OVERWORLD
Definition World.h:28
static constexpr const common::registry::Identifier END
Definition World.h:31
entity::Player * addPlayer(std::unique_ptr< entity::Player > player, bool resetCoords=true)
Definition World.cpp:52
void movePlayerToLevel(const std::string &id, const common::registry::Identifier &level, const bool resetCoords=true)
Definition World.h:84
std::string m_name
World name.
Definition World.h:121
const map_t< std::string, std::unique_ptr< entity::Player > > & getPlayers() const
Definition World.cpp:111
virtual Level * getDefaultLevel() const
Definition World.cpp:46
std::unique_ptr< entity::Player > removePlayer(const std::string &id)
Definition World.cpp:73
map_t< std::string, std::unique_ptr< entity::Player > > m_players
Definition World.h:131
std::unique_ptr< properties::AbstractProperty > getProperty(const std::string &name) override
Definition World.cpp:106
std::string toString() const override
Definition World.h:64
bool hasPlayer(const std::string &id) const
Definition World.cpp:87
Level * getLevel(const common::registry::Identifier &id) const
Definition World.cpp:21
Level * addLevel(const common::registry::Identifier &id, std::unique_ptr< Level > level)
Definition World.cpp:7
World(const std::string &name="New World")
Definition World.h:38
uint64_t getCreationTime()
Gets the creation time of the world, which would be the earliest level's creation time.
Definition World.cpp:91
entity::Player * getPlayer(const std::string &id) const
Definition World.cpp:66
std::string getName() const
Definition World.cpp:42
void setName(const std::string &n)
Definition World.cpp:44
void movePlayerToWorld(const std::string &id, World *target)
Definition World.h:104
World(const std::string &name, map_t< common::registry::Identifier, std::unique_ptr< Level > > &&levels)
Definition World.h:46
size_t getPlayerCount() const
Definition World.cpp:50
map_t< common::registry::Identifier, std::unique_ptr< Level > > m_levels
Levels.
Definition World.h:129
const map_t< common::registry::Identifier, std::unique_ptr< Level > > & getLevels() const
Definition World.cpp:116
World(std::unique_ptr< Level > overworldLevel, const std::string &name="New World")
Definition World.h:40
void removeLevel(const common::registry::Identifier &id)
Definition World.cpp:28
bool hasLevel(const common::registry::Identifier &id) const
Definition World.cpp:38
Definition World.h:16
Definition World.cpp:6
Definition Block.cpp:6
STL namespace.