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

172 lines
4.8 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* 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 "tile.h"
#include "item.h"
2011-08-31 17:03:33 +02:00
#include "thingstype.h"
#include "map.h"
2011-08-20 04:08:27 +02:00
#include "game.h"
#include "localplayer.h"
2011-08-31 22:22:57 +02:00
#include "effect.h"
2011-08-20 04:08:27 +02:00
#include <framework/graphics/fontmanager.h>
2011-08-15 16:11:24 +02:00
2011-08-31 01:39:14 +02:00
Tile::Tile(const Position& position)
2011-08-15 16:11:24 +02:00
{
2011-08-31 22:22:57 +02:00
m_drawElevation = 0;
2011-08-31 01:39:14 +02:00
m_position = position;
2011-08-15 16:11:24 +02:00
}
void Tile::draw(int x, int y)
{
2011-08-31 22:22:57 +02:00
m_drawElevation = 0;
2011-08-20 04:08:27 +02:00
2011-08-31 22:22:57 +02:00
// first bottom items
for(const ThingPtr& thing : m_things) {
const ThingType& type = thing->getType();
if(thing->asCreature() || type.isOnTop)
continue;
thing->draw(x - m_drawElevation, y - m_drawElevation);
m_drawElevation += type.elevation;
}
2011-08-20 04:08:27 +02:00
2011-08-31 22:22:57 +02:00
// creatures
for(const ThingPtr& thing : m_things) {
if(thing->asCreature())
thing->draw(x - m_drawElevation, y - m_drawElevation);
}
2011-08-20 04:08:27 +02:00
2011-08-31 22:22:57 +02:00
// effects
for(const EffectPtr& effect : m_effects)
effect->draw(x - m_drawElevation, y - m_drawElevation);
2011-08-20 04:08:27 +02:00
2011-08-31 22:22:57 +02:00
// top items
for(const ThingPtr& thing : m_things) {
const ThingType& type = thing->getType();
if(type.isOnTop)
thing->draw(x - m_drawElevation, y - m_drawElevation);
}
2011-08-31 22:22:57 +02:00
}
2011-08-31 22:22:57 +02:00
void Tile::clean()
{
m_things.clear();
m_effects.clear();
}
2011-08-31 01:39:14 +02:00
2011-08-31 22:22:57 +02:00
void Tile::addEffect(const EffectPtr& effect)
{
m_effects.push_back(effect);
effect->setPosition(m_position);
}
2011-08-20 04:08:27 +02:00
2011-08-31 22:22:57 +02:00
void Tile::removeEffect(const EffectPtr& effect)
{
auto it = std::find(m_effects.begin(), m_effects.end(), effect);
if(it != m_effects.end()) {
m_effects.erase(it);
2011-08-31 01:39:14 +02:00
}
}
2011-08-31 22:22:57 +02:00
ThingPtr Tile::addThing(const ThingPtr& thing, int stackPos)
2011-08-15 16:11:24 +02:00
{
if(!thing)
2011-08-31 22:22:57 +02:00
return nullptr;
if(stackPos < 0) {
stackPos = 0;
int priority = thing->getStackPriority();
for(stackPos = 0; stackPos < (int)m_things.size(); ++stackPos) {
int otherPriority = m_things[stackPos]->getStackPriority();
if(otherPriority > priority || (otherPriority == priority && otherPriority == 5))
break;
2011-08-15 16:11:24 +02:00
}
2011-08-31 22:22:57 +02:00
} else if(stackPos > (int)m_things.size())
stackPos = m_things.size();
ThingPtr oldObject;
if(stackPos < (int)m_things.size())
oldObject = m_things[stackPos];
m_things.insert(m_things.begin() + stackPos, thing);
thing->setPosition(m_position);
return oldObject;
}
2011-08-31 22:22:57 +02:00
ThingPtr Tile::getThing(int stackPos)
{
2011-08-31 22:22:57 +02:00
if(stackPos >= 0 && stackPos < (int)m_things.size())
return m_things[stackPos];
return nullptr;
2011-08-15 16:11:24 +02:00
}
2011-08-31 22:22:57 +02:00
ThingPtr Tile::removeThing(int stackPos)
2011-08-15 16:11:24 +02:00
{
2011-08-31 22:22:57 +02:00
ThingPtr oldObject;
if(stackPos >= 0 && stackPos < (int)m_things.size()) {
oldObject = m_things[stackPos];
m_things.erase(m_things.begin() + stackPos);
}
2011-08-31 22:22:57 +02:00
return oldObject;
}
2011-08-31 22:22:57 +02:00
ThingPtr Tile::removeThing(const ThingPtr& thing)
2011-08-17 07:04:45 +02:00
{
2011-08-31 22:22:57 +02:00
ThingPtr oldObject;
auto it = std::find(m_things.begin(), m_things.end(), thing);
if(it != m_things.end()) {
oldObject = *it;
m_things.erase(it);
2011-08-17 07:04:45 +02:00
}
2011-08-31 22:22:57 +02:00
return oldObject;
2011-08-17 07:04:45 +02:00
}
2011-08-31 22:22:57 +02:00
std::vector<CreaturePtr> Tile::getCreatures()
{
2011-08-31 22:22:57 +02:00
std::vector<CreaturePtr> creatures;
for(const ThingPtr& thing : m_things) {
if(CreaturePtr creature = thing->asCreature())
creatures.push_back(creature);
}
return creatures;
2011-08-15 16:11:24 +02:00
}
2011-08-31 22:22:57 +02:00
ItemPtr Tile::getGround()
2011-08-15 16:11:24 +02:00
{
2011-08-31 22:22:57 +02:00
ThingPtr firstObject = getThing(0);
if(!firstObject)
return nullptr;
const ThingType& type = firstObject->getType();
if(type.isGround)
return firstObject->asItem();
return nullptr;
2011-08-15 16:11:24 +02:00
}
2011-08-31 01:39:14 +02:00
2011-08-31 22:22:57 +02:00
bool Tile::isFullyOpaque()
2011-08-31 01:39:14 +02:00
{
2011-08-31 22:22:57 +02:00
ThingPtr firstObject = getThing(0);
if(firstObject) {
const ThingType& type = firstObject->getType();
if(type.isGround && !type.isTranslucent)
return true;
}
2011-08-31 01:39:14 +02:00
return false;
}