mirror of
https://github.com/mod-playerbots/mod-playerbots.git
synced 2026-01-14 09:29:09 +00:00
Run clang-format
This commit is contained in:
@@ -1,36 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it
|
||||
* and/or modify it under version 2 of the License, or (at your option), any later version.
|
||||
*/
|
||||
|
||||
#include "FleeManager.h"
|
||||
|
||||
#include "Playerbots.h"
|
||||
#include "ServerFacade.h"
|
||||
|
||||
FleeManager::FleeManager(Player* bot, float maxAllowedDistance, float followAngle, bool forceMaxDistance, WorldPosition startPosition) :
|
||||
bot(bot), maxAllowedDistance(maxAllowedDistance), followAngle(followAngle), forceMaxDistance(forceMaxDistance), startPosition(startPosition ? startPosition : WorldPosition(bot))
|
||||
FleeManager::FleeManager(Player* bot, float maxAllowedDistance, float followAngle, bool forceMaxDistance,
|
||||
WorldPosition startPosition)
|
||||
: bot(bot),
|
||||
maxAllowedDistance(maxAllowedDistance),
|
||||
followAngle(followAngle),
|
||||
forceMaxDistance(forceMaxDistance),
|
||||
startPosition(startPosition ? startPosition : WorldPosition(bot))
|
||||
{
|
||||
}
|
||||
|
||||
void FleeManager::calculateDistanceToCreatures(FleePoint *point)
|
||||
void FleeManager::calculateDistanceToCreatures(FleePoint* point)
|
||||
{
|
||||
point->minDistance = -1.0f;
|
||||
point->sumDistance = 0.0f;
|
||||
PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot);
|
||||
if (!botAI) {
|
||||
if (!botAI)
|
||||
{
|
||||
return;
|
||||
}
|
||||
GuidVector units = *botAI->GetAiObjectContext()->GetValue<GuidVector>("possible targets no los");
|
||||
for (GuidVector::iterator i = units.begin(); i != units.end(); ++i)
|
||||
GuidVector units = *botAI->GetAiObjectContext()->GetValue<GuidVector>("possible targets no los");
|
||||
for (GuidVector::iterator i = units.begin(); i != units.end(); ++i)
|
||||
{
|
||||
Unit* unit = botAI->GetUnit(*i);
|
||||
if (!unit)
|
||||
continue;
|
||||
Unit* unit = botAI->GetUnit(*i);
|
||||
if (!unit)
|
||||
continue;
|
||||
|
||||
float d = sServerFacade->GetDistance2d(unit, point->x, point->y);
|
||||
point->sumDistance += d;
|
||||
if (point->minDistance < 0 || point->minDistance > d)
|
||||
float d = sServerFacade->GetDistance2d(unit, point->x, point->y);
|
||||
point->sumDistance += d;
|
||||
if (point->minDistance < 0 || point->minDistance > d)
|
||||
point->minDistance = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool intersectsOri(float angle, std::vector<float>& angles, float angleIncrement)
|
||||
@@ -45,20 +53,21 @@ bool intersectsOri(float angle, std::vector<float>& angles, float angleIncrement
|
||||
return false;
|
||||
}
|
||||
|
||||
void FleeManager::calculatePossibleDestinations(std::vector<FleePoint*> &points)
|
||||
void FleeManager::calculatePossibleDestinations(std::vector<FleePoint*>& points)
|
||||
{
|
||||
PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot);
|
||||
if (!botAI) {
|
||||
if (!botAI)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Unit* target = *botAI->GetAiObjectContext()->GetValue<Unit*>("current target");
|
||||
|
||||
float botPosX = startPosition.getX();
|
||||
float botPosX = startPosition.getX();
|
||||
float botPosY = startPosition.getY();
|
||||
float botPosZ = startPosition.getZ();
|
||||
|
||||
FleePoint start(botAI, botPosX, botPosY, botPosZ);
|
||||
calculateDistanceToCreatures(&start);
|
||||
FleePoint start(botAI, botPosX, botPosY, botPosZ);
|
||||
calculateDistanceToCreatures(&start);
|
||||
|
||||
std::vector<float> enemyOri;
|
||||
GuidVector units = *botAI->GetAiObjectContext()->GetValue<GuidVector>("possible targets no los");
|
||||
@@ -72,19 +81,24 @@ void FleeManager::calculatePossibleDestinations(std::vector<FleePoint*> &points)
|
||||
enemyOri.push_back(ori);
|
||||
}
|
||||
|
||||
float distIncrement = std::max(sPlayerbotAIConfig->followDistance, (maxAllowedDistance - sPlayerbotAIConfig->tooCloseDistance) / 10.0f);
|
||||
for (float dist = maxAllowedDistance; dist >= sPlayerbotAIConfig->tooCloseDistance ; dist -= distIncrement)
|
||||
float distIncrement = std::max(sPlayerbotAIConfig->followDistance,
|
||||
(maxAllowedDistance - sPlayerbotAIConfig->tooCloseDistance) / 10.0f);
|
||||
for (float dist = maxAllowedDistance; dist >= sPlayerbotAIConfig->tooCloseDistance; dist -= distIncrement)
|
||||
{
|
||||
float angleIncrement = std::max(M_PI / 20, M_PI / 4 / (1.0 + dist - sPlayerbotAIConfig->tooCloseDistance));
|
||||
for (float add = 0.0f; add < M_PI / 4 + angleIncrement; add += angleIncrement)
|
||||
{
|
||||
for (float angle = add; angle < add + 2 * static_cast<float>(M_PI) + angleIncrement; angle += static_cast<float>(M_PI) / 4)
|
||||
for (float angle = add; angle < add + 2 * static_cast<float>(M_PI) + angleIncrement;
|
||||
angle += static_cast<float>(M_PI) / 4)
|
||||
{
|
||||
if (intersectsOri(angle, enemyOri, angleIncrement))
|
||||
continue;
|
||||
|
||||
float x = botPosX + cos(angle) * maxAllowedDistance, y = botPosY + sin(angle) * maxAllowedDistance, z = botPosZ + CONTACT_DISTANCE;
|
||||
if (forceMaxDistance && sServerFacade->IsDistanceLessThan(sServerFacade->GetDistance2d(bot, x, y), maxAllowedDistance - sPlayerbotAIConfig->tooCloseDistance))
|
||||
float x = botPosX + cos(angle) * maxAllowedDistance, y = botPosY + sin(angle) * maxAllowedDistance,
|
||||
z = botPosZ + CONTACT_DISTANCE;
|
||||
if (forceMaxDistance &&
|
||||
sServerFacade->IsDistanceLessThan(sServerFacade->GetDistance2d(bot, x, y),
|
||||
maxAllowedDistance - sPlayerbotAIConfig->tooCloseDistance))
|
||||
continue;
|
||||
|
||||
bot->UpdateAllowedPositionZ(x, y, z);
|
||||
@@ -99,7 +113,8 @@ void FleeManager::calculatePossibleDestinations(std::vector<FleePoint*> &points)
|
||||
FleePoint* point = new FleePoint(botAI, x, y, z);
|
||||
calculateDistanceToCreatures(point);
|
||||
|
||||
if (sServerFacade->IsDistanceGreaterOrEqualThan(point->minDistance - start.minDistance, sPlayerbotAIConfig->followDistance))
|
||||
if (sServerFacade->IsDistanceGreaterOrEqualThan(point->minDistance - start.minDistance,
|
||||
sPlayerbotAIConfig->followDistance))
|
||||
points.push_back(point);
|
||||
else
|
||||
delete point;
|
||||
@@ -108,14 +123,14 @@ void FleeManager::calculatePossibleDestinations(std::vector<FleePoint*> &points)
|
||||
}
|
||||
}
|
||||
|
||||
void FleeManager::cleanup(std::vector<FleePoint*> &points)
|
||||
void FleeManager::cleanup(std::vector<FleePoint*>& points)
|
||||
{
|
||||
for (std::vector<FleePoint*>::iterator i = points.begin(); i != points.end(); i++)
|
||||
for (std::vector<FleePoint*>::iterator i = points.begin(); i != points.end(); i++)
|
||||
{
|
||||
delete *i;
|
||||
}
|
||||
delete *i;
|
||||
}
|
||||
|
||||
points.clear();
|
||||
points.clear();
|
||||
}
|
||||
|
||||
bool FleeManager::isBetterThan(FleePoint* point, FleePoint* other)
|
||||
@@ -123,23 +138,23 @@ bool FleeManager::isBetterThan(FleePoint* point, FleePoint* other)
|
||||
return point->sumDistance - other->sumDistance > 0;
|
||||
}
|
||||
|
||||
FleePoint* FleeManager::selectOptimalDestination(std::vector<FleePoint*> &points)
|
||||
FleePoint* FleeManager::selectOptimalDestination(std::vector<FleePoint*>& points)
|
||||
{
|
||||
FleePoint* best = nullptr;
|
||||
for (std::vector<FleePoint*>::iterator i = points.begin(); i != points.end(); i++)
|
||||
FleePoint* best = nullptr;
|
||||
for (std::vector<FleePoint*>::iterator i = points.begin(); i != points.end(); i++)
|
||||
{
|
||||
FleePoint* point = *i;
|
||||
if (!best || isBetterThan(point, best))
|
||||
FleePoint* point = *i;
|
||||
if (!best || isBetterThan(point, best))
|
||||
best = point;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
return best;
|
||||
}
|
||||
|
||||
bool FleeManager::CalculateDestination(float* rx, float* ry, float* rz)
|
||||
{
|
||||
std::vector<FleePoint*> points;
|
||||
calculatePossibleDestinations(points);
|
||||
calculatePossibleDestinations(points);
|
||||
|
||||
FleePoint* point = selectOptimalDestination(points);
|
||||
if (!point)
|
||||
@@ -148,18 +163,19 @@ bool FleeManager::CalculateDestination(float* rx, float* ry, float* rz)
|
||||
return false;
|
||||
}
|
||||
|
||||
*rx = point->x;
|
||||
*ry = point->y;
|
||||
*rz = point->z;
|
||||
*rx = point->x;
|
||||
*ry = point->y;
|
||||
*rz = point->z;
|
||||
|
||||
cleanup(points);
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FleeManager::isUseful()
|
||||
{
|
||||
PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot);
|
||||
if (!botAI) {
|
||||
if (!botAI)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
GuidVector units = *botAI->GetAiObjectContext()->GetValue<GuidVector>("possible targets no los");
|
||||
@@ -169,7 +185,8 @@ bool FleeManager::isUseful()
|
||||
if (!creature)
|
||||
continue;
|
||||
|
||||
if (startPosition.sqDistance(WorldPosition(creature)) < creature->GetAttackDistance(bot) * creature->GetAttackDistance(bot))
|
||||
if (startPosition.sqDistance(WorldPosition(creature)) <
|
||||
creature->GetAttackDistance(bot) * creature->GetAttackDistance(bot))
|
||||
return true;
|
||||
|
||||
// float d = sServerFacade->GetDistance2d(unit, bot);
|
||||
|
||||
Reference in New Issue
Block a user