]> git.localhorst.tv Git - blank.git/blob - src/world/World.cpp
make entities removable
[blank.git] / src / world / World.cpp
1 #include "World.hpp"
2
3 #include "WorldCollision.hpp"
4 #include "../graphics/Viewport.hpp"
5
6 #include <iostream>
7 #include <limits>
8 #include <glm/gtx/io.hpp>
9 #include <glm/gtx/transform.hpp>
10
11
12 namespace blank {
13
14 World::World(const Config &config)
15 : blockType()
16 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
17 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
18 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
19 , generate(config.gen)
20 , chunks(config.load, blockType, generate)
21 , player()
22 , entities()
23 , light_direction(config.light_direction)
24 , fog_density(config.fog_density) {
25         BlockType::Faces block_fill = {  true,  true,  true,  true,  true,  true };
26         BlockType::Faces slab_fill  = { false,  true, false, false, false, false };
27         BlockType::Faces stair_fill = { false,  true, false, false, false,  true };
28
29         { // white block
30                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &blockShape);
31                 type.label = "White Block";
32                 type.block_light = true;
33                 type.collision = true;
34                 type.collide_block = true;
35                 type.fill = block_fill;
36                 blockType.Add(type);
37         }
38         { // white slab
39                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &slabShape);
40                 type.label = "White Slab";
41                 type.block_light = true;
42                 type.collision = true;
43                 type.collide_block = true;
44                 type.fill = slab_fill;
45                 blockType.Add(type);
46         }
47         { // white stair
48                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &stairShape);
49                 type.label = "White Stair";
50                 type.block_light = true;
51                 type.collision = true;
52                 type.collide_block = true;
53                 type.fill = stair_fill;
54                 blockType.Add(type);
55         }
56
57         { // red block
58                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &blockShape);
59                 type.label = "Red Block";
60                 type.block_light = true;
61                 type.collision = true;
62                 type.collide_block = true;
63                 type.fill = block_fill;
64                 blockType.Add(type);
65         }
66         { // red slab
67                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &slabShape);
68                 type.label = "Red Slab";
69                 type.block_light = true;
70                 type.collision = true;
71                 type.collide_block = true;
72                 type.fill = slab_fill;
73                 blockType.Add(type);
74         }
75         { // red stair
76                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &stairShape);
77                 type.label = "Red Stair";
78                 type.block_light = true;
79                 type.collision = true;
80                 type.collide_block = true;
81                 type.fill = stair_fill;
82                 blockType.Add(type);
83         }
84
85         { // green block
86                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &blockShape);
87                 type.label = "Green Block";
88                 type.block_light = true;
89                 type.collision = true;
90                 type.collide_block = true;
91                 type.fill = block_fill;
92                 blockType.Add(type);
93         }
94         { // green slab
95                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &slabShape);
96                 type.label = "Green Slab";
97                 type.block_light = true;
98                 type.collision = true;
99                 type.collide_block = true;
100                 type.fill = slab_fill;
101                 blockType.Add(type);
102         }
103         { // green stair
104                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &stairShape);
105                 type.label = "Green Stair";
106                 type.block_light = true;
107                 type.collision = true;
108                 type.collide_block = true;
109                 type.fill = stair_fill;
110                 blockType.Add(type);
111         }
112
113         { // blue block
114                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &blockShape);
115                 type.label = "Blue Block";
116                 type.block_light = true;
117                 type.collision = true;
118                 type.collide_block = true;
119                 type.fill = block_fill;
120                 blockType.Add(type);
121         }
122         { // blue slab
123                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &slabShape);
124                 type.label = "Blue Slab";
125                 type.block_light = true;
126                 type.collision = true;
127                 type.collide_block = true;
128                 type.fill = slab_fill;
129                 blockType.Add(type);
130         }
131         { // blue stair
132                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &stairShape);
133                 type.label = "Blue Stair";
134                 type.block_light = true;
135                 type.collision = true;
136                 type.collide_block = true;
137                 type.fill = stair_fill;
138                 blockType.Add(type);
139         }
140
141         { // glowing yellow block
142                 BlockType type(true, { 1.0f, 1.0f, 0.0f }, &blockShape);
143                 type.label = "Light";
144                 type.luminosity = 15;
145                 type.block_light = true;
146                 type.collision = true;
147                 type.collide_block = true;
148                 type.fill = block_fill;
149                 blockType.Add(type);
150         }
151
152         generate.Space(0);
153         generate.Light(13);
154         generate.Solids({ 1, 4, 7, 10 });
155
156         player = &AddEntity();
157         player->Name("player");
158         player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
159         player->WorldCollidable(true);
160         player->Position(config.spawn);
161
162         chunks.GenerateSurrounding(player->ChunkCoords());
163 }
164
165
166 namespace {
167
168 struct Candidate {
169         Chunk *chunk;
170         float dist;
171 };
172
173 std::vector<Candidate> candidates;
174
175 }
176
177 bool World::Intersection(
178         const Ray &ray,
179         const glm::mat4 &M,
180         Chunk *&chunk,
181         int &blkid,
182         float &dist,
183         glm::vec3 &normal
184 ) {
185         candidates.clear();
186
187         for (Chunk &cur_chunk : chunks.Loaded()) {
188                 float cur_dist;
189                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
190                         candidates.push_back({ &cur_chunk, cur_dist });
191                 }
192         }
193
194         if (candidates.empty()) return false;
195
196         chunk = nullptr;
197         dist = std::numeric_limits<float>::infinity();
198         blkid = -1;
199
200         for (Candidate &cand : candidates) {
201                 if (cand.dist > dist) continue;
202                 int cur_blkid;
203                 float cur_dist;
204                 glm::vec3 cur_normal;
205                 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
206                         if (cur_dist < dist) {
207                                 chunk = cand.chunk;
208                                 blkid = cur_blkid;
209                                 dist = cur_dist;
210                                 normal = cur_normal;
211                         }
212                 }
213         }
214
215         return chunk;
216 }
217
218 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
219         AABB box = e.Bounds();
220         glm::mat4 M = e.Transform(player->ChunkCoords());
221         bool any = false;
222         // TODO: this only needs to check the chunks surrounding the entity's chunk position
223         //       need find out if that is quicker than the rough chunk bounds test
224         for (Chunk &cur_chunk : chunks.Loaded()) {
225                 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()), col)) {
226                         any = true;
227                 }
228         }
229         return any;
230 }
231
232
233 Chunk &World::PlayerChunk() {
234         return chunks.ForceLoad(player->ChunkCoords());
235 }
236
237 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
238         const Chunk::Pos tgt_pos = to.Position() + dir;
239         return chunks.ForceLoad(tgt_pos);
240 }
241
242
243 namespace {
244
245 std::vector<WorldCollision> col;
246
247 }
248
249 void World::Update(int dt) {
250         for (Entity &entity : entities) {
251                 entity.Update(dt);
252         }
253         for (Entity &entity : entities) {
254                 col.clear();
255                 if (entity.WorldCollidable() && Intersection(entity, col)) {
256                         // entity collides with the world
257                         Resolve(entity, col);
258                 }
259         }
260         for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
261                 if (iter->CanRemove()) {
262                         iter = entities.erase(iter);
263                 } else {
264                         ++iter;
265                 }
266         }
267         chunks.Rebase(player->ChunkCoords());
268         chunks.Update(dt);
269 }
270
271 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
272         // determine displacement for each cardinal axis and move entity accordingly
273         glm::vec3 min_disp(0.0f);
274         glm::vec3 max_disp(0.0f);
275         for (const WorldCollision &c : col) {
276                 if (!c.Blocks()) continue;
277                 glm::vec3 local_disp(c.normal * c.depth);
278                 // swap if neccessary (normal may point away from the entity)
279                 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
280                         local_disp *= -1;
281                 }
282                 min_disp = min(min_disp, local_disp);
283                 max_disp = max(max_disp, local_disp);
284         }
285         // for each axis
286         // if only one direction is set, use that as the final
287         // if both directions are set, use average
288         glm::vec3 final_disp(0.0f);
289         for (int axis = 0; axis < 3; ++axis) {
290                 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
291                         if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
292                                 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
293                         } else {
294                                 final_disp[axis] = min_disp[axis];
295                         }
296                 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
297                         final_disp[axis] = max_disp[axis];
298                 }
299         }
300         e.Move(final_disp);
301 }
302
303
304 void World::Render(Viewport &viewport) {
305         viewport.WorldPosition(player->Transform(player->ChunkCoords()));
306
307         BlockLighting &chunk_prog = viewport.ChunkProgram();
308         chunk_prog.SetFogDensity(fog_density);
309
310         for (Chunk &chunk : chunks.Loaded()) {
311                 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
312                 chunk_prog.SetM(m);
313                 glm::mat4 mvp(chunk_prog.GetVP() * m);
314                 if (!CullTest(Chunk::Bounds(), mvp)) {
315                         chunk.Draw();
316                 }
317         }
318
319         DirectionalLighting &entity_prog = viewport.EntityProgram();
320         entity_prog.SetLightDirection(light_direction);
321         entity_prog.SetFogDensity(fog_density);
322
323         for (Entity &entity : entities) {
324                 if (entity.HasShape()) {
325                         entity_prog.SetM(entity.Transform(player->ChunkCoords()));
326                         entity.Draw();
327                 }
328         }
329 }
330
331 }