forked from mangoszero/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorpse.cpp
More file actions
287 lines (237 loc) · 9.58 KB
/
Corpse.cpp
File metadata and controls
287 lines (237 loc) · 9.58 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* MaNGOS is a full featured server for World of Warcraft, supporting
* the following clients: 1.12.x, 2.4.3, 3.3.5a, 4.3.4a and 5.4.8
*
* Copyright (C) 2005-2019 MaNGOS project <https://getmangos.eu>
*
* 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
*
* World of Warcraft, and all World of Warcraft or Warcraft art, images,
* and lore are copyrighted by Blizzard Entertainment, Inc.
*/
#include "Corpse.h"
#include "Player.h"
#include "ObjectAccessor.h"
#include "ObjectGuid.h"
#include "Database/DatabaseEnv.h"
#include "World.h"
#include "ObjectMgr.h"
Corpse::Corpse(CorpseType type) : WorldObject(),
loot(this),
lootRecipient(NULL),
lootForBody(false)
{
m_objectType |= TYPEMASK_CORPSE;
m_objectTypeId = TYPEID_CORPSE;
m_updateFlag = (UPDATEFLAG_TRANSPORT | UPDATEFLAG_ALL | UPDATEFLAG_HAS_POSITION);
m_valuesCount = CORPSE_END;
m_type = type;
m_time = time(NULL);
}
Corpse::~Corpse()
{
}
void Corpse::AddToWorld()
{
///- Register the corpse for guid lookup
if (!IsInWorld())
{ sObjectAccessor.AddObject(this); }
Object::AddToWorld();
}
void Corpse::RemoveFromWorld()
{
///- Remove the corpse from the accessor
if (IsInWorld())
{ sObjectAccessor.RemoveObject(this); }
Object::RemoveFromWorld();
}
bool Corpse::Create(uint32 guidlow)
{
Object::_Create(guidlow, 0, HIGHGUID_CORPSE);
return true;
}
bool Corpse::Create(uint32 guidlow, Player* owner)
{
MANGOS_ASSERT(owner);
WorldObject::_Create(guidlow, HIGHGUID_CORPSE);
Relocate(owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->GetOrientation());
// we need to assign owner's map for corpse
// in other way we will get a crash in Corpse::SaveToDB()
SetMap(owner->GetMap());
if (!IsPositionValid())
{
sLog.outError("Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
guidlow, owner->GetName(), owner->GetPositionX(), owner->GetPositionY());
return false;
}
SetObjectScale(DEFAULT_OBJECT_SCALE);
SetFloatValue(CORPSE_FIELD_POS_X, GetPositionX());
SetFloatValue(CORPSE_FIELD_POS_Y, GetPositionY());
SetFloatValue(CORPSE_FIELD_POS_Z, GetPositionZ());
SetFloatValue(CORPSE_FIELD_FACING, GetOrientation());
SetGuidValue(CORPSE_FIELD_OWNER, owner->GetObjectGuid());
m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
return true;
}
void Corpse::SaveToDB()
{
// bones should not be saved to DB (would be deleted on startup anyway)
MANGOS_ASSERT(GetType() != CORPSE_BONES);
// prevent DB data inconsistence problems and duplicates
CharacterDatabase.BeginTransaction();
DeleteFromDB();
std::ostringstream ss;
ss << "INSERT INTO corpse (guid,player,position_x,position_y,position_z,orientation,map,time,corpse_type,instance) VALUES ("
<< GetGUIDLow() << ", "
<< GetOwnerGuid().GetCounter() << ", "
<< GetPositionX() << ", "
<< GetPositionY() << ", "
<< GetPositionZ() << ", "
<< GetOrientation() << ", "
<< GetMapId() << ", "
<< uint64(m_time) << ", "
<< uint32(GetType()) << ", "
<< int(GetInstanceId()) << ")";
CharacterDatabase.Execute(ss.str().c_str());
CharacterDatabase.CommitTransaction();
}
void Corpse::DeleteBonesFromWorld()
{
MANGOS_ASSERT(GetType() == CORPSE_BONES);
Corpse* corpse = GetMap()->GetCorpse(GetObjectGuid());
if (!corpse)
{
sLog.outError("Bones %u not found in world.", GetGUIDLow());
return;
}
AddObjectToRemoveList();
}
void Corpse::DeleteFromDB()
{
// bones should not be saved to DB (would be deleted on startup anyway)
MANGOS_ASSERT(GetType() != CORPSE_BONES);
// all corpses (not bones)
static SqlStatementID id;
SqlStatement stmt = CharacterDatabase.CreateStatement(id, "DELETE FROM corpse WHERE player = ? AND corpse_type <> '0'");
stmt.PExecute(GetOwnerGuid().GetCounter());
}
bool Corpse::LoadFromDB(uint32 lowguid, Field* fields)
{
//// 0 1 2 3 4 5 6
// QueryResult *result = CharacterDatabase.Query("SELECT corpse.guid, player, corpse.position_x, corpse.position_y, corpse.position_z, corpse.orientation, corpse.map,"
//// 7 8 9 10 11 12 13 14 15 16 17
// "time, corpse_type, instance, gender, race, class, playerBytes, playerBytes2, equipmentCache, guildId, playerFlags FROM corpse"
uint32 playerLowGuid = fields[1].GetUInt32();
float positionX = fields[2].GetFloat();
float positionY = fields[3].GetFloat();
float positionZ = fields[4].GetFloat();
float orientation = fields[5].GetFloat();
uint32 mapid = fields[6].GetUInt32();
Object::_Create(lowguid, 0, HIGHGUID_CORPSE);
m_time = time_t(fields[7].GetUInt64());
m_type = CorpseType(fields[8].GetUInt32());
if (m_type >= MAX_CORPSE_TYPE)
{
sLog.outError("%s Owner %s have wrong corpse type (%i), not load.", GetGuidStr().c_str(), GetOwnerGuid().GetString().c_str(), m_type);
return false;
}
uint32 instanceid = fields[9].GetUInt32();
uint8 gender = fields[10].GetUInt8();
uint8 race = fields[11].GetUInt8();
uint8 _class = fields[12].GetUInt8();
uint32 playerBytes = fields[13].GetUInt32();
uint32 playerBytes2 = fields[14].GetUInt32();
uint32 guildId = fields[16].GetUInt32();
uint32 playerFlags = fields[17].GetUInt32();
ObjectGuid guid = ObjectGuid(HIGHGUID_CORPSE, lowguid);
ObjectGuid playerGuid = ObjectGuid(HIGHGUID_PLAYER, playerLowGuid);
// overwrite possible wrong/corrupted guid
SetGuidValue(OBJECT_FIELD_GUID, guid);
SetGuidValue(CORPSE_FIELD_OWNER, playerGuid);
SetObjectScale(DEFAULT_OBJECT_SCALE);
PlayerInfo const* info = sObjectMgr.GetPlayerInfo(race, _class);
if (!info)
{
sLog.outError("Player %u has incorrect race/class pair.", GetGUIDLow());
return false;
}
SetUInt32Value(CORPSE_FIELD_DISPLAY_ID, gender == GENDER_FEMALE ? info->displayId_f : info->displayId_m);
// Load equipment
Tokens data = StrSplit(fields[15].GetCppString(), " ");
for (uint8 slot = 0; slot < EQUIPMENT_SLOT_END; ++slot)
{
uint32 visualbase = slot * 2;
uint32 item_id = GetUInt32ValueFromArray(data, visualbase);
const ItemPrototype* proto = ObjectMgr::GetItemPrototype(item_id);
if (!proto)
{
SetUInt32Value(CORPSE_FIELD_ITEM + slot, 0);
continue;
}
SetUInt32Value(CORPSE_FIELD_ITEM + slot, proto->DisplayInfoID | (proto->InventoryType << 24));
}
uint8 skin = (uint8)(playerBytes);
uint8 face = (uint8)(playerBytes >> 8);
uint8 hairstyle = (uint8)(playerBytes >> 16);
uint8 haircolor = (uint8)(playerBytes >> 24);
uint8 facialhair = (uint8)(playerBytes2);
SetUInt32Value(CORPSE_FIELD_BYTES_1, ((0x00) | (race << 8) | (gender << 16) | (skin << 24)));
SetUInt32Value(CORPSE_FIELD_BYTES_2, ((face) | (hairstyle << 8) | (haircolor << 16) | (facialhair << 24)));
SetUInt32Value(CORPSE_FIELD_GUILD, guildId);
uint32 flags = CORPSE_FLAG_UNK2;
if (playerFlags & PLAYER_FLAGS_HIDE_HELM)
{ flags |= CORPSE_FLAG_HIDE_HELM; }
if (playerFlags & PLAYER_FLAGS_HIDE_CLOAK)
{ flags |= CORPSE_FLAG_HIDE_CLOAK; }
SetUInt32Value(CORPSE_FIELD_FLAGS, flags);
// no need to mark corpse as lootable, because corpses are not saved in battle grounds
// place
SetLocationInstanceId(instanceid);
SetLocationMapId(mapid);
Relocate(positionX, positionY, positionZ, orientation);
if (!IsPositionValid())
{
sLog.outError("%s Owner %s not created. Suggested coordinates isn't valid (X: %f Y: %f)",
GetGuidStr().c_str(), GetOwnerGuid().GetString().c_str(), GetPositionX(), GetPositionY());
return false;
}
m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
return true;
}
bool Corpse::IsVisibleForInState(Player const* u, WorldObject const* viewPoint, bool inVisibleList) const
{
return IsInWorld() && u->IsInWorld() && IsWithinDistInMap(viewPoint, GetMap()->GetVisibilityDistance() + (inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false);
}
bool Corpse::IsHostileTo(Unit const* unit) const
{
if (Player* owner = sObjectMgr.GetPlayer(GetOwnerGuid()))
{ return owner->IsHostileTo(unit); }
else
{ return false; }
}
bool Corpse::IsFriendlyTo(Unit const* unit) const
{
if (Player* owner = sObjectMgr.GetPlayer(GetOwnerGuid()))
{ return owner->IsFriendlyTo(unit); }
else
{ return true; }
}
bool Corpse::IsExpired(time_t t) const
{
if (m_type == CORPSE_BONES)
{ return m_time < t - 60 * MINUTE; }
else
{ return m_time < t - 3 * DAY; }
}