2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-15 16:11:24 +02:00
|
|
|
#include "map.h"
|
|
|
|
#include "game.h"
|
2011-08-15 23:02:52 +02:00
|
|
|
#include "localplayer.h"
|
2011-08-29 16:14:21 +02:00
|
|
|
#include "tile.h"
|
2011-09-02 00:00:46 +02:00
|
|
|
#include "item.h"
|
2011-11-08 02:44:30 +01:00
|
|
|
#include "missile.h"
|
2011-12-29 18:45:59 +01:00
|
|
|
#include "statictext.h"
|
2011-12-29 19:18:12 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
#include <framework/core/eventdispatcher.h>
|
|
|
|
#include "mapview.h"
|
2012-01-30 04:11:05 +01:00
|
|
|
#include <framework/core/resourcemanager.h>
|
2012-06-21 04:31:29 +02:00
|
|
|
#include <framework/core/filestream.h>
|
2012-07-09 08:46:11 +02:00
|
|
|
#include <framework/core/binarytree.h>
|
2012-07-14 19:17:03 +02:00
|
|
|
#include <framework/core/application.h>
|
2012-07-16 04:41:38 +02:00
|
|
|
#include <framework/xml/tinyxml.h>
|
2011-12-29 19:18:12 +01:00
|
|
|
|
2011-08-15 23:02:52 +02:00
|
|
|
Map g_map;
|
|
|
|
|
2012-06-21 19:54:20 +02:00
|
|
|
void Map::terminate()
|
|
|
|
{
|
|
|
|
clean();
|
|
|
|
}
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
void Map::addMapView(const MapViewPtr& mapView)
|
2011-11-23 05:27:58 +01:00
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
m_mapViews.push_back(mapView);
|
2011-11-23 05:27:58 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
void Map::removeMapView(const MapViewPtr& mapView)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
auto it = std::find(m_mapViews.begin(), m_mapViews.end(), mapView);
|
|
|
|
if(it != m_mapViews.end())
|
|
|
|
m_mapViews.erase(it);
|
|
|
|
}
|
2011-12-29 18:45:59 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
void Map::notificateTileUpdateToMapViews(const Position& pos)
|
|
|
|
{
|
|
|
|
for(const MapViewPtr& mapView : m_mapViews)
|
|
|
|
mapView->onTileUpdate(pos);
|
2011-08-31 01:39:14 +02:00
|
|
|
}
|
|
|
|
|
2012-07-15 01:20:38 +02:00
|
|
|
void Map::loadOtbm(const std::string& fileName)
|
2012-01-30 04:11:05 +01:00
|
|
|
{
|
2012-06-21 05:05:44 +02:00
|
|
|
FileStreamPtr fin = g_resources.openFile(fileName);
|
2012-07-14 19:29:42 +02:00
|
|
|
if(!fin)
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception(stdext::format("Unable to load map '%s'", fileName));
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-09 08:59:16 +02:00
|
|
|
fin->cache();
|
2012-07-14 19:29:42 +02:00
|
|
|
if(!g_things.isOtbLoaded())
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception("OTB isn't loaded yet to load a map.");
|
|
|
|
|
2012-07-14 19:29:42 +02:00
|
|
|
if(fin->getU32())
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception("Unknown file version detected");
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-09 08:46:11 +02:00
|
|
|
BinaryTreePtr root = fin->getBinaryTree();
|
2012-07-14 19:29:42 +02:00
|
|
|
if(root->getU8())
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception("could not read root property!");
|
|
|
|
|
2012-07-09 08:46:11 +02:00
|
|
|
uint32 headerVersion = root->getU32();
|
2012-07-14 19:29:42 +02:00
|
|
|
if(!headerVersion || headerVersion > 3)
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception(stdext::format("Unknown OTBM version detected: %u.", headerVersion));
|
|
|
|
|
2012-07-15 01:20:38 +02:00
|
|
|
m_width = root->getU16();
|
|
|
|
m_height = root->getU16();
|
|
|
|
dump << "Map size: " << m_width << "x" << m_height;
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-09 08:46:11 +02:00
|
|
|
uint32 headerMajorItems = root->getU8();
|
2012-07-14 19:29:42 +02:00
|
|
|
if(headerMajorItems < 3) {
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception(stdext::format("This map needs to be upgraded. read %d what it's supposed to be: %u",
|
|
|
|
headerMajorItems, g_things.getOtbMajorVersion()));
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 19:29:42 +02:00
|
|
|
if(headerMajorItems > g_things.getOtbMajorVersion()) {
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception(stdext::format("This map was saved with different OTB version. read %d what it's supposed to be: %d",
|
|
|
|
headerMajorItems, g_things.getOtbMajorVersion()));
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
|
|
|
|
2012-07-09 08:59:16 +02:00
|
|
|
root->skip(3);
|
|
|
|
uint32 headerMinorItems = root->getU32();
|
2012-07-14 19:29:42 +02:00
|
|
|
if(headerMinorItems > g_things.getOtbMinorVersion()) {
|
2012-07-18 08:04:27 +02:00
|
|
|
g_logger.warning(stdext::format("This map needs an updated OTB. read %d what it's supposed to be: %d or less",
|
2012-07-09 08:59:16 +02:00
|
|
|
headerMinorItems, g_things.getOtbMinorVersion()));
|
2012-07-15 01:20:38 +02:00
|
|
|
}
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-09 08:46:11 +02:00
|
|
|
BinaryTreePtr node = root->getChildren()[0];
|
2012-07-14 19:29:42 +02:00
|
|
|
if(node->getU8() != OTBM_MAP_DATA)
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception("Could not read root data node");
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-09 08:46:11 +02:00
|
|
|
while (node->canRead()) {
|
2012-07-09 08:59:16 +02:00
|
|
|
uint8 attribute = node->getU8();
|
|
|
|
std::string tmp = node->getString();
|
2012-06-21 05:05:44 +02:00
|
|
|
switch (attribute) {
|
|
|
|
case OTBM_ATTR_DESCRIPTION:
|
2012-07-15 01:20:38 +02:00
|
|
|
m_description += tmp + "\n";
|
2012-06-21 05:05:44 +02:00
|
|
|
break;
|
|
|
|
case OTBM_ATTR_SPAWN_FILE:
|
|
|
|
m_spawnFile = fileName.substr(0, fileName.rfind('/') + 1) + tmp;
|
|
|
|
break;
|
|
|
|
case OTBM_ATTR_HOUSE_FILE:
|
|
|
|
m_houseFile = fileName.substr(0, fileName.rfind('/') + 1) + tmp;
|
|
|
|
break;
|
|
|
|
default:
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception(stdext::format("Invalid attribute '%c'", attribute));
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 08:04:27 +02:00
|
|
|
for(const BinaryTreePtr &nodeMapData : node->getChildren()) {
|
2012-07-09 08:46:11 +02:00
|
|
|
uint8 mapDataType = nodeMapData->getU8();
|
2012-07-14 19:29:42 +02:00
|
|
|
if(mapDataType == OTBM_TILE_AREA) {
|
2012-07-09 08:46:11 +02:00
|
|
|
uint16 baseX = nodeMapData->getU16(), baseY = nodeMapData->getU16();
|
|
|
|
uint8 pz = nodeMapData->getU8();
|
|
|
|
|
2012-07-18 08:04:27 +02:00
|
|
|
for(const BinaryTreePtr &nodeTile : nodeMapData->getChildren()) {
|
2012-07-09 08:46:11 +02:00
|
|
|
uint8 type = nodeTile->getU8();
|
2012-07-14 19:29:42 +02:00
|
|
|
if(type != OTBM_TILE && type != OTBM_HOUSETILE)
|
2012-07-15 01:20:38 +02:00
|
|
|
stdext::throw_exception(stdext::format("invalid node tile type %d", (int)type));
|
|
|
|
|
|
|
|
HousePtr house = nullptr;
|
|
|
|
uint32 flags = TILESTATE_NONE;
|
|
|
|
|
|
|
|
uint16 px = baseX + nodeTile->getU8(), py = baseY + nodeTile->getU8();
|
|
|
|
Position pos(px, py, pz);
|
|
|
|
|
2012-07-14 19:29:42 +02:00
|
|
|
if(type == OTBM_HOUSETILE) {
|
2012-07-15 01:20:38 +02:00
|
|
|
uint32 hId = nodeTile->getU32();
|
2012-07-15 15:23:13 +02:00
|
|
|
TilePtr tile = getOrCreateTile(pos);
|
2012-07-14 19:29:42 +02:00
|
|
|
if(!(house = m_houses.getHouse(hId))) {
|
2012-07-15 01:20:38 +02:00
|
|
|
house = HousePtr(new House(hId));
|
|
|
|
m_houses.addHouse(house);
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
2012-07-15 01:20:38 +02:00
|
|
|
house->setTile(tile);
|
|
|
|
}
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-18 08:04:27 +02:00
|
|
|
while(nodeTile->canRead()) {
|
2012-07-15 01:20:38 +02:00
|
|
|
uint8 tileAttr = nodeTile->getU8();
|
|
|
|
switch (tileAttr) {
|
2012-06-21 05:05:44 +02:00
|
|
|
case OTBM_ATTR_TILE_FLAGS: {
|
2012-07-09 08:46:11 +02:00
|
|
|
uint32 _flags = nodeTile->getU32();
|
2012-07-14 19:29:42 +02:00
|
|
|
if((_flags & TILESTATE_PROTECTIONZONE) == TILESTATE_PROTECTIONZONE)
|
2012-06-21 05:05:44 +02:00
|
|
|
flags |= TILESTATE_PROTECTIONZONE;
|
2012-07-14 19:29:42 +02:00
|
|
|
else if((_flags & TILESTATE_OPTIONALZONE) == TILESTATE_OPTIONALZONE)
|
2012-06-21 05:05:44 +02:00
|
|
|
flags |= TILESTATE_OPTIONALZONE;
|
2012-07-14 19:29:42 +02:00
|
|
|
else if((_flags & TILESTATE_HARDCOREZONE) == TILESTATE_HARDCOREZONE)
|
2012-07-15 01:20:38 +02:00
|
|
|
flags |= TILESTATE_HARDCOREZONE;
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-14 19:29:42 +02:00
|
|
|
if((_flags & TILESTATE_NOLOGOUT) == TILESTATE_NOLOGOUT)
|
2012-06-21 05:05:44 +02:00
|
|
|
flags |= TILESTATE_NOLOGOUT;
|
|
|
|
|
2012-07-14 19:29:42 +02:00
|
|
|
if((_flags & TILESTATE_REFRESH) == TILESTATE_REFRESH)
|
2012-06-21 05:05:44 +02:00
|
|
|
flags |= TILESTATE_REFRESH;
|
2012-07-15 01:20:38 +02:00
|
|
|
|
2012-06-21 05:05:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-07-15 01:20:38 +02:00
|
|
|
case OTBM_ATTR_ITEM: {
|
2012-07-15 15:23:13 +02:00
|
|
|
addThing(Item::createFromOtb(nodeTile->getU16()), pos);
|
2012-07-15 01:20:38 +02:00
|
|
|
break;
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
2012-07-18 08:04:27 +02:00
|
|
|
default: {
|
2012-07-15 01:20:38 +02:00
|
|
|
stdext::throw_exception(stdext::format("invalid tile attribute %d at pos %d, %d, %d",
|
|
|
|
(int)tileAttr, px, py, pz));
|
2012-07-18 08:04:27 +02:00
|
|
|
}
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
2012-07-15 01:20:38 +02:00
|
|
|
}
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-18 08:04:27 +02:00
|
|
|
for(const BinaryTreePtr &nodeItem : nodeTile->getChildren()) {
|
2012-07-14 19:29:42 +02:00
|
|
|
if(nodeItem->getU8() != OTBM_ITEM)
|
2012-07-15 01:20:38 +02:00
|
|
|
stdext::throw_exception("invalid item node");
|
|
|
|
|
|
|
|
ItemPtr item = Item::createFromOtb(nodeItem->getU16());
|
|
|
|
item->unserializeItem(nodeItem);
|
2012-07-15 15:23:13 +02:00
|
|
|
|
2012-07-14 19:29:42 +02:00
|
|
|
if(item->isContainer()) {
|
2012-07-15 01:20:38 +02:00
|
|
|
// This is a temporary way for reading container items.
|
|
|
|
MapContainerPtr mapContainer(new MapContainer);
|
2012-07-18 08:04:27 +02:00
|
|
|
for(const BinaryTreePtr &insideItem : nodeItem->getChildren()) {
|
2012-07-14 19:29:42 +02:00
|
|
|
if(insideItem->getU8() != OTBM_ITEM)
|
2012-07-15 01:20:38 +02:00
|
|
|
stdext::throw_exception("invalid container item node");
|
|
|
|
|
|
|
|
ItemPtr newItem = Item::createFromOtb(insideItem->getU16());
|
|
|
|
newItem->unserializeItem(insideItem);
|
|
|
|
mapContainer->add(newItem);
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
2012-07-15 01:20:38 +02:00
|
|
|
m_containers.push_back(mapContainer);
|
2012-07-09 08:46:11 +02:00
|
|
|
}
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-14 19:29:42 +02:00
|
|
|
if(house) {
|
|
|
|
if(item->isMoveable()) {
|
2012-07-15 01:20:38 +02:00
|
|
|
g_logger.warning(stdext::format("Movable item found in house: %d at pos %d %d %d - escaping...", item->getId(),
|
|
|
|
px, py, pz));
|
2012-07-18 08:04:27 +02:00
|
|
|
item.reset();
|
2012-07-14 19:29:42 +02:00
|
|
|
} else if(item->isDoor())
|
2012-07-15 01:20:38 +02:00
|
|
|
house->addDoor(item->getDoorId(), pos);
|
2012-07-15 15:23:13 +02:00
|
|
|
}
|
2012-07-15 01:20:38 +02:00
|
|
|
|
2012-07-15 15:23:13 +02:00
|
|
|
addThing(item, pos);
|
|
|
|
}
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-15 15:23:13 +02:00
|
|
|
if(const TilePtr& tile = getTile(pos))
|
|
|
|
tile->setFlags((tileflags_t)flags);
|
2012-07-09 08:46:11 +02:00
|
|
|
}
|
2012-07-14 19:29:42 +02:00
|
|
|
} else if(mapDataType == OTBM_TOWNS) {
|
2012-07-09 08:59:16 +02:00
|
|
|
TownPtr town = nullptr;
|
2012-07-18 08:04:27 +02:00
|
|
|
for(const BinaryTreePtr &nodeTown : nodeMapData->getChildren()) {
|
2012-07-14 19:29:42 +02:00
|
|
|
if(nodeTown->getU8() != OTBM_TOWN)
|
2012-07-15 01:20:38 +02:00
|
|
|
stdext::throw_exception("invalid town node.");
|
|
|
|
|
|
|
|
uint32 townId = nodeTown->getU32();
|
|
|
|
std::string townName = nodeTown->getString();
|
|
|
|
Position townCoords(nodeTown->getU16(), nodeTown->getU16(), nodeTown->getU8());
|
2012-07-14 19:29:42 +02:00
|
|
|
if(!(town = m_towns.getTown(townId))) {
|
2012-07-15 01:20:38 +02:00
|
|
|
town = TownPtr(new Town(townId, townName, townCoords));
|
|
|
|
m_towns.addTown(town);
|
2012-07-18 08:04:27 +02:00
|
|
|
} // a map editor cannot be that dumb and write duplicate towns
|
2012-07-09 08:46:11 +02:00
|
|
|
}
|
2012-07-14 19:29:42 +02:00
|
|
|
} else if(mapDataType == OTBM_WAYPOINTS && headerVersion > 1) {
|
2012-07-18 08:04:27 +02:00
|
|
|
for(const BinaryTreePtr &nodeWaypoint : nodeMapData->getChildren()) {
|
2012-07-14 19:29:42 +02:00
|
|
|
if(nodeWaypoint->getU8() != OTBM_WAYPOINT)
|
2012-07-15 01:20:38 +02:00
|
|
|
stdext::throw_exception("invalid waypoint node.");
|
|
|
|
|
|
|
|
std::string name = nodeWaypoint->getString();
|
|
|
|
Position waypointPos(nodeWaypoint->getU16(), nodeWaypoint->getU16(), nodeWaypoint->getU8());
|
2012-07-14 19:29:42 +02:00
|
|
|
if(waypointPos.isValid() && !name.empty() && m_waypoints.find(waypointPos) == m_waypoints.end())
|
2012-07-15 01:20:38 +02:00
|
|
|
m_waypoints.insert(std::make_pair(waypointPos, name));
|
2012-07-09 08:46:11 +02:00
|
|
|
}
|
|
|
|
} else
|
2012-07-09 08:59:16 +02:00
|
|
|
stdext::throw_exception("Unknown map data node");
|
2012-07-09 08:46:11 +02:00
|
|
|
}
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-07-15 15:23:13 +02:00
|
|
|
|
|
|
|
int numItems = 0;
|
|
|
|
for(const auto& it : m_tiles)
|
|
|
|
numItems += it.second->getThingCount();
|
|
|
|
|
|
|
|
g_logger.debug(stdext::format("Total items: %d", numItems));
|
|
|
|
g_logger.debug(stdext::format("Total tiles: %d", m_tiles.size()));
|
2012-07-09 08:59:16 +02:00
|
|
|
g_logger.debug("OTBM read successfully.");
|
|
|
|
fin->close();
|
2012-07-16 04:41:38 +02:00
|
|
|
|
|
|
|
loadSpawns(m_spawnFile);
|
|
|
|
m_houses.load(m_houseFile);
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
|
|
|
|
2012-07-15 01:20:38 +02:00
|
|
|
void Map::saveOtbm(const std::string &fileName)
|
|
|
|
{
|
|
|
|
// TODO: Continue sleepy work.
|
|
|
|
#if 0
|
|
|
|
/// TODO: Use binary trees for this
|
|
|
|
FileStreamPtr fin = g_resources.openFile(fileName);
|
|
|
|
if(!fin)
|
|
|
|
stdext::throw_exception(stdext::format("failed to open file '%s'", fileName));
|
|
|
|
|
|
|
|
std::string dir;
|
2012-07-14 19:29:42 +02:00
|
|
|
if(fileName.find_last_of('/') <= 0)
|
2012-07-15 01:20:38 +02:00
|
|
|
dir = g_resources.getWorkDir();
|
|
|
|
else
|
|
|
|
dir = fileName.substr(0, fileName.find_last_of('/'));
|
|
|
|
|
2012-07-14 19:29:42 +02:00
|
|
|
if(m_houseFile.empty())
|
2012-07-15 01:20:38 +02:00
|
|
|
m_houseFile = "houses.xml";
|
2012-07-14 19:29:42 +02:00
|
|
|
if(m_spawnFile.empty())
|
2012-07-15 01:20:38 +02:00
|
|
|
m_spawnFile = "spawns.xml";
|
|
|
|
|
|
|
|
#if 0
|
2012-07-14 19:29:42 +02:00
|
|
|
if(!m_houses->save(dir + "/" + m_houseFile))
|
2012-07-15 01:20:38 +02:00
|
|
|
;
|
2012-07-14 19:29:42 +02:00
|
|
|
if(!m_spawns->save(dir + "/" + m_spawnFile))
|
2012-07-15 01:20:38 +02:00
|
|
|
;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint32 ver;
|
2012-07-14 19:29:42 +02:00
|
|
|
if(g_things.getOtbMajorVersion() < 2)
|
2012-07-15 01:20:38 +02:00
|
|
|
ver =0;
|
2012-07-14 19:29:42 +02:00
|
|
|
else if(g_things.getOtbMajorVersion() < 10)
|
2012-07-15 01:20:38 +02:00
|
|
|
ver = 1;
|
|
|
|
else
|
|
|
|
ver = 2;
|
|
|
|
|
|
|
|
fin->addU32(0x00); // file version
|
|
|
|
{
|
|
|
|
fin->startNode(0x00); // root
|
|
|
|
fin->addU32(ver);
|
|
|
|
fin->addU16(m_width); // some random width.
|
|
|
|
fin->addU16(m_height); // some random height.
|
|
|
|
|
|
|
|
fin->addU32(g_things.getOtbMajorVersion());
|
|
|
|
fin->addU32(g_things.getOtbMinorVersion());
|
|
|
|
|
|
|
|
fin->startNode(OTBM_MAP_DATA); // map data node
|
|
|
|
{
|
|
|
|
// own description.
|
|
|
|
fin->addU8(OTBM_ATTR_DESCRIPTION);
|
|
|
|
fin->addString(m_description);
|
|
|
|
|
|
|
|
// special one
|
|
|
|
fin->addU8(OTBM_ATTR_DESCRIPTION);
|
|
|
|
fin->addString(stdext::format("Saved with %s v%d", g_app.getName(), stdext::unsafe_cast<int>(g_app.getVersion())));
|
|
|
|
|
|
|
|
// spawn file.
|
|
|
|
fin->addU8(OTBM_ATTR_SPAWN_FILE);
|
|
|
|
fin->addString(m_spawnFile);
|
|
|
|
|
|
|
|
// house file.
|
2012-07-14 19:29:42 +02:00
|
|
|
if(ver > 1) {
|
2012-07-15 01:20:38 +02:00
|
|
|
fin->addU8(OTBM_ATTR_HOUSE_FILE);
|
|
|
|
fin->addString(m_houseFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
Position pos(-1, -1, -1);
|
|
|
|
Boolean<true> first;
|
|
|
|
for (auto& pair : m_tiles) {
|
|
|
|
TilePtr tile = pair.second;
|
|
|
|
if(!tile || tile->isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Position tilePos = pair.first;
|
2012-07-14 19:29:42 +02:00
|
|
|
if(tilePos.x < pos.x || tilePos.x >= pos.x + 256 ||
|
2012-07-15 01:20:38 +02:00
|
|
|
tilePos.y < pos.y || tilePos.y >= pos.y + 256 ||
|
|
|
|
tilePos.z != pos.z) {
|
2012-07-14 19:29:42 +02:00
|
|
|
if(!first)
|
2012-07-15 01:20:38 +02:00
|
|
|
fin->endNode();
|
|
|
|
|
|
|
|
pos.x = tilePos.x & 0xFF00;
|
|
|
|
pos.y = tilePos.y & 0xFF00;
|
|
|
|
pos.z = tilePos.z;
|
|
|
|
fin->addU16(pos.x);
|
|
|
|
fin->addU16(pos.y);
|
|
|
|
fin->addU8(pos.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: hOUSES.
|
|
|
|
fin->startNode(OTBM_TILE);
|
|
|
|
fin->addU8(tilePos.x);
|
|
|
|
fin->addU8(tilePos.y);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// TODO: hOUSES again.
|
2012-07-14 19:29:42 +02:00
|
|
|
if(is house tile)
|
2012-07-15 01:20:38 +02:00
|
|
|
add u32 house id...;
|
|
|
|
#endif
|
2012-07-14 19:29:42 +02:00
|
|
|
if(tile->flags()) {
|
2012-07-15 01:20:38 +02:00
|
|
|
fin->addU8(OTBM_ATTR_TILE_FLAGS);
|
|
|
|
fin->addU32(tile->flags());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
|
|
|
|
2012-07-16 04:41:38 +02:00
|
|
|
void Map::loadSpawns(const std::string &fileName)
|
|
|
|
{
|
|
|
|
if(!m_monsters.isLoaded())
|
|
|
|
stdext::throw_exception("cannot load spawns; monsters aren't loaded.");
|
|
|
|
|
|
|
|
TiXmlDocument doc(fileName);
|
|
|
|
if(!doc.LoadFile())
|
|
|
|
stdext::throw_exception(stdext::format("cannot load spawns xml file '%s", fileName));
|
|
|
|
|
|
|
|
TiXmlElement* root = doc.FirstChildElement();
|
|
|
|
if(!root || root->ValueStr() != "spawns")
|
2012-07-17 21:31:55 +02:00
|
|
|
stdext::throw_exception("malformed spawns file");
|
2012-07-16 04:41:38 +02:00
|
|
|
|
|
|
|
for(TiXmlElement* node = root->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
|
|
|
if (node->ValueTStr() != "spawn")
|
2012-07-17 21:31:55 +02:00
|
|
|
stdext::throw_exception("invalid spawn node");
|
2012-07-16 04:41:38 +02:00
|
|
|
|
2012-07-18 00:03:46 +02:00
|
|
|
Position centerPos = node->readPos("center");
|
2012-07-16 04:41:38 +02:00
|
|
|
for(TiXmlElement* mType = node->FirstChildElement(); mType; mType = mType->NextSiblingElement()) {
|
|
|
|
if (mType->ValueStr() != "monster")
|
|
|
|
stdext::throw_exception("invalid spawn-subnode");
|
|
|
|
|
|
|
|
std::string mName = mType->Attribute("name");
|
2012-07-18 08:04:27 +02:00
|
|
|
MonsterTypePtr m = m_monsters.getMonster(mName);
|
2012-07-16 04:41:38 +02:00
|
|
|
if (!m)
|
|
|
|
stdext::throw_exception(stdext::format("unkown monster %s", mName));
|
|
|
|
|
2012-07-18 00:03:46 +02:00
|
|
|
Point off = mType->readPoint();
|
2012-07-16 04:41:38 +02:00
|
|
|
Position mPos(centerPos.x + off.x, centerPos.y + off.y, centerPos.z);
|
2012-07-18 08:04:27 +02:00
|
|
|
m->setPos(mPos);
|
2012-07-16 04:41:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 19:54:20 +02:00
|
|
|
bool Map::loadOtcm(const std::string& fileName)
|
2012-06-21 05:05:44 +02:00
|
|
|
{
|
2012-06-23 16:26:18 +02:00
|
|
|
try {
|
|
|
|
FileStreamPtr fin = g_resources.openFile(fileName);
|
2012-06-26 00:13:30 +02:00
|
|
|
if(!fin)
|
|
|
|
stdext::throw_exception("unable to open file");
|
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
fin->cache();
|
2012-06-23 16:26:18 +02:00
|
|
|
|
|
|
|
uint32 signature = fin->getU32();
|
|
|
|
if(signature != OTCM_SIGNATURE)
|
|
|
|
stdext::throw_exception("invalid otcm file");
|
2012-06-21 05:05:44 +02:00
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
uint16 start = fin->getU16();
|
2012-06-23 16:26:18 +02:00
|
|
|
uint16 version = fin->getU16();
|
2012-06-23 23:30:54 +02:00
|
|
|
fin->getU32(); // flags
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
switch(version) {
|
|
|
|
case 1: {
|
|
|
|
fin->getString(); // description
|
|
|
|
uint32 datSignature = fin->getU32();
|
|
|
|
fin->getU16(); // protocol version
|
|
|
|
fin->getString(); // world name
|
|
|
|
|
|
|
|
if(datSignature != g_things.getDatSignature())
|
2012-07-09 08:59:16 +02:00
|
|
|
g_logger.warning("otcm map loaded was created with a different dat signature");
|
2012-06-23 16:26:18 +02:00
|
|
|
|
|
|
|
break;
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
2012-06-23 16:26:18 +02:00
|
|
|
default:
|
|
|
|
stdext::throw_exception("otcm version not supported");
|
|
|
|
break;
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
fin->seek(start);
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
while(true) {
|
|
|
|
Position pos;
|
2012-06-23 23:30:54 +02:00
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
pos.x = fin->getU16();
|
|
|
|
pos.y = fin->getU16();
|
|
|
|
pos.z = fin->getU8();
|
|
|
|
|
|
|
|
// end of file
|
|
|
|
if(!pos.isValid())
|
|
|
|
break;
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
uint16 id = fin->getU16();
|
|
|
|
|
|
|
|
// end of tile
|
|
|
|
if(id == 0xFFFF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
uint8 countOrSubType = fin->getU8();
|
|
|
|
|
|
|
|
ItemPtr item = Item::create(id);
|
2012-07-18 01:49:21 +02:00
|
|
|
if(item->isStackable() || item->isFluidContainer() || item->isSplash() || item->isChargeable())
|
2012-06-23 16:26:18 +02:00
|
|
|
item->setCountOrSubType(countOrSubType);
|
|
|
|
|
|
|
|
if(item->isValid())
|
2012-07-15 07:46:56 +02:00
|
|
|
addThing(item, pos);
|
2012-06-23 16:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fin->close();
|
2012-06-23 23:30:54 +02:00
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
return true;
|
|
|
|
} catch(stdext::exception& e) {
|
|
|
|
g_logger.error(stdext::format("failed to load OTCM map: %s", e.what()));
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-30 04:11:05 +01:00
|
|
|
}
|
|
|
|
|
2012-06-21 19:54:20 +02:00
|
|
|
void Map::saveOtcm(const std::string& fileName)
|
2012-01-30 04:11:05 +01:00
|
|
|
{
|
2012-06-23 16:26:18 +02:00
|
|
|
try {
|
2012-06-23 23:30:54 +02:00
|
|
|
g_clock.update();
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
FileStreamPtr fin = g_resources.createFile(fileName);
|
2012-06-23 23:30:54 +02:00
|
|
|
fin->cache();
|
|
|
|
|
|
|
|
//TODO: compression flag with zlib
|
|
|
|
uint32 flags = 0;
|
2012-06-23 16:26:18 +02:00
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
// header
|
2012-06-23 16:26:18 +02:00
|
|
|
fin->addU32(OTCM_SIGNATURE);
|
2012-06-23 23:30:54 +02:00
|
|
|
fin->addU16(0); // data start, will be overwritten later
|
2012-06-23 16:26:18 +02:00
|
|
|
fin->addU16(OTCM_VERSION);
|
2012-06-23 23:30:54 +02:00
|
|
|
fin->addU32(flags);
|
|
|
|
|
|
|
|
// version 1 header
|
|
|
|
fin->addString("OTCM 1.0"); // map description
|
2012-06-23 16:26:18 +02:00
|
|
|
fin->addU32(g_things.getDatSignature());
|
|
|
|
fin->addU16(g_game.getProtocolVersion());
|
|
|
|
fin->addString(g_game.getWorldName());
|
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
// go back and rewrite where the map data starts
|
|
|
|
uint32 start = fin->tell();
|
|
|
|
fin->seek(4);
|
|
|
|
fin->addU16(start);
|
|
|
|
fin->seek(start);
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
for(auto& pair : m_tiles) {
|
2012-06-23 23:30:54 +02:00
|
|
|
const TilePtr& tile = pair.second;
|
2012-06-23 16:26:18 +02:00
|
|
|
if(!tile || tile->isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Position pos = pair.first;
|
|
|
|
fin->addU16(pos.x);
|
|
|
|
fin->addU16(pos.y);
|
|
|
|
fin->addU8(pos.z);
|
|
|
|
|
|
|
|
const auto& list = tile->getThings();
|
|
|
|
auto first = std::find_if(list.begin(), list.end(), [](const ThingPtr& thing) { return thing->isItem(); });
|
|
|
|
for(auto it = first, end = list.end(); it != end; ++it) {
|
2012-07-15 16:28:15 +02:00
|
|
|
if(ItemPtr item = (*it)->asItem()) {
|
|
|
|
fin->addU16(item->getId());
|
|
|
|
fin->addU8(item->getCountOrSubType());
|
|
|
|
}
|
2012-01-30 04:11:05 +01:00
|
|
|
}
|
2012-06-23 16:26:18 +02:00
|
|
|
|
|
|
|
// end of tile
|
|
|
|
fin->addU16(0xFFFF);
|
2012-01-30 04:11:05 +01:00
|
|
|
}
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
// end of file
|
|
|
|
Position invalidPos;
|
|
|
|
fin->addU16(invalidPos.x);
|
|
|
|
fin->addU16(invalidPos.y);
|
|
|
|
fin->addU8(invalidPos.z);
|
|
|
|
|
|
|
|
fin->flush();
|
|
|
|
fin->close();
|
|
|
|
} catch(stdext::exception& e) {
|
|
|
|
g_logger.error(stdext::format("failed to save OTCM map: %s", e.what()));
|
|
|
|
}
|
2012-01-30 04:11:05 +01:00
|
|
|
}
|
|
|
|
|
2011-08-31 22:22:57 +02:00
|
|
|
void Map::clean()
|
|
|
|
{
|
2012-03-19 18:54:47 +01:00
|
|
|
cleanDynamicThings();
|
2011-08-31 22:22:57 +02:00
|
|
|
m_tiles.clear();
|
2012-07-09 08:59:16 +02:00
|
|
|
m_waypoints.clear();
|
2012-07-15 01:20:38 +02:00
|
|
|
|
|
|
|
// This is a fix to a segfault on exit.
|
|
|
|
m_towns.clear();
|
|
|
|
m_houses.clear();
|
2012-07-16 04:41:38 +02:00
|
|
|
m_monsters.clear();
|
2012-03-19 18:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Map::cleanDynamicThings()
|
|
|
|
{
|
|
|
|
for(const auto& pair : m_knownCreatures) {
|
|
|
|
const CreaturePtr& creature = pair.second;
|
|
|
|
removeThing(creature);
|
|
|
|
creature->setRemoved(true);
|
|
|
|
}
|
2012-05-10 01:04:09 +02:00
|
|
|
m_knownCreatures.clear();
|
2012-03-19 18:54:47 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
for(int i=0;i<=Otc::MAX_Z;++i)
|
|
|
|
m_floorMissiles[i].clear();
|
2012-06-09 02:40:22 +02:00
|
|
|
|
|
|
|
cleanTexts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::cleanTexts()
|
|
|
|
{
|
2012-01-19 05:12:53 +01:00
|
|
|
m_animatedTexts.clear();
|
|
|
|
m_staticTexts.clear();
|
2011-08-31 22:22:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Map::addThing(const ThingPtr& thing, const Position& pos, int stackPos)
|
2011-08-16 07:47:35 +02:00
|
|
|
{
|
2011-08-17 07:04:45 +02:00
|
|
|
if(!thing)
|
|
|
|
return;
|
|
|
|
|
2012-02-02 21:10:14 +01:00
|
|
|
TilePtr tile = getOrCreateTile(pos);
|
2011-12-27 03:18:15 +01:00
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
Position oldPos = thing->getPosition();
|
|
|
|
|
|
|
|
if(thing->isItem() || thing->isCreature() || thing->isEffect()) {
|
|
|
|
tile->addThing(thing, stackPos);
|
|
|
|
} else if(thing->isMissile()) {
|
|
|
|
m_floorMissiles[pos.z].push_back(thing->asMissile());
|
|
|
|
} else if(thing->isAnimatedText()) {
|
|
|
|
m_animatedTexts.push_back(thing->asAnimatedText());
|
|
|
|
} else if(thing->isStaticText()) {
|
|
|
|
StaticTextPtr staticText = thing->asStaticText();
|
2011-12-29 18:45:59 +01:00
|
|
|
bool mustAdd = true;
|
|
|
|
for(auto it = m_staticTexts.begin(), end = m_staticTexts.end(); it != end; ++it) {
|
|
|
|
StaticTextPtr cStaticText = *it;
|
2012-01-30 01:00:12 +01:00
|
|
|
if(cStaticText->getPosition() == pos) {
|
2011-12-29 18:45:59 +01:00
|
|
|
// try to combine messages
|
|
|
|
if(cStaticText->addMessage(staticText->getName(), staticText->getMessageType(), staticText->getFirstMessage())) {
|
|
|
|
mustAdd = false;
|
|
|
|
break;
|
2012-01-30 01:00:12 +01:00
|
|
|
} else {
|
2011-12-29 18:45:59 +01:00
|
|
|
// must add another message and rearrenge current
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mustAdd)
|
|
|
|
m_staticTexts.push_back(staticText);
|
2011-12-27 03:18:15 +01:00
|
|
|
}
|
2011-11-08 02:44:30 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
thing->startAnimation();
|
|
|
|
thing->setPosition(pos);
|
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
if(thing->isCreature()) {
|
|
|
|
CreaturePtr creature = thing->asCreature();
|
2012-05-10 00:19:05 +02:00
|
|
|
if(oldPos != pos) {
|
|
|
|
if(oldPos.isInRange(pos,1,1))
|
|
|
|
g_game.processCreatureMove(creature, oldPos, pos);
|
|
|
|
else
|
|
|
|
g_game.processCreatureTeleport(creature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
notificateTileUpdateToMapViews(pos);
|
2011-08-17 06:45:55 +02:00
|
|
|
}
|
|
|
|
|
2011-08-31 22:22:57 +02:00
|
|
|
ThingPtr Map::getThing(const Position& pos, int stackPos)
|
2011-08-17 06:45:55 +02:00
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
if(TilePtr tile = getTile(pos))
|
2011-08-31 22:22:57 +02:00
|
|
|
return tile->getThing(stackPos);
|
|
|
|
return nullptr;
|
2011-08-16 14:47:30 +02:00
|
|
|
}
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
bool Map::removeThing(const ThingPtr& thing)
|
2011-08-17 07:04:45 +02:00
|
|
|
{
|
2012-02-02 22:20:34 +01:00
|
|
|
if(!thing)
|
2012-01-30 01:00:12 +01:00
|
|
|
return false;
|
2012-02-02 22:20:34 +01:00
|
|
|
|
2012-02-09 08:52:52 +01:00
|
|
|
notificateTileUpdateToMapViews(thing->getPosition());
|
|
|
|
|
2012-02-02 22:20:34 +01:00
|
|
|
if(MissilePtr missile = thing->asMissile()) {
|
2012-02-08 22:23:15 +01:00
|
|
|
int z = missile->getPosition().z;
|
|
|
|
auto it = std::find(m_floorMissiles[z].begin(), m_floorMissiles[z].end(), missile);
|
|
|
|
if(it != m_floorMissiles[z].end()) {
|
|
|
|
m_floorMissiles[z].erase(it);
|
2012-01-30 01:00:12 +01:00
|
|
|
return true;
|
2011-11-08 02:44:30 +01:00
|
|
|
}
|
2012-01-30 01:00:12 +01:00
|
|
|
} else if(AnimatedTextPtr animatedText = thing->asAnimatedText()) {
|
2011-12-26 12:53:16 +01:00
|
|
|
auto it = std::find(m_animatedTexts.begin(), m_animatedTexts.end(), animatedText);
|
2012-01-30 01:00:12 +01:00
|
|
|
if(it != m_animatedTexts.end()) {
|
2011-12-26 12:53:16 +01:00
|
|
|
m_animatedTexts.erase(it);
|
2012-01-30 01:00:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if(StaticTextPtr staticText = thing->asStaticText()) {
|
2011-12-29 18:45:59 +01:00
|
|
|
auto it = std::find(m_staticTexts.begin(), m_staticTexts.end(), staticText);
|
2012-01-30 01:00:12 +01:00
|
|
|
if(it != m_staticTexts.end()) {
|
2011-12-29 18:45:59 +01:00
|
|
|
m_staticTexts.erase(it);
|
2012-01-30 01:00:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-15 07:46:56 +02:00
|
|
|
} else if(const TilePtr& tile = thing->getTile())
|
2012-01-30 01:00:12 +01:00
|
|
|
return tile->removeThing(thing);
|
2011-11-08 02:44:30 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
return false;
|
2011-08-17 07:04:45 +02:00
|
|
|
}
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
bool Map::removeThingByPos(const Position& pos, int stackPos)
|
2011-08-17 06:45:55 +02:00
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
if(TilePtr tile = getTile(pos))
|
|
|
|
return removeThing(tile->getThing(stackPos));
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-04 14:02:35 +01:00
|
|
|
|
2012-07-15 01:20:38 +02:00
|
|
|
template <typename... Items>
|
|
|
|
TilePtr Map::createTileEx(const Position& pos, const Items&... items)
|
2012-01-30 01:00:12 +01:00
|
|
|
{
|
2012-07-15 01:20:38 +02:00
|
|
|
TilePtr tile = getOrCreateTile(pos);
|
|
|
|
auto vec = {items...};
|
|
|
|
for (auto it : vec)
|
2012-07-15 07:46:56 +02:00
|
|
|
addThing(it, pos);
|
2012-07-09 08:59:16 +02:00
|
|
|
|
2012-07-15 01:20:38 +02:00
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
TilePtr Map::createTile(const Position& pos)
|
|
|
|
{
|
|
|
|
TilePtr tile = TilePtr(new Tile(pos));
|
|
|
|
m_tiles[pos] = tile;
|
2011-08-31 22:22:57 +02:00
|
|
|
return tile;
|
2011-08-17 06:45:55 +02:00
|
|
|
}
|
|
|
|
|
2012-01-31 18:06:55 +01:00
|
|
|
const TilePtr& Map::getTile(const Position& pos)
|
|
|
|
{
|
|
|
|
auto it = m_tiles.find(pos);
|
|
|
|
if(it != m_tiles.end())
|
|
|
|
return it->second;
|
|
|
|
static TilePtr nulltile;
|
|
|
|
return nulltile;
|
|
|
|
}
|
|
|
|
|
2012-02-02 21:10:14 +01:00
|
|
|
TilePtr Map::getOrCreateTile(const Position& pos)
|
|
|
|
{
|
|
|
|
const TilePtr& tile = getTile(pos);
|
|
|
|
if(tile)
|
|
|
|
return tile;
|
|
|
|
else
|
|
|
|
return createTile(pos);
|
|
|
|
}
|
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
void Map::cleanTile(const Position& pos)
|
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
if(TilePtr tile = getTile(pos)) {
|
2011-08-17 06:45:55 +02:00
|
|
|
tile->clean();
|
2012-02-02 21:10:14 +01:00
|
|
|
if(tile->canErase())
|
|
|
|
m_tiles.erase(m_tiles.find(pos));
|
2012-01-30 01:00:12 +01:00
|
|
|
|
|
|
|
notificateTileUpdateToMapViews(pos);
|
|
|
|
}
|
2011-08-17 06:45:55 +02:00
|
|
|
}
|
|
|
|
|
2011-08-31 22:22:57 +02:00
|
|
|
void Map::addCreature(const CreaturePtr& creature)
|
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
m_knownCreatures[creature->getId()] = creature;
|
2011-08-31 22:22:57 +02:00
|
|
|
}
|
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
CreaturePtr Map::getCreatureById(uint32 id)
|
|
|
|
{
|
2012-05-11 20:02:57 +02:00
|
|
|
auto it = m_knownCreatures.find(id);
|
|
|
|
if(it == m_knownCreatures.end())
|
|
|
|
return nullptr;
|
|
|
|
return it->second;
|
2011-08-17 06:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Map::removeCreatureById(uint32 id)
|
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
if(id == 0)
|
|
|
|
return;
|
2012-05-11 20:02:57 +02:00
|
|
|
|
|
|
|
auto it = m_knownCreatures.find(id);
|
|
|
|
if(it != m_knownCreatures.end())
|
|
|
|
it->second->setRemoved(true);
|
|
|
|
|
|
|
|
m_knownCreatures.erase(it);
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 19:18:10 +01:00
|
|
|
void Map::setCentralPosition(const Position& centralPosition)
|
|
|
|
{
|
|
|
|
m_centralPosition = centralPosition;
|
|
|
|
|
|
|
|
// remove creatures from tiles that we are not aware anymore
|
2012-04-29 04:02:50 +02:00
|
|
|
for(const auto& pair : m_knownCreatures) {
|
|
|
|
const CreaturePtr& creature = pair.second;
|
2012-05-11 20:02:57 +02:00
|
|
|
if(!isAwareOfPosition(creature->getPosition()))
|
|
|
|
removeThing(creature);
|
2012-01-30 19:18:10 +01:00
|
|
|
}
|
2012-05-10 00:19:05 +02:00
|
|
|
|
2012-05-10 01:04:09 +02:00
|
|
|
// this fixes local player position when the local player is removed from the map,
|
|
|
|
// the local player is removed from the map when there are too many creatures on his tile,
|
|
|
|
// so there is no enough stackpos to the server send him
|
2012-06-26 00:13:30 +02:00
|
|
|
g_dispatcher.addEvent([this] {
|
2012-05-10 00:19:05 +02:00
|
|
|
LocalPlayerPtr localPlayer = g_game.getLocalPlayer();
|
|
|
|
if(!localPlayer || localPlayer->getPosition() == m_centralPosition)
|
|
|
|
return;
|
2012-05-10 15:06:06 +02:00
|
|
|
TilePtr tile = localPlayer->getTile();
|
|
|
|
if(tile && tile->hasThing(localPlayer))
|
2012-05-10 00:19:05 +02:00
|
|
|
return;
|
2012-05-10 18:55:33 +02:00
|
|
|
|
|
|
|
Position oldPos = localPlayer->getPosition();
|
|
|
|
Position pos = m_centralPosition;
|
|
|
|
localPlayer->setPosition(pos);
|
|
|
|
if(oldPos != pos) {
|
|
|
|
if(oldPos.isInRange(pos,1,1))
|
|
|
|
g_game.processCreatureMove(localPlayer, oldPos, pos);
|
|
|
|
else
|
|
|
|
g_game.processCreatureTeleport(localPlayer);
|
|
|
|
}
|
2012-05-10 00:19:05 +02:00
|
|
|
});
|
2012-06-22 07:26:22 +02:00
|
|
|
|
|
|
|
for(const MapViewPtr& mapView : m_mapViews)
|
|
|
|
mapView->onMapCenterChange(centralPosition);
|
2012-01-30 19:18:10 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
std::vector<CreaturePtr> Map::getSpectators(const Position& centerPos, bool multiFloor)
|
|
|
|
{
|
|
|
|
return getSpectatorsInRange(centerPos, multiFloor, (Otc::VISIBLE_X_TILES - 1)/2, (Otc::VISIBLE_Y_TILES - 1)/2);
|
2011-08-17 06:45:55 +02:00
|
|
|
}
|
2011-08-31 01:39:14 +02:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
std::vector<CreaturePtr> Map::getSpectatorsInRange(const Position& centerPos, bool multiFloor, int xRange, int yRange)
|
2011-08-31 01:39:14 +02:00
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
return getSpectatorsInRangeEx(centerPos, multiFloor, xRange, xRange, yRange, yRange);
|
2011-08-31 01:39:14 +02:00
|
|
|
}
|
2011-11-23 04:11:49 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
std::vector<CreaturePtr> Map::getSpectatorsInRangeEx(const Position& centerPos, bool multiFloor, int minXRange, int maxXRange, int minYRange, int maxYRange)
|
2011-11-23 04:11:49 +01:00
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
int minZRange = 0;
|
|
|
|
int maxZRange = 0;
|
|
|
|
std::vector<CreaturePtr> creatures;
|
2011-11-23 04:11:49 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
if(multiFloor) {
|
|
|
|
minZRange = 0;
|
|
|
|
maxZRange = Otc::MAX_Z;
|
|
|
|
}
|
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
//TODO: optimize
|
2012-02-01 20:37:40 +01:00
|
|
|
//TODO: get creatures from other floors corretly
|
2012-02-01 23:46:31 +01:00
|
|
|
//TODO: delivery creatures in distance order
|
2012-02-01 20:37:40 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
for(int iz=-minZRange; iz<=maxZRange; ++iz) {
|
|
|
|
for(int iy=-minYRange; iy<=maxYRange; ++iy) {
|
|
|
|
for(int ix=-minXRange; ix<=maxXRange; ++ix) {
|
2012-01-30 19:18:10 +01:00
|
|
|
TilePtr tile = getTile(centerPos.translated(ix,iy,iz));
|
2012-01-30 01:00:12 +01:00
|
|
|
if(!tile)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto tileCreatures = tile->getCreatures();
|
2012-01-30 22:28:08 +01:00
|
|
|
creatures.insert(creatures.end(), tileCreatures.rbegin(), tileCreatures.rend());
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return creatures;
|
|
|
|
}
|
2011-11-23 04:11:49 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
bool Map::isLookPossible(const Position& pos)
|
|
|
|
{
|
|
|
|
TilePtr tile = getTile(pos);
|
|
|
|
return tile && tile->isLookPossible();
|
|
|
|
}
|
2011-11-23 04:11:49 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
bool Map::isCovered(const Position& pos, int firstFloor)
|
|
|
|
{
|
|
|
|
// check for tiles on top of the postion
|
|
|
|
Position tilePos = pos;
|
2012-01-30 19:18:10 +01:00
|
|
|
while(tilePos.coveredUp() && tilePos.z >= firstFloor) {
|
2012-01-30 01:00:12 +01:00
|
|
|
TilePtr tile = getTile(tilePos);
|
|
|
|
// the below tile is covered when the above tile has a full ground
|
|
|
|
if(tile && tile->isFullGround())
|
|
|
|
return true;
|
2011-12-25 00:14:12 +01:00
|
|
|
}
|
2012-01-30 01:00:12 +01:00
|
|
|
return false;
|
2011-11-23 04:11:49 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
bool Map::isCompletelyCovered(const Position& pos, int firstFloor)
|
2011-11-23 04:11:49 +01:00
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
Position tilePos = pos;
|
2012-01-30 19:18:10 +01:00
|
|
|
while(tilePos.coveredUp() && tilePos.z >= firstFloor) {
|
2012-01-30 01:00:12 +01:00
|
|
|
bool covered = true;
|
|
|
|
// check in 2x2 range tiles that has no transparent pixels
|
|
|
|
for(int x=0;x<2;++x) {
|
|
|
|
for(int y=0;y<2;++y) {
|
2012-02-01 04:47:00 +01:00
|
|
|
const TilePtr& tile = getTile(tilePos.translated(-x, -y));
|
2012-01-30 01:00:12 +01:00
|
|
|
if(!tile || !tile->isFullyOpaque()) {
|
|
|
|
covered = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(covered)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-11-23 04:11:49 +01:00
|
|
|
}
|
2012-01-30 19:18:10 +01:00
|
|
|
|
|
|
|
bool Map::isAwareOfPosition(const Position& pos)
|
|
|
|
{
|
|
|
|
if(pos.z < getFirstAwareFloor() || pos.z > getLastAwareFloor())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Position groundedPos = pos;
|
|
|
|
while(groundedPos.z != m_centralPosition.z) {
|
|
|
|
if(groundedPos.z > m_centralPosition.z)
|
|
|
|
groundedPos.coveredUp();
|
|
|
|
else
|
|
|
|
groundedPos.coveredDown();
|
|
|
|
}
|
|
|
|
return m_centralPosition.isInRange(groundedPos, Otc::AWARE_X_LEFT_TILES,
|
|
|
|
Otc::AWARE_X_RIGHT_TILES,
|
|
|
|
Otc::AWARE_Y_TOP_TILES,
|
|
|
|
Otc::AWARE_Y_BOTTOM_TILES);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Map::getFirstAwareFloor()
|
|
|
|
{
|
|
|
|
if(m_centralPosition.z > Otc::SEA_FLOOR)
|
2012-01-30 22:28:08 +01:00
|
|
|
return m_centralPosition.z-Otc::AWARE_UNDEGROUND_FLOOR_RANGE;
|
2012-01-30 19:18:10 +01:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Map::getLastAwareFloor()
|
|
|
|
{
|
|
|
|
if(m_centralPosition.z > Otc::SEA_FLOOR)
|
2012-01-30 22:28:08 +01:00
|
|
|
return std::min(m_centralPosition.z+Otc::AWARE_UNDEGROUND_FLOOR_RANGE, (int)Otc::MAX_Z);
|
2012-01-30 19:18:10 +01:00
|
|
|
else
|
|
|
|
return Otc::SEA_FLOOR;
|
|
|
|
}
|
2012-03-23 21:36:58 +01:00
|
|
|
|
2012-07-05 14:38:48 +02:00
|
|
|
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const Position& startPos, const Position& goalPos, int maxSteps)
|
2012-03-23 21:36:58 +01:00
|
|
|
{
|
2012-07-05 14:38:48 +02:00
|
|
|
// pathfinding using A* search algorithm
|
|
|
|
// as described in http://en.wikipedia.org/wiki/A*_search_algorithm
|
|
|
|
|
2012-03-23 21:36:58 +01:00
|
|
|
struct Node {
|
|
|
|
Node(const Position& pos) : cost(0), totalCost(0), steps(0), pos(pos), prev(nullptr), dir(Otc::InvalidDirection), evaluated(false) { }
|
|
|
|
bool operator<(const Node& other) const { return totalCost < other.totalCost; }
|
|
|
|
float cost;
|
|
|
|
float totalCost;
|
|
|
|
int steps;
|
|
|
|
Position pos;
|
|
|
|
Node *prev;
|
|
|
|
Otc::Direction dir;
|
|
|
|
bool evaluated;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LessNode : std::binary_function<Node*, Node*, bool> {
|
|
|
|
bool operator()(Node* a, Node* b) const {
|
|
|
|
return b->totalCost < a->totalCost;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-05 14:38:48 +02:00
|
|
|
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret;
|
|
|
|
std::vector<Otc::Direction>& dirs = std::get<0>(ret);
|
|
|
|
Otc::PathFindResult& result = std::get<1>(ret);
|
|
|
|
|
|
|
|
result = Otc::PATHFIND_OK;
|
|
|
|
|
|
|
|
if(startPos == goalPos) {
|
|
|
|
result = Otc::PATHFIND_SAME_POSITION;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(startPos.z != goalPos.z) {
|
|
|
|
result = Otc::PATHFIND_IMPOSSIBLE;
|
|
|
|
return ret;
|
|
|
|
}
|
2012-03-23 21:36:58 +01:00
|
|
|
|
2012-07-05 14:38:48 +02:00
|
|
|
if(startPos.distance(goalPos) > maxSteps) {
|
|
|
|
result = Otc::PATHFIND_TOO_FAR;
|
|
|
|
return ret;
|
|
|
|
}
|
2012-03-23 21:36:58 +01:00
|
|
|
|
|
|
|
std::unordered_map<Position, Node*, PositionHasher> nodes;
|
|
|
|
std::priority_queue<Node*, std::vector<Node*>, LessNode> searchList;
|
|
|
|
|
|
|
|
Node *currentNode = new Node(startPos);
|
|
|
|
currentNode->pos = startPos;
|
2012-04-02 19:53:25 +02:00
|
|
|
nodes[startPos] = currentNode;
|
2012-03-23 21:36:58 +01:00
|
|
|
Node *foundNode = nullptr;
|
|
|
|
while(currentNode && currentNode->steps < maxSteps) {
|
|
|
|
if(currentNode->pos == goalPos && (!foundNode || currentNode->cost < foundNode->cost))
|
|
|
|
foundNode = currentNode;
|
|
|
|
|
|
|
|
if(foundNode && currentNode->totalCost >= foundNode->cost)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for(int i=-1;i<=1;++i) {
|
|
|
|
for(int j=-1;j<=1;++j) {
|
|
|
|
if(i == 0 && j == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Position neighborPos = currentNode->pos.translated(i, j);
|
|
|
|
const TilePtr& tile = getTile(neighborPos);
|
2012-03-24 16:22:40 +01:00
|
|
|
if(!tile || (!tile->isPathable() && neighborPos != goalPos) || (!tile->isWalkable() && neighborPos == goalPos))
|
2012-03-23 21:36:58 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
float walkFactor;
|
|
|
|
Otc::Direction walkDir = currentNode->pos.getDirectionFromPosition(neighborPos);
|
|
|
|
if(walkDir >= Otc::NorthEast)
|
|
|
|
walkFactor = 3.0f;
|
|
|
|
else
|
|
|
|
walkFactor = 1.0f;
|
|
|
|
|
|
|
|
float cost = currentNode->cost + (tile->getGroundSpeed() * walkFactor) / 100.0f;
|
|
|
|
|
|
|
|
Node *neighborNode;
|
|
|
|
if(nodes.find(neighborPos) == nodes.end()) {
|
|
|
|
neighborNode = new Node(neighborPos);
|
|
|
|
nodes[neighborPos] = neighborNode;
|
|
|
|
} else {
|
|
|
|
neighborNode = nodes[neighborPos];
|
|
|
|
if(neighborNode->cost < cost)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
neighborNode->prev = currentNode;
|
|
|
|
neighborNode->cost = cost;
|
|
|
|
neighborNode->steps = currentNode->steps + 1;
|
2012-03-23 23:02:58 +01:00
|
|
|
neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos);
|
2012-03-23 21:36:58 +01:00
|
|
|
neighborNode->dir = walkDir;
|
|
|
|
neighborNode->evaluated = false;
|
|
|
|
searchList.push(neighborNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentNode->evaluated = true;
|
|
|
|
currentNode = nullptr;
|
|
|
|
while(searchList.size() > 0 && !currentNode) {
|
|
|
|
Node *node = searchList.top();
|
|
|
|
searchList.pop();
|
|
|
|
|
|
|
|
if(!node->evaluated)
|
|
|
|
currentNode = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(foundNode) {
|
|
|
|
currentNode = foundNode;
|
|
|
|
while(currentNode) {
|
|
|
|
dirs.push_back(currentNode->dir);
|
|
|
|
currentNode = currentNode->prev;
|
|
|
|
}
|
|
|
|
dirs.pop_back();
|
|
|
|
std::reverse(dirs.begin(), dirs.end());
|
2012-07-05 14:38:48 +02:00
|
|
|
} else
|
|
|
|
result = Otc::PATHFIND_NO_WAY;
|
2012-03-23 21:36:58 +01:00
|
|
|
|
|
|
|
for(auto it : nodes)
|
|
|
|
delete it.second;
|
|
|
|
|
2012-07-05 14:38:48 +02:00
|
|
|
return ret;
|
2012-03-23 21:36:58 +01:00
|
|
|
}
|
2012-07-17 21:31:55 +02:00
|
|
|
|
2012-07-18 08:04:27 +02:00
|
|
|
MonsterTypePtr Map::getMonster(const std::string& name)
|
2012-07-17 21:31:55 +02:00
|
|
|
{
|
2012-07-18 08:04:27 +02:00
|
|
|
return m_monsters.getMonster(stdext::trim(stdext::tolower(name)));
|
2012-07-17 21:31:55 +02:00
|
|
|
}
|