]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
40136c8dff4a99315003582bd3b58a37d7cc5374
[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 "../rand/GaloisLFSR.hpp"
9
10 #include <cstdint>
11 #include <list>
12 #include <string>
13 #include <vector>
14 #include <glm/glm.hpp>
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         std::list<Player> &Players() noexcept { return players; }
100         const std::list<Player> &Players() const noexcept { return players; }
101         std::list<Entity> &Entities() noexcept { return entities; }
102         const std::list<Entity> &Entities() const noexcept { return entities; }
103
104         // dt in ms
105         void Update(int dt);
106
107         /// fix state so entity doesn't intersect with the solid world
108         void ResolveWorldCollision(const Entity &, EntityState &);
109         /// get force due to gravity at given location
110         glm::vec3 GravityAt(const ExactLocation &) const noexcept;
111
112         void Render(Viewport &);
113         void RenderDebug(Viewport &);
114
115 private:
116         using EntityHandle = std::list<Entity>::iterator;
117         EntityHandle RemoveEntity(EntityHandle &);
118
119         /// calculate light direction and intensity at entity's location
120         void GetLight(
121                 const Entity &entity,
122                 glm::vec3 &direction,
123                 glm::vec3 &color,
124                 glm::vec3 &ambient
125         );
126
127 private:
128         Config config;
129
130         const BlockTypeRegistry &block_type;
131
132         ChunkStore chunks;
133
134         std::list<Player> players;
135         std::list<Entity> entities;
136
137         GaloisLFSR rng;
138
139         glm::vec3 light_direction;
140         float fog_density;
141
142 };
143
144 }
145
146 #endif