2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
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.
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.
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
21 #include "GameObject.h"
22 #include "ObjectMgr.h"
23 #include "PoolHandler.h"
26 #include "UpdateMask.h"
28 #include "WorldPacket.h"
30 #include "Database/DatabaseEnv.h"
32 #include "GridNotifiers.h"
33 #include "GridNotifiersImpl.h"
35 #include "InstanceData.h"
36 #include "BattleGround.h"
39 GameObject::GameObject() : WorldObject()
41 m_objectType
|= TYPEMASK_GAMEOBJECT
;
42 m_objectTypeId
= TYPEID_GAMEOBJECT
;
44 m_updateFlag
= (UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HAS_POSITION
| UPDATEFLAG_POSITION
| UPDATEFLAG_ROTATION
);
46 m_valuesCount
= GAMEOBJECT_END
;
48 m_respawnDelayTime
= 25;
49 m_lootState
= GO_NOT_READY
;
50 m_spawnedByDefault
= true;
60 GameObject::~GameObject()
62 CleanupsBeforeDelete();
65 void GameObject::CleanupsBeforeDelete()
67 if(m_uint32Values
) // field array can be not exist if GameOBject not loaded
69 // Possible crash at access to deleted GO in Unit::m_gameobj
70 if(uint64 owner_guid
= GetOwnerGUID())
73 if(IS_PLAYER_GUID(owner_guid
))
74 owner
= ObjectAccessor::GetObjectInWorld(owner_guid
, (Player
*)NULL
);
76 owner
= ObjectAccessor::GetUnit(*this,owner_guid
);
79 owner
->RemoveGameObject(this,false);
82 const char * ownerType
= "creature";
83 if(IS_PLAYER_GUID(owner_guid
))
85 else if(IS_PET_GUID(owner_guid
))
88 sLog
.outError("Delete GameObject (GUID: %u Entry: %u SpellId %u LinkedGO %u) that lost references to owner (GUID %u Type '%s') GO list. Crash possible later.",
89 GetGUIDLow(), GetGOInfo()->id
, m_spellId
, GetGOInfo()->GetLinkedGameObjectEntry(), GUID_LOPART(owner_guid
), ownerType
);
95 void GameObject::AddToWorld()
97 ///- Register the gameobject for guid lookup
98 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
102 void GameObject::RemoveFromWorld()
104 ///- Remove the gameobject from the accessor
105 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
106 Object::RemoveFromWorld();
109 bool GameObject::Create(uint32 guidlow
, uint32 name_id
, Map
*map
, uint32 phaseMask
, float x
, float y
, float z
, float ang
, float rotation0
, float rotation1
, float rotation2
, float rotation3
, uint32 animprogress
, GOState go_state
)
114 SetPhaseMask(phaseMask
,false);
116 if(!IsPositionValid())
118 sLog
.outError("Gameobject (GUID: %u Entry: %u ) not created. Suggested coordinates isn't valid (X: %f Y: %f)",guidlow
,name_id
,x
,y
);
122 GameObjectInfo
const* goinfo
= objmgr
.GetGameObjectInfo(name_id
);
125 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
);
129 Object::_Create(guidlow
, goinfo
->id
, HIGHGUID_GAMEOBJECT
);
133 if (goinfo
->type
>= MAX_GAMEOBJECT_TYPE
)
135 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
);
139 SetFloatValue(GAMEOBJECT_PARENTROTATION
+0, rotation0
);
140 SetFloatValue(GAMEOBJECT_PARENTROTATION
+1, rotation1
);
142 UpdateRotationFields(rotation2
,rotation3
); // GAMEOBJECT_FACING, GAMEOBJECT_ROTATION, GAMEOBJECT_PARENTROTATION+2/3
144 SetFloatValue(OBJECT_FIELD_SCALE_X
, goinfo
->size
);
146 SetUInt32Value(GAMEOBJECT_FACTION
, goinfo
->faction
);
147 SetUInt32Value(GAMEOBJECT_FLAGS
, goinfo
->flags
);
149 SetEntry(goinfo
->id
);
151 SetUInt32Value(GAMEOBJECT_DISPLAYID
, goinfo
->displayId
);
153 SetGoState(go_state
);
154 SetGoType(GameobjectTypes(goinfo
->type
));
156 SetGoAnimProgress(animprogress
);
158 //Notify the map's instance data.
159 //Only works if you create the object in it, not if it is moves to that map.
160 //Normally non-players do not teleport to other maps.
161 if(map
->IsDungeon() && ((InstanceMap
*)map
)->GetInstanceData())
163 ((InstanceMap
*)map
)->GetInstanceData()->OnObjectCreate(this);
169 void GameObject::Update(uint32
/*p_time*/)
171 if (IS_MO_TRANSPORT(GetGUID()))
173 //((Transport*)this)->Update(p_time);
183 case GAMEOBJECT_TYPE_TRAP
:
185 // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
186 Unit
* owner
= GetOwner();
187 if (owner
&& ((Player
*)owner
)->isInCombat())
188 m_cooldownTime
= time(NULL
) + GetGOInfo()->trap
.startDelay
;
189 m_lootState
= GO_READY
;
192 case GAMEOBJECT_TYPE_FISHINGNODE
:
194 // fishing code (bobber ready)
195 if( time(NULL
) > m_respawnTime
- FISHING_BOBBER_READY_TIME
)
197 // splash bobber (bobber ready now)
198 Unit
* caster
= GetOwner();
199 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
201 SetGoState(GO_STATE_ACTIVE
);
202 SetUInt32Value(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
206 BuildValuesUpdateBlockForPlayer(&udata
,((Player
*)caster
));
207 udata
.BuildPacket(&packet
);
208 ((Player
*)caster
)->GetSession()->SendPacket(&packet
);
210 WorldPacket
data(SMSG_GAMEOBJECT_CUSTOM_ANIM
,8+4);
213 ((Player
*)caster
)->SendMessageToSet(&data
,true);
216 m_lootState
= GO_READY
; // can be successfully open with some chance
221 m_lootState
= GO_READY
; // for other GOis same switched without delay to GO_READY
224 // NO BREAK for switch (m_lootState)
228 if (m_respawnTime
> 0) // timer on
230 if (m_respawnTime
<= time(NULL
)) // timer expired
233 m_SkillupList
.clear();
238 case GAMEOBJECT_TYPE_FISHINGNODE
: // can't fish now
240 Unit
* caster
= GetOwner();
241 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
243 caster
->FinishSpell(CURRENT_CHANNELED_SPELL
);
245 WorldPacket
data(SMSG_FISH_NOT_HOOKED
,0);
246 ((Player
*)caster
)->GetSession()->SendPacket(&data
);
249 m_lootState
= GO_JUST_DEACTIVATED
;
252 case GAMEOBJECT_TYPE_DOOR
:
253 case GAMEOBJECT_TYPE_BUTTON
:
254 //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)
255 if (GetGoState() != GO_STATE_READY
)
257 //flags in AB are type_button and we need to add them here so no break!
259 if (!m_spawnedByDefault
) // despawn timer
261 // can be despawned or destroyed
262 SetLootState(GO_JUST_DEACTIVATED
);
266 uint16 poolid
= poolhandler
.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT
);
268 poolhandler
.UpdatePool(poolid
, GetGUIDLow(), TYPEID_GAMEOBJECT
);
278 // traps can have time and can not have
279 GameObjectInfo
const* goInfo
= GetGOInfo();
280 if(goInfo
->type
== GAMEOBJECT_TYPE_TRAP
)
282 if(m_cooldownTime
>= time(NULL
))
286 Unit
* owner
= GetOwner();
287 Unit
* ok
= NULL
; // pointer to appropriate target if found any
289 bool IsBattleGroundTrap
= false;
290 //FIXME: this is activation radius (in different casting radius that must be selected from spell data)
291 //TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
292 float radius
= goInfo
->trap
.radius
;
295 if(goInfo
->trap
.cooldown
!= 3) // cast in other case (at some triggering/linked go/etc explicit call)
299 if(m_respawnTime
> 0)
302 radius
= goInfo
->trap
.cooldown
; // battlegrounds gameobjects has data2 == 0 && data5 == 3
303 IsBattleGroundTrap
= true;
307 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
309 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
311 // Note: this hack with search required until GO casting not implemented
312 // search unfriendly creature
313 if(owner
&& goInfo
->trap
.charges
> 0) // hunter trap
315 MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
u_check(this, owner
, radius
);
316 MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
> checker(this,ok
, u_check
);
318 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
320 TypeContainerVisitor
<MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_object_checker(checker
);
321 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap());
323 // or unfriendly player/pet
326 TypeContainerVisitor
<MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_object_checker(checker
);
327 cell_lock
->Visit(cell_lock
, world_object_checker
, *GetMap());
330 else // environmental trap
332 // environmental damage spells already have around enemies targeting but this not help in case not existed GO casting support
334 // affect only players
336 MaNGOS::AnyPlayerInObjectRangeCheck
p_check(this, radius
);
337 MaNGOS::PlayerSearcher
<MaNGOS::AnyPlayerInObjectRangeCheck
> checker(this,p_ok
, p_check
);
339 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
341 TypeContainerVisitor
<MaNGOS::PlayerSearcher
<MaNGOS::AnyPlayerInObjectRangeCheck
>, WorldTypeMapContainer
> world_object_checker(checker
);
342 cell_lock
->Visit(cell_lock
, world_object_checker
, *GetMap());
348 Unit
*caster
= owner
? owner
: ok
;
350 caster
->CastSpell(ok
, goInfo
->trap
.spellId
, true, 0, 0, GetGUID());
351 m_cooldownTime
= time(NULL
) + 4; // 4 seconds
354 if(goInfo
->trap
.charges
> 0)
357 if(IsBattleGroundTrap
&& ok
->GetTypeId() == TYPEID_PLAYER
)
359 //BattleGround gameobjects case
360 if(((Player
*)ok
)->InBattleGround())
361 if(BattleGround
*bg
= ((Player
*)ok
)->GetBattleGround())
362 bg
->HandleTriggerBuff(GetGUID());
367 if(uint32 max_charges
= goInfo
->GetCharges())
369 if (m_usetimes
>= max_charges
)
372 SetLootState(GO_JUST_DEACTIVATED
); // can be despawned or destroyed
383 case GAMEOBJECT_TYPE_DOOR
:
384 case GAMEOBJECT_TYPE_BUTTON
:
385 if (GetGOInfo()->GetAutoCloseTime() && (m_cooldownTime
< time(NULL
)))
391 case GO_JUST_DEACTIVATED
:
393 //if Gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
394 if (GetGoType() == GAMEOBJECT_TYPE_GOOBER
)
396 uint32 spellId
= GetGOInfo()->goober
.spellId
;
400 std::set
<uint32
>::const_iterator it
= m_unique_users
.begin();
401 std::set
<uint32
>::const_iterator end
= m_unique_users
.end();
402 for (; it
!= end
; it
++)
404 Unit
* owner
= Unit::GetUnit(*this, uint64(*it
));
405 if (owner
) owner
->CastSpell(owner
, spellId
, false, 0, 0, GetGUID());
408 m_unique_users
.clear();
411 //any return here in case battleground traps
416 if(Unit
* owner
= GetOwner())
417 owner
->RemoveGameObject(this, false);
424 //burning flags in some battlegrounds, if you find better condition, just add it
425 if (GetGOInfo()->IsDespawnAtAction() || GetGoAnimProgress() > 0)
427 SendObjectDeSpawnAnim(GetGUID());
429 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
433 SetLootState(GO_READY
);
435 if(!m_respawnDelayTime
)
438 if(!m_spawnedByDefault
)
444 m_respawnTime
= time(NULL
) + m_respawnDelayTime
;
446 // if option not set then object will be saved at grid unload
447 if(sWorld
.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY
))
450 ObjectAccessor::UpdateObjectVisibility(this);
457 void GameObject::Refresh()
459 // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
460 if(m_respawnTime
> 0 && m_spawnedByDefault
)
467 void GameObject::AddUniqueUse(Player
* player
)
470 m_unique_users
.insert(player
->GetGUIDLow());
473 void GameObject::Delete()
475 SendObjectDeSpawnAnim(GetGUID());
477 SetGoState(GO_STATE_READY
);
478 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
480 uint16 poolid
= poolhandler
.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT
);
482 poolhandler
.UpdatePool(poolid
, GetGUIDLow(), TYPEID_GAMEOBJECT
);
484 AddObjectToRemoveList();
487 void GameObject::getFishLoot(Loot
*fishloot
, Player
* loot_owner
)
491 uint32 zone
, subzone
;
492 GetZoneAndAreaId(zone
,subzone
);
494 // if subzone loot exist use it
495 if(LootTemplates_Fishing
.HaveLootFor(subzone
))
496 fishloot
->FillLoot(subzone
, LootTemplates_Fishing
, loot_owner
,true);
497 // else use zone loot
499 fishloot
->FillLoot(zone
, LootTemplates_Fishing
, loot_owner
,true);
502 void GameObject::SaveToDB()
504 // this should only be used when the gameobject has already been loaded
505 // preferably after adding to map, because mapid may not be valid otherwise
506 GameObjectData
const *data
= objmgr
.GetGOData(m_DBTableGuid
);
509 sLog
.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
513 SaveToDB(GetMapId(), data
->spawnMask
, data
->phaseMask
);
516 void GameObject::SaveToDB(uint32 mapid
, uint8 spawnMask
, uint32 phaseMask
)
518 const GameObjectInfo
*goI
= GetGOInfo();
524 m_DBTableGuid
= GetGUIDLow();
525 // update in loaded data (changing data only in this place)
526 GameObjectData
& data
= objmgr
.NewGOData(m_DBTableGuid
);
528 // data->guid = guid don't must be update at save
529 data
.id
= GetEntry();
531 data
.phaseMask
= phaseMask
;
532 data
.posX
= GetPositionX();
533 data
.posY
= GetPositionY();
534 data
.posZ
= GetPositionZ();
535 data
.orientation
= GetOrientation();
536 data
.rotation0
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+0);
537 data
.rotation1
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+1);
538 data
.rotation2
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+2);
539 data
.rotation3
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+3);
540 data
.spawntimesecs
= m_spawnedByDefault
? m_respawnDelayTime
: -(int32
)m_respawnDelayTime
;
541 data
.animprogress
= GetGoAnimProgress();
542 data
.go_state
= GetGoState();
543 data
.spawnMask
= spawnMask
;
546 std::ostringstream ss
;
547 ss
<< "INSERT INTO gameobject VALUES ( "
548 << m_DBTableGuid
<< ", "
549 << GetEntry() << ", "
551 << uint32(spawnMask
) << "," // cast to prevent save as symbol
552 << uint16(GetPhaseMask()) << "," // prevent out of range error
553 << GetPositionX() << ", "
554 << GetPositionY() << ", "
555 << GetPositionZ() << ", "
556 << GetOrientation() << ", "
557 << GetFloatValue(GAMEOBJECT_PARENTROTATION
) << ", "
558 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+1) << ", "
559 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+2) << ", "
560 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+3) << ", "
561 << m_respawnDelayTime
<< ", "
562 << uint32(GetGoAnimProgress()) << ", "
563 << uint32(GetGoState()) << ")";
565 WorldDatabase
.BeginTransaction();
566 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
567 WorldDatabase
.PExecuteLog( ss
.str( ).c_str( ) );
568 WorldDatabase
.CommitTransaction();
571 bool GameObject::LoadFromDB(uint32 guid
, Map
*map
)
573 GameObjectData
const* data
= objmgr
.GetGOData(guid
);
577 sLog
.outErrorDb("Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid
);
581 uint32 entry
= data
->id
;
582 //uint32 map_id = data->mapid; // already used before call
583 uint32 phaseMask
= data
->phaseMask
;
584 float x
= data
->posX
;
585 float y
= data
->posY
;
586 float z
= data
->posZ
;
587 float ang
= data
->orientation
;
589 float rotation0
= data
->rotation0
;
590 float rotation1
= data
->rotation1
;
591 float rotation2
= data
->rotation2
;
592 float rotation3
= data
->rotation3
;
594 uint32 animprogress
= data
->animprogress
;
595 GOState go_state
= data
->go_state
;
597 m_DBTableGuid
= guid
;
598 if (map
->GetInstanceId() != 0) guid
= objmgr
.GenerateLowGuid(HIGHGUID_GAMEOBJECT
);
600 if (!Create(guid
,entry
, map
, phaseMask
, x
, y
, z
, ang
, rotation0
, rotation1
, rotation2
, rotation3
, animprogress
, go_state
) )
603 if(!GetGOInfo()->GetDespawnPossibility() && !GetGOInfo()->IsDespawnAtAction())
605 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
606 m_spawnedByDefault
= true;
607 m_respawnDelayTime
= 0;
612 if(data
->spawntimesecs
>= 0)
614 m_spawnedByDefault
= true;
615 m_respawnDelayTime
= data
->spawntimesecs
;
616 m_respawnTime
= objmgr
.GetGORespawnTime(m_DBTableGuid
, map
->GetInstanceId());
619 if(m_respawnTime
&& m_respawnTime
<= time(NULL
))
622 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
627 m_spawnedByDefault
= false;
628 m_respawnDelayTime
= -data
->spawntimesecs
;
636 void GameObject::DeleteFromDB()
638 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
639 objmgr
.DeleteGOData(m_DBTableGuid
);
640 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
641 WorldDatabase
.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid
);
644 GameObjectInfo
const *GameObject::GetGOInfo() const
649 /*********************************************************/
650 /*** QUEST SYSTEM ***/
651 /*********************************************************/
652 bool GameObject::hasQuest(uint32 quest_id
) const
654 QuestRelations
const& qr
= objmgr
.mGOQuestRelations
;
655 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
657 if(itr
->second
==quest_id
)
663 bool GameObject::hasInvolvedQuest(uint32 quest_id
) const
665 QuestRelations
const& qr
= objmgr
.mGOQuestInvolvedRelations
;
666 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
668 if(itr
->second
==quest_id
)
674 bool GameObject::IsTransport() const
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
;
682 Unit
* GameObject::GetOwner() const
684 return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
687 void GameObject::SaveRespawnTime()
689 if(m_respawnTime
> time(NULL
) && m_spawnedByDefault
)
690 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),m_respawnTime
);
693 bool GameObject::isVisibleForInState(Player
const* u
, WorldObject
const* viewPoint
, bool inVisibleList
) const
696 if(!IsInWorld() || !u
->IsInWorld())
699 // Transport always visible at this step implementation
700 if(IsTransport() && IsInMap(u
))
703 // quick check visibility false cases for non-GM-mode
704 if(!u
->isGameMaster())
706 // despawned and then not visible for non-GM in GM-mode
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()))
720 return IsWithinDistInMap(viewPoint
,World::GetMaxVisibleDistanceForObject() +
721 (inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);
724 void GameObject::Respawn()
726 if(m_spawnedByDefault
&& m_respawnTime
> 0)
728 m_respawnTime
= time(NULL
);
729 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
733 bool GameObject::ActivateToQuest( Player
*pTarget
)const
735 if(!objmgr
.IsGameObjectForQuests(GetEntry()))
740 // scan GO chest with loot including quest items
741 case GAMEOBJECT_TYPE_CHEST
:
743 if(LootTemplates_Gameobject
.HaveQuestLootForPlayer(GetGOInfo()->GetLootId(), pTarget
))
747 case GAMEOBJECT_TYPE_GOOBER
:
749 if(pTarget
->GetQuestStatus(GetGOInfo()->goober
.questId
) == QUEST_STATUS_INCOMPLETE
)
760 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry
, Unit
* target
)
762 GameObjectInfo
const* trapInfo
= sGOStorage
.LookupEntry
<GameObjectInfo
>(trapEntry
);
763 if(!trapInfo
|| trapInfo
->type
!=GAMEOBJECT_TYPE_TRAP
)
766 SpellEntry
const* trapSpell
= sSpellStore
.LookupEntry(trapInfo
->trap
.spellId
);
767 if(!trapSpell
) // checked at load already
770 float range
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(trapSpell
->rangeIndex
));
772 // search nearest linked GO
773 GameObject
* trapGO
= NULL
;
775 // using original GO distance
776 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
778 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
780 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*target
,trapEntry
,range
);
781 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(this, trapGO
,go_check
);
783 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
784 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
785 cell_lock
->Visit(cell_lock
, object_checker
, *GetMap());
789 // FIXME: when GO casting will be implemented trap must cast spell to target
791 target
->CastSpell(target
,trapSpell
,true, 0, 0, GetGUID());
794 GameObject
* GameObject::LookupFishingHoleAround(float range
)
796 GameObject
* ok
= NULL
;
798 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
800 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
801 MaNGOS::NearestGameObjectFishingHole
u_check(*this, range
);
802 MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
> checker(this, ok
, u_check
);
804 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
806 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
>, GridTypeMapContainer
> grid_object_checker(checker
);
807 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap());
812 void GameObject::ResetDoorOrButton()
814 if (m_lootState
== GO_READY
|| m_lootState
== GO_JUST_DEACTIVATED
)
817 SwitchDoorOrButton(false);
818 SetLootState(GO_JUST_DEACTIVATED
);
822 void GameObject::UseDoorOrButton(uint32 time_to_restore
, bool alternative
/* = false */)
824 if(m_lootState
!= GO_READY
)
828 time_to_restore
= GetGOInfo()->GetAutoCloseTime();
830 SwitchDoorOrButton(true,alternative
);
831 SetLootState(GO_ACTIVATED
);
833 m_cooldownTime
= time(NULL
) + time_to_restore
;
836 void GameObject::SwitchDoorOrButton(bool activate
, bool alternative
/* = false */)
839 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
841 RemoveFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
843 if(GetGoState() == GO_STATE_READY
) //if closed -> open
844 SetGoState(alternative
? GO_STATE_ACTIVE_ALTERNATIVE
: GO_STATE_ACTIVE
);
845 else //if open -> close
846 SetGoState(GO_STATE_READY
);
849 void GameObject::Use(Unit
* user
)
851 // by default spell caster is user
852 Unit
* spellCaster
= user
;
854 bool triggered
= false;
858 case GAMEOBJECT_TYPE_DOOR
: //0
859 case GAMEOBJECT_TYPE_BUTTON
: //1
860 //doors/buttons never really despawn, only reset to default state/flags
864 GetMap()->ScriptsStart(sGameObjectScripts
, GetDBTableGUIDLow(), spellCaster
, this);
867 case GAMEOBJECT_TYPE_QUESTGIVER
: //2
869 if(user
->GetTypeId()!=TYPEID_PLAYER
)
872 Player
* player
= (Player
*)user
;
874 player
->PrepareQuestMenu( GetGUID() );
875 player
->SendPreparedQuest( GetGUID() );
878 //Sitting: Wooden bench, chairs enzz
879 case GAMEOBJECT_TYPE_CHAIR
: //7
881 GameObjectInfo
const* info
= GetGOInfo();
885 if(user
->GetTypeId()!=TYPEID_PLAYER
)
888 Player
* player
= (Player
*)user
;
890 // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
892 // check if the db is sane
893 if(info
->chair
.slots
> 0)
895 float lowestDist
= DEFAULT_VISIBILITY_DISTANCE
;
897 float x_lowest
= GetPositionX();
898 float y_lowest
= GetPositionY();
900 // the object orientation + 1/2 pi
901 // every slot will be on that straight line
902 float orthogonalOrientation
= GetOrientation()+M_PI
*0.5f
;
904 for(uint32 i
=0; i
<info
->chair
.slots
; ++i
)
906 // the distance between this slot and the center of the go - imagine a 1D space
907 float relativeDistance
= (info
->size
*i
)-(info
->size
*(info
->chair
.slots
-1)/2.0f
);
909 float x_i
= GetPositionX() + relativeDistance
* cos(orthogonalOrientation
);
910 float y_i
= GetPositionY() + relativeDistance
* sin(orthogonalOrientation
);
912 // calculate the distance between the player and this slot
913 float thisDistance
= player
->GetDistance2d(x_i
, y_i
);
915 /* debug code. It will spawn a npc on each slot to visualize them.
916 Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
917 std::ostringstream output;
918 output << i << ": thisDist: " << thisDistance;
919 helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
922 if(thisDistance
<= lowestDist
)
924 lowestDist
= thisDistance
;
929 player
->TeleportTo(GetMapId(), x_lowest
, y_lowest
, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
933 // fallback, will always work
934 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
936 player
->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR
+info
->chair
.height
);
939 //big gun, its a spell/aura
940 case GAMEOBJECT_TYPE_GOOBER
: //10
942 GameObjectInfo
const* info
= GetGOInfo();
944 if(user
->GetTypeId()==TYPEID_PLAYER
)
946 Player
* player
= (Player
*)user
;
949 if(info
->goober
.pageId
)
951 WorldPacket
data(SMSG_GAMEOBJECT_PAGETEXT
, 8);
953 player
->GetSession()->SendPacket(&data
);
956 // possible quest objective for active quests
957 player
->CastedCreatureOrGO(info
->id
, GetGUID(), 0);
959 if (info
->goober
.eventId
)
960 GetMap()->ScriptsStart(sEventScripts
, info
->goober
.eventId
, player
, this);
963 // cast this spell later if provided
964 spellId
= info
->goober
.spellId
;
968 case GAMEOBJECT_TYPE_CAMERA
: //13
970 GameObjectInfo
const* info
= GetGOInfo();
974 if(user
->GetTypeId()!=TYPEID_PLAYER
)
977 Player
* player
= (Player
*)user
;
979 if (info
->camera
.cinematicId
)
980 player
->SendCinematicStart(info
->camera
.cinematicId
);
982 if (info
->camera
.eventID
)
983 GetMap()->ScriptsStart(sEventScripts
, info
->camera
.eventID
, player
, this);
988 case GAMEOBJECT_TYPE_FISHINGNODE
: //17
990 if(user
->GetTypeId()!=TYPEID_PLAYER
)
993 Player
* player
= (Player
*)user
;
995 if(player
->GetGUID() != GetOwnerGUID())
998 switch(getLootState())
1000 case GO_READY
: // ready for loot
1002 // 1) skill must be >= base_zone_skill
1003 // 2) if skill == base_zone_skill => 5% chance
1004 // 3) chance is linear dependence from (base_zone_skill-skill)
1006 uint32 zone
, subzone
;
1007 GetZoneAndAreaId(zone
,subzone
);
1009 int32 zone_skill
= objmgr
.GetFishingBaseSkillLevel( subzone
);
1011 zone_skill
= objmgr
.GetFishingBaseSkillLevel( zone
);
1013 //provide error, no fishable zone or area should be 0
1015 sLog
.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone
);
1017 int32 skill
= player
->GetSkillValue(SKILL_FISHING
);
1018 int32 chance
= skill
- zone_skill
+ 5;
1019 int32 roll
= irand(1,100);
1021 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill
,zone_skill
,chance
,roll
);
1023 if(skill
>= zone_skill
&& chance
>= roll
)
1025 // prevent removing GO at spell cancel
1026 player
->RemoveGameObject(this,false);
1027 SetOwnerGUID(player
->GetGUID());
1030 player
->UpdateFishingSkill();
1032 GameObject
* ok
= LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE
);
1035 player
->SendLoot(ok
->GetGUID(),LOOT_FISHINGHOLE
);
1036 player
->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_FISH_IN_GAMEOBJECT
, ok
->GetGOInfo()->id
);
1037 SetLootState(GO_JUST_DEACTIVATED
);
1040 player
->SendLoot(GetGUID(),LOOT_FISHING
);
1044 // fish escaped, can be deleted now
1045 SetLootState(GO_JUST_DEACTIVATED
);
1047 WorldPacket
data(SMSG_FISH_ESCAPED
, 0);
1048 player
->GetSession()->SendPacket(&data
);
1052 case GO_JUST_DEACTIVATED
: // nothing to do, will be deleted at next update
1056 SetLootState(GO_JUST_DEACTIVATED
);
1058 WorldPacket
data(SMSG_FISH_NOT_HOOKED
, 0);
1059 player
->GetSession()->SendPacket(&data
);
1064 player
->FinishSpell(CURRENT_CHANNELED_SPELL
);
1068 case GAMEOBJECT_TYPE_SUMMONING_RITUAL
: //18
1070 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1073 Player
* player
= (Player
*)user
;
1075 Unit
* caster
= GetOwner();
1077 GameObjectInfo
const* info
= GetGOInfo();
1079 if( !caster
|| caster
->GetTypeId()!=TYPEID_PLAYER
)
1082 // accept only use by player from same group for caster except caster itself
1083 if(((Player
*)caster
)==player
|| !((Player
*)caster
)->IsInSameRaidWith(player
))
1086 AddUniqueUse(player
);
1088 // full amount unique participants including original summoner
1089 if(GetUniqueUseCount() < info
->summoningRitual
.reqParticipants
)
1092 // in case summoning ritual caster is GO creator
1093 spellCaster
= caster
;
1095 if(!caster
->GetCurrentSpell(CURRENT_CHANNELED_SPELL
))
1098 spellId
= info
->summoningRitual
.spellId
;
1099 if(spellId
==62330) // GO store not existed spell, replace by expected
1101 // spell have reagent and mana cost but it not expected use its
1102 // it triggered spell in fact casted at currently channeled GO
1108 player
->FinishSpell(CURRENT_CHANNELED_SPELL
);
1110 // can be deleted now
1111 SetLootState(GO_JUST_DEACTIVATED
);
1113 // go to end function to spell casting
1116 case GAMEOBJECT_TYPE_SPELLCASTER
: //22
1118 SetUInt32Value(GAMEOBJECT_FLAGS
,2);
1120 GameObjectInfo
const* info
= GetGOInfo();
1124 if(info
->spellcaster
.partyOnly
)
1126 Unit
* caster
= GetOwner();
1127 if( !caster
|| caster
->GetTypeId()!=TYPEID_PLAYER
)
1130 if(user
->GetTypeId()!=TYPEID_PLAYER
|| !((Player
*)user
)->IsInSameRaidWith((Player
*)caster
))
1134 spellId
= info
->spellcaster
.spellId
;
1139 case GAMEOBJECT_TYPE_MEETINGSTONE
: //23
1141 GameObjectInfo
const* info
= GetGOInfo();
1143 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1146 Player
* player
= (Player
*)user
;
1148 Player
* targetPlayer
= ObjectAccessor::FindPlayer(player
->GetSelection());
1150 // accept only use by player from same group for caster except caster itself
1151 if(!targetPlayer
|| targetPlayer
== player
|| !targetPlayer
->IsInSameGroupWith(player
))
1154 //required lvl checks!
1155 uint8 level
= player
->getLevel();
1156 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1158 level
= targetPlayer
->getLevel();
1159 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1162 if(info
->id
==194097)
1163 spellId
= 61994; // Ritual of Summoning
1165 spellId
= 59782; // Summoning Stone Effect
1170 case GAMEOBJECT_TYPE_FLAGSTAND
: // 24
1172 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1175 Player
* player
= (Player
*)user
;
1177 if( player
->CanUseBattleGroundObject() )
1179 // in battleground check
1180 BattleGround
*bg
= player
->GetBattleGround();
1190 bg
->EventPlayerClickedOnFlag(player
, this);
1191 return; //we don;t need to delete flag ... it is despawned!
1195 case GAMEOBJECT_TYPE_FLAGDROP
: // 26
1197 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1200 Player
* player
= (Player
*)user
;
1202 if( player
->CanUseBattleGroundObject() )
1204 // in battleground check
1205 BattleGround
*bg
= player
->GetBattleGround();
1210 // 179785 - Silverwing Flag
1211 // 179786 - Warsong Flag
1213 // 184142 - Netherstorm Flag
1214 GameObjectInfo
const* info
= GetGOInfo();
1219 case 179785: // Silverwing Flag
1220 // check if it's correct bg
1221 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1222 bg
->EventPlayerClickedOnFlag(player
, this);
1224 case 179786: // Warsong Flag
1225 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1226 bg
->EventPlayerClickedOnFlag(player
, this);
1228 case 184142: // Netherstorm Flag
1229 if(bg
->GetTypeID() == BATTLEGROUND_EY
)
1230 bg
->EventPlayerClickedOnFlag(player
, this);
1234 //this cause to call return, all flags must be deleted here!!
1240 case GAMEOBJECT_TYPE_BARBER_CHAIR
: //32
1242 GameObjectInfo
const* info
= GetGOInfo();
1246 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1249 Player
* player
= (Player
*)user
;
1251 // fallback, will always work
1252 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
1254 WorldPacket
data(SMSG_ENABLE_BARBER_SHOP
, 0);
1255 player
->GetSession()->SendPacket(&data
);
1257 player
->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR
+info
->barberChair
.chairheight
);
1261 sLog
.outDebug("Unknown Object Type %u", GetGoType());
1268 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry( spellId
);
1271 sLog
.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId
,GetEntry(),GetGoType());
1275 Spell
*spell
= new Spell(spellCaster
, spellInfo
, triggered
,GetGUID());
1277 // spell target is user of GO
1278 SpellCastTargets targets
;
1279 targets
.setUnitTarget( user
);
1281 spell
->prepare(&targets
);
1284 // overwrite WorldObject function for proper name localization
1285 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx
) const
1289 GameObjectLocale
const *cl
= objmgr
.GetGameObjectLocale(GetEntry());
1292 if (cl
->Name
.size() > loc_idx
&& !cl
->Name
[loc_idx
].empty())
1293 return cl
->Name
[loc_idx
].c_str();
1300 void GameObject::UpdateRotationFields(float rotation2
/*=0.0f*/, float rotation3
/*=0.0f*/)
1302 static double const atan_pow
= atan(pow(2.0f
, -20.0f
));
1304 double f_rot1
= sin(GetOrientation() / 2.0f
);
1305 double f_rot2
= cos(GetOrientation() / 2.0f
);
1307 int64 i_rot1
= int64(f_rot1
/ atan_pow
*(f_rot2
>= 0 ? 1.0f
: -1.0f
));
1308 int64 rotation
= (i_rot1
<< 43 >> 43) & 0x00000000001FFFFF;
1310 //float f_rot2 = sin(0.0f / 2.0f);
1311 //int64 i_rot2 = f_rot2 / atan(pow(2.0f, -20.0f));
1312 //rotation |= (((i_rot2 << 22) >> 32) >> 11) & 0x000003FFFFE00000;
1314 //float f_rot3 = sin(0.0f / 2.0f);
1315 //int64 i_rot3 = f_rot3 / atan(pow(2.0f, -21.0f));
1316 //rotation |= (i_rot3 >> 42) & 0x7FFFFC0000000000;
1318 m_rotation
= rotation
;
1320 if(rotation2
==0.0f
&& rotation3
==0.0f
)
1326 SetFloatValue(GAMEOBJECT_PARENTROTATION
+2, rotation2
);
1327 SetFloatValue(GAMEOBJECT_PARENTROTATION
+3, rotation3
);