]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
273bfa63abc3809a507f77889015f044515b254c
[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
9 #include <cstdint>
10 #include <list>
11 #include <string>
12 #include <vector>
13 #include <glm/glm.hpp>
14
15
16 namespace blank {
17
18 class BlockTypeRegistry;
19 class EntityCollision;
20 struct EntityDerivative;
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         /// check if this ray hits a block
46         /// depth in the collision is the distance between the ray's
47         /// origin and the intersection point
48         /// M is the global transform for given reference chunk
49         bool Intersection(
50                 const Ray &,
51                 const glm::mat4 &M,
52                 const Chunk::Pos &reference,
53                 WorldCollision &);
54
55         /// check if this ray hits an entity
56         /// intersections with the reference are not tested
57         /// M is the global transform for the chunk of given reference entity
58         bool Intersection(
59                 const Ray &,
60                 const glm::mat4 &M,
61                 const Entity &reference,
62                 EntityCollision &);
63
64         /// check if given entity intersects with the world
65         bool Intersection(const Entity &e, const EntityState &, std::vector<WorldCollision> &);
66
67         /// check if given box (M * AABB) intersects with the world
68         /// M is assumed to be calculated in reference to given chunk coords
69         bool Intersection(
70                 const AABB &box,
71                 const glm::mat4 &M,
72                 const glm::ivec3 &reference,
73                 std::vector<WorldCollision> &);
74
75         const BlockTypeRegistry &BlockTypes() noexcept { return block_type; }
76         ChunkStore &Chunks() noexcept { return chunks; }
77
78         /// add player with given name
79         /// returns nullptr if the name is already taken
80         Player *AddPlayer(const std::string &name);
81         /// add player with given name and ID
82         /// returns nullptr if the name or ID is already taken
83         Player *AddPlayer(const std::string &name, std::uint32_t id);
84         /// add an entity with an autogenerated ID
85         Entity &AddEntity();
86         /// add entity with given ID
87         /// returns nullptr if the ID is already taken
88         Entity *AddEntity(std::uint32_t id);
89         /// add entity with given ID
90         /// returs an existing entity if ID is already taken
91         Entity &ForceAddEntity(std::uint32_t id);
92
93         std::list<Player> &Players() noexcept { return players; }
94         const std::list<Player> &Players() const noexcept { return players; }
95         std::list<Entity> &Entities() noexcept { return entities; }
96         const std::list<Entity> &Entities() const noexcept { return entities; }
97
98         // dt in ms
99         void Update(int dt);
100         // dt in s
101         void Update(Entity &, float dt);
102
103         void Render(Viewport &);
104         void RenderDebug(Viewport &);
105
106 private:
107         using EntityHandle = std::list<Entity>::iterator;
108         EntityHandle RemoveEntity(EntityHandle &);
109
110         EntityDerivative CalculateStep(
111                 const Entity &,
112                 const EntityState &cur,
113                 float dt,
114                 const EntityDerivative &prev
115         );
116         glm::vec3 CalculateForce(
117                 const Entity &,
118                 const EntityState &cur
119         );
120         glm::vec3 ControlForce(
121                 const Entity &,
122                 const EntityState &
123         );
124         glm::vec3 CollisionForce(
125                 const Entity &,
126                 const EntityState &
127         );
128         glm::vec3 Gravity(
129                 const Entity &,
130                 const EntityState &
131         );
132
133         /// calculate light direction and intensity at entity's location
134         void GetLight(
135                 const Entity &entity,
136                 glm::vec3 &direction,
137                 glm::vec3 &color,
138                 glm::vec3 &ambient
139         );
140
141 private:
142         Config config;
143
144         const BlockTypeRegistry &block_type;
145
146         ChunkStore chunks;
147
148         std::list<Player> players;
149         std::list<Entity> entities;
150
151         glm::vec3 light_direction;
152         float fog_density;
153
154 };
155
156 }
157
158 #endif