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