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