1 #ifndef BLANK_WORLD_WORLD_HPP_
2 #define BLANK_WORLD_WORLD_HPP_
4 #include "ChunkStore.hpp"
6 #include "Generator.hpp"
8 #include "../graphics/glm.hpp"
9 #include "../rand/GaloisLFSR.hpp"
19 class BlockTypeRegistry;
20 class EntityCollision;
28 std::string name = "default";
29 // chunk base where new players are spawned
30 glm::ivec3 spawn = { 0, 0, 0 };
31 // direction facing towards(!) the light
32 glm::vec3 light_direction = { -1.0f, -3.0f, -2.0f };
33 // fade out reaches 1/e (0.3679) at 1/fog_density,
34 // gets less than 0.01 at e/(2 * fog_density)
35 // I chose 0.011 because it yields 91 and 124 for those, so
36 // slightly less than 6 and 8 chunks
37 float fog_density = 0.011f;
40 World(const BlockTypeRegistry &, const Config &);
43 const std::string &Name() const noexcept { return config.name; }
45 /// get the shared random source for this world
46 GaloisLFSR &Random() noexcept { return rng; }
48 /// check if this ray hits a block
49 /// depth in the collision is the distance between the ray's
50 /// origin and the intersection point
51 /// reference is the chunk offset of the ray in world space
54 const ExactLocation::Coarse &reference,
57 /// check if this ray hits an entity
58 /// intersections with the reference are not tested
59 /// the ray is assumed to be in world space offset by entity's chunk coords
62 const Entity &reference,
65 /// check if given entity intersects with the world
66 bool Intersection(const Entity &e, const EntityState &, std::vector<WorldCollision> &);
67 /// combine contacts into a single penetration vector
68 /// depth is given to point towards position of given state
69 static glm::vec3 CombinedInterpenetration(
71 const std::vector<WorldCollision> &) noexcept;
73 /// check if given box (M * AABB) intersects with the world
74 /// M is assumed to be calculated in reference to given chunk coords
78 const glm::ivec3 &reference,
79 std::vector<WorldCollision> &);
81 const BlockTypeRegistry &BlockTypes() noexcept { return block_type; }
82 ChunkStore &Chunks() noexcept { return chunks; }
84 /// add player with given name
85 /// returns nullptr if the name is already taken
86 Player *AddPlayer(const std::string &name);
87 /// add player with given name and ID
88 /// returns nullptr if the name or ID is already taken
89 Player *AddPlayer(const std::string &name, std::uint32_t id);
90 /// add an entity with an autogenerated ID
92 /// add entity with given ID
93 /// returns nullptr if the ID is already taken
94 Entity *AddEntity(std::uint32_t id);
95 /// add entity with given ID
96 /// returs an existing entity if ID is already taken
97 Entity &ForceAddEntity(std::uint32_t id);
99 /// get the player with given name
100 /// returns nullptr if no player bears this name
101 Player *FindPlayer(const std::string &name);
102 /// get an entity with given name
103 /// returns nullptr if name doesn't refer to any entity
104 /// note that unlike players, entity names are not unique
105 Entity *FindEntity(const std::string &name);
107 std::list<Player> &Players() noexcept { return players; }
108 const std::list<Player> &Players() const noexcept { return players; }
109 std::list<Entity> &Entities() noexcept { return entities; }
110 const std::list<Entity> &Entities() const noexcept { return entities; }
115 /// fix state so entity doesn't intersect with the solid world
116 void ResolveWorldCollision(const Entity &, EntityState &);
117 /// get force due to gravity at given location
118 glm::vec3 GravityAt(const ExactLocation &) const noexcept;
120 void Render(Viewport &);
121 void RenderDebug(Viewport &);
124 using EntityHandle = std::list<Entity>::iterator;
125 EntityHandle RemoveEntity(EntityHandle &);
127 /// calculate light direction and intensity at entity's location
129 const Entity &entity,
130 glm::vec3 &direction,
138 const BlockTypeRegistry &block_type;
142 std::list<Player> players;
143 std::list<Entity> entities;
147 glm::vec3 light_direction;