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

Revision 102, 6.9 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

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