2 * Copyright (C) 2005-2008 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"
25 #include "UpdateMask.h"
27 #include "WorldPacket.h"
28 #include "WorldSession.h"
30 #include "Database/DatabaseEnv.h"
31 #include "MapManager.h"
33 #include "GridNotifiers.h"
34 #include "GridNotifiersImpl.h"
36 #include "InstanceData.h"
37 #include "BattleGround.h"
40 GameObject::GameObject() : WorldObject()
42 m_objectType
|= TYPEMASK_GAMEOBJECT
;
43 m_objectTypeId
= TYPEID_GAMEOBJECT
;
45 m_updateFlag
= (UPDATEFLAG_LOWGUID
| UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HASPOSITION
);
47 m_valuesCount
= GAMEOBJECT_END
;
49 m_respawnDelayTime
= 25;
50 m_lootState
= GO_NOT_READY
;
51 m_spawnedByDefault
= true;
61 GameObject::~GameObject()
63 if(m_uint32Values
) // field array can be not exist if GameOBject not loaded
65 // crash possible 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
, float x
, float y
, float z
, float ang
, float rotation0
, float rotation1
, float rotation2
, float rotation3
, uint32 animprogress
, uint32 go_state
)
95 SetMapId(map
->GetId());
96 SetInstanceId(map
->GetInstanceId());
98 if(!IsPositionValid())
100 sLog
.outError("ERROR: Gameobject (GUID: %u Entry: %u ) not created. Suggested coordinates isn't valid (X: %f Y: %f)",guidlow
,name_id
,x
,y
);
104 GameObjectInfo
const* goinfo
= objmgr
.GetGameObjectInfo(name_id
);
107 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
);
111 Object::_Create(guidlow
, goinfo
->id
, HIGHGUID_GAMEOBJECT
);
115 if (goinfo
->type
>= MAX_GAMEOBJECT_TYPE
)
117 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
);
121 SetFloatValue(GAMEOBJECT_POS_X
, x
);
122 SetFloatValue(GAMEOBJECT_POS_Y
, y
);
123 SetFloatValue(GAMEOBJECT_POS_Z
, z
);
124 SetFloatValue(GAMEOBJECT_FACING
, ang
); //this is not facing angle
126 SetFloatValue (GAMEOBJECT_ROTATION
, rotation0
);
127 SetFloatValue (GAMEOBJECT_ROTATION
+1, rotation1
);
128 SetFloatValue (GAMEOBJECT_ROTATION
+2, rotation2
);
129 SetFloatValue (GAMEOBJECT_ROTATION
+3, rotation3
);
131 SetFloatValue(OBJECT_FIELD_SCALE_X
, goinfo
->size
);
133 SetUInt32Value(GAMEOBJECT_FACTION
, goinfo
->faction
);
134 SetUInt32Value(GAMEOBJECT_FLAGS
, goinfo
->flags
);
136 SetEntry(goinfo
->id
);
138 SetUInt32Value(GAMEOBJECT_DISPLAYID
, goinfo
->displayId
);
140 SetGoState(go_state
);
141 SetGoType(GameobjectTypes(goinfo
->type
));
143 SetGoAnimProgress(animprogress
);
145 // Spell charges for GAMEOBJECT_TYPE_SPELLCASTER (22)
146 if (goinfo
->type
== GAMEOBJECT_TYPE_SPELLCASTER
)
147 m_charges
= goinfo
->spellcaster
.charges
;
149 //Notify the map's instance data.
150 //Only works if you create the object in it, not if it is moves to that map.
151 //Normally non-players do not teleport to other maps.
152 if(map
->IsDungeon() && ((InstanceMap
*)map
)->GetInstanceData())
154 ((InstanceMap
*)map
)->GetInstanceData()->OnObjectCreate(this);
160 void GameObject::Update(uint32
/*p_time*/)
162 if (IS_MO_TRANSPORT(GetGUID()))
164 //((Transport*)this)->Update(p_time);
174 case GAMEOBJECT_TYPE_TRAP
:
176 // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
177 Unit
* owner
= GetOwner();
178 if (owner
&& ((Player
*)owner
)->isInCombat())
179 m_cooldownTime
= time(NULL
) + GetGOInfo()->trap
.startDelay
;
180 m_lootState
= GO_READY
;
183 case GAMEOBJECT_TYPE_FISHINGNODE
:
185 // fishing code (bobber ready)
186 if( time(NULL
) > m_respawnTime
- FISHING_BOBBER_READY_TIME
)
188 // splash bobber (bobber ready now)
189 Unit
* caster
= GetOwner();
190 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
193 SetUInt32Value(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
197 BuildValuesUpdateBlockForPlayer(&udata
,((Player
*)caster
));
198 udata
.BuildPacket(&packet
);
199 ((Player
*)caster
)->GetSession()->SendPacket(&packet
);
201 WorldPacket
data(SMSG_GAMEOBJECT_CUSTOM_ANIM
,8+4);
204 ((Player
*)caster
)->SendMessageToSet(&data
,true);
207 m_lootState
= GO_READY
; // can be successfully open with some chance
212 m_lootState
= GO_READY
; // for other GOis same switched without delay to GO_READY
215 // NO BREAK for switch (m_lootState)
219 if (m_respawnTime
> 0) // timer on
221 if (m_respawnTime
<= time(NULL
)) // timer expired
224 m_SkillupList
.clear();
229 case GAMEOBJECT_TYPE_FISHINGNODE
: // can't fish now
231 Unit
* caster
= GetOwner();
232 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
234 if(caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
])
236 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->SendChannelUpdate(0);
237 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->finish(false);
240 WorldPacket
data(SMSG_FISH_NOT_HOOKED
,0);
241 ((Player
*)caster
)->GetSession()->SendPacket(&data
);
244 m_lootState
= GO_JUST_DEACTIVATED
;
247 case GAMEOBJECT_TYPE_DOOR
:
248 case GAMEOBJECT_TYPE_BUTTON
:
249 //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)
251 SwitchDoorOrButton(false);
252 //flags in AB are type_button and we need to add them here so no break!
254 if(!m_spawnedByDefault
) // despawn timer
256 // can be despawned or destroyed
257 SetLootState(GO_JUST_DEACTIVATED
);
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(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(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);
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
)))
370 SwitchDoorOrButton(false);
371 SetLootState(GO_JUST_DEACTIVATED
);
377 case GO_JUST_DEACTIVATED
:
379 //if Gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
380 if (GetGoType() == GAMEOBJECT_TYPE_GOOBER
)
382 uint32 spellId
= GetGOInfo()->goober
.spellId
;
386 std::set
<uint32
>::iterator it
= m_unique_users
.begin();
387 std::set
<uint32
>::iterator end
= m_unique_users
.end();
388 for (; it
!= end
; it
++)
390 Unit
* owner
= Unit::GetUnit(*this, uint64(*it
));
391 if (owner
) owner
->CastSpell(owner
, spellId
, false);
394 m_unique_users
.clear();
397 //any return here in case battleground traps
407 //burning flags in some battlegrounds, if you find better condition, just add it
408 if (GetGoAnimProgress() > 0)
410 SendObjectDeSpawnAnim(GetGUID());
412 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
416 SetLootState(GO_READY
);
418 if(!m_respawnDelayTime
)
421 if(!m_spawnedByDefault
)
427 m_respawnTime
= time(NULL
) + m_respawnDelayTime
;
429 // if option not set then object will be saved at grid unload
430 if(sWorld
.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY
))
433 ObjectAccessor::UpdateObjectVisibility(this);
440 void GameObject::Refresh()
442 // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
443 if(m_respawnTime
> 0 && m_spawnedByDefault
)
450 void GameObject::AddUniqueUse(Player
* player
)
453 m_unique_users
.insert(player
->GetGUIDLow());
456 void GameObject::Delete()
458 SendObjectDeSpawnAnim(GetGUID());
461 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
463 AddObjectToRemoveList();
466 void GameObject::getFishLoot(Loot
*fishloot
)
470 uint32 subzone
= GetAreaId();
472 // if subzone loot exist use it
473 if(LootTemplates_Fishing
.HaveLootFor(subzone
))
474 fishloot
->FillLoot(subzone
, LootTemplates_Fishing
, NULL
);
475 // else use zone loot
477 fishloot
->FillLoot(GetZoneId(), LootTemplates_Fishing
, NULL
);
480 void GameObject::SaveToDB()
482 // this should only be used when the gameobject has already been loaded
483 // preferably after adding to map, because mapid may not be valid otherwise
484 GameObjectData
const *data
= objmgr
.GetGOData(m_DBTableGuid
);
487 sLog
.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
491 SaveToDB(GetMapId(), data
->spawnMask
);
494 void GameObject::SaveToDB(uint32 mapid
, uint8 spawnMask
)
496 const GameObjectInfo
*goI
= GetGOInfo();
502 m_DBTableGuid
= GetGUIDLow();
503 // update in loaded data (changing data only in this place)
504 GameObjectData
& data
= objmgr
.NewGOData(m_DBTableGuid
);
506 // data->guid = guid don't must be update at save
507 data
.id
= GetEntry();
509 data
.posX
= GetFloatValue(GAMEOBJECT_POS_X
);
510 data
.posY
= GetFloatValue(GAMEOBJECT_POS_Y
);
511 data
.posZ
= GetFloatValue(GAMEOBJECT_POS_Z
);
512 data
.orientation
= GetFloatValue(GAMEOBJECT_FACING
);
513 data
.rotation0
= GetFloatValue(GAMEOBJECT_ROTATION
+0);
514 data
.rotation1
= GetFloatValue(GAMEOBJECT_ROTATION
+1);
515 data
.rotation2
= GetFloatValue(GAMEOBJECT_ROTATION
+2);
516 data
.rotation3
= GetFloatValue(GAMEOBJECT_ROTATION
+3);
517 data
.spawntimesecs
= m_spawnedByDefault
? m_respawnDelayTime
: -(int32
)m_respawnDelayTime
;
518 data
.animprogress
= GetGoAnimProgress();
519 data
.go_state
= GetGoState();
520 data
.spawnMask
= spawnMask
;
523 std::ostringstream ss
;
524 ss
<< "INSERT INTO gameobject VALUES ( "
525 << m_DBTableGuid
<< ", "
526 << GetEntry() << ", "
528 << (uint32
)spawnMask
<< ", "
529 << GetFloatValue(GAMEOBJECT_POS_X
) << ", "
530 << GetFloatValue(GAMEOBJECT_POS_Y
) << ", "
531 << GetFloatValue(GAMEOBJECT_POS_Z
) << ", "
532 << GetFloatValue(GAMEOBJECT_FACING
) << ", "
533 << GetFloatValue(GAMEOBJECT_ROTATION
) << ", "
534 << GetFloatValue(GAMEOBJECT_ROTATION
+1) << ", "
535 << GetFloatValue(GAMEOBJECT_ROTATION
+2) << ", "
536 << GetFloatValue(GAMEOBJECT_ROTATION
+3) << ", "
537 << m_respawnDelayTime
<< ", "
538 << (uint32
)GetGoAnimProgress() << ", "
539 << (uint32
)GetGoState() << ")";
541 WorldDatabase
.BeginTransaction();
542 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
543 WorldDatabase
.PExecuteLog( ss
.str( ).c_str( ) );
544 WorldDatabase
.CommitTransaction();
547 bool GameObject::LoadFromDB(uint32 guid
, Map
*map
)
549 GameObjectData
const* data
= objmgr
.GetGOData(guid
);
553 sLog
.outErrorDb("ERROR: Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid
);
557 uint32 entry
= data
->id
;
558 //uint32 map_id = data->mapid; // already used before call
559 float x
= data
->posX
;
560 float y
= data
->posY
;
561 float z
= data
->posZ
;
562 float ang
= data
->orientation
;
564 float rotation0
= data
->rotation0
;
565 float rotation1
= data
->rotation1
;
566 float rotation2
= data
->rotation2
;
567 float rotation3
= data
->rotation3
;
569 uint32 animprogress
= data
->animprogress
;
570 uint32 go_state
= data
->go_state
;
572 m_DBTableGuid
= guid
;
573 if (map
->GetInstanceId() != 0) guid
= objmgr
.GenerateLowGuid(HIGHGUID_GAMEOBJECT
);
575 if (!Create(guid
,entry
, map
, x
, y
, z
, ang
, rotation0
, rotation1
, rotation2
, rotation3
, animprogress
, go_state
) )
578 switch(GetGOInfo()->type
)
580 case GAMEOBJECT_TYPE_DOOR
:
581 case GAMEOBJECT_TYPE_BUTTON
:
582 /* this code (in comment) isn't correct because in battlegrounds we need despawnable doors and buttons, pls remove
583 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
584 m_spawnedByDefault = true;
585 m_respawnDelayTime = 0;
589 if(data
->spawntimesecs
>= 0)
591 m_spawnedByDefault
= true;
592 m_respawnDelayTime
= data
->spawntimesecs
;
593 m_respawnTime
= objmgr
.GetGORespawnTime(m_DBTableGuid
, map
->GetInstanceId());
596 if(m_respawnTime
&& m_respawnTime
<= time(NULL
))
599 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
604 m_spawnedByDefault
= false;
605 m_respawnDelayTime
= -data
->spawntimesecs
;
614 void GameObject::DeleteFromDB()
616 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
617 objmgr
.DeleteGOData(m_DBTableGuid
);
618 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
619 WorldDatabase
.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid
);
622 GameObject
* GameObject::GetGameObject(WorldObject
& object
, uint64 guid
)
624 return ObjectAccessor::GetGameObject(object
,guid
);
627 GameObjectInfo
const *GameObject::GetGOInfo() const
632 uint32
GameObject::GetLootId(GameObjectInfo
const* ginfo
)
639 case GAMEOBJECT_TYPE_CHEST
:
640 return ginfo
->chest
.lootId
;
641 case GAMEOBJECT_TYPE_FISHINGHOLE
:
642 return ginfo
->fishinghole
.lootId
;
643 case GAMEOBJECT_TYPE_FISHINGNODE
:
644 return ginfo
->fishnode
.lootId
;
650 /*********************************************************/
651 /*** QUEST SYSTEM ***/
652 /*********************************************************/
653 bool GameObject::hasQuest(uint32 quest_id
) const
655 QuestRelations
const& qr
= objmgr
.mGOQuestRelations
;
656 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
658 if(itr
->second
==quest_id
)
664 bool GameObject::hasInvolvedQuest(uint32 quest_id
) const
666 QuestRelations
const& qr
= objmgr
.mGOQuestInvolvedRelations
;
667 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
669 if(itr
->second
==quest_id
)
675 bool GameObject::IsTransport() const
677 // If something is marked as a transport, don't transmit an out of range packet for it.
678 GameObjectInfo
const * gInfo
= GetGOInfo();
679 if(!gInfo
) return false;
680 return gInfo
->type
== GAMEOBJECT_TYPE_TRANSPORT
|| gInfo
->type
== GAMEOBJECT_TYPE_MO_TRANSPORT
;
683 Unit
* GameObject::GetOwner() const
685 return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
688 void GameObject::SaveRespawnTime()
690 if(m_respawnTime
> time(NULL
) && m_spawnedByDefault
)
691 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),m_respawnTime
);
694 bool GameObject::isVisibleForInState(Player
const* u
, bool inVisibleList
) const
697 if(!IsInWorld() || !u
->IsInWorld())
700 // Transport always visible at this step implementation
701 if(IsTransport() && IsInMap(u
))
704 // quick check visibility false cases for non-GM-mode
705 if(!u
->isGameMaster())
707 // despawned and then not visible for non-GM in GM-mode
711 // special invisibility cases
712 /* TODO: implement trap stealth, take look at spell 2836
713 if(GetGOInfo()->type == GAMEOBJECT_TYPE_TRAP && GetGOInfo()->trap.stealthed && u->IsHostileTo(GetOwner()))
719 // Smuggled Mana Cell required 10 invisibility type detection/state
720 if(GetEntry()==187039 && ((u
->m_detectInvisibilityMask
| u
->m_invisibilityMask
) & (1<<10))==0)
725 return IsWithinDistInMap(u
,World::GetMaxVisibleDistanceForObject() +
726 (inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);
729 void GameObject::Respawn()
731 if(m_spawnedByDefault
&& m_respawnTime
> 0)
733 m_respawnTime
= time(NULL
);
734 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
738 bool GameObject::ActivateToQuest( Player
*pTarget
)const
740 if(!objmgr
.IsGameObjectForQuests(GetEntry()))
745 // scan GO chest with loot including quest items
746 case GAMEOBJECT_TYPE_CHEST
:
748 if(LootTemplates_Gameobject
.HaveQuestLootForPlayer(GetLootId(), pTarget
))
752 case GAMEOBJECT_TYPE_GOOBER
:
754 if(pTarget
->GetQuestStatus(GetGOInfo()->goober
.questId
) == QUEST_STATUS_INCOMPLETE
)
765 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry
, Unit
* target
)
767 GameObjectInfo
const* trapInfo
= sGOStorage
.LookupEntry
<GameObjectInfo
>(trapEntry
);
768 if(!trapInfo
|| trapInfo
->type
!=GAMEOBJECT_TYPE_TRAP
)
771 SpellEntry
const* trapSpell
= sSpellStore
.LookupEntry(trapInfo
->trap
.spellId
);
772 if(!trapSpell
) // checked at load already
775 float range
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(trapSpell
->rangeIndex
));
777 // search nearest linked GO
778 GameObject
* trapGO
= NULL
;
780 // using original GO distance
781 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
783 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
785 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*target
,trapEntry
,range
);
786 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(trapGO
,go_check
);
788 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
789 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
790 cell_lock
->Visit(cell_lock
, object_checker
, *GetMap());
794 // FIXME: when GO casting will be implemented trap must cast spell to target
796 target
->CastSpell(target
,trapSpell
,true);
799 GameObject
* GameObject::LookupFishingHoleAround(float range
)
801 GameObject
* ok
= NULL
;
803 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
805 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
806 MaNGOS::NearestGameObjectFishingHole
u_check(*this, range
);
807 MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
> checker(ok
, u_check
);
809 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
811 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
>, GridTypeMapContainer
> grid_object_checker(checker
);
812 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap());
817 void GameObject::UseDoorOrButton(uint32 time_to_restore
)
819 if(m_lootState
!= GO_READY
)
823 time_to_restore
= GetAutoCloseTime();
825 SwitchDoorOrButton(true);
826 SetLootState(GO_ACTIVATED
);
828 m_cooldownTime
= time(NULL
) + time_to_restore
;
832 void GameObject::SwitchDoorOrButton(bool activate
)
835 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
837 RemoveFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
839 if(GetGoState()) //if closed -> open
841 else //if open -> close
845 void GameObject::Use(Unit
* user
)
847 // by default spell caster is user
848 Unit
* spellCaster
= user
;
853 case GAMEOBJECT_TYPE_DOOR
: //0
854 case GAMEOBJECT_TYPE_BUTTON
: //1
855 //doors/buttons never really despawn, only reset to default state/flags
859 sWorld
.ScriptsStart(sGameObjectScripts
, GetDBTableGUIDLow(), spellCaster
, this);
862 case GAMEOBJECT_TYPE_QUESTGIVER
: //2
864 if(user
->GetTypeId()!=TYPEID_PLAYER
)
867 Player
* player
= (Player
*)user
;
869 player
->PrepareQuestMenu( GetGUID() );
870 player
->SendPreparedQuest( GetGUID() );
873 //Sitting: Wooden bench, chairs enzz
874 case GAMEOBJECT_TYPE_CHAIR
: //7
876 GameObjectInfo
const* info
= GetGOInfo();
880 if(user
->GetTypeId()!=TYPEID_PLAYER
)
883 Player
* player
= (Player
*)user
;
885 // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
887 // check if the db is sane
888 if(info
->chair
.slots
> 0)
890 float lowestDist
= DEFAULT_VISIBILITY_DISTANCE
;
892 float x_lowest
= GetPositionX();
893 float y_lowest
= GetPositionY();
895 // the object orientation + 1/2 pi
896 // every slot will be on that straight line
897 float orthogonalOrientation
= GetOrientation()+M_PI
*0.5f
;
899 for(uint32 i
=0; i
<info
->chair
.slots
; i
++)
901 // the distance between this slot and the center of the go - imagine a 1D space
902 float relativeDistance
= (info
->size
*i
)-(info
->size
*(info
->chair
.slots
-1)/2.0f
);
904 float x_i
= GetPositionX() + relativeDistance
* cos(orthogonalOrientation
);
905 float y_i
= GetPositionY() + relativeDistance
* sin(orthogonalOrientation
);
907 // calculate the distance between the player and this slot
908 float thisDistance
= player
->GetDistance2d(x_i
, y_i
);
910 /* debug code. It will spawn a npc on each slot to visualize them.
911 Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
912 std::ostringstream output;
913 output << i << ": thisDist: " << thisDistance;
914 helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
917 if(thisDistance
<= lowestDist
)
919 lowestDist
= thisDistance
;
924 player
->TeleportTo(GetMapId(), x_lowest
, y_lowest
, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
928 // fallback, will always work
929 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
931 player
->SetStandState(PLAYER_STATE_SIT_LOW_CHAIR
+info
->chair
.height
);
934 //big gun, its a spell/aura
935 case GAMEOBJECT_TYPE_GOOBER
: //10
937 GameObjectInfo
const* info
= GetGOInfo();
939 if(user
->GetTypeId()==TYPEID_PLAYER
)
941 Player
* player
= (Player
*)user
;
944 if(info
->goober
.pageId
)
946 WorldPacket
data(SMSG_GAMEOBJECT_PAGETEXT
, 8);
948 player
->GetSession()->SendPacket(&data
);
951 // possible quest objective for active quests
952 player
->CastedCreatureOrGO(info
->id
, GetGUID(), 0);
955 // cast this spell later if provided
956 spellId
= info
->goober
.spellId
;
960 case GAMEOBJECT_TYPE_CAMERA
: //13
962 GameObjectInfo
const* info
= GetGOInfo();
966 if(user
->GetTypeId()!=TYPEID_PLAYER
)
969 Player
* player
= (Player
*)user
;
971 if(info
->camera
.cinematicId
)
973 WorldPacket
data(SMSG_TRIGGER_CINEMATIC
, 4);
974 data
<< info
->camera
.cinematicId
;
975 player
->GetSession()->SendPacket(&data
);
980 case GAMEOBJECT_TYPE_FISHINGNODE
: //17
982 if(user
->GetTypeId()!=TYPEID_PLAYER
)
985 Player
* player
= (Player
*)user
;
987 if(player
->GetGUID() != GetOwnerGUID())
990 switch(getLootState())
992 case GO_READY
: // ready for loot
994 // 1) skill must be >= base_zone_skill
995 // 2) if skill == base_zone_skill => 5% chance
996 // 3) chance is linear dependence from (base_zone_skill-skill)
998 uint32 subzone
= GetAreaId();
1000 int32 zone_skill
= objmgr
.GetFishingBaseSkillLevel( subzone
);
1002 zone_skill
= objmgr
.GetFishingBaseSkillLevel( GetZoneId() );
1004 //provide error, no fishable zone or area should be 0
1006 sLog
.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone
);
1008 int32 skill
= player
->GetSkillValue(SKILL_FISHING
);
1009 int32 chance
= skill
- zone_skill
+ 5;
1010 int32 roll
= irand(1,100);
1012 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill
,zone_skill
,chance
,roll
);
1014 if(skill
>= zone_skill
&& chance
>= roll
)
1016 // prevent removing GO at spell cancel
1017 player
->RemoveGameObject(this,false);
1018 SetOwnerGUID(player
->GetGUID());
1021 player
->UpdateFishingSkill();
1023 GameObject
* ok
= LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE
);
1026 player
->SendLoot(ok
->GetGUID(),LOOT_FISHINGHOLE
);
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
;
1095 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->SendChannelUpdate(0);
1096 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->finish();
1098 // can be deleted now
1099 SetLootState(GO_JUST_DEACTIVATED
);
1101 // go to end function to spell casting
1104 case GAMEOBJECT_TYPE_SPELLCASTER
: //22
1106 SetUInt32Value(GAMEOBJECT_FLAGS
,2);
1108 GameObjectInfo
const* info
= GetGOInfo();
1112 if(info
->spellcaster
.partyOnly
)
1114 Unit
* caster
= GetOwner();
1115 if( !caster
|| caster
->GetTypeId()!=TYPEID_PLAYER
)
1118 if(user
->GetTypeId()!=TYPEID_PLAYER
|| !((Player
*)user
)->IsInSameRaidWith((Player
*)caster
))
1122 spellId
= info
->spellcaster
.spellId
;
1127 case GAMEOBJECT_TYPE_MEETINGSTONE
: //23
1129 GameObjectInfo
const* info
= GetGOInfo();
1131 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1134 Player
* player
= (Player
*)user
;
1136 Player
* targetPlayer
= ObjectAccessor::FindPlayer(player
->GetSelection());
1138 // accept only use by player from same group for caster except caster itself
1139 if(!targetPlayer
|| targetPlayer
== player
|| !targetPlayer
->IsInSameGroupWith(player
))
1142 //required lvl checks!
1143 uint8 level
= player
->getLevel();
1144 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1146 level
= targetPlayer
->getLevel();
1147 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1155 case GAMEOBJECT_TYPE_FLAGSTAND
: // 24
1157 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1160 Player
* player
= (Player
*)user
;
1162 if( player
->isAllowUseBattleGroundObject() )
1164 // in battleground check
1165 BattleGround
*bg
= player
->GetBattleGround();
1175 bg
->EventPlayerClickedOnFlag(player
, this);
1176 return; //we don;t need to delete flag ... it is despawned!
1180 case GAMEOBJECT_TYPE_FLAGDROP
: // 26
1182 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1185 Player
* player
= (Player
*)user
;
1187 if( player
->isAllowUseBattleGroundObject() )
1189 // in battleground check
1190 BattleGround
*bg
= player
->GetBattleGround();
1195 // 179785 - Silverwing Flag
1196 // 179786 - Warsong Flag
1198 // 184142 - Netherstorm Flag
1199 GameObjectInfo
const* info
= GetGOInfo();
1204 case 179785: // Silverwing Flag
1205 // check if it's correct bg
1206 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1207 bg
->EventPlayerClickedOnFlag(player
, this);
1209 case 179786: // Warsong Flag
1210 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1211 bg
->EventPlayerClickedOnFlag(player
, this);
1213 case 184142: // Netherstorm Flag
1214 if(bg
->GetTypeID() == BATTLEGROUND_EY
)
1215 bg
->EventPlayerClickedOnFlag(player
, this);
1219 //this cause to call return, all flags must be deleted here!!
1226 sLog
.outDebug("Unknown Object Type %u", GetGoType());
1233 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry( spellId
);
1236 sLog
.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId
,GetEntry(),GetGoType());
1240 Spell
*spell
= new Spell(spellCaster
, spellInfo
, false);
1242 // spell target is user of GO
1243 SpellCastTargets targets
;
1244 targets
.setUnitTarget( user
);
1246 spell
->prepare(&targets
);
1249 // overwrite WorldObject function for proper name localization
1250 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx
) const
1254 GameObjectLocale
const *cl
= objmgr
.GetGameObjectLocale(GetEntry());
1257 if (cl
->Name
.size() > loc_idx
&& !cl
->Name
[loc_idx
].empty())
1258 return cl
->Name
[loc_idx
].c_str();