1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #include "Common.h" |
---|
22 | #include "Corpse.h" |
---|
23 | #include "Player.h" |
---|
24 | #include "UpdateMask.h" |
---|
25 | #include "MapManager.h" |
---|
26 | #include "ObjectAccessor.h" |
---|
27 | #include "Database/DatabaseEnv.h" |
---|
28 | #include "Opcodes.h" |
---|
29 | #include "WorldSession.h" |
---|
30 | #include "WorldPacket.h" |
---|
31 | #include "GossipDef.h" |
---|
32 | #include "World.h" |
---|
33 | |
---|
34 | Corpse::Corpse(CorpseType type) : WorldObject() |
---|
35 | { |
---|
36 | m_objectType |= TYPEMASK_CORPSE; |
---|
37 | m_objectTypeId = TYPEID_CORPSE; |
---|
38 | // 2.3.2 - 0x58 |
---|
39 | m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HASPOSITION); |
---|
40 | |
---|
41 | m_valuesCount = CORPSE_END; |
---|
42 | |
---|
43 | m_type = type; |
---|
44 | |
---|
45 | m_time = time(NULL); |
---|
46 | |
---|
47 | lootForBody = false; |
---|
48 | } |
---|
49 | |
---|
50 | Corpse::~Corpse() |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | void Corpse::AddToWorld() |
---|
55 | { |
---|
56 | ///- Register the corpse for guid lookup |
---|
57 | if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this); |
---|
58 | Object::AddToWorld(); |
---|
59 | } |
---|
60 | |
---|
61 | void Corpse::RemoveFromWorld() |
---|
62 | { |
---|
63 | ///- Remove the corpse from the accessor |
---|
64 | if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this); |
---|
65 | Object::RemoveFromWorld(); |
---|
66 | } |
---|
67 | |
---|
68 | bool Corpse::Create( uint32 guidlow ) |
---|
69 | { |
---|
70 | Object::_Create(guidlow, 0, HIGHGUID_CORPSE); |
---|
71 | return true; |
---|
72 | } |
---|
73 | |
---|
74 | bool Corpse::Create( uint32 guidlow, Player *owner, uint32 mapid, float x, float y, float z, float ang ) |
---|
75 | { |
---|
76 | SetInstanceId(owner->GetInstanceId()); |
---|
77 | |
---|
78 | WorldObject::_Create(guidlow, HIGHGUID_CORPSE, mapid); |
---|
79 | |
---|
80 | Relocate(x,y,z,ang); |
---|
81 | |
---|
82 | if(!IsPositionValid()) |
---|
83 | { |
---|
84 | sLog.outError("ERROR: Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)", |
---|
85 | guidlow,owner->GetName(),x,y); |
---|
86 | return false; |
---|
87 | } |
---|
88 | |
---|
89 | SetFloatValue( OBJECT_FIELD_SCALE_X, 1 ); |
---|
90 | SetFloatValue( CORPSE_FIELD_POS_X, x ); |
---|
91 | SetFloatValue( CORPSE_FIELD_POS_Y, y ); |
---|
92 | SetFloatValue( CORPSE_FIELD_POS_Z, z ); |
---|
93 | SetFloatValue( CORPSE_FIELD_FACING, ang ); |
---|
94 | SetUInt64Value( CORPSE_FIELD_OWNER, owner->GetGUID() ); |
---|
95 | |
---|
96 | m_grid = Trinity::ComputeGridPair(GetPositionX(), GetPositionY()); |
---|
97 | |
---|
98 | return true; |
---|
99 | } |
---|
100 | |
---|
101 | void Corpse::SaveToDB() |
---|
102 | { |
---|
103 | // prevent DB data inconsistance problems and duplicates |
---|
104 | CharacterDatabase.BeginTransaction(); |
---|
105 | DeleteFromDB(); |
---|
106 | |
---|
107 | std::ostringstream ss; |
---|
108 | ss << "INSERT INTO corpse (guid,player,position_x,position_y,position_z,orientation,zone,map,data,time,corpse_type,instance) VALUES (" |
---|
109 | << GetGUIDLow() << ", " << GUID_LOPART(GetOwnerGUID()) << ", " << GetPositionX() << ", " << GetPositionY() << ", " << GetPositionZ() << ", " |
---|
110 | << GetOrientation() << ", " << GetZoneId() << ", " << GetMapId() << ", '"; |
---|
111 | for(uint16 i = 0; i < m_valuesCount; i++ ) |
---|
112 | ss << GetUInt32Value(i) << " "; |
---|
113 | ss << "'," << uint64(m_time) <<", " << uint32(GetType()) << ", " << int(GetInstanceId()) << ")"; |
---|
114 | CharacterDatabase.Execute( ss.str().c_str() ); |
---|
115 | CharacterDatabase.CommitTransaction(); |
---|
116 | } |
---|
117 | |
---|
118 | void Corpse::DeleteBonesFromWorld() |
---|
119 | { |
---|
120 | assert(GetType()==CORPSE_BONES); |
---|
121 | Corpse* corpse = ObjectAccessor::GetCorpse(*this, GetGUID()); |
---|
122 | |
---|
123 | if (!corpse) |
---|
124 | { |
---|
125 | sLog.outError("Bones %u not found in world.", GetGUIDLow()); |
---|
126 | return; |
---|
127 | } |
---|
128 | |
---|
129 | AddObjectToRemoveList(); |
---|
130 | } |
---|
131 | |
---|
132 | void Corpse::DeleteFromDB() |
---|
133 | { |
---|
134 | if(GetType() == CORPSE_BONES) |
---|
135 | // only specific bones |
---|
136 | CharacterDatabase.PExecute("DELETE FROM corpse WHERE guid = '%d'", GetGUIDLow()); |
---|
137 | else |
---|
138 | // all corpses (not bones) |
---|
139 | CharacterDatabase.PExecute("DELETE FROM corpse WHERE player = '%d' AND corpse_type <> '0'", GUID_LOPART(GetOwnerGUID())); |
---|
140 | } |
---|
141 | |
---|
142 | bool Corpse::LoadFromDB(uint32 guid, QueryResult *result, uint32 InstanceId) |
---|
143 | { |
---|
144 | bool external = (result != NULL); |
---|
145 | if (!external) |
---|
146 | // 0 1 2 3 4 5 6 7 8 |
---|
147 | result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance FROM corpse WHERE guid = '%u'",guid); |
---|
148 | |
---|
149 | if( ! result ) |
---|
150 | { |
---|
151 | sLog.outError("ERROR: Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid); |
---|
152 | return false; |
---|
153 | } |
---|
154 | |
---|
155 | Field *fields = result->Fetch(); |
---|
156 | |
---|
157 | if(!LoadFromDB(guid,fields)) |
---|
158 | { |
---|
159 | if (!external) delete result; |
---|
160 | return false; |
---|
161 | } |
---|
162 | |
---|
163 | if (!external) delete result; |
---|
164 | return true; |
---|
165 | } |
---|
166 | |
---|
167 | bool Corpse::LoadFromDB(uint32 guid, Field *fields) |
---|
168 | { |
---|
169 | // 0 1 2 3 4 5 6 7 8 |
---|
170 | //result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance FROM corpse WHERE guid = '%u'",guid); |
---|
171 | float positionX = fields[0].GetFloat(); |
---|
172 | float positionY = fields[1].GetFloat(); |
---|
173 | float positionZ = fields[2].GetFloat(); |
---|
174 | float ort = fields[3].GetFloat(); |
---|
175 | uint32 mapid = fields[4].GetUInt32(); |
---|
176 | |
---|
177 | if(!LoadValues( fields[5].GetString() )) |
---|
178 | { |
---|
179 | sLog.outError("ERROR: Corpse #%d have broken data in `data` field. Can't be loaded.",guid); |
---|
180 | return false; |
---|
181 | } |
---|
182 | |
---|
183 | m_time = time_t(fields[6].GetUInt64()); |
---|
184 | m_type = CorpseType(fields[7].GetUInt32()); |
---|
185 | if(m_type >= MAX_CORPSE_TYPE) |
---|
186 | { |
---|
187 | sLog.outError("ERROR: Corpse (guidlow %d, owner %d) have wrong corpse type, not load.",GetGUIDLow(),GUID_LOPART(GetOwnerGUID())); |
---|
188 | return false; |
---|
189 | } |
---|
190 | uint32 instanceid = fields[8].GetUInt32(); |
---|
191 | |
---|
192 | // overwrite possible wrong/corrupted guid |
---|
193 | SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid, 0, HIGHGUID_CORPSE)); |
---|
194 | |
---|
195 | // place |
---|
196 | SetInstanceId(instanceid); |
---|
197 | SetMapId(mapid); |
---|
198 | Relocate(positionX,positionY,positionZ,ort); |
---|
199 | |
---|
200 | if(!IsPositionValid()) |
---|
201 | { |
---|
202 | sLog.outError("ERROR: Corpse (guidlow %d, owner %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)", |
---|
203 | GetGUIDLow(),GUID_LOPART(GetOwnerGUID()),GetPositionX(),GetPositionY()); |
---|
204 | return false; |
---|
205 | } |
---|
206 | |
---|
207 | m_grid = Trinity::ComputeGridPair(GetPositionX(), GetPositionY()); |
---|
208 | |
---|
209 | return true; |
---|
210 | } |
---|
211 | |
---|
212 | bool Corpse::isVisibleForInState(Player const* u, bool inVisibleList) const |
---|
213 | { |
---|
214 | return IsInWorld() && u->IsInWorld() && IsWithinDistInMap(u,World::GetMaxVisibleDistanceForObject()+(inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f)); |
---|
215 | } |
---|