]> git.localhorst.tv Git - blank.git/blob - src/world/World.cpp
special treatment for players
[blank.git] / src / world / World.cpp
1 #include "World.hpp"
2
3 #include "EntityCollision.hpp"
4 #include "WorldCollision.hpp"
5 #include "../app/Assets.hpp"
6 #include "../graphics/Format.hpp"
7 #include "../graphics/Viewport.hpp"
8
9 #include <algorithm>
10 #include <limits>
11 #include <glm/gtx/io.hpp>
12 #include <glm/gtx/transform.hpp>
13
14
15 namespace blank {
16
17 World::World(const BlockTypeRegistry &types, const Config &config, const WorldSave &save)
18 : config(config)
19 , block_type(types)
20 , generate(config.gen)
21 , chunks(config.load, types, generate, save)
22 , players()
23 , entities()
24 , light_direction(config.light_direction)
25 , fog_density(config.fog_density) {
26         generate.Space(0);
27         generate.Light(13);
28         generate.Solids({ 1, 4, 7, 10 });
29 }
30
31
32 Entity *World::AddPlayer(const std::string &name) {
33         for (Entity *e : players) {
34                 if (e->Name() == name) {
35                         return nullptr;
36                 }
37         }
38         Entity &player = AddEntity();
39         player.Name(name);
40         // TODO: load from save file here
41         player.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
42         player.WorldCollidable(true);
43         player.Position(config.spawn);
44         players.push_back(&player);
45         chunks.QueueSurrounding(player.ChunkCoords());
46         return &player;
47 }
48
49
50 namespace {
51
52 struct Candidate {
53         Chunk *chunk;
54         float dist;
55 };
56
57 bool CandidateLess(const Candidate &a, const Candidate &b) {
58         return a.dist < b.dist;
59 }
60
61 std::vector<Candidate> candidates;
62
63 }
64
65 bool World::Intersection(
66         const Ray &ray,
67         const glm::mat4 &M,
68         const Chunk::Pos &reference,
69         WorldCollision &coll
70 ) {
71         candidates.clear();
72
73         for (Chunk &cur_chunk : chunks.Loaded()) {
74                 float cur_dist;
75                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
76                         candidates.push_back({ &cur_chunk, cur_dist });
77                 }
78         }
79
80         if (candidates.empty()) return false;
81
82         std::sort(candidates.begin(), candidates.end(), CandidateLess);
83
84         coll.chunk = nullptr;
85         coll.block = -1;
86         coll.depth = std::numeric_limits<float>::infinity();
87
88         for (Candidate &cand : candidates) {
89                 if (cand.dist > coll.depth) continue;
90                 WorldCollision cur_coll;
91                 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
92                         if (cur_coll.depth < coll.depth) {
93                                 coll = cur_coll;
94                         }
95                 }
96         }
97
98         return coll.chunk;
99 }
100
101 bool World::Intersection(
102         const Ray &ray,
103         const glm::mat4 &M,
104         const Entity &reference,
105         EntityCollision &coll
106 ) {
107         coll.entity = nullptr;
108         coll.depth = std::numeric_limits<float>::infinity();
109         for (Entity &cur_entity : entities) {
110                 if (&cur_entity == &reference) {
111                         continue;
112                 }
113                 float cur_dist;
114                 glm::vec3 cur_normal;
115                 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
116                         // TODO: fine grained check goes here? maybe?
117                         if (cur_dist < coll.depth) {
118                                 coll.entity = &cur_entity;
119                                 coll.depth = cur_dist;
120                                 coll.normal = cur_normal;
121                         }
122                 }
123         }
124
125         return coll.entity;
126 }
127
128 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
129         AABB box = e.Bounds();
130         Chunk::Pos reference = e.ChunkCoords();
131         glm::mat4 M = e.Transform(reference);
132         bool any = false;
133         for (Chunk &cur_chunk : chunks.Loaded()) {
134                 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
135                         // chunk is not one of the 3x3x3 surrounding the entity
136                         // since there's no entity which can extent over 16 blocks, they can be skipped
137                         continue;
138                 }
139                 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
140                         any = true;
141                 }
142         }
143         return any;
144 }
145
146
147 namespace {
148
149 std::vector<WorldCollision> col;
150
151 }
152
153 void World::Update(int dt) {
154         for (Entity &entity : entities) {
155                 entity.Update(dt);
156         }
157         for (Entity &entity : entities) {
158                 col.clear();
159                 if (entity.WorldCollidable() && Intersection(entity, col)) {
160                         // entity collides with the world
161                         Resolve(entity, col);
162                 }
163         }
164         for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
165                 if (iter->CanRemove()) {
166                         iter = entities.erase(iter);
167                 } else {
168                         ++iter;
169                 }
170         }
171         // TODO: make flexible
172         chunks.Rebase(players[0]->ChunkCoords());
173         chunks.Update(dt);
174 }
175
176 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
177         // determine displacement for each cardinal axis and move entity accordingly
178         glm::vec3 min_disp(0.0f);
179         glm::vec3 max_disp(0.0f);
180         for (const WorldCollision &c : col) {
181                 if (!c.Blocks()) continue;
182                 glm::vec3 local_disp(c.normal * c.depth);
183                 // swap if neccessary (normal may point away from the entity)
184                 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
185                         local_disp *= -1;
186                 }
187                 min_disp = min(min_disp, local_disp);
188                 max_disp = max(max_disp, local_disp);
189         }
190         // for each axis
191         // if only one direction is set, use that as the final
192         // if both directions are set, use average
193         glm::vec3 final_disp(0.0f);
194         for (int axis = 0; axis < 3; ++axis) {
195                 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
196                         if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
197                                 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
198                         } else {
199                                 final_disp[axis] = min_disp[axis];
200                         }
201                 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
202                         final_disp[axis] = max_disp[axis];
203                 }
204         }
205         e.Move(final_disp);
206 }
207
208
209 void World::Render(Viewport &viewport) {
210         DirectionalLighting &entity_prog = viewport.EntityProgram();
211         entity_prog.SetLightDirection(light_direction);
212         entity_prog.SetFogDensity(fog_density);
213
214         for (Entity &entity : entities) {
215                 entity.Render(entity.ChunkTransform(players[0]->ChunkCoords()), entity_prog);
216         }
217 }
218
219 }