diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-10-16 18:24:07 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-10-16 18:24:07 +0000 |
commit | 4453a80a2f43c5ebf20dce881586a89c484d4830 (patch) | |
tree | 84c14687a6cc3276d9c60583689baf954942546e | |
parent | 837072a1482934a572853f5c41974e4ac90da1ca (diff) | |
download | mana-client-4453a80a2f43c5ebf20dce881586a89c484d4830.tar.gz mana-client-4453a80a2f43c5ebf20dce881586a89c484d4830.tar.bz2 mana-client-4453a80a2f43c5ebf20dce881586a89c484d4830.tar.xz mana-client-4453a80a2f43c5ebf20dce881586a89c484d4830.zip |
Reduce code duplication in the findNode functions and use a functor to do the search.
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | src/being.cpp | 49 |
2 files changed, 25 insertions, 26 deletions
@@ -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(): |