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

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