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_LOWGUID
| UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HAS_POSITION
);
46 m_valuesCount
= GAMEOBJECT_END
;
48 m_respawnDelayTime
= 25;
49 m_lootState
= GO_NOT_READY
;
50 m_spawnedByDefault
= true;
60 GameObject::~GameObject()
62 if(m_uint32Values
) // field array can be not exist if GameOBject not loaded
64 // Possible crash at access to deleted GO in Unit::m_gameobj
65 uint64 owner_guid
= GetOwnerGUID();
68 Unit
* owner
= ObjectAccessor::GetUnit(*this,owner_guid
);
70 owner
->RemoveGameObject(this,false);
71 else if(!IS_PLAYER_GUID(owner_guid
))
72 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
));
77 void GameObject::AddToWorld()
79 ///- Register the gameobject for guid lookup
80 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
84 void GameObject::RemoveFromWorld()
86 ///- Remove the gameobject from the accessor
87 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
88 Object::RemoveFromWorld();
91 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
)
94 SetMapId(map
->GetId());
95 SetInstanceId(map
->GetInstanceId());
96 SetPhaseMask(phaseMask
,false);
98 if(!IsPositionValid())
100 sLog
.outError("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
);
125 SetFloatValue(GAMEOBJECT_PARENTROTATION
+0, rotation0
);
126 SetFloatValue(GAMEOBJECT_PARENTROTATION
+1, rotation1
);
128 UpdateRotationFields(rotation2
,rotation3
); // GAMEOBJECT_FACING, GAMEOBJECT_ROTATION, GAMEOBJECT_PARENTROTATION+2/3
130 SetFloatValue(OBJECT_FIELD_SCALE_X
, goinfo
->size
);
132 SetUInt32Value(GAMEOBJECT_FACTION
, goinfo
->faction
);
133 SetUInt32Value(GAMEOBJECT_FLAGS
, goinfo
->flags
);
135 SetEntry(goinfo
->id
);
137 SetUInt32Value(GAMEOBJECT_DISPLAYID
, goinfo
->displayId
);
139 SetGoState(go_state
);
140 SetGoType(GameobjectTypes(goinfo
->type
));
142 SetGoAnimProgress(animprogress
);
144 // Spell charges for GAMEOBJECT_TYPE_SPELLCASTER (22)
145 if (goinfo
->type
== GAMEOBJECT_TYPE_SPELLCASTER
)
146 m_charges
= goinfo
->spellcaster
.charges
;
148 //Notify the map's instance data.
149 //Only works if you create the object in it, not if it is moves to that map.
150 //Normally non-players do not teleport to other maps.
151 if(map
->IsDungeon() && ((InstanceMap
*)map
)->GetInstanceData())
153 ((InstanceMap
*)map
)->GetInstanceData()->OnObjectCreate(this);
159 void GameObject::Update(uint32
/*p_time*/)
161 if (IS_MO_TRANSPORT(GetGUID()))
163 //((Transport*)this)->Update(p_time);
173 case GAMEOBJECT_TYPE_TRAP
:
175 // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
176 Unit
* owner
= GetOwner();
177 if (owner
&& ((Player
*)owner
)->isInCombat())
178 m_cooldownTime
= time(NULL
) + GetGOInfo()->trap
.startDelay
;
179 m_lootState
= GO_READY
;
182 case GAMEOBJECT_TYPE_FISHINGNODE
:
184 // fishing code (bobber ready)
185 if( time(NULL
) > m_respawnTime
- FISHING_BOBBER_READY_TIME
)
187 // splash bobber (bobber ready now)
188 Unit
* caster
= GetOwner();
189 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
191 SetGoState(GO_STATE_ACTIVE
);
192 SetUInt32Value(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
196 BuildValuesUpdateBlockForPlayer(&udata
,((Player
*)caster
));
197 udata
.BuildPacket(&packet
);
198 ((Player
*)caster
)->GetSession()->SendPacket(&packet
);
200 WorldPacket
data(SMSG_GAMEOBJECT_CUSTOM_ANIM
,8+4);
203 ((Player
*)caster
)->SendMessageToSet(&data
,true);
206 m_lootState
= GO_READY
; // can be successfully open with some chance
211 m_lootState
= GO_READY
; // for other GOis same switched without delay to GO_READY
214 // NO BREAK for switch (m_lootState)
218 if (m_respawnTime
> 0) // timer on
220 if (m_respawnTime
<= time(NULL
)) // timer expired
223 m_SkillupList
.clear();
228 case GAMEOBJECT_TYPE_FISHINGNODE
: // can't fish now
230 Unit
* caster
= GetOwner();
231 if(caster
&& caster
->GetTypeId()==TYPEID_PLAYER
)
233 if(caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
])
235 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->SendChannelUpdate(0);
236 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->finish(false);
239 WorldPacket
data(SMSG_FISH_NOT_HOOKED
,0);
240 ((Player
*)caster
)->GetSession()->SendPacket(&data
);
243 m_lootState
= GO_JUST_DEACTIVATED
;
246 case GAMEOBJECT_TYPE_DOOR
:
247 case GAMEOBJECT_TYPE_BUTTON
:
248 //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)
249 if (GetGoState() != GO_STATE_READY
)
251 //flags in AB are type_button and we need to add them here so no break!
253 if (!m_spawnedByDefault
) // despawn timer
255 // can be despawned or destroyed
256 SetLootState(GO_JUST_DEACTIVATED
);
260 uint16 poolid
= poolhandler
.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT
);
262 poolhandler
.UpdatePool(poolid
, GetGUIDLow(), TYPEID_GAMEOBJECT
);
270 // traps can have time and can not have
271 GameObjectInfo
const* goInfo
= GetGOInfo();
272 if(goInfo
->type
== GAMEOBJECT_TYPE_TRAP
)
275 Unit
* owner
= GetOwner();
276 Unit
* ok
= NULL
; // pointer to appropriate target if found any
278 if(m_cooldownTime
>= time(NULL
))
281 bool IsBattleGroundTrap
= false;
282 //FIXME: this is activation radius (in different casting radius that must be selected from spell data)
283 //TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
284 float radius
= goInfo
->trap
.radius
;
287 if(goInfo
->trap
.cooldown
!= 3) // cast in other case (at some triggering/linked go/etc explicit call)
291 if(m_respawnTime
> 0)
294 radius
= goInfo
->trap
.cooldown
; // battlegrounds gameobjects has data2 == 0 && data5 == 3
295 IsBattleGroundTrap
= true;
299 bool NeedDespawn
= (goInfo
->trap
.charges
!= 0);
301 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
303 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
305 // Note: this hack with search required until GO casting not implemented
306 // search unfriendly creature
307 if(owner
&& NeedDespawn
) // hunter trap
309 MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
u_check(this, owner
, radius
);
310 MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
> checker(this,ok
, u_check
);
312 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
314 TypeContainerVisitor
<MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_object_checker(checker
);
315 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap());
317 // or unfriendly player/pet
320 TypeContainerVisitor
<MaNGOS::UnitSearcher
<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_object_checker(checker
);
321 cell_lock
->Visit(cell_lock
, world_object_checker
, *GetMap());
324 else // environmental trap
326 // environmental damage spells already have around enemies targeting but this not help in case not existed GO casting support
328 // affect only players
330 MaNGOS::AnyPlayerInObjectRangeCheck
p_check(this, radius
);
331 MaNGOS::PlayerSearcher
<MaNGOS::AnyPlayerInObjectRangeCheck
> checker(this,p_ok
, p_check
);
333 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
335 TypeContainerVisitor
<MaNGOS::PlayerSearcher
<MaNGOS::AnyPlayerInObjectRangeCheck
>, WorldTypeMapContainer
> world_object_checker(checker
);
336 cell_lock
->Visit(cell_lock
, world_object_checker
, *GetMap());
342 Unit
*caster
= owner
? owner
: ok
;
344 caster
->CastSpell(ok
, goInfo
->trap
.spellId
, true, 0, 0, GetGUID());
345 m_cooldownTime
= time(NULL
) + 4; // 4 seconds
348 SetLootState(GO_JUST_DEACTIVATED
); // can be despawned or destroyed
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 (m_charges
&& m_usetimes
>= m_charges
)
361 SetLootState(GO_JUST_DEACTIVATED
); // can be despawned or destroyed
369 case GAMEOBJECT_TYPE_DOOR
:
370 case GAMEOBJECT_TYPE_BUTTON
:
371 if (GetAutoCloseTime() && (m_cooldownTime
< time(NULL
)))
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
>::const_iterator it
= m_unique_users
.begin();
387 std::set
<uint32
>::const_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, 0, 0, GetGUID());
394 m_unique_users
.clear();
397 //any return here in case battleground traps
402 if(Unit
* owner
= GetOwner())
403 owner
->RemoveGameObject(this, false);
410 //burning flags in some battlegrounds, if you find better condition, just add it
411 if (GetGoAnimProgress() > 0)
413 SendObjectDeSpawnAnim(GetGUID());
415 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
419 SetLootState(GO_READY
);
421 if(!m_respawnDelayTime
)
424 if(!m_spawnedByDefault
)
430 m_respawnTime
= time(NULL
) + m_respawnDelayTime
;
432 // if option not set then object will be saved at grid unload
433 if(sWorld
.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY
))
436 ObjectAccessor::UpdateObjectVisibility(this);
443 void GameObject::Refresh()
445 // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
446 if(m_respawnTime
> 0 && m_spawnedByDefault
)
453 void GameObject::AddUniqueUse(Player
* player
)
456 m_unique_users
.insert(player
->GetGUIDLow());
459 void GameObject::Delete()
461 SendObjectDeSpawnAnim(GetGUID());
463 SetGoState(GO_STATE_READY
);
464 SetUInt32Value(GAMEOBJECT_FLAGS
, GetGOInfo()->flags
);
466 uint16 poolid
= poolhandler
.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT
);
468 poolhandler
.UpdatePool(poolid
, GetGUIDLow(), TYPEID_GAMEOBJECT
);
470 AddObjectToRemoveList();
473 void GameObject::getFishLoot(Loot
*fishloot
, Player
* loot_owner
)
477 uint32 zone
, subzone
;
478 GetZoneAndAreaId(zone
,subzone
);
480 // if subzone loot exist use it
481 if(LootTemplates_Fishing
.HaveLootFor(subzone
))
482 fishloot
->FillLoot(subzone
, LootTemplates_Fishing
, loot_owner
,true);
483 // else use zone loot
485 fishloot
->FillLoot(zone
, LootTemplates_Fishing
, loot_owner
,true);
488 void GameObject::SaveToDB()
490 // this should only be used when the gameobject has already been loaded
491 // preferably after adding to map, because mapid may not be valid otherwise
492 GameObjectData
const *data
= objmgr
.GetGOData(m_DBTableGuid
);
495 sLog
.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
499 SaveToDB(GetMapId(), data
->spawnMask
, data
->phaseMask
);
502 void GameObject::SaveToDB(uint32 mapid
, uint8 spawnMask
, uint32 phaseMask
)
504 const GameObjectInfo
*goI
= GetGOInfo();
510 m_DBTableGuid
= GetGUIDLow();
511 // update in loaded data (changing data only in this place)
512 GameObjectData
& data
= objmgr
.NewGOData(m_DBTableGuid
);
514 // data->guid = guid don't must be update at save
515 data
.id
= GetEntry();
517 data
.phaseMask
= phaseMask
;
518 data
.posX
= GetFloatValue(GAMEOBJECT_POS_X
);
519 data
.posY
= GetFloatValue(GAMEOBJECT_POS_Y
);
520 data
.posZ
= GetFloatValue(GAMEOBJECT_POS_Z
);
521 data
.orientation
= GetFloatValue(GAMEOBJECT_FACING
);
522 data
.rotation0
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+0);
523 data
.rotation1
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+1);
524 data
.rotation2
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+2);
525 data
.rotation3
= GetFloatValue(GAMEOBJECT_PARENTROTATION
+3);
526 data
.spawntimesecs
= m_spawnedByDefault
? m_respawnDelayTime
: -(int32
)m_respawnDelayTime
;
527 data
.animprogress
= GetGoAnimProgress();
528 data
.go_state
= GetGoState();
529 data
.spawnMask
= spawnMask
;
532 std::ostringstream ss
;
533 ss
<< "INSERT INTO gameobject VALUES ( "
534 << m_DBTableGuid
<< ", "
535 << GetEntry() << ", "
537 << uint32(spawnMask
) << "," // cast to prevent save as symbol
538 << uint16(GetPhaseMask()) << "," // prevent out of range error
539 << GetFloatValue(GAMEOBJECT_POS_X
) << ", "
540 << GetFloatValue(GAMEOBJECT_POS_Y
) << ", "
541 << GetFloatValue(GAMEOBJECT_POS_Z
) << ", "
542 << GetFloatValue(GAMEOBJECT_FACING
) << ", "
543 << GetFloatValue(GAMEOBJECT_PARENTROTATION
) << ", "
544 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+1) << ", "
545 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+2) << ", "
546 << GetFloatValue(GAMEOBJECT_PARENTROTATION
+3) << ", "
547 << m_respawnDelayTime
<< ", "
548 << uint32(GetGoAnimProgress()) << ", "
549 << uint32(GetGoState()) << ")";
551 WorldDatabase
.BeginTransaction();
552 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
553 WorldDatabase
.PExecuteLog( ss
.str( ).c_str( ) );
554 WorldDatabase
.CommitTransaction();
557 bool GameObject::LoadFromDB(uint32 guid
, Map
*map
)
559 GameObjectData
const* data
= objmgr
.GetGOData(guid
);
563 sLog
.outErrorDb("Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid
);
567 uint32 entry
= data
->id
;
568 //uint32 map_id = data->mapid; // already used before call
569 uint32 phaseMask
= data
->phaseMask
;
570 float x
= data
->posX
;
571 float y
= data
->posY
;
572 float z
= data
->posZ
;
573 float ang
= data
->orientation
;
575 float rotation0
= data
->rotation0
;
576 float rotation1
= data
->rotation1
;
577 float rotation2
= data
->rotation2
;
578 float rotation3
= data
->rotation3
;
580 uint32 animprogress
= data
->animprogress
;
581 GOState go_state
= data
->go_state
;
583 m_DBTableGuid
= guid
;
584 if (map
->GetInstanceId() != 0) guid
= objmgr
.GenerateLowGuid(HIGHGUID_GAMEOBJECT
);
586 if (!Create(guid
,entry
, map
, phaseMask
, x
, y
, z
, ang
, rotation0
, rotation1
, rotation2
, rotation3
, animprogress
, go_state
) )
589 if(!GetDespawnPossibility())
591 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_NODESPAWN
);
592 m_spawnedByDefault
= true;
593 m_respawnDelayTime
= 0;
598 if(data
->spawntimesecs
>= 0)
600 m_spawnedByDefault
= true;
601 m_respawnDelayTime
= data
->spawntimesecs
;
602 m_respawnTime
= objmgr
.GetGORespawnTime(m_DBTableGuid
, map
->GetInstanceId());
605 if(m_respawnTime
&& m_respawnTime
<= time(NULL
))
608 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
613 m_spawnedByDefault
= false;
614 m_respawnDelayTime
= -data
->spawntimesecs
;
622 void GameObject::DeleteFromDB()
624 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
625 objmgr
.DeleteGOData(m_DBTableGuid
);
626 WorldDatabase
.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid
);
627 WorldDatabase
.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid
);
630 GameObjectInfo
const *GameObject::GetGOInfo() const
635 uint32
GameObject::GetLootId(GameObjectInfo
const* ginfo
)
642 case GAMEOBJECT_TYPE_CHEST
:
643 return ginfo
->chest
.lootId
;
644 case GAMEOBJECT_TYPE_FISHINGHOLE
:
645 return ginfo
->fishinghole
.lootId
;
646 case GAMEOBJECT_TYPE_FISHINGNODE
:
647 return ginfo
->fishnode
.lootId
;
653 /*********************************************************/
654 /*** QUEST SYSTEM ***/
655 /*********************************************************/
656 bool GameObject::hasQuest(uint32 quest_id
) const
658 QuestRelations
const& qr
= objmgr
.mGOQuestRelations
;
659 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
661 if(itr
->second
==quest_id
)
667 bool GameObject::hasInvolvedQuest(uint32 quest_id
) const
669 QuestRelations
const& qr
= objmgr
.mGOQuestInvolvedRelations
;
670 for(QuestRelations::const_iterator itr
= qr
.lower_bound(GetEntry()); itr
!= qr
.upper_bound(GetEntry()); ++itr
)
672 if(itr
->second
==quest_id
)
678 bool GameObject::IsTransport() const
680 // If something is marked as a transport, don't transmit an out of range packet for it.
681 GameObjectInfo
const * gInfo
= GetGOInfo();
682 if(!gInfo
) return false;
683 return gInfo
->type
== GAMEOBJECT_TYPE_TRANSPORT
|| gInfo
->type
== GAMEOBJECT_TYPE_MO_TRANSPORT
;
686 Unit
* GameObject::GetOwner() const
688 return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
691 void GameObject::SaveRespawnTime()
693 if(m_respawnTime
> time(NULL
) && m_spawnedByDefault
)
694 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),m_respawnTime
);
697 bool GameObject::isVisibleForInState(Player
const* u
, bool inVisibleList
) const
700 if(!IsInWorld() || !u
->IsInWorld())
703 // Transport always visible at this step implementation
704 if(IsTransport() && IsInMap(u
))
707 // quick check visibility false cases for non-GM-mode
708 if(!u
->isGameMaster())
710 // despawned and then not visible for non-GM in GM-mode
714 // special invisibility cases
715 /* TODO: implement trap stealth, take look at spell 2836
716 if(GetGOInfo()->type == GAMEOBJECT_TYPE_TRAP && GetGOInfo()->trap.stealthed && u->IsHostileTo(GetOwner()))
724 return IsWithinDistInMap(u
,World::GetMaxVisibleDistanceForObject() +
725 (inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);
728 void GameObject::Respawn()
730 if(m_spawnedByDefault
&& m_respawnTime
> 0)
732 m_respawnTime
= time(NULL
);
733 objmgr
.SaveGORespawnTime(m_DBTableGuid
,GetInstanceId(),0);
737 bool GameObject::ActivateToQuest( Player
*pTarget
)const
739 if(!objmgr
.IsGameObjectForQuests(GetEntry()))
744 // scan GO chest with loot including quest items
745 case GAMEOBJECT_TYPE_CHEST
:
747 if(LootTemplates_Gameobject
.HaveQuestLootForPlayer(GetLootId(), pTarget
))
751 case GAMEOBJECT_TYPE_GOOBER
:
753 if(pTarget
->GetQuestStatus(GetGOInfo()->goober
.questId
) == QUEST_STATUS_INCOMPLETE
)
764 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry
, Unit
* target
)
766 GameObjectInfo
const* trapInfo
= sGOStorage
.LookupEntry
<GameObjectInfo
>(trapEntry
);
767 if(!trapInfo
|| trapInfo
->type
!=GAMEOBJECT_TYPE_TRAP
)
770 SpellEntry
const* trapSpell
= sSpellStore
.LookupEntry(trapInfo
->trap
.spellId
);
771 if(!trapSpell
) // checked at load already
774 float range
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(trapSpell
->rangeIndex
));
776 // search nearest linked GO
777 GameObject
* trapGO
= NULL
;
779 // using original GO distance
780 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
782 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
784 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*target
,trapEntry
,range
);
785 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(this, trapGO
,go_check
);
787 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
788 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
789 cell_lock
->Visit(cell_lock
, object_checker
, *GetMap());
793 // FIXME: when GO casting will be implemented trap must cast spell to target
795 target
->CastSpell(target
,trapSpell
,true, 0, 0, GetGUID());
798 GameObject
* GameObject::LookupFishingHoleAround(float range
)
800 GameObject
* ok
= NULL
;
802 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
804 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
805 MaNGOS::NearestGameObjectFishingHole
u_check(*this, range
);
806 MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
> checker(this, ok
, u_check
);
808 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
810 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::NearestGameObjectFishingHole
>, GridTypeMapContainer
> grid_object_checker(checker
);
811 cell_lock
->Visit(cell_lock
, grid_object_checker
, *GetMap());
816 void GameObject::ResetDoorOrButton()
818 if (m_lootState
== GO_READY
|| m_lootState
== GO_JUST_DEACTIVATED
)
821 SwitchDoorOrButton(false);
822 SetLootState(GO_JUST_DEACTIVATED
);
826 void GameObject::UseDoorOrButton(uint32 time_to_restore
, bool alternative
/* = false */)
828 if(m_lootState
!= GO_READY
)
832 time_to_restore
= GetAutoCloseTime();
834 SwitchDoorOrButton(true,alternative
);
835 SetLootState(GO_ACTIVATED
);
837 m_cooldownTime
= time(NULL
) + time_to_restore
;
840 void GameObject::SwitchDoorOrButton(bool activate
, bool alternative
/* = false */)
843 SetFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
845 RemoveFlag(GAMEOBJECT_FLAGS
, GO_FLAG_IN_USE
);
847 if(GetGoState() == GO_STATE_READY
) //if closed -> open
848 SetGoState(alternative
? GO_STATE_ACTIVE_ALTERNATIVE
: GO_STATE_ACTIVE
);
849 else //if open -> close
850 SetGoState(GO_STATE_READY
);
853 void GameObject::Use(Unit
* user
)
855 // by default spell caster is user
856 Unit
* spellCaster
= user
;
858 bool triggered
= false;
862 case GAMEOBJECT_TYPE_DOOR
: //0
863 case GAMEOBJECT_TYPE_BUTTON
: //1
864 //doors/buttons never really despawn, only reset to default state/flags
868 sWorld
.ScriptsStart(sGameObjectScripts
, GetDBTableGUIDLow(), spellCaster
, this);
871 case GAMEOBJECT_TYPE_QUESTGIVER
: //2
873 if(user
->GetTypeId()!=TYPEID_PLAYER
)
876 Player
* player
= (Player
*)user
;
878 player
->PrepareQuestMenu( GetGUID() );
879 player
->SendPreparedQuest( GetGUID() );
882 //Sitting: Wooden bench, chairs enzz
883 case GAMEOBJECT_TYPE_CHAIR
: //7
885 GameObjectInfo
const* info
= GetGOInfo();
889 if(user
->GetTypeId()!=TYPEID_PLAYER
)
892 Player
* player
= (Player
*)user
;
894 // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
896 // check if the db is sane
897 if(info
->chair
.slots
> 0)
899 float lowestDist
= DEFAULT_VISIBILITY_DISTANCE
;
901 float x_lowest
= GetPositionX();
902 float y_lowest
= GetPositionY();
904 // the object orientation + 1/2 pi
905 // every slot will be on that straight line
906 float orthogonalOrientation
= GetOrientation()+M_PI
*0.5f
;
908 for(uint32 i
=0; i
<info
->chair
.slots
; ++i
)
910 // the distance between this slot and the center of the go - imagine a 1D space
911 float relativeDistance
= (info
->size
*i
)-(info
->size
*(info
->chair
.slots
-1)/2.0f
);
913 float x_i
= GetPositionX() + relativeDistance
* cos(orthogonalOrientation
);
914 float y_i
= GetPositionY() + relativeDistance
* sin(orthogonalOrientation
);
916 // calculate the distance between the player and this slot
917 float thisDistance
= player
->GetDistance2d(x_i
, y_i
);
919 /* debug code. It will spawn a npc on each slot to visualize them.
920 Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
921 std::ostringstream output;
922 output << i << ": thisDist: " << thisDistance;
923 helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
926 if(thisDistance
<= lowestDist
)
928 lowestDist
= thisDistance
;
933 player
->TeleportTo(GetMapId(), x_lowest
, y_lowest
, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
937 // fallback, will always work
938 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
940 player
->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR
+info
->chair
.height
);
943 //big gun, its a spell/aura
944 case GAMEOBJECT_TYPE_GOOBER
: //10
946 GameObjectInfo
const* info
= GetGOInfo();
948 if(user
->GetTypeId()==TYPEID_PLAYER
)
950 Player
* player
= (Player
*)user
;
953 if(info
->goober
.pageId
)
955 WorldPacket
data(SMSG_GAMEOBJECT_PAGETEXT
, 8);
957 player
->GetSession()->SendPacket(&data
);
960 // possible quest objective for active quests
961 player
->CastedCreatureOrGO(info
->id
, GetGUID(), 0);
963 if (info
->goober
.eventId
)
964 sWorld
.ScriptsStart(sEventScripts
, info
->goober
.eventId
, player
, this);
967 // cast this spell later if provided
968 spellId
= info
->goober
.spellId
;
972 case GAMEOBJECT_TYPE_CAMERA
: //13
974 GameObjectInfo
const* info
= GetGOInfo();
978 if(user
->GetTypeId()!=TYPEID_PLAYER
)
981 Player
* player
= (Player
*)user
;
983 if (info
->camera
.cinematicId
)
984 player
->SendCinematicStart(info
->camera
.cinematicId
);
986 if (info
->camera
.eventID
)
987 sWorld
.ScriptsStart(sEventScripts
, info
->camera
.eventID
, player
, this);
992 case GAMEOBJECT_TYPE_FISHINGNODE
: //17
994 if(user
->GetTypeId()!=TYPEID_PLAYER
)
997 Player
* player
= (Player
*)user
;
999 if(player
->GetGUID() != GetOwnerGUID())
1002 switch(getLootState())
1004 case GO_READY
: // ready for loot
1006 // 1) skill must be >= base_zone_skill
1007 // 2) if skill == base_zone_skill => 5% chance
1008 // 3) chance is linear dependence from (base_zone_skill-skill)
1010 uint32 zone
, subzone
;
1011 GetZoneAndAreaId(zone
,subzone
);
1013 int32 zone_skill
= objmgr
.GetFishingBaseSkillLevel( subzone
);
1015 zone_skill
= objmgr
.GetFishingBaseSkillLevel( zone
);
1017 //provide error, no fishable zone or area should be 0
1019 sLog
.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone
);
1021 int32 skill
= player
->GetSkillValue(SKILL_FISHING
);
1022 int32 chance
= skill
- zone_skill
+ 5;
1023 int32 roll
= irand(1,100);
1025 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill
,zone_skill
,chance
,roll
);
1027 if(skill
>= zone_skill
&& chance
>= roll
)
1029 // prevent removing GO at spell cancel
1030 player
->RemoveGameObject(this,false);
1031 SetOwnerGUID(player
->GetGUID());
1034 player
->UpdateFishingSkill();
1036 GameObject
* ok
= LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE
);
1039 player
->SendLoot(ok
->GetGUID(),LOOT_FISHINGHOLE
);
1040 player
->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_FISH_IN_GAMEOBJECT
, ok
->GetGOInfo()->id
);
1041 SetLootState(GO_JUST_DEACTIVATED
);
1044 player
->SendLoot(GetGUID(),LOOT_FISHING
);
1048 // fish escaped, can be deleted now
1049 SetLootState(GO_JUST_DEACTIVATED
);
1051 WorldPacket
data(SMSG_FISH_ESCAPED
, 0);
1052 player
->GetSession()->SendPacket(&data
);
1056 case GO_JUST_DEACTIVATED
: // nothing to do, will be deleted at next update
1060 SetLootState(GO_JUST_DEACTIVATED
);
1062 WorldPacket
data(SMSG_FISH_NOT_HOOKED
, 0);
1063 player
->GetSession()->SendPacket(&data
);
1068 if(player
->m_currentSpells
[CURRENT_CHANNELED_SPELL
])
1070 player
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->SendChannelUpdate(0);
1071 player
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->finish();
1076 case GAMEOBJECT_TYPE_SUMMONING_RITUAL
: //18
1078 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1081 Player
* player
= (Player
*)user
;
1083 Unit
* caster
= GetOwner();
1085 GameObjectInfo
const* info
= GetGOInfo();
1087 if( !caster
|| caster
->GetTypeId()!=TYPEID_PLAYER
)
1090 // accept only use by player from same group for caster except caster itself
1091 if(((Player
*)caster
)==player
|| !((Player
*)caster
)->IsInSameRaidWith(player
))
1094 AddUniqueUse(player
);
1096 // full amount unique participants including original summoner
1097 if(GetUniqueUseCount() < info
->summoningRitual
.reqParticipants
)
1100 // in case summoning ritual caster is GO creator
1101 spellCaster
= caster
;
1103 if(!caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
])
1106 spellId
= info
->summoningRitual
.spellId
;
1107 if(spellId
==62330) // GO store not existed spell, replace by expected
1109 // spell have reagent and mana cost but it not expected use its
1110 // it triggered spell in fact casted at currently channeled GO
1116 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->SendChannelUpdate(0);
1117 caster
->m_currentSpells
[CURRENT_CHANNELED_SPELL
]->finish();
1119 // can be deleted now
1120 SetLootState(GO_JUST_DEACTIVATED
);
1122 // go to end function to spell casting
1125 case GAMEOBJECT_TYPE_SPELLCASTER
: //22
1127 SetUInt32Value(GAMEOBJECT_FLAGS
,2);
1129 GameObjectInfo
const* info
= GetGOInfo();
1133 if(info
->spellcaster
.partyOnly
)
1135 Unit
* caster
= GetOwner();
1136 if( !caster
|| caster
->GetTypeId()!=TYPEID_PLAYER
)
1139 if(user
->GetTypeId()!=TYPEID_PLAYER
|| !((Player
*)user
)->IsInSameRaidWith((Player
*)caster
))
1143 spellId
= info
->spellcaster
.spellId
;
1148 case GAMEOBJECT_TYPE_MEETINGSTONE
: //23
1150 GameObjectInfo
const* info
= GetGOInfo();
1152 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1155 Player
* player
= (Player
*)user
;
1157 Player
* targetPlayer
= ObjectAccessor::FindPlayer(player
->GetSelection());
1159 // accept only use by player from same group for caster except caster itself
1160 if(!targetPlayer
|| targetPlayer
== player
|| !targetPlayer
->IsInSameGroupWith(player
))
1163 //required lvl checks!
1164 uint8 level
= player
->getLevel();
1165 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1167 level
= targetPlayer
->getLevel();
1168 if (level
< info
->meetingstone
.minLevel
|| level
> info
->meetingstone
.maxLevel
)
1171 if(info
->id
==194097)
1172 spellId
= 61994; // Ritual of Summoning
1174 spellId
= 59782; // Summoning Stone Effect
1179 case GAMEOBJECT_TYPE_FLAGSTAND
: // 24
1181 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1184 Player
* player
= (Player
*)user
;
1186 if( player
->CanUseBattleGroundObject() )
1188 // in battleground check
1189 BattleGround
*bg
= player
->GetBattleGround();
1199 bg
->EventPlayerClickedOnFlag(player
, this);
1200 return; //we don;t need to delete flag ... it is despawned!
1204 case GAMEOBJECT_TYPE_FLAGDROP
: // 26
1206 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1209 Player
* player
= (Player
*)user
;
1211 if( player
->CanUseBattleGroundObject() )
1213 // in battleground check
1214 BattleGround
*bg
= player
->GetBattleGround();
1219 // 179785 - Silverwing Flag
1220 // 179786 - Warsong Flag
1222 // 184142 - Netherstorm Flag
1223 GameObjectInfo
const* info
= GetGOInfo();
1228 case 179785: // Silverwing Flag
1229 // check if it's correct bg
1230 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1231 bg
->EventPlayerClickedOnFlag(player
, this);
1233 case 179786: // Warsong Flag
1234 if(bg
->GetTypeID() == BATTLEGROUND_WS
)
1235 bg
->EventPlayerClickedOnFlag(player
, this);
1237 case 184142: // Netherstorm Flag
1238 if(bg
->GetTypeID() == BATTLEGROUND_EY
)
1239 bg
->EventPlayerClickedOnFlag(player
, this);
1243 //this cause to call return, all flags must be deleted here!!
1249 case GAMEOBJECT_TYPE_BARBER_CHAIR
: //32
1251 GameObjectInfo
const* info
= GetGOInfo();
1255 if(user
->GetTypeId()!=TYPEID_PLAYER
)
1258 Player
* player
= (Player
*)user
;
1260 // fallback, will always work
1261 player
->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT
| TELE_TO_NOT_LEAVE_COMBAT
| TELE_TO_NOT_UNSUMMON_PET
);
1263 WorldPacket
data(SMSG_ENABLE_BARBER_SHOP
, 0);
1264 player
->GetSession()->SendPacket(&data
);
1266 player
->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR
+info
->barberChair
.chairheight
);
1270 sLog
.outDebug("Unknown Object Type %u", GetGoType());
1277 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry( spellId
);
1280 sLog
.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId
,GetEntry(),GetGoType());
1284 Spell
*spell
= new Spell(spellCaster
, spellInfo
, triggered
);
1286 // spell target is user of GO
1287 SpellCastTargets targets
;
1288 targets
.setUnitTarget( user
);
1290 spell
->prepare(&targets
);
1293 // overwrite WorldObject function for proper name localization
1294 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx
) const
1298 GameObjectLocale
const *cl
= objmgr
.GetGameObjectLocale(GetEntry());
1301 if (cl
->Name
.size() > loc_idx
&& !cl
->Name
[loc_idx
].empty())
1302 return cl
->Name
[loc_idx
].c_str();
1309 void GameObject::UpdateRotationFields(float rotation2
/*=0.0f*/, float rotation3
/*=0.0f*/)
1311 static double const atan_pow
= atan(pow(2.0f
, -20.0f
));
1313 SetFloatValue(GAMEOBJECT_FACING
, GetOrientation());
1315 double f_rot1
= sin(GetOrientation() / 2.0f
);
1316 double f_rot2
= cos(GetOrientation() / 2.0f
);
1318 int64 i_rot1
= int64(f_rot1
/ atan_pow
*(f_rot2
>= 0 ? 1.0f
: -1.0f
));
1319 int64 rotation
= (i_rot1
<< 43 >> 43) & 0x00000000001FFFFF;
1320 SetUInt64Value(GAMEOBJECT_ROTATION
, rotation
);
1322 if(rotation2
==0.0f
&& rotation3
==0.0f
)
1328 SetFloatValue(GAMEOBJECT_PARENTROTATION
+2, rotation2
);
1329 SetFloatValue(GAMEOBJECT_PARENTROTATION
+3, rotation3
);