From 9952ec8092599a5142adae48e050539aaa7c94d9 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Sun, 19 Feb 2006 01:48:04 +0000 Subject: Made use of counted pointer for objects, just the way it is for beings. --- src/object.h | 16 ++++++++++++---- src/state.cpp | 24 ++++++++++++------------ src/state.h | 10 +++++----- 3 files changed, 29 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/object.h b/src/object.h index fbc49cd8..b8640285 100644 --- a/src/object.h +++ b/src/object.h @@ -24,15 +24,14 @@ #ifndef _TMWSERV_OBJECT_H_ #define _TMWSERV_OBJECT_H_ - #include #include - +#include +#include "utils/countedptr.h" namespace tmwserv { - /** * Structure type for the statistics. * @@ -52,7 +51,6 @@ struct Statistics int speed; }; - /** * Generic in-game object definition. * Base class for in-game objects. @@ -179,6 +177,16 @@ class Object unsigned int mMapId; /**< id of the map being is on */ }; +/** + * Type definition for a smart pointer to Object. + */ +typedef utils::CountedPtr ObjectPtr; + + +/** + * Type definition for a list of Objects. + */ +typedef std::vector Objects; } // namespace tmwserv diff --git a/src/state.cpp b/src/state.cpp index cb9a9e22..75dae62e 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -145,23 +145,23 @@ bool State::loadMap(const unsigned int mapId) { return true; // We let true for testing on beings } -void State::addObject(Object *object, const unsigned int mapId) { - if (!objectExists(object)) { +void State::addObject(ObjectPtr objectPtr, const unsigned int mapId) { + if (!objectExists(objectPtr)) { if (!mapExists(mapId)) if (!loadMap(mapId)) return; - maps[mapId].objects.push_back(object); + maps[mapId].objects.push_back(objectPtr); } } -void State::removeObject(Object *object) { +void State::removeObject(ObjectPtr objectPtr) { for (std::map::iterator i = maps.begin(); i != maps.end(); i++) { - for (std::vector::iterator b = i->second.objects.begin(); + for (Objects::iterator b = i->second.objects.begin(); b != i->second.objects.end(); b++) { - if (*b == object) { + if (b->get() == objectPtr.get()) { i->second.objects.erase(b); return; } @@ -169,14 +169,14 @@ void State::removeObject(Object *object) { } } -bool State::objectExists(const Object *object) { +bool State::objectExists(const ObjectPtr objectPtr) { for (std::map::iterator i = maps.begin(); i != maps.end(); i++) { - for (std::vector::iterator b = i->second.objects.begin(); + for (Objects::iterator b = i->second.objects.begin(); b != i->second.objects.end(); b++) { - if (*b == object) + if (b->get() == objectPtr.get()) return true; } } @@ -197,14 +197,14 @@ const unsigned int State::findPlayer(BeingPtr beingPtr) { return 0; } -const unsigned int State::findObject(Object *object) { +const unsigned int State::findObject(ObjectPtr objectPtr) { for (std::map::iterator i = maps.begin(); i != maps.end(); i++) { - for (std::vector::iterator b = i->second.objects.begin(); + for (Objects::iterator b = i->second.objects.begin(); b != i->second.objects.end(); b++) { - if (*b == object) + if (b->get() == objectPtr.get()) return i->first; } } diff --git a/src/state.h b/src/state.h index bc6d3331..ba32bdcd 100644 --- a/src/state.h +++ b/src/state.h @@ -56,7 +56,7 @@ struct MapComposite { /** * Items located on the map */ - std::vector objects; + Objects objects; }; /** @@ -110,17 +110,17 @@ class State : public utils::Singleton /** * Add object to the map */ - void addObject(Object *object, const unsigned int mapId); + void addObject(ObjectPtr objectPtr, const unsigned int mapId); /** * Remove an object from the map */ - void removeObject(Object *object); + void removeObject(ObjectPtr objectPtr); /** * Find out whether an object exists in the game world or not */ - bool objectExists(const Object *object); + bool objectExists(const ObjectPtr objectPtr); /** * Find map player in world is on @@ -130,7 +130,7 @@ class State : public utils::Singleton /** * Find map object in world is on */ - const unsigned int findObject(Object *object); + const unsigned int findObject(ObjectPtr objectPtr); }; } // namespace tmwserv -- cgit v1.2.3-70-g09d2