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-15 21:15:49 +02:00
|
|
|
#include "datmanager.h"
|
2011-08-17 06:45:55 +02:00
|
|
|
#include "map.h"
|
2011-08-20 04:08:27 +02:00
|
|
|
#include "game.h"
|
2011-08-17 06:45:55 +02:00
|
|
|
#include "localplayer.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-20 04:08:27 +02:00
|
|
|
m_drawNextOffset = 0;
|
2011-08-31 01:39:14 +02:00
|
|
|
m_position = position;
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
void Tile::draw(int x, int y)
|
2011-08-17 06:45:55 +02:00
|
|
|
{
|
2011-08-31 02:28:06 +02:00
|
|
|
m_drawNextOffset = 0;
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
if(m_ground)
|
|
|
|
m_ground->draw(x, y);
|
2011-08-21 05:21:35 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
for(auto it = m_itemsTop.rbegin(), end = m_itemsTop.rend(); it != end; ++it) {
|
|
|
|
const ThingPtr& thing = *it;
|
|
|
|
const ThingAttributes& thingAttributes = thing->getAttributes();
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
if(thingAttributes.alwaysOnTopOrder == 1) {
|
|
|
|
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
|
|
|
|
m_drawNextOffset += thingAttributes.drawNextOffset;
|
2011-08-26 17:44:29 +02:00
|
|
|
}
|
2011-08-31 02:28:06 +02:00
|
|
|
}
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
for(auto it = m_itemsTop.rbegin(), end = m_itemsTop.rend(); it != end; ++it) {
|
|
|
|
const ThingPtr& thing = *it;
|
|
|
|
const ThingAttributes& thingAttributes = thing->getAttributes();
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
if(thingAttributes.alwaysOnTopOrder == 2) {
|
2011-08-20 04:08:27 +02:00
|
|
|
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
|
|
|
|
m_drawNextOffset += thingAttributes.drawNextOffset;
|
|
|
|
}
|
2011-08-31 02:28:06 +02:00
|
|
|
}
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
for(auto it = m_itemsBottom.rbegin(), end = m_itemsBottom.rend(); it != end; ++it) {
|
|
|
|
const ThingPtr& thing = *it;
|
|
|
|
const ThingAttributes& thingAttributes = thing->getAttributes();
|
|
|
|
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
|
|
|
|
m_drawNextOffset += thingAttributes.drawNextOffset;
|
|
|
|
}
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
for(auto it = m_creatures.rbegin(), end = m_creatures.rend(); it != end; ++it) {
|
|
|
|
const ThingPtr& thing = *it;
|
|
|
|
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto it = m_effects.rbegin(), end = m_effects.rend(); it != end; ++it) {
|
|
|
|
const ThingPtr& thing = *it;
|
|
|
|
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
|
|
|
|
}
|
2011-08-31 01:39:14 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
for(auto it = m_itemsTop.rbegin(), end = m_itemsTop.rend(); it != end; ++it) {
|
|
|
|
const ThingPtr& thing = *it;
|
|
|
|
const ThingAttributes& thingAttributes = thing->getAttributes();
|
2011-08-20 04:08:27 +02:00
|
|
|
|
2011-08-31 02:28:06 +02:00
|
|
|
if(thingAttributes.alwaysOnTopOrder == 3) {
|
|
|
|
thing->draw(x, y);
|
2011-08-20 04:08:27 +02:00
|
|
|
}
|
2011-08-31 01:39:14 +02:00
|
|
|
}
|
2011-08-17 06:45:55 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
void Tile::addThing(ThingPtr thing, int stackpos)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
2011-08-24 05:58:23 +02:00
|
|
|
// TODO: rework this. that -1 sucks
|
2011-08-15 16:11:24 +02:00
|
|
|
if(!thing)
|
|
|
|
return;
|
|
|
|
|
2011-08-26 07:07:23 +02:00
|
|
|
const ThingAttributes& thingAttributes = thing->getAttributes();
|
|
|
|
|
2011-08-15 23:02:52 +02:00
|
|
|
if(thing->asItem()) {
|
2011-08-28 18:02:26 +02:00
|
|
|
if(thingAttributes.group == Otc::ThingGroundGroup)
|
2011-08-15 21:15:49 +02:00
|
|
|
m_ground = thing;
|
|
|
|
else {
|
|
|
|
if(thingAttributes.alwaysOnTop)
|
2011-08-21 05:21:35 +02:00
|
|
|
m_itemsTop.push_back(thing);
|
2011-08-24 05:58:23 +02:00
|
|
|
else {
|
|
|
|
if(stackpos == -1)
|
|
|
|
m_itemsBottom.push_back(thing);
|
|
|
|
else {
|
|
|
|
m_itemsBottom.insert(m_itemsBottom.begin()+(stackpos-getStackSize(2)), thing);
|
|
|
|
}
|
|
|
|
}
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
2011-08-16 07:47:35 +02:00
|
|
|
}
|
|
|
|
else if(thing->asCreature()) {
|
2011-08-21 05:21:35 +02:00
|
|
|
m_creatures.push_back(thing);
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
2011-08-16 07:47:35 +02:00
|
|
|
else if(thing->asEffect()) {
|
2011-08-21 05:21:35 +02:00
|
|
|
m_effects.push_back(thing);
|
2011-08-16 07:47:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
ThingPtr Tile::getThing(unsigned int stackpos)
|
2011-08-16 07:47:35 +02:00
|
|
|
{
|
2011-08-17 06:45:55 +02:00
|
|
|
if(stackpos == 0)
|
|
|
|
return m_ground;
|
|
|
|
--stackpos;
|
|
|
|
|
|
|
|
if(stackpos < m_itemsTop.size())
|
|
|
|
return m_itemsTop[stackpos];
|
|
|
|
stackpos -= m_itemsTop.size();
|
|
|
|
|
|
|
|
if(stackpos < m_creatures.size())
|
|
|
|
return m_creatures[stackpos];
|
|
|
|
stackpos -= m_creatures.size();
|
|
|
|
|
|
|
|
if(stackpos < m_itemsBottom.size())
|
|
|
|
return m_itemsBottom[stackpos];
|
|
|
|
|
|
|
|
return ThingPtr();
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
void Tile::removeThing(unsigned int stackpos)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
2011-08-17 06:45:55 +02:00
|
|
|
if(stackpos == 0) {
|
|
|
|
m_ground.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
--stackpos;
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
if(stackpos < m_itemsTop.size()) {
|
|
|
|
m_itemsTop.erase(m_itemsTop.begin() + stackpos);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stackpos -= m_itemsTop.size();
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
if(stackpos < m_creatures.size()) {
|
|
|
|
m_creatures.erase(m_creatures.begin() + stackpos);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stackpos -= m_creatures.size();
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
if(stackpos < m_itemsBottom.size()) {
|
|
|
|
m_itemsBottom.erase(m_itemsBottom.begin() + stackpos);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-16 07:47:35 +02:00
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
logDebug("Invalid stackpos.");
|
|
|
|
}
|
|
|
|
|
2011-08-17 07:04:45 +02:00
|
|
|
void Tile::removeThingByPtr(ThingPtr thing)
|
|
|
|
{
|
|
|
|
// Items
|
|
|
|
if(thing->asItem()) {
|
|
|
|
const ThingAttributes& thingAttributes = thing->getAttributes();
|
|
|
|
|
|
|
|
if(!thingAttributes.alwaysOnTop) {
|
|
|
|
for(auto it = m_itemsBottom.begin(), end = m_itemsBottom.end(); it != end; ++it) {
|
|
|
|
if(*it == thing) {
|
|
|
|
m_itemsBottom.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(auto it = m_itemsTop.begin(), end = m_itemsTop.end(); it != end; ++it) {
|
|
|
|
if(*it == thing) {
|
|
|
|
m_itemsTop.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creatures
|
|
|
|
else if(thing->asCreature()) {
|
|
|
|
for(auto it = m_creatures.begin(), end = m_creatures.end(); it != end; ++it) {
|
|
|
|
if(*it == thing) {
|
|
|
|
m_creatures.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Effects
|
|
|
|
else if(thing->asEffect()) {
|
|
|
|
for(auto it = m_effects.begin(), end = m_effects.end(); it != end; ++it) {
|
|
|
|
if(*it == thing) {
|
|
|
|
m_effects.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 06:45:55 +02:00
|
|
|
void Tile::clean()
|
|
|
|
{
|
|
|
|
m_itemsTop.clear();
|
|
|
|
m_creatures.clear();
|
|
|
|
m_itemsBottom.clear();
|
|
|
|
m_effects.clear();
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
int Tile::getStackSize(int stop)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
2011-08-24 05:58:23 +02:00
|
|
|
int ret = m_ground ? 1 : 0;
|
|
|
|
if(stop == 0)
|
|
|
|
return ret;
|
|
|
|
|
2011-08-15 16:11:24 +02:00
|
|
|
ret += m_itemsTop.size();
|
2011-08-24 05:58:23 +02:00
|
|
|
if(stop == 1)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret += m_creatures.size();
|
|
|
|
if(stop == 2)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret += m_itemsBottom.size();
|
2011-08-15 16:11:24 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2011-08-31 01:39:14 +02:00
|
|
|
|
|
|
|
bool Tile::isOpaque()
|
|
|
|
{
|
|
|
|
if(m_ground && !m_ground->getAttributes().changesFloor)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|