/* Copyright (c) 2014, Matthias Schiffer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include #include #include #include #include #include #include "CollisionType.hpp" #include "Entity.hpp" namespace RPGEdit { namespace Model { class _Map { protected: std::vector tileset; size_t width, height; std::vector collision; std::vector> tiles; _Map(size_t width0, size_t height0, size_t layers) : width(width0), height(height0), collision(width*height) { for (size_t i = 0; i < layers; i++) tiles.emplace_back(width*height); } }; class Map : private _Map { private: struct EntityState { Position position; Direction direction; uint64_t transitionStart = 0; uint64_t transitionEnd = 0; EntityState(const Position &position0) : position(position0) {} }; std::vector> entities; std::unordered_map, std::unordered_set> positions; std::unordered_map entityStates; void addEntityPosition(const Position &position, Entity *entity) { positions[position].insert(entity); } void removeEntityPosition(const Position &position, Entity *entity) { auto it = positions.find(position); it->second.erase(entity); if (it->second.empty()) positions.erase(it); } void pushEntity(Entity *entity, EntityState &&state) { entities.push_back(std::unique_ptr(entity)); addEntityPosition(state.position, entity); entityStates.emplace(entity, std::move(state)); } void copyEntities(const Map &other) { for (auto &e : other.entities) pushEntity(new Entity(*e), EntityState(other.entityStates.at(e.get()))); } bool hasTransitionTo(const Position &p, Direction dir) const { auto it = positions.find(p+dir); if (it == positions.end()) return false; for (const Entity *entity : it->second) { const EntityState &state = entityStates.at(entity); if (state.transitionEnd && (state.direction == -dir)) return true; } return false; } bool hasTransitionTo(const Position &p) const { if (hasTransitionTo(p, Direction::NORTH)) return true; if (hasTransitionTo(p, Direction::EAST)) return true; if (hasTransitionTo(p, Direction::SOUTH)) return true; if (hasTransitionTo(p, Direction::WEST)) return true; return false; } Map(size_t width0, size_t height0, size_t layers) : _Map(width0, height0, layers) { } public: Map & operator=(const Map &other) { static_cast<_Map>(*this) = other; copyEntities(other); return *this; } Map(const Map &other) : _Map(other) { copyEntities(other); } Map & operator=(Map &&other) { static_cast<_Map>(*this) = std::move(other); entities = std::move(other.entities); positions = std::move(other.positions); entityStates = std::move(other.entityStates); return *this; } Map(Map &&other) : _Map(std::move(other)) { entities = std::move(other.entities); positions = std::move(other.positions); entityStates = std::move(other.entityStates); } std::vector & getTileset() { return tileset; } const std::vector & getTileset() const { return tileset; } const std::vector> & getEntities() const { return entities; } Entity * addEntity(const std::string &name, const Position &pos) { Entity *e = new Entity(name); pushEntity(e, EntityState(pos)); return e; } size_t getWidth() const { return width; } size_t getHeight() const { return height; } size_t getLayerCount() const { return tiles.size(); } void setCollisionAt(const Position &p, CollisionType value) { if (p.x < 0 || size_t(p.x) >= width || p.y < 0 || size_t(p.y) >= height) throw std::range_error("Map::setCollisionAt: bad coordinates"); collision[p.y*width + p.x] = value; } bool isBlocked(const Position &p) const { if (p.x < 0 || size_t(p.x) >= width || p.y < 0 || size_t(p.y) >= height) return true; if (positions.find(p) != positions.end()) return true; if (hasTransitionTo(p)) return true; return collision[p.y*width + p.x] == CollisionType::BLOCKED; } void setTileAt(size_t layer, const Position &p, uint32_t value) { if (layer >= tiles.size() || p.x < 0 || size_t(p.x) >= width || p.y < 0 || size_t(p.y) >= height) throw std::range_error("Map::setTileAt: bad coordinates"); tiles[layer][p.y*width + p.x] = value; } uint32_t getTileAt(size_t layer, const Position &p) const { if (layer >= tiles.size() || p.x < 0 || size_t(p.x) >= width || p.y < 0 || size_t(p.y) >= height) return 0; return tiles[layer][p.y*width + p.x]; } Entity * getEntityAt(const Position &p) { auto it = positions.find(p); if (it == positions.end()) return nullptr; return *it->second.begin(); } Position getEntityPosition(const Entity *entity) const { return entityStates.at(entity).position; } Position getEntityPosition(const Entity *entity, uint64_t time) const; bool moveEntity(Entity *entity, Direction dir, uint64_t start, uint64_t end); void moveEntityTo(Entity *entity, Position pos); void finishEntityTransition(Entity *entity); static std::unique_ptr load(const std::string &name); }; } }