From 6a584f49867811e2c122a3eb01111b325d1ed6a8 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Fri, 31 Aug 2012 23:41:56 +0200 Subject: Allow map objects as warp targets This patch allows map objects as warp targets. For use: - Create object in tiled with type="WARP_DEST" - Set name to anything you want - Create usual WARP object - Leave out the DEST_{X,Y} part - Add DEST_NAME property with the name of the first object This requires the game server to parse all maps at startup. Change is tested. Reviewed-by: bjorn. --- src/game-server/mapcomposite.cpp | 63 +++++++++++++++++++++++++++++++--------- src/game-server/mapcomposite.h | 5 +++- src/game-server/mapmanager.cpp | 3 ++ 3 files changed, 57 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp index a1e4781..d684fd7 100644 --- a/src/game-server/mapcomposite.cpp +++ b/src/game-server/mapcomposite.cpp @@ -460,8 +460,9 @@ Script::Ref MapComposite::mInitializeCallback; Script::Ref MapComposite::mUpdateCallback; MapComposite::MapComposite(int id, const std::string &name): - mMap(NULL), - mContent(NULL), + mActive(false), + mMap(0), + mContent(0), mName(name), mID(id) { @@ -473,13 +474,18 @@ MapComposite::~MapComposite() delete mContent; } -bool MapComposite::activate() +bool MapComposite::readMap() { - assert(!isActive()); - std::string file = "maps/" + mName + ".tmx"; mMap = MapReader::readMap(file); + return mMap; +} + +bool MapComposite::activate() +{ + assert(!isActive()); + if (!mMap) return false; @@ -506,6 +512,8 @@ bool MapComposite::activate() s->execute(); } + mActive = true; + return true; } @@ -728,7 +736,7 @@ void MapComposite::initializeContent() { mContent = new MapContent(mMap); - const std::vector &objects = mMap->getObjects(); + const std::vector &objects = mMap->getObjects(); for (size_t i = 0; i < objects.size(); ++i) { @@ -738,19 +746,48 @@ void MapComposite::initializeContent() if (utils::compareStrI(type, "WARP") == 0) { std::string destMapName = object->getProperty("DEST_MAP"); - int destX = utils::stringToInt(object->getProperty("DEST_X")); - int destY = utils::stringToInt(object->getProperty("DEST_Y")); + std::string destMapObjectName = object->getProperty("DEST_NAME"); - if (!destMapName.empty() && destX && destY) + MapComposite *destMap = MapManager::getMap(destMapName); + int destX = 0; + int destY = 0; + + if (destMap && !destMapObjectName.empty()) { - if (MapComposite *destMap = MapManager::getMap(destMapName)) + const std::vector &destObjects = + destMap->getMap()->getObjects(); + + std::vector::const_iterator it, it_end; + for (it = destObjects.begin(), it_end = destObjects.end(); + it != it_end; ++it) { - WarpAction *action = new WarpAction(destMap, destX, destY); - insert(new TriggerArea(this, object->getBounds(), - action, false)); + const MapObject *destObject = *it; + if (utils::compareStrI(destObject->getType(), + "WARP_DEST") == 0 && + utils::compareStrI(destObject->getName(), + destMapObjectName) == 0) + { + const Rectangle &rect = destObject->getBounds(); + destX = rect.x + rect.w / 2; + destY = rect.y + rect.h / 2; + break; + } } } else + { + destX = utils::stringToInt(object->getProperty("DEST_X")); + destY = utils::stringToInt(object->getProperty("DEST_Y")); + } + + + if (destMap && destX && destY) + { + WarpAction *action = new WarpAction(destMap, destX, destY); + insert(new TriggerArea(this, object->getBounds(), + action, false)); + } + else { LOG_WARN("Unrecognized warp format"); } diff --git a/src/game-server/mapcomposite.h b/src/game-server/mapcomposite.h index 079e5e4..d61f8ce 100644 --- a/src/game-server/mapcomposite.h +++ b/src/game-server/mapcomposite.h @@ -234,6 +234,8 @@ class MapComposite MapComposite(int id, const std::string &name); ~MapComposite(); + bool readMap(); + /** * Loads the map and initializes the map content. Should only be called * once! @@ -253,7 +255,7 @@ class MapComposite * Returns whether the map is active on this server or not. */ bool isActive() const - { return mMap; } + { return mActive; } /** * Gets the game ID of this map. @@ -366,6 +368,7 @@ class MapComposite void callMapVariableCallback(const std::string &key, const std::string &value); + bool mActive; /**< Status of map. */ Map *mMap; /**< Actual map. */ MapContent *mContent; /**< Entities on the map. */ std::string mName; /**< Name of the map. */ diff --git a/src/game-server/mapmanager.cpp b/src/game-server/mapmanager.cpp index 4d7b25b..b3b2931 100644 --- a/src/game-server/mapmanager.cpp +++ b/src/game-server/mapmanager.cpp @@ -80,6 +80,9 @@ int MapManager::initialize(const std::string &mapReferenceFile) if (mapFileExists) { maps[id] = new MapComposite(id, name); + if (!maps[id]->readMap()) + LOG_FATAL("Failed to load map \"" << name << "\"!"); + ++loadedMaps; } } -- cgit