[7986] MaNGOS 0.13 release.
[getmangos.git] / src / game / GameObject.cpp
blob98be0da03a1a9801a2e66ae189db21db8e35dc15
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;
43 // 2.3.2 - 0x58
44 m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION);
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;
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();
66 if(owner_guid)
68 Unit* owner = ObjectAccessor::GetUnit(*this,owner_guid);
69 if(owner)
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);
81 Object::AddToWorld();
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)
93 Relocate(x,y,z,ang);
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);
101 return false;
104 GameObjectInfo const* goinfo = objmgr.GetGameObjectInfo(name_id);
105 if (!goinfo)
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);
108 return false;
111 Object::_Create(guidlow, goinfo->id, HIGHGUID_GAMEOBJECT);
113 m_goInfo = goinfo;
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);
118 return false;
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);
156 return true;
159 void GameObject::Update(uint32 /*p_time*/)
161 if (IS_MO_TRANSPORT(GetGUID()))
163 //((Transport*)this)->Update(p_time);
164 return;
167 switch (m_lootState)
169 case GO_NOT_READY:
171 switch(GetGoType())
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;
180 break;
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);
194 UpdateData udata;
195 WorldPacket packet;
196 BuildValuesUpdateBlockForPlayer(&udata,((Player*)caster));
197 udata.BuildPacket(&packet);
198 ((Player*)caster)->GetSession()->SendPacket(&packet);
200 WorldPacket data(SMSG_GAMEOBJECT_CUSTOM_ANIM,8+4);
201 data << GetGUID();
202 data << (uint32)(0);
203 ((Player*)caster)->SendMessageToSet(&data,true);
206 m_lootState = GO_READY; // can be successfully open with some chance
208 return;
210 default:
211 m_lootState = GO_READY; // for other GOis same switched without delay to GO_READY
212 break;
214 // NO BREAK for switch (m_lootState)
216 case GO_READY:
218 if (m_respawnTime > 0) // timer on
220 if (m_respawnTime <= time(NULL)) // timer expired
222 m_respawnTime = 0;
223 m_SkillupList.clear();
224 m_usetimes = 0;
226 switch (GetGoType())
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);
242 // can be delete
243 m_lootState = GO_JUST_DEACTIVATED;
244 return;
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)
250 ResetDoorOrButton();
251 //flags in AB are type_button and we need to add them here so no break!
252 default:
253 if (!m_spawnedByDefault) // despawn timer
255 // can be despawned or destroyed
256 SetLootState(GO_JUST_DEACTIVATED);
257 return;
259 // respawn timer
260 uint16 poolid = poolhandler.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT);
261 if (poolid)
262 poolhandler.UpdatePool(poolid, GetGUIDLow(), TYPEID_GAMEOBJECT);
263 else
264 GetMap()->Add(this);
265 break;
270 // traps can have time and can not have
271 GameObjectInfo const* goInfo = GetGOInfo();
272 if(goInfo->type == GAMEOBJECT_TYPE_TRAP)
274 // traps
275 Unit* owner = GetOwner();
276 Unit* ok = NULL; // pointer to appropriate target if found any
278 if(m_cooldownTime >= time(NULL))
279 return;
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;
285 if(!radius)
287 if(goInfo->trap.cooldown != 3) // cast in other case (at some triggering/linked go/etc explicit call)
288 return;
289 else
291 if(m_respawnTime > 0)
292 break;
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()));
302 Cell cell(p);
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
318 if(!ok)
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
329 Player* p_ok = NULL;
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());
337 ok = p_ok;
340 if (ok)
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
347 if(NeedDespawn)
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
363 break;
365 case GO_ACTIVATED:
367 switch(GetGoType())
369 case GAMEOBJECT_TYPE_DOOR:
370 case GAMEOBJECT_TYPE_BUTTON:
371 if (GetAutoCloseTime() && (m_cooldownTime < time(NULL)))
372 ResetDoorOrButton();
373 break;
375 break;
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;
384 if(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();
395 m_usetimes = 0;
397 //any return here in case battleground traps
400 if(GetOwnerGUID())
402 if(Unit* owner = GetOwner())
403 owner->RemoveGameObject(this, false);
405 SetRespawnTime(0);
406 Delete();
407 return;
410 //burning flags in some battlegrounds, if you find better condition, just add it
411 if (GetGoAnimProgress() > 0)
413 SendObjectDeSpawnAnim(GetGUID());
414 //reset flags
415 SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
418 loot.clear();
419 SetLootState(GO_READY);
421 if(!m_respawnDelayTime)
422 return;
424 if(!m_spawnedByDefault)
426 m_respawnTime = 0;
427 return;
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))
434 SaveRespawnTime();
436 ObjectAccessor::UpdateObjectVisibility(this);
438 break;
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)
447 return;
449 if(isSpawned())
450 GetMap()->Add(this);
453 void GameObject::AddUniqueUse(Player* player)
455 AddUse();
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);
467 if (poolid)
468 poolhandler.UpdatePool(poolid, GetGUIDLow(), TYPEID_GAMEOBJECT);
469 else
470 AddObjectToRemoveList();
473 void GameObject::getFishLoot(Loot *fishloot, Player* loot_owner)
475 fishloot->clear();
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
484 else
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);
493 if(!data)
495 sLog.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
496 return;
499 SaveToDB(GetMapId(), data->spawnMask, data->phaseMask);
502 void GameObject::SaveToDB(uint32 mapid, uint8 spawnMask, uint32 phaseMask)
504 const GameObjectInfo *goI = GetGOInfo();
506 if (!goI)
507 return;
509 if (!m_DBTableGuid)
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();
516 data.mapid = mapid;
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;
531 // updated in DB
532 std::ostringstream ss;
533 ss << "INSERT INTO gameobject VALUES ( "
534 << m_DBTableGuid << ", "
535 << GetEntry() << ", "
536 << mapid << ", "
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);
561 if( !data )
563 sLog.outErrorDb("Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid);
564 return false;
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) )
587 return false;
589 if(!GetDespawnPossibility())
591 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
592 m_spawnedByDefault = true;
593 m_respawnDelayTime = 0;
594 m_respawnTime = 0;
596 else
598 if(data->spawntimesecs >= 0)
600 m_spawnedByDefault = true;
601 m_respawnDelayTime = data->spawntimesecs;
602 m_respawnTime = objmgr.GetGORespawnTime(m_DBTableGuid, map->GetInstanceId());
604 // ready to respawn
605 if(m_respawnTime && m_respawnTime <= time(NULL))
607 m_respawnTime = 0;
608 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
611 else
613 m_spawnedByDefault = false;
614 m_respawnDelayTime = -data->spawntimesecs;
615 m_respawnTime = 0;
619 return true;
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
632 return m_goInfo;
635 uint32 GameObject::GetLootId(GameObjectInfo const* ginfo)
637 if (!ginfo)
638 return 0;
640 switch(ginfo->type)
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;
648 default:
649 return 0;
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)
662 return true;
664 return false;
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)
673 return true;
675 return false;
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
699 // Not in world
700 if(!IsInWorld() || !u->IsInWorld())
701 return false;
703 // Transport always visible at this step implementation
704 if(IsTransport() && IsInMap(u))
705 return true;
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
711 if(!isSpawned())
712 return false;
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()))
718 if(check stuff here)
719 return false;
723 // check distance
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()))
740 return false;
742 switch(GetGoType())
744 // scan GO chest with loot including quest items
745 case GAMEOBJECT_TYPE_CHEST:
747 if(LootTemplates_Gameobject.HaveQuestLootForPlayer(GetLootId(), pTarget))
748 return true;
749 break;
751 case GAMEOBJECT_TYPE_GOOBER:
753 if(pTarget->GetQuestStatus(GetGOInfo()->goober.questId) == QUEST_STATUS_INCOMPLETE)
754 return true;
755 break;
757 default:
758 break;
761 return false;
764 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry, Unit* target)
766 GameObjectInfo const* trapInfo = sGOStorage.LookupEntry<GameObjectInfo>(trapEntry);
767 if(!trapInfo || trapInfo->type!=GAMEOBJECT_TYPE_TRAP)
768 return;
770 SpellEntry const* trapSpell = sSpellStore.LookupEntry(trapInfo->trap.spellId);
771 if(!trapSpell) // checked at load already
772 return;
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()));
781 Cell cell(p);
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());
792 // found correct GO
793 // FIXME: when GO casting will be implemented trap must cast spell to target
794 if(trapGO)
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()));
803 Cell cell(p);
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());
813 return ok;
816 void GameObject::ResetDoorOrButton()
818 if (m_lootState == GO_READY || m_lootState == GO_JUST_DEACTIVATED)
819 return;
821 SwitchDoorOrButton(false);
822 SetLootState(GO_JUST_DEACTIVATED);
823 m_cooldownTime = 0;
826 void GameObject::UseDoorOrButton(uint32 time_to_restore, bool alternative /* = false */)
828 if(m_lootState != GO_READY)
829 return;
831 if(!time_to_restore)
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 */)
842 if(activate)
843 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
844 else
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;
857 uint32 spellId = 0;
858 bool triggered = false;
860 switch(GetGoType())
862 case GAMEOBJECT_TYPE_DOOR: //0
863 case GAMEOBJECT_TYPE_BUTTON: //1
864 //doors/buttons never really despawn, only reset to default state/flags
865 UseDoorOrButton();
867 // activate script
868 sWorld.ScriptsStart(sGameObjectScripts, GetDBTableGUIDLow(), spellCaster, this);
869 return;
871 case GAMEOBJECT_TYPE_QUESTGIVER: //2
873 if(user->GetTypeId()!=TYPEID_PLAYER)
874 return;
876 Player* player = (Player*)user;
878 player->PrepareQuestMenu( GetGUID() );
879 player->SendPreparedQuest( GetGUID() );
880 return;
882 //Sitting: Wooden bench, chairs enzz
883 case GAMEOBJECT_TYPE_CHAIR: //7
885 GameObjectInfo const* info = GetGOInfo();
886 if(!info)
887 return;
889 if(user->GetTypeId()!=TYPEID_PLAYER)
890 return;
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;
907 // find nearest slot
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;
929 x_lowest = x_i;
930 y_lowest = y_i;
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);
935 else
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);
941 return;
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;
952 // show page
953 if(info->goober.pageId)
955 WorldPacket data(SMSG_GAMEOBJECT_PAGETEXT, 8);
956 data << GetGUID();
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;
970 break;
972 case GAMEOBJECT_TYPE_CAMERA: //13
974 GameObjectInfo const* info = GetGOInfo();
975 if(!info)
976 return;
978 if(user->GetTypeId()!=TYPEID_PLAYER)
979 return;
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);
989 return;
991 //fishing bobber
992 case GAMEOBJECT_TYPE_FISHINGNODE: //17
994 if(user->GetTypeId()!=TYPEID_PLAYER)
995 return;
997 Player* player = (Player*)user;
999 if(player->GetGUID() != GetOwnerGUID())
1000 return;
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 );
1014 if(!zone_skill)
1015 zone_skill = objmgr.GetFishingBaseSkillLevel( zone );
1017 //provide error, no fishable zone or area should be 0
1018 if(!zone_skill)
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());
1033 //fish catched
1034 player->UpdateFishingSkill();
1036 GameObject* ok = LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE);
1037 if (ok)
1039 player->SendLoot(ok->GetGUID(),LOOT_FISHINGHOLE);
1040 player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_FISH_IN_GAMEOBJECT, ok->GetGOInfo()->id);
1041 SetLootState(GO_JUST_DEACTIVATED);
1043 else
1044 player->SendLoot(GetGUID(),LOOT_FISHING);
1046 else
1048 // fish escaped, can be deleted now
1049 SetLootState(GO_JUST_DEACTIVATED);
1051 WorldPacket data(SMSG_FISH_ESCAPED, 0);
1052 player->GetSession()->SendPacket(&data);
1054 break;
1056 case GO_JUST_DEACTIVATED: // nothing to do, will be deleted at next update
1057 break;
1058 default:
1060 SetLootState(GO_JUST_DEACTIVATED);
1062 WorldPacket data(SMSG_FISH_NOT_HOOKED, 0);
1063 player->GetSession()->SendPacket(&data);
1064 break;
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();
1073 return;
1076 case GAMEOBJECT_TYPE_SUMMONING_RITUAL: //18
1078 if(user->GetTypeId()!=TYPEID_PLAYER)
1079 return;
1081 Player* player = (Player*)user;
1083 Unit* caster = GetOwner();
1085 GameObjectInfo const* info = GetGOInfo();
1087 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1088 return;
1090 // accept only use by player from same group for caster except caster itself
1091 if(((Player*)caster)==player || !((Player*)caster)->IsInSameRaidWith(player))
1092 return;
1094 AddUniqueUse(player);
1096 // full amount unique participants including original summoner
1097 if(GetUniqueUseCount() < info->summoningRitual.reqParticipants)
1098 return;
1100 // in case summoning ritual caster is GO creator
1101 spellCaster = caster;
1103 if(!caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
1104 return;
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
1111 spellId = 61993;
1112 triggered = true;
1115 // finish spell
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
1123 break;
1125 case GAMEOBJECT_TYPE_SPELLCASTER: //22
1127 SetUInt32Value(GAMEOBJECT_FLAGS,2);
1129 GameObjectInfo const* info = GetGOInfo();
1130 if(!info)
1131 return;
1133 if(info->spellcaster.partyOnly)
1135 Unit* caster = GetOwner();
1136 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1137 return;
1139 if(user->GetTypeId()!=TYPEID_PLAYER || !((Player*)user)->IsInSameRaidWith((Player*)caster))
1140 return;
1143 spellId = info->spellcaster.spellId;
1145 AddUse();
1146 break;
1148 case GAMEOBJECT_TYPE_MEETINGSTONE: //23
1150 GameObjectInfo const* info = GetGOInfo();
1152 if(user->GetTypeId()!=TYPEID_PLAYER)
1153 return;
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))
1161 return;
1163 //required lvl checks!
1164 uint8 level = player->getLevel();
1165 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1166 return;
1167 level = targetPlayer->getLevel();
1168 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1169 return;
1171 if(info->id==194097)
1172 spellId = 61994; // Ritual of Summoning
1173 else
1174 spellId = 59782; // Summoning Stone Effect
1176 break;
1179 case GAMEOBJECT_TYPE_FLAGSTAND: // 24
1181 if(user->GetTypeId()!=TYPEID_PLAYER)
1182 return;
1184 Player* player = (Player*)user;
1186 if( player->CanUseBattleGroundObject() )
1188 // in battleground check
1189 BattleGround *bg = player->GetBattleGround();
1190 if(!bg)
1191 return;
1192 // BG flag click
1193 // AB:
1194 // 15001
1195 // 15002
1196 // 15003
1197 // 15004
1198 // 15005
1199 bg->EventPlayerClickedOnFlag(player, this);
1200 return; //we don;t need to delete flag ... it is despawned!
1202 break;
1204 case GAMEOBJECT_TYPE_FLAGDROP: // 26
1206 if(user->GetTypeId()!=TYPEID_PLAYER)
1207 return;
1209 Player* player = (Player*)user;
1211 if( player->CanUseBattleGroundObject() )
1213 // in battleground check
1214 BattleGround *bg = player->GetBattleGround();
1215 if(!bg)
1216 return;
1217 // BG flag dropped
1218 // WS:
1219 // 179785 - Silverwing Flag
1220 // 179786 - Warsong Flag
1221 // EotS:
1222 // 184142 - Netherstorm Flag
1223 GameObjectInfo const* info = GetGOInfo();
1224 if(info)
1226 switch(info->id)
1228 case 179785: // Silverwing Flag
1229 // check if it's correct bg
1230 if(bg->GetTypeID() == BATTLEGROUND_WS)
1231 bg->EventPlayerClickedOnFlag(player, this);
1232 break;
1233 case 179786: // Warsong Flag
1234 if(bg->GetTypeID() == BATTLEGROUND_WS)
1235 bg->EventPlayerClickedOnFlag(player, this);
1236 break;
1237 case 184142: // Netherstorm Flag
1238 if(bg->GetTypeID() == BATTLEGROUND_EY)
1239 bg->EventPlayerClickedOnFlag(player, this);
1240 break;
1243 //this cause to call return, all flags must be deleted here!!
1244 spellId = 0;
1245 Delete();
1247 break;
1249 case GAMEOBJECT_TYPE_BARBER_CHAIR: //32
1251 GameObjectInfo const* info = GetGOInfo();
1252 if(!info)
1253 return;
1255 if(user->GetTypeId()!=TYPEID_PLAYER)
1256 return;
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);
1267 return;
1269 default:
1270 sLog.outDebug("Unknown Object Type %u", GetGoType());
1271 break;
1274 if(!spellId)
1275 return;
1277 SpellEntry const *spellInfo = sSpellStore.LookupEntry( spellId );
1278 if(!spellInfo)
1280 sLog.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId,GetEntry(),GetGoType());
1281 return;
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
1296 if (loc_idx >= 0)
1298 GameObjectLocale const *cl = objmgr.GetGameObjectLocale(GetEntry());
1299 if (cl)
1301 if (cl->Name.size() > loc_idx && !cl->Name[loc_idx].empty())
1302 return cl->Name[loc_idx].c_str();
1306 return GetName();
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)
1324 rotation2 = f_rot1;
1325 rotation3 = f_rot2;
1328 SetFloatValue(GAMEOBJECT_PARENTROTATION+2, rotation2);
1329 SetFloatValue(GAMEOBJECT_PARENTROTATION+3, rotation3);