Skip to content

Commit

Permalink
Fix various formations - Issue liyunfan1223#749 (liyunfan1223#807)
Browse files Browse the repository at this point in the history
* Fix various formations

* Refactor Formation::GetFollowAngle()

* Update Formations.cpp

* Refactored Formation::GetFollowAngle()

Final refactor of Formation::GetFollowAngle()

 - By combining the group member iteration, unnecessary loops are avoided.
- Clearer Structure: The code is more readable, with fewer redundant operations.
- Better Maintainability: Comments and logical grouping make it easier to modify or extend the function in the future.

* Logic order improvement
  • Loading branch information
nl-saw authored Dec 22, 2024
1 parent e5f1b86 commit d9f9d98
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions src/strategy/values/Formations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,79 +413,83 @@ float Formation::GetFollowAngle()
Group* group = bot->GetGroup();
PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot);

// If there's no master and no group
if (!master && !group)
return 0.0f;

uint32 index = 1;
uint32 total = 1;

PlayerbotMgr* masterBotMgr = nullptr;
if (master)
masterBotMgr = GET_PLAYERBOT_MGR(master);
if (!group && master && !GET_PLAYERBOT_AI(master) && masterBotMgr)
{
for (PlayerBotMap::const_iterator i = masterBotMgr->GetPlayerBotsBegin(); i != masterBotMgr->GetPlayerBotsEnd();
++i)
{
if (i->second == bot)
index = total;
std::vector<Player*> roster;

++total;
}
}
else if (group)
if (group)
{
std::vector<Player*> roster;
bool left = true; // Used for alternating tanks' positions

for (GroupReference* ref = group->GetFirstMember(); ref; ref = ref->next())
{
if (Player* member = ref->GetSource())
Player* member = ref->GetSource();

// Skip invalid, dead, or out-of-map members
if (!member || !member->IsAlive() || bot->GetMapId() != member->GetMapId())
continue;

// Skip the master
if (member == master)
continue;

// Put DPS in the middle
if (!botAI->IsTank(member) && !botAI->IsHeal(member))
{
if (!member || member == bot || !member->IsAlive() || bot->GetMapId() != member->GetMapId()) continue;
if (member != master && !botAI->IsTank(member) && !botAI->IsHeal(member))
{
roster.insert(roster.begin() + roster.size() / 2, member);
}
roster.insert(roster.begin() + roster.size() / 2, member);
}
}

for (GroupReference* ref = group->GetFirstMember(); ref; ref = ref->next())
{
if (Player* member = ref->GetSource())
// Put Healers in the middle
else if (botAI->IsHeal(member))
{
if (!member || member == bot || !member->IsAlive() || bot->GetMapId() != member->GetMapId()) continue;
if (member != master && botAI->IsHeal(member))
{
roster.insert(roster.begin() + roster.size() / 2, member);
}
roster.insert(roster.begin() + roster.size() / 2, member);
}
}

bool left = true;
for (GroupReference* ref = group->GetFirstMember(); ref; ref = ref->next())
{
if (Player* member = ref->GetSource())
// Handle tanks (alternate between front and back)
else if (botAI->IsTank(member))
{
if (!member || member == bot || !member->IsAlive() || bot->GetMapId() != member->GetMapId()) continue;
if (member != master && botAI->IsTank(member))
{
if (left)
roster.push_back(member);
else
roster.insert(roster.begin(), member);
if (left)
roster.push_back(member); // Place tank at the back
else
roster.insert(roster.begin(), member); // Place tank at the front

left = !left;
}
left = !left; // Alternate for the next tank
}
}

for (Player* playerRoster : roster)
total++;
}
}
else if (master)
{
// If the bot is following a master, look up the bot's position in the master's list
PlayerbotMgr* masterBotMgr = GET_PLAYERBOT_MGR(master);
if (masterBotMgr && !GET_PLAYERBOT_AI(master))
{
if (playerRoster == bot)
break;

++index;
for (auto it = masterBotMgr->GetPlayerBotsBegin(); it != masterBotMgr->GetPlayerBotsEnd(); ++it)
{
if (it->second == bot)
{
index = total; // Found bot in master's list, set the index
break;
}
++total;
}
}
}

total = roster.size() + 1;
// Find the bot's position in the roster
auto it = std::find(roster.begin(), roster.end(), bot);
if (it != roster.end())
{
index = std::distance(roster.begin(), it) + 1; // Find bot's index in the roster
}

// Return
float start = (master ? master->GetOrientation() : 0.0f);
return start + (0.125f + 1.75f * index / total + (total == 2 ? 0.125f : 0.0f)) * M_PI;
}
Expand Down

0 comments on commit d9f9d98

Please sign in to comment.