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 "item.h"
|
2012-06-21 19:54:20 +02:00
|
|
|
#include "thingtypemanager.h"
|
2011-08-15 21:15:49 +02:00
|
|
|
#include "spritemanager.h"
|
2011-08-15 16:11:24 +02:00
|
|
|
#include "thing.h"
|
2012-02-02 17:55:42 +01:00
|
|
|
#include "tile.h"
|
2012-06-14 20:26:55 +02:00
|
|
|
#include "shadermanager.h"
|
2012-07-15 01:20:38 +02:00
|
|
|
#include "container.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include "houses.h"
|
2012-08-02 03:33:56 +02:00
|
|
|
#include "game.h"
|
2012-06-21 04:31:29 +02:00
|
|
|
|
2011-12-01 23:25:32 +01:00
|
|
|
#include <framework/core/clock.h>
|
2011-12-27 03:18:15 +01:00
|
|
|
#include <framework/core/eventdispatcher.h>
|
2012-01-30 01:00:12 +01:00
|
|
|
#include <framework/graphics/graphics.h>
|
2012-06-21 04:31:29 +02:00
|
|
|
#include <framework/core/filestream.h>
|
2012-07-09 08:46:11 +02:00
|
|
|
#include <framework/core/binarytree.h>
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2012-06-23 23:30:54 +02:00
|
|
|
Item::Item() :
|
|
|
|
m_id(0),
|
2012-07-15 15:23:13 +02:00
|
|
|
m_otbId(0),
|
|
|
|
m_countOrSubType(1)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-09 06:23:39 +01:00
|
|
|
ItemPtr Item::create(int id)
|
|
|
|
{
|
2012-07-09 08:59:16 +02:00
|
|
|
ItemPtr item(new Item);
|
2012-06-21 19:54:20 +02:00
|
|
|
item->setId(id);
|
2012-01-09 06:23:39 +01:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2012-06-22 01:58:18 +02:00
|
|
|
ItemPtr Item::createFromOtb(int id)
|
|
|
|
{
|
2012-07-09 08:59:16 +02:00
|
|
|
ItemPtr item(new Item);
|
2012-06-22 01:58:18 +02:00
|
|
|
item->setOtbId(id);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
void Item::draw(const Point& dest, float scaleFactor, bool animate)
|
2011-08-28 21:51:34 +02:00
|
|
|
{
|
2012-02-02 17:37:52 +01:00
|
|
|
if(m_id == 0)
|
|
|
|
return;
|
2012-01-30 01:00:12 +01:00
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
// determine animation phase
|
|
|
|
int animationPhase = 0;
|
|
|
|
if(getAnimationPhases() > 1) {
|
|
|
|
if(animate)
|
2012-06-02 16:43:27 +02:00
|
|
|
animationPhase = (g_clock.millis() % (Otc::ITEM_TICKS_PER_FRAME * getAnimationPhases())) / Otc::ITEM_TICKS_PER_FRAME;
|
2012-02-02 17:37:52 +01:00
|
|
|
else
|
|
|
|
animationPhase = getAnimationPhases()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine x,y,z patterns
|
|
|
|
int xPattern = 0, yPattern = 0, zPattern = 0;
|
2012-06-21 19:54:20 +02:00
|
|
|
if(isStackable() && getNumPatternX() == 4 && getNumPatternY() == 2) {
|
2012-04-28 17:30:19 +02:00
|
|
|
if(m_countOrSubType <= 0) {
|
|
|
|
xPattern = 0;
|
|
|
|
yPattern = 0;
|
|
|
|
} else if(m_countOrSubType < 5) {
|
2012-02-02 17:37:52 +01:00
|
|
|
xPattern = m_countOrSubType-1;
|
|
|
|
yPattern = 0;
|
|
|
|
} else if(m_countOrSubType < 10) {
|
|
|
|
xPattern = 0;
|
|
|
|
yPattern = 1;
|
|
|
|
} else if(m_countOrSubType < 25) {
|
|
|
|
xPattern = 1;
|
|
|
|
yPattern = 1;
|
|
|
|
} else if(m_countOrSubType < 50) {
|
|
|
|
xPattern = 2;
|
|
|
|
yPattern = 1;
|
2012-04-28 17:12:02 +02:00
|
|
|
} else {
|
2012-02-02 17:37:52 +01:00
|
|
|
xPattern = 3;
|
|
|
|
yPattern = 1;
|
|
|
|
}
|
|
|
|
} else if(isHangable()) {
|
2012-02-02 17:55:42 +01:00
|
|
|
const TilePtr& tile = getTile();
|
|
|
|
if(tile) {
|
|
|
|
if(tile->mustHookSouth())
|
2012-06-21 19:54:20 +02:00
|
|
|
xPattern = getNumPatternX() >= 2 ? 1 : 0;
|
2012-04-05 05:20:40 +02:00
|
|
|
else if(tile->mustHookEast())
|
2012-06-21 19:54:20 +02:00
|
|
|
xPattern = getNumPatternX() >= 3 ? 2 : 0;
|
2012-02-02 17:55:42 +01:00
|
|
|
}
|
2012-07-18 01:49:21 +02:00
|
|
|
} else if(isSplash() || isFluidContainer()) {
|
2012-02-02 17:37:52 +01:00
|
|
|
int color = Otc::FluidTransparent;
|
|
|
|
switch(m_countOrSubType) {
|
|
|
|
case Otc::FluidNone:
|
|
|
|
color = Otc::FluidTransparent;
|
|
|
|
break;
|
|
|
|
case Otc::FluidWater:
|
|
|
|
color = Otc::FluidBlue;
|
|
|
|
break;
|
|
|
|
case Otc::FluidMana:
|
|
|
|
color = Otc::FluidPurple;
|
|
|
|
break;
|
|
|
|
case Otc::FluidBeer:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidOil:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidBlood:
|
|
|
|
color = Otc::FluidRed;
|
|
|
|
break;
|
|
|
|
case Otc::FluidSlime:
|
|
|
|
color = Otc::FluidGreen;
|
|
|
|
break;
|
|
|
|
case Otc::FluidMud:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidLemonade:
|
|
|
|
color = Otc::FluidYellow;
|
|
|
|
break;
|
|
|
|
case Otc::FluidMilk:
|
|
|
|
color = Otc::FluidWhite;
|
|
|
|
break;
|
|
|
|
case Otc::FluidWine:
|
|
|
|
color = Otc::FluidPurple;
|
|
|
|
break;
|
|
|
|
case Otc::FluidHealth:
|
|
|
|
color = Otc::FluidRed;
|
|
|
|
break;
|
|
|
|
case Otc::FluidUrine:
|
|
|
|
color = Otc::FluidYellow;
|
|
|
|
break;
|
|
|
|
case Otc::FluidRum:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidFruidJuice:
|
|
|
|
color = Otc::FluidYellow;
|
|
|
|
break;
|
|
|
|
case Otc::FluidCoconutMilk:
|
|
|
|
color = Otc::FluidWhite;
|
|
|
|
break;
|
|
|
|
case Otc::FluidTea:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidMead:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
color = Otc::FluidTransparent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-21 19:54:20 +02:00
|
|
|
xPattern = (color % 4) % getNumPatternX();
|
|
|
|
yPattern = (color / 4) % getNumPatternY();
|
2012-06-02 03:52:40 +02:00
|
|
|
} else if(isGround() || isOnBottom()) {
|
2012-06-21 19:54:20 +02:00
|
|
|
xPattern = m_position.x % getNumPatternX();
|
|
|
|
yPattern = m_position.y % getNumPatternY();
|
|
|
|
zPattern = m_position.z % getNumPatternZ();
|
2012-02-02 17:37:52 +01:00
|
|
|
}
|
|
|
|
|
2012-07-19 23:22:52 +02:00
|
|
|
rawGetThingType()->draw(dest, scaleFactor, 0, xPattern, yPattern, zPattern, animationPhase);
|
2011-08-28 21:51:34 +02:00
|
|
|
}
|
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
void Item::setId(uint32 id)
|
2011-08-28 21:47:23 +02:00
|
|
|
{
|
2012-07-20 07:45:11 +02:00
|
|
|
if(!g_things.isValidDatId(id, ThingCategoryItem))
|
2012-07-18 01:49:21 +02:00
|
|
|
id = 0;
|
2012-07-30 17:08:21 +02:00
|
|
|
//m_otbId = g_things.findItemTypeByClientId(id)->getServerId();
|
2012-07-15 15:23:13 +02:00
|
|
|
m_id = id;
|
2012-07-18 01:49:21 +02:00
|
|
|
m_otbId = 0;
|
2012-06-22 01:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Item::setOtbId(uint16 id)
|
|
|
|
{
|
2012-07-18 01:49:21 +02:00
|
|
|
if(!g_things.isValidOtbId(id))
|
|
|
|
id = 0;
|
2012-07-26 08:10:28 +02:00
|
|
|
auto itemType = g_things.getItemType(id);
|
2012-07-18 01:49:21 +02:00
|
|
|
m_otbId = id;
|
2012-07-27 23:25:41 +02:00
|
|
|
|
|
|
|
id = itemType->getClientId();
|
|
|
|
if(!g_things.isValidDatId(id, ThingCategoryItem))
|
|
|
|
id = 0;
|
|
|
|
m_id = id;
|
2011-08-28 21:47:23 +02:00
|
|
|
}
|
2012-06-21 04:31:29 +02:00
|
|
|
|
2012-06-23 16:26:18 +02:00
|
|
|
bool Item::isValid()
|
|
|
|
{
|
2012-07-20 07:45:11 +02:00
|
|
|
return g_things.isValidDatId(m_id, ThingCategoryItem);
|
2012-06-23 16:26:18 +02:00
|
|
|
}
|
|
|
|
|
2012-07-09 08:59:16 +02:00
|
|
|
void Item::unserializeItem(const BinaryTreePtr &in)
|
2012-06-21 04:31:29 +02:00
|
|
|
{
|
2012-07-18 10:34:17 +02:00
|
|
|
try {
|
|
|
|
while(in->canRead()) {
|
|
|
|
int attrib = in->getU8();
|
|
|
|
if(attrib == 0)
|
2012-07-15 01:20:38 +02:00
|
|
|
break;
|
2012-07-18 10:34:17 +02:00
|
|
|
|
|
|
|
switch(attrib) {
|
|
|
|
case ATTR_COUNT:
|
|
|
|
case ATTR_RUNE_CHARGES:
|
|
|
|
setCount(in->getU8());
|
|
|
|
break;
|
|
|
|
case ATTR_CHARGES:
|
|
|
|
setCount(in->getU16());
|
|
|
|
break;
|
|
|
|
case ATTR_HOUSEDOORID:
|
|
|
|
case ATTR_SCRIPTPROTECTED:
|
|
|
|
case ATTR_DUALWIELD:
|
|
|
|
case ATTR_DECAYING_STATE:
|
|
|
|
m_attribs.set(attrib, in->getU8());
|
|
|
|
break;
|
|
|
|
case ATTR_ACTION_ID:
|
|
|
|
case ATTR_UNIQUE_ID:
|
|
|
|
case ATTR_DEPOT_ID:
|
|
|
|
m_attribs.set(attrib, in->getU16());
|
|
|
|
break;
|
|
|
|
case ATTR_CONTAINER_ITEMS:
|
|
|
|
case ATTR_ATTACK:
|
|
|
|
case ATTR_EXTRAATTACK:
|
|
|
|
case ATTR_DEFENSE:
|
|
|
|
case ATTR_EXTRADEFENSE:
|
|
|
|
case ATTR_ARMOR:
|
|
|
|
case ATTR_ATTACKSPEED:
|
|
|
|
case ATTR_HITCHANCE:
|
|
|
|
case ATTR_DURATION:
|
|
|
|
case ATTR_WRITTENDATE:
|
|
|
|
case ATTR_SLEEPERGUID:
|
|
|
|
case ATTR_SLEEPSTART:
|
|
|
|
case ATTR_ATTRIBUTE_MAP:
|
|
|
|
m_attribs.set(attrib, in->getU32());
|
|
|
|
break;
|
|
|
|
case ATTR_TELE_DEST: {
|
|
|
|
Position pos;
|
|
|
|
pos.x = in->getU16();
|
|
|
|
pos.y = in->getU16();
|
|
|
|
pos.z = in->getU8();
|
|
|
|
m_attribs.set(attrib, pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ATTR_NAME:
|
|
|
|
case ATTR_TEXT:
|
|
|
|
case ATTR_DESC:
|
|
|
|
case ATTR_ARTICLE:
|
|
|
|
case ATTR_WRITTENBY:
|
|
|
|
m_attribs.set(attrib, in->getString());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stdext::throw_exception(stdext::format("invalid item attribute %d", attrib));
|
2012-07-15 01:20:38 +02:00
|
|
|
}
|
2012-07-09 08:59:16 +02:00
|
|
|
}
|
2012-07-18 10:34:17 +02:00
|
|
|
} catch(stdext::exception& e) {
|
|
|
|
g_logger.error(stdext::format("Failed to unserialize OTBM item: %s", e.what()));
|
2012-06-21 05:05:44 +02:00
|
|
|
}
|
2012-06-21 04:31:29 +02:00
|
|
|
}
|
2012-07-09 08:46:11 +02:00
|
|
|
|
2012-07-20 20:56:08 +02:00
|
|
|
void Item::serializeItem(const BinaryTreePtr& out)
|
|
|
|
{
|
|
|
|
out->writeU8(ATTR_COUNT);
|
|
|
|
out->writeU8(getCount());
|
|
|
|
|
|
|
|
out->writeU8(ATTR_CHARGES);
|
|
|
|
out->writeU16(getCountOrSubType());
|
|
|
|
|
|
|
|
Position dest = m_attribs.get<Position>(ATTR_TELE_DEST);
|
|
|
|
if(dest.isValid()) {
|
|
|
|
out->writeU8(ATTR_TELE_DEST);
|
|
|
|
out->writePos(dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isDepot()) {
|
|
|
|
out->writeU8(ATTR_DEPOT_ID);
|
|
|
|
out->writeU16(getDepotId());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16 aid = m_attribs.get<uint16>(ATTR_ACTION_ID), uid = m_attribs.get<uint16>(ATTR_UNIQUE_ID);
|
|
|
|
if(aid) {
|
|
|
|
out->writeU8(ATTR_ACTION_ID);
|
|
|
|
out->writeU16(aid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(uid) {
|
|
|
|
out->writeU8(ATTR_UNIQUE_ID);
|
|
|
|
out->writeU16(uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 10:51:03 +02:00
|
|
|
int Item::getSubType()
|
|
|
|
{
|
|
|
|
if(isSplash() || isFluidContainer())
|
|
|
|
return m_countOrSubType;
|
2012-08-02 03:33:56 +02:00
|
|
|
if(g_game.getClientVersion() >= 900)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2012-07-30 10:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Item::getCount()
|
|
|
|
{
|
|
|
|
if(isStackable())
|
|
|
|
return m_countOrSubType;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-09 08:46:11 +02:00
|
|
|
bool Item::isMoveable()
|
|
|
|
{
|
2012-07-19 23:22:52 +02:00
|
|
|
return !rawGetThingType()->isNotMoveable();
|
2012-07-09 08:46:11 +02:00
|
|
|
}
|
2012-07-15 07:46:56 +02:00
|
|
|
|
|
|
|
ItemPtr Item::clone()
|
|
|
|
{
|
|
|
|
ItemPtr item = ItemPtr(new Item);
|
|
|
|
*(item.get()) = *this;
|
|
|
|
return item;
|
|
|
|
}
|
2012-07-15 15:23:13 +02:00
|
|
|
|
2012-07-19 23:22:52 +02:00
|
|
|
const ThingTypePtr& Item::getThingType()
|
2012-07-15 15:23:13 +02:00
|
|
|
{
|
2012-07-20 07:45:11 +02:00
|
|
|
return g_things.getThingType(m_id, ThingCategoryItem);
|
2012-07-15 15:23:13 +02:00
|
|
|
}
|
|
|
|
|
2012-07-19 23:22:52 +02:00
|
|
|
ThingType* Item::rawGetThingType()
|
2012-07-15 15:23:13 +02:00
|
|
|
{
|
2012-07-20 07:45:11 +02:00
|
|
|
return g_things.rawGetThingType(m_id, ThingCategoryItem);
|
2012-07-15 15:23:13 +02:00
|
|
|
}
|