root/trunk/src/game/GameObject.cpp

Revision 283, 46.3 kB (checked in by yumileroy, 17 years ago)

*Alterac Valley. By Bogie and Balrok. Note: some core contents are modified. Will fix them later. Some sql are disabled because of possible conflict with offical DB. Use them at your own risk.

Original author: megamage
Date: 2008-11-21 19:45:49-06:00

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