Skip to content

Commit

Permalink
Cleanup and cast functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mignari committed Jan 26, 2023
1 parent d37c107 commit 873b066
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 117 deletions.
34 changes: 16 additions & 18 deletions source/complexitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,28 @@ Container::~Container()
for(Item* item : contents) {
delete item;
}
contents.clear();
}

Item* Container::deepCopy() const
{
Item* copy = Item::deepCopy();
Container* copyContainer = dynamic_cast<Container*>(copy);
if(copyContainer) {
for(Item* item : contents) {
copyContainer->contents.push_back(item->deepCopy());
if(Container* container = copy->getContainer()) {
for(const Item* item : contents) {
container->contents.push_back(item->deepCopy());
}
}
return copy;
}

Item* Container::getItem(size_t index) const
{
if(index < contents.size()) {
return contents[index];
if(index >= 0 && index < contents.size()) {
return contents.at(index);
}
return nullptr;
}

double Container::getWeight()
{
return getItemType().weight;
}

// Teleport
Teleport::Teleport(const uint16_t type) : Item(type, 0),
destination(0, 0, 0)
Expand All @@ -68,8 +63,10 @@ Teleport::Teleport(const uint16_t type) : Item(type, 0),

Item* Teleport::deepCopy() const
{
Teleport* copy = static_cast<Teleport*>(Item::deepCopy());
copy->destination = destination;
Item* copy = Item::deepCopy();
if(Teleport* teleport = copy->getTeleport()) {
teleport->setDestination(destination);
}
return copy;
}

Expand All @@ -82,8 +79,10 @@ Door::Door(const uint16_t type) : Item(type, 0),

Item* Door::deepCopy() const
{
Door* copy = static_cast<Door*>(Item::deepCopy());
copy->doorId = doorId;
Item* copy = Item::deepCopy();
if(Door* door = copy->getDoor()) {
door->doorId = doorId;
}
return copy;
}

Expand All @@ -97,9 +96,8 @@ Depot::Depot(const uint16_t type) : Item(type, 0),
Item* Depot::deepCopy() const
{
Item* copy = Item::deepCopy();
Depot* copy_depot = dynamic_cast<Depot*>(copy);
if(copy_depot) {
copy_depot->depotId = depotId;
if(Depot* depot = copy->getDepot()) {
depot->depotId = depotId;
}
return copy;
}
120 changes: 60 additions & 60 deletions source/complexitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,91 +34,91 @@ struct OTBM_TeleportDestination

class Container : public Item
{
public:
Container(const uint16_t type);
~Container();
public:
Container(const uint16_t type);
~Container();

Item* deepCopy() const;
Item* getItem(size_t index) const;
Item* deepCopy() const override;
Container* getContainer() override { return this; }

size_t getItemCount() const noexcept { return contents.size(); }
size_t getVolume() const noexcept { return getItemType().volume; }
Item* getItem(size_t index) const;

ItemVector& getVector() { return contents; }
double getWeight();
ItemVector& getVector() noexcept { return contents; }
size_t getItemCount() const noexcept { return contents.size(); }
size_t getVolume() const noexcept { return getItemType().volume; }
double getWeight() noexcept { return getItemType().weight; }

virtual bool unserializeItemNode_OTBM(const IOMap& maphandle, BinaryNode* node);
virtual bool serializeItemNode_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
//virtual bool unserializeItemNode_OTMM(const IOMap& maphandle, BinaryNode* node);
//virtual bool serializeItemNode_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual bool unserializeItemNode_OTBM(const IOMap& maphandle, BinaryNode* node);
virtual bool serializeItemNode_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
//virtual bool unserializeItemNode_OTMM(const IOMap& maphandle, BinaryNode* node);
//virtual bool serializeItemNode_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;

protected:
ItemVector contents;
protected:
ItemVector contents;
};

class Teleport : public Item
{
public:
Teleport(const uint16_t type);

Item* deepCopy() const;

virtual void serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual bool readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* node);
//virtual void serializeItemAttributes_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
//virtual bool readItemAttribute_OTMM(const IOMap& maphandle, OTMM_ItemAttribute attr, BinaryNode* node);

const Position& getDestination() const noexcept { return destination; }
int getX() const noexcept { return destination.x; }
int getY() const noexcept { return destination.y; }
int getZ() const noexcept { return destination.z; }
void setDestination(const Position& position) { destination = position; }

bool hasDestination() const { return destination.isValid(); }

protected:
// We could've made this public and skip the functions, but that would
// make the handling of aid/uid/text different from handling teleports,
// which would be weird.
Position destination;
public:
Teleport(const uint16_t type);

Item* deepCopy() const override;
Teleport* getTeleport() override { return this; }

virtual void serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual bool readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* node);
//virtual void serializeItemAttributes_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
//virtual bool readItemAttribute_OTMM(const IOMap& maphandle, OTMM_ItemAttribute attr, BinaryNode* node);

const Position& getDestination() const noexcept { return destination; }
int getX() const noexcept { return destination.x; }
int getY() const noexcept { return destination.y; }
int getZ() const noexcept { return destination.z; }
void setDestination(const Position& position) noexcept { destination = position; }
bool hasDestination() const noexcept { return destination.isValid(); }

protected:
Position destination;
};

class Door : public Item
{
public:
Door(const uint16_t type);
public:
Door(const uint16_t type);

Item* deepCopy() const;
Item* deepCopy() const override;
Door* getDoor() override { return this; }

uint8_t getDoorID() const { return doorId; }
void setDoorID(uint8_t id) { doorId = id; }
uint8_t getDoorID() const { return doorId; }
void setDoorID(uint8_t id) { doorId = id; }

virtual void serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual bool readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* node);
//virtual void serializeItemAttributes_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
//virtual bool readItemAttribute_OTMM(const IOMap& maphandle, OTMM_ItemAttribute attr, BinaryNode* node);
virtual void serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual bool readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* node);
//virtual void serializeItemAttributes_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
//virtual bool readItemAttribute_OTMM(const IOMap& maphandle, OTMM_ItemAttribute attr, BinaryNode* node);

protected:
uint8_t doorId;
protected:
uint8_t doorId;
};

class Depot : public Item
{
public:
Depot(const uint16_t _type);
public:
Depot(const uint16_t _type);

Item* deepCopy() const;
Item* deepCopy() const override;
Depot* getDepot() override { return this; }

uint8_t getDepotID() const { return depotId; }
void setDepotID(uint8_t id) { depotId = id; }
uint8_t getDepotID() const { return depotId; }
void setDepotID(uint8_t id) { depotId = id; }

virtual void serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual bool readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* node);
//virtual void serializeItemAttributes_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
//virtual bool readItemAttribute_OTMM(const IOMap& maphandle, OTMM_ItemAttribute attr, BinaryNode* node);
virtual void serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual bool readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* node);
//virtual void serializeItemAttributes_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
//virtual bool readItemAttribute_OTMM(const IOMap& maphandle, OTMM_ItemAttribute attr, BinaryNode* node);

protected:
uint8_t depotId;
protected:
uint8_t depotId;
};

#endif
73 changes: 34 additions & 39 deletions source/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
//#include "iomap_otmm.h"
#include "item_attributes.h"

class Creature;
class Border;
class Tile;

enum ITEMPROPERTY {
BLOCKSOLID,
HASHEIGHT,
Expand Down Expand Up @@ -71,6 +67,14 @@ enum SplashType

IMPLEMENT_INCREMENT_OP(SplashType)

class Creature;
class Border;
class Tile;
class Container;
class Depot;
class Teleport;
class Door;

class Item : public ItemAttributes
{
public:
Expand All @@ -92,48 +96,39 @@ class Item : public ItemAttributes

// Get memory footprint size
uint32_t memsize() const;
/*

virtual Container* getContainer() { return nullptr; }
virtual const Container* getContainer() const { return nullptr; }
virtual Depot* getDepot() { return nullptr; }
virtual Teleport* getTeleport() { return nullptr; }
virtual const Teleport* getTeleport() const { return nullptr; }
virtual TrashHolder* getTrashHolder() { return nullptr; }
virtual const TrashHolder* getTrashHolder() const { return nullptr; }
virtual Mailbox* getMailbox() { return nullptr; }
virtual const Mailbox* getMailbox() const { return nullptr; }
virtual Door* getDoor() { return nullptr; }
virtual const Door* getDoor() const { return nullptr; }
virtual MagicField* getMagicField() { return nullptr; }
virtual const MagicField* getMagicField() const { return nullptr; }
*/

// OTBM map interface
// Serialize and unserialize (for save/load)
// Used internally
virtual bool readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* stream);
virtual bool unserializeAttributes_OTBM(const IOMap& maphandle, BinaryNode* stream);
virtual bool unserializeItemNode_OTBM(const IOMap& maphandle, BinaryNode* node);

// Will return a node containing this item
virtual bool serializeItemNode_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
// Will write this item to the stream supplied in the argument
virtual void serializeItemCompact_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual void serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
// Serialize and unserialize (for save/load)
// Used internally
virtual bool readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* stream);
virtual bool unserializeAttributes_OTBM(const IOMap& maphandle, BinaryNode* stream);
virtual bool unserializeItemNode_OTBM(const IOMap& maphandle, BinaryNode* node);

// Will return a node containing this item
virtual bool serializeItemNode_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
// Will write this item to the stream supplied in the argument
virtual void serializeItemCompact_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual void serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& f) const;

// OTMM map interface
/*
// Serialize and unserialize (for save/load)
// Used internally
virtual bool readItemAttribute_OTMM(const IOMap& maphandle, OTMM_ItemAttribute attr, BinaryNode* stream);
virtual bool unserializeAttributes_OTMM(const IOMap& maphandle, BinaryNode* stream);
virtual bool unserializeItemNode_OTMM(const IOMap& maphandle, BinaryNode* node);
// Will return a node containing this item
virtual bool serializeItemNode_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
// Will write this item to the stream supplied in the argument
virtual void serializeItemCompact_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual void serializeItemAttributes_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
*/
/*
// Serialize and unserialize (for save/load)
// Used internally
virtual bool readItemAttribute_OTMM(const IOMap& maphandle, OTMM_ItemAttribute attr, BinaryNode* stream);
virtual bool unserializeAttributes_OTMM(const IOMap& maphandle, BinaryNode* stream);
virtual bool unserializeItemNode_OTMM(const IOMap& maphandle, BinaryNode* node);
// Will return a node containing this item
virtual bool serializeItemNode_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
// Will write this item to the stream supplied in the argument
virtual void serializeItemCompact_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
virtual void serializeItemAttributes_OTMM(const IOMap& maphandle, NodeFileWriteHandle& f) const;
*/

// Static conversions
static std::string LiquidID2Name(uint16_t id);
Expand Down

0 comments on commit 873b066

Please sign in to comment.