2 * Copyright (C) 2005-2010 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 "PoolManager.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"
37 #include "BattleGroundAV.h"
39 #include "ScriptCalls.h"
41 GameObject::GameObject() : WorldObject()
43 m_objectType
|= TYPEMASK_GAMEOBJECT
;
44 m_objectTypeId
= TYPEID_GAMEOBJECT
;
46 m_updateFlag
= (UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HAS_POSITION
| UPDATEFLAG_POSITION
| UPDATEFLAG_ROTATION
);
48 m_valuesCount
= GAMEOBJECT_END
;
50 m_respawnDelayTime
= 25;
51 m_lootState
= GO_NOT_READY
;
52 m_spawnedByDefault
= true;
62 GameObject::~GameObject()
66 void GameObject::AddToWorld()
68 ///- Register the gameobject for guid lookup
70 GetMap()->GetObjectsStore().insert
<GameObject
>(GetGUID(), (GameObject
*)this);
75 void GameObject::RemoveFromWorld()
77 ///- Remove the gameobject from the accessor
80 // Remove GO from owner
81 if(uint64 owner_guid
= GetOwnerGUID())
83 if (Unit
* owner
= ObjectAccessor::GetUnit(*this,owner_guid
))
84 owner
->RemoveGameObject(this,false);
87 const char * ownerType
= "creature";
88 if(IS_PLAYER_GUID(owner_guid
))
90 else if(IS_PET_GUID(owner_guid
))
93 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.",
94 GetGUIDLow(), GetGOInfo()->id
, m_spellId
, GetGOInfo()->GetLinkedGameObjectEntry(), GUID_LOPART(owner_guid
), ownerType
);
98 GetMap()->GetObjectsStore().erase
<GameObject
>(GetGUID(), (GameObject
*)NULL
);
101 Object::RemoveFromWorld();
104 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
)
109 SetPhaseMask(phaseMask
,false);
111 if(!IsPositionValid())
113 sLog
.outError("Gameobject (GUID: %u Entry: %u ) not created. Suggested coordinates isn't valid (X: %f Y: %f)",guidlow
,name_id
,x
,y
);
117 GameObjectInfo
const* goinfo
= ObjectMgr::GetGameObjectInfo(name_id
);
120 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
);
124 Object::_Create(guidlow
, goinfo
->id
, HIGHGUID_GAMEOBJECT
);
128 if (goinfo
->type
>= MAX_GAMEOBJECT_TYPE
)
130 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
);
134 SetFloatValue(GAMEOBJECT_PARENTROTATION
+0, rotation0
);
135 SetFloatValue(GAMEOBJECT_PARENTROTATION
+1, rotation1
);
137 UpdateRotationFields(rotation2
,rotation3
); // GAMEOBJECT_FACING, GAMEOBJECT_ROTATION, GAMEOBJECT_PARENTROTATION+2/3
139 SetFloatValue(OBJECT_FIELD_SCALE_X
, goinfo
->size
);
141 SetUInt32Value(GAMEOBJECT_FACTION
, goinfo
->faction
);
142 SetUInt32Value(GAMEOBJECT_FLAGS
, goinfo
->flags
);
144 SetEntry(goinfo
->id
);
146 SetUInt32Value(GAMEOBJECT_DISPLAYID
, goinfo
->displayId
);
148 // GAMEOBJECT_BYTES_1, index at 0, 1, 2 and 3
149 SetGoState(go_state
);
150 SetGoType(GameobjectTypes(goinfo
->type
));
151 SetGoArtKit(0); // unknown what this is
152 SetGoAnimProgress(animprogress
);
154 //Notify the map's instance data.
155 //Only works if you create the object in it, not if it is moves to that map.
156 //Normally non-players do not teleport to other maps.
157 if(map
->IsDungeon() && ((InstanceMap
*)map
)->GetInstanceData())
159 ((InstanceMap
*)map
)->GetInstanceData()->OnObjectCreate(this);
165 void GameObject::Update(uint32
/*p_time*/)
167 if (IS_MO_TRANSPORT(GetGUID()))
169 //((Transport*)this)->Update(p_time);
179 case GAMEOBJECT_TYPE_TRAP
:
181 // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
182 Unit
* owner
= GetOwner();
183 if (owner
&& ((Player
*)owner
)->isInCombat())
184 m_cooldownTime
= time(NULL
) + GetGOInfo()->trap
.startDelay
;
185 m_lootState
= GO_READY
;
188 case GAMEOBJECT_TYPE_FISHINGNODE
:
190 // fishing code (bobber ready)
191 if( time(NULL
) > m_respawnTime
- FISHING_BOBBER_READY_TIME
)
193 // splash bobber (bobber ready now)
194 Unit
* caster
= GetOwner();
195 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
197 SetGoState(GO_STATE_ACTIVE
);
198 SetUInt32Value(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
202 BuildValuesUpdateBlockForPlayer(&udata
,((Player
*)caster
));
203 udata
.BuildPacket(&packet
);
204 ((Player
*)caster
)->GetSession()->SendPacket(&packet
);
206 SendGameObjectCustomAnim(GetGUID());
209 m_lootState
= GO_READY
; // can be successfully open with some chance
214 m_lootState
= GO_READY
; // for other GOis same switched without delay to GO_READY
217 // NO BREAK for switch (m_lootState)
221 if (m_respawnTime
> 0) // timer on
223 if (m_respawnTime
<= time(NULL
)) // timer expired
226 m_SkillupList
.clear();
231 case GAMEOBJECT_TYPE_FISHINGNODE
: // can't fish now
233 Unit
* caster
= GetOwner();
234 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
236 caster
->FinishSpell(CURRENT_CHANNELED_SPELL
);
238 WorldPacket
data(SMSG_FISH_NOT_HOOKED
,0);
239 ((Player
*)caster
)->GetSession()->SendPacket(&data
);
242 m_lootState
= GO_JUST_DEACTIVATED
;
245 case GAMEOBJECT_TYPE_DOOR
:
246 case GAMEOBJECT_TYPE_BUTTON
:
247 //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)
248 if (GetGoState() != GO_STATE_READY
)
250 //flags in AB are type_button and we need to add them here so no break!
252 if (!m_spawnedByDefault
) // despawn timer
254 // can be despawned or destroyed
255 SetLootState(GO_JUST_DEACTIVATED
);
259 uint16 poolid
= GetDBTableGUIDLow() ? sPoolMgr
.IsPartOfAPool
<GameObject
>(GetDBTableGUIDLow()) : 0;
261 sPoolMgr
.UpdatePool
<GameObject
>(poolid
, GetDBTableGUIDLow());
271 // traps can have time and can not have
272 GameObjectInfo
const* goInfo
= GetGOInfo();
273 if(goInfo
->type
== GAMEOBJECT_TYPE_TRAP
)
275 if(m_cooldownTime
>= time(NULL
))
279 Unit
* owner
= GetOwner();
280 Unit
* ok
= NULL
; // pointer to appropriate target if found any
282 bool IsBattleGroundTrap
= false;
283 //FIXME: this is activation radius (in different casting radius that must be selected from spell data)
284 //TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
285 float radius
= goInfo
->trap
.radius
;
288 if(goInfo
->trap
.cooldown
!= 3) // cast in other case (at some triggering/linked go/etc explicit call)
292 if(m_respawnTime
> 0)
295 radius
= goInfo
->trap
.cooldown
; // battlegrounds gameobjects has data2 == 0 && data5 == 3
296 IsBattleGroundTrap
= true;
300 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
302 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
304 // Note: this hack with search required until GO casting not implemented
305 // search unfriendly creature
306 if(owner
&& goInfo
->trap
.charges
> 0) // hunter trap
308 MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
u_check(this, owner
, radius
);
309 MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
> checker(this,ok
, u_check
);
311 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
313 TypeContainerVisitor
<MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_object_checker(checker
);
314 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap(), *this, radius
);
316 // or unfriendly player/pet
319 TypeContainerVisitor
<MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_object_checker(checker
);
320 cell_lock
->Visit(cell_lock
, world_object_checker
, *GetMap(), *this, radius
);
323 else // environmental trap
325 // environmental damage spells already have around enemies targeting but this not help in case not existed GO casting support
327 // affect only players
329 MaNGOS::AnyPlayerInObjectRangeCheck
p_check(this, radius
);
330 MaNGOS::PlayerSearcher
<MaNGOS::AnyPlayerInObjectRangeCheck
> checker(this,p_ok
, p_check
);
332 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
334 TypeContainerVisitor
<MaNGOS::PlayerSearcher
<MaNGOS::AnyPlayerInObjectRangeCheck
>, WorldTypeMapContainer
> world_object_checker(checker
);
335 cell_lock
->Visit(cell_lock
, world_object_checker
, *GetMap(), *this, radius
);
341 Unit
*caster
= owner
? owner
: ok
;
343 caster
->CastSpell(ok
, goInfo
->trap
.spellId
, true, NULL
, NULL
, GetGUID());
344 m_cooldownTime
= time(NULL
) + 4; // 4 seconds
347 if(goInfo
->trap
.charges
> 0)
350 if(IsBattleGroundTrap
&& ok
->GetTypeId() == TYPEID_PLAYER
)
352 //BattleGround gameobjects case
353 if(((Player
*)ok
)->InBattleGround())
354 if(BattleGround
*bg
= ((Player
*)ok
)->GetBattleGround())
355 bg
->HandleTriggerBuff(GetGUID());
360 if(uint32 max_charges
= goInfo
->GetCharges())
362 if (m_usetimes
>= max_charges
)
365 SetLootState(GO_JUST_DEACTIVATED
); // can be despawned or destroyed
376 case GAMEOBJECT_TYPE_DOOR
:
377 case GAMEOBJECT_TYPE_BUTTON
:
378 if (GetGOInfo()->GetAutoCloseTime() && (m_cooldownTime
< time(NULL
)))
381 case GAMEOBJECT_TYPE_GOOBER
:
382 if (m_cooldownTime
< time(NULL
))
384 RemoveFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
386 SetLootState(GO_JUST_DEACTIVATED
);
395 case GO_JUST_DEACTIVATED
:
397 //if Gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
398 if (GetGoType() == GAMEOBJECT_TYPE_GOOBER
)
400 uint32 spellId
= GetGOInfo()->goober
.spellId
;
404 std::set
<uint32
>::const_iterator it
= m_unique_users
.begin();
405 std::set
<uint32
>::const_iterator end
= m_unique_users
.end();
406 for (; it
!= end
; it
++)
408 if (Unit
* owner
= Unit::GetUnit(*this, uint64(*it
)))
409 owner
->CastSpell(owner
, spellId
, false, NULL
, NULL
, GetGUID());
412 m_unique_users
.clear();
416 SetGoState(GO_STATE_READY
);
418 //any return here in case battleground traps
423 if(Unit
* owner
= GetOwner())
424 owner
->RemoveGameObject(this, false);
431 //burning flags in some battlegrounds, if you find better condition, just add it
432 if (GetGOInfo()->IsDespawnAtAction() || GetGoAnimProgress() > 0)
434 SendObjectDeSpawnAnim(GetGUID());
436 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
440 SetLootState(GO_READY
);
442 if(!m_respawnDelayTime
)
445 if(!m_spawnedByDefault
)
451 m_respawnTime
= time(NULL
) + m_respawnDelayTime
;
453 // if option not set then object will be saved at grid unload
454 if(sWorld
.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY
))
457 UpdateObjectVisibility();
464 void GameObject::Refresh()
466 // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
467 if(m_respawnTime
> 0 && m_spawnedByDefault
)
474 void GameObject::AddUniqueUse(Player
* player
)
477 m_unique_users
.insert(player
->GetGUIDLow());
480 void GameObject::Delete()
482 SendObjectDeSpawnAnim(GetGUID());
484 SetGoState(GO_STATE_READY
);
485 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
487 uint16 poolid
= GetDBTableGUIDLow() ? sPoolMgr
.IsPartOfAPool
<GameObject
>(GetDBTableGUIDLow()) : 0;
489 sPoolMgr
.UpdatePool
<GameObject
>(poolid
, GetDBTableGUIDLow());
491 AddObjectToRemoveList();
494 void GameObject::getFishLoot(Loot
*fishloot
, Player
* loot_owner
)
498 uint32 zone
, subzone
;
499 GetZoneAndAreaId(zone
,subzone
);
501 // if subzone loot exist use it
502 if (!fishloot
->FillLoot(subzone
, LootTemplates_Fishing
, loot_owner
, true, true))
503 // else use zone loot (must exist in like case)
504 fishloot
->FillLoot(zone
, LootTemplates_Fishing
, loot_owner
,true);
507 void GameObject::SaveToDB()
509 // this should only be used when the gameobject has already been loaded
510 // preferably after adding to map, because mapid may not be valid otherwise
511 GameObjectData
const *data
= sObjectMgr
.GetGOData(m_DBTableGuid
);
514 sLog
.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
518 SaveToDB(GetMapId(), data
->spawnMask
, data
->phaseMask
);
521 void GameObject::SaveToDB(uint32 mapid
, uint8 spawnMask
, uint32 phaseMask
)
523 const GameObjectInfo
*goI
= GetGOInfo();
529 m_DBTableGuid
= GetGUIDLow();
530 // update in loaded data (changing data only in this place)
531 GameObjectData
& data
= sObjectMgr
.NewGOData(m_DBTableGuid
);
533 // data->guid = guid don't must be update at save
534 data
.id
= GetEntry();
536 data
.phaseMask
= phaseMask
;
537 data
.posX
= GetPositionX();
538 data
.posY
= GetPositionY();
539 data
.posZ
= GetPositionZ();
540 data
.orientation
= GetOrientation();
541 data
.rotation0
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+0);
542 data
.rotation1
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+1);
543 data
.rotation2
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+2);
544 data
.rotation3
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+3);
545 data
.spawntimesecs
= m_spawnedByDefault
? m_respawnDelayTime
: -(int32
)m_respawnDelayTime
;
546 data
.animprogress
= GetGoAnimProgress();
547 data
.go_state
= GetGoState();
548 data
.spawnMask
= spawnMask
;
551 std::ostringstream ss
;
552 ss
<< "INSERT INTO gameobject VALUES ( "
553 << m_DBTableGuid
<< ", "
554 << GetEntry() << ", "
556 << uint32(spawnMask
) << "," // cast to prevent save as symbol
557 << uint16(GetPhaseMask()) << "," // prevent out of range error
558 << GetPositionX() << ", "
559 << GetPositionY() << ", "
560 << GetPositionZ() << ", "
561 << GetOrientation() << ", "
562 << GetFloatValue(GAMEOBJECT_PARENTROTATION
) << ", "
563 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+1) << ", "
564 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+2) << ", "
565 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+3) << ", "
566 << m_respawnDelayTime
<< ", "
567 << uint32(GetGoAnimProgress()) << ", "
568 << uint32(GetGoState()) << ")";
570 WorldDatabase
.BeginTransaction();
571 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
572 WorldDatabase
.PExecuteLog("%s", ss
.str().c_str());
573 WorldDatabase
.CommitTransaction();
576 bool GameObject::LoadFromDB(uint32 guid
, Map
*map
)
578 GameObjectData
const* data
= sObjectMgr
.GetGOData(guid
);
582 sLog
.outErrorDb("Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid
);
586 uint32 entry
= data
->id
;
587 //uint32 map_id = data->mapid; // already used before call
588 uint32 phaseMask
= data
->phaseMask
;
589 float x
= data
->posX
;
590 float y
= data
->posY
;
591 float z
= data
->posZ
;
592 float ang
= data
->orientation
;
594 float rotation0
= data
->rotation0
;
595 float rotation1
= data
->rotation1
;
596 float rotation2
= data
->rotation2
;
597 float rotation3
= data
->rotation3
;
599 uint32 animprogress
= data
->animprogress
;
600 GOState go_state
= data
->go_state
;
602 m_DBTableGuid
= guid
;
603 if (map
->GetInstanceId() != 0) guid
= sObjectMgr
.GenerateLowGuid(HIGHGUID_GAMEOBJECT
);
605 if (!Create(guid
,entry
, map
, phaseMask
, x
, y
, z
, ang
, rotation0
, rotation1
, rotation2
, rotation3
, animprogress
, go_state
) )
608 if (!GetGOInfo()->GetDespawnPossibility() && !GetGOInfo()->IsDespawnAtAction() && data
->spawntimesecs
>= 0)
610 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
611 m_spawnedByDefault
= true;
612 m_respawnDelayTime
= 0;
617 if(data
->spawntimesecs
>= 0)
619 m_spawnedByDefault
= true;
620 m_respawnDelayTime
= data
->spawntimesecs
;
621 m_respawnTime
= sObjectMgr
.GetGORespawnTime(m_DBTableGuid
, map
->GetInstanceId());
624 if(m_respawnTime
&& m_respawnTime
<= time(NULL
))
627 sObjectMgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
632 m_spawnedByDefault
= false;
633 m_respawnDelayTime
= -data
->spawntimesecs
;
641 void GameObject::DeleteFromDB()
643 sObjectMgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
644 sObjectMgr
.DeleteGOData(m_DBTableGuid
);
645 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
646 WorldDatabase
.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid
);
647 WorldDatabase
.PExecuteLog("DELETE FROM gameobject_battleground WHERE guid = '%u'", m_DBTableGuid
);
650 GameObjectInfo
const *GameObject::GetGOInfo() const
655 /*********************************************************/
656 /*** QUEST SYSTEM ***/
657 /*********************************************************/
658 bool GameObject::hasQuest(uint32 quest_id
) const
660 QuestRelations
const& qr
= sObjectMgr
.mGOQuestRelations
;
661 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
663 if(itr
->second
==quest_id
)
669 bool GameObject::hasInvolvedQuest(uint32 quest_id
) const
671 QuestRelations
const& qr
= sObjectMgr
.mGOQuestInvolvedRelations
;
672 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
674 if(itr
->second
==quest_id
)
680 bool GameObject::IsTransport() const
682 // If something is marked as a transport, don't transmit an out of range packet for it.
683 GameObjectInfo
const * gInfo
= GetGOInfo();
684 if(!gInfo
) return false;
685 return gInfo
->type
== GAMEOBJECT_TYPE_TRANSPORT
|| gInfo
->type
== GAMEOBJECT_TYPE_MO_TRANSPORT
;
688 Unit
* GameObject::GetOwner() const
690 return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
693 void GameObject::SaveRespawnTime()
695 if(m_respawnTime
> time(NULL
) && m_spawnedByDefault
)
696 sObjectMgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),m_respawnTime
);
699 bool GameObject::isVisibleForInState(Player
const* u
, WorldObject
const* viewPoint
, bool inVisibleList
) const
702 if(!IsInWorld() || !u
->IsInWorld())
705 // invisible at client always
706 if(!GetGOInfo()->displayId
)
709 // Transport always visible at this step implementation
710 if(IsTransport() && IsInMap(u
))
713 // quick check visibility false cases for non-GM-mode
714 if(!u
->isGameMaster())
716 // despawned and then not visible for non-GM in GM-mode
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()))
730 return IsWithinDistInMap(viewPoint
,World::GetMaxVisibleDistanceForObject() +
731 (inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);
734 void GameObject::Respawn()
736 if(m_spawnedByDefault
&& m_respawnTime
> 0)
738 m_respawnTime
= time(NULL
);
739 sObjectMgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
743 bool GameObject::ActivateToQuest( Player
*pTarget
)const
745 if(!sObjectMgr
.IsGameObjectForQuests(GetEntry()))
750 // scan GO chest with loot including quest items
751 case GAMEOBJECT_TYPE_CHEST
:
753 if(LootTemplates_Gameobject
.HaveQuestLootForPlayer(GetGOInfo()->GetLootId(), pTarget
))
755 //look for battlegroundAV for some objects which are only activated after mine gots captured by own team
756 if (GetEntry() == BG_AV_OBJECTID_MINE_N
|| GetEntry() == BG_AV_OBJECTID_MINE_S
)
757 if (BattleGround
*bg
= pTarget
->GetBattleGround())
758 if (bg
->GetTypeID() == BATTLEGROUND_AV
&& !(((BattleGroundAV
*)bg
)->PlayerCanDoMineQuest(GetEntry(),pTarget
->GetTeam())))
764 case GAMEOBJECT_TYPE_GOOBER
:
766 if(pTarget
->GetQuestStatus(GetGOInfo()->goober
.questId
) == QUEST_STATUS_INCOMPLETE
)
777 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry
, Unit
* target
)
779 GameObjectInfo
const* trapInfo
= sGOStorage
.LookupEntry
<GameObjectInfo
>(trapEntry
);
780 if(!trapInfo
|| trapInfo
->type
!=GAMEOBJECT_TYPE_TRAP
)
783 SpellEntry
const* trapSpell
= sSpellStore
.LookupEntry(trapInfo
->trap
.spellId
);
784 if(!trapSpell
) // checked at load already
787 float range
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(trapSpell
->rangeIndex
));
789 // search nearest linked GO
790 GameObject
* trapGO
= NULL
;
792 // using original GO distance
793 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
795 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
797 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*target
,trapEntry
,range
);
798 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(this, trapGO
,go_check
);
800 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
801 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
802 cell_lock
->Visit(cell_lock
, object_checker
, *GetMap(), *target
, range
);
806 // FIXME: when GO casting will be implemented trap must cast spell to target
808 target
->CastSpell(target
, trapSpell
, true, NULL
, NULL
, GetGUID());
811 GameObject
* GameObject::LookupFishingHoleAround(float range
)
813 GameObject
* ok
= NULL
;
815 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
817 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
818 MaNGOS::NearestGameObjectFishingHole
u_check(*this, range
);
819 MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
> checker(this, ok
, u_check
);
821 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
823 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
>, GridTypeMapContainer
> grid_object_checker(checker
);
824 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap(), *this, range
);
829 void GameObject::ResetDoorOrButton()
831 if (m_lootState
== GO_READY
|| m_lootState
== GO_JUST_DEACTIVATED
)
834 SwitchDoorOrButton(false);
835 SetLootState(GO_JUST_DEACTIVATED
);
839 void GameObject::UseDoorOrButton(uint32 time_to_restore
, bool alternative
/* = false */)
841 if(m_lootState
!= GO_READY
)
845 time_to_restore
= GetGOInfo()->GetAutoCloseTime();
847 SwitchDoorOrButton(true,alternative
);
848 SetLootState(GO_ACTIVATED
);
850 m_cooldownTime
= time(NULL
) + time_to_restore
;
853 void GameObject::SwitchDoorOrButton(bool activate
, bool alternative
/* = false */)
856 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
858 RemoveFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
860 if(GetGoState() == GO_STATE_READY
) //if closed -> open
861 SetGoState(alternative
? GO_STATE_ACTIVE_ALTERNATIVE
: GO_STATE_ACTIVE
);
862 else //if open -> close
863 SetGoState(GO_STATE_READY
);
866 void GameObject::Use(Unit
* user
)
868 // by default spell caster is user
869 Unit
* spellCaster
= user
;
871 bool triggered
= false;
873 if (user
->GetTypeId() == TYPEID_PLAYER
&& Script
->GOHello((Player
*)user
, this))
878 case GAMEOBJECT_TYPE_DOOR
: //0
879 case GAMEOBJECT_TYPE_BUTTON
: //1
881 //doors/buttons never really despawn, only reset to default state/flags
885 GetMap()->ScriptsStart(sGameObjectScripts
, GetDBTableGUIDLow(), spellCaster
, this);
888 case GAMEOBJECT_TYPE_QUESTGIVER
: //2
890 if (user
->GetTypeId() != TYPEID_PLAYER
)
893 Player
* player
= (Player
*)user
;
895 player
->PrepareGossipMenu(this, GetGOInfo()->questgiver
.gossipID
);
896 player
->SendPreparedGossip(this);
899 case GAMEOBJECT_TYPE_CHEST
:
901 if (user
->GetTypeId() != TYPEID_PLAYER
)
904 // TODO: possible must be moved to loot release (in different from linked triggering)
905 if (GetGOInfo()->chest
.eventId
)
907 sLog
.outDebug("Chest ScriptStart id %u for GO %u", GetGOInfo()->chest
.eventId
, GetDBTableGUIDLow());
908 GetMap()->ScriptsStart(sEventScripts
, GetGOInfo()->chest
.eventId
, user
, this);
911 // triggering linked GO
912 if (uint32 trapEntry
= GetGOInfo()->chest
.linkedTrapId
)
913 TriggeringLinkedGameObject(trapEntry
, user
);
917 case GAMEOBJECT_TYPE_CHAIR
: //7 Sitting: Wooden bench, chairs
919 GameObjectInfo
const* info
= GetGOInfo();
923 if (user
->GetTypeId() != TYPEID_PLAYER
)
926 Player
* player
= (Player
*)user
;
928 // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
930 // check if the db is sane
931 if (info
->chair
.slots
> 0)
933 float lowestDist
= DEFAULT_VISIBILITY_DISTANCE
;
935 float x_lowest
= GetPositionX();
936 float y_lowest
= GetPositionY();
938 // the object orientation + 1/2 pi
939 // every slot will be on that straight line
940 float orthogonalOrientation
= GetOrientation()+M_PI
*0.5f
;
942 for(uint32 i
=0; i
<info
->chair
.slots
; ++i
)
944 // the distance between this slot and the center of the go - imagine a 1D space
945 float relativeDistance
= (info
->size
*i
)-(info
->size
*(info
->chair
.slots
-1)/2.0f
);
947 float x_i
= GetPositionX() + relativeDistance
* cos(orthogonalOrientation
);
948 float y_i
= GetPositionY() + relativeDistance
* sin(orthogonalOrientation
);
950 // calculate the distance between the player and this slot
951 float thisDistance
= player
->GetDistance2d(x_i
, y_i
);
953 /* debug code. It will spawn a npc on each slot to visualize them.
954 Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
955 std::ostringstream output;
956 output << i << ": thisDist: " << thisDistance;
957 helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
960 if (thisDistance
<= lowestDist
)
962 lowestDist
= thisDistance
;
967 player
->TeleportTo(GetMapId(), x_lowest
, y_lowest
, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
971 // fallback, will always work
972 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
974 player
->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR
+info
->chair
.height
);
977 case GAMEOBJECT_TYPE_SPELL_FOCUS
:
979 // triggering linked GO
980 if (uint32 trapEntry
= GetGOInfo()->spellFocus
.linkedTrapId
)
981 TriggeringLinkedGameObject(trapEntry
, user
);
983 // some may be activated in addition? Conditions for this? (ex: entry 181616)
986 case GAMEOBJECT_TYPE_GOOBER
: //10
988 GameObjectInfo
const* info
= GetGOInfo();
990 if(user
->GetTypeId()==TYPEID_PLAYER
)
992 Player
* player
= (Player
*)user
;
994 if (info
->goober
.pageId
) // show page...
996 WorldPacket
data(SMSG_GAMEOBJECT_PAGETEXT
, 8);
998 player
->GetSession()->SendPacket(&data
);
1000 else if (info
->goober
.gossipID
) // ...or gossip, if page does not exist
1002 player
->PrepareGossipMenu(this, info
->goober
.gossipID
);
1003 player
->SendPreparedGossip(this);
1006 if (info
->goober
.eventId
)
1008 sLog
.outDebug("Goober ScriptStart id %u for GO entry %u (GUID %u).", info
->goober
.eventId
, GetEntry(), GetDBTableGUIDLow());
1009 GetMap()->ScriptsStart(sEventScripts
, info
->goober
.eventId
, player
, this);
1012 // possible quest objective for active quests
1013 if (info
->goober
.questId
&& sObjectMgr
.GetQuestTemplate(info
->goober
.questId
))
1015 //Quest require to be active for GO using
1016 if (player
->GetQuestStatus(info
->goober
.questId
) != QUEST_STATUS_INCOMPLETE
)
1020 player
->CastedCreatureOrGO(info
->id
, GetGUID(), 0);
1023 if (uint32 trapEntry
= info
->goober
.linkedTrapId
)
1024 TriggeringLinkedGameObject(trapEntry
, user
);
1026 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
1027 SetLootState(GO_ACTIVATED
);
1029 uint32 time_to_restore
= info
->GetAutoCloseTime();
1031 // this appear to be ok, however others exist in addition to this that should have custom (ex: 190510, 188692, 187389)
1032 if (time_to_restore
&& info
->goober
.customAnim
)
1033 SendGameObjectCustomAnim(GetGUID());
1035 SetGoState(GO_STATE_ACTIVE
);
1037 m_cooldownTime
= time(NULL
) + time_to_restore
;
1039 // cast this spell later if provided
1040 spellId
= info
->goober
.spellId
;
1044 case GAMEOBJECT_TYPE_CAMERA
: //13
1046 GameObjectInfo
const* info
= GetGOInfo();
1050 if (user
->GetTypeId() != TYPEID_PLAYER
)
1053 Player
* player
= (Player
*)user
;
1055 if (info
->camera
.cinematicId
)
1056 player
->SendCinematicStart(info
->camera
.cinematicId
);
1058 if (info
->camera
.eventID
)
1059 GetMap()->ScriptsStart(sEventScripts
, info
->camera
.eventID
, player
, this);
1063 case GAMEOBJECT_TYPE_FISHINGNODE
: //17 fishing bobber
1065 if (user
->GetTypeId() != TYPEID_PLAYER
)
1068 Player
* player
= (Player
*)user
;
1070 if (player
->GetGUID() != GetOwnerGUID())
1073 switch(getLootState())
1075 case GO_READY
: // ready for loot
1077 // 1) skill must be >= base_zone_skill
1078 // 2) if skill == base_zone_skill => 5% chance
1079 // 3) chance is linear dependence from (base_zone_skill-skill)
1081 uint32 zone
, subzone
;
1082 GetZoneAndAreaId(zone
,subzone
);
1084 int32 zone_skill
= sObjectMgr
.GetFishingBaseSkillLevel(subzone
);
1086 zone_skill
= sObjectMgr
.GetFishingBaseSkillLevel(zone
);
1088 //provide error, no fishable zone or area should be 0
1090 sLog
.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone
);
1092 int32 skill
= player
->GetSkillValue(SKILL_FISHING
);
1093 int32 chance
= skill
- zone_skill
+ 5;
1094 int32 roll
= irand(1,100);
1096 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill
,zone_skill
,chance
,roll
);
1098 if (skill
>= zone_skill
&& chance
>= roll
)
1100 // prevent removing GO at spell cancel
1101 player
->RemoveGameObject(this,false);
1102 SetOwnerGUID(player
->GetGUID());
1105 player
->UpdateFishingSkill();
1107 //TODO: find reasonable value for fishing hole search
1108 GameObject
* ok
= LookupFishingHoleAround(20.0f
+ CONTACT_DISTANCE
);
1111 player
->SendLoot(ok
->GetGUID(),LOOT_FISHINGHOLE
);
1112 player
->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_FISH_IN_GAMEOBJECT
, ok
->GetGOInfo()->id
);
1113 SetLootState(GO_JUST_DEACTIVATED
);
1116 player
->SendLoot(GetGUID(),LOOT_FISHING
);
1120 // fish escaped, can be deleted now
1121 SetLootState(GO_JUST_DEACTIVATED
);
1123 WorldPacket
data(SMSG_FISH_ESCAPED
, 0);
1124 player
->GetSession()->SendPacket(&data
);
1128 case GO_JUST_DEACTIVATED
: // nothing to do, will be deleted at next update
1132 SetLootState(GO_JUST_DEACTIVATED
);
1134 WorldPacket
data(SMSG_FISH_NOT_HOOKED
, 0);
1135 player
->GetSession()->SendPacket(&data
);
1140 player
->FinishSpell(CURRENT_CHANNELED_SPELL
);
1143 case GAMEOBJECT_TYPE_SUMMONING_RITUAL
: //18
1145 if (user
->GetTypeId() != TYPEID_PLAYER
)
1148 Player
* player
= (Player
*)user
;
1150 Unit
* caster
= GetOwner();
1152 GameObjectInfo
const* info
= GetGOInfo();
1154 if (!caster
|| caster
->GetTypeId()!=TYPEID_PLAYER
)
1157 // accept only use by player from same group for caster except caster itself
1158 if (((Player
*)caster
) == player
|| !((Player
*)caster
)->IsInSameRaidWith(player
))
1161 AddUniqueUse(player
);
1163 // full amount unique participants including original summoner
1164 if (GetUniqueUseCount() < info
->summoningRitual
.reqParticipants
)
1167 // in case summoning ritual caster is GO creator
1168 spellCaster
= caster
;
1170 if (!caster
->GetCurrentSpell(CURRENT_CHANNELED_SPELL
))
1173 spellId
= info
->summoningRitual
.spellId
;
1174 if (spellId
== 62330) // GO store not existed spell, replace by expected
1176 // spell have reagent and mana cost but it not expected use its
1177 // it triggered spell in fact casted at currently channeled GO
1183 player
->FinishSpell(CURRENT_CHANNELED_SPELL
);
1185 // can be deleted now
1186 SetLootState(GO_JUST_DEACTIVATED
);
1188 // go to end function to spell casting
1191 case GAMEOBJECT_TYPE_SPELLCASTER
: //22
1193 SetUInt32Value(GAMEOBJECT_FLAGS
,2);
1195 GameObjectInfo
const* info
= GetGOInfo();
1199 if (info
->spellcaster
.partyOnly
)
1201 Unit
* caster
= GetOwner();
1202 if (!caster
|| caster
->GetTypeId() != TYPEID_PLAYER
)
1205 if (user
->GetTypeId() != TYPEID_PLAYER
|| !((Player
*)user
)->IsInSameRaidWith((Player
*)caster
))
1209 spellId
= info
->spellcaster
.spellId
;
1214 case GAMEOBJECT_TYPE_MEETINGSTONE
: //23
1216 GameObjectInfo
const* info
= GetGOInfo();
1218 if (user
->GetTypeId() != TYPEID_PLAYER
)
1221 Player
* player
= (Player
*)user
;
1223 Player
* targetPlayer
= ObjectAccessor::FindPlayer(player
->GetSelection());
1225 // accept only use by player from same group for caster except caster itself
1226 if (!targetPlayer
|| targetPlayer
== player
|| !targetPlayer
->IsInSameGroupWith(player
))
1229 //required lvl checks!
1230 uint8 level
= player
->getLevel();
1231 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1234 level
= targetPlayer
->getLevel();
1235 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1238 if (info
->id
== 194097)
1239 spellId
= 61994; // Ritual of Summoning
1241 spellId
= 59782; // Summoning Stone Effect
1245 case GAMEOBJECT_TYPE_FLAGSTAND
: // 24
1247 if (user
->GetTypeId() != TYPEID_PLAYER
)
1250 Player
* player
= (Player
*)user
;
1252 if (player
->CanUseBattleGroundObject())
1254 // in battleground check
1255 BattleGround
*bg
= player
->GetBattleGround();
1265 bg
->EventPlayerClickedOnFlag(player
, this);
1266 return; //we don;t need to delete flag ... it is despawned!
1270 case GAMEOBJECT_TYPE_FLAGDROP
: // 26
1272 if (user
->GetTypeId() != TYPEID_PLAYER
)
1275 Player
* player
= (Player
*)user
;
1277 if (player
->CanUseBattleGroundObject())
1279 // in battleground check
1280 BattleGround
*bg
= player
->GetBattleGround();
1285 // 179785 - Silverwing Flag
1286 // 179786 - Warsong Flag
1288 // 184142 - Netherstorm Flag
1289 GameObjectInfo
const* info
= GetGOInfo();
1294 case 179785: // Silverwing Flag
1295 // check if it's correct bg
1296 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1297 bg
->EventPlayerClickedOnFlag(player
, this);
1299 case 179786: // Warsong Flag
1300 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1301 bg
->EventPlayerClickedOnFlag(player
, this);
1303 case 184142: // Netherstorm Flag
1304 if(bg
->GetTypeID() == BATTLEGROUND_EY
)
1305 bg
->EventPlayerClickedOnFlag(player
, this);
1309 //this cause to call return, all flags must be deleted here!!
1315 case GAMEOBJECT_TYPE_BARBER_CHAIR
: //32
1317 GameObjectInfo
const* info
= GetGOInfo();
1321 if (user
->GetTypeId() != TYPEID_PLAYER
)
1324 Player
* player
= (Player
*)user
;
1326 // fallback, will always work
1327 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
1329 WorldPacket
data(SMSG_ENABLE_BARBER_SHOP
, 0);
1330 player
->GetSession()->SendPacket(&data
);
1332 player
->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR
+info
->barberChair
.chairheight
);
1336 sLog
.outError("GameObject::Use unhandled GameObject type %u (entry %u).", GetGoType(), GetEntry());
1343 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry( spellId
);
1346 sLog
.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId
,GetEntry(),GetGoType());
1350 Spell
*spell
= new Spell(spellCaster
, spellInfo
, triggered
,GetGUID());
1352 // spell target is user of GO
1353 SpellCastTargets targets
;
1354 targets
.setUnitTarget( user
);
1356 spell
->prepare(&targets
);
1359 // overwrite WorldObject function for proper name localization
1360 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx
) const
1364 GameObjectLocale
const *cl
= sObjectMgr
.GetGameObjectLocale(GetEntry());
1367 if (cl
->Name
.size() > (size_t)loc_idx
&& !cl
->Name
[loc_idx
].empty())
1368 return cl
->Name
[loc_idx
].c_str();
1375 void GameObject::UpdateRotationFields(float rotation2
/*=0.0f*/, float rotation3
/*=0.0f*/)
1377 static double const atan_pow
= atan(pow(2.0f
, -20.0f
));
1379 double f_rot1
= sin(GetOrientation() / 2.0f
);
1380 double f_rot2
= cos(GetOrientation() / 2.0f
);
1382 int64 i_rot1
= int64(f_rot1
/ atan_pow
*(f_rot2
>= 0 ? 1.0f
: -1.0f
));
1383 int64 rotation
= (i_rot1
<< 43 >> 43) & 0x00000000001FFFFF;
1385 //float f_rot2 = sin(0.0f / 2.0f);
1386 //int64 i_rot2 = f_rot2 / atan(pow(2.0f, -20.0f));
1387 //rotation |= (((i_rot2 << 22) >> 32) >> 11) & 0x000003FFFFE00000;
1389 //float f_rot3 = sin(0.0f / 2.0f);
1390 //int64 i_rot3 = f_rot3 / atan(pow(2.0f, -21.0f));
1391 //rotation |= (i_rot3 >> 42) & 0x7FFFFC0000000000;
1393 m_rotation
= rotation
;
1395 if(rotation2
==0.0f
&& rotation3
==0.0f
)
1401 SetFloatValue(GAMEOBJECT_PARENTROTATION
+2, rotation2
);
1402 SetFloatValue(GAMEOBJECT_PARENTROTATION
+3, rotation3
);