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