]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
impersonate command
[blank.git] / src / world / World.hpp
1 #ifndef BLANK_WORLD_WORLD_HPP_
2 #define BLANK_WORLD_WORLD_HPP_
3
4 #include "ChunkStore.hpp"
5 #include "Entity.hpp"
6 #include "Generator.hpp"
7 #include "Player.hpp"
8 #include "../graphics/glm.hpp"
9 #include "../rand/GaloisLFSR.hpp"
10
11 #include <cstdint>
12 #include <list>
13 #include <string>
14 #include <vector>
15
16
17 namespace blank {
18
19 class BlockTypeRegistry;
20 class EntityCollision;
21 class Viewport;
22 class WorldCollision;
23
24 class World {
25
26 public:
27         struct Config {
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;
38         };
39
40         World(const BlockTypeRegistry &, const Config &);
41         ~World();
42
43         const std::string &Name() const noexcept { return config.name; }
44
45         /// get the shared random source for this world
46         GaloisLFSR &Random() noexcept { return rng; }
47
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
52         bool Intersection(
53                 const Ray &,
54                 const ExactLocation::Coarse &reference,
55                 WorldCollision &);
56
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
60         bool Intersection(
61                 const Ray &,
62                 const Entity &reference,
63                 EntityCollision &);
64
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(
70                 const EntityState &,
71                 const std::vector<WorldCollision> &) noexcept;
72
73         /// check if given box (M * AABB) intersects with the world
74         /// M is assumed to be calculated in reference to given chunk coords
75         bool Intersection(
76                 const AABB &box,
77                 const glm::mat4 &M,
78                 const glm::ivec3 &reference,
79                 std::vector<WorldCollision> &);
80
81         const BlockTypeRegistry &BlockTypes() noexcept { return block_type; }
82         ChunkStore &Chunks() noexcept { return chunks; }
83
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
91         Entity &AddEntity();
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);
98
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);
106
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; }
111
112         // dt in ms
113         void Update(int dt);
114
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;
119
120         void Render(Viewport &);
121         void RenderDebug(Viewport &);
122
123 private:
124         using EntityHandle = std::list<Entity>::iterator;
125         EntityHandle RemoveEntity(EntityHandle &);
126
127         /// calculate light direction and intensity at entity's location
128         void GetLight(
129                 const Entity &entity,
130                 glm::vec3 &direction,
131                 glm::vec3 &color,
132                 glm::vec3 &ambient
133         );
134
135 private:
136         Config config;
137
138         const BlockTypeRegistry &block_type;
139
140         ChunkStore chunks;
141
142         std::list<Player> players;
143         std::list<Entity> entities;
144
145         GaloisLFSR rng;
146
147         glm::vec3 light_direction;
148         float fog_density;
149
150 };
151
152 }
153
154 #endif