2011-08-31 17:03:33 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-31 17:03:33 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "thingstype.h"
|
|
|
|
#include "spritemanager.h"
|
|
|
|
#include "thing.h"
|
|
|
|
#include <framework/core/resourcemanager.h>
|
2012-05-11 20:02:57 +02:00
|
|
|
#include <framework/core/filestream.h>
|
2011-08-31 17:03:33 +02:00
|
|
|
|
|
|
|
ThingsType g_thingsType;
|
2011-12-02 04:01:56 +01:00
|
|
|
ThingType ThingsType::m_emptyThingType;
|
2011-08-31 17:03:33 +02:00
|
|
|
|
|
|
|
bool ThingsType::load(const std::string& file)
|
|
|
|
{
|
2012-05-11 20:02:57 +02:00
|
|
|
FileStreamPtr fin = g_resources.openFile(file);
|
|
|
|
if(!fin) {
|
2012-05-29 00:04:44 +02:00
|
|
|
logError("unable to open dat file '%s'", file);
|
2012-05-11 20:02:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-08-31 17:03:33 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
m_signature = fin->getU32();
|
2011-08-31 17:03:33 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
int numThings[LastCategory];
|
|
|
|
for(int i = 0; i < LastCategory; ++i)
|
|
|
|
numThings[i] = fin->getU16();
|
2011-08-31 17:03:33 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
numThings[Item] -= 99;
|
2011-08-31 17:03:33 +02:00
|
|
|
|
2012-05-11 20:02:57 +02:00
|
|
|
for(int i = 0; i < LastCategory; ++i) {
|
|
|
|
m_things[i].resize(numThings[i]);
|
2012-05-12 14:30:51 +02:00
|
|
|
for(int id = 0; id < numThings[i]; ++id) {
|
2012-05-13 17:05:17 +02:00
|
|
|
m_things[i][id].m_category = i;
|
2012-05-12 14:30:51 +02:00
|
|
|
if(!parseThingType(fin, m_things[i][id])) {
|
|
|
|
logError("corrupt or dat file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-08-31 17:03:33 +02:00
|
|
|
}
|
2012-05-11 20:02:57 +02:00
|
|
|
|
|
|
|
m_loaded = true;
|
|
|
|
return true;
|
2011-08-31 17:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThingsType::unload()
|
|
|
|
{
|
2011-11-12 07:24:32 +01:00
|
|
|
for(int i = 0; i < LastCategory; ++i)
|
|
|
|
m_things[i].clear();
|
2011-08-31 17:03:33 +02:00
|
|
|
}
|
|
|
|
|
2012-05-12 14:30:51 +02:00
|
|
|
bool ThingsType::parseThingType(const FileStreamPtr& fin, ThingType& thingType)
|
2011-08-31 17:03:33 +02:00
|
|
|
{
|
2012-05-12 14:30:51 +02:00
|
|
|
bool done = false;
|
|
|
|
for(int i=0;i<ThingType::LastProperty;++i) {
|
2012-05-11 20:02:57 +02:00
|
|
|
int property = fin->getU8();
|
2012-05-12 06:52:16 +02:00
|
|
|
if(property == ThingType::LastPropertyValue) {
|
2012-05-12 14:30:51 +02:00
|
|
|
done = true;
|
2011-11-12 07:24:32 +01:00
|
|
|
break;
|
2012-05-12 06:52:16 +02:00
|
|
|
}
|
2011-11-12 07:24:32 +01:00
|
|
|
|
2012-05-09 22:26:34 +02:00
|
|
|
thingType.m_properties[property] = true;
|
2011-11-12 07:24:32 +01:00
|
|
|
|
2012-05-12 14:30:51 +02:00
|
|
|
if(property == ThingType::IsGround) {
|
|
|
|
int speed = fin->getU16();
|
|
|
|
if(speed == 0)
|
|
|
|
speed = 100;
|
|
|
|
thingType.m_parameters[ThingType::GroundSpeed] = speed;
|
|
|
|
}
|
2011-11-12 07:24:32 +01:00
|
|
|
else if(property == ThingType::IsWritable || property == ThingType::IsWritableOnce)
|
2012-05-11 20:02:57 +02:00
|
|
|
thingType.m_parameters[ThingType::MaxTextLenght] = fin->getU16();
|
2011-11-12 07:24:32 +01:00
|
|
|
else if(property == ThingType::HasLight) {
|
2012-05-11 20:02:57 +02:00
|
|
|
thingType.m_parameters[ThingType::LightLevel] = fin->getU16();
|
|
|
|
thingType.m_parameters[ThingType::LightColor] = fin->getU16();
|
2011-08-31 17:03:33 +02:00
|
|
|
}
|
2011-11-12 07:24:32 +01:00
|
|
|
else if(property == ThingType::HasDisplacement) {
|
2012-05-11 20:02:57 +02:00
|
|
|
thingType.m_parameters[ThingType::DisplacementX] = fin->getU16();
|
|
|
|
thingType.m_parameters[ThingType::DisplacementY] = fin->getU16();
|
2011-11-12 07:24:32 +01:00
|
|
|
}
|
|
|
|
else if(property == ThingType::HasElevation)
|
2012-05-11 20:02:57 +02:00
|
|
|
thingType.m_parameters[ThingType::Elevation] = fin->getU16();
|
2011-11-12 07:24:32 +01:00
|
|
|
else if(property == ThingType::MiniMap)
|
2012-05-11 20:02:57 +02:00
|
|
|
thingType.m_parameters[ThingType::MiniMapColor] = fin->getU16();
|
2011-11-12 07:24:32 +01:00
|
|
|
else if(property == ThingType::LensHelp)
|
2012-05-11 20:02:57 +02:00
|
|
|
thingType.m_parameters[ThingType::LensHelpParameter] = fin->getU16();
|
2011-11-12 07:24:32 +01:00
|
|
|
else if(property == ThingType::Cloth)
|
2012-05-11 20:02:57 +02:00
|
|
|
thingType.m_parameters[ThingType::ClothSlot] = fin->getU16();
|
|
|
|
#if PROTOCOL<=810
|
|
|
|
else if(property == ThingType::IsRune)
|
|
|
|
thingType.m_properties[ThingType::IsStackable] = true;
|
2012-05-12 06:52:16 +02:00
|
|
|
#endif
|
|
|
|
else if(property == ThingType::Market) {
|
|
|
|
fin->getU16(); // category
|
|
|
|
fin->getU16(); // trade as
|
|
|
|
fin->getU16(); // show as
|
|
|
|
fin->getString(); // name
|
|
|
|
fin->getU16(); // restrict profession
|
|
|
|
fin->getU16(); // level
|
|
|
|
}
|
2011-08-31 17:03:33 +02:00
|
|
|
}
|
|
|
|
|
2012-05-12 14:30:51 +02:00
|
|
|
if(!done)
|
|
|
|
return false;
|
|
|
|
|
2011-11-12 07:24:32 +01:00
|
|
|
int totalSprites = 1;
|
|
|
|
for(int i = 0; i < ThingType::LastDimension; ++i) {
|
2012-05-12 14:30:51 +02:00
|
|
|
int value;
|
|
|
|
if(i == ThingType::ExactSize) {
|
|
|
|
if(thingType.m_dimensions[ThingType::Width] <= 1 && thingType.m_dimensions[ThingType::Height] <= 1)
|
|
|
|
value = 32;
|
|
|
|
else
|
|
|
|
value = std::min((int)fin->getU8(), std::max(thingType.m_dimensions[ThingType::Width] * 32, thingType.m_dimensions[ThingType::Height] * 32));
|
|
|
|
} else {
|
|
|
|
value = fin->getU8();
|
|
|
|
if(value == 0)
|
|
|
|
return false;
|
|
|
|
totalSprites *= value;
|
2011-11-12 07:24:32 +01:00
|
|
|
}
|
|
|
|
|
2012-05-12 14:30:51 +02:00
|
|
|
thingType.m_dimensions[i] = value;
|
2011-11-12 07:24:32 +01:00
|
|
|
}
|
2011-08-31 17:03:33 +02:00
|
|
|
|
2012-05-12 14:30:51 +02:00
|
|
|
if(totalSprites > 4096)
|
|
|
|
return false;
|
|
|
|
|
2012-05-09 22:26:34 +02:00
|
|
|
thingType.m_spritesIndex.resize(totalSprites);
|
2011-11-12 07:24:32 +01:00
|
|
|
for(int i = 0; i < totalSprites; i++)
|
2012-05-11 20:02:57 +02:00
|
|
|
thingType.m_spritesIndex[i] = fin->getU16();
|
2012-05-12 14:30:51 +02:00
|
|
|
|
2012-05-13 17:05:17 +02:00
|
|
|
thingType.m_textures.resize(thingType.m_dimensions[ThingType::AnimationPhases]);
|
|
|
|
thingType.m_texturesFramesRects.resize(thingType.m_dimensions[ThingType::AnimationPhases]);
|
|
|
|
thingType.m_texturesFramesOffsets.resize(thingType.m_dimensions[ThingType::AnimationPhases]);
|
|
|
|
|
2012-05-12 14:30:51 +02:00
|
|
|
return true;
|
2011-08-31 17:03:33 +02:00
|
|
|
}
|
2011-11-12 07:24:32 +01:00
|
|
|
|
2011-12-02 03:29:05 +01:00
|
|
|
ThingType *ThingsType::getThingType(uint16 id, Categories category)
|
2011-11-12 07:24:32 +01:00
|
|
|
{
|
2012-03-21 18:30:57 +01:00
|
|
|
if(id == 0)
|
|
|
|
return &m_emptyThingType;
|
|
|
|
|
2011-11-12 07:24:32 +01:00
|
|
|
if(category == Item)
|
|
|
|
id -= 100;
|
2012-01-09 22:16:50 +01:00
|
|
|
else if(category == Creature || category == Effect || category == Missile)
|
|
|
|
id -= 1;
|
2012-02-02 17:37:52 +01:00
|
|
|
|
2012-03-21 18:30:57 +01:00
|
|
|
if(id >= m_things[category].size())
|
2012-02-02 17:37:52 +01:00
|
|
|
return &m_emptyThingType;
|
2011-12-02 03:29:05 +01:00
|
|
|
return &m_things[category][id];
|
2011-11-12 07:24:32 +01:00
|
|
|
}
|