summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjörn Steinbrink <B.Steinbrink@gmx.de>2005-10-16 18:24:07 +0000
committerBjörn Steinbrink <B.Steinbrink@gmx.de>2005-10-16 18:24:07 +0000
commit4453a80a2f43c5ebf20dce881586a89c484d4830 (patch)
tree84c14687a6cc3276d9c60583689baf954942546e
parent837072a1482934a572853f5c41974e4ac90da1ca (diff)
downloadmana-4453a80a2f43c5ebf20dce881586a89c484d4830.tar.gz
mana-4453a80a2f43c5ebf20dce881586a89c484d4830.tar.bz2
mana-4453a80a2f43c5ebf20dce881586a89c484d4830.tar.xz
mana-4453a80a2f43c5ebf20dce881586a89c484d4830.zip
Reduce code duplication in the findNode functions and use a functor to do the search.
-rw-r--r--ChangeLog2
-rw-r--r--src/being.cpp49
2 files changed, 25 insertions, 26 deletions
diff --git a/ChangeLog b/ChangeLog
index d43949ce..c538c85c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,8 @@
2005-10-16 Björn Steinbrink <B.Steinbrink@gmx.de>
+ * src/being.cpp: Reduce code duplication in the findNode functions and
+ use a functor to do the search.
* src/map.cpp: Some code improvements.
* src/Makefile.am: Added two missing files.
diff --git a/src/being.cpp b/src/being.cpp
index 7bd1ad2d..fdfc5c8c 100644
--- a/src/being.cpp
+++ b/src/being.cpp
@@ -23,6 +23,7 @@
#include "being.h"
+#include <algorithm>
#include <sstream>
#include "game.h"
@@ -139,39 +140,35 @@ Being *findNode(Uint32 id)
return NULL;
}
-Being *findNode(Uint16 x, Uint16 y)
+class FindNodeFunctor
{
- for (Beings::iterator i = beings.begin(); i != beings.end(); i++)
- {
- Being *being = (*i);
- // Return being if found and it is not a dead monster
- if (being->x == x &&
- (being->y == y ||
- (being->getType() == Being::NPC && being->y == y + 1)) &&
- being->action != Being::MONSTER_DEAD)
+ public:
+ bool operator() (Being *being)
{
- return being;
+ Uint16 other_y = y + ((being->getType() == Being::NPC) ? 1 : 0);
+ return (being->x == x && (being->y == y || being->y == other_y) &&
+ being->action != Being::MONSTER_DEAD &&
+ (type == Being::UNKNOWN || being->getType() == type));
}
- }
- return NULL;
+
+ Uint16 x, y;
+ Being::Type type;
+} nodeFinder;
+
+Being *findNode(Uint16 x, Uint16 y)
+{
+ return findNode(x, y, Being::UNKNOWN);
}
Being* findNode(Uint16 x, Uint16 y, Being::Type type)
{
- for (Beings::iterator i = beings.begin(); i != beings.end(); i++)
- {
- Being *being = (*i);
- // Check if is a NPC (only low job ids)
- if (being->x == x &&
- (being->y == y ||
- (being->getType() == Being::NPC && being->y == y + 1)) &&
- being->getType() == type &&
- being->action != Being::MONSTER_DEAD)
- {
- return being;
- }
- }
- return NULL;
+ nodeFinder.x = x;
+ nodeFinder.y = y;
+ nodeFinder.type = type;
+
+ Beings::iterator i = find_if(beings.begin(), beings.end(), nodeFinder);
+
+ return (i == beings.end()) ? NULL : *i;
}
Being::Being():