root/trunk/src/game/GameObject.cpp @ 37

Revision 37, 43.9 kB (checked in by yumileroy, 17 years ago)

[svn] * svn:eol-style native set on all files that need it

Original author: Neo2003
Date: 2008-10-11 14:16:25-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 "QuestDef.h"
21#include "GameObject.h"
22#include "ObjectMgr.h"
23#include "SpellMgr.h"
24#include "Spell.h"
25#include "UpdateMask.h"
26#include "Opcodes.h"
27#include "WorldPacket.h"
28#include "WorldSession.h"
29#include "World.h"
30#include "Database/DatabaseEnv.h"
31#include "MapManager.h"
32#include "LootMgr.h"
33#include "GridNotifiers.h"
34#include "GridNotifiersImpl.h"
35#include "CellImpl.h"
36#include "InstanceData.h"
37#include "BattleGround.h"
38#include "Util.h"
39
40GameObject::GameObject() : WorldObject()
41{
42    m_objectType |= TYPEMASK_GAMEOBJECT;
43    m_objectTypeId = TYPEID_GAMEOBJECT;
44                                                            // 2.3.2 - 0x58
45    m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HASPOSITION);
46
47    m_valuesCount = GAMEOBJECT_END;
48    m_respawnTime = 0;
49    m_respawnDelayTime = 25;
50    m_lootState = GO_NOT_READY;
51    m_spawnedByDefault = true;
52    m_usetimes = 0;
53    m_spellId = 0;
54    m_charges = 5;
55    m_cooldownTime = 0;
56    m_goInfo = NULL;
57
58    m_DBTableGuid = 0;
59}
60
61GameObject::~GameObject()
62{
63    if(m_uint32Values)                                      // field array can be not exist if GameOBject not loaded
64    {
65        // crash possable at access to deleted GO in Unit::m_gameobj
66        uint64 owner_guid = GetOwnerGUID();
67        if(owner_guid)
68        {
69            Unit* owner = ObjectAccessor::GetUnit(*this,owner_guid);
70            if(owner)
71                owner->RemoveGameObject(this,false);
72            else if(!IS_PLAYER_GUID(owner_guid))
73                sLog.outError("Delete GameObject (GUID: %u Entry: %u ) that have references in not found creature %u GO list. Crash possable later.",GetGUIDLow(),GetGOInfo()->id,GUID_LOPART(owner_guid));
74        }
75    }
76}
77
78void GameObject::AddToWorld()
79{
80    ///- Register the gameobject for guid lookup
81    if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
82    Object::AddToWorld();
83}
84
85void GameObject::RemoveFromWorld()
86{
87    ///- Remove the gameobject from the accessor
88    if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
89    Object::RemoveFromWorld();
90}
91
92bool GameObject::Create(uint32 guidlow, uint32 name_id, Map *map, float x, float y, float z, float ang, float rotation0, float rotation1, float rotation2, float rotation3, uint32 animprogress, uint32 go_state)
93{
94    Relocate(x,y,z,ang);
95    SetMapId(map->GetId());
96    SetInstanceId(map->GetInstanceId());
97
98    if(!IsPositionValid())
99    {
100        sLog.outError("ERROR: Gameobject (GUID: %u Entry: %u ) not created. Suggested coordinates isn't valid (X: %f Y: %f)",guidlow,name_id,x,y);
101        return false;
102    }
103
104    GameObjectInfo const* goinfo = objmgr.GetGameObjectInfo(name_id);
105    if (!goinfo)
106    {
107        sLog.outErrorDb("Gameobject (GUID: %u Entry: %u) not created: it have not exist entry in `gameobject_template`. Map: %u  (X: %f Y: %f Z: %f) ang: %f rotation0: %f rotation1: %f rotation2: %f rotation3: %f",guidlow, name_id, map->GetId(), x, y, z, ang, rotation0, rotation1, rotation2, rotation3);
108        return false;
109    }
110
111    Object::_Create(guidlow, goinfo->id, HIGHGUID_GAMEOBJECT);
112
113    m_goInfo = goinfo;
114
115    if (goinfo->type >= MAX_GAMEOBJECT_TYPE)
116    {
117        sLog.outErrorDb("Gameobject (GUID: %u Entry: %u) not created: it have not exist GO type '%u' in `gameobject_template`. It's will crash client if created.",guidlow,name_id,goinfo->type);
118        return false;
119    }
120
121    SetFloatValue(GAMEOBJECT_POS_X, x);
122    SetFloatValue(GAMEOBJECT_POS_Y, y);
123    SetFloatValue(GAMEOBJECT_POS_Z, z);
124    SetFloatValue(GAMEOBJECT_FACING, ang);                  //this is not facing angle
125
126    SetFloatValue (GAMEOBJECT_ROTATION, rotation0);
127    SetFloatValue (GAMEOBJECT_ROTATION+1, rotation1);
128    SetFloatValue (GAMEOBJECT_ROTATION+2, rotation2);
129    SetFloatValue (GAMEOBJECT_ROTATION+3, rotation3);
130
131    SetFloatValue(OBJECT_FIELD_SCALE_X, goinfo->size);
132
133    SetUInt32Value(GAMEOBJECT_FACTION, goinfo->faction);
134    SetUInt32Value(GAMEOBJECT_FLAGS, goinfo->flags);
135
136    SetUInt32Value(OBJECT_FIELD_ENTRY, goinfo->id);
137
138    SetUInt32Value(GAMEOBJECT_DISPLAYID, goinfo->displayId);
139
140    SetGoState(go_state);
141    SetGoType(GameobjectTypes(goinfo->type));
142
143    SetGoAnimProgress(animprogress);
144
145    // Spell charges for GAMEOBJECT_TYPE_SPELLCASTER (22)
146    if (goinfo->type == GAMEOBJECT_TYPE_SPELLCASTER)
147        m_charges = goinfo->spellcaster.charges;
148
149    //Notify the map's instance data.
150    //Only works if you create the object in it, not if it is moves to that map.
151    //Normally non-players do not teleport to other maps.
152    if(map->IsDungeon() && ((InstanceMap*)map)->GetInstanceData())
153    {
154        ((InstanceMap*)map)->GetInstanceData()->OnObjectCreate(this);
155    }
156
157    return true;
158}
159
160void GameObject::Update(uint32 /*p_time*/)
161{
162    if (IS_MO_TRANSPORT(GetGUID()))
163    {
164        //((Transport*)this)->Update(p_time);
165        return;
166    }
167
168    switch (m_lootState)
169    {
170        case GO_NOT_READY:
171        {
172            switch(GetGoType())
173            {
174                case GAMEOBJECT_TYPE_TRAP:
175                {
176                    // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
177                    Unit* owner = GetOwner();
178                    if (owner && ((Player*)owner)->isInCombat())
179                        m_cooldownTime = time(NULL) + GetGOInfo()->trap.startDelay;
180                    m_lootState = GO_READY;
181                    break;
182                }
183                case GAMEOBJECT_TYPE_FISHINGNODE:
184                {
185                    // fishing code (bobber ready)
186                    if( time(NULL) > m_respawnTime - FISHING_BOBBER_READY_TIME )
187                    {
188                        // splash bobber (bobber ready now)
189                        Unit* caster = GetOwner();
190                        if(caster && caster->GetTypeId()==TYPEID_PLAYER)
191                        {
192                            SetGoState(0);
193                            SetUInt32Value(GAMEOBJECT_FLAGS, 32);
194
195                            UpdateData udata;
196                            WorldPacket packet;
197                            BuildValuesUpdateBlockForPlayer(&udata,((Player*)caster));
198                            udata.BuildPacket(&packet);
199                            ((Player*)caster)->GetSession()->SendPacket(&packet);
200
201                            WorldPacket data(SMSG_GAMEOBJECT_CUSTOM_ANIM,8+4);
202                            data << GetGUID();
203                            data << (uint32)(0);
204                            ((Player*)caster)->SendMessageToSet(&data,true);
205                        }
206
207                        m_lootState = GO_READY;                 // can be succesfully open with some chance
208                    }
209                    return;
210                }
211                default:
212                    m_lootState = GO_READY;                         // for other GOis same switched without delay to GO_READY
213                    break;
214            }
215            // NO BREAK for switch (m_lootState)
216        }
217        case GO_READY:
218        {
219            if (m_respawnTime > 0)                          // timer on
220            {
221                if (m_respawnTime <= time(NULL))            // timer expired
222                {
223                    m_respawnTime = 0;
224                    m_SkillupList.clear();
225                    m_usetimes = 0;
226
227                    switch (GetGoType())
228                    {
229                        case GAMEOBJECT_TYPE_FISHINGNODE:   //  can't fish now
230                        {
231                            Unit* caster = GetOwner();
232                            if(caster && caster->GetTypeId()==TYPEID_PLAYER)
233                            {
234                                if(caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
235                                {
236                                    caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
237                                    caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish(false);
238                                }
239
240                                WorldPacket data(SMSG_FISH_NOT_HOOKED,0);
241                                ((Player*)caster)->GetSession()->SendPacket(&data);
242                            }
243                            // can be delete
244                            m_lootState = GO_JUST_DEACTIVATED;
245                            return;
246                        }
247                        case GAMEOBJECT_TYPE_DOOR:
248                        case GAMEOBJECT_TYPE_BUTTON:
249                            //we need to open doors if they are closed (add there another condition if this code breaks some usage, but it need to be here for battlegrounds)
250                            if( !GetGoState() )
251                                SwitchDoorOrButton(false);
252                            //flags in AB are type_button and we need to add them here so no break!
253                        default:
254                            if(!m_spawnedByDefault)         // despawn timer
255                            {
256                                                            // can be despawned or destroyed
257                                SetLootState(GO_JUST_DEACTIVATED);
258                                return;
259                            }
260                                                            // respawn timer
261                            MapManager::Instance().GetMap(GetMapId(), this)->Add(this);
262                            break;
263                    }
264                }
265            }
266
267            // traps can have time and can not have
268            GameObjectInfo const* goInfo = GetGOInfo();
269            if(goInfo->type == GAMEOBJECT_TYPE_TRAP)
270            {
271                // traps
272                Unit* owner = GetOwner();
273                Unit* ok = NULL;                            // pointer to appropriate target if found any
274
275                if(m_cooldownTime >= time(NULL))
276                    return;
277
278                bool IsBattleGroundTrap = false;
279                //FIXME: this is activation radius (in different casting radius that must be selected from spell data)
280                //TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
281                float radius = goInfo->trap.radius;
282                if(!radius)
283                {
284                    if(goInfo->trap.cooldown != 3)            // cast in other case (at some triggring/linked go/etc explicit call)
285                        return;
286                    else
287                    {
288                        if(m_respawnTime > 0)
289                            break;
290
291                        radius = goInfo->trap.cooldown;       // battlegrounds gameobjects has data2 == 0 && data5 == 3
292                        IsBattleGroundTrap = true;
293                    }
294                }
295
296                bool NeedDespawn = (goInfo->trap.charges != 0);
297
298                CellPair p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
299                Cell cell(p);
300                cell.data.Part.reserved = ALL_DISTRICT;
301
302                // Note: this hack with search required until GO casting not implemented
303                // search unfriendly creature
304                if(owner && NeedDespawn)                    // hunter trap
305                {
306                    MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck u_check(this, owner, radius);
307                    MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck> checker(ok, u_check);
308
309                    CellLock<GridReadGuard> cell_lock(cell, p);
310
311                    TypeContainerVisitor<MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck>, GridTypeMapContainer > grid_object_checker(checker);
312                    cell_lock->Visit(cell_lock, grid_object_checker, *MapManager::Instance().GetMap(GetMapId(), this));
313
314                    // or unfriendly player/pet
315                    if(!ok)
316                    {
317                        TypeContainerVisitor<MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
318                        cell_lock->Visit(cell_lock, world_object_checker, *MapManager::Instance().GetMap(GetMapId(), this));
319                    }
320                }
321                else                                        // environmental trap
322                {
323                    // environmental damage spells already have around enemies targeting but this not help in case not existed GO casting support
324
325                    // affect only players
326                    Player* p_ok = NULL;
327                    MaNGOS::AnyPlayerInObjectRangeCheck p_check(this, radius);
328                    MaNGOS::PlayerSearcher<MaNGOS::AnyPlayerInObjectRangeCheck>  checker(p_ok, p_check);
329
330                    CellLock<GridReadGuard> cell_lock(cell, p);
331
332                    TypeContainerVisitor<MaNGOS::PlayerSearcher<MaNGOS::AnyPlayerInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
333                    cell_lock->Visit(cell_lock, world_object_checker, *MapManager::Instance().GetMap(GetMapId(), this));
334                    ok = p_ok;
335                }
336
337                if (ok)
338                {
339                    Unit *caster =  owner ? owner : ok;
340
341                    caster->CastSpell(ok, goInfo->trap.spellId, true);
342                    m_cooldownTime = time(NULL) + 4;        // 4 seconds
343
344                    if(NeedDespawn)
345                        SetLootState(GO_JUST_DEACTIVATED);  // can be despawned or destroyed
346
347                    if(IsBattleGroundTrap && ok->GetTypeId() == TYPEID_PLAYER)
348                    {
349                        //BattleGround gameobjects case
350                        if(((Player*)ok)->InBattleGround())
351                            if(BattleGround *bg = ((Player*)ok)->GetBattleGround())
352                                bg->HandleTriggerBuff(GetGUID());
353                    }
354                }
355            }
356
357            if (m_charges && m_usetimes >= m_charges)
358                SetLootState(GO_JUST_DEACTIVATED);          // can be despawned or destroyed
359
360            break;
361        }
362        case GO_ACTIVATED:
363        {
364            switch(GetGoType())
365            {
366                case GAMEOBJECT_TYPE_DOOR:
367                case GAMEOBJECT_TYPE_BUTTON:
368                    if(GetAutoCloseTime() && (m_cooldownTime < time(NULL)))
369                    {
370                        SwitchDoorOrButton(false);
371                        SetLootState(GO_JUST_DEACTIVATED);
372                    }
373                    break;
374            }
375            break;
376        }
377        case GO_JUST_DEACTIVATED:
378        {
379            //if Gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
380            if (GetGoType() == GAMEOBJECT_TYPE_GOOBER)
381            {
382                uint32 spellId = GetGOInfo()->goober.spellId;
383
384                if(spellId)
385                {
386                    std::set<uint32>::iterator it = m_unique_users.begin();
387                    std::set<uint32>::iterator end = m_unique_users.end();
388                    for (; it != end; it++)
389                    {
390                        Unit* owner = Unit::GetUnit(*this, uint64(*it));
391                        if (owner) owner->CastSpell(owner, spellId, false);
392                    }
393
394                    m_unique_users.clear();
395                    m_usetimes = 0;
396                }
397                //any return here in case battleground traps
398            }
399
400            if(GetOwnerGUID())
401            {
402                m_respawnTime = 0;
403                Delete();
404                return;
405            }
406
407            //burning flags in some battlegrounds, if you find better condition, just add it
408            if (GetGoAnimProgress() > 0)
409            {
410                SendObjectDeSpawnAnim(this->GetGUID());
411                //reset flags
412                SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
413            }
414
415            loot.clear();
416            SetLootState(GO_READY);
417
418            if(!m_respawnDelayTime)
419                return;
420
421            if(!m_spawnedByDefault)
422            {
423                m_respawnTime = 0;
424                return;
425            }
426
427            m_respawnTime = time(NULL) + m_respawnDelayTime;
428
429            // if option not set then object will be saved at grid unload
430            if(sWorld.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY))
431                SaveRespawnTime();
432
433            ObjectAccessor::UpdateObjectVisibility(this);
434
435            break;
436        }
437    }
438}
439
440void GameObject::Refresh()
441{
442    // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
443    if(m_respawnTime > 0 && m_spawnedByDefault)
444        return;
445
446    if(isSpawned())
447        MapManager::Instance().GetMap(GetMapId(), this)->Add(this);
448}
449
450void GameObject::AddUniqueUse(Player* player)
451{
452    AddUse();
453    m_unique_users.insert(player->GetGUIDLow());
454}
455
456void GameObject::Delete()
457{
458    SendObjectDeSpawnAnim(GetGUID());
459
460    SetGoState(1);
461    SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
462
463    AddObjectToRemoveList();
464}
465
466void GameObject::getFishLoot(Loot *fishloot)
467{
468    fishloot->clear();
469
470    uint32 subzone = GetAreaId();
471
472    // if subzone loot exist use it
473    if(LootTemplates_Fishing.HaveLootFor(subzone))
474        fishloot->FillLoot(subzone, LootTemplates_Fishing, NULL);
475    // else use zone loot
476    else
477        fishloot->FillLoot(GetZoneId(), LootTemplates_Fishing, NULL);
478}
479
480void GameObject::SaveToDB()
481{
482    // this should only be used when the gameobject has already been loaded
483    // perferably after adding to map, because mapid may not be valid otherwise
484    GameObjectData const *data = objmgr.GetGOData(m_DBTableGuid);
485    if(!data)
486    {
487        sLog.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
488        return;
489    }
490
491    SaveToDB(GetMapId(), data->spawnMask);
492}
493
494void GameObject::SaveToDB(uint32 mapid, uint8 spawnMask)
495{
496    const GameObjectInfo *goI = GetGOInfo();
497
498    if (!goI)
499        return;
500   
501    if (!m_DBTableGuid)
502        m_DBTableGuid = GetGUIDLow();
503    // update in loaded data (changing data only in this place)
504    GameObjectData& data = objmgr.NewGOData(m_DBTableGuid);
505
506    // data->guid = guid don't must be update at save
507    data.id = GetEntry();
508    data.mapid = mapid;
509    data.posX = GetFloatValue(GAMEOBJECT_POS_X);
510    data.posY = GetFloatValue(GAMEOBJECT_POS_Y);
511    data.posZ = GetFloatValue(GAMEOBJECT_POS_Z);
512    data.orientation = GetFloatValue(GAMEOBJECT_FACING);
513    data.rotation0 = GetFloatValue(GAMEOBJECT_ROTATION+0);
514    data.rotation1 = GetFloatValue(GAMEOBJECT_ROTATION+1);
515    data.rotation2 = GetFloatValue(GAMEOBJECT_ROTATION+2);
516    data.rotation3 = GetFloatValue(GAMEOBJECT_ROTATION+3);
517    data.spawntimesecs = m_spawnedByDefault ? m_respawnDelayTime : -(int32)m_respawnDelayTime;
518    data.animprogress = GetGoAnimProgress();
519    data.go_state = GetGoState();
520    data.spawnMask = spawnMask;
521
522    // updated in DB
523    std::ostringstream ss;
524    ss << "INSERT INTO gameobject VALUES ( "
525        << m_DBTableGuid << ", "
526        << GetUInt32Value (OBJECT_FIELD_ENTRY) << ", "
527        << mapid << ", "
528        << (uint32)spawnMask << ", "
529        << GetFloatValue(GAMEOBJECT_POS_X) << ", "
530        << GetFloatValue(GAMEOBJECT_POS_Y) << ", "
531        << GetFloatValue(GAMEOBJECT_POS_Z) << ", "
532        << GetFloatValue(GAMEOBJECT_FACING) << ", "
533        << GetFloatValue(GAMEOBJECT_ROTATION) << ", "
534        << GetFloatValue(GAMEOBJECT_ROTATION+1) << ", "
535        << GetFloatValue(GAMEOBJECT_ROTATION+2) << ", "
536        << GetFloatValue(GAMEOBJECT_ROTATION+3) << ", "
537        << m_respawnDelayTime << ", "
538        << GetGoAnimProgress() << ", "
539        << GetGoState() << ")";
540
541    WorldDatabase.BeginTransaction();
542    WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid);
543    WorldDatabase.PExecuteLog( ss.str( ).c_str( ) );
544    WorldDatabase.CommitTransaction();
545}
546
547bool GameObject::LoadFromDB(uint32 guid, Map *map)
548{
549    GameObjectData const* data = objmgr.GetGOData(guid);
550
551    if( !data )
552    {
553        sLog.outErrorDb("ERROR: Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid);
554        return false;
555    }
556
557    uint32 entry = data->id;
558    uint32 map_id = data->mapid;
559    float x = data->posX;
560    float y = data->posY;
561    float z = data->posZ;
562    float ang = data->orientation;
563
564    float rotation0 = data->rotation0;
565    float rotation1 = data->rotation1;
566    float rotation2 = data->rotation2;
567    float rotation3 = data->rotation3;
568
569    uint32 animprogress = data->animprogress;
570    uint32 go_state = data->go_state;
571
572    m_DBTableGuid = guid;
573    if (map->GetInstanceId() != 0) guid = objmgr.GenerateLowGuid(HIGHGUID_GAMEOBJECT);
574
575    if (!Create(guid,entry, map, x, y, z, ang, rotation0, rotation1, rotation2, rotation3, animprogress, go_state) )
576        return false;
577
578    switch(GetGOInfo()->type)
579    {
580        case GAMEOBJECT_TYPE_DOOR:
581        case GAMEOBJECT_TYPE_BUTTON:
582            /* this code (in comment) isn't correct because in battlegrounds we need despawnable doors and buttons, pls remove
583            SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
584            m_spawnedByDefault = true;
585            m_respawnDelayTime = 0;
586            m_respawnTime = 0;
587            break;*/
588        default:
589            if(data->spawntimesecs >= 0)
590            {
591                m_spawnedByDefault = true;
592                m_respawnDelayTime = data->spawntimesecs;
593                m_respawnTime = objmgr.GetGORespawnTime(m_DBTableGuid, map->GetInstanceId());
594
595                                                            // ready to respawn
596                if(m_respawnTime && m_respawnTime <= time(NULL))
597                {
598                    m_respawnTime = 0;
599                    objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
600                }
601            }
602            else
603            {
604                m_spawnedByDefault = false;
605                m_respawnDelayTime = -data->spawntimesecs;
606                m_respawnTime = 0;
607            }
608            break;
609    }
610
611    return true;
612}
613
614void GameObject::DeleteFromDB()
615{
616    objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
617    objmgr.DeleteGOData(m_DBTableGuid);
618    WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid);
619    WorldDatabase.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid);
620}
621
622GameObject* GameObject::GetGameObject(WorldObject& object, uint64 guid)
623{
624    return ObjectAccessor::GetGameObject(object,guid);
625}
626
627GameObjectInfo const *GameObject::GetGOInfo() const
628{
629    return m_goInfo;
630}
631
632uint32 GameObject::GetLootId(GameObjectInfo const* ginfo)
633{
634    if (!ginfo)
635        return 0;
636
637    switch(ginfo->type)
638    {
639        case GAMEOBJECT_TYPE_CHEST:
640            return ginfo->chest.lootId;
641        case GAMEOBJECT_TYPE_FISHINGHOLE:
642            return ginfo->fishinghole.lootId;
643        case GAMEOBJECT_TYPE_FISHINGNODE:
644            return ginfo->fishnode.lootId;
645        default:
646            return 0;
647    }
648}
649
650/*********************************************************/
651/***                    QUEST SYSTEM                   ***/
652/*********************************************************/
653bool GameObject::hasQuest(uint32 quest_id) const
654{
655    QuestRelations const& qr = objmgr.mGOQuestRelations;
656    for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr)
657    {
658        if(itr->second==quest_id)
659            return true;
660    }
661    return false;
662}
663
664bool GameObject::hasInvolvedQuest(uint32 quest_id) const
665{
666    QuestRelations const& qr = objmgr.mGOQuestInvolvedRelations;
667    for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr)
668    {
669        if(itr->second==quest_id)
670            return true;
671    }
672    return false;
673}
674
675bool GameObject::IsTransport() const
676{
677    // If something is marked as a transport, don't transmit an out of range packet for it.
678    GameObjectInfo const * gInfo = GetGOInfo();
679    if(!gInfo) return false;
680    return gInfo->type == GAMEOBJECT_TYPE_TRANSPORT || gInfo->type == GAMEOBJECT_TYPE_MO_TRANSPORT;
681}
682
683Unit* GameObject::GetOwner() const
684{
685    return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
686}
687
688void GameObject::SaveRespawnTime()
689{
690    if(m_respawnTime > time(NULL) && m_spawnedByDefault)
691        objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),m_respawnTime);
692}
693
694bool GameObject::isVisibleForInState(Player const* u, bool inVisibleList) const
695{
696    // Not in world
697    if(!IsInWorld() || !u->IsInWorld())
698        return false;
699
700    // Transport always visible at this step implementation
701    if(IsTransport() && IsInMap(u))
702        return true;
703
704    // quick check visibility false cases for non-GM-mode
705    if(!u->isGameMaster())
706    {
707        // despawned and then not visible for non-GM in GM-mode
708        if(!isSpawned())
709            return false;
710
711        // special invisibility cases
712        /* TODO: implement trap stealth, take look at spell 2836
713        if(GetGOInfo()->type == GAMEOBJECT_TYPE_TRAP && GetGOInfo()->trap.stealthed && u->IsHostileTo(GetOwner()))
714        {
715            if(check stuff here)
716                return false;
717        }*/
718
719        // Smuggled Mana Cell required 10 invisibility type detection/state
720        if(GetEntry()==187039 && ((u->m_detectInvisibilityMask | u->m_invisibilityMask) & (1<<10))==0)
721            return false;
722    }
723
724    // check distance
725    return IsWithinDistInMap(u,World::GetMaxVisibleDistanceForObject() +
726        (inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f) );
727}
728
729void GameObject::Respawn()
730{
731    if(m_spawnedByDefault && m_respawnTime > 0)
732    {
733        m_respawnTime = time(NULL);
734        objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
735    }
736}
737
738bool GameObject::ActivateToQuest( Player *pTarget)const
739{
740    if(!objmgr.IsGameObjectForQuests(GetEntry()))
741        return false;
742
743    switch(GetGoType())
744    {
745        // scan GO chest with loot including quest items
746        case GAMEOBJECT_TYPE_CHEST:
747        {
748            if(LootTemplates_Gameobject.HaveQuestLootForPlayer(GetLootId(), pTarget))
749                return true;
750            break;
751        }
752        case GAMEOBJECT_TYPE_GOOBER:
753        {
754            if(pTarget->GetQuestStatus(GetGOInfo()->goober.questId) == QUEST_STATUS_INCOMPLETE)
755                return true;
756            break;
757        }
758        default:
759            break;
760    }
761
762    return false;
763}
764
765void GameObject::TriggeringLinkedGameObject( uint32 trapEntry, Unit* target)
766{
767    GameObjectInfo const* trapInfo = sGOStorage.LookupEntry<GameObjectInfo>(trapEntry);
768    if(!trapInfo || trapInfo->type!=GAMEOBJECT_TYPE_TRAP)
769        return;
770
771    SpellEntry const* trapSpell = sSpellStore.LookupEntry(trapInfo->trap.spellId);
772    if(!trapSpell)                                          // checked at load already
773        return;
774
775    float range = GetSpellMaxRange(sSpellRangeStore.LookupEntry(trapSpell->rangeIndex));
776
777    // search nearest linked GO
778    GameObject* trapGO = NULL;
779    {
780        // using original GO distance
781        CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
782        Cell cell(p);
783        cell.data.Part.reserved = ALL_DISTRICT;
784
785        MaNGOS::NearestGameObjectEntryInObjectRangeCheck go_check(*target,trapEntry,range);
786        MaNGOS::GameObjectLastSearcher<MaNGOS::NearestGameObjectEntryInObjectRangeCheck> checker(trapGO,go_check);
787
788        TypeContainerVisitor<MaNGOS::GameObjectLastSearcher<MaNGOS::NearestGameObjectEntryInObjectRangeCheck>, GridTypeMapContainer > object_checker(checker);
789        CellLock<GridReadGuard> cell_lock(cell, p);
790        cell_lock->Visit(cell_lock, object_checker, *MapManager::Instance().GetMap(GetMapId(), this));
791    }
792
793    // found correct GO
794    // FIXME: when GO casting will be implemented trap must cast spell to target
795    if(trapGO)
796        target->CastSpell(target,trapSpell,true);
797}
798
799GameObject* GameObject::LookupFishingHoleAround(float range)
800{
801    GameObject* ok = NULL;
802
803    CellPair p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
804    Cell cell(p);
805    cell.data.Part.reserved = ALL_DISTRICT;
806    MaNGOS::NearestGameObjectFishingHole u_check(*this, range);
807    MaNGOS::GameObjectSearcher<MaNGOS::NearestGameObjectFishingHole> checker(ok, u_check);
808
809    CellLock<GridReadGuard> cell_lock(cell, p);
810
811    TypeContainerVisitor<MaNGOS::GameObjectSearcher<MaNGOS::NearestGameObjectFishingHole>, GridTypeMapContainer > grid_object_checker(checker);
812    cell_lock->Visit(cell_lock, grid_object_checker, *MapManager::Instance().GetMap(GetMapId(), this));
813
814    return ok;
815}
816
817void GameObject::UseDoorOrButton(uint32 time_to_restore)
818{
819    if(m_lootState != GO_READY)
820        return;
821
822    if(!time_to_restore)
823        time_to_restore = GetAutoCloseTime();
824
825    SwitchDoorOrButton(true);
826    SetLootState(GO_ACTIVATED);
827
828    m_cooldownTime = time(NULL) + time_to_restore;
829
830}
831
832void GameObject::SwitchDoorOrButton(bool activate)
833{
834    if(activate)
835        SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
836    else
837        RemoveFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
838
839    if(GetGoState())                                        //if closed -> open
840        SetGoState(0);
841    else                                                    //if open -> close
842        SetGoState(1);
843}
844
845void GameObject::Use(Unit* user)
846{
847    // by default spell caster is user
848    Unit* spellCaster = user;
849    uint32 spellId = 0;
850
851    switch(GetGoType())
852    {
853        case GAMEOBJECT_TYPE_DOOR:                          //0
854        case GAMEOBJECT_TYPE_BUTTON:                        //1
855            //doors/buttons never really despawn, only reset to default state/flags
856            UseDoorOrButton();
857
858            // activate script
859            sWorld.ScriptsStart(sGameObjectScripts, GetDBTableGUIDLow(), spellCaster, this);
860            return;
861
862        case GAMEOBJECT_TYPE_QUESTGIVER:                    //2
863        {
864            if(user->GetTypeId()!=TYPEID_PLAYER)
865                return;
866
867            Player* player = (Player*)user;
868
869            player->PrepareQuestMenu( GetGUID() );
870            player->SendPreparedQuest( GetGUID() );
871            return;
872        }
873        //Sitting: Wooden bench, chairs enzz
874        case GAMEOBJECT_TYPE_CHAIR:                         //7
875        {
876            GameObjectInfo const* info = GetGOInfo();
877            if(!info)
878                return;
879
880            if(user->GetTypeId()!=TYPEID_PLAYER)
881                return;
882
883            Player* player = (Player*)user;
884
885            // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
886
887            // check if the db is sane
888            if(info->chair.slots > 0)
889            {
890                float lowestDist = DEFAULT_VISIBILITY_DISTANCE;
891
892                float x_lowest = GetPositionX();
893                float y_lowest = GetPositionY();
894
895                // the object orientation + 1/2 pi
896                // every slot will be on that straight line
897                float orthogonalOrientation = GetOrientation()+M_PI*0.5f;
898                // find nearest slot
899                for(uint32 i=0; i<info->chair.slots; i++)
900                {
901                    // the distance between this slot and the center of the go - imagine a 1D space
902                    float relativeDistance = (info->size*i)-(info->size*(info->chair.slots-1)/2.0f);
903
904                    float x_i = GetPositionX() + relativeDistance * cos(orthogonalOrientation);
905                    float y_i = GetPositionY() + relativeDistance * sin(orthogonalOrientation);
906
907                    // calculate the distance between the player and this slot
908                    float thisDistance = player->GetDistance2d(x_i, y_i);
909
910                    /* debug code. It will spawn a npc on each slot to visualize them.
911                    Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
912                    std::ostringstream output;
913                    output << i << ": thisDist: " << thisDistance;
914                    helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
915                    */
916
917                    if(thisDistance <= lowestDist)
918                    {
919                        lowestDist = thisDistance;
920                        x_lowest = x_i;
921                        y_lowest = y_i;
922                    }
923                }
924                player->TeleportTo(GetMapId(), x_lowest, y_lowest, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
925            }
926            else
927            {
928                // fallback, will always work
929                player->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
930            }
931            player->SetStandState(PLAYER_STATE_SIT_LOW_CHAIR+info->chair.height);
932            return;
933        }
934        //big gun, its a spell/aura
935        case GAMEOBJECT_TYPE_GOOBER:                        //10
936        {
937            GameObjectInfo const* info = GetGOInfo();
938
939            if(user->GetTypeId()==TYPEID_PLAYER)
940            {
941                Player* player = (Player*)user;
942
943                // show page
944                if(info->goober.pageId)
945                {
946                    WorldPacket data(SMSG_GAMEOBJECT_PAGETEXT, 8);
947                    data << GetGUID();
948                    player->GetSession()->SendPacket(&data);
949                }
950
951                // possible quest objective for active quests
952                player->CastedCreatureOrGO(info->id, GetGUID(), 0);
953            }
954
955            // cast this spell later if provided
956            spellId = info->goober.spellId;
957
958            break;
959        }
960        case GAMEOBJECT_TYPE_CAMERA:                        //13
961        {
962            GameObjectInfo const* info = GetGOInfo();
963            if(!info)
964                return;
965
966            if(user->GetTypeId()!=TYPEID_PLAYER)
967                return;
968
969            Player* player = (Player*)user;
970
971            if(info->camera.cinematicId)
972            {
973                WorldPacket data(SMSG_TRIGGER_CINEMATIC, 4);
974                data << info->camera.cinematicId;
975                player->GetSession()->SendPacket(&data);
976            }
977            return;
978        }
979        //fishing bobber
980        case GAMEOBJECT_TYPE_FISHINGNODE:                   //17
981        {
982            if(user->GetTypeId()!=TYPEID_PLAYER)
983                return;
984
985            Player* player = (Player*)user;
986
987            if(player->GetGUID() != GetOwnerGUID())
988                return;
989
990            switch(getLootState())
991            {
992                case GO_READY:                              // ready for loot
993                {
994                    // 1) skill must be >= base_zone_skill
995                    // 2) if skill == base_zone_skill => 5% chance
996                    // 3) chance is linear dependence from (base_zone_skill-skill)
997
998                    uint32 subzone = GetAreaId();
999
1000                    int32 zone_skill = objmgr.GetFishingBaseSkillLevel( subzone );
1001                    if(!zone_skill)
1002                        zone_skill = objmgr.GetFishingBaseSkillLevel( GetZoneId() );
1003
1004                    //provide error, no fishable zone or area should be 0
1005                    if(!zone_skill)
1006                        sLog.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone);
1007
1008                    int32 skill = player->GetSkillValue(SKILL_FISHING);
1009                    int32 chance = skill - zone_skill + 5;
1010                    int32 roll = irand(1,100);
1011
1012                    DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill,zone_skill,chance,roll);
1013
1014                    if(skill >= zone_skill && chance >= roll)
1015                    {
1016                        // prevent removing GO at spell cancel
1017                        player->RemoveGameObject(this,false);
1018                        SetOwnerGUID(player->GetGUID());
1019
1020                        //fish catched
1021                        player->UpdateFishingSkill();
1022
1023                        GameObject* ok = LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE);
1024                        if (ok)
1025                        {
1026                            player->SendLoot(ok->GetGUID(),LOOT_FISHINGHOLE);
1027                            SetLootState(GO_JUST_DEACTIVATED);
1028                        }
1029                        else
1030                            player->SendLoot(GetGUID(),LOOT_FISHING);
1031                    }
1032                    else
1033                    {
1034                        // fish escaped, can be deleted now
1035                        SetLootState(GO_JUST_DEACTIVATED);
1036
1037                        WorldPacket data(SMSG_FISH_ESCAPED, 0);
1038                        player->GetSession()->SendPacket(&data);
1039                    }
1040                    break;
1041                }
1042                case GO_JUST_DEACTIVATED:                   // nothing to do, will be deleted at next update
1043                    break;
1044                default:
1045                {
1046                    SetLootState(GO_JUST_DEACTIVATED);
1047
1048                    WorldPacket data(SMSG_FISH_NOT_HOOKED, 0);
1049                    player->GetSession()->SendPacket(&data);
1050                    break;
1051                }
1052            }
1053
1054            if(player->m_currentSpells[CURRENT_CHANNELED_SPELL])
1055            {
1056                player->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
1057                player->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
1058            }
1059            return;
1060        }
1061
1062        case GAMEOBJECT_TYPE_SUMMONING_RITUAL:              //18
1063        {
1064            if(user->GetTypeId()!=TYPEID_PLAYER)
1065                return;
1066
1067            Player* player = (Player*)user;
1068
1069            Unit* caster = GetOwner();
1070
1071            GameObjectInfo const* info = GetGOInfo();
1072
1073            if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1074                return;
1075
1076            // accept only use by player from same group for caster except caster itself
1077            if(((Player*)caster)==player || !((Player*)caster)->IsInSameRaidWith(player))
1078                return;
1079
1080            AddUniqueUse(player);
1081
1082            // full amount unique participants including original summoner
1083            if(GetUniqueUseCount() < info->summoningRitual.reqParticipants)
1084                return;
1085
1086            // in case summoning ritual caster is GO creator
1087            spellCaster = caster;
1088
1089            if(!caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
1090                return;
1091
1092            spellId = info->summoningRitual.spellId;
1093
1094            // finish spell
1095            caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
1096            caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
1097
1098            // can be deleted now
1099            SetLootState(GO_JUST_DEACTIVATED);
1100
1101            // go to end function to spell casting
1102            break;
1103        }
1104        case GAMEOBJECT_TYPE_SPELLCASTER:                   //22
1105        {
1106            SetUInt32Value(GAMEOBJECT_FLAGS,2);
1107
1108            GameObjectInfo const* info = GetGOInfo();
1109            if(!info)
1110                return;
1111
1112            if(info->spellcaster.partyOnly)
1113            {
1114                Unit* caster = GetOwner();
1115                if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1116                    return;
1117
1118                if(user->GetTypeId()!=TYPEID_PLAYER || !((Player*)user)->IsInSameRaidWith((Player*)caster))
1119                    return;
1120            }
1121
1122            spellId = info->spellcaster.spellId;
1123
1124            AddUse();
1125            break;
1126        }
1127        case GAMEOBJECT_TYPE_MEETINGSTONE:                  //23
1128        {
1129            GameObjectInfo const* info = GetGOInfo();
1130
1131            if(user->GetTypeId()!=TYPEID_PLAYER)
1132                return;
1133
1134            Player* player = (Player*)user;
1135
1136            Player* targetPlayer = ObjectAccessor::FindPlayer(player->GetSelection());
1137
1138            // accept only use by player from same group for caster except caster itself
1139            if(!targetPlayer || targetPlayer == player || !targetPlayer->IsInSameGroupWith(player))
1140                return;
1141
1142            //required lvl checks!
1143            uint8 level = player->getLevel();
1144            if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1145                return;
1146            level = targetPlayer->getLevel();
1147            if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1148                return;
1149
1150            spellId = 23598;
1151
1152            break;
1153        }
1154
1155        case GAMEOBJECT_TYPE_FLAGSTAND:                     // 24
1156        {
1157            if(user->GetTypeId()!=TYPEID_PLAYER)
1158                return;
1159
1160            Player* player = (Player*)user;
1161
1162            if( player->isAllowUseBattleGroundObject() )
1163            {
1164                // in battleground check
1165                BattleGround *bg = player->GetBattleGround();
1166                if(!bg)
1167                    return;
1168                // BG flag click
1169                // AB:
1170                // 15001
1171                // 15002
1172                // 15003
1173                // 15004
1174                // 15005
1175                bg->EventPlayerClickedOnFlag(player, this);
1176                return;                                     //we don;t need to delete flag ... it is despawned!
1177            }
1178            break;
1179        }
1180        case GAMEOBJECT_TYPE_FLAGDROP:                      // 26
1181        {
1182            if(user->GetTypeId()!=TYPEID_PLAYER)
1183                return;
1184
1185            Player* player = (Player*)user;
1186
1187            if( player->isAllowUseBattleGroundObject() )     
1188            {
1189                // in battleground check
1190                BattleGround *bg = player->GetBattleGround();
1191                if(!bg)
1192                    return;
1193                // BG flag dropped
1194                // WS:
1195                // 179785 - Silverwing Flag
1196                // 179786 - Warsong Flag
1197                // EotS:
1198                // 184142 - Netherstorm Flag
1199                GameObjectInfo const* info = GetGOInfo();
1200                if(info)
1201                {
1202                    switch(info->id)
1203                    {
1204                        case 179785:                        // Silverwing Flag
1205                            // check if it's correct bg
1206                            if(bg->GetTypeID() == BATTLEGROUND_WS)
1207                                bg->EventPlayerClickedOnFlag(player, this);
1208                            break;
1209                        case 179786:                        // Warsong Flag
1210                            if(bg->GetTypeID() == BATTLEGROUND_WS)
1211                                bg->EventPlayerClickedOnFlag(player, this);
1212                            break;
1213                        case 184142:                        // Netherstorm Flag
1214                            if(bg->GetTypeID() == BATTLEGROUND_EY)
1215                                bg->EventPlayerClickedOnFlag(player, this);
1216                            break;
1217                    }
1218                }
1219                //this cause to call return, all flags must be deleted here!!
1220                spellId = 0;
1221                Delete();
1222            }
1223            break;
1224        }
1225        default:
1226            sLog.outDebug("Unknown Object Type %u", GetGoType());
1227            break;
1228    }
1229
1230    if(!spellId)
1231        return;
1232
1233    SpellEntry const *spellInfo = sSpellStore.LookupEntry( spellId );
1234    if(!spellInfo)
1235    {
1236        sLog.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId,GetEntry(),GetGoType());
1237        return;
1238    }
1239
1240    Spell *spell = new Spell(spellCaster, spellInfo, false);
1241
1242    // spell target is user of GO
1243    SpellCastTargets targets;
1244    targets.setUnitTarget( user );
1245
1246    spell->prepare(&targets);
1247}
Note: See TracBrowser for help on using the browser.