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

253 lines
6.4 KiB
C++
Raw Normal View History

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"
#include "map.h"
2011-08-20 04:08:27 +02:00
#include "game.h"
#include "localplayer.h"
2011-08-20 04:08:27 +02:00
#include <framework/graphics/fontmanager.h>
2011-08-15 16:11:24 +02:00
Tile::Tile()
{
2011-08-20 04:08:27 +02:00
m_drawNextOffset = 0;
2011-08-15 16:11:24 +02:00
}
2011-08-20 04:08:27 +02:00
void Tile::draw(int x, int y, int step)
{
2011-08-20 04:08:27 +02:00
// STEP 0 = draw ground, top 1
// STEP 1 = top 2
// STEP 2 = top 3
// STEP 3 = bottom, creatures, names, etc
2011-08-20 04:08:27 +02:00
FontPtr font = g_fonts.getDefaultFont();
if(step == 0 && m_drawNextOffset != 0) {
logDebug("error with tile offset.");
return;
}
if(step == 0) {
if(m_ground)
m_ground->draw(x, y);
for(const ThingPtr& thing : m_itemsTop) {
const ThingAttributes& thingAttributes = thing->getAttributes();
if(thingAttributes.alwaysOnTopOrder == 1) {
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
//font->renderText("T1", Rect(x + 5, y+5, 100, 100));
m_drawNextOffset += thingAttributes.drawNextOffset;
}
}
for(const ThingPtr& thing : m_itemsTop) {
const ThingAttributes& thingAttributes = thing->getAttributes();
if(thingAttributes.alwaysOnTopOrder == 2) {
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
//font->renderText("T2", Rect(x + 5, y+5, 100, 100));
m_drawNextOffset += thingAttributes.drawNextOffset;
}
}
for(const ThingPtr& thing : m_itemsBottom) {
const ThingAttributes& thingAttributes = thing->getAttributes();
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
//font->renderText("B0", Rect(x + 5, y+5, 100, 100));
m_drawNextOffset += thingAttributes.drawNextOffset;
}
for(const ThingPtr& thing : m_creatures) {
const ThingAttributes& thingAttributes = thing->getAttributes();
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
m_drawNextOffset += thingAttributes.drawNextOffset;
}
for(const ThingPtr& thing : m_itemsTop) {
const ThingAttributes& thingAttributes = thing->getAttributes();
if(thingAttributes.alwaysOnTopOrder == 3) {
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
//font->renderText("T3", Rect(x + 5, y+5, 100, 100));
m_drawNextOffset += thingAttributes.drawNextOffset;
}
}
for(const ThingPtr& thing : m_effects) {
thing->draw(x - m_drawNextOffset, y - m_drawNextOffset);
}
m_drawNextOffset = 0;
}
else if(step == 1) {
}
else if(step == 2) {
2011-08-20 04:08:27 +02:00
}
else if(step == 3) {
}
}
2011-08-15 16:11:24 +02:00
void Tile::addThing(ThingPtr thing, uint8 stackpos)
{
if(!thing)
return;
2011-08-20 04:08:27 +02:00
//8308
//2526
//5296
const ThingAttributes& item1 = g_dat.getItemAttributes(8308);
const ThingAttributes& item2 = g_dat.getItemAttributes(2526);
const ThingAttributes& item3 = g_dat.getItemAttributes(5296);
int j = item1.alwaysOnTopOrder + item2.alwaysOnTopOrder + item3.alwaysOnTopOrder;
j++;
if(thing->getPosition() == g_game.getLocalPlayer()->getPosition() + Position(-1, 0, 0) && thing->getAttributes().alwaysOnTop) {
logDebug((int)thing->getId());
}
2011-08-15 21:15:49 +02:00
const ThingAttributes& thingAttributes = thing->getAttributes();
2011-08-15 23:02:52 +02:00
if(thing->asItem()) {
2011-08-15 21:15:49 +02:00
if(thingAttributes.group == THING_GROUP_GROUND)
m_ground = thing;
else {
if(thingAttributes.alwaysOnTop)
2011-08-20 04:08:27 +02:00
m_itemsTop.push_front(thing);
2011-08-15 21:15:49 +02:00
else
2011-08-20 04:08:27 +02:00
m_itemsBottom.push_front(thing);
2011-08-15 16:11:24 +02:00
}
}
else if(thing->asCreature()) {
2011-08-20 04:08:27 +02:00
m_creatures.push_front(thing);
2011-08-15 16:11:24 +02:00
}
else if(thing->asEffect()) {
2011-08-20 04:08:27 +02:00
m_effects.push_front(thing);
}
}
ThingPtr Tile::getThing(uint8 stackpos)
{
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
}
void Tile::removeThing(uint8 stackpos)
2011-08-15 16:11:24 +02:00
{
if(stackpos == 0) {
m_ground.reset();
return;
}
--stackpos;
2011-08-15 16:11:24 +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
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
if(stackpos < m_itemsBottom.size()) {
m_itemsBottom.erase(m_itemsBottom.begin() + stackpos);
return;
}
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;
}
}
}
}
void Tile::clean()
{
for(const ThingPtr& thing : m_creatures)
g_map.removeCreatureById(thing->getId());
m_itemsTop.clear();
m_creatures.clear();
m_itemsBottom.clear();
m_effects.clear();
2011-08-15 16:11:24 +02:00
}
int Tile::getStackSize()
{
int ret = 0;
if(m_ground)
ret++;
2011-08-16 02:30:31 +02:00
ret += m_itemsBottom.size();
2011-08-15 16:11:24 +02:00
ret += m_creatures.size();
ret += m_itemsTop.size();
return ret;
}