1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
*
* 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"
#include "CreatureAI.h"
#define MAX_DESYNC 5.0f
INSTANTIATE_SINGLETON_1(CreatureGroupManager);
CreatureGroupHolderType CreatureGroupHolder;
CreatureGroupInfoType CreatureGroupMap;
void CreatureGroupManager::AddCreatureToGroup(uint32 group_id, Creature *member)
{
CreatureGroupHolderType::iterator cgroup_data = CreatureGroupHolder.find(group_id);
//Add member to an existing group
if(cgroup_data != CreatureGroupHolder.end())
{
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;
}
}
}
//Create new group
sLog.outDebug("Group not found: %u. Creating new group.", group_id);
CreatureGroup* cgroup = new CreatureGroup(group_id, member->GetInstanceId());
CreatureGroupHolder.insert(std::make_pair(group_id, cgroup));
cgroup->AddMember(member);
}
void CreatureGroupManager::RemoveCreatureFromGroup(CreatureGroup *formation, Creature *member)
{
sLog.outDebug("Deleting member pointer to GUID: %u from group %u", formation->GetId(), member->GetDBTableGUIDLow());
formation->RemoveMember(member);
if(formation->isEmpty())
{
uint32 id = formation->GetId();
typedef std::multimap<uint32, CreatureGroup *>::iterator multiplegroup;
std::pair<multiplegroup, multiplegroup> range = CreatureGroupHolder.equal_range(id);
for(multiplegroup i = range.first; i != range.second; ++i)
{
if(i->second == formation)
{
sLog.outDebug("Deleting group with InstanceID %u", i->second->getInstanceID());
CreatureGroupHolder.erase(i);
delete formation;
}
}
}
}
void CreatureGroupManager::LoadCreatureFormations()
{
//Clear existing map
CreatureGroupMap.clear();
//Check Integrity of the table
QueryResult *result = WorldDatabase.PQuery("SELECT MAX(`leaderGUID`) FROM `creature_formations`");
if(!result)
{
sLog.outErrorDb(" ...an error occured while loading the table `creature_formations` ( maybe it doesn't exist ?)\n");
return;
}
delete result;
//Get group data
result = WorldDatabase.PQuery("SELECT `leaderGUID`, `memberGUID`, `dist`, `angle`, `groupAI` FROM `creature_formations` ORDER BY `leaderGUID`");
if(!result)
{
sLog.outErrorDb("The table `creature_formations` is empty or corrupted");
return;
}
uint32 total_records = result->GetRowCount();
barGoLink bar( total_records);
Field *fields;
FormationInfo *group_member;
//Loading data...
do
{
fields = result->Fetch();
bar.step();
//Load group member data
group_member = new FormationInfo;
group_member->leaderGUID = fields[0].GetUInt32();
uint32 memberGUID = fields[1].GetUInt32();
group_member->groupAI = fields[4].GetUInt8();
//If creature is group leader we may skip loading of dist/angle
if(group_member->leaderGUID != memberGUID)
{
group_member->follow_dist = fields[2].GetUInt32();
group_member->follow_angle = fields[3].GetUInt32();
}
// check data correctness
{
QueryResult* result = WorldDatabase.PQuery("SELECT guid FROM creature WHERE guid = %u", group_member->leaderGUID);
if(!result)
{
sLog.outErrorDb("creature_formations table leader guid %u incorrect (not exist)", group_member->leaderGUID);
continue;
}
result = WorldDatabase.PQuery("SELECT guid FROM creature WHERE guid = %u", memberGUID);
if(!result)
{
sLog.outErrorDb("creature_formations table member guid %u incorrect (not exist)", memberGUID);
continue;
}
}
CreatureGroupMap[memberGUID] = group_member;
}
while(result->NextRow()) ;
sLog.outString();
sLog.outString( ">> Loaded %u creatures in formations", total_records );
sLog.outString();
//Free some heap
delete result;
}
void CreatureGroup::AddMember(Creature *member)
{
sLog.outDebug("CreatureGroup::AddMember: Adding unit GUIDLow: %u.", member->GetGUIDLow());
//Check if it is a leader
if(member->GetDBTableGUIDLow() == m_groupID)
{
sLog.outDebug("Unit GUID: %u is formation leader. Adding group.", member->GetGUIDLow());
m_leader = member;
}
m_members[member] = CreatureGroupMap.find(member->GetDBTableGUIDLow())->second;
member->SetFormation(this);
}
void CreatureGroup::RemoveMember(Creature *member)
{
if(m_leader == member)
m_leader = NULL;
m_members.erase(member);
member->SetFormation(NULL);
}
void CreatureGroup::MemberAttackStart(Creature *member, Unit *target)
{
uint8 groupAI = CreatureGroupMap[member->GetDBTableGUIDLow()]->groupAI;
if(!groupAI)
return;
if(groupAI == 1 && member != m_leader)
return;
for(CreatureGroupMemberType::iterator itr = m_members.begin(); itr != m_members.end(); ++itr)
{
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());
//Skip one check
if(itr->first == member)
continue;
if(!itr->first->isAlive())
continue;
if(itr->first->getVictim())
continue;
if(itr->first->canAttack(target))
itr->first->AI()->AttackStart(target);
}
}
void CreatureGroup::FormationReset(bool dismiss)
{
for(CreatureGroupMemberType::iterator itr = m_members.begin(); itr != m_members.end(); ++itr)
{
if(itr->first != m_leader && itr->first->isAlive())
{
if(dismiss)
itr->first->GetMotionMaster()->Initialize();
else
itr->first->GetMotionMaster()->MoveIdle(MOTION_SLOT_IDLE);
sLog.outDebug("Set %s movement for member GUID: %u", dismiss ? "default" : "idle", itr->first->GetGUIDLow());
}
}
m_Formed = !dismiss;
}
void CreatureGroup::LeaderMoveTo(float x, float y, float z)
{
if(!m_leader)
return;
float pathangle = atan2(m_leader->GetPositionY() - y, m_leader->GetPositionX() - x);
for(CreatureGroupMemberType::iterator itr = m_members.begin(); itr != m_members.end(); ++itr)
{
Creature *member = itr->first;
if(member == m_leader || !member->isAlive() || member->getVictim())
continue;
float angle = itr->second->follow_angle;
float dist = itr->second->follow_dist;
float dx = x + cos(angle + pathangle) * dist;
float dy = y + sin(angle + pathangle) * dist;
float dz;
member->UpdateGroundPositionZ(dx, dy, dz);
if(member->GetDistance(m_leader) < dist + MAX_DESYNC)
member->SetUnitMovementFlags(m_leader->GetUnitMovementFlags());
else
member->RemoveUnitMovementFlag(MOVEMENTFLAG_WALK_MODE);
member->GetMotionMaster()->MovePoint(0, dx, dy, dz);
member->SetHomePosition(dx, dy, dz, pathangle);
}
}
|