From 0ad45d6826f2ac1e4d7441c773112b43da28b64f Mon Sep 17 00:00:00 2001 From: BenDol Date: Sat, 31 May 2014 15:46:55 +1200 Subject: [PATCH] Added 2 global functions for Position & g_map.getTiles(). --- src/client/declarations.h | 2 ++ src/client/luafunctions.cpp | 6 +++++- src/client/map.cpp | 30 ++++++++++++++++++++++++++ src/client/map.h | 1 + src/client/mapview.cpp | 1 - src/client/position.h | 42 +++++++++++++++++++++++-------------- src/client/tile.h | 2 +- 7 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/client/declarations.h b/src/client/declarations.h index b2edbdbe..95c8a783 100644 --- a/src/client/declarations.h +++ b/src/client/declarations.h @@ -51,6 +51,7 @@ class House; class Town; class CreatureType; class Spawn; +class TileBlock; typedef stdext::shared_object_ptr MapViewPtr; typedef stdext::shared_object_ptr LightViewPtr; @@ -80,6 +81,7 @@ typedef std::vector ItemTypeList; typedef std::list HouseList; typedef std::list TownList; typedef std::list ItemList; +typedef std::list TileList; typedef std::vector ItemVector; typedef std::unordered_map TileMap; typedef std::unordered_map CreatureMap; diff --git a/src/client/luafunctions.cpp b/src/client/luafunctions.cpp index 431b2a8b..63209a12 100644 --- a/src/client/luafunctions.cpp +++ b/src/client/luafunctions.cpp @@ -83,7 +83,7 @@ void Client::registerLuaFunctions() g_lua.bindSingletonFunction("g_houses", "removeHouse", &HouseManager::removeHouse, &g_houses); g_lua.bindSingletonFunction("g_houses", "getHouseList", &HouseManager::getHouseList, &g_houses); g_lua.bindSingletonFunction("g_houses", "filterHouses", &HouseManager::filterHouses, &g_houses); - g_lua.bindSingletonFunction("g_houses", "sort", &HouseManager::sort, &g_houses); + g_lua.bindSingletonFunction("g_houses", "sort", &HouseManager::sort, &g_houses); g_lua.registerSingletonClass("g_towns"); g_lua.bindSingletonFunction("g_towns", "getTown", &TownManager::getTown, &g_towns); @@ -115,6 +115,7 @@ void Client::registerLuaFunctions() g_lua.bindSingletonFunction("g_map", "cleanTile", &Map::cleanTile, &g_map); g_lua.bindSingletonFunction("g_map", "cleanTexts", &Map::cleanTexts, &g_map); g_lua.bindSingletonFunction("g_map", "getTile", &Map::getTile, &g_map); + g_lua.bindSingletonFunction("g_map", "getTiles", &Map::getTiles, &g_map); g_lua.bindSingletonFunction("g_map", "setCentralPosition", &Map::setCentralPosition, &g_map); g_lua.bindSingletonFunction("g_map", "getCentralPosition", &Map::getCentralPosition, &g_map); g_lua.bindSingletonFunction("g_map", "getCreatureById", &Map::getCreatureById, &g_map); @@ -307,6 +308,8 @@ void Client::registerLuaFunctions() g_lua.bindSingletonFunction("g_shaders", "getShader", &ShaderManager::getShader, &g_shaders); g_lua.bindGlobalFunction("getOufitColor", Outfit::getColor); + g_lua.bindGlobalFunction("getAngleFromPos", Position::getAngleFromPositions); + g_lua.bindGlobalFunction("getDirectionFromPos", Position::getDirectionFromPositions); g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return ProtocolGamePtr(new ProtocolGame); }); @@ -651,6 +654,7 @@ void Client::registerLuaFunctions() g_lua.bindClassMemberFunction("getCreatures", &Tile::getCreatures); g_lua.bindClassMemberFunction("getGround", &Tile::getGround); g_lua.bindClassMemberFunction("isWalkable", &Tile::isWalkable); + g_lua.bindClassMemberFunction("isHouseTile", &Tile::isHouseTile); g_lua.bindClassMemberFunction("isFullGround", &Tile::isFullGround); g_lua.bindClassMemberFunction("isFullyOpaque", &Tile::isFullyOpaque); g_lua.bindClassMemberFunction("isLookPossible", &Tile::isLookPossible); diff --git a/src/client/map.cpp b/src/client/map.cpp index 367d6a33..5133e7dc 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -318,6 +318,36 @@ const TilePtr& Map::getTile(const Position& pos) return m_nulltile; } +const TileList Map::getTiles(int floor/* = -1*/) +{ + TileList tiles; + if(floor > Otc::MAX_Z) { + return tiles; + } + else if(floor < 0) { + // Search all floors + for(uint8_t z = 0; z <= Otc::MAX_Z; ++z) { + for(const auto& pair : m_tileBlocks[z]) { + const TileBlock& block = pair.second; + for(const TilePtr& tile : block.getTiles()) { + if(tile != nullptr) + tiles.push_back(tile); + } + } + } + } + else { + for(const auto& pair : m_tileBlocks[floor]) { + const TileBlock& block = pair.second; + for(const TilePtr& tile : block.getTiles()) { + if(tile != nullptr) + tiles.push_back(tile); + } + } + } + return tiles; +} + void Map::cleanTile(const Position& pos) { if(!pos.isMapPosition()) diff --git a/src/client/map.h b/src/client/map.h index 6de2fc08..6836a1ea 100644 --- a/src/client/map.h +++ b/src/client/map.h @@ -186,6 +186,7 @@ public: const TilePtr& createTileEx(const Position& pos, const Items&... items); const TilePtr& getOrCreateTile(const Position& pos); const TilePtr& getTile(const Position& pos); + const TileList getTiles(int floor = -1); void cleanTile(const Position& pos); // tile zone related diff --git a/src/client/mapview.cpp b/src/client/mapview.cpp index 9103a73e..bba7fbf2 100644 --- a/src/client/mapview.cpp +++ b/src/client/mapview.cpp @@ -467,7 +467,6 @@ void MapView::updateGeometry(const Size& visibleDimension, const Size& optimized } */ - m_viewMode = viewMode; m_visibleDimension = visibleDimension; m_drawDimension = drawDimension; diff --git a/src/client/position.h b/src/client/position.h index c8ac5c98..4585fd34 100644 --- a/src/client/position.h +++ b/src/client/position.h @@ -110,20 +110,6 @@ public: return pos; } - double getAngleFromPosition(const Position& position) const { - // Returns angle in radians from 0 to 2Pi. -1 means positions are equal. - int dx = position.x - x; - int dy = position.y - y; - if(dx == 0 && dy == 0) - return -1; - - float angle = std::atan2(dy * -1, dx); - if(angle < 0) - angle += 2 * Fw::pi; - - return angle; - } - std::vector translatedToDirections(const std::vector& dirs) const { Position lastPos = *this; std::vector positions; @@ -143,8 +129,28 @@ public: return positions; } - Otc::Direction getDirectionFromPosition(const Position& position) const { - float angle = getAngleFromPosition(position) * RAD_TO_DEC; + static double getAngleFromPositions(const Position& fromPos, const Position& toPos) { + // Returns angle in radians from 0 to 2Pi. -1 means positions are equal. + int dx = toPos.x - fromPos.x; + int dy = toPos.y - fromPos.y; + if(dx == 0 && dy == 0) + return -1; + + float angle = std::atan2(dy * -1, dx); + if(angle < 0) + angle += 2 * Fw::pi; + + return angle; + } + + double getAngleFromPosition(const Position& position) const { + return getAngleFromPositions(*this, position); + } + + static Otc::Direction getDirectionFromPositions(const Position& fromPos, + const Position& toPos) + { + float angle = getAngleFromPositions(fromPos, toPos) * RAD_TO_DEC; if(angle >= 360 - 22.5 || angle < 0 + 22.5) return Otc::East; @@ -166,6 +172,10 @@ public: return Otc::InvalidDirection; } + Otc::Direction getDirectionFromPosition(const Position& position) const { + return getDirectionFromPositions(*this, position); + } + bool isMapPosition() const { return (x >=0 && y >= 0 && z >= 0 && x < 65535 && y < 65535 && z <= Otc::MAX_Z); } bool isValid() const { return !(x == 65535 && y == 65535 && z == 255); } float distance(const Position& pos) const { return sqrt(pow((pos.x - x), 2) + pow((pos.y - y), 2)); } diff --git a/src/client/tile.h b/src/client/tile.h index a197beef..c0432f69 100644 --- a/src/client/tile.h +++ b/src/client/tile.h @@ -120,7 +120,7 @@ public: void setHouseId(uint32 hid) { m_houseId = hid; } uint32 getHouseId() { return m_houseId; } - bool isHouseTile() const { return m_houseId != 0 && (m_flags & TILESTATE_HOUSE) == TILESTATE_HOUSE; } + bool isHouseTile() { return m_houseId != 0 && (m_flags & TILESTATE_HOUSE) == TILESTATE_HOUSE; } void select() { m_selected = true; } void unselect() { m_selected = false; }