summaryrefslogtreecommitdiff
path: root/src/actormanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/actormanager.cpp')
-rw-r--r--src/actormanager.cpp67
1 files changed, 63 insertions, 4 deletions
diff --git a/src/actormanager.cpp b/src/actormanager.cpp
index b83a08a0d..43330a7c5 100644
--- a/src/actormanager.cpp
+++ b/src/actormanager.cpp
@@ -22,6 +22,7 @@
#include "actormanager.h"
+#include "game.h"
#include "configuration.h"
#include "settings.h"
@@ -52,6 +53,8 @@
#include "resources/chatobject.h"
#include "resources/iteminfo.h"
+#include "resources/map/map.h"
+
#include "resources/db/itemdb.h"
#include <algorithm>
@@ -905,6 +908,62 @@ void ActorManager::clear()
mActors.insert(localPlayer);
}
+Being *ActorManager::findNearestPvpPlayer() const
+{
+ if (!localPlayer)
+ return nullptr;
+
+ // don't attack players
+ if (settings.pvpAttackType == 3)
+ return nullptr;
+
+ const Game *const game = Game::instance();
+ if (!game)
+ return nullptr;
+
+ const Map *const map = game->getCurrentMap();
+ if (!map)
+ return nullptr;
+
+ const int mapPvpMode = map->getPvpMode();
+ Being *target = nullptr;
+ int minDistSquared = 20000;
+
+ for_actors
+ {
+ if ((*it)->getType() != ActorType::Player)
+ continue;
+
+ Being *const being = static_cast<Being*>(*it);
+
+ if (!being || !being->isAlive() ||
+ localPlayer == being)
+ {
+ continue;
+ }
+
+ const int teamId = being->getTeamId();
+ // this condition is very TMW-specific
+ if (!(mapPvpMode || teamId))
+ continue;
+
+ if (!localPlayer->checAttackPermissions(being))
+ continue;
+
+ const int dx = being->getTileX() - localPlayer->getTileX();
+ const int dy = being->getTileY() - localPlayer->getTileY();
+ const int distSquared = dx * dx + dy * dy;
+ if (distSquared < minDistSquared)
+ {
+ minDistSquared = distSquared;
+ target = being;
+ }
+ }
+
+ return target;
+}
+
+
Being *ActorManager::findNearestLivingBeing(const int x, const int y,
const int maxTileDist,
const ActorTypeT type,
@@ -1321,10 +1380,10 @@ Being* ActorManager::findMostDamagedPlayer(const int maxTileDist) const
Being *const being = static_cast<Being*>(*it);
- if ((!being) || (!being->isAlive()) || // don't heal dead
- (player_relations.getRelation(being->getName()) ==
- Relation::ENEMY2) || // don't heal enemy
- (localPlayer == being)) // don't heal self
+ if (!being || !being->isAlive() || // don't heal dead
+ player_relations.getRelation(being->getName()) ==
+ Relation::ENEMY2 || // don't heal enemy
+ localPlayer == being) // don't heal self
{
continue;
}