summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2019-02-23 12:15:46 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2019-02-23 12:15:46 -0500
commit3504fd5080dbcfd0172299c5c6d13895e53ad163 (patch)
treecaab8e868566262b3aa4d80aa6a06cdf13e7c21d /src
parent15b511e694f976686fdec1fb9f959f8a92f3b594 (diff)
downloaddispatcher-3504fd5080dbcfd0172299c5c6d13895e53ad163.tar.gz
dispatcher-3504fd5080dbcfd0172299c5c6d13895e53ad163.tar.bz2
dispatcher-3504fd5080dbcfd0172299c5c6d13895e53ad163.zip
Removed position caches
There aren't going to be enough entities at once for position checking to ever really be a bottleneck, I don't think. Removing the caches makes the range logic a bit more intuitive, and it removes the possibility of accidentally not updating a cache when it needs to be.
Diffstat (limited to 'src')
-rw-r--r--src/grid_cache.h59
-rw-r--r--src/simulation.cpp22
-rw-r--r--src/simulation.h4
-rw-r--r--src/vector.h5
-rw-r--r--src/views.h14
5 files changed, 27 insertions, 77 deletions
diff --git a/src/grid_cache.h b/src/grid_cache.h deleted file mode 100644 index e837f55..0000000 --- a/src/grid_cache.h +++ /dev/null
@@ -1,59 +0,0 @@
1#ifndef GRID_CACHE_H_67BBE74D
2#define GRID_CACHE_H_67BBE74D
3
4#include <unordered_map>
5#include <unordered_set>
6#include "level.h"
7
8template <typename T>
9class GridCache {
10public:
11
12 explicit GridCache(const Level& level) : width_(level.getSize().w())
13 {
14 }
15
16 void set(T value, vec2s pos)
17 {
18 if (reverse_.count(value))
19 {
20 size_t oldPosIndex = reverse_.at(value);
21 lookup_[oldPosIndex].erase(value);
22 }
23
24 size_t newPosIndex = getIndex(pos);
25 lookup_[newPosIndex].insert(value);
26 reverse_[value] = newPosIndex;
27 }
28
29 void remove(T value)
30 {
31 if (reverse_.count(value))
32 {
33 size_t index = reverse_.at(value);
34 lookup_[index].erase(value);
35 reverse_.erase(value);
36 }
37 }
38
39 const std::unordered_set<T>& at(vec2s pos) const
40 {
41 size_t index = getIndex(pos);
42
43 return lookup_[index];
44 }
45
46private:
47
48 inline size_t getIndex(const vec2s& pos) const
49 {
50 return pos.x() + pos.y() * width_;
51 }
52
53 size_t width_;
54
55 mutable std::unordered_map<size_t, std::unordered_set<T>> lookup_;
56 std::unordered_map<T, size_t> reverse_;
57};
58
59#endif /* end of include guard: GRID_CACHE_H_67BBE74D */
diff --git a/src/simulation.cpp b/src/simulation.cpp index f75bd3d..27ac6ab 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp
@@ -89,12 +89,6 @@ Simulation::Simulation(
89 crate2.canBePushedBy.insert(ColliderType::crate); 89 crate2.canBePushedBy.insert(ColliderType::crate);
90 crate2.canBePushedBy.insert(ColliderType::train); 90 crate2.canBePushedBy.insert(ColliderType::train);
91 crate2.gridPos = vec2s { 6, 7 }; 91 crate2.gridPos = vec2s { 6, 7 };
92
93
94 for (Entity& entity : active_ | entityView())
95 {
96 posCache_.set(entity.id, entity.gridPos);
97 }
98} 92}
99 93
100void Simulation::tick( 94void Simulation::tick(
@@ -132,8 +126,9 @@ void Simulation::tick(
132 vec2s lookPos = posInDir(entity.gridPos, lookDir); 126 vec2s lookPos = posInDir(entity.gridPos, lookDir);
133 127
134 for (Entity& block : 128 for (Entity& block :
135 posCache_.at(lookPos) | 129 active_ |
136 entityView() | 130 entityView() |
131 views::atGridPos(lookPos) |
137 views::isNotMoving() | 132 views::isNotMoving() |
138 views::canBePushedBy(entity.colliderType) | 133 views::canBePushedBy(entity.colliderType) |
139 views::isOnLayer(entity.layer)) 134 views::isOnLayer(entity.layer))
@@ -185,8 +180,9 @@ void Simulation::tick(
185 views::isNotMoving()) 180 views::isNotMoving())
186 { 181 {
187 auto tracks = 182 auto tracks =
188 posCache_.at(entity.gridPos) | 183 active_ |
189 entityView() | 184 entityView() |
185 views::atGridPos(entity.gridPos) |
190 views::isTrack(); 186 views::isTrack();
191 187
192 if (!ranges::empty(tracks)) 188 if (!ranges::empty(tracks))
@@ -233,8 +229,6 @@ void Simulation::tick(
233 { 229 {
234 entity.moving = false; 230 entity.moving = false;
235 entity.gridPos = entity.destPos; 231 entity.gridPos = entity.destPos;
236 posCache_.set(entity.id, entity.gridPos);
237 moveToCache_.remove(entity.id);
238 } 232 }
239 } 233 }
240 234
@@ -340,16 +334,18 @@ bool Simulation::moveEntityOnGrid(
340 334
341 // Can't move into a space that something else is already moving into. 335 // Can't move into a space that something else is already moving into.
342 if (!ranges::empty( 336 if (!ranges::empty(
343 moveToCache_.at(shouldMoveTo) | 337 active_ |
344 entityView() | 338 entityView() |
339 views::isMovingTo(shouldMoveTo) |
345 views::isOnLayer(entity.layer))) 340 views::isOnLayer(entity.layer)))
346 { 341 {
347 return false; 342 return false;
348 } 343 }
349 344
350 for (Entity& block : 345 for (Entity& block :
351 posCache_.at(shouldMoveTo) | 346 active_ |
352 entityView() | 347 entityView() |
348 views::atGridPos(shouldMoveTo) |
353 views::isOnLayer(entity.layer)) 349 views::isOnLayer(entity.layer))
354 { 350 {
355 if (!block.moving) 351 if (!block.moving)
@@ -389,8 +385,6 @@ bool Simulation::moveEntityOnGrid(
389 entity.destPos = shouldMoveTo; 385 entity.destPos = shouldMoveTo;
390 entity.movementTween = 0.0; 386 entity.movementTween = 0.0;
391 entity.moveDir = moveDir; 387 entity.moveDir = moveDir;
392
393 moveToCache_.set(entity.id, entity.destPos);
394 } 388 }
395 389
396 return true; 390 return true;
diff --git a/src/simulation.h b/src/simulation.h index a205796..096377c 100644 --- a/src/simulation.h +++ b/src/simulation.h
@@ -4,7 +4,6 @@
4#include "entity.h" 4#include "entity.h"
5#include "renderer.h" 5#include "renderer.h"
6#include "schedule.h" 6#include "schedule.h"
7#include "grid_cache.h"
8#include <range/v3/all.hpp> 7#include <range/v3/all.hpp>
9#include <vector> 8#include <vector>
10#include <deque> 9#include <deque>
@@ -76,9 +75,6 @@ private:
76 std::vector<Entity> entities_; 75 std::vector<Entity> entities_;
77 std::deque<id_type> available_; 76 std::deque<id_type> available_;
78 std::set<id_type> active_; 77 std::set<id_type> active_;
79
80 mutable GridCache<id_type> posCache_ { level_ };
81 mutable GridCache<id_type> moveToCache_ { level_ };
82}; 78};
83 79
84#endif /* end of include guard: SIMULATION_H_7BF6EEA4 */ 80#endif /* end of include guard: SIMULATION_H_7BF6EEA4 */
diff --git a/src/vector.h b/src/vector.h index 10fe4da..a94aea0 100644 --- a/src/vector.h +++ b/src/vector.h
@@ -112,6 +112,11 @@ public:
112 return vec2(x() * other.x(), y() * other.y()); 112 return vec2(x() * other.x(), y() * other.y());
113 } 113 }
114 114
115 constexpr bool operator==(const vec2& other) const
116 {
117 return (x() == other.x()) && (y() == other.y());
118 }
119
115}; 120};
116 121
117using vec2s = vec2<size_t>; 122using vec2s = vec2<size_t>;
diff --git a/src/views.h b/src/views.h index 69bd2b5..540dc52 100644 --- a/src/views.h +++ b/src/views.h
@@ -55,6 +55,20 @@ namespace views {
55 }); 55 });
56 } 56 }
57 57
58 inline auto atGridPos(vec2s pos)
59 {
60 return ranges::view::filter([pos] (const Entity& entity) {
61 return entity.gridPos == pos;
62 });
63 }
64
65 inline auto isMovingTo(vec2s pos)
66 {
67 return ranges::view::filter([pos] (const Entity& entity) {
68 return entity.moving && entity.destPos == pos;
69 });
70 }
71
58 72
59 73
60} 74}