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

315 lines
9.2 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 "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"
#include <otclient/net/protocolgame.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
}
2011-09-02 00:00:46 +02:00
void Tile::draw(const Point& p)
{
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) {
2011-12-02 03:29:05 +01:00
ThingType *type = thing->getType();
if(!type->properties[ThingType::IsGround] && !type->properties[ThingType::IsGroundBorder] && !type->properties[ThingType::IsOnBottom])
break;
2011-11-08 02:44:30 +01:00
thing->draw(p - m_drawElevation);
2011-12-02 03:29:05 +01:00
m_drawElevation += type->parameters[ThingType::Elevation];
if(m_drawElevation > MAX_DRAW_ELEVATION)
m_drawElevation = MAX_DRAW_ELEVATION;
}
// now common items
for(auto it = m_things.rbegin(); it != m_things.rend(); ++it) {
const ThingPtr& thing = *it;
2011-12-02 03:29:05 +01:00
ThingType *type = thing->getType();
if(thing->asCreature() || type->properties[ThingType::IsOnTop] || type->properties[ThingType::IsOnBottom] || type->properties[ThingType::IsGroundBorder] || type->properties[ThingType::IsGround])
break;
2011-11-08 02:44:30 +01:00
thing->draw(p - m_drawElevation);
2011-12-02 03:29:05 +01:00
m_drawElevation += type->parameters[ThingType::Elevation];
if(m_drawElevation > MAX_DRAW_ELEVATION)
m_drawElevation = MAX_DRAW_ELEVATION;
}
2011-08-20 04:08:27 +02:00
2011-09-01 16:49:51 +02:00
// we can render creatures in 3x3 range
2011-09-01 19:47:38 +02:00
//TODO: this algorithm is slowing down render too much, but it could be cached to improve framerate
2011-09-01 16:49:51 +02:00
for(int xi = -1; xi <= 1; ++xi) {
for(int yi = -1; yi <= 1; ++yi) {
2011-09-01 00:42:52 +02:00
for(CreaturePtr creature : g_map.getTile(m_position + Position(xi, yi, 0))->getCreatures()) {
2011-12-02 03:29:05 +01:00
ThingType *type = creature->getType();
Rect creatureRect(p.x + xi*32 + creature->getWalkOffset().x - type->parameters[ThingType::DisplacementX], p.y + yi*32 + creature->getWalkOffset().y - type->parameters[ThingType::DisplacementY], 32, 32);
2011-09-02 00:00:46 +02:00
Rect thisTileRect(p.x, p.y, 32, 32);
2011-09-01 16:49:51 +02:00
// only render creatures where bottom right is inside our rect
2011-11-07 18:20:13 +01:00
if(thisTileRect.contains(creatureRect.bottomRight())) {
2011-11-08 02:44:30 +01:00
creature->draw(Point(p.x + xi*32 - m_drawElevation, p.y + yi*32 - m_drawElevation));
2011-11-07 18:20:13 +01:00
}
2011-09-01 00:42:52 +02:00
}
}
}
2011-08-20 04:08:27 +02:00
2011-08-31 22:22:57 +02:00
// effects
for(const EffectPtr& effect : m_effects)
2011-11-08 02:44:30 +01:00
effect->draw(p - 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) {
2011-12-02 03:29:05 +01:00
ThingType *type = thing->getType();
if(type->properties[ThingType::IsOnTop])
2011-11-08 02:44:30 +01:00
thing->draw(p);
}
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
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;
2011-12-30 15:15:23 +01:00
if(EffectPtr effect = thing->asEffect()) {
m_effects.push_back(effect);
return nullptr;
}
2011-08-31 22:22:57 +02:00
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);
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
}
int Tile::getThingStackpos(const ThingPtr& thing)
{
for(uint stackpos = 0; stackpos < m_things.size(); ++stackpos)
if(thing == m_things[stackpos])
return stackpos;
return -1;
}
2012-01-03 21:41:00 +01:00
ThingPtr Tile::getTopThing()
{
if(isEmpty())
return nullptr;
return m_things[m_things.size() - 1];
}
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-12-30 15:15:23 +01:00
if(EffectPtr effect = thing->asEffect()) {
auto it = std::find(m_effects.begin(), m_effects.end(), effect);
if(it != m_effects.end())
m_effects.erase(it);
return thing;
}
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;
2011-12-02 03:29:05 +01:00
ThingType *type = firstObject->getType();
if(type->properties[ThingType::IsGround])
2011-08-31 22:22:57 +02:00
return firstObject->asItem();
return nullptr;
2011-08-15 16:11:24 +02:00
}
2011-08-31 01:39:14 +02:00
2012-01-04 14:02:35 +01:00
ThingPtr Tile::getTopLookThing()
{
2012-01-04 14:02:35 +01:00
ThingPtr retThing;
// check if there is any lookable object in this tile
2012-01-02 20:01:48 +01:00
for(int i = m_things.size() - 1; i >= 0; --i) {
2012-01-04 14:02:35 +01:00
ThingPtr thing = m_things[i];
if(!thing->ignoreLook() && (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop()))
return thing;
else if(!thing->ignoreLook())
retThing = thing;
2012-01-02 20:01:48 +01:00
}
2012-01-04 14:02:35 +01:00
// return this, it it is lookable.
if(retThing)
return retThing;
// if not, check on under tile
Position tilePos = m_position;
tilePos.coveredDown();
TilePtr tile = g_map.getTile(tilePos);
if(tile)
return tile->getTopLookThing();
return nullptr;
}
ThingPtr Tile::getTopUseThing()
{
if(isEmpty())
return nullptr;
for(int i = m_things.size() - 1; i >= 0; --i) {
ThingPtr thing = m_things[i];
if(thing->isForceUse() || (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop()))
return thing;
}
return m_things[0];
}
CreaturePtr Tile::getTopCreature()
{
CreaturePtr creature;
for(int i = m_things.size() - 1; i >= 0; --i) {
ThingPtr thing = m_things[i];
if(thing->asLocalPlayer()) // return local player if there aint no other creature.
creature = thing->asCreature();
else if(thing->asCreature() && !thing->asLocalPlayer())
return thing->asCreature();
}
return creature;
}
2011-11-05 21:34:49 +01:00
bool Tile::isWalkable()
{
if(!getGround())
return false;
for(const ThingPtr& thing : m_things) {
2011-12-02 03:29:05 +01:00
ThingType *type = thing->getType();
if(type->properties[ThingType::NotWalkable])
2011-11-05 21:34:49 +01:00
return false;
2011-12-30 15:15:23 +01:00
if(CreaturePtr creature = thing->asCreature()) {
if(!creature->getPassable())
return false;
}
2011-11-05 21:34:49 +01:00
}
return true;
}
2011-09-02 00:00:46 +02:00
bool Tile::isFullGround()
{
ThingPtr ground = getThing(0);
if(!ground)
return false;
2011-12-02 03:29:05 +01:00
ThingType *type = ground->getType();
if(type->properties[ThingType::IsGround] && type->properties[ThingType::IsFullGround])
2011-09-02 00:00:46 +02:00
return true;
return false;
}
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) {
2011-12-02 03:29:05 +01:00
ThingType *type = firstObject->getType();
if(type->properties[ThingType::IsFullGround])
2011-08-31 22:22:57 +02:00
return true;
}
2011-08-31 01:39:14 +02:00
return false;
}
2011-09-02 00:00:46 +02:00
bool Tile::isLookPossible()
{
for(const ThingPtr& thing : m_things) {
2011-12-02 03:29:05 +01:00
ThingType *type = thing->getType();
if(type->properties[ThingType::BlockProjectile])
2011-09-02 00:00:46 +02:00
return false;
}
return true;
}
2011-12-30 15:15:23 +01:00
bool Tile::hasCreature()
{
for(const ThingPtr& thing : m_things)
if(thing->asCreature())
return true;
return false;
}
2012-01-02 20:01:48 +01:00
bool Tile::isEmpty()
{
return m_things.size() == 0;
}