[7719] Use all existed 4 world map overlay area ids instead 3, replace values by...
[AHbot.git] / src / game / Corpse.cpp
blobcf1f11bd897e6ef82aa029a5cbd2925b86569be5
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 "ObjectAccessor.h"
24 #include "Database/DatabaseEnv.h"
25 #include "Opcodes.h"
26 #include "GossipDef.h"
27 #include "World.h"
29 Corpse::Corpse(CorpseType type) : WorldObject()
31 m_objectType |= TYPEMASK_CORPSE;
32 m_objectTypeId = TYPEID_CORPSE;
33 // 2.3.2 - 0x58
34 m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION);
36 m_valuesCount = CORPSE_END;
38 m_type = type;
40 m_time = time(NULL);
42 lootForBody = false;
45 Corpse::~Corpse()
49 void Corpse::AddToWorld()
51 ///- Register the corpse for guid lookup
52 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
53 Object::AddToWorld();
56 void Corpse::RemoveFromWorld()
58 ///- Remove the corpse from the accessor
59 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
60 Object::RemoveFromWorld();
63 bool Corpse::Create( uint32 guidlow )
65 Object::_Create(guidlow, 0, HIGHGUID_CORPSE);
66 return true;
69 bool Corpse::Create( uint32 guidlow, Player *owner)
71 SetInstanceId(owner->GetInstanceId());
73 WorldObject::_Create(guidlow, HIGHGUID_CORPSE, owner->GetMapId(), owner->GetPhaseMask());
75 Relocate(owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->GetOrientation());
77 if(!IsPositionValid())
79 sLog.outError("Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
80 guidlow,owner->GetName(),owner->GetPositionX(), owner->GetPositionY());
81 return false;
84 SetFloatValue( OBJECT_FIELD_SCALE_X, 1 );
85 SetFloatValue( CORPSE_FIELD_POS_X, GetPositionX() );
86 SetFloatValue( CORPSE_FIELD_POS_Y, GetPositionY() );
87 SetFloatValue( CORPSE_FIELD_POS_Z, GetPositionZ() );
88 SetFloatValue( CORPSE_FIELD_FACING, GetOrientation() );
89 SetUInt64Value( CORPSE_FIELD_OWNER, owner->GetGUID() );
91 m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
93 return true;
96 void Corpse::SaveToDB()
98 // prevent DB data inconsistence problems and duplicates
99 CharacterDatabase.BeginTransaction();
100 DeleteFromDB();
102 std::ostringstream ss;
103 ss << "INSERT INTO corpse (guid,player,position_x,position_y,position_z,orientation,zone,map,data,time,corpse_type,instance,phaseMask) VALUES ("
104 << GetGUIDLow() << ", "
105 << GUID_LOPART(GetOwnerGUID()) << ", "
106 << GetPositionX() << ", "
107 << GetPositionY() << ", "
108 << GetPositionZ() << ", "
109 << GetOrientation() << ", "
110 << GetZoneId() << ", "
111 << GetMapId() << ", '";
112 for(uint16 i = 0; i < m_valuesCount; i++ )
113 ss << GetUInt32Value(i) << " ";
114 ss << "',"
115 << uint64(m_time) <<", "
116 << uint32(GetType()) << ", "
117 << int(GetInstanceId()) << ", "
118 << uint16(GetPhaseMask()) << ")"; // prevent out of range error
119 CharacterDatabase.Execute( ss.str().c_str() );
120 CharacterDatabase.CommitTransaction();
123 void Corpse::DeleteBonesFromWorld()
125 assert(GetType()==CORPSE_BONES);
126 Corpse* corpse = ObjectAccessor::GetCorpse(*this, GetGUID());
128 if (!corpse)
130 sLog.outError("Bones %u not found in world.", GetGUIDLow());
131 return;
134 AddObjectToRemoveList();
137 void Corpse::DeleteFromDB()
139 if(GetType() == CORPSE_BONES)
140 // only specific bones
141 CharacterDatabase.PExecute("DELETE FROM corpse WHERE guid = '%d'", GetGUIDLow());
142 else
143 // all corpses (not bones)
144 CharacterDatabase.PExecute("DELETE FROM corpse WHERE player = '%d' AND corpse_type <> '0'", GUID_LOPART(GetOwnerGUID()));
147 bool Corpse::LoadFromDB(uint32 guid, QueryResult *result)
149 bool external = (result != NULL);
150 if (!external)
151 // 0 1 2 3 4 5 6 7 8 9
152 result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance,phaseMask FROM corpse WHERE guid = '%u'",guid);
154 if( ! result )
156 sLog.outError("Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid);
157 return false;
160 Field *fields = result->Fetch();
162 if(!LoadFromDB(guid,fields))
164 if (!external) delete result;
165 return false;
168 if (!external) delete result;
169 return true;
172 bool Corpse::LoadFromDB(uint32 guid, Field *fields)
174 // 0 1 2 3 4 5 6 7 8 9
175 //result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance,phaseMask FROM corpse WHERE guid = '%u'",guid);
176 float positionX = fields[0].GetFloat();
177 float positionY = fields[1].GetFloat();
178 float positionZ = fields[2].GetFloat();
179 float ort = fields[3].GetFloat();
180 uint32 mapid = fields[4].GetUInt32();
182 if(!LoadValues( fields[5].GetString() ))
184 sLog.outError("Corpse #%d have broken data in `data` field. Can't be loaded.",guid);
185 return false;
188 m_time = time_t(fields[6].GetUInt64());
189 m_type = CorpseType(fields[7].GetUInt32());
190 if(m_type >= MAX_CORPSE_TYPE)
192 sLog.outError("Corpse (guidlow %d, owner %d) have wrong corpse type, not load.",GetGUIDLow(),GUID_LOPART(GetOwnerGUID()));
193 return false;
195 uint32 instanceid = fields[8].GetUInt32();
196 uint32 phaseMask = fields[9].GetUInt32();
198 // overwrite possible wrong/corrupted guid
199 SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid, 0, HIGHGUID_CORPSE));
201 // place
202 SetInstanceId(instanceid);
203 SetMapId(mapid);
204 SetPhaseMask(phaseMask,false);
205 Relocate(positionX,positionY,positionZ,ort);
207 if(!IsPositionValid())
209 sLog.outError("Corpse (guidlow %d, owner %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
210 GetGUIDLow(),GUID_LOPART(GetOwnerGUID()),GetPositionX(),GetPositionY());
211 return false;
214 m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
216 return true;
219 bool Corpse::isVisibleForInState(Player const* u, bool inVisibleList) const
221 return IsInWorld() && u->IsInWorld() && IsWithinDistInMap(u,World::GetMaxVisibleDistanceForObject()+(inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false);