3 #include "ChunkIndex.hpp"
4 #include "EntityCollision.hpp"
5 #include "WorldCollision.hpp"
6 #include "../app/Assets.hpp"
7 #include "../graphics/Format.hpp"
8 #include "../graphics/Viewport.hpp"
12 #include <glm/gtx/io.hpp>
13 #include <glm/gtx/transform.hpp>
18 World::World(const BlockTypeRegistry &types, const Config &config)
22 // TODO: set spawn base and extent from config
23 , spawn_index(chunks.MakeIndex(Chunk::Pos(0, 0, 0), 3))
26 , light_direction(config.light_direction)
27 , fog_density(config.fog_density) {
32 chunks.UnregisterIndex(spawn_index);
36 Player World::AddPlayer(const std::string &name) {
37 for (Player &p : players) {
38 if (p.entity->Name() == name) {
39 return { nullptr, nullptr };
42 Entity &entity = AddEntity();
44 // TODO: load from save file here
45 entity.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
46 entity.WorldCollidable(true);
47 entity.Position(config.spawn);
48 ChunkIndex *index = &chunks.MakeIndex(entity.ChunkCoords(), 6);
49 players.emplace_back(&entity, index);
50 return players.back();
53 Player World::AddPlayer(const std::string &name, std::uint32_t id) {
54 for (Player &p : players) {
55 if (p.entity->Name() == name) {
56 return { nullptr, nullptr };
59 Entity *entity = AddEntity(id);
61 return { nullptr, nullptr };
64 // TODO: load from save file here
65 entity->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
66 entity->WorldCollidable(true);
67 entity->Position(config.spawn);
68 ChunkIndex *index = &chunks.MakeIndex(entity->ChunkCoords(), 6);
69 players.emplace_back(entity, index);
70 return players.back();
73 Entity &World::AddEntity() {
74 if (entities.empty()) {
75 entities.emplace_back();
76 entities.back().ID(1);
77 return entities.back();
79 if (entities.back().ID() < std::numeric_limits<std::uint32_t>::max()) {
80 std::uint32_t id = entities.back().ID() + 1;
81 entities.emplace_back();
82 entities.back().ID(id);
83 return entities.back();
86 auto position = entities.begin();
87 auto end = entities.end();
88 while (position != end && position->ID() == id) {
92 auto entity = entities.emplace(position);
97 Entity *World::AddEntity(std::uint32_t id) {
98 if (entities.empty() || entities.back().ID() < id) {
99 entities.emplace_back();
100 entities.back().ID(id);
101 return &entities.back();
104 auto position = entities.begin();
105 auto end = entities.end();
106 while (position != end && position->ID() < id) {
109 if (position != end && position->ID() == id) {
112 auto entity = entities.emplace(position);
117 Entity &World::ForceAddEntity(std::uint32_t id) {
118 if (entities.empty() || entities.back().ID() < id) {
119 entities.emplace_back();
120 entities.back().ID(id);
121 return entities.back();
124 auto position = entities.begin();
125 auto end = entities.end();
126 while (position != end && position->ID() < id) {
129 if (position != end && position->ID() == id) {
132 auto entity = entities.emplace(position);
145 bool CandidateLess(const Candidate &a, const Candidate &b) {
146 return a.dist < b.dist;
149 std::vector<Candidate> candidates;
153 bool World::Intersection(
156 const Chunk::Pos &reference,
161 for (Chunk &cur_chunk : chunks) {
163 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
164 candidates.push_back({ &cur_chunk, cur_dist });
168 if (candidates.empty()) return false;
170 std::sort(candidates.begin(), candidates.end(), CandidateLess);
172 coll.chunk = nullptr;
174 coll.depth = std::numeric_limits<float>::infinity();
176 for (Candidate &cand : candidates) {
177 if (cand.dist > coll.depth) continue;
178 WorldCollision cur_coll;
179 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
180 if (cur_coll.depth < coll.depth) {
189 bool World::Intersection(
192 const Entity &reference,
193 EntityCollision &coll
195 coll.entity = nullptr;
196 coll.depth = std::numeric_limits<float>::infinity();
197 for (Entity &cur_entity : entities) {
198 if (&cur_entity == &reference) {
202 glm::vec3 cur_normal;
203 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
204 // TODO: fine grained check goes here? maybe?
205 if (cur_dist < coll.depth) {
206 coll.entity = &cur_entity;
207 coll.depth = cur_dist;
208 coll.normal = cur_normal;
216 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
217 AABB box = e.Bounds();
218 Chunk::Pos reference = e.ChunkCoords();
219 glm::mat4 M = e.Transform(reference);
221 for (Chunk &cur_chunk : chunks) {
222 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
223 // chunk is not one of the 3x3x3 surrounding the entity
224 // since there's no entity which can extent over 16 blocks, they can be skipped
227 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
237 std::vector<WorldCollision> col;
241 void World::Update(int dt) {
242 for (Entity &entity : entities) {
245 for (Entity &entity : entities) {
247 if (entity.WorldCollidable() && Intersection(entity, col)) {
248 // entity collides with the world
249 Resolve(entity, col);
252 for (Player &player : players) {
253 player.chunks->Rebase(player.entity->ChunkCoords());
255 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
256 if (iter->CanRemove()) {
257 iter = RemoveEntity(iter);
264 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
265 // determine displacement for each cardinal axis and move entity accordingly
266 glm::vec3 min_disp(0.0f);
267 glm::vec3 max_disp(0.0f);
268 for (const WorldCollision &c : col) {
269 if (!c.Blocks()) continue;
270 glm::vec3 local_disp(c.normal * c.depth);
271 // swap if neccessary (normal may point away from the entity)
272 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
275 min_disp = min(min_disp, local_disp);
276 max_disp = max(max_disp, local_disp);
279 // if only one direction is set, use that as the final
280 // if both directions are set, use average
281 glm::vec3 final_disp(0.0f);
282 for (int axis = 0; axis < 3; ++axis) {
283 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
284 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
285 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
287 final_disp[axis] = min_disp[axis];
289 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
290 final_disp[axis] = max_disp[axis];
296 World::EntityHandle World::RemoveEntity(EntityHandle &eh) {
298 for (auto player = players.begin(), end = players.end(); player != end;) {
299 if (player->entity == &*eh) {
300 chunks.UnregisterIndex(*player->chunks);
301 player = players.erase(player);
307 return entities.erase(eh);
311 void World::Render(Viewport &viewport) {
312 DirectionalLighting &entity_prog = viewport.EntityProgram();
313 entity_prog.SetLightDirection(light_direction);
314 entity_prog.SetFogDensity(fog_density);
316 for (Entity &entity : entities) {
317 entity.Render(entity.ChunkTransform(players[0].entity->ChunkCoords()), entity_prog);