[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / Corpse.cpp
blobf2587b555780ad7fecc2455cae261a7de0c8be30
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Corpse.h"
21 #include "Player.h"
22 #include "UpdateMask.h"
23 #include "MapManager.h"
24 #include "ObjectAccessor.h"
25 #include "Database/DatabaseEnv.h"
26 #include "Opcodes.h"
27 #include "WorldSession.h"
28 #include "WorldPacket.h"
29 #include "GossipDef.h"
30 #include "World.h"
32 Corpse::Corpse(CorpseType type) : WorldObject()
34 m_objectType |= TYPEMASK_CORPSE;
35 m_objectTypeId = TYPEID_CORPSE;
36 // 2.3.2 - 0x58
37 m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION);
39 m_valuesCount = CORPSE_END;
41 m_type = type;
43 m_time = time(NULL);
45 lootForBody = false;
48 Corpse::~Corpse()
52 void Corpse::AddToWorld()
54 ///- Register the corpse for guid lookup
55 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
56 Object::AddToWorld();
59 void Corpse::RemoveFromWorld()
61 ///- Remove the corpse from the accessor
62 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
63 Object::RemoveFromWorld();
66 bool Corpse::Create( uint32 guidlow )
68 Object::_Create(guidlow, 0, HIGHGUID_CORPSE);
69 return true;
72 bool Corpse::Create( uint32 guidlow, Player *owner)
74 SetInstanceId(owner->GetInstanceId());
76 WorldObject::_Create(guidlow, HIGHGUID_CORPSE, owner->GetMapId(), owner->GetPhaseMask());
78 Relocate(owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->GetOrientation());
80 if(!IsPositionValid())
82 sLog.outError("ERROR: Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
83 guidlow,owner->GetName(),owner->GetPositionX(), owner->GetPositionY());
84 return false;
87 SetFloatValue( OBJECT_FIELD_SCALE_X, 1 );
88 SetFloatValue( CORPSE_FIELD_POS_X, GetPositionX() );
89 SetFloatValue( CORPSE_FIELD_POS_Y, GetPositionY() );
90 SetFloatValue( CORPSE_FIELD_POS_Z, GetPositionZ() );
91 SetFloatValue( CORPSE_FIELD_FACING, GetOrientation() );
92 SetUInt64Value( CORPSE_FIELD_OWNER, owner->GetGUID() );
94 m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
96 return true;
99 void Corpse::SaveToDB()
101 // prevent DB data inconsistence problems and duplicates
102 CharacterDatabase.BeginTransaction();
103 DeleteFromDB();
105 std::ostringstream ss;
106 ss << "INSERT INTO corpse (guid,player,position_x,position_y,position_z,orientation,zone,map,data,time,corpse_type,instance,phaseMask) VALUES ("
107 << GetGUIDLow() << ", " << GUID_LOPART(GetOwnerGUID()) << ", " << GetPositionX() << ", " << GetPositionY() << ", " << GetPositionZ() << ", "
108 << GetOrientation() << ", " << GetZoneId() << ", " << GetMapId() << ", '";
109 for(uint16 i = 0; i < m_valuesCount; i++ )
110 ss << GetUInt32Value(i) << " ";
111 ss << "'," << uint64(m_time) <<", " << uint32(GetType())
112 << ", " << int(GetInstanceId()) << ", " << int(GetPhaseMask()) << ")";
113 CharacterDatabase.Execute( ss.str().c_str() );
114 CharacterDatabase.CommitTransaction();
117 void Corpse::DeleteBonesFromWorld()
119 assert(GetType()==CORPSE_BONES);
120 Corpse* corpse = ObjectAccessor::GetCorpse(*this, GetGUID());
122 if (!corpse)
124 sLog.outError("Bones %u not found in world.", GetGUIDLow());
125 return;
128 AddObjectToRemoveList();
131 void Corpse::DeleteFromDB()
133 if(GetType() == CORPSE_BONES)
134 // only specific bones
135 CharacterDatabase.PExecute("DELETE FROM corpse WHERE guid = '%d'", GetGUIDLow());
136 else
137 // all corpses (not bones)
138 CharacterDatabase.PExecute("DELETE FROM corpse WHERE player = '%d' AND corpse_type <> '0'", GUID_LOPART(GetOwnerGUID()));
141 bool Corpse::LoadFromDB(uint32 guid, QueryResult *result)
143 bool external = (result != NULL);
144 if (!external)
145 // 0 1 2 3 4 5 6 7 8 9
146 result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance,phaseMask FROM corpse WHERE guid = '%u'",guid);
148 if( ! result )
150 sLog.outError("ERROR: Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid);
151 return false;
154 Field *fields = result->Fetch();
156 if(!LoadFromDB(guid,fields))
158 if (!external) delete result;
159 return false;
162 if (!external) delete result;
163 return true;
166 bool Corpse::LoadFromDB(uint32 guid, Field *fields)
168 // 0 1 2 3 4 5 6 7 8 9
169 //result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance,phaseMask FROM corpse WHERE guid = '%u'",guid);
170 float positionX = fields[0].GetFloat();
171 float positionY = fields[1].GetFloat();
172 float positionZ = fields[2].GetFloat();
173 float ort = fields[3].GetFloat();
174 uint32 mapid = fields[4].GetUInt32();
176 if(!LoadValues( fields[5].GetString() ))
178 sLog.outError("ERROR: Corpse #%d have broken data in `data` field. Can't be loaded.",guid);
179 return false;
182 m_time = time_t(fields[6].GetUInt64());
183 m_type = CorpseType(fields[7].GetUInt32());
184 if(m_type >= MAX_CORPSE_TYPE)
186 sLog.outError("ERROR: Corpse (guidlow %d, owner %d) have wrong corpse type, not load.",GetGUIDLow(),GUID_LOPART(GetOwnerGUID()));
187 return false;
189 uint32 instanceid = fields[8].GetUInt32();
190 uint32 phaseMask = fields[9].GetUInt32();
192 // overwrite possible wrong/corrupted guid
193 SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid, 0, HIGHGUID_CORPSE));
195 // place
196 SetInstanceId(instanceid);
197 SetMapId(mapid);
198 SetPhaseMask(phaseMask,false);
199 Relocate(positionX,positionY,positionZ,ort);
201 if(!IsPositionValid())
203 sLog.outError("ERROR: Corpse (guidlow %d, owner %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
204 GetGUIDLow(),GUID_LOPART(GetOwnerGUID()),GetPositionX(),GetPositionY());
205 return false;
208 m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
210 return true;
213 bool Corpse::isVisibleForInState(Player const* u, bool inVisibleList) const
215 return IsInWorld() && u->IsInWorld() && IsWithinDistInMap(u,World::GetMaxVisibleDistanceForObject()+(inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false);