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;
61 GameObject::~GameObject()
63 if(m_uint32Values
) // field array can be not exist if GameOBject not loaded
65 // Possible crash at access to deleted GO in Unit::m_gameobj
66 uint64 owner_guid
= GetOwnerGUID();
69 Unit
* owner
= ObjectAccessor::GetUnit(*this,owner_guid
);
71 owner
->RemoveGameObject(this,false);
72 else if(!IS_PLAYER_GUID(owner_guid
))
73 sLog
.outError("Delete GameObject (GUID: %u Entry: %u ) that have references in not found creature %u GO list. Crash possible later.",GetGUIDLow(),GetGOInfo()->id
,GUID_LOPART(owner_guid
));
78 void GameObject::AddToWorld()
80 ///- Register the gameobject for guid lookup
81 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
85 void GameObject::RemoveFromWorld()
87 ///- Remove the gameobject from the accessor
88 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
89 Object::RemoveFromWorld();
92 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
)
95 SetMapId(map
->GetId());
96 SetInstanceId(map
->GetInstanceId());
97 SetPhaseMask(phaseMask
,false);
99 if(!IsPositionValid())
101 sLog
.outError("Gameobject (GUID: %u Entry: %u ) not created. Suggested coordinates isn't valid (X: %f Y: %f)",guidlow
,name_id
,x
,y
);
105 GameObjectInfo
const* goinfo
= objmgr
.GetGameObjectInfo(name_id
);
108 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 Object::_Create(guidlow
, goinfo
->id
, HIGHGUID_GAMEOBJECT
);
116 if (goinfo
->type
>= MAX_GAMEOBJECT_TYPE
)
118 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 SetFloatValue(GAMEOBJECT_PARENTROTATION
+0, rotation0
);
123 SetFloatValue(GAMEOBJECT_PARENTROTATION
+1, rotation1
);
125 UpdateRotationFields(rotation2
,rotation3
); // GAMEOBJECT_FACING, GAMEOBJECT_ROTATION, GAMEOBJECT_PARENTROTATION+2/3
127 SetFloatValue(OBJECT_FIELD_SCALE_X
, goinfo
->size
);
129 SetUInt32Value(GAMEOBJECT_FACTION
, goinfo
->faction
);
130 SetUInt32Value(GAMEOBJECT_FLAGS
, goinfo
->flags
);
132 SetEntry(goinfo
->id
);
134 SetUInt32Value(GAMEOBJECT_DISPLAYID
, goinfo
->displayId
);
136 SetGoState(go_state
);
137 SetGoType(GameobjectTypes(goinfo
->type
));
139 SetGoAnimProgress(animprogress
);
141 // Spell charges for GAMEOBJECT_TYPE_SPELLCASTER (22)
142 if (goinfo
->type
== GAMEOBJECT_TYPE_SPELLCASTER
)
143 m_charges
= goinfo
->spellcaster
.charges
;
145 //Notify the map's instance data.
146 //Only works if you create the object in it, not if it is moves to that map.
147 //Normally non-players do not teleport to other maps.
148 if(map
->IsDungeon() && ((InstanceMap
*)map
)->GetInstanceData())
150 ((InstanceMap
*)map
)->GetInstanceData()->OnObjectCreate(this);
156 void GameObject::Update(uint32
/*p_time*/)
158 if (IS_MO_TRANSPORT(GetGUID()))
160 //((Transport*)this)->Update(p_time);
170 case GAMEOBJECT_TYPE_TRAP
:
172 // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
173 Unit
* owner
= GetOwner();
174 if (owner
&& ((Player
*)owner
)->isInCombat())
175 m_cooldownTime
= time(NULL
) + GetGOInfo()->trap
.startDelay
;
176 m_lootState
= GO_READY
;
179 case GAMEOBJECT_TYPE_FISHINGNODE
:
181 // fishing code (bobber ready)
182 if( time(NULL
) > m_respawnTime
- FISHING_BOBBER_READY_TIME
)
184 // splash bobber (bobber ready now)
185 Unit
* caster
= GetOwner();
186 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
188 SetGoState(GO_STATE_ACTIVE
);
189 SetUInt32Value(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
193 BuildValuesUpdateBlockForPlayer(&udata
,((Player
*)caster
));
194 udata
.BuildPacket(&packet
);
195 ((Player
*)caster
)->GetSession()->SendPacket(&packet
);
197 WorldPacket
data(SMSG_GAMEOBJECT_CUSTOM_ANIM
,8+4);
200 ((Player
*)caster
)->SendMessageToSet(&data
,true);
203 m_lootState
= GO_READY
; // can be successfully open with some chance
208 m_lootState
= GO_READY
; // for other GOis same switched without delay to GO_READY
211 // NO BREAK for switch (m_lootState)
215 if (m_respawnTime
> 0) // timer on
217 if (m_respawnTime
<= time(NULL
)) // timer expired
220 m_SkillupList
.clear();
225 case GAMEOBJECT_TYPE_FISHINGNODE
: // can't fish now
227 Unit
* caster
= GetOwner();
228 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
230 if(caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
])
232 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->SendChannelUpdate(0);
233 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->finish(false);
236 WorldPacket
data(SMSG_FISH_NOT_HOOKED
,0);
237 ((Player
*)caster
)->GetSession()->SendPacket(&data
);
240 m_lootState
= GO_JUST_DEACTIVATED
;
243 case GAMEOBJECT_TYPE_DOOR
:
244 case GAMEOBJECT_TYPE_BUTTON
:
245 //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)
246 if (GetGoState() != GO_STATE_READY
)
248 //flags in AB are type_button and we need to add them here so no break!
250 if (!m_spawnedByDefault
) // despawn timer
252 // can be despawned or destroyed
253 SetLootState(GO_JUST_DEACTIVATED
);
257 uint16 poolid
= poolhandler
.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT
);
259 poolhandler
.UpdatePool(poolid
, GetGUIDLow(), TYPEID_GAMEOBJECT
);
267 // traps can have time and can not have
268 GameObjectInfo
const* goInfo
= GetGOInfo();
269 if(goInfo
->type
== GAMEOBJECT_TYPE_TRAP
)
272 Unit
* owner
= GetOwner();
273 Unit
* ok
= NULL
; // pointer to appropriate target if found any
275 if(m_cooldownTime
>= time(NULL
))
278 bool IsBattleGroundTrap
= false;
279 //FIXME: this is activation radius (in different casting radius that must be selected from spell data)
280 //TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
281 float radius
= goInfo
->trap
.radius
;
284 if(goInfo
->trap
.cooldown
!= 3) // cast in other case (at some triggering/linked go/etc explicit call)
288 if(m_respawnTime
> 0)
291 radius
= goInfo
->trap
.cooldown
; // battlegrounds gameobjects has data2 == 0 && data5 == 3
292 IsBattleGroundTrap
= true;
296 bool NeedDespawn
= (goInfo
->trap
.charges
!= 0);
298 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
300 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
302 // Note: this hack with search required until GO casting not implemented
303 // search unfriendly creature
304 if(owner
&& NeedDespawn
) // hunter trap
306 MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
u_check(this, owner
, radius
);
307 MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
> checker(this,ok
, u_check
);
309 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
311 TypeContainerVisitor
<MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_object_checker(checker
);
312 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap());
314 // or unfriendly player/pet
317 TypeContainerVisitor
<MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_object_checker(checker
);
318 cell_lock
->Visit(cell_lock
, world_object_checker
, *GetMap());
321 else // environmental trap
323 // environmental damage spells already have around enemies targeting but this not help in case not existed GO casting support
325 // affect only players
327 MaNGOS::AnyPlayerInObjectRangeCheck
p_check(this, radius
);
328 MaNGOS::PlayerSearcher
<MaNGOS::AnyPlayerInObjectRangeCheck
> checker(this,p_ok
, p_check
);
330 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
332 TypeContainerVisitor
<MaNGOS::PlayerSearcher
<MaNGOS::AnyPlayerInObjectRangeCheck
>, WorldTypeMapContainer
> world_object_checker(checker
);
333 cell_lock
->Visit(cell_lock
, world_object_checker
, *GetMap());
339 Unit
*caster
= owner
? owner
: ok
;
341 caster
->CastSpell(ok
, goInfo
->trap
.spellId
, true, 0, 0, GetGUID());
342 m_cooldownTime
= time(NULL
) + 4; // 4 seconds
345 SetLootState(GO_JUST_DEACTIVATED
); // can be despawned or destroyed
347 if(IsBattleGroundTrap
&& ok
->GetTypeId() == TYPEID_PLAYER
)
349 //BattleGround gameobjects case
350 if(((Player
*)ok
)->InBattleGround())
351 if(BattleGround
*bg
= ((Player
*)ok
)->GetBattleGround())
352 bg
->HandleTriggerBuff(GetGUID());
357 if (m_charges
&& m_usetimes
>= m_charges
)
358 SetLootState(GO_JUST_DEACTIVATED
); // can be despawned or destroyed
366 case GAMEOBJECT_TYPE_DOOR
:
367 case GAMEOBJECT_TYPE_BUTTON
:
368 if (GetAutoCloseTime() && (m_cooldownTime
< time(NULL
)))
374 case GO_JUST_DEACTIVATED
:
376 //if Gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
377 if (GetGoType() == GAMEOBJECT_TYPE_GOOBER
)
379 uint32 spellId
= GetGOInfo()->goober
.spellId
;
383 std::set
<uint32
>::const_iterator it
= m_unique_users
.begin();
384 std::set
<uint32
>::const_iterator end
= m_unique_users
.end();
385 for (; it
!= end
; it
++)
387 Unit
* owner
= Unit::GetUnit(*this, uint64(*it
));
388 if (owner
) owner
->CastSpell(owner
, spellId
, false, 0, 0, GetGUID());
391 m_unique_users
.clear();
394 //any return here in case battleground traps
404 //burning flags in some battlegrounds, if you find better condition, just add it
405 if (GetGoAnimProgress() > 0)
407 SendObjectDeSpawnAnim(GetGUID());
409 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
413 SetLootState(GO_READY
);
415 if(!m_respawnDelayTime
)
418 if(!m_spawnedByDefault
)
424 m_respawnTime
= time(NULL
) + m_respawnDelayTime
;
426 // if option not set then object will be saved at grid unload
427 if(sWorld
.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY
))
430 ObjectAccessor::UpdateObjectVisibility(this);
437 void GameObject::Refresh()
439 // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
440 if(m_respawnTime
> 0 && m_spawnedByDefault
)
447 void GameObject::AddUniqueUse(Player
* player
)
450 m_unique_users
.insert(player
->GetGUIDLow());
453 void GameObject::Delete()
455 SendObjectDeSpawnAnim(GetGUID());
457 SetGoState(GO_STATE_READY
);
458 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
460 uint16 poolid
= poolhandler
.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT
);
462 poolhandler
.UpdatePool(poolid
, GetGUIDLow(), TYPEID_GAMEOBJECT
);
464 AddObjectToRemoveList();
467 void GameObject::getFishLoot(Loot
*fishloot
, Player
* loot_owner
)
471 uint32 zone
, subzone
;
472 GetZoneAndAreaId(zone
,subzone
);
474 // if subzone loot exist use it
475 if(LootTemplates_Fishing
.HaveLootFor(subzone
))
476 fishloot
->FillLoot(subzone
, LootTemplates_Fishing
, loot_owner
,true);
477 // else use zone loot
479 fishloot
->FillLoot(zone
, LootTemplates_Fishing
, loot_owner
,true);
482 void GameObject::SaveToDB()
484 // this should only be used when the gameobject has already been loaded
485 // preferably after adding to map, because mapid may not be valid otherwise
486 GameObjectData
const *data
= objmgr
.GetGOData(m_DBTableGuid
);
489 sLog
.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
493 SaveToDB(GetMapId(), data
->spawnMask
, data
->phaseMask
);
496 void GameObject::SaveToDB(uint32 mapid
, uint8 spawnMask
, uint32 phaseMask
)
498 const GameObjectInfo
*goI
= GetGOInfo();
504 m_DBTableGuid
= GetGUIDLow();
505 // update in loaded data (changing data only in this place)
506 GameObjectData
& data
= objmgr
.NewGOData(m_DBTableGuid
);
508 // data->guid = guid don't must be update at save
509 data
.id
= GetEntry();
511 data
.phaseMask
= phaseMask
;
512 data
.posX
= GetPositionX();
513 data
.posY
= GetPositionY();
514 data
.posZ
= GetPositionZ();
515 data
.orientation
= GetOrientation();
516 data
.rotation0
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+0);
517 data
.rotation1
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+1);
518 data
.rotation2
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+2);
519 data
.rotation3
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+3);
520 data
.spawntimesecs
= m_spawnedByDefault
? m_respawnDelayTime
: -(int32
)m_respawnDelayTime
;
521 data
.animprogress
= GetGoAnimProgress();
522 data
.go_state
= GetGoState();
523 data
.spawnMask
= spawnMask
;
526 std::ostringstream ss
;
527 ss
<< "INSERT INTO gameobject VALUES ( "
528 << m_DBTableGuid
<< ", "
529 << GetEntry() << ", "
531 << uint32(spawnMask
) << "," // cast to prevent save as symbol
532 << uint16(GetPhaseMask()) << "," // prevent out of range error
533 << GetPositionX() << ", "
534 << GetPositionY() << ", "
535 << GetPositionZ() << ", "
536 << GetOrientation() << ", "
537 << GetFloatValue(GAMEOBJECT_PARENTROTATION
) << ", "
538 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+1) << ", "
539 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+2) << ", "
540 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+3) << ", "
541 << m_respawnDelayTime
<< ", "
542 << uint32(GetGoAnimProgress()) << ", "
543 << uint32(GetGoState()) << ")";
545 WorldDatabase
.BeginTransaction();
546 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
547 WorldDatabase
.PExecuteLog( ss
.str( ).c_str( ) );
548 WorldDatabase
.CommitTransaction();
551 bool GameObject::LoadFromDB(uint32 guid
, Map
*map
)
553 GameObjectData
const* data
= objmgr
.GetGOData(guid
);
557 sLog
.outErrorDb("Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid
);
561 uint32 entry
= data
->id
;
562 //uint32 map_id = data->mapid; // already used before call
563 uint32 phaseMask
= data
->phaseMask
;
564 float x
= data
->posX
;
565 float y
= data
->posY
;
566 float z
= data
->posZ
;
567 float ang
= data
->orientation
;
569 float rotation0
= data
->rotation0
;
570 float rotation1
= data
->rotation1
;
571 float rotation2
= data
->rotation2
;
572 float rotation3
= data
->rotation3
;
574 uint32 animprogress
= data
->animprogress
;
575 GOState go_state
= data
->go_state
;
577 m_DBTableGuid
= guid
;
578 if (map
->GetInstanceId() != 0) guid
= objmgr
.GenerateLowGuid(HIGHGUID_GAMEOBJECT
);
580 if (!Create(guid
,entry
, map
, phaseMask
, x
, y
, z
, ang
, rotation0
, rotation1
, rotation2
, rotation3
, animprogress
, go_state
) )
583 if(!GetDespawnPossibility())
585 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
586 m_spawnedByDefault
= true;
587 m_respawnDelayTime
= 0;
592 if(data
->spawntimesecs
>= 0)
594 m_spawnedByDefault
= true;
595 m_respawnDelayTime
= data
->spawntimesecs
;
596 m_respawnTime
= objmgr
.GetGORespawnTime(m_DBTableGuid
, map
->GetInstanceId());
599 if(m_respawnTime
&& m_respawnTime
<= time(NULL
))
602 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
607 m_spawnedByDefault
= false;
608 m_respawnDelayTime
= -data
->spawntimesecs
;
616 void GameObject::DeleteFromDB()
618 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
619 objmgr
.DeleteGOData(m_DBTableGuid
);
620 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
621 WorldDatabase
.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid
);
624 GameObjectInfo
const *GameObject::GetGOInfo() const
629 uint32
GameObject::GetLootId(GameObjectInfo
const* ginfo
)
636 case GAMEOBJECT_TYPE_CHEST
:
637 return ginfo
->chest
.lootId
;
638 case GAMEOBJECT_TYPE_FISHINGHOLE
:
639 return ginfo
->fishinghole
.lootId
;
645 /*********************************************************/
646 /*** QUEST SYSTEM ***/
647 /*********************************************************/
648 bool GameObject::hasQuest(uint32 quest_id
) const
650 QuestRelations
const& qr
= objmgr
.mGOQuestRelations
;
651 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
653 if(itr
->second
==quest_id
)
659 bool GameObject::hasInvolvedQuest(uint32 quest_id
) const
661 QuestRelations
const& qr
= objmgr
.mGOQuestInvolvedRelations
;
662 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
664 if(itr
->second
==quest_id
)
670 bool GameObject::IsTransport() const
672 // If something is marked as a transport, don't transmit an out of range packet for it.
673 GameObjectInfo
const * gInfo
= GetGOInfo();
674 if(!gInfo
) return false;
675 return gInfo
->type
== GAMEOBJECT_TYPE_TRANSPORT
|| gInfo
->type
== GAMEOBJECT_TYPE_MO_TRANSPORT
;
678 Unit
* GameObject::GetOwner() const
680 return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
683 void GameObject::SaveRespawnTime()
685 if(m_respawnTime
> time(NULL
) && m_spawnedByDefault
)
686 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),m_respawnTime
);
689 bool GameObject::isVisibleForInState(Player
const* u
, bool inVisibleList
) const
692 if(!IsInWorld() || !u
->IsInWorld())
695 // Transport always visible at this step implementation
696 if(IsTransport() && IsInMap(u
))
699 // quick check visibility false cases for non-GM-mode
700 if(!u
->isGameMaster())
702 // despawned and then not visible for non-GM in GM-mode
706 // special invisibility cases
707 /* TODO: implement trap stealth, take look at spell 2836
708 if(GetGOInfo()->type == GAMEOBJECT_TYPE_TRAP && GetGOInfo()->trap.stealthed && u->IsHostileTo(GetOwner()))
716 return IsWithinDistInMap(u
,World::GetMaxVisibleDistanceForObject() +
717 (inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);
720 void GameObject::Respawn()
722 if(m_spawnedByDefault
&& m_respawnTime
> 0)
724 m_respawnTime
= time(NULL
);
725 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
729 bool GameObject::ActivateToQuest( Player
*pTarget
)const
731 if(!objmgr
.IsGameObjectForQuests(GetEntry()))
736 // scan GO chest with loot including quest items
737 case GAMEOBJECT_TYPE_CHEST
:
739 if(LootTemplates_Gameobject
.HaveQuestLootForPlayer(GetLootId(), pTarget
))
743 case GAMEOBJECT_TYPE_GOOBER
:
745 if(pTarget
->GetQuestStatus(GetGOInfo()->goober
.questId
) == QUEST_STATUS_INCOMPLETE
)
756 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry
, Unit
* target
)
758 GameObjectInfo
const* trapInfo
= sGOStorage
.LookupEntry
<GameObjectInfo
>(trapEntry
);
759 if(!trapInfo
|| trapInfo
->type
!=GAMEOBJECT_TYPE_TRAP
)
762 SpellEntry
const* trapSpell
= sSpellStore
.LookupEntry(trapInfo
->trap
.spellId
);
763 if(!trapSpell
) // checked at load already
766 float range
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(trapSpell
->rangeIndex
));
768 // search nearest linked GO
769 GameObject
* trapGO
= NULL
;
771 // using original GO distance
772 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
774 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
776 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*target
,trapEntry
,range
);
777 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(this, trapGO
,go_check
);
779 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
780 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
781 cell_lock
->Visit(cell_lock
, object_checker
, *GetMap());
785 // FIXME: when GO casting will be implemented trap must cast spell to target
787 target
->CastSpell(target
,trapSpell
,true, 0, 0, GetGUID());
790 GameObject
* GameObject::LookupFishingHoleAround(float range
)
792 GameObject
* ok
= NULL
;
794 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
796 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
797 MaNGOS::NearestGameObjectFishingHole
u_check(*this, range
);
798 MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
> checker(this, ok
, u_check
);
800 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
802 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
>, GridTypeMapContainer
> grid_object_checker(checker
);
803 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap());
808 void GameObject::ResetDoorOrButton()
810 if (m_lootState
== GO_READY
|| m_lootState
== GO_JUST_DEACTIVATED
)
813 SwitchDoorOrButton(false);
814 SetLootState(GO_JUST_DEACTIVATED
);
818 void GameObject::UseDoorOrButton(uint32 time_to_restore
, bool alternative
/* = false */)
820 if(m_lootState
!= GO_READY
)
824 time_to_restore
= GetAutoCloseTime();
826 SwitchDoorOrButton(true,alternative
);
827 SetLootState(GO_ACTIVATED
);
829 m_cooldownTime
= time(NULL
) + time_to_restore
;
832 void GameObject::SwitchDoorOrButton(bool activate
, bool alternative
/* = false */)
835 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
837 RemoveFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
839 if(GetGoState() == GO_STATE_READY
) //if closed -> open
840 SetGoState(alternative
? GO_STATE_ACTIVE_ALTERNATIVE
: GO_STATE_ACTIVE
);
841 else //if open -> close
842 SetGoState(GO_STATE_READY
);
845 void GameObject::Use(Unit
* user
)
847 // by default spell caster is user
848 Unit
* spellCaster
= user
;
850 bool triggered
= false;
854 case GAMEOBJECT_TYPE_DOOR
: //0
855 case GAMEOBJECT_TYPE_BUTTON
: //1
856 //doors/buttons never really despawn, only reset to default state/flags
860 sWorld
.ScriptsStart(sGameObjectScripts
, GetDBTableGUIDLow(), spellCaster
, this);
863 case GAMEOBJECT_TYPE_QUESTGIVER
: //2
865 if(user
->GetTypeId()!=TYPEID_PLAYER
)
868 Player
* player
= (Player
*)user
;
870 player
->PrepareQuestMenu( GetGUID() );
871 player
->SendPreparedQuest( GetGUID() );
874 //Sitting: Wooden bench, chairs enzz
875 case GAMEOBJECT_TYPE_CHAIR
: //7
877 GameObjectInfo
const* info
= GetGOInfo();
881 if(user
->GetTypeId()!=TYPEID_PLAYER
)
884 Player
* player
= (Player
*)user
;
886 // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
888 // check if the db is sane
889 if(info
->chair
.slots
> 0)
891 float lowestDist
= DEFAULT_VISIBILITY_DISTANCE
;
893 float x_lowest
= GetPositionX();
894 float y_lowest
= GetPositionY();
896 // the object orientation + 1/2 pi
897 // every slot will be on that straight line
898 float orthogonalOrientation
= GetOrientation()+M_PI
*0.5f
;
900 for(uint32 i
=0; i
<info
->chair
.slots
; ++i
)
902 // the distance between this slot and the center of the go - imagine a 1D space
903 float relativeDistance
= (info
->size
*i
)-(info
->size
*(info
->chair
.slots
-1)/2.0f
);
905 float x_i
= GetPositionX() + relativeDistance
* cos(orthogonalOrientation
);
906 float y_i
= GetPositionY() + relativeDistance
* sin(orthogonalOrientation
);
908 // calculate the distance between the player and this slot
909 float thisDistance
= player
->GetDistance2d(x_i
, y_i
);
911 /* debug code. It will spawn a npc on each slot to visualize them.
912 Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
913 std::ostringstream output;
914 output << i << ": thisDist: " << thisDistance;
915 helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
918 if(thisDistance
<= lowestDist
)
920 lowestDist
= thisDistance
;
925 player
->TeleportTo(GetMapId(), x_lowest
, y_lowest
, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
929 // fallback, will always work
930 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
932 player
->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR
+info
->chair
.height
);
935 //big gun, its a spell/aura
936 case GAMEOBJECT_TYPE_GOOBER
: //10
938 GameObjectInfo
const* info
= GetGOInfo();
940 if(user
->GetTypeId()==TYPEID_PLAYER
)
942 Player
* player
= (Player
*)user
;
945 if(info
->goober
.pageId
)
947 WorldPacket
data(SMSG_GAMEOBJECT_PAGETEXT
, 8);
949 player
->GetSession()->SendPacket(&data
);
952 // possible quest objective for active quests
953 player
->CastedCreatureOrGO(info
->id
, GetGUID(), 0);
956 // cast this spell later if provided
957 spellId
= info
->goober
.spellId
;
961 case GAMEOBJECT_TYPE_CAMERA
: //13
963 GameObjectInfo
const* info
= GetGOInfo();
967 if(user
->GetTypeId()!=TYPEID_PLAYER
)
970 Player
* player
= (Player
*)user
;
972 if (info
->camera
.cinematicId
)
973 player
->SendCinematicStart(info
->camera
.cinematicId
);
978 case GAMEOBJECT_TYPE_FISHINGNODE
: //17
980 if(user
->GetTypeId()!=TYPEID_PLAYER
)
983 Player
* player
= (Player
*)user
;
985 if(player
->GetGUID() != GetOwnerGUID())
988 switch(getLootState())
990 case GO_READY
: // ready for loot
992 // 1) skill must be >= base_zone_skill
993 // 2) if skill == base_zone_skill => 5% chance
994 // 3) chance is linear dependence from (base_zone_skill-skill)
996 uint32 zone
, subzone
;
997 GetZoneAndAreaId(zone
,subzone
);
999 int32 zone_skill
= objmgr
.GetFishingBaseSkillLevel( subzone
);
1001 zone_skill
= objmgr
.GetFishingBaseSkillLevel( zone
);
1003 //provide error, no fishable zone or area should be 0
1005 sLog
.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone
);
1007 int32 skill
= player
->GetSkillValue(SKILL_FISHING
);
1008 int32 chance
= skill
- zone_skill
+ 5;
1009 int32 roll
= irand(1,100);
1011 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill
,zone_skill
,chance
,roll
);
1013 if(skill
>= zone_skill
&& chance
>= roll
)
1015 // prevent removing GO at spell cancel
1016 player
->RemoveGameObject(this,false);
1017 SetOwnerGUID(player
->GetGUID());
1020 player
->UpdateFishingSkill();
1022 GameObject
* ok
= LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE
);
1025 player
->SendLoot(ok
->GetGUID(),LOOT_FISHINGHOLE
);
1026 player
->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_FISH_IN_GAMEOBJECT
, ok
->GetGOInfo()->id
);
1027 SetLootState(GO_JUST_DEACTIVATED
);
1030 player
->SendLoot(GetGUID(),LOOT_FISHING
);
1034 // fish escaped, can be deleted now
1035 SetLootState(GO_JUST_DEACTIVATED
);
1037 WorldPacket
data(SMSG_FISH_ESCAPED
, 0);
1038 player
->GetSession()->SendPacket(&data
);
1042 case GO_JUST_DEACTIVATED
: // nothing to do, will be deleted at next update
1046 SetLootState(GO_JUST_DEACTIVATED
);
1048 WorldPacket
data(SMSG_FISH_NOT_HOOKED
, 0);
1049 player
->GetSession()->SendPacket(&data
);
1054 if(player
->m_currentSpells
[CURRENT_CHANNELED_SPELL
])
1056 player
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->SendChannelUpdate(0);
1057 player
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->finish();
1062 case GAMEOBJECT_TYPE_SUMMONING_RITUAL
: //18
1064 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1067 Player
* player
= (Player
*)user
;
1069 Unit
* caster
= GetOwner();
1071 GameObjectInfo
const* info
= GetGOInfo();
1073 if( !caster
|| caster
->GetTypeId()!=TYPEID_PLAYER
)
1076 // accept only use by player from same group for caster except caster itself
1077 if(((Player
*)caster
)==player
|| !((Player
*)caster
)->IsInSameRaidWith(player
))
1080 AddUniqueUse(player
);
1082 // full amount unique participants including original summoner
1083 if(GetUniqueUseCount() < info
->summoningRitual
.reqParticipants
)
1086 // in case summoning ritual caster is GO creator
1087 spellCaster
= caster
;
1089 if(!caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
])
1092 spellId
= info
->summoningRitual
.spellId
;
1093 if(spellId
==62330) // GO store not existed spell, replace by expected
1095 // spell have reagent and mana cost but it not expected use its
1096 // it triggered spell in fact casted at currently channeled GO
1102 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->SendChannelUpdate(0);
1103 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->finish();
1105 // can be deleted now
1106 SetLootState(GO_JUST_DEACTIVATED
);
1108 // go to end function to spell casting
1111 case GAMEOBJECT_TYPE_SPELLCASTER
: //22
1113 SetUInt32Value(GAMEOBJECT_FLAGS
,2);
1115 GameObjectInfo
const* info
= GetGOInfo();
1119 if(info
->spellcaster
.partyOnly
)
1121 Unit
* caster
= GetOwner();
1122 if( !caster
|| caster
->GetTypeId()!=TYPEID_PLAYER
)
1125 if(user
->GetTypeId()!=TYPEID_PLAYER
|| !((Player
*)user
)->IsInSameRaidWith((Player
*)caster
))
1129 spellId
= info
->spellcaster
.spellId
;
1134 case GAMEOBJECT_TYPE_MEETINGSTONE
: //23
1136 GameObjectInfo
const* info
= GetGOInfo();
1138 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1141 Player
* player
= (Player
*)user
;
1143 Player
* targetPlayer
= ObjectAccessor::FindPlayer(player
->GetSelection());
1145 // accept only use by player from same group for caster except caster itself
1146 if(!targetPlayer
|| targetPlayer
== player
|| !targetPlayer
->IsInSameGroupWith(player
))
1149 //required lvl checks!
1150 uint8 level
= player
->getLevel();
1151 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1153 level
= targetPlayer
->getLevel();
1154 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1157 if(info
->id
==194097)
1158 spellId
= 61994; // Ritual of Summoning
1160 spellId
= 59782; // Summoning Stone Effect
1165 case GAMEOBJECT_TYPE_FLAGSTAND
: // 24
1167 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1170 Player
* player
= (Player
*)user
;
1172 if( player
->CanUseBattleGroundObject() )
1174 // in battleground check
1175 BattleGround
*bg
= player
->GetBattleGround();
1185 bg
->EventPlayerClickedOnFlag(player
, this);
1186 return; //we don;t need to delete flag ... it is despawned!
1190 case GAMEOBJECT_TYPE_FLAGDROP
: // 26
1192 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1195 Player
* player
= (Player
*)user
;
1197 if( player
->CanUseBattleGroundObject() )
1199 // in battleground check
1200 BattleGround
*bg
= player
->GetBattleGround();
1205 // 179785 - Silverwing Flag
1206 // 179786 - Warsong Flag
1208 // 184142 - Netherstorm Flag
1209 GameObjectInfo
const* info
= GetGOInfo();
1214 case 179785: // Silverwing Flag
1215 // check if it's correct bg
1216 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1217 bg
->EventPlayerClickedOnFlag(player
, this);
1219 case 179786: // Warsong Flag
1220 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1221 bg
->EventPlayerClickedOnFlag(player
, this);
1223 case 184142: // Netherstorm Flag
1224 if(bg
->GetTypeID() == BATTLEGROUND_EY
)
1225 bg
->EventPlayerClickedOnFlag(player
, this);
1229 //this cause to call return, all flags must be deleted here!!
1235 case GAMEOBJECT_TYPE_BARBER_CHAIR
: //32
1237 GameObjectInfo
const* info
= GetGOInfo();
1241 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1244 Player
* player
= (Player
*)user
;
1246 // fallback, will always work
1247 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
1249 WorldPacket
data(SMSG_ENABLE_BARBER_SHOP
, 0);
1250 player
->GetSession()->SendPacket(&data
);
1252 player
->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR
+info
->barberChair
.chairheight
);
1256 sLog
.outDebug("Unknown Object Type %u", GetGoType());
1263 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry( spellId
);
1266 sLog
.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId
,GetEntry(),GetGoType());
1270 Spell
*spell
= new Spell(spellCaster
, spellInfo
, triggered
);
1272 // spell target is user of GO
1273 SpellCastTargets targets
;
1274 targets
.setUnitTarget( user
);
1276 spell
->prepare(&targets
);
1279 // overwrite WorldObject function for proper name localization
1280 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx
) const
1284 GameObjectLocale
const *cl
= objmgr
.GetGameObjectLocale(GetEntry());
1287 if (cl
->Name
.size() > loc_idx
&& !cl
->Name
[loc_idx
].empty())
1288 return cl
->Name
[loc_idx
].c_str();
1295 void GameObject::UpdateRotationFields(float rotation2
/*=0.0f*/, float rotation3
/*=0.0f*/)
1297 static double const atan_pow
= atan(pow(2.0f
, -20.0f
));
1299 double f_rot1
= sin(GetOrientation() / 2.0f
);
1300 double f_rot2
= cos(GetOrientation() / 2.0f
);
1302 int64 i_rot1
= int64(f_rot1
/ atan_pow
*(f_rot2
>= 0 ? 1.0f
: -1.0f
));
1303 int64 rotation
= (i_rot1
<< 43 >> 43) & 0x00000000001FFFFF;
1305 //float f_rot2 = sin(0.0f / 2.0f);
1306 //int64 i_rot2 = f_rot2 / atan(pow(2.0f, -20.0f));
1307 //rotation |= (((i_rot2 << 22) >> 32) >> 11) & 0x000003FFFFE00000;
1309 //float f_rot3 = sin(0.0f / 2.0f);
1310 //int64 i_rot3 = f_rot3 / atan(pow(2.0f, -21.0f));
1311 //rotation |= (i_rot3 >> 42) & 0x7FFFFC0000000000;
1313 m_rotation
= rotation
;
1315 if(rotation2
==0.0f
&& rotation3
==0.0f
)
1321 SetFloatValue(GAMEOBJECT_PARENTROTATION
+2, rotation2
);
1322 SetFloatValue(GAMEOBJECT_PARENTROTATION
+3, rotation3
);