2009-02-04 10:33:18 -06:00
|
|
|
/*
|
2009-02-04 12:42:26 +01:00
|
|
|
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
2008-12-15 00:07:21 +02:00
|
|
|
*
|
2009-02-04 12:04:12 +01:00
|
|
|
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
|
2008-12-15 00:07:21 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Creature.h"
|
|
|
|
|
#include "CreatureGroups.h"
|
|
|
|
|
#include "ObjectMgr.h"
|
|
|
|
|
#include "ProgressBar.h"
|
|
|
|
|
#include "Policies/SingletonImp.h"
|
|
|
|
|
|
|
|
|
|
#define MAX_DESYNC 5.0f
|
2009-04-06 13:27:42 -05:00
|
|
|
|
2008-12-15 00:07:21 +02:00
|
|
|
INSTANTIATE_SINGLETON_1(CreatureGroupManager);
|
|
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
CreatureGroupHolderType CreatureGroupHolder;
|
2008-12-15 00:07:21 +02:00
|
|
|
UNORDERED_MAP<uint32, FormationMember*> CreatureGroupMap;
|
|
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
void CreatureGroupManager::UpdateCreatureGroup(uint32 group_id, Creature &member)
|
2008-12-15 00:07:21 +02:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
std::multimap<uint32, CreatureGroup *>::iterator cgroup_data = CreatureGroupHolder.find(group_id);
|
|
|
|
|
|
|
|
|
|
if(cgroup_data != CreatureGroupHolder.end())
|
2009-02-09 08:16:34 -05:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
typedef std::multimap<uint32, CreatureGroup *>::iterator multiplegroup;
|
|
|
|
|
std::pair<multiplegroup, multiplegroup> range = CreatureGroupHolder.equal_range(group_id);
|
|
|
|
|
|
|
|
|
|
for(multiplegroup i = range.first; i != range.second; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(i->second->getInstanceID() == member.GetInstanceId())
|
|
|
|
|
{
|
|
|
|
|
sLog.outDebug("Group found: %u, inserting creature GUID: %u, Group InstanceID %u", group_id, member.GetGUIDLow(), i->second->getInstanceID());
|
|
|
|
|
i->second->AddMember(member);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-09 08:16:34 -05:00
|
|
|
}
|
2009-04-06 13:27:42 -05:00
|
|
|
|
|
|
|
|
sLog.outDebug("Group not found: %u. Creating new group.", group_id);
|
|
|
|
|
CreatureGroup* cgroup = new CreatureGroup(group_id, member.GetInstanceId());
|
|
|
|
|
cgroup->AddMember(member);
|
|
|
|
|
CreatureGroupHolder.insert(std::make_pair(group_id, cgroup));
|
2008-12-15 00:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
void CreatureGroupManager::DestroyGroup(CreatureGroup *&formation, uint64 guid)
|
2008-12-15 00:07:21 +02:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
if(!formation || !guid)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
sLog.outDebug("Deleting member pointer to GUID: %u from group %u", formation->GetId(), guid);
|
|
|
|
|
formation->RemoveMember(guid);
|
|
|
|
|
|
|
|
|
|
if(formation->isEmpty())
|
2009-02-09 08:16:34 -05:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
uint32 id = formation->GetId();
|
|
|
|
|
typedef std::multimap<uint32, CreatureGroup *>::iterator multiplegroup;
|
|
|
|
|
std::pair<multiplegroup, multiplegroup> range = CreatureGroupHolder.equal_range(id);
|
2009-02-09 08:16:34 -05:00
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
for(multiplegroup i = range.first; i != range.second; ++i)
|
2009-02-09 08:16:34 -05:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
if(i->second == formation)
|
|
|
|
|
{
|
|
|
|
|
sLog.outDebug("Deleting group with InstanceID %u", i->second->getInstanceID());
|
|
|
|
|
CreatureGroupHolder.erase(i);
|
|
|
|
|
delete formation;
|
|
|
|
|
}
|
2009-02-09 08:16:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
2008-12-15 00:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreatureGroupManager::LoadCreatureFormations()
|
|
|
|
|
{
|
2009-02-09 08:16:34 -05:00
|
|
|
//Clear existing map
|
|
|
|
|
CreatureGroupMap.clear();
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
//Check Integrity of the table
|
|
|
|
|
QueryResult *result = WorldDatabase.PQuery("SELECT MAX(`leaderGUID`) FROM `creature_formations`");
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
if(!result)
|
2008-12-15 00:07:21 +02:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
sLog.outErrorDb(" ...an error occured while loading the table `creature_formations` ( maybe it doesn't exist ?)\n");
|
2008-12-15 00:07:21 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2009-02-09 08:16:34 -05:00
|
|
|
delete result;
|
2008-12-15 00:07:21 +02:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
//Get group data
|
|
|
|
|
result = WorldDatabase.PQuery("SELECT `leaderGUID`, `memberGUID`, `dist`, `angle`, `groupAI` FROM `creature_formations` ORDER BY `leaderGUID`");
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
if(!result)
|
2008-12-15 00:07:21 +02:00
|
|
|
{
|
|
|
|
|
sLog.outErrorDb("The table `creature_formations` is empty or corrupted");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
uint32 total_records = result->GetRowCount(), lastLeaderGUID = 0;
|
2009-02-09 08:16:34 -05:00
|
|
|
barGoLink bar( total_records);
|
2008-12-15 00:07:21 +02:00
|
|
|
Field *fields;
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
FormationMember *group_member;
|
|
|
|
|
//Loading data...
|
|
|
|
|
do
|
2008-12-15 00:07:21 +02:00
|
|
|
{
|
|
|
|
|
fields = result->Fetch();
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
bar.step();
|
2008-12-17 20:30:36 +02:00
|
|
|
//Load group member data
|
2009-04-06 13:27:42 -05:00
|
|
|
group_member = new FormationMember;
|
|
|
|
|
group_member->leaderGUID = fields[0].GetUInt32();
|
|
|
|
|
group_member->memberGUID = fields[1].GetUInt32();
|
|
|
|
|
group_member->groupAI = fields[4].GetUInt8();
|
2009-02-09 08:16:34 -05:00
|
|
|
//If creature is group leader we may skip loading of dist/angle
|
|
|
|
|
if(group_member->leaderGUID != group_member->memberGUID)
|
|
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
group_member->follow_dist = fields[2].GetUInt32();
|
|
|
|
|
group_member->follow_angle = fields[3].GetUInt32();
|
2009-04-05 18:56:10 +02:00
|
|
|
}
|
2009-02-09 08:16:34 -05:00
|
|
|
|
2009-04-05 18:56:10 +02:00
|
|
|
CreatureGroupMap[group_member->memberGUID] = group_member;
|
2009-04-06 13:27:42 -05:00
|
|
|
|
|
|
|
|
if(lastLeaderGUID != group_member->leaderGUID)
|
|
|
|
|
lastLeaderGUID = group_member->leaderGUID;
|
|
|
|
|
}
|
2009-02-09 08:16:34 -05:00
|
|
|
while(result->NextRow()) ;
|
|
|
|
|
|
2009-04-03 16:36:20 -06:00
|
|
|
sLog.outString();
|
2009-02-09 08:16:34 -05:00
|
|
|
sLog.outString( ">> Loaded %u creatures in formations", total_records );
|
2009-04-03 16:36:20 -06:00
|
|
|
sLog.outString();
|
2009-02-09 08:16:34 -05:00
|
|
|
//Free some heap
|
|
|
|
|
delete result;
|
2008-12-15 00:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
void CreatureGroup::AddMember(Creature &member)
|
2008-12-15 00:07:21 +02:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
sLog.outDebug("Adding unit GUID: %u.", member.GetGUIDLow());
|
2009-02-09 08:16:34 -05:00
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
uint64 guid = member.GetGUID();
|
2009-02-09 08:16:34 -05:00
|
|
|
//We first search creature member if exist
|
|
|
|
|
if(CreatureGroupMembers.find(guid) != CreatureGroupMembers.end())
|
|
|
|
|
return;
|
|
|
|
|
//Check is that shit is a leader
|
2009-04-06 13:27:42 -05:00
|
|
|
if(member.GetDBTableGUIDLow()==m_groupID)
|
|
|
|
|
{
|
|
|
|
|
sLog.outDebug("Unit GUID: %u is formation leader. Adding group.", member.GetGUIDLow());
|
|
|
|
|
m_leader = &member;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sLog.outDebug("Unit GUID: %u is NOT a formation leader. Set IDLE as default movement.", member.GetGUIDLow());
|
|
|
|
|
member.GetMotionMaster()->MoveIdle(MOTION_SLOT_IDLE);
|
2009-02-09 08:16:34 -05:00
|
|
|
}
|
|
|
|
|
//Add to the club
|
2009-04-06 13:27:42 -05:00
|
|
|
CreatureGroupMembers[guid] = &member;
|
|
|
|
|
member.SetFormation(this);
|
|
|
|
|
sLog.outDebug("Unit GUID: %u added.", member.GetGUID());
|
2008-12-15 00:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreatureGroup::RemoveMember(uint64 guid)
|
|
|
|
|
{
|
2009-02-09 08:16:34 -05:00
|
|
|
if(CreatureGroupMembers.find(guid) != CreatureGroupMembers.end())
|
2009-04-06 13:27:42 -05:00
|
|
|
CreatureGroupMembers.erase(guid);
|
2008-12-15 00:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
void CreatureGroup::MemberHasAttacked(Creature &member)
|
2008-12-15 00:07:21 +02:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
uint8 groupAI = CreatureGroupMap[member.GetDBTableGUIDLow()]->groupAI;
|
2009-02-09 08:16:34 -05:00
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
for(UNORDERED_MAP<uint64, Creature*>::iterator itr = CreatureGroupMembers.begin(); itr != CreatureGroupMembers.end(); ++itr)
|
2009-02-09 08:16:34 -05:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
sLog.outDebug("GROUP ATTACK: group instance id %u calls member instid %u", m_leader->GetInstanceId(), member.GetInstanceId());
|
|
|
|
|
//sLog.outDebug("AI:%u:Group member found: %u, attacked by %s.", groupAI, itr->second->GetGUIDLow(), member.getVictim()->GetName());
|
|
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
if(itr->second)
|
|
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
//Skip one check
|
|
|
|
|
if(itr->second == &member)
|
|
|
|
|
return;
|
|
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
switch(groupAI)
|
|
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
case 0:
|
|
|
|
|
return;
|
|
|
|
|
case 1:
|
|
|
|
|
if(&member != m_leader)
|
2009-02-09 08:16:34 -05:00
|
|
|
return;
|
2009-04-06 13:27:42 -05:00
|
|
|
}
|
2009-02-09 08:16:34 -05:00
|
|
|
//Attack if everything is OK
|
2009-04-06 13:27:42 -05:00
|
|
|
if(itr->second->isAlive() && itr->second->AI() && !itr->second->isInCombat() &&
|
|
|
|
|
!itr->second->getVictim() && member.getVictim() && itr->second->canAttack(member.getVictim()))
|
|
|
|
|
itr->second->AI()->AttackStart(member.getVictim());
|
2009-02-09 08:16:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
2008-12-15 00:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreatureGroup::LeaderMovedInEvade()
|
|
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
for(UNORDERED_MAP<uint64, Creature*>::iterator itr = CreatureGroupMembers.begin(); itr != CreatureGroupMembers.end(); ++itr)
|
2009-02-09 08:16:34 -05:00
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
if(itr->second && itr->second != m_leader && itr->second->isAlive() && !itr->second->isInCombat() && !itr->second->getVictim())
|
|
|
|
|
{
|
|
|
|
|
SetMemberDestination(itr->second);
|
|
|
|
|
sLog.outDebug("GROUP MOVE IN EVADE: Set destination for member GUID %u", itr->second->GetGUIDLow());
|
|
|
|
|
}
|
2009-02-09 08:16:34 -05:00
|
|
|
}
|
2008-12-15 00:07:21 +02:00
|
|
|
}
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
void CreatureGroup::FormDismiss(bool dismiss)
|
|
|
|
|
{
|
|
|
|
|
for(UNORDERED_MAP<uint64, Creature*>::iterator itr = CreatureGroupMembers.begin(); itr != CreatureGroupMembers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if(itr->second && itr->second != m_leader && itr->second->isAlive())
|
|
|
|
|
{
|
|
|
|
|
dismiss ? itr->second->GetMotionMaster()->Initialize() : itr->second->GetMotionMaster()->MoveIdle(MOTION_SLOT_IDLE);
|
|
|
|
|
sLog.outDebug("Set %s movement for member GUID: %u", dismiss ? "default" : "idle", itr->second->GetGUIDLow());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_Formed = !dismiss;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-15 00:07:21 +02:00
|
|
|
void CreatureGroup::SetMemberDestination(Creature *member)
|
|
|
|
|
{
|
2009-04-06 13:27:42 -05:00
|
|
|
if(!member || !m_leader || !member->isAlive() || member->isInCombat() || member->getVictim())
|
2009-02-09 08:16:34 -05:00
|
|
|
return;
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
sLog.outDebug("GROUP MOVE IN EVADE: leader instid %u set destination of member instid %u", m_leader->GetInstanceId(), member->GetInstanceId());
|
|
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
float x,y,z;
|
2009-03-14 11:21:25 +01:00
|
|
|
uint32 lowguid = member->GetDBTableGUIDLow();
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
UNORDERED_MAP<uint32, FormationMember*>::iterator itr2 = CreatureGroupMap.find(lowguid);
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
if(itr2 != CreatureGroupMap.end() && m_leader->GetMotionMaster()->GetDestination(x,y,z))
|
|
|
|
|
{
|
2009-02-09 08:16:34 -05:00
|
|
|
float dist, angle, pathangle;
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
angle = itr2->second->follow_angle;
|
|
|
|
|
dist = itr2->second->follow_dist;
|
|
|
|
|
pathangle = atan2(m_leader->GetPositionY()-y, m_leader->GetPositionX()-x);
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
x = x+cos(angle+pathangle)*dist;
|
|
|
|
|
y = y+sin(angle+pathangle)*dist;
|
2009-02-04 10:33:18 -06:00
|
|
|
|
2009-02-09 08:16:34 -05:00
|
|
|
if(member->GetDistance(m_leader) < dist+MAX_DESYNC)
|
|
|
|
|
member->SetUnitMovementFlags(m_leader->GetUnitMovementFlags());
|
|
|
|
|
else
|
|
|
|
|
member->RemoveUnitMovementFlag(MOVEMENTFLAG_WALK_MODE);
|
2008-12-15 00:07:21 +02:00
|
|
|
|
2009-04-06 13:27:42 -05:00
|
|
|
member->GetMotionMaster()->Clear();
|
2009-02-09 08:16:34 -05:00
|
|
|
member->GetMotionMaster()->MovePoint(0, x, y, z);
|
2009-04-06 13:27:42 -05:00
|
|
|
member->SetHomePosition(x, y, z, pathangle);
|
2009-02-09 08:16:34 -05:00
|
|
|
}
|
2008-12-31 11:33:25 -06:00
|
|
|
}
|