summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2013-09-19 19:59:58 +0200
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2013-09-19 19:59:58 +0200
commit810f968b177c332b4459ca08b7e9b010b209bea6 (patch)
tree3e0ffdf76711cfc538fe33c1fdb699de2a2b2a46
parentc168472002a465101e556a5165965de6bfb6231c (diff)
downloadmanaserv-810f968b177c332b4459ca08b7e9b010b209bea6.tar.gz
manaserv-810f968b177c332b4459ca08b7e9b010b209bea6.tar.bz2
manaserv-810f968b177c332b4459ca08b7e9b010b209bea6.tar.xz
manaserv-810f968b177c332b4459ca08b7e9b010b209bea6.zip
Some optimizations in GameHandler
The findActorNear, findBeingNear and findCharacterNear functions in gamehandler.cpp were iterating over nearby entities in search for an entity with a specific ID. Now they do a quick lookup of exactly that entity, and then check whether it is in range.
-rw-r--r--src/game-server/gamehandler.cpp55
-rw-r--r--src/game-server/mapcomposite.cpp20
-rw-r--r--src/game-server/mapcomposite.h6
3 files changed, 43 insertions, 38 deletions
diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp
index 6546d8d3..48e54bdf 100644
--- a/src/game-server/gamehandler.cpp
+++ b/src/game-server/gamehandler.cpp
@@ -139,56 +139,35 @@ void GameHandler::updateCharacter(int charid, int partyid)
static Entity *findActorNear(Entity *p, int id)
{
MapComposite *map = p->getMap();
- const Point &ppos = p->getComponent<ActorComponent>()->getPosition();
- // See map.h for tiles constants
- const int pixelDist = DEFAULT_TILE_LENGTH * TILES_TO_BE_NEAR;
- for (ActorIterator i(map->getAroundPointIterator(ppos, pixelDist)); i; ++i)
- {
- Entity *a = *i;
- if (a->getComponent<ActorComponent>()->getPublicID() != id)
- continue;
- return ppos.inRangeOf(a->getComponent<ActorComponent>()->getPosition(),
- pixelDist) ? a : 0;
+
+ if (Entity *e = map->findEntityById(id)) {
+ const Point &ppos = p->getComponent<ActorComponent>()->getPosition();
+ const Point &epos = e->getComponent<ActorComponent>()->getPosition();
+
+ // See map.h for tiles constants
+ const int pixelDist = DEFAULT_TILE_LENGTH * TILES_TO_BE_NEAR;
+ if (ppos.inRangeOf(epos, pixelDist))
+ return e;
}
+
return 0;
}
static Entity *findBeingNear(Entity *p, int id)
{
- MapComposite *map = p->getMap();
- const Point &ppos = p->getComponent<ActorComponent>()->getPosition();
- // See map.h for tiles constants
- const int pixelDist = DEFAULT_TILE_LENGTH * TILES_TO_BE_NEAR;
- for (BeingIterator i(map->getAroundPointIterator(ppos, pixelDist)); i; ++i)
- {
- Entity *b = *i;
- if (b->getComponent<ActorComponent>()->getPublicID() != id)
- continue;
- return ppos.inRangeOf(b->getComponent<ActorComponent>()->getPosition(),
- pixelDist) ? b : 0;
- }
+ if (Entity *e = findActorNear(p, id))
+ if (e->hasComponent<BeingComponent>())
+ return e;
+
return 0;
}
static Entity *findCharacterNear(Entity *p, int id)
{
- MapComposite *map = p->getMap();
- const Point &ppos = p->getComponent<ActorComponent>()->getPosition();
- // See map.h for tiles constants
- const int pixelDist = DEFAULT_TILE_LENGTH * TILES_TO_BE_NEAR;
- for (CharacterIterator i(map->getAroundPointIterator(ppos,
- pixelDist)); i; ++i)
- {
- Entity *c = *i;
- if (c->getComponent<ActorComponent>()->getPublicID() != id)
- continue;
-
- if (ppos.inRangeOf(c->getComponent<ActorComponent>()->getPosition(),
- pixelDist))
- return c;
+ if (Entity *e = findActorNear(p, id))
+ if (e->getType() == OBJECT_CHARACTER)
+ return e;
- return 0;
- }
return 0;
}
diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp
index e00407ff..deb43b4e 100644
--- a/src/game-server/mapcomposite.cpp
+++ b/src/game-server/mapcomposite.cpp
@@ -285,6 +285,8 @@ struct MapContent
*/
void deallocate(Entity *);
+ Entity *findEntityById(int publicId) const;
+
/**
* Fills a region of zones within the range of a point.
*/
@@ -396,6 +398,19 @@ void MapContent::deallocate(Entity *obj)
buckets[id / 256]->deallocate(id % 256);
}
+/**
+ * Returns the entity matching \a publicId, or null if no such entity exists.
+ */
+Entity *MapContent::findEntityById(int publicId) const
+{
+ if (ObjectBucket *b = buckets[publicId / 256]) {
+ const int bucketIndex = publicId % 256;
+ if (b->isAllocated(bucketIndex))
+ return b->objects[bucketIndex];
+ }
+ return 0;
+}
+
static void addZone(MapRegion &r, unsigned z)
{
MapRegion::iterator i_end = r.end(),
@@ -725,6 +740,11 @@ void MapComposite::remove(Entity *ptr)
}
}
+Entity *MapComposite::findEntityById(int publicId) const
+{
+ return mContent->findEntityById(publicId);
+}
+
void MapComposite::update()
{
// Update object status
diff --git a/src/game-server/mapcomposite.h b/src/game-server/mapcomposite.h
index 464dfe7c..212d4529 100644
--- a/src/game-server/mapcomposite.h
+++ b/src/game-server/mapcomposite.h
@@ -182,6 +182,12 @@ class MapComposite
void remove(Entity *);
/**
+ * Returns the actor entity matching \a publicID, or null when no such
+ * entity exists.
+ */
+ Entity *findEntityById(int publicId) const;
+
+ /**
* Updates zones of every moving beings.
*/
void update();