From 3504fd5080dbcfd0172299c5c6d13895e53ad163 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 23 Feb 2019 12:15:46 -0500 Subject: 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. --- src/grid_cache.h | 59 ------------------------------------------------------ src/simulation.cpp | 22 ++++++++------------ src/simulation.h | 4 ---- src/vector.h | 5 +++++ src/views.h | 14 +++++++++++++ 5 files changed, 27 insertions(+), 77 deletions(-) delete mode 100644 src/grid_cache.h (limited to 'src') 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 @@ -#ifndef GRID_CACHE_H_67BBE74D -#define GRID_CACHE_H_67BBE74D - -#include -#include -#include "level.h" - -template -class GridCache { -public: - - explicit GridCache(const Level& level) : width_(level.getSize().w()) - { - } - - void set(T value, vec2s pos) - { - if (reverse_.count(value)) - { - size_t oldPosIndex = reverse_.at(value); - lookup_[oldPosIndex].erase(value); - } - - size_t newPosIndex = getIndex(pos); - lookup_[newPosIndex].insert(value); - reverse_[value] = newPosIndex; - } - - void remove(T value) - { - if (reverse_.count(value)) - { - size_t index = reverse_.at(value); - lookup_[index].erase(value); - reverse_.erase(value); - } - } - - const std::unordered_set& at(vec2s pos) const - { - size_t index = getIndex(pos); - - return lookup_[index]; - } - -private: - - inline size_t getIndex(const vec2s& pos) const - { - return pos.x() + pos.y() * width_; - } - - size_t width_; - - mutable std::unordered_map> lookup_; - std::unordered_map reverse_; -}; - -#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( crate2.canBePushedBy.insert(ColliderType::crate); crate2.canBePushedBy.insert(ColliderType::train); crate2.gridPos = vec2s { 6, 7 }; - - - for (Entity& entity : active_ | entityView()) - { - posCache_.set(entity.id, entity.gridPos); - } } void Simulation::tick( @@ -132,8 +126,9 @@ void Simulation::tick( vec2s lookPos = posInDir(entity.gridPos, lookDir); for (Entity& block : - posCache_.at(lookPos) | + active_ | entityView() | + views::atGridPos(lookPos) | views::isNotMoving() | views::canBePushedBy(entity.colliderType) | views::isOnLayer(entity.layer)) @@ -185,8 +180,9 @@ void Simulation::tick( views::isNotMoving()) { auto tracks = - posCache_.at(entity.gridPos) | + active_ | entityView() | + views::atGridPos(entity.gridPos) | views::isTrack(); if (!ranges::empty(tracks)) @@ -233,8 +229,6 @@ void Simulation::tick( { entity.moving = false; entity.gridPos = entity.destPos; - posCache_.set(entity.id, entity.gridPos); - moveToCache_.remove(entity.id); } } @@ -340,16 +334,18 @@ bool Simulation::moveEntityOnGrid( // Can't move into a space that something else is already moving into. if (!ranges::empty( - moveToCache_.at(shouldMoveTo) | + active_ | entityView() | + views::isMovingTo(shouldMoveTo) | views::isOnLayer(entity.layer))) { return false; } for (Entity& block : - posCache_.at(shouldMoveTo) | + active_ | entityView() | + views::atGridPos(shouldMoveTo) | views::isOnLayer(entity.layer)) { if (!block.moving) @@ -389,8 +385,6 @@ bool Simulation::moveEntityOnGrid( entity.destPos = shouldMoveTo; entity.movementTween = 0.0; entity.moveDir = moveDir; - - moveToCache_.set(entity.id, entity.destPos); } 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 @@ #include "entity.h" #include "renderer.h" #include "schedule.h" -#include "grid_cache.h" #include #include #include @@ -76,9 +75,6 @@ private: std::vector entities_; std::deque available_; std::set active_; - - mutable GridCache posCache_ { level_ }; - mutable GridCache moveToCache_ { level_ }; }; #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: return vec2(x() * other.x(), y() * other.y()); } + constexpr bool operator==(const vec2& other) const + { + return (x() == other.x()) && (y() == other.y()); + } + }; using vec2s = vec2; 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 { }); } + inline auto atGridPos(vec2s pos) + { + return ranges::view::filter([pos] (const Entity& entity) { + return entity.gridPos == pos; + }); + } + + inline auto isMovingTo(vec2s pos) + { + return ranges::view::filter([pos] (const Entity& entity) { + return entity.moving && entity.destPos == pos; + }); + } + } -- cgit 1.4.1