map stuff

This commit is contained in:
otfallen 2012-06-21 04:31:29 +02:00
parent 9d5d400f7a
commit d139c3738b
15 changed files with 6244 additions and 23 deletions

View File

@ -324,7 +324,13 @@ SET(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/thirdparty/apngloader.cpp ${CMAKE_CURRENT_LIST_DIR}/thirdparty/apngloader.cpp
${CMAKE_CURRENT_LIST_DIR}/thirdparty/apngloader.h ${CMAKE_CURRENT_LIST_DIR}/thirdparty/apngloader.h
${CMAKE_CURRENT_LIST_DIR}/thirdparty/lbitlib-5.2.0-backport4.cpp ${CMAKE_CURRENT_LIST_DIR}/thirdparty/lbitlib-5.2.0-backport4.cpp
${CMAKE_CURRENT_LIST_DIR}/thirdparty/lbitlib-5.2.0-backport4.h ${CMAKE_CURRENT_LIST_DIR}/thirdparty/lbitlib-5.2.0-backport4.h
${CMAKE_CURRENT_LIST_DIR}/thirdparty/tinyxml.cpp
${CMAKE_CURRENT_LIST_DIR}/thirdparty/tinyxml.h
${CMAKE_CURRENT_LIST_DIR}/thirdparty/tinystr.cpp
${CMAKE_CURRENT_LIST_DIR}/thirdparty/tinystr.h
${CMAKE_CURRENT_LIST_DIR}/thirdparty/tinyxmlerror.cpp
${CMAKE_CURRENT_LIST_DIR}/thirdparty/tinyxmlparser.cpp
# ui # ui
${CMAKE_CURRENT_LIST_DIR}/ui/declarations.h ${CMAKE_CURRENT_LIST_DIR}/ui/declarations.h

View File

@ -249,6 +249,30 @@ std::string FileStream::getString()
return str; return str;
} }
uint8 FileStream::readNode(uint8 &oldNode, uint32 &type)
{
if (!oldNode) {
if ((oldNode = getU8()) == 0xFE) {
type = getU32();
return oldNode;
} else {
dump << "Failed to read new node.";
return 0;
}
}
assert(getU8() == 0xFF);
if ((oldNode = getU8()) == 0xFE) {
type = getU32();
return oldNode;
} else {
dump << "Failed to read node with old type: " << type;
return 0;
}
return 0;
}
void FileStream::addU8(uint8 v) void FileStream::addU8(uint8 v)
{ {
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1) if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1)

View File

@ -59,6 +59,7 @@ public:
void addU32(uint8 v); void addU32(uint8 v);
void addU64(uint8 v); void addU64(uint8 v);
uint8 readNode(uint8 &oldNode, uint32 &type);
private: private:
std::string m_name; std::string m_name;
PHYSFS_File *m_fileHandle; PHYSFS_File *m_fileHandle;

111
src/framework/thirdparty/tinystr.cpp vendored Normal file
View File

@ -0,0 +1,111 @@
/*
www.sourceforge.net/projects/tinyxml
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef TIXML_USE_STL
#include "tinystr.h"
// Error value for find primitive
const TiXmlString::size_type TiXmlString::npos = static_cast< TiXmlString::size_type >(-1);
// Null rep.
TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, { '\0' } };
void TiXmlString::reserve (size_type cap)
{
if (cap > capacity())
{
TiXmlString tmp;
tmp.init(length(), cap);
memcpy(tmp.start(), data(), length());
swap(tmp);
}
}
TiXmlString& TiXmlString::assign(const char* str, size_type len)
{
size_type cap = capacity();
if (len > cap || cap > 3*(len + 8))
{
TiXmlString tmp;
tmp.init(len);
memcpy(tmp.start(), str, len);
swap(tmp);
}
else
{
memmove(start(), str, len);
set_size(len);
}
return *this;
}
TiXmlString& TiXmlString::append(const char* str, size_type len)
{
size_type newsize = length() + len;
if (newsize > capacity())
{
reserve (newsize + capacity());
}
memmove(finish(), str, len);
set_size(newsize);
return *this;
}
TiXmlString operator + (const TiXmlString & a, const TiXmlString & b)
{
TiXmlString tmp;
tmp.reserve(a.length() + b.length());
tmp += a;
tmp += b;
return tmp;
}
TiXmlString operator + (const TiXmlString & a, const char* b)
{
TiXmlString tmp;
TiXmlString::size_type b_len = static_cast<TiXmlString::size_type>( strlen(b) );
tmp.reserve(a.length() + b_len);
tmp += a;
tmp.append(b, b_len);
return tmp;
}
TiXmlString operator + (const char* a, const TiXmlString & b)
{
TiXmlString tmp;
TiXmlString::size_type a_len = static_cast<TiXmlString::size_type>( strlen(a) );
tmp.reserve(a_len + b.length());
tmp.append(a, a_len);
tmp += b;
return tmp;
}
#endif // TIXML_USE_STL

305
src/framework/thirdparty/tinystr.h vendored Normal file
View File

@ -0,0 +1,305 @@
/*
www.sourceforge.net/projects/tinyxml
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef TIXML_USE_STL
#ifndef TIXML_STRING_INCLUDED
#define TIXML_STRING_INCLUDED
#include <assert.h>
#include <string.h>
/* The support for explicit isn't that universal, and it isn't really
required - it is used to check that the TiXmlString class isn't incorrectly
used. Be nice to old compilers and macro it here:
*/
#if defined(_MSC_VER) && (_MSC_VER >= 1200 )
// Microsoft visual studio, version 6 and higher.
#define TIXML_EXPLICIT explicit
#elif defined(__GNUC__) && (__GNUC__ >= 3 )
// GCC version 3 and higher.s
#define TIXML_EXPLICIT explicit
#else
#define TIXML_EXPLICIT
#endif
/*
TiXmlString is an emulation of a subset of the std::string template.
Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
Only the member functions relevant to the TinyXML project have been implemented.
The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
a string and there's no more room, we allocate a buffer twice as big as we need.
*/
class TiXmlString
{
public :
// The size type used
typedef size_t size_type;
// Error value for find primitive
static const size_type npos; // = -1;
// TiXmlString empty constructor
TiXmlString () : rep_(&nullrep_)
{
}
// TiXmlString copy constructor
TiXmlString ( const TiXmlString & copy) : rep_(0)
{
init(copy.length());
memcpy(start(), copy.data(), length());
}
// TiXmlString constructor, based on a string
TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0)
{
init( static_cast<size_type>( strlen(copy) ));
memcpy(start(), copy, length());
}
// TiXmlString constructor, based on a string
TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0)
{
init(len);
memcpy(start(), str, len);
}
// TiXmlString destructor
~TiXmlString ()
{
quit();
}
TiXmlString& operator = (const char * copy)
{
return assign( copy, (size_type)strlen(copy));
}
TiXmlString& operator = (const TiXmlString & copy)
{
return assign(copy.start(), copy.length());
}
// += operator. Maps to append
TiXmlString& operator += (const char * suffix)
{
return append(suffix, static_cast<size_type>( strlen(suffix) ));
}
// += operator. Maps to append
TiXmlString& operator += (char single)
{
return append(&single, 1);
}
// += operator. Maps to append
TiXmlString& operator += (const TiXmlString & suffix)
{
return append(suffix.data(), suffix.length());
}
// Convert a TiXmlString into a null-terminated char *
const char * c_str () const { return rep_->str; }
// Convert a TiXmlString into a char * (need not be null terminated).
const char * data () const { return rep_->str; }
// Return the length of a TiXmlString
size_type length () const { return rep_->size; }
// Alias for length()
size_type size () const { return rep_->size; }
// Checks if a TiXmlString is empty
bool empty () const { return rep_->size == 0; }
// Return capacity of string
size_type capacity () const { return rep_->capacity; }
// single char extraction
const char& at (size_type index) const
{
assert( index < length() );
return rep_->str[ index ];
}
// [] operator
char& operator [] (size_type index) const
{
assert( index < length() );
return rep_->str[ index ];
}
// find a char in a string. Return TiXmlString::npos if not found
size_type find (char lookup) const
{
return find(lookup, 0);
}
// find a char in a string from an offset. Return TiXmlString::npos if not found
size_type find (char tofind, size_type offset) const
{
if (offset >= length()) return npos;
for (const char* p = c_str() + offset; *p != '\0'; ++p)
{
if (*p == tofind) return static_cast< size_type >( p - c_str() );
}
return npos;
}
void clear ()
{
//Lee:
//The original was just too strange, though correct:
// TiXmlString().swap(*this);
//Instead use the quit & re-init:
quit();
init(0,0);
}
/* Function to reserve a big amount of data when we know we'll need it. Be aware that this
function DOES NOT clear the content of the TiXmlString if any exists.
*/
void reserve (size_type cap);
TiXmlString& assign (const char* str, size_type len);
TiXmlString& append (const char* str, size_type len);
void swap (TiXmlString& other)
{
Rep* r = rep_;
rep_ = other.rep_;
other.rep_ = r;
}
private:
void init(size_type sz) { init(sz, sz); }
void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
char* start() const { return rep_->str; }
char* finish() const { return rep_->str + rep_->size; }
struct Rep
{
size_type size, capacity;
char str[1];
};
void init(size_type sz, size_type cap)
{
if (cap)
{
// Lee: the original form:
// rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
// doesn't work in some cases of new being overloaded. Switching
// to the normal allocation, although use an 'int' for systems
// that are overly picky about structure alignment.
const size_type bytesNeeded = sizeof(Rep) + cap;
const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
rep_->str[ rep_->size = sz ] = '\0';
rep_->capacity = cap;
}
else
{
rep_ = &nullrep_;
}
}
void quit()
{
if (rep_ != &nullrep_)
{
// The rep_ is really an array of ints. (see the allocator, above).
// Cast it back before delete, so the compiler won't incorrectly call destructors.
delete [] ( reinterpret_cast<int*>( rep_ ) );
}
}
Rep * rep_;
static Rep nullrep_;
} ;
inline bool operator == (const TiXmlString & a, const TiXmlString & b)
{
return ( a.length() == b.length() ) // optimization on some platforms
&& ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
}
inline bool operator < (const TiXmlString & a, const TiXmlString & b)
{
return strcmp(a.c_str(), b.c_str()) < 0;
}
inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
TiXmlString operator + (const TiXmlString & a, const char* b);
TiXmlString operator + (const char* a, const TiXmlString & b);
/*
TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
Only the operators that we need for TinyXML have been developped.
*/
class TiXmlOutStream : public TiXmlString
{
public :
// TiXmlOutStream << operator.
TiXmlOutStream & operator << (const TiXmlString & in)
{
*this += in;
return *this;
}
// TiXmlOutStream << operator.
TiXmlOutStream & operator << (const char * in)
{
*this += in;
return *this;
}
} ;
#endif // TIXML_STRING_INCLUDED
#endif // TIXML_USE_STL

1886
src/framework/thirdparty/tinyxml.cpp vendored Normal file

File diff suppressed because it is too large Load Diff

1805
src/framework/thirdparty/tinyxml.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,52 @@
/*
www.sourceforge.net/projects/tinyxml
Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "tinyxml.h"
// The goal of the seperate error file is to make the first
// step towards localization. tinyxml (currently) only supports
// english error messages, but the could now be translated.
//
// It also cleans up the code a bit.
//
const char* TiXmlBase::errorString[ TiXmlBase::TIXML_ERROR_STRING_COUNT ] =
{
"No error",
"Error",
"Failed to open file",
"Error parsing Element.",
"Failed to read Element name",
"Error reading Element value.",
"Error reading Attributes.",
"Error: empty tag.",
"Error reading end tag.",
"Error parsing Unknown.",
"Error parsing Comment.",
"Error parsing Declaration.",
"Error document empty.",
"Error null (0) or unexpected EOF found in input stream.",
"Error parsing CDATA.",
"Error when TiXmlDocument added to document, because TiXmlDocument can only be at the root.",
};

1638
src/framework/thirdparty/tinyxmlparser.cpp vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -44,11 +44,13 @@ class StaticText;
class ThingType; class ThingType;
class ThingsType; class ThingsType;
class ItemShader; class ItemShader;
class ItemData;
typedef std::shared_ptr<MapView> MapViewPtr; typedef std::shared_ptr<MapView> MapViewPtr;
typedef std::shared_ptr<Tile> TilePtr; typedef std::shared_ptr<Tile> TilePtr;
typedef std::shared_ptr<Thing> ThingPtr; typedef std::shared_ptr<Thing> ThingPtr;
typedef std::shared_ptr<Item> ItemPtr; typedef std::shared_ptr<Item> ItemPtr;
typedef std::shared_ptr<ItemData> ItemDataPtr;
typedef std::shared_ptr<Container> ContainerPtr; typedef std::shared_ptr<Container> ContainerPtr;
typedef std::shared_ptr<Creature> CreaturePtr; typedef std::shared_ptr<Creature> CreaturePtr;
typedef std::shared_ptr<Monster> MonsterPtr; typedef std::shared_ptr<Monster> MonsterPtr;
@ -63,5 +65,6 @@ typedef std::shared_ptr<ItemShader> ItemShaderPtr;
typedef std::vector<ThingPtr> ThingList; typedef std::vector<ThingPtr> ThingList;
typedef std::vector<ThingType> ThingTypeList; typedef std::vector<ThingType> ThingTypeList;
typedef std::vector<ItemDataPtr> ItemDataList;
#endif #endif

View File

@ -26,9 +26,11 @@
#include "thing.h" #include "thing.h"
#include "tile.h" #include "tile.h"
#include "shadermanager.h" #include "shadermanager.h"
#include <framework/core/clock.h> #include <framework/core/clock.h>
#include <framework/core/eventdispatcher.h> #include <framework/core/eventdispatcher.h>
#include <framework/graphics/graphics.h> #include <framework/graphics/graphics.h>
#include <framework/core/filestream.h>
Item::Item() : Thing() Item::Item() : Thing()
{ {
@ -185,3 +187,72 @@ void Item::setId(uint32 id)
m_id = id; m_id = id;
m_type = g_thingsType.getThingType(m_id, ThingsType::Item); m_type = g_thingsType.getThingType(m_id, ThingsType::Item);
} }
bool Item::unserializeAttr(FileStreamPtr fin)
{
uint8 attrType;
while ((attrType = fin->getU8()) != 0)
readAttr((AttrTypes_t)attrType, fin);
return true;
}
void Item::readAttr(AttrTypes_t attrType, FileStreamPtr fin)
{
switch (attrType) {
case ATTR_COUNT:
setSubType(fin->getU8());
break;
case ATTR_ACTION_ID:
setActionId(fin->getU16());
break;
case ATTR_UNIQUE_ID:
setUniqueId(fin->getU16());
break;
case ATTR_NAME:
setName(fin->getString());
break;
case ATTR_ARTICLE:
fin->getString();
case ATTR_ATTACK: // \/ not needed.
case ATTR_EXTRAATTACK:
case ATTR_DEFENSE:
case ATTR_EXTRADEFENSE:
case ATTR_ARMOR:
case ATTR_ATTACKSPEED:
case ATTR_HPPITCHANCE:
case ATTR_DURATION:
fin->getU32();
break;
case ATTR_SCRIPTPROTECTED:
case ATTR_DUALWIELD:
case ATTR_DECAYING_STATE:
case ATTR_HPPOUSEDOORID:
fin->getU8();
break;
case ATTR_TEXT:
setText(fin->getString());
break;
case ATTR_WRITTENDATE:
fin->getU32();
break;
case ATTR_WRITTENBY:
fin->getString();
break;
case ATTR_DESC:
setDescription(fin->getString());
break;
case ATTR_RUNE_CHARGES:
fin->getU8();
break;
case ATTR_TELE_DEST: // Teleport should read that.
case ATTR_SLEEPERGUID: // Bed should read that.
case ATTR_SLEEPSTART:
case ATTR_CONTAINER_ITEMS:
case ATTR_ATTRIBUTE_MAP:
default:
break;
}
}

View File

@ -26,6 +26,47 @@
#include <framework/global.h> #include <framework/global.h>
#include "thing.h" #include "thing.h"
enum AttrTypes_t {
ATTR_END = 0,
//ATTR_DESCRIPTION = 1,
//ATTR_EXT_FILE = 2,
ATTR_TILE_FLAGS = 3,
ATTR_ACTION_ID = 4,
ATTR_UNIQUE_ID = 5,
ATTR_TEXT = 6,
ATTR_DESC = 7,
ATTR_TELE_DEST = 8,
ATTR_ITEM = 9,
ATTR_DEPOT_ID = 10,
//ATTR_EXT_SPAWN_FILE = 11,
ATTR_RUNE_CHARGES = 12,
//ATTR_EXT_HPPOUSE_FILE = 13,
ATTR_HPPOUSEDOORID = 14,
ATTR_COUNT = 15,
ATTR_DURATION = 16,
ATTR_DECAYING_STATE = 17,
ATTR_WRITTENDATE = 18,
ATTR_WRITTENBY = 19,
ATTR_SLEEPERGUID = 20,
ATTR_SLEEPSTART = 21,
ATTR_CHARGES = 22,
ATTR_CONTAINER_ITEMS = 23,
ATTR_NAME = 30,
ATTR_PLURALNAME = 31,
ATTR_ATTACK = 33,
ATTR_EXTRAATTACK = 34,
ATTR_DEFENSE = 35,
ATTR_EXTRADEFENSE = 36,
ATTR_ARMOR = 37,
ATTR_ATTACKSPEED = 38,
ATTR_HPPITCHANCE = 39,
ATTR_SHOOTRANGE = 40,
ATTR_ARTICLE = 41,
ATTR_SCRIPTPROTECTED = 42,
ATTR_DUALWIELD = 43,
ATTR_ATTRIBUTE_MAP = 128
};
class Item : public Thing class Item : public Thing
{ {
public: public:
@ -39,18 +80,33 @@ public:
void setCountOrSubType(int value) { m_countOrSubType = value; } void setCountOrSubType(int value) { m_countOrSubType = value; }
void setCount(int count) { m_countOrSubType = count; } void setCount(int count) { m_countOrSubType = count; }
void setSubType(int subType) { m_countOrSubType = subType; } void setSubType(int subType) { m_countOrSubType = subType; }
void setActionId(int actionId) { m_actionId = actionId; }
void setUniqueId(int uniqueId) { m_uniqueId = uniqueId; }
void setName(const std::string &name) { m_name = name; }
void setText(const std::string &text) { m_text = text; }
void setDescription(const std::string &description) { m_description = description; }
int getCountOrSubType() { return m_countOrSubType; } int getCountOrSubType() { return m_countOrSubType; }
int getSubType() { return m_countOrSubType; } int getSubType() { return m_countOrSubType; }
int getCount() { return m_countOrSubType; } int getCount() { return m_countOrSubType; }
uint32 getId() { return m_id; } uint32 getId() { return m_id; }
std::string getName() { return m_name; }
ItemPtr asItem() { return std::static_pointer_cast<Item>(shared_from_this()); } ItemPtr asItem() { return std::static_pointer_cast<Item>(shared_from_this()); }
bool isItem() { return true; } bool isItem() { return true; }
// TODO: These should be abstract and declared in i.e containers, doors, etc.
bool unserializeAttr(FileStreamPtr fin);
bool unserializeItemNode(FileStreamPtr fin, uint8) { return unserializeAttr(fin); }
void readAttr(AttrTypes_t attrType, FileStreamPtr fin);
bool isMovable() { return false; }
private: private:
uint16 m_id; uint16 m_id;
uint8 m_countOrSubType; uint8 m_countOrSubType;
uint32 m_actionId, m_uniqueId;
std::string m_name, m_text, m_description;
PainterShaderProgramPtr m_shaderProgram; PainterShaderProgramPtr m_shaderProgram;
}; };

View File

@ -27,10 +27,12 @@
#include "item.h" #include "item.h"
#include "missile.h" #include "missile.h"
#include "statictext.h" #include "statictext.h"
#include "itemloader.h"
#include <framework/core/eventdispatcher.h> #include <framework/core/eventdispatcher.h>
#include "mapview.h" #include "mapview.h"
#include <framework/core/resourcemanager.h> #include <framework/core/resourcemanager.h>
#include <framework/core/filestream.h>
Map g_map; Map g_map;
@ -54,32 +56,221 @@ void Map::notificateTileUpdateToMapViews(const Position& pos)
bool Map::load(const std::string& fileName) bool Map::load(const std::string& fileName)
{ {
if(!g_resources.fileExists(fileName)) { FileStreamPtr fin = g_resources.openFile(fileName);
g_logger.error(stdext::format("Unable to load map '%s'", fileName)); if (!fin) {
return false; g_logger.error(stdext::format("Unable to load map '%s'", fileName));
} return false;
}
std::stringstream in; if (!g_itemLoader.isLoaded()) {
g_resources.loadFile(fileName, in); g_logger.error(stdext::format("OTB and XML are not loaded yet to load a map."));
return false;
}
while(!in.eof()) { uint32 type = 0;
Position pos; uint8 root = fin->readNode(root, type);
in.read((char*)&pos, sizeof(pos));
uint16 id; uint32 headerVersion = fin->getU32();
in.read((char*)&id, sizeof(id)); if (!headerVersion || headerVersion > 3) {
while(id != 0xFFFF) { g_logger.error("Unknown OTBM version detected.");
ItemPtr item = Item::create(id); return false;
if(item->isStackable() || item->isFluidContainer() || item->isFluid()) { }
uint8 countOrSubType;
in.read((char*)&countOrSubType, sizeof(countOrSubType));
item->setCountOrSubType(countOrSubType);
}
addThing(item, pos, 255);
in.read((char*)&id, sizeof(id));
}
}
uint32 headerMajorItems = fin->getU32();
if (headerMajorItems < 3) {
g_logger.error("This map needs to be upgraded.");
return false;
}
if (headerMajorItems > g_itemLoader.dwMajorVersion) {
g_logger.error("This map was saved with different OTB version.");
return false;
}
uint32_t headerMinorItems = fin->getU32();
if (headerMinorItems > g_itemLoader.dwMinorVersion)
g_logger.warning("This map needs an updated OTB.");
uint8 node = fin->readNode(root, type);
if (type != OTBM_MAP_DATA) {
g_logger.error("Could not read data node.");
return false;
}
std::string tmp;
uint8 attribute;
while ((attribute = fin->getU8())) {
tmp = fin->getString();
switch (attribute) {
case OTBM_ATTR_DESCRIPTION:
if (!m_description.empty())
m_description += "\n" + tmp;
else
m_description = tmp;
break;
case OTBM_ATTR_SPAWN_FILE:
m_spawnFile = fileName.substr(0, fileName.rfind('/') + 1) + tmp;
break;
case OTBM_ATTR_HOUSE_FILE:
m_houseFile = fileName.substr(0, fileName.rfind('/') + 1) + tmp;
break;
default:
g_logger.error(stdext::format("Invalid attribute '%c'", attribute));
break;
}
}
dump << m_description;
uint8 nodeMapData;
do {
nodeMapData = fin->readNode(node, type);
if (type == OTBM_TILE_AREA) {
uint16 baseX = fin->getU16(), baseY = fin->getU16();
uint8 pz = fin->getU8();
uint8 nodeTile;
do {
nodeTile = fin->readNode(nodeMapData, type);
if (type == OTBM_TILE || type == OTBM_HOUSETILE) {
TilePtr tile = 0;
ItemPtr ground = 0;
uint32 flags = 0;
uint16 px = baseX + fin->getU16(), py = fin->getU16();
Position pos(px, py, pz);
// TODO: Houses.
if (type == OTBM_HOUSETILE) {
uint32 hId = fin->getU32();
tile = createTile(pos);
// TODO: add it to house.
}
uint8 tileAttr;
while ((tileAttr = fin->getU8())) {
switch (tileAttr) {
case OTBM_ATTR_TILE_FLAGS: {
uint32 _flags = fin->getU32();
if ((_flags & TILESTATE_PROTECTIONZONE) == TILESTATE_PROTECTIONZONE)
flags |= TILESTATE_PROTECTIONZONE;
else if ((_flags & TILESTATE_OPTIONALZONE) == TILESTATE_OPTIONALZONE)
flags |= TILESTATE_OPTIONALZONE;
else if ((_flags & TILESTATE_HPPARDCOREZONE) == TILESTATE_HPPARDCOREZONE)
flags |= TILESTATE_HPPARDCOREZONE;
if ((_flags & TILESTATE_NOLOGOUT) == TILESTATE_NOLOGOUT)
flags |= TILESTATE_NOLOGOUT;
if ((_flags & TILESTATE_REFRESH) == TILESTATE_REFRESH)
flags |= TILESTATE_REFRESH;
break;
} case OTBM_ATTR_ITEM: {
ItemPtr item = Item::create(fin->getU16());
if (!item) {
g_logger.error(stdext::format("failed to create new item at tile pos %d, %d, %d", px, py, pz));
return false;
}
if (tile) {
tile->addThing(item);
} else if (item->isGround()) {
ground = item;
} else {
tile = createTile(pos);
tile->addThing(ground);
tile->addThing(item);
}
} default: {
g_logger.error(stdext::format("invalid tile attribute at pos %d, %d, %d", px, py, pz));
return false;
}
}
}
uint8 nodeItem;
do {
nodeItem = fin->readNode(nodeTile, type);
if (type == OTBM_ITEM) {
ItemPtr item = Item::create(fin->getU16());
if (!item) {
g_logger.error(stdext::format("failed to create new item at pos %d, %d, %d", px, py, pz));
return false;
}
if (item->unserializeItemNode(fin, nodeItem)) {
if (/* house && */item->isMovable()) {
g_logger.warning(stdext::format("Movable item found in house: %d at pos %d %d %d", item->getId(),
px, py, pz));
item = 0;
} else if (tile) {
tile->addThing(item);
} else if (item->isGround()) {
ground = item;
} else {
tile = createTile(pos);
tile->addThing(ground);
tile->addThing(item);
}
} else {
g_logger.error(stdext::format("failed to unserialize item with %d at pos %d %d %d", item->getId(),
px, py, pz));
return false;
}
} else {
g_logger.error(stdext::format("Unknown item node type %d", type));
return false;
}
} while (nodeItem);
if (!tile) {
tile = createTile(pos);
tile->addThing(ground);
}
tile->setFlags((tileflags_t)flags);
} else {
g_logger.error(stdext::format("Unknown tile node type %d", type));
return false;
}
} while (nodeTile);
} else if (type == OTBM_TOWNS) {
uint8 nodeTown;
do {
nodeTown = fin->readNode(nodeMapData, type);
if (type == OTBM_TOWN) {
uint32 townId = fin->getU32();
std::string townName = fin->getString();
Position townCoords(fin->getU16(), fin->getU16(), fin->getU8());
} else {
g_logger.error(stdext::format("Unknown town node type %d", type));
return false;
}
} while (nodeTown);
} else if (type == OTBM_WAYPOINTS && headerVersion > 1) {
uint8 nodeWaypoint;
do {
nodeWaypoint = fin->readNode(nodeMapData, type);
if (type == OTBM_WAYPOINT) {
std::string name = fin->getString();
Position waypointPos(fin->getU16(), fin->getU16(), fin->getU8());
}
} while (nodeWaypoint);
} else {
g_logger.error(stdext::format("Unknown map node %d", type));
return false;
}
} while (nodeMapData);
// TODO: Load house & spawns.
return true; return true;
} }

View File

@ -27,6 +27,54 @@
#include "animatedtext.h" #include "animatedtext.h"
#include <framework/core/clock.h> #include <framework/core/clock.h>
enum OTBM_AttrTypes_t
{
OTBM_ATTR_DESCRIPTION = 1,
OTBM_ATTR_EXT_FILE = 2,
OTBM_ATTR_TILE_FLAGS = 3,
OTBM_ATTR_ACTION_ID = 4,
OTBM_ATTR_UNIQUE_ID = 5,
OTBM_ATTR_TEXT = 6,
OTBM_ATTR_DESC = 7,
OTBM_ATTR_TELE_DEST = 8,
OTBM_ATTR_ITEM = 9,
OTBM_ATTR_DEPOT_ID = 10,
OTBM_ATTR_SPAWN_FILE = 11,
OTBM_ATTR_RUNE_CHARGES = 12,
OTBM_ATTR_HOUSE_FILE = 13,
OTBM_ATTR_HPPOUSEDOORID = 14,
OTBM_ATTR_COUNT = 15,
OTBM_ATTR_DURATION = 16,
OTBM_ATTR_DECAYING_STATE = 17,
OTBM_ATTR_WRITTENDATE = 18,
OTBM_ATTR_WRITTENBY = 19,
OTBM_ATTR_SLEEPERGUID = 20,
OTBM_ATTR_SLEEPSTART = 21,
OTBM_ATTR_CHARGES = 22,
OTBM_ATTR_CONTAINER_ITEMS = 23,
OTBM_ATTR_ATTRIBUTE_MAP = 128
};
enum OTBM_NodeTypes_t
{
OTBM_ROOTV2 = 1,
OTBM_MAP_DATA = 2,
OTBM_ITEM_DEF = 3,
OTBM_TILE_AREA = 4,
OTBM_TILE = 5,
OTBM_ITEM = 6,
OTBM_TILE_SQUARE = 7,
OTBM_TILE_REF = 8,
OTBM_SPAWNS = 9,
OTBM_SPAWN_AREA = 10,
OTBM_MONSTER = 11,
OTBM_TOWNS = 12,
OTBM_TOWN = 13,
OTBM_HOUSETILE = 14,
OTBM_WAYPOINTS = 15,
OTBM_WAYPOINT = 16
};
class Map class Map
{ {
public: public:
@ -89,6 +137,8 @@ private:
Light m_light; Light m_light;
Position m_centralPosition; Position m_centralPosition;
std::string m_description, m_spawnFile, m_houseFile;
}; };
extern Map g_map; extern Map g_map;

View File

@ -26,6 +26,26 @@
#include "declarations.h" #include "declarations.h"
#include <framework/luascript/luaobject.h> #include <framework/luascript/luaobject.h>
enum tileflags_t
{
TILESTATE_NONE = 0,
TILESTATE_PROTECTIONZONE = 1 << 0,
TILESTATE_TRASHED = 1 << 1,
TILESTATE_OPTIONALZONE = 1 << 2,
TILESTATE_NOLOGOUT = 1 << 3,
TILESTATE_HPPARDCOREZONE = 1 << 4,
TILESTATE_REFRESH = 1 << 5,
// internal usage
TILESTATE_HOUSE = 1 << 6,
TILESTATE_TELEPORT = 1 << 17,
TILESTATE_MAGICFIELD = 1 << 18,
TILESTATE_MAILBOX = 1 << 19,
TILESTATE_TRASHHOLDER = 1 << 20,
TILESTATE_BED = 1 << 21,
TILESTATE_DEPOT = 1 << 22
};
class Tile : public LuaObject class Tile : public LuaObject
{ {
public: public:
@ -78,6 +98,7 @@ public:
bool canErase(); bool canErase();
TilePtr asTile() { return std::static_pointer_cast<Tile>(shared_from_this()); } TilePtr asTile() { return std::static_pointer_cast<Tile>(shared_from_this()); }
void setFlags(tileflags_t flags) { m_flags |= (uint32)flags; }
private: private:
std::vector<CreaturePtr> m_walkingCreatures; std::vector<CreaturePtr> m_walkingCreatures;
@ -85,6 +106,7 @@ private:
std::vector<ThingPtr> m_things; std::vector<ThingPtr> m_things;
Position m_position; Position m_position;
uint8 m_drawElevation; uint8 m_drawElevation;
uint32 m_flags;
}; };
#endif #endif