summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-07-07 16:50:47 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-07-07 16:50:47 +0000
commit04e694b067a21dee8e13368c17d1815cc0624ce4 (patch)
treea57ab98d76040838fa369e6823544baeb19d1bbc
parent60b4a57bdfe664a6729b3573a6a614621b6c2b2c (diff)
downloadmanaserv-04e694b067a21dee8e13368c17d1815cc0624ce4.tar.gz
manaserv-04e694b067a21dee8e13368c17d1815cc0624ce4.tar.xz
manaserv-04e694b067a21dee8e13368c17d1815cc0624ce4.zip
Simplified code by using map pointers only, instead of using both map IDs and map pointers.
-rw-r--r--ChangeLog19
-rw-r--r--src/common/inventorydata.hpp2
-rw-r--r--src/game-server/character.cpp12
-rw-r--r--src/game-server/character.hpp82
-rw-r--r--src/game-server/gamehandler.cpp5
-rw-r--r--src/game-server/mapcomposite.cpp197
-rw-r--r--src/game-server/mapcomposite.hpp135
-rw-r--r--src/game-server/mapmanager.cpp62
-rw-r--r--src/game-server/mapmanager.hpp28
-rw-r--r--src/game-server/monster.cpp10
-rw-r--r--src/game-server/movingobject.cpp4
-rw-r--r--src/game-server/spawnarea.cpp17
-rw-r--r--src/game-server/spawnarea.hpp2
-rw-r--r--src/game-server/state.cpp58
-rw-r--r--src/game-server/state.hpp20
-rw-r--r--src/game-server/testing.cpp30
-rw-r--r--src/game-server/thing.hpp21
-rw-r--r--src/game-server/trigger.cpp12
-rw-r--r--src/game-server/trigger.hpp9
19 files changed, 330 insertions, 395 deletions
diff --git a/ChangeLog b/ChangeLog
index 87c8f9a..8cee769 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,21 @@
-2007-07-03 Guillaume Melquiond <guillaume.melquiond@gmail.com>
+2007-07-07 Guillaume Melquiond <guillaume.melquiond@gmail.com>
+
+ * src/common/inventorydata.hpp: Fixed missing include.
+ * src/game-server/mapcomposite.cpp, src/game-server/mapcomposite.hpp:
+ Moved implementation details out of the header.
+ * src/game-server/mapmanager.cpp, src/game-server/mapmanager.hpp,
+ src/game-server/state.cpp, src/game-server/state.hpp: Merged LoadedMap
+ and MapComposite classes. Delegated handling of composite maps to the
+ map manager.
+ * src/game-server/testing.cpp, src/game-server/thing.hpp,
+ src/game-server/spawnarea.cpp, src/game-server/spawnarea.hpp,
+ src/game-server/movingobject.cpp, src/game-server/monster.cpp,
+ src/game-server/character.cpp, src/game-server/character.hpp,
+ src/game-server/trigger.cpp, src/game-server/trigger.hpp,
+ src/gamehandler.cpp: Simplified code by using map pointers only,
+ instead of using both map IDs and map pointers.
+
+2007-07-03 Guillaume Melquiond <guillaume.melquiond@gmail.com>
* src/account-server/dalstorage.cpp: Added error messages for failed
SQL requests when retrieving character. Added check for failed
diff --git a/src/common/inventorydata.hpp b/src/common/inventorydata.hpp
index 51cb034..88bb708 100644
--- a/src/common/inventorydata.hpp
+++ b/src/common/inventorydata.hpp
@@ -23,6 +23,8 @@
#ifndef _TMWSERV_COMMON_INVENTORYDATA_HPP_
#define _TMWSERV_COMMON_INVENTORYDATA_HPP_
+#include <vector>
+
/**
* Numbers of inventory slots
*/
diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp
index 4d04c22..3e5d919 100644
--- a/src/game-server/character.cpp
+++ b/src/game-server/character.cpp
@@ -24,6 +24,8 @@
#include "defines.h"
#include "game-server/character.hpp"
+#include "game-server/mapcomposite.hpp"
+#include "game-server/mapmanager.hpp"
#include "net/messagein.hpp"
#include "net/messageout.hpp"
#include "serialize/characterdata.hpp"
@@ -110,3 +112,13 @@ Character::writeAttributeUpdateMessage(MessageOut &msg)
mAttributesChanged = false;
}
+
+int Character::getMapId() const
+{
+ return getMap()->getID();
+}
+
+void Character::setMapId(int id)
+{
+ setMap(mapManager->getMap(id));
+}
diff --git a/src/game-server/character.hpp b/src/game-server/character.hpp
index 275b5e6..15c72a3 100644
--- a/src/game-server/character.hpp
+++ b/src/game-server/character.hpp
@@ -26,8 +26,8 @@
#include <string>
#include <vector>
-#include "game-server/being.hpp"
#include "common/inventorydata.hpp"
+#include "game-server/being.hpp"
class GameClient;
class MessageIn;
@@ -154,21 +154,15 @@ class Character : public Being
/**
* Gets the value of an attribute of the character.
- * Inherited from Being, explicitly defined because
- * of double inheritance.
*/
- unsigned short
- getBaseAttribute(int attributeNumber) const
- { return Being::getAttribute(attributeNumber); }
+ int getBaseAttribute(int attributeNumber) const
+ { return getAttribute(attributeNumber); }
/**
* Sets the value of an attribute of the character.
- * Inherited from Being, explicitly defined because
- * of double inheritance.
*/
- void
- setBaseAttribute(int attributeNumber, int value)
- { Being::setAttribute(attributeNumber, value); }
+ void setBaseAttribute(int attributeNumber, int value)
+ { setAttribute(attributeNumber, value); }
/**
* Creates a message that informs the client about the attribute
@@ -178,70 +172,16 @@ class Character : public Being
writeAttributeUpdateMessage(MessageOut &msg);
/**
- * Gets the Id of the map that the character is on.
- * Inherited from Thing through Being, explicitly defined because
- * of double inheritance.
- */
- int
- getMapId() const
- { return Being::getMapId(); }
-
- /**
- * Sets the Id of the map that the character is on.
- * Inherited from Thing through Being, explicitly defined because
- * of double inheritance.
- */
- void
- setMapId(int mapId)
- { Being::setMapId(mapId); }
-
- /**
- * Gets the position of the character on the map.
- * Inherited from Object through Being, explicitly defined because
- * of double inheritance.
- */
- Point const &
- getPosition() const
- { return Being::getPosition(); }
-
- /**
- * Sets the position of the character on the map.
- * Inherited from Object through Being, explicitly defined because
- * of double inheritance.
+ * Gets the ID of the map that the character is on.
+ * For serialization purpose only.
*/
- void
- setPosition(const Point &p)
- { Being::setPosition(p); }
+ int getMapId() const;
/**
- * The access functions for inventory
- *
- * Currently not implemented
+ * Sets the ID of the map that the character is on.
+ * For serialization purpose only.
*/
-
- /**
- * Returns the number of inventory items.
- * (items don't have to be unique)
- * TODO: maybe renaming to NumberOfFilledSlots would be better.
- */
- int
- getNumberOfInventoryItems() const;
-
- /**
- * Returns a reference to the item in inventory at slot.
- * TODO: Keep this consistent with whatever is chosen for
- * getNumberOfInventoryItems.
- */
- InventoryItem const &
- getInventoryItem(unsigned short slot) const;
-
- /** Clears the inventory, in preperation for an update. */
- void
- clearInventory();
-
- /** Adds an inventory item to the inventory. */
- void
- addItemToInventory(const InventoryItem& item);
+ void setMapId(int);
protected:
/**
diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp
index 1ca4f75..8c33bf1 100644
--- a/src/game-server/gamehandler.cpp
+++ b/src/game-server/gamehandler.cpp
@@ -150,8 +150,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message)
// TODO: use a less arbitrary value.
if (std::abs(x - ppos.x) + std::abs(y - ppos.y) < 48)
{
- int mapId = computer.character->getMapId();
- MapComposite *map = gameState->getMap(mapId);
+ MapComposite *map = computer.character->getMap();
Point ipos(x, y);
for (FixedObjectIterator i(map->getAroundPointIterator(ipos, 0)); i; ++i)
{
@@ -179,7 +178,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message)
{
int nb = inv.removeFromSlot(slot, amount);
Item *item = new Item(ic, amount - nb);
- item->setMapId(computer.character->getMapId());
+ item->setMap(computer.character->getMap());
item->setPosition(computer.character->getPosition());
gameState->insert(item);
}
diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp
index 027db92..627582f 100644
--- a/src/game-server/mapcomposite.cpp
+++ b/src/game-server/mapcomposite.cpp
@@ -45,6 +45,104 @@
in dealing with zone changes. */
static int const zoneDiam = 256;
+/**
+ * Part of a map.
+ */
+struct MapZone
+{
+ unsigned short nbCharacters, nbMovingObjects;
+ /**
+ * Objects present in this zone.
+ * Characters are stored first, then the remaining MovingObjects, then the
+ * remaining Objects.
+ */
+ std::vector< Object * > objects;
+
+ /**
+ * Destinations of the objects that left this zone.
+ * This is necessary in order to have an accurate iterator around moving
+ * objects.
+ */
+ MapRegion destinations;
+
+ MapZone(): nbCharacters(0), nbMovingObjects(0) {}
+ void insert(Object *);
+ void remove(Object *);
+};
+
+/**
+ * Pool of public IDs for MovingObjects on a map. By maintaining public ID
+ * availability using bits, it can locate an available public ID fast while
+ * using minimal memory access.
+ */
+struct ObjectBucket
+{
+ static int const int_bitsize = sizeof(unsigned) * 8;
+ unsigned bitmap[256 / int_bitsize]; /**< Bitmap of free locations. */
+ short free; /**< Number of empty places. */
+ short next_object; /**< Next object to look at. */
+ MovingObject *objects[256];
+
+ ObjectBucket();
+ int allocate();
+ void deallocate(int);
+};
+
+/**
+ * Entities on a map.
+ */
+struct MapContent
+{
+ MapContent(Map *);
+ ~MapContent();
+
+ /**
+ * Allocates a unique ID for a moving object on this map.
+ */
+ bool allocate(MovingObject *);
+
+ /**
+ * Deallocates an ID.
+ */
+ void deallocate(MovingObject *);
+
+ /**
+ * Fills a region of zones within the range of a point.
+ */
+ void fillRegion(MapRegion &, Point const &, int) const;
+
+ /**
+ * Fills a region of zones inside a rectangle.
+ */
+ void fillRegion(MapRegion &, Rectangle const &) const;
+
+ /**
+ * Gets zone at given position.
+ */
+ MapZone &getZone(Point const &pos) const
+ { return zones[(pos.x / zoneDiam) + (pos.y / zoneDiam) * mapWidth]; }
+
+ /**
+ * Things (items, characters, monsters, etc) located on the map.
+ */
+ std::vector< Thing * > things;
+
+ /**
+ * Buckets of MovingObjects located on the map, referenced by ID.
+ */
+ ObjectBucket *buckets[256];
+
+ int last_bucket; /**< Last bucket acted upon. */
+
+ /**
+ * Partition of the Objects, depending on their position on the map.
+ */
+ MapZone *zones;
+
+ unsigned short mapWidth; /**< Width with respect to zones. */
+ unsigned short mapHeight; /**< Height with respect to zones. */
+};
+
void MapZone::insert(Object *obj)
{
int type = obj->getType();
@@ -141,7 +239,7 @@ static void addZone(MapRegion &r, unsigned z)
}
}
-ZoneIterator::ZoneIterator(MapRegion const &r, MapComposite const *m)
+ZoneIterator::ZoneIterator(MapRegion const &r, MapContent const *m)
: region(r), pos(0), map(m)
{
current = &map->zones[r.empty() ? 0 : r[0]];
@@ -325,8 +423,8 @@ void ObjectBucket::deallocate(int i)
++free;
}
-MapComposite::MapComposite(Map *m)
- : map(m), last_bucket(0)
+MapContent::MapContent(Map *map)
+ : last_bucket(0), zones(NULL)
{
buckets[0] = new ObjectBucket;
for (int i = 1; i < 256; ++i)
@@ -338,17 +436,29 @@ MapComposite::MapComposite(Map *m)
zones = new MapZone[mapWidth * mapHeight];
}
-MapComposite::~MapComposite()
+MapContent::~MapContent()
{
for (int i = 0; i < 256; ++i)
{
delete buckets[i];
}
delete[] zones;
- // MapManger will delete the maps when necessary.
}
-bool MapComposite::allocate(MovingObject *obj)
+void MapComposite::setMap(Map *m)
+{
+ assert(!mMap && m);
+ mMap = m;
+ mContent = new MapContent(m);
+}
+
+MapComposite::~MapComposite()
+{
+ delete mMap;
+ delete mContent;
+}
+
+bool MapContent::allocate(MovingObject *obj)
{
// First, try allocating from the last used bucket.
ObjectBucket *b = buckets[last_bucket];
@@ -387,13 +497,13 @@ bool MapComposite::allocate(MovingObject *obj)
return false;
}
-void MapComposite::deallocate(MovingObject *obj)
+void MapContent::deallocate(MovingObject *obj)
{
unsigned short id = obj->getPublicID();
buckets[id / 256]->deallocate(id % 256);
}
-void MapComposite::fillRegion(MapRegion &r, Point const &p, int radius) const
+void MapContent::fillRegion(MapRegion &r, Point const &p, int radius) const
{
int ax = p.x > radius ? (p.x - radius) / zoneDiam : 0,
ay = p.y > radius ? (p.y - radius) / zoneDiam : 0,
@@ -408,7 +518,7 @@ void MapComposite::fillRegion(MapRegion &r, Point const &p, int radius) const
}
}
-void MapComposite::fillRegion(MapRegion &r, Rectangle const &p) const
+void MapContent::fillRegion(MapRegion &r, Rectangle const &p) const
{
int ax = p.x / zoneDiam,
ay = p.y / zoneDiam,
@@ -426,28 +536,28 @@ void MapComposite::fillRegion(MapRegion &r, Rectangle const &p) const
ZoneIterator MapComposite::getAroundPointIterator(Point const &p, int radius) const
{
MapRegion r;
- fillRegion(r, p, radius);
- return ZoneIterator(r, this);
+ mContent->fillRegion(r, p, radius);
+ return ZoneIterator(r, mContent);
}
ZoneIterator MapComposite::getAroundObjectIterator(Object *obj, int radius) const
{
MapRegion r;
- fillRegion(r, obj->getPosition(), radius);
- return ZoneIterator(r, this);
+ mContent->fillRegion(r, obj->getPosition(), radius);
+ return ZoneIterator(r, mContent);
}
ZoneIterator MapComposite::getInsideRectangleIterator(Rectangle const &p) const
{
MapRegion r;
- fillRegion(r, p);
- return ZoneIterator(r, this);
+ mContent->fillRegion(r, p);
+ return ZoneIterator(r, mContent);
}
ZoneIterator MapComposite::getAroundCharacterIterator(MovingObject *obj, int radius) const
{
MapRegion r1;
- fillRegion(r1, obj->getOldPosition(), radius);
+ mContent->fillRegion(r1, obj->getOldPosition(), radius);
MapRegion r2 = r1;
for (MapRegion::iterator i = r1.begin(), i_end = r1.end(); i != i_end; ++i)
{
@@ -455,7 +565,7 @@ ZoneIterator MapComposite::getAroundCharacterIterator(MovingObject *obj, int rad
This is necessary to detect two moving objects changing zones at the
same time and at the border, and going in opposite directions (or
more simply to detect teleportations, if any). */
- MapRegion &r4 = zones[*i].destinations;
+ MapRegion &r4 = mContent->zones[*i].destinations;
if (!r4.empty())
{
MapRegion r3;
@@ -465,26 +575,25 @@ ZoneIterator MapComposite::getAroundCharacterIterator(MovingObject *obj, int rad
r2.swap(r3);
}
}
- fillRegion(r2, obj->getPosition(), radius);
- return ZoneIterator(r2, this);
+ mContent->fillRegion(r2, obj->getPosition(), radius);
+ return ZoneIterator(r2, mContent);
}
bool MapComposite::insert(Thing *ptr)
{
if (ptr->isVisible())
{
- if (ptr->canMove() && !allocate(static_cast< MovingObject * >(ptr)))
+ if (ptr->canMove() && !mContent->allocate(static_cast< MovingObject * >(ptr)))
{
return false;
}
Object *obj = static_cast< Object * >(ptr);
- Point const &pos = obj->getPosition();
- zones[(pos.x / zoneDiam) + (pos.y / zoneDiam) * mapWidth].insert(obj);
+ mContent->getZone(obj->getPosition()).insert(obj);
}
ptr->setMap(this);
- things.push_back(ptr);
+ mContent->things.push_back(ptr);
return true;
}
@@ -493,22 +602,21 @@ void MapComposite::remove(Thing *ptr)
if (ptr->isVisible())
{
Object *obj = static_cast< Object * >(ptr);
- Point const &pos = obj->getPosition();
- zones[(pos.x / zoneDiam) + (pos.y / zoneDiam) * mapWidth].remove(obj);
+ mContent->getZone(obj->getPosition()).remove(obj);
if (ptr->canMove())
{
- deallocate(static_cast< MovingObject * >(ptr));
+ mContent->deallocate(static_cast< MovingObject * >(ptr));
}
}
- for (std::vector< Thing * >::iterator i = things.begin(),
- i_end = things.end(); i != i_end; ++i)
+ for (std::vector< Thing * >::iterator i = mContent->things.begin(),
+ i_end = mContent->things.end(); i != i_end; ++i)
{
if (*i == ptr)
{
*i = *(i_end - 1);
- things.pop_back();
+ mContent->things.pop_back();
return;
}
}
@@ -517,16 +625,16 @@ void MapComposite::remove(Thing *ptr)
void MapComposite::update()
{
- map->resetTempWalk();
+ mMap->resetTempWalk();
- for (int i = 0; i < mapHeight * mapWidth; ++i)
+ for (int i = 0; i < mContent->mapHeight * mContent->mapWidth; ++i)
{
- zones[i].destinations.clear();
+ mContent->zones[i].destinations.clear();
}
// Cannot use a WholeMap iterator as objects will change zones under its feet.
- for (std::vector< Thing * >::iterator i = things.begin(),
- i_end = things.end(); i != i_end; ++i)
+ for (std::vector< Thing * >::iterator i = mContent->things.begin(),
+ i_end = mContent->things.end(); i != i_end; ++i)
{
if (!(*i)->canMove())
{
@@ -537,15 +645,22 @@ void MapComposite::update()
Point const &pos1 = obj->getOldPosition(),
&pos2 = obj->getPosition();
- map->setTempWalk(pos2.x / 32, pos2.y / 32, false);
+ mMap->setTempWalk(pos2.x / 32, pos2.y / 32, false);
- int src = (pos1.x / zoneDiam) + (pos1.y / zoneDiam) * mapWidth,
- dst = (pos2.x / zoneDiam) + (pos2.y / zoneDiam) * mapWidth;
- if (src != dst)
+ MapZone &src = mContent->getZone(pos1),
+ &dst = mContent->getZone(pos2);
+ if (&src != &dst)
{
- addZone(zones[src].destinations, dst);
- zones[src].remove(obj);
- zones[dst].insert(obj);
+ addZone(src.destinations, &dst - mContent->zones);
+ src.remove(obj);
+ dst.insert(obj);
}
}
}
+
+std::vector< Thing * > const &MapComposite::getEverything() const
+{
+ return mContent->things;
+}
+
+
diff --git a/src/game-server/mapcomposite.hpp b/src/game-server/mapcomposite.hpp
index 0fd9dbd..880f4c1 100644
--- a/src/game-server/mapcomposite.hpp
+++ b/src/game-server/mapcomposite.hpp
@@ -24,6 +24,7 @@
#ifndef _TMW_SERVER_MAPCOMPOSITE_
#define _TMW_SERVER_MAPCOMPOSITE_
+#include <string>
#include <vector>
class Map;
@@ -35,37 +36,15 @@ class Point;
class Rectangle;
class Thing;
+struct MapContent;
+struct MapZone;
+
/**
* Ordered sets of zones of a map.
*/
typedef std::vector< unsigned > MapRegion;
/**
- * Part of the map.
- */
-struct MapZone
-{
- unsigned short nbCharacters, nbMovingObjects;
- /**
- * Objects present in this zone.
- * Characters are stored first, then the remaining MovingObjects, then the
- * remaining Objects.
- */
- std::vector< Object * > objects;
-
- /**
- * Destinations of the objects that left this zone.
- * This is necessary in order to have an accurate iterator around moving
- * objects.
- */
- MapRegion destinations;
-
- MapZone(): nbCharacters(0), nbMovingObjects(0) {}
- void insert(Object *);
- void remove(Object *);
-};
-
-/**
* Iterates through the zones of a region of the map.
*/
struct ZoneIterator
@@ -73,9 +52,9 @@ struct ZoneIterator
MapRegion region; /**< Zones to visit. Empty means the entire map. */
unsigned pos;
MapZone *current;
- MapComposite const *map;
+ MapContent const *map;
- ZoneIterator(MapRegion const &, MapComposite const *);
+ ZoneIterator(MapRegion const &, MapContent const *);
void operator++();
MapZone *operator*() const { return current; }
operator bool() const { return current; }
@@ -142,24 +121,6 @@ struct ObjectIterator
};
/**
- * Pool of public IDs for MovingObjects on a map. By maintaining public ID
- * availability using bits, it can locate an available public ID fast while
- * using minimal memory access.
- */
-struct ObjectBucket
-{
- static int const int_bitsize = sizeof(unsigned) * 8;
- unsigned bitmap[256 / int_bitsize]; /**< Bitmap of free locations. */
- short free; /**< Number of empty places. */
- short next_object; /**< Next object to look at. */
- MovingObject *objects[256];
-
- ObjectBucket();
- int allocate();
- void deallocate(int);
-};
-
-/**
* Combined map/entity structure.
*/
class MapComposite
@@ -168,15 +129,43 @@ class MapComposite
/**
* Constructor.
*/
- MapComposite(Map *);
+ MapComposite(int id, std::string const &name)
+ : mMap(NULL), mContent(NULL), mName(name), mID(id) {}
/**
* Destructor.
*/
~MapComposite();
+ /**
+ * Sets the underlying pathfinding map.
+ * Can be done only once.
+ */
+ void setMap(Map *);
+
+ /**
+ * Gets the underlying pathfinding map.
+ */
Map *getMap() const
- { return map; }
+ { return mMap; }
+
+ /**
+ * Returns whether the map is active on this server or not.
+ */
+ bool isActive() const
+ { return mMap; }
+
+ /**
+ * Gets the game ID of this map.
+ */
+ int getID() const
+ { return mID; }
+
+ /**
+ * Gets the name of this map.
+ */
+ std::string const &getName() const
+ { return mName; }
/**
* Inserts an object on the map. Sets its public ID if relevant.
@@ -202,7 +191,7 @@ class MapComposite
* Gets an iterator on the objects of the whole map.
*/
ZoneIterator getWholeMapIterator() const
- { return ZoneIterator(MapRegion(), this); }
+ { return ZoneIterator(MapRegion(), mContent); }
/**
* Gets an iterator on the objects inside a given rectangle.
@@ -228,55 +217,15 @@ class MapComposite
/**
* Gets everything related to the map.
*/
- std::vector< Thing * > const &getEverything() const
- { return things; }
+ std::vector< Thing * > const &getEverything() const;
private:
MapComposite(MapComposite const &);
- /**
- * Allocates a unique ID for a moving object on this map.
- */
- bool allocate(MovingObject *);
-
- /**
- * Deallocates an ID.
- */
- void deallocate(MovingObject *);
-
- /**
- * Fills a region of zones within the range of a point.
- */
- void fillRegion(MapRegion &, Point const &, int) const;
-
- /**
- * Fills a region of zones inside a rectangle.
- */
- void fillRegion(MapRegion &, Rectangle const &) const;
-
- Map *map; /**< Actual map. */
-
- /**
- * Things (items, characters, monsters, etc) located on the map.
- */
- std::vector< Thing * > things;
-
- /**
- * Buckets of MovingObjects located on the map, referenced by ID.
- */
- ObjectBucket *buckets[256];
-
- int last_bucket; /**< Last bucket acted upon. */
-
- /**
- * Partition of the Objects, depending on their position on the map.
- */
- MapZone *zones;
-
- unsigned short mapWidth; /**< Width with respect to zones. */
- unsigned short mapHeight; /**< Height with respect to zones. */
-
- friend class ZoneIterator;
+ Map *mMap; /**< Actual map. */
+ MapContent *mContent; /**< Entities on the map. */
+ std::string mName; /**< Name of the map. */
+ unsigned short mID; /**< ID of the map. */
};
#endif
diff --git a/src/game-server/mapmanager.cpp b/src/game-server/mapmanager.cpp
index dac57ca..7de489c 100644
--- a/src/game-server/mapmanager.cpp
+++ b/src/game-server/mapmanager.cpp
@@ -25,6 +25,7 @@
#include "resourcemanager.h"
#include "game-server/map.hpp"
+#include "game-server/mapcomposite.hpp"
#include "game-server/mapmanager.hpp"
#include "game-server/mapreader.hpp"
#include "utils/logger.h"
@@ -72,8 +73,7 @@ MapManager::MapManager(std::string const &mapReferenceFile)
std::string name = XML::getProperty(node, "name", std::string());
if (id != 0 && !name.empty())
{
- LoadedMap m = { false, name, NULL };
- maps[id] = m;
+ maps[id] = new MapComposite(id, name);
}
}
@@ -84,54 +84,40 @@ MapManager::~MapManager()
{
for (Maps::iterator i = maps.begin(), i_end = maps.end(); i != i_end; ++i)
{
- delete i->second.map;
+ delete i->second;
}
}
-Map* MapManager::getMap(int mapId)
+MapComposite *MapManager::getMap(int mapId)
{
Maps::iterator i = maps.find(mapId);
- assert(i != maps.end() && i->second.isActive);
- Map *&map = i->second.map;
- if (!map)
- {
- std::string const &file = i->second.fileName;
- map = MapReader::readMap("maps/" + file);
- if (!map)
- {
- LOG_ERROR("Unable to load map \"" << file << "\" (id "
- << mapId << ")");
- return NULL;
- }
- LOG_INFO("Loaded map \"" << file << "\" (id " << mapId << ")");
- }
- return map;
-}
-
-std::string MapManager::getMapName(int mapId) const
-{
- Maps::const_iterator i = maps.find(mapId);
assert(i != maps.end());
- return i->second.fileName;
+ return i->second;
}
void MapManager::raiseActive(int mapId)
{
Maps::iterator i = maps.find(mapId);
assert(i != maps.end());
- i->second.isActive = true;
- LOG_INFO("Activating map \"" << i->second.fileName << "\" (id "
- << i->first << ")");
-}
+ MapComposite *composite = i->second;
+ if (composite->isActive())
+ {
+ return;
+ }
-bool MapManager::isActive(int mapId) const
-{
- Maps::const_iterator i = maps.find(mapId);
- assert(i != maps.end());
- return i->second.isActive;
-}
+ std::string const &file = composite->getName();
+ Map *map = MapReader::readMap("maps/" + file);
+ if (!map)
+ {
+ LOG_ERROR("Unable to load map \"" << file << "\" (id "
+ << mapId << ")");
+ return;
+ }
-short MapManager::numberOfMaps() const
-{
- return maps.size();
+ composite->setMap(map);
+ LOG_INFO("Activated map \"" << file << "\" (id " << mapId << ")");
+ // will need to load extra map related resources here also
+ extern void testingMap(MapComposite *);
+ testingMap(composite);
}
+
diff --git a/src/game-server/mapmanager.hpp b/src/game-server/mapmanager.hpp
index 8693dbf..63ef32c 100644
--- a/src/game-server/mapmanager.hpp
+++ b/src/game-server/mapmanager.hpp
@@ -27,14 +27,7 @@
#include <map>
#include <string>
-class Map;
-
-struct LoadedMap
-{
- bool isActive;
- std::string fileName;
- Map *map;
-};
+class MapComposite;
/**
* MapManager loads/unloads maps
@@ -42,7 +35,7 @@ struct LoadedMap
class MapManager
{
public:
- typedef std::map< unsigned short, LoadedMap > Maps;
+ typedef std::map< int, MapComposite * > Maps;
/**
* Constructor (loads map reference file).
@@ -52,12 +45,7 @@ class MapManager
/**
* Returns the requested map.
*/
- Map *getMap(int);
-
- /**
- * Returns the requested map name.
- */
- std::string getMapName(int) const;
+ MapComposite *getMap(int);
/**
* Returns all the maps.
@@ -70,16 +58,6 @@ class MapManager
void raiseActive(int);
/**
- * Gets the activity status of the map.
- */
- bool isActive(int) const;
-
- /**
- * Gets the number of maps
- */
- short numberOfMaps() const;
-
- /**
* Destructor.
*/
~MapManager();
diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp
index 4881a9b..be9d6fa 100644
--- a/src/game-server/monster.cpp
+++ b/src/game-server/monster.cpp
@@ -92,7 +92,7 @@ void Monster::update()
Direction bestAttackDirection = DIRECTION_DOWN;
// iterate through objects nearby
- for (MovingObjectIterator i(mMap->getAroundCharacterIterator(this, AROUND_AREA)); i; ++i)
+ for (MovingObjectIterator i(getMap()->getAroundCharacterIterator(this, AROUND_AREA)); i; ++i)
{
// we only want to attack player characters
if ((*i)->getType() != OBJECT_CHARACTER) continue;
@@ -185,11 +185,9 @@ int Monster::calculatePositionPriority(Point position, int targetPriority)
}
std::list<PATH_NODE> path;
- path = mMap->getMap()->findPath(thisPos.x / 32,
- thisPos.y / 32,
- position.x / 32,
- position.y / 32,
- mAgressionRange);
+ path = getMap()->getMap()->findPath(thisPos.x / 32, thisPos.y / 32,
+ position.x / 32, position.y / 32,
+ mAgressionRange);
if (path.empty() || path.size() >= mAgressionRange)
{
diff --git a/src/game-server/movingobject.cpp b/src/game-server/movingobject.cpp
index 7eb040e..2375c90 100644
--- a/src/game-server/movingobject.cpp
+++ b/src/game-server/movingobject.cpp
@@ -21,7 +21,7 @@
*/
#include "game-server/map.hpp"
-#include "game-server/mapmanager.hpp"
+#include "game-server/mapcomposite.hpp"
#include "game-server/movingobject.hpp"
void MovingObject::move()
@@ -44,7 +44,7 @@ void MovingObject::move()
return;
}
- Map *map = mapManager->getMap(getMapId());
+ Map *map = getMap()->getMap();
/* If no path exists, the for-loop won't be entered. Else a path for the
* current destination has already been calculated.
diff --git a/src/game-server/spawnarea.cpp b/src/game-server/spawnarea.cpp
index 780e3e6..5e2c041 100644
--- a/src/game-server/spawnarea.cpp
+++ b/src/game-server/spawnarea.cpp
@@ -33,8 +33,8 @@
* TODO: Allow specifying being type and use it.
*/
-SpawnArea::SpawnArea(int mapId, const Rectangle &zone):
- Thing(OBJECT_OTHER),
+SpawnArea::SpawnArea(MapComposite *map, const Rectangle &zone):
+ Thing(OBJECT_OTHER, map),
mZone(zone),
mMaxBeings(10),
mBeingType(1),
@@ -42,7 +42,6 @@ SpawnArea::SpawnArea(int mapId, const Rectangle &zone):
mNumBeings(0),
mNextSpawn(0)
{
- setMapId(mapId);
}
void
@@ -57,15 +56,16 @@ SpawnArea::update()
//find a free spawn location. Give up after 10 tries
int c = 10;
Point position;
+ MapComposite *map = getMap();
+ Map *realMap = map->getMap();
do
{
position = Point(mZone.x + rand() % mZone.w,
mZone.y + rand() % mZone.h);
c--;
- } while (! mMap->getMap()->getWalk(position.x / 32, position.y / 32)
- && c);
+ } while (!realMap->getWalk(position.x / 32, position.y / 32) && c);
- if (c >= 0)
+ if (c)
{
Being *being = new Monster();
being->addDeathListener(this);
@@ -76,14 +76,15 @@ SpawnArea::update()
being->setAttribute(BASE_ATTR_VITALITY, 10);
being->fillHitpoints();
- being->setMapId(1);
+ being->setMap(map);
being->setPosition(position);
DelayedEvent e = { EVENT_INSERT };
gameState->enqueueEvent(being, e);
mNumBeings++;
}
- else {
+ else
+ {
//TODO: This log message should have more information when
// more flexibility is added to the spawn area
LOG_WARN("Unable to find a free spawn location for monster");
diff --git a/src/game-server/spawnarea.hpp b/src/game-server/spawnarea.hpp
index 2f8ff51..91aec14 100644
--- a/src/game-server/spawnarea.hpp
+++ b/src/game-server/spawnarea.hpp
@@ -37,7 +37,7 @@ class Being;
class SpawnArea : public Thing, public DeathListener
{
public:
- SpawnArea(int mapId, const Rectangle &zone);
+ SpawnArea(MapComposite *, const Rectangle &zone);
virtual ~SpawnArea() {}
diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp
index 645906c..3903d93 100644
--- a/src/game-server/state.cpp
+++ b/src/game-server/state.cpp
@@ -35,14 +35,6 @@
#include "net/messageout.hpp"
#include "utils/logger.h"
-State::~State()
-{
- for (Maps::iterator i = maps.begin(), i_end = maps.end(); i != i_end; ++i)
- {
- delete i->second;
- }
-}
-
void State::updateMap(MapComposite *map)
{
// 1. update object status.
@@ -288,9 +280,15 @@ void State::update()
# endif
// Update game state (update AI, etc.)
- for (Maps::iterator m = maps.begin(), m_end = maps.end(); m != m_end; ++m)
+ MapManager::Maps const &maps = mapManager->getMaps();
+ for (MapManager::Maps::const_iterator m = maps.begin(), m_end = maps.end(); m != m_end; ++m)
{
MapComposite *map = m->second;
+ if (!map->isActive())
+ {
+ continue;
+ }
+
updateMap(map);
for (CharacterIterator p(map->getWholeMapIterator()); p; ++p)
@@ -340,7 +338,7 @@ void State::update()
{
remove(o);
Point pos(e.x, e.y);
- o->setMapId(e.map);
+ o->setMap(e.map);
o->setPosition(pos);
assert(o->getType() == OBJECT_CHARACTER);
@@ -348,11 +346,9 @@ void State::update()
/* Force update of persistent data on map change, so that
characters can respawn at the start of the map after a death or
a disconnection. */
- p->setMapId(e.map);
- p->setPosition(pos);
accountHandler->sendCharacterData(p);
- if (mapManager->isActive(e.map))
+ if (e.map->getMap())
{
insert(o);
}
@@ -372,8 +368,7 @@ void State::update()
void State::insert(Thing *ptr)
{
assert(!dbgLockObjects);
- int mapId = ptr->getMapId();
- MapComposite *map = loadMap(mapId);
+ MapComposite *map = ptr->getMap();
if (!map || !map->insert(ptr))
{
// TODO: Deal with failure to place Thing on the map.
@@ -389,7 +384,7 @@ void State::insert(Thing *ptr)
/* Since the character doesn't know yet where on the world he is after
connecting to the map server, we send him an initial change map message. */
MessageOut mapChangeMessage(GPMSG_PLAYER_MAP_CHANGE);
- mapChangeMessage.writeString(mapManager->getMapName(mapId));
+ mapChangeMessage.writeString(map->getName());
Point pos = obj->getPosition();
mapChangeMessage.writeShort(pos.x);
mapChangeMessage.writeShort(pos.y);
@@ -400,10 +395,7 @@ void State::insert(Thing *ptr)
void State::remove(Thing *ptr)
{
assert(!dbgLockObjects);
- int mapId = ptr->getMapId();
- Maps::iterator m = maps.find(mapId);
- assert(m != maps.end());
- MapComposite *map = m->second;
+ MapComposite *map = ptr->getMap();
if (ptr->canMove())
{
@@ -452,29 +444,6 @@ void State::enqueueEvent(Object *ptr, DelayedEvent const &e)
}
}
-MapComposite *State::getMap(int map)
-{
- Maps::iterator m = maps.find(map);
- assert(m != maps.end());
- return m->second;
-}
-
-MapComposite *State::loadMap(int mapId)
-{
- Maps::iterator m = maps.find(mapId);
- if (m != maps.end()) return m->second;
- Map *map = mapManager->getMap(mapId);
- assert(map);
- MapComposite *tmp = new MapComposite(map);
- maps[mapId] = tmp;
-
- // will need to load extra map related resources here also
- extern void testingMap(int);
- testingMap(mapId);
-
- return tmp;
-}
-
void State::sayAround(Object *obj, std::string text)
{
MessageOut msg(GPMSG_SAY);
@@ -482,10 +451,9 @@ void State::sayAround(Object *obj, std::string text)
static_cast< MovingObject * >(obj)->getPublicID());
msg.writeString(text);
- MapComposite *map = getMap(obj->getMapId());
Point speakerPosition = obj->getPosition();
- for (CharacterIterator i(map->getAroundObjectIterator(obj, AROUND_AREA)); i; ++i)
+ for (CharacterIterator i(obj->getMap()->getAroundObjectIterator(obj, AROUND_AREA)); i; ++i)
{
if (speakerPosition.inRangeOf((*i)->getPosition(), AROUND_AREA))
{
diff --git a/src/game-server/state.hpp b/src/game-server/state.hpp
index adbeb99..0d03743 100644
--- a/src/game-server/state.hpp
+++ b/src/game-server/state.hpp
@@ -41,7 +41,8 @@ enum
struct DelayedEvent
{
- unsigned short type, map, x, y;
+ unsigned short type, x, y;
+ MapComposite *map;
};
/**
@@ -50,15 +51,9 @@ struct DelayedEvent
*/
class State
{
- typedef std::map< int, MapComposite * > Maps;
typedef std::map< Object *, DelayedEvent > DelayedEvents;
/**
- * List of maps.
- */
- Maps maps;
-
- /**
* List of delayed events.
*/
DelayedEvents delayedEvents;
@@ -73,13 +68,7 @@ class State
*/
void informPlayer(MapComposite *, Character *);
- /**
- * Loads map into game world.
- */
- MapComposite *loadMap(int mapId);
-
public:
- ~State();
/**
* Updates game state (contains core server logic).
@@ -87,11 +76,6 @@ class State
void update();
/**
- * Gets a composite map.
- */
- MapComposite *getMap(int map);
-
- /**
* Inserts an object on the map.
*/
void insert(Thing *);
diff --git a/src/game-server/testing.cpp b/src/game-server/testing.cpp
index cd2f08f..495b361 100644
--- a/src/game-server/testing.cpp
+++ b/src/game-server/testing.cpp
@@ -6,48 +6,50 @@
#include "defines.h"
#include "game-server/itemmanager.hpp"
+#include "game-server/mapcomposite.hpp"
+#include "game-server/mapmanager.hpp"
#include "game-server/spawnarea.hpp"
#include "game-server/state.hpp"
#include "game-server/trigger.hpp"
-static Rectangle rectA = { 56 * 32, 12 * 32, 5 * 32, 32 };
-static WarpAction warpA(3, 44 * 32 + 16, 80 * 32 + 16);
-static Rectangle rectB = { 42 * 32, 88 * 32, 5 * 32, 32 };
-static WarpAction warpB(1, 58 * 32 + 16, 17 * 32 + 16);
-
-static void dropItem(int map, int x, int y, int type)
+static void dropItem(MapComposite *map, int x, int y, int type)
{
ItemClass *ic = itemManager->getItem(type);
assert(ic);
Item *i = new Item(ic, 1);
- i->setMapId(map);
+ i->setMap(map);
Point pos(x, y);
i->setPosition(pos);
gameState->insert(i);
}
-void testingMap(int id)
+void testingMap(MapComposite *map)
{
- switch (id)
+ static Rectangle rectA = { 56 * 32, 12 * 32, 5 * 32, 32 };
+ static WarpAction warpA(mapManager->getMap(3), 44 * 32 + 16, 80 * 32 + 16);
+ static Rectangle rectB = { 42 * 32, 88 * 32, 5 * 32, 32 };
+ static WarpAction warpB(mapManager->getMap(1), 58 * 32 + 16, 17 * 32 + 16);
+
+ switch (map->getID())
{
case 1:
{
// Create maggot spawn area
Rectangle maggotSpawnRect = { 720, 900, 320, 320 };
- gameState->insert(new SpawnArea(1, maggotSpawnRect));
+ gameState->insert(new SpawnArea(map, maggotSpawnRect));
// Portal to map 3
- gameState->insert(new TriggerArea(1, rectA, &warpA));
+ gameState->insert(new TriggerArea(map, rectA, &warpA));
// Drop some items
- dropItem(1, 58 * 32 + 16, 20 * 32 + 16, 508);
- dropItem(1, 58 * 32 + 16, 21 * 32 + 16, 524);
+ dropItem(map, 58 * 32 + 16, 20 * 32 + 16, 508);
+ dropItem(map, 58 * 32 + 16, 21 * 32 + 16, 524);
} break;
case 3:
{
// Portal to map 1
- gameState->insert(new TriggerArea(3, rectB, &warpB));
+ gameState->insert(new TriggerArea(map, rectB, &warpB));
} break;
}
}
diff --git a/src/game-server/thing.hpp b/src/game-server/thing.hpp
index 1d95c23..1fdf584 100644
--- a/src/game-server/thing.hpp
+++ b/src/game-server/thing.hpp
@@ -51,8 +51,8 @@ class Thing
/**
* Constructor.
*/
- Thing(int type)
- : mMap(NULL),
+ Thing(int type, MapComposite *map = NULL)
+ : mMap(map),
mType(type)
{}
@@ -96,17 +96,9 @@ class Thing
/**
* Gets the map this thing is located on.
- *
- * @return ID of map.
- */
- int getMapId() const
- { return mMapId; }
-
- /**
- * Sets the map ID this thing is located on.
*/
- void setMapId(int mapId)
- { mMapId = mapId; }
+ MapComposite *getMap() const
+ { return mMap; }
/**
* Sets the map this thing is located on.
@@ -114,11 +106,8 @@ class Thing
void setMap(MapComposite *map)
{ mMap = map; }
- protected:
- MapComposite *mMap; /**< Map the thing is on */
-
private:
- unsigned short mMapId; /**< ID of the map this thing is on. */
+ MapComposite *mMap; /**< Map the thing is on */
char mType; /**< Type of this thing. */
};
diff --git a/src/game-server/trigger.cpp b/src/game-server/trigger.cpp
index d658c7a..b1cdc7c 100644
--- a/src/game-server/trigger.cpp
+++ b/src/game-server/trigger.cpp
@@ -32,22 +32,14 @@ void WarpAction::process(Object *obj)
{
if (obj->getType() == OBJECT_CHARACTER)
{
- DelayedEvent e = { EVENT_WARP, mMap, mX, mY };
+ DelayedEvent e = { EVENT_WARP, mX, mY, mMap };
gameState->enqueueEvent(obj, e);
}
}
-TriggerArea::TriggerArea(int map, Rectangle const &r, TriggerAction *ptr)
- : Thing(OBJECT_OTHER), mZone(r), mAction(ptr)
-{
- setMapId(map);
-}
-
void TriggerArea::update()
{
- MapComposite *map = gameState->getMap(getMapId());
-
- for (MovingObjectIterator i(map->getInsideRectangleIterator(mZone)); i; ++i)
+ for (MovingObjectIterator i(getMap()->getInsideRectangleIterator(mZone)); i; ++i)
{
if (mZone.contains((*i)->getPosition()))
{
diff --git a/src/game-server/trigger.hpp b/src/game-server/trigger.hpp
index d444f75..d6e4d2c 100644
--- a/src/game-server/trigger.hpp
+++ b/src/game-server/trigger.hpp
@@ -39,13 +39,14 @@ class TriggerAction
class WarpAction : public TriggerAction
{
public:
- WarpAction(int m, int x, int y)
+ WarpAction(MapComposite *m, int x, int y)
: mMap(m), mX(x), mY(y) {}
virtual void process(Object *obj);
private:
- unsigned short mMap, mX, mY;
+ MapComposite *mMap;
+ unsigned short mX, mY;
};
class TriggerArea : public Thing
@@ -54,7 +55,9 @@ class TriggerArea : public Thing
/**
* Creates a rectangular trigger for a given map.
*/
- TriggerArea(int map, Rectangle const &, TriggerAction *);
+ TriggerArea(MapComposite *m, Rectangle const &r, TriggerAction *ptr)
+ : Thing(OBJECT_OTHER, m), mZone(r), mAction(ptr) {}
+
virtual void update();
private: