summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-01-18 09:14:36 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-01-18 09:14:36 +0100
commitb823889f1747bbceb61273f7a7383580c4b034e3 (patch)
treee86c37728c0fdd991ed18f101092c07b03b7e918 /src
parent6eb2e01ef6943e4f8ef7cc32ec325e3c22b1f5ec (diff)
downloadmana-b823889f1747bbceb61273f7a7383580c4b034e3.tar.gz
mana-b823889f1747bbceb61273f7a7383580c4b034e3.tar.bz2
mana-b823889f1747bbceb61273f7a7383580c4b034e3.tar.xz
mana-b823889f1747bbceb61273f7a7383580c4b034e3.zip
Unified BeingManager::findNearestLivingBeing for eAthena and manaserv
It now always works with pixels.
Diffstat (limited to 'src')
-rw-r--r--src/beingmanager.cpp63
-rw-r--r--src/beingmanager.h32
-rw-r--r--src/game.cpp9
-rw-r--r--src/gui/viewport.cpp21
4 files changed, 38 insertions, 87 deletions
diff --git a/src/beingmanager.cpp b/src/beingmanager.cpp
index 8bd951fa..cc3d68e5 100644
--- a/src/beingmanager.cpp
+++ b/src/beingmanager.cpp
@@ -206,23 +206,14 @@ void BeingManager::clear()
mBeings.push_back(player_node);
}
-Being *BeingManager::findNearestLivingBeing(int x, int y, int maxdist,
+Being *BeingManager::findNearestLivingBeing(int x, int y,
+ int maxTileDist,
Being::Type type) const
{
- Being *closestBeing = NULL;
+ Being *closestBeing = 0;
int dist = 0;
-#ifdef MANASERV_SUPPORT
- //Why do we do this:
- //For some reason x,y passed to this function is always
- //in map coords, while down below its in pixels
- //
- //I believe there is a deeper problem under this, but
- //for a temp solution we'll convert to coords to pixels
- x = x * 32;
- y = y * 32;
- maxdist = maxdist * 32;
-#endif
+ const int maxDist = maxTileDist * 32;
Beings::const_iterator itr = mBeings.begin();
Beings::const_iterator itr_end = mBeings.end();
@@ -230,15 +221,11 @@ Being *BeingManager::findNearestLivingBeing(int x, int y, int maxdist,
for (; itr != itr_end; ++itr)
{
Being *being = (*itr);
-#ifdef MANASERV_SUPPORT
const Vector &pos = being->getPosition();
int d = abs(((int) pos.x) - x) + abs(((int) pos.y) - y);
-#else
- int d = abs(being->getTileX() - x) + abs(being->getTileY() - y);
-#endif
if ((being->getType() == type || type == Being::UNKNOWN)
- && (d < dist || closestBeing == NULL) // it is closer
+ && (d < dist || !closestBeing) // it is closer
&& being->mAction != Being::DEAD) // no dead beings
{
dist = d;
@@ -246,46 +233,14 @@ Being *BeingManager::findNearestLivingBeing(int x, int y, int maxdist,
}
}
- return (maxdist >= dist) ? closestBeing : NULL;
+ return (maxDist >= dist) ? closestBeing : 0;
}
-Being *BeingManager::findNearestLivingBeing(Being *aroundBeing, int maxdist,
+Being *BeingManager::findNearestLivingBeing(Being *aroundBeing, int maxDist,
Being::Type type) const
{
- Being *closestBeing = NULL;
- int dist = 0;
-#ifdef MANASERV_SUPPORT
- const Vector &apos = aroundBeing->getPosition();
- int x = apos.x;
- int y = apos.y;
- maxdist = maxdist * 32;
-#else
- int x = aroundBeing->getTileX();
- int y = aroundBeing->getTileY();
-#endif
-
- for (Beings::const_iterator i = mBeings.begin(), i_end = mBeings.end();
- i != i_end; ++i)
- {
- Being *being = (*i);
-#ifdef MANASERV_SUPPORT
- const Vector &pos = being->getPosition();
- int d = abs(((int) pos.x) - x) + abs(((int) pos.y) - y);
-#else
- int d = abs(being->getTileX() - x) + abs(being->getTileY() - y);
-#endif
-
- if ((being->getType() == type || type == Being::UNKNOWN)
- && (d < dist || closestBeing == NULL) // it is closer
- && being->mAction != Being::DEAD // no dead beings
- && being != aroundBeing)
- {
- dist = d;
- closestBeing = being;
- }
- }
-
- return (maxdist >= dist) ? closestBeing : NULL;
+ const Vector &pos = aroundBeing->getPosition();
+ return findNearestLivingBeing(pos.x, pos.y, maxDist, type);
}
bool BeingManager::hasBeing(Being *being) const
diff --git a/src/beingmanager.h b/src/beingmanager.h
index bea84e25..b288cc26 100644
--- a/src/beingmanager.h
+++ b/src/beingmanager.h
@@ -70,30 +70,32 @@ class BeingManager
/**
* Returns a being nearest to specific coordinates.
*
- * @param x X coordinate.
- * @param y Y coordinate.
- * @param maxdist Maximal distance. If minimal distance is larger,
- * no being is returned.
- * @param type The type of being to look for.
+ * @param x X coordinate in pixels.
+ * @param y Y coordinate in pixels.
+ * @param maxTileDist Maximal distance in tiles. If minimal distance is
+ * larger, no being is returned.
+ * @param type The type of being to look for.
*/
- Being *findNearestLivingBeing(int x, int y, int maxdist,
+ Being *findNearestLivingBeing(int x, int y, int maxTileDist,
Being::Type type = Being::UNKNOWN) const;
+ /**
+ * Returns a being nearest to another being.
+ *
+ * @param aroundBeing The being to search around.
+ * @param maxTileDist Maximal distance in tiles. If minimal distance is
+ * larger, no being is returned.
+ * @param type The type of being to look for.
+ */
+ Being *findNearestLivingBeing(Being *aroundBeing, int maxTileDist,
+ Being::Type type = Being::UNKNOWN) const;
+
/**
* Finds a being by name and (optionally) by type.
*/
Being *findBeingByName(const std::string &name,
Being::Type type = Being::UNKNOWN) const;
- /**
- * Returns a being nearest to another being.
- *
- * \param maxdist maximal distance. If minimal distance is larger,
- * no being is returned
- */
- Being *findNearestLivingBeing(Being *aroundBeing, int maxdist,
- Being::Type type = Being::UNKNOWN) const;
-
/**
* Returns the whole list of beings.
*/
diff --git a/src/game.cpp b/src/game.cpp
index c8f976a2..9ca58461 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -980,19 +980,14 @@ void Game::handleInput()
if (keyboard.isKeyActive(keyboard.KEY_TARGET_ATTACK))
{
- Being *target = NULL;
+ Being *target = 0;
bool newTarget = !keyboard.isKeyActive(keyboard.KEY_TARGET);
// A set target has highest priority
if (!player_node->getTarget())
{
-#ifdef MANASERV_SUPPORT
- Uint16 targetX = x / 32, targetY = y / 32;
-#else
- Uint16 targetX = x, targetY = y;
-#endif
// Only auto target Monsters
- target = beingManager->findNearestLivingBeing(targetX, targetY,
+ target = beingManager->findNearestLivingBeing(player_node,
20, Being::MONSTER);
}
player_node->attack(target, newTarget);
diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp
index ec8cf341..db995ab0 100644
--- a/src/gui/viewport.cpp
+++ b/src/gui/viewport.cpp
@@ -307,10 +307,10 @@ void Viewport::mousePressed(gcn::MouseEvent &event)
mPlayerFollowMouse = false;
- const int pixelx = event.getX() + (int) mPixelViewX;
- const int pixely = event.getY() + (int) mPixelViewY;
- const int tilex = pixelx / mMap->getTileWidth();
- const int tiley = pixely / mMap->getTileHeight();
+ const int pixelX = event.getX() + (int) mPixelViewX;
+ const int pixelY = event.getY() + (int) mPixelViewY;
+ const int tileX = pixelX / mMap->getTileWidth();
+ const int tileY = pixelY / mMap->getTileHeight();
// Right click might open a popup
if (event.getButton() == gcn::MouseEvent::RIGHT)
@@ -318,14 +318,14 @@ void Viewport::mousePressed(gcn::MouseEvent &event)
Being *being;
FloorItem *floorItem;
- if ((being = beingManager->findBeingByPixel(pixelx, pixely)) &&
+ if ((being = beingManager->findBeingByPixel(pixelX, pixelY)) &&
being != player_node)
{
mPopupMenu->showPopup(event.getX(), event.getY(), being);
return;
}
- else if ((floorItem = floorItemManager->findByCoordinates(tilex,
- tiley)))
+ else if ((floorItem = floorItemManager->findByCoordinates(tileX,
+ tileY)))
{
mPopupMenu->showPopup(event.getX(), event.getY(), floorItem);
return;
@@ -346,7 +346,7 @@ void Viewport::mousePressed(gcn::MouseEvent &event)
Being *being;
// Interact with some being
- if ((being = beingManager->findBeingByPixel(pixelx, pixely)))
+ if ((being = beingManager->findBeingByPixel(pixelX, pixelY)))
{
switch (being->getType())
{
@@ -378,7 +378,7 @@ void Viewport::mousePressed(gcn::MouseEvent &event)
}
// Picks up a item if we clicked on one
}
- else if ((item = floorItemManager->findByCoordinates(tilex, tiley)))
+ else if ((item = floorItemManager->findByCoordinates(tileX, tileY)))
{
player_node->pickUp(item);
}
@@ -401,8 +401,7 @@ void Viewport::mousePressed(gcn::MouseEvent &event)
{
// Find the being nearest to the clicked position
Being *target = beingManager->findNearestLivingBeing(
- tilex, tiley,
- 20, Being::MONSTER);
+ pixelX, pixelY, 20, Being::MONSTER);
if (target)
player_node->setTarget(target);