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>
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
std::stringstream fin;
|
|
|
|
g_resources.loadFile(file, fin);
|
|
|
|
|
|
|
|
m_signature = Fw::getU32(fin);
|
|
|
|
|
2011-11-12 07:24:32 +01:00
|
|
|
int numThings[LastCategory];
|
|
|
|
for(int i = 0; i < LastCategory; ++i)
|
|
|
|
numThings[i] = Fw::getU16(fin);
|
2011-08-31 17:03:33 +02:00
|
|
|
|
2011-11-12 07:24:32 +01:00
|
|
|
numThings[Item] -= 100;
|
2011-08-31 17:03:33 +02:00
|
|
|
|
2011-11-12 07:24:32 +01:00
|
|
|
for(int i = 0; i < LastCategory; ++i) {
|
|
|
|
m_things[i].resize(numThings[i]);
|
|
|
|
for(int id = 0; id < numThings[i]; ++id)
|
|
|
|
parseThingType(fin, m_things[i][id]);
|
|
|
|
}
|
2011-08-31 17:03:33 +02:00
|
|
|
|
2012-01-05 02:28:29 +01:00
|
|
|
m_loaded = true;
|
2011-08-31 17:03:33 +02:00
|
|
|
return true;
|
2011-12-01 23:25:32 +01:00
|
|
|
} catch(Exception& e) {
|
|
|
|
logError("Failed to load dat from '", file, "': ", e.what());
|
2011-08-31 17:03:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ThingsType::parseThingType(std::stringstream& fin, ThingType& thingType)
|
|
|
|
{
|
|
|
|
assert(fin.good());
|
|
|
|
|
2011-11-12 07:24:32 +01:00
|
|
|
while(true) {
|
|
|
|
int property = Fw::getU8(fin);
|
|
|
|
if(property == ThingType::LastPropertyValue)
|
|
|
|
break;
|
|
|
|
|
|
|
|
thingType.properties[property] = true;
|
|
|
|
|
|
|
|
if(property == ThingType::IsGround)
|
|
|
|
thingType.parameters[ThingType::GroundSpeed] = Fw::getU16(fin);
|
|
|
|
else if(property == ThingType::IsWritable || property == ThingType::IsWritableOnce)
|
|
|
|
thingType.parameters[ThingType::MaxTextLenght] = Fw::getU16(fin);
|
|
|
|
else if(property == ThingType::HasLight) {
|
|
|
|
thingType.parameters[ThingType::LightLevel] = Fw::getU16(fin);
|
|
|
|
thingType.parameters[ThingType::LightColor] = Fw::getU16(fin);
|
2011-08-31 17:03:33 +02:00
|
|
|
}
|
2011-11-12 07:24:32 +01:00
|
|
|
else if(property == ThingType::HasDisplacement) {
|
|
|
|
thingType.parameters[ThingType::DisplacementX] = Fw::getU16(fin);
|
|
|
|
thingType.parameters[ThingType::DisplacementY] = Fw::getU16(fin);
|
|
|
|
}
|
|
|
|
else if(property == ThingType::HasElevation)
|
|
|
|
thingType.parameters[ThingType::Elevation] = Fw::getU16(fin);
|
|
|
|
else if(property == ThingType::MiniMap)
|
|
|
|
thingType.parameters[ThingType::MiniMapColor] = Fw::getU16(fin);
|
|
|
|
else if(property == ThingType::LensHelp)
|
|
|
|
thingType.parameters[ThingType::LensHelpParameter] = Fw::getU16(fin);
|
|
|
|
else if(property == ThingType::Cloth)
|
|
|
|
thingType.parameters[ThingType::ClothSlot] = Fw::getU16(fin);
|
2011-08-31 17:03:33 +02:00
|
|
|
}
|
|
|
|
|
2011-11-12 07:24:32 +01:00
|
|
|
int totalSprites = 1;
|
|
|
|
for(int i = 0; i < ThingType::LastDimension; ++i) {
|
|
|
|
if(i == ThingType::ExactSize && thingType.dimensions[ThingType::Width] <= 1 && thingType.dimensions[ThingType::Height] <= 1) {
|
|
|
|
thingType.dimensions[i] = 32;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
thingType.dimensions[i] = Fw::getU8(fin);
|
|
|
|
|
|
|
|
if(i != ThingType::ExactSize)
|
|
|
|
totalSprites *= thingType.dimensions[i];
|
|
|
|
}
|
2011-08-31 17:03:33 +02:00
|
|
|
|
|
|
|
thingType.sprites.resize(totalSprites);
|
2011-11-12 07:24:32 +01:00
|
|
|
for(int i = 0; i < totalSprites; i++)
|
2011-08-31 17:03:33 +02:00
|
|
|
thingType.sprites[i] = Fw::getU16(fin);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
if(category == Item)
|
|
|
|
id -= 100;
|
|
|
|
|
2011-12-01 23:25:32 +01:00
|
|
|
//assert(id < m_things[category].size());
|
|
|
|
|
|
|
|
// assert wrokground
|
|
|
|
if(id >= m_things[category].size()) {
|
|
|
|
//logTraceErrorOnce("got an invalid type");
|
2011-12-02 04:01:56 +01:00
|
|
|
return &m_emptyThingType;
|
2011-12-01 23:25:32 +01:00
|
|
|
}
|
2011-11-12 07:24:32 +01:00
|
|
|
|
2011-12-02 03:29:05 +01:00
|
|
|
return &m_things[category][id];
|
2011-11-12 07:24:32 +01:00
|
|
|
}
|