tibia-client/src/otclient/core/map.cpp

534 lines
16 KiB
C++
Raw Normal View History

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"
#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>
2011-12-29 19:18:12 +01:00
2011-08-15 23:02:52 +02:00
Map g_map;
2012-01-30 01:00:12 +01:00
void Map::addMapView(const MapViewPtr& mapView)
{
2012-01-30 01:00:12 +01:00
m_mapViews.push_back(mapView);
}
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-01-30 04:11:05 +01:00
void Map::load()
{
if(!g_resources.fileExists("/map.otcmap"))
return;
std::stringstream in;
g_resources.loadFile("/map.otcmap", in);
while(!in.eof()) {
Position pos;
in.read((char*)&pos, sizeof(pos));
uint16 id;
in.read((char*)&id, sizeof(id));
while(id != 0xFFFF) {
2012-01-30 19:18:10 +01:00
ItemPtr item = Item::create(id);
if(item->isStackable() || item->isFluidContainer() || item->isFluid()) {
uint8 countOrSubType;
in.read((char*)&countOrSubType, sizeof(countOrSubType));
item->setCountOrSubType(countOrSubType);
2012-01-30 19:18:10 +01:00
}
addThing(item, pos, 255);
2012-01-30 04:11:05 +01:00
in.read((char*)&id, sizeof(id));
}
}
}
void Map::save()
{
std::stringstream out;
for(auto& pair : m_tiles) {
Position pos = pair.first;
TilePtr tile = pair.second;
2012-01-30 19:18:10 +01:00
if(!tile || tile->isEmpty())
2012-01-30 04:11:05 +01:00
continue;
out.write((char*)&pos, sizeof(pos));
uint16 id;
for(const ThingPtr& thing : tile->getThings()) {
if(ItemPtr item = thing->asItem()) {
id = item->getId();
out.write((char*)&id, sizeof(id));
2012-01-30 19:18:10 +01:00
if(item->isStackable() || item->isFluidContainer() || item->isFluid()) {
uint8 countOrSubType = item->getCountOrSubType();
out.write((char*)&countOrSubType, sizeof(countOrSubType));
2012-01-30 19:18:10 +01:00
}
2012-01-30 04:11:05 +01:00
}
}
id = 0xFFFF;
out.write((char*)&id, sizeof(id));
}
g_resources.saveFile("/map.otcmap", out);
}
2011-08-31 22:22:57 +02:00
void Map::clean()
{
cleanDynamicThings();
2011-08-31 22:22:57 +02:00
m_tiles.clear();
}
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-01-30 01:00:12 +01:00
for(int i=0;i<=Otc::MAX_Z;++i)
m_floorMissiles[i].clear();
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-17 07:04:45 +02:00
if(!thing)
return;
Position oldPos = thing->getPosition();
2012-02-02 21:10:14 +01:00
TilePtr tile = getOrCreateTile(pos);
2011-12-27 03:18:15 +01:00
if(MissilePtr missile = thing->asMissile()) {
2012-01-30 01:00:12 +01:00
m_floorMissiles[pos.z].push_back(missile);
} else if(AnimatedTextPtr animatedText = thing->asAnimatedText()) {
2011-12-26 12:53:16 +01:00
m_animatedTexts.push_back(animatedText);
2012-01-30 01:00:12 +01:00
} else if(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);
2012-01-30 01:00:12 +01:00
} else {
2011-12-27 03:18:15 +01:00
tile->addThing(thing, stackPos);
}
2011-11-08 02:44:30 +01:00
2012-01-30 01:00:12 +01:00
thing->startAnimation();
thing->setPosition(pos);
if(CreaturePtr creature = thing->asCreature()) {
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-31 22:22:57 +02:00
ThingPtr Map::getThing(const Position& pos, int stackPos)
{
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;
}
2012-01-30 01:00:12 +01:00
bool Map::removeThing(const ThingPtr& thing)
2011-08-17 07:04:45 +02:00
{
if(!thing)
2012-01-30 01:00:12 +01:00
return false;
2012-02-09 08:52:52 +01:00
notificateTileUpdateToMapViews(thing->getPosition());
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;
}
} else if(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)
{
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-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;
}
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);
}
void Map::cleanTile(const Position& pos)
{
2012-01-30 01:00:12 +01:00
if(TilePtr tile = getTile(pos)) {
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-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
}
CreaturePtr Map::getCreatureById(uint32 id)
{
2012-01-30 01:00:12 +01:00
LocalPlayerPtr localPlayer = g_game.getLocalPlayer();
if(localPlayer && localPlayer->getId() == id)
return localPlayer;
return m_knownCreatures[id];
}
void Map::removeCreatureById(uint32 id)
{
2012-01-30 01:00:12 +01:00
if(id == 0)
return;
if(CreaturePtr creature = m_knownCreatures[id])
creature->setRemoved(true);
2012-01-30 01:00:12 +01:00
m_knownCreatures.erase(id);
}
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
for(const auto& pair : m_knownCreatures) {
const CreaturePtr& creature = pair.second;
if(creature) {
if(!isAwareOfPosition(creature->getPosition())) {
removeThing(creature);
2012-01-30 19:18:10 +01:00
}
}
else
logTraceError("invalid creature");
2012-01-30 19:18:10 +01: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
g_eventDispatcher.addEvent([this] {
LocalPlayerPtr localPlayer = g_game.getLocalPlayer();
if(!localPlayer || localPlayer->getPosition() == m_centralPosition)
return;
TilePtr tile = getTile(localPlayer->getPosition());
if(!tile || tile->hasThing(localPlayer))
return;
localPlayer->setPosition(m_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-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-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();
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)
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)
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;
}
// pathfinding using A* search algorithm
// as described in http://en.wikipedia.org/wiki/A*_search_algorithm
std::vector<Otc::Direction> Map::findPath(const Position& startPos, const Position& goalPos, int maxSteps)
{
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;
}
};
std::vector<Otc::Direction> dirs;
2012-03-23 23:02:58 +01:00
if(startPos == goalPos || startPos.z != goalPos.z || startPos.distance(goalPos) > maxSteps)
return dirs;
std::unordered_map<Position, Node*, PositionHasher> nodes;
std::priority_queue<Node*, std::vector<Node*>, LessNode> searchList;
Node *currentNode = new Node(startPos);
currentNode->pos = startPos;
nodes[startPos] = currentNode;
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);
if(!tile || (!tile->isPathable() && neighborPos != goalPos) || (!tile->isWalkable() && neighborPos == goalPos))
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);
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());
}
for(auto it : nodes)
delete it.second;
return dirs;
}