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