root/trunk/src/game/Corpse.cpp @ 39

Revision 2, 6.8 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
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"
31
32Corpse::Corpse(CorpseType type) : WorldObject()
33{
34    m_objectType |= TYPEMASK_CORPSE;
35    m_objectTypeId = TYPEID_CORPSE;
36                                                            // 2.3.2 - 0x58
37    m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HASPOSITION);
38
39    m_valuesCount = CORPSE_END;
40
41    m_type = type;
42
43    m_time = time(NULL);
44
45    lootForBody = false;
46}
47
48Corpse::~Corpse()
49{
50}
51
52void Corpse::AddToWorld()
53{
54    ///- Register the corpse for guid lookup
55    if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
56    Object::AddToWorld();
57}
58
59void Corpse::RemoveFromWorld()
60{
61    ///- Remove the corpse from the accessor
62    if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
63    Object::RemoveFromWorld();
64}
65
66bool Corpse::Create( uint32 guidlow )
67{
68    Object::_Create(guidlow, 0, HIGHGUID_CORPSE);
69    return true;
70}
71
72bool Corpse::Create( uint32 guidlow, Player *owner, uint32 mapid, float x, float y, float z, float ang )
73{
74    SetInstanceId(owner->GetInstanceId());
75
76    WorldObject::_Create(guidlow, HIGHGUID_CORPSE, mapid);
77
78    Relocate(x,y,z,ang);
79
80    if(!IsPositionValid())
81    {
82        sLog.outError("ERROR: Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %d Y: ^%d)",guidlow,owner->GetName(),x,y);
83        return false;
84    }
85
86    SetFloatValue( OBJECT_FIELD_SCALE_X, 1 );
87    SetFloatValue( CORPSE_FIELD_POS_X, x );
88    SetFloatValue( CORPSE_FIELD_POS_Y, y );
89    SetFloatValue( CORPSE_FIELD_POS_Z, z );
90    SetFloatValue( CORPSE_FIELD_FACING, ang );
91    SetUInt64Value( CORPSE_FIELD_OWNER, owner->GetGUID() );
92
93    m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
94
95    return true;
96}
97
98void Corpse::SaveToDB()
99{
100    // prevent DB data inconsistance problems and duplicates
101    CharacterDatabase.BeginTransaction();
102    DeleteFromDB();
103
104    std::ostringstream ss;
105    ss  << "INSERT INTO corpse (guid,player,position_x,position_y,position_z,orientation,zone,map,data,time,corpse_type,instance) VALUES ("
106        << GetGUIDLow() << ", " << GUID_LOPART(GetOwnerGUID()) << ", " << GetPositionX() << ", " << GetPositionY() << ", " << GetPositionZ() << ", "
107        << GetOrientation() << ", "  << GetZoneId() << ", "  << GetMapId() << ", '";
108    for(uint16 i = 0; i < m_valuesCount; i++ )
109        ss << GetUInt32Value(i) << " ";
110    ss << "'," << uint64(m_time) <<", " << uint32(GetType()) << ", " << int(GetInstanceId()) << ")";
111    CharacterDatabase.Execute( ss.str().c_str() );
112    CharacterDatabase.CommitTransaction();
113}
114
115void Corpse::DeleteBonesFromWorld()
116{
117    assert(GetType()==CORPSE_BONES);
118    Corpse* corpse = ObjectAccessor::GetCorpse(*this, GetGUID());
119
120    if (!corpse)
121    {
122        sLog.outError("Bones %u not found in world.", GetGUIDLow());
123        return;
124    }
125
126    AddObjectToRemoveList();
127}
128
129void Corpse::DeleteFromDB()
130{
131    if(GetType() == CORPSE_BONES)
132        // only specific bones
133        CharacterDatabase.PExecute("DELETE FROM corpse WHERE guid = '%d'", GetGUIDLow());
134    else
135        // all corpses (not bones)
136        CharacterDatabase.PExecute("DELETE FROM corpse WHERE player = '%d' AND corpse_type <> '0'",  GUID_LOPART(GetOwnerGUID()));
137}
138
139bool Corpse::LoadFromDB(uint32 guid, QueryResult *result, uint32 InstanceId)
140{
141    bool external = (result != NULL);
142    if (!external)
143        //                                        0          1          2          3           4   5    6    7           8
144        result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance FROM corpse WHERE guid = '%u'",guid);
145
146    if( ! result )
147    {
148        sLog.outError("ERROR: Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid);
149        return false;
150    }
151
152    Field *fields = result->Fetch();
153
154    if(!LoadFromDB(guid,fields))
155    {
156        if (!external) delete result;
157        return false;
158    }
159
160    if (!external) delete result;
161    return true;
162}
163
164bool Corpse::LoadFromDB(uint32 guid, Field *fields)
165{
166    //                                          0          1          2          3           4   5    6    7           8
167    //result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance FROM corpse WHERE guid = '%u'",guid);
168    float positionX = fields[0].GetFloat();
169    float positionY = fields[1].GetFloat();
170    float positionZ = fields[2].GetFloat();
171    float ort       = fields[3].GetFloat();
172    uint32 mapid    = fields[4].GetUInt32();
173
174    if(!LoadValues( fields[5].GetString() ))
175    {
176        sLog.outError("ERROR: Corpse #%d have broken data in `data` field. Can't be loaded.",guid);
177        return false;
178    }
179
180    m_time             = time_t(fields[6].GetUInt64());
181    m_type             = CorpseType(fields[7].GetUInt32());
182    if(m_type >= MAX_CORPSE_TYPE)
183    {
184        sLog.outError("ERROR: Corpse (guidlow %d, owner %d) have wrong corpse type, not load.",GetGUIDLow(),GUID_LOPART(GetOwnerGUID()));
185        return false;
186    }
187    uint32 instanceid  = fields[8].GetUInt32();
188
189    // overwrite possible wrong/corrupted guid
190    SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid, 0, HIGHGUID_CORPSE));
191
192    // place
193    SetInstanceId(instanceid);
194    SetMapId(mapid);
195    Relocate(positionX,positionY,positionZ,ort);
196
197    if(!IsPositionValid())
198    {
199        sLog.outError("ERROR: Corpse (guidlow %d, owner %d) not created. Suggested coordinates isn't valid (X: %d Y: ^%d)",GetGUIDLow(),GUID_LOPART(GetOwnerGUID()),GetPositionX(),GetPositionY());
200        return false;
201    }
202
203    m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
204
205    return true;
206}
207
208bool Corpse::isVisibleForInState(Player const* u, bool inVisibleList) const
209{
210    return IsInWorld() && u->IsInWorld() && IsWithinDistInMap(u,World::GetMaxVisibleDistanceForObject()+(inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f));
211}
Note: See TracBrowser for help on using the browser.