]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
reorder world update
[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 class Viewport;
21 class WorldCollision;
22
23 class World {
24
25 public:
26         struct Config {
27                 std::string name = "default";
28                 // chunk base where new players are spawned
29                 glm::ivec3 spawn = { 0, 0, 0 };
30                 // direction facing towards(!) the light
31                 glm::vec3 light_direction = { -1.0f, -3.0f, -2.0f };
32                 // fade out reaches 1/e (0.3679) at 1/fog_density,
33                 // gets less than 0.01 at e/(2 * fog_density)
34                 // I chose 0.011 because it yields 91 and 124 for those, so
35                 // slightly less than 6 and 8 chunks
36                 float fog_density = 0.011f;
37         };
38
39         World(const BlockTypeRegistry &, const Config &);
40         ~World();
41
42         const std::string &Name() const noexcept { return config.name; }
43
44         /// check if this ray hits a block
45         /// depth in the collision is the distance between the ray's
46         /// origin and the intersection point
47         /// reference is the chunk offset of the ray in world space
48         bool Intersection(
49                 const Ray &,
50                 const ExactLocation::Coarse &reference,
51                 WorldCollision &);
52
53         /// check if this ray hits an entity
54         /// intersections with the reference are not tested
55         /// the ray is assumed to be in world space offset by entity's chunk coords
56         bool Intersection(
57                 const Ray &,
58                 const Entity &reference,
59                 EntityCollision &);
60
61         /// check if given entity intersects with the world
62         bool Intersection(const Entity &e, const EntityState &, std::vector<WorldCollision> &);
63         /// combine contacts into a single penetration vector
64         /// depth is given to point towards position of given state
65         static glm::vec3 CombinedInterpenetration(
66                 const EntityState &,
67                 const std::vector<WorldCollision> &) noexcept;
68
69         /// check if given box (M * AABB) intersects with the world
70         /// M is assumed to be calculated in reference to given chunk coords
71         bool Intersection(
72                 const AABB &box,
73                 const glm::mat4 &M,
74                 const glm::ivec3 &reference,
75                 std::vector<WorldCollision> &);
76
77         const BlockTypeRegistry &BlockTypes() noexcept { return block_type; }
78         ChunkStore &Chunks() noexcept { return chunks; }
79
80         /// add player with given name
81         /// returns nullptr if the name is already taken
82         Player *AddPlayer(const std::string &name);
83         /// add player with given name and ID
84         /// returns nullptr if the name or ID is already taken
85         Player *AddPlayer(const std::string &name, std::uint32_t id);
86         /// add an entity with an autogenerated ID
87         Entity &AddEntity();
88         /// add entity with given ID
89         /// returns nullptr if the ID is already taken
90         Entity *AddEntity(std::uint32_t id);
91         /// add entity with given ID
92         /// returs an existing entity if ID is already taken
93         Entity &ForceAddEntity(std::uint32_t id);
94
95         std::list<Player> &Players() noexcept { return players; }
96         const std::list<Player> &Players() const noexcept { return players; }
97         std::list<Entity> &Entities() noexcept { return entities; }
98         const std::list<Entity> &Entities() const noexcept { return entities; }
99
100         // dt in ms
101         void Update(int dt);
102
103         /// fix state so entity doesn't intersect with the solid world
104         void ResolveWorldCollision(const Entity &, EntityState &);
105         /// get force due to gravity at given location
106         glm::vec3 GravityAt(const ExactLocation &) const noexcept;
107
108         void Render(Viewport &);
109         void RenderDebug(Viewport &);
110
111 private:
112         using EntityHandle = std::list<Entity>::iterator;
113         EntityHandle RemoveEntity(EntityHandle &);
114
115         /// calculate light direction and intensity at entity's location
116         void GetLight(
117                 const Entity &entity,
118                 glm::vec3 &direction,
119                 glm::vec3 &color,
120                 glm::vec3 &ambient
121         );
122
123 private:
124         Config config;
125
126         const BlockTypeRegistry &block_type;
127
128         ChunkStore chunks;
129
130         std::list<Player> players;
131         std::list<Entity> entities;
132
133         glm::vec3 light_direction;
134         float fog_density;
135
136 };
137
138 }
139
140 #endif