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