Fixed crash in MoveFromWhirlwindAction::Execute function (#1150)

* Fixed crash in MoveFromWhirlwindAction::Execute function

This PR fixes a crash in the MoveFromWhirlwindAction::Execute function in NexusActions.cpp. The error occurred when accessing boss->GetPosition() without checking whether boss was a valid pointer. The fix adds a nullptr check before calculating the distance, preventing invalid accesses and ensuring the stability of the action execution.

* Fix comment
This commit is contained in:
EricksOliveira
2025-03-31 11:47:13 -03:00
committed by GitHub
parent 7676fd6427
commit a50dc87dbc

View File

@@ -6,8 +6,8 @@ bool MoveFromWhirlwindAction::Execute(Event event)
{ {
Unit* boss = nullptr; Unit* boss = nullptr;
uint8 faction = bot->GetTeamId(); uint8 faction = bot->GetTeamId();
float targetDist = 10.0f; // Whirlwind has range of 8, add a couple for safety buffer float targetDist = 10.0f; // Whirlwind has a range of 8, adding a safety buffer
switch (bot->GetMap()->GetDifficulty()) switch (bot->GetMap()->GetDifficulty())
{ {
case DUNGEON_DIFFICULTY_NORMAL: case DUNGEON_DIFFICULTY_NORMAL:
@@ -15,7 +15,7 @@ bool MoveFromWhirlwindAction::Execute(Event event)
{ {
boss = AI_VALUE2(Unit*, "find target", "horde commander"); boss = AI_VALUE2(Unit*, "find target", "horde commander");
} }
else //if (faction == TEAM_HORDE) else // TEAM_HORDE
{ {
boss = AI_VALUE2(Unit*, "find target", "alliance commander"); boss = AI_VALUE2(Unit*, "find target", "alliance commander");
} }
@@ -25,7 +25,7 @@ bool MoveFromWhirlwindAction::Execute(Event event)
{ {
boss = AI_VALUE2(Unit*, "find target", "commander kolurg"); boss = AI_VALUE2(Unit*, "find target", "commander kolurg");
} }
else //if (faction == TEAM_HORDE) else // TEAM_HORDE
{ {
boss = AI_VALUE2(Unit*, "find target", "commander stoutbeard"); boss = AI_VALUE2(Unit*, "find target", "commander stoutbeard");
} }
@@ -33,11 +33,22 @@ bool MoveFromWhirlwindAction::Execute(Event event)
default: default:
break; break;
} }
float bossDistance = bot->GetExactDist2d(boss->GetPosition());
if (!boss || bossDistance > targetDist) // Ensure boss is valid before accessing its methods
if (!boss)
{ {
return false; return false;
} }
float bossDistance = bot->GetExactDist2d(boss->GetPosition());
// Check if the bot is already at a safe distance
if (bossDistance > targetDist)
{
return false;
}
// Move away from the boss to avoid Whirlwind
return MoveAway(boss, targetDist - bossDistance); return MoveAway(boss, targetDist - bossDistance);
} }