From 8d4dc1c28ee54f67c155b6759cf5776a917904ff Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Sun, 30 Dec 2007 00:02:14 +0000 Subject: Added a key for targeting the nearest player character based on patches by Trinexx. Some mapping fixes at snake dungeon map. --- src/beingmanager.cpp | 27 +++++++++++++++++++++++++++ src/beingmanager.h | 9 +++++++++ src/game.cpp | 13 +++++++++++++ src/keyboardconfig.cpp | 1 + src/keyboardconfig.h | 1 + 5 files changed, 51 insertions(+) (limited to 'src') diff --git a/src/beingmanager.cpp b/src/beingmanager.cpp index f35a305d..daceca5e 100644 --- a/src/beingmanager.cpp +++ b/src/beingmanager.cpp @@ -188,3 +188,30 @@ Being* BeingManager::findNearestLivingBeing(Uint16 x, Uint16 y, int maxdist, return (maxdist >= dist) ? closestBeing : NULL; } + +Being* BeingManager::findNearestLivingBeing(Being *aroundBeing, int maxdist, + Being::Type type) +{ + Being *closestBeing = NULL; + int dist = 0; + int x = aroundBeing->mX; + int y = aroundBeing->mY; + + for (BeingIterator i = mBeings.begin(); i != mBeings.end(); i++) + { + Being *being = (*i); + int d = abs(being->mX - x) + abs(being->mY - y); + + 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; +} diff --git a/src/beingmanager.h b/src/beingmanager.h index 152fb640..a9d0db35 100644 --- a/src/beingmanager.h +++ b/src/beingmanager.h @@ -77,6 +77,15 @@ class BeingManager Being* findNearestLivingBeing(Uint16 x, Uint16 y, int maxdist, Being::Type type = Being::UNKNOWN); + /** + * Return 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); + /** * Returns the whole list of beings */ diff --git a/src/game.cpp b/src/game.cpp index b3fa6f07..52d70bc0 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -770,6 +770,19 @@ void Game::handleInput() player_node->attack(target, newTarget); } + // Target the nearest player if 'q' is pressed + if ( keyboard.isKeyActive(keyboard.KEY_TARGET_PLAYER) ) + //if (keys[SDLK_q]) + { + Being *target = + beingManager->findNearestLivingBeing(player_node, 20, Being::PLAYER); + + if (target) + { + player_node->setTarget(target); + } + } + // Target the nearest monster if 'a' pressed if ( keyboard.isKeyActive(keyboard.KEY_TARGET_CLOSEST) ) //if (keys[SDLK_a]) diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp index 271961c8..a959e244 100644 --- a/src/keyboardconfig.cpp +++ b/src/keyboardconfig.cpp @@ -45,6 +45,7 @@ static KeyData const keyData[KeyboardConfig::KEY_TOTAL] = { {"keyAttack", SDLK_LCTRL, "Attack"}, {"keyTarget", SDLK_LSHIFT, "Target"}, {"keyTargetClosest", SDLK_a, "Target Closest"}, + {"keyTargetPlayer", SDLK_q, "Target Player"}, {"keyPickup", SDLK_z, "Pickup"}, {"keyHideWindows", SDLK_h, "Hide Windows"}, {"keyBeingSit", SDLK_s, "Sit"}, diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h index 46394fa5..53a5c96d 100644 --- a/src/keyboardconfig.h +++ b/src/keyboardconfig.h @@ -153,6 +153,7 @@ class KeyboardConfig KEY_ATTACK, KEY_TARGET, KEY_TARGET_CLOSEST, + KEY_TARGET_PLAYER, KEY_PICKUP, KEY_HIDE_WINDOWS, KEY_SIT, -- cgit v1.2.3-70-g09d2