OregonCore  revision fb2a440-git
Your Favourite TBC server
Corpse.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "Common.h"
19 #include "Corpse.h"
20 #include "Player.h"
21 #include "UpdateMask.h"
22 #include "MapManager.h"
23 #include "ObjectAccessor.h"
24 #include "Database/DatabaseEnv.h"
25 #include "WorldSession.h"
26 #include "WorldPacket.h"
27 
28 Corpse::Corpse(CorpseType type) : WorldObject(type != CORPSE_BONES), m_type(type)
29 {
32  // 2.3.2 - 0x58
34 
36 
37  m_time = time(NULL);
38 
39  lootForBody = false;
40 }
41 
43 {
44 }
45 
47 {
48  // Register the corpse for guid lookup
49  if (!IsInWorld())
51 
53 }
54 
56 {
57  // Remove the corpse from the accessor
58  if (IsInWorld())
60 
62 }
63 
64 bool Corpse::Create(uint32 guidlow, Map* map)
65 {
66  SetMap(map);
67  Object::_Create(guidlow, 0, HIGHGUID_CORPSE);
68  return true;
69 }
70 
71 bool Corpse::Create(uint32 guidlow, Player* owner, uint32 /*mapid*/, float x, float y, float z, float ang)
72 {
73  ASSERT(owner);
74 
75  Relocate(x, y, z, ang);
76 
77  if (!IsPositionValid())
78  {
79  sLog.outError("Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
80  guidlow, owner->GetName(), x, y);
81  return false;
82  }
83 
84  //we need to assign owner's map for corpse
85  //in other way we will get a crash in Corpse::SaveToDB()
86  SetMap(owner->GetMap());
87 
89 
90  SetObjectScale(1);
96 
98 
99  return true;
100 }
101 
103 {
104  // prevent DB data inconsistance problems and duplicates
106  DeleteFromDB();
107 
108  std::ostringstream ss;
109  ss << "INSERT INTO corpse (guid,player,position_x,position_y,position_z,orientation,zone,map,displayId,itemCache,bytes1,bytes2,guild,flags,dynFlags,time,corpse_type,instance, phaseMask) VALUES ("
110  << GetGUIDLow() << ", "
111  << GUID_LOPART(GetOwnerGUID()) << ", "
112  << GetPositionX() << ", "
113  << GetPositionY() << ", "
114  << GetPositionZ() << ", "
115  << GetOrientation() << ", "
116  << GetZoneId() << ", "
117  << GetMapId() << ", "
119  for (uint16 i = 0; i < EQUIPMENT_SLOT_END; ++i)
120  ss << GetUInt32Value(CORPSE_FIELD_ITEM + i) << " ";
121  ss << "', "
127  << uint64(m_time) << ", "
128  << uint32(GetType()) << ", "
129  << int(GetInstanceId()) << ", "
130  << uint64(GetPhaseMask()) << ")";
131  CharacterDatabase.Execute(ss.str().c_str());
133 }
134 
136 {
138  Corpse* corpse = ObjectAccessor::GetCorpse(*this, GetGUID());
139 
140  if (!corpse)
141  {
142  sLog.outError("Bones %u not found in world.", GetGUIDLow());
143  return;
144  }
145 
147 }
148 
150 {
151  if (GetType() == CORPSE_BONES)
152  // only specific bones
153  CharacterDatabase.PExecute("DELETE FROM corpse WHERE guid = '%d'", GetGUIDLow());
154  else
155  // all corpses (not bones)
156  CharacterDatabase.PExecute("DELETE FROM corpse WHERE player = '%d' AND corpse_type <> '0'", GUID_LOPART(GetOwnerGUID()));
157 }
158 
160 {
161  // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
162  //SELECT position_x, position_y, position_z, orientation, map, displayId, itemCache, bytes1, bytes2, guild, flags, dynFlags, time, corpse_type, instance, guid, player FROM corpse WHERE corpse_type <> 0
163  float positionX = fields[0].GetFloat();
164  float positionY = fields[1].GetFloat();
165  float positionZ = fields[2].GetFloat();
166  float ort = fields[3].GetFloat();
167  uint32 mapid = fields[4].GetUInt32();
168 
169  // Initialize the datastores for this object
171 
172  SetObjectScale(1.0f);
173  SetUInt32Value(CORPSE_FIELD_DISPLAY_ID, fields[5].GetUInt32());
175  SetUInt32Value(CORPSE_FIELD_BYTES_1, fields[7].GetUInt32());
176  SetUInt32Value(CORPSE_FIELD_BYTES_2, fields[8].GetUInt32());
177  SetUInt32Value(CORPSE_FIELD_GUILD, fields[9].GetUInt32());
178  SetUInt32Value(CORPSE_FIELD_FLAGS, fields[10].GetUInt32());
179  SetUInt32Value(CORPSE_FIELD_DYNAMIC_FLAGS, fields[11].GetUInt32());
180  SetUInt64Value(CORPSE_FIELD_OWNER, MAKE_NEW_GUID(fields[16].GetUInt32(), 0, HIGHGUID_PLAYER));
181 
182  m_time = time_t(fields[12].GetUInt64());
183 
184  uint32 instanceid = fields[14].GetUInt32();
185 
186  uint32 phaseMask = fields[15].GetUInt32();
187 
188  // place
189  SetLocationInstanceId(instanceid);
190  SetLocationMapId(mapid);
191  SetPhaseMask(phaseMask, false);
192  Relocate(positionX, positionY, positionZ, ort);
193 
194  if (!IsPositionValid())
195  {
196  sLog.outError("Corpse (guidlow %d, owner %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
198  return false;
199  }
200 
202  return true;
203 }
204 
205 bool Corpse::IsExpired(time_t t) const
206 {
207  if (m_type == CORPSE_BONES)
208  return m_time < t - 60 * MINUTE;
209  else
210  return m_time < t - 3 * DAY;
211 }
212 
void _Create(uint32 guidlow, HighGuid guidhigh, uint32 phaseMask)
Definition: Object.cpp:1162
Definition: Corpse.h:48
void DeleteFromDB()
Definition: Corpse.cpp:149
const uint32 & GetUInt32Value(uint16 index) const
Definition: Object.h:234
virtual void SetPhaseMask(uint32 newPhaseMask, bool update)
Definition: Object.cpp:2536
#define GUID_LOPART(x)
Definition: ObjectGuid.h:110
Map * GetMap() const
Definition: Object.h:841
bool BeginTransaction()
Definition: Database.cpp:533
void RemoveFromWorld() override
Definition: Corpse.cpp:55
Definition: Field.h:24
void SetObjectScale(float scale)
Definition: Object.h:205
bool Execute(const char *sql)
Definition: Database.cpp:420
uint32 GetZoneId() const
Definition: Object.cpp:1179
virtual void SetMap(Map *map)
Definition: Object.cpp:1954
void _LoadIntoDataField(const char *data, uint32 startOffset, uint32 count)
Definition: Object.cpp:721
void AddObject(T *object)
#define sLog
Log class singleton.
Definition: Log.h:187
void SetUInt32Value(uint16 index, uint32 value)
Definition: Object.cpp:779
CorpseType
Definition: Corpse.h:26
void SetLocationMapId(uint32 _mapId)
Definition: Object.h:939
uint32 GetGUIDLow() const
Definition: Object.h:166
bool lootForBody
Definition: Corpse.h:95
CorpseType m_type
Definition: Corpse.h:133
uint16 m_objectType
Definition: Object.h:424
bool CommitTransaction()
Definition: Database.cpp:551
~Corpse() override
Definition: Corpse.cpp:42
Definition: Common.h:181
virtual void RemoveFromWorld()
Definition: Object.h:151
CorpseType GetType() const
Definition: Corpse.h:79
uint8 m_objectTypeId
Definition: Object.h:426
Definition: Common.h:179
void AddToWorld() override
Definition: Corpse.cpp:46
void _Create(uint32 guidlow, uint32 entry, HighGuid guidhigh)
Definition: Object.cpp:125
float GetOrientation() const
Definition: Position.h:100
void Relocate(float x, float y)
Definition: Position.h:65
const bool & IsInWorld() const
Definition: Object.h:135
void AddObjectToRemoveList()
Definition: Object.cpp:1990
uint32 GetPhaseMask() const
Definition: Object.h:694
bool Create(uint32 guidlow, Map *map)
Definition: Corpse.cpp:64
float GetPositionY() const
Definition: Position.h:98
uint8 m_updateFlag
Definition: Object.h:427
#define MAKE_NEW_GUID(l, e, h)
Definition: ObjectGuid.h:80
uint32 GetInstanceId() const
Definition: Object.h:688
float GetPositionZ() const
Definition: Position.h:99
uint32 GetMapId() const
Definition: Object.h:591
bool IsExpired(time_t t) const
Definition: Corpse.cpp:205
bool PExecute(const char *format,...) ATTR_PRINTF(2
Definition: Database.cpp:441
Definition: Map.h:266
void SaveToDB()
Definition: Corpse.cpp:102
void SetUInt64Value(uint16 index, const uint64 &value)
Definition: Object.cpp:798
const char * GetName() const
Definition: Object.h:704
void RemoveObject(T *object)
ACE_UINT64 uint64
Definition: Define.h:70
GridCoord _gridCoord
Definition: Corpse.h:135
void DeleteBonesFromWorld()
Definition: Corpse.cpp:135
void SetLocationInstanceId(uint32 _instanceId)
Definition: Object.h:943
virtual void AddToWorld()
Definition: Object.h:139
uint64 const & GetOwnerGUID() const
Definition: Corpse.h:66
time_t m_time
Definition: Corpse.h:134
void SetFloatValue(uint16 index, float value)
Definition: Object.cpp:859
uint16 m_valuesCount
Definition: Object.h:438
#define ASSERT
Definition: Errors.h:29
bool IsPositionValid() const
Definition: Position.cpp:40
Corpse(CorpseType type=CORPSE_BONES)
Definition: Corpse.cpp:28
DatabaseType CharacterDatabase
Accessor to the character database.
Definition: Main.cpp:54
ACE_UINT16 uint16
Definition: Define.h:72
ACE_UINT32 uint32
Definition: Define.h:71
float GetPositionX() const
Definition: Position.h:97
static Corpse * GetCorpse(WorldObject const &u, uint64 guid)
Definition: Player.h:922
bool LoadCorpseFromDB(uint32 guid, Field *fields)
Definition: Corpse.cpp:159
const uint64 & GetGUID() const
Definition: Object.h:162
GridCoord ComputeGridCoord(float x, float y)
Definition: GridDefines.h:162