From b9b9a32b831c25c3bd389c1c2f6f45b1a3b05722 Mon Sep 17 00:00:00 2001 From: Ahmed Samy Date: Wed, 4 Dec 2013 14:29:23 +0200 Subject: [PATCH] Spawns: add function to find a spawn to place a creature on --- src/client/creatures.cpp | 14 ++++++++++++++ src/client/creatures.h | 1 + src/client/luafunctions.cpp | 1 + 3 files changed, 16 insertions(+) diff --git a/src/client/creatures.cpp b/src/client/creatures.cpp index 9a9b2fb0..57d64283 100644 --- a/src/client/creatures.cpp +++ b/src/client/creatures.cpp @@ -369,6 +369,20 @@ SpawnPtr CreatureManager::getSpawn(const Position& centerPos) return nullptr; } +SpawnPtr CreatureManager::getSpawnForPlacePos(const Position& pos) +{ + for (const auto& pair : m_spawns) { + const Position& centerPos = pair.first; + const SpawnPtr& spawn = pair.second; + + if (isInZone(pos, centerPos, spawn->getRadius())) + return spawn; + } + + g_logger.debug(stdext::format("failed to find spawn at %s", stdext::to_string(pos))); + return nullptr; +} + SpawnPtr CreatureManager::addSpawn(const Position& centerPos, int radius) { auto iter = m_spawns.find(centerPos); diff --git a/src/client/creatures.h b/src/client/creatures.h index b20b1d58..4e2fb531 100644 --- a/src/client/creatures.h +++ b/src/client/creatures.h @@ -121,6 +121,7 @@ public: const CreatureTypePtr& getCreatureByLook(int look); SpawnPtr getSpawn(const Position& centerPos); + SpawnPtr getSpawnForPlacePos(const Position& pos); SpawnPtr addSpawn(const Position& centerPos, int radius); bool isLoaded() { return m_loaded; } diff --git a/src/client/luafunctions.cpp b/src/client/luafunctions.cpp index f71dace8..031a19de 100644 --- a/src/client/luafunctions.cpp +++ b/src/client/luafunctions.cpp @@ -155,6 +155,7 @@ void Client::registerLuaFunctions() g_lua.bindSingletonFunction("g_creatures", "getCreatureByName", &CreatureManager::getCreatureByName, &g_creatures); g_lua.bindSingletonFunction("g_creatures", "getCreatureByLook", &CreatureManager::getCreatureByLook, &g_creatures); g_lua.bindSingletonFunction("g_creatures", "getSpawn", &CreatureManager::getSpawn, &g_creatures); + g_lua.bindSingletonFunction("g_creatures", "getSpawnForPlacePos", &CreatureManager::getSpawnForPlacePos, &g_creatures); g_lua.bindSingletonFunction("g_creatures", "addSpawn", &CreatureManager::addSpawn, &g_creatures); g_lua.bindSingletonFunction("g_creatures", "loadMonsters", &CreatureManager::loadMonsters, &g_creatures); g_lua.bindSingletonFunction("g_creatures", "loadNpcs", &CreatureManager::loadNpcs, &g_creatures);