[7690] Move GetCreature/GetGameobject to class Map.
[AHbot.git] / src / game / GameObject.cpp
blob7531c01b914d6f3800d82115697faa5ce80cd898
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, uint32 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(0);
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() )
250 SwitchDoorOrButton(false);
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)))
373 SwitchDoorOrButton(false);
374 SetLootState(GO_JUST_DEACTIVATED);
376 break;
378 break;
380 case GO_JUST_DEACTIVATED:
382 //if Gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
383 if (GetGoType() == GAMEOBJECT_TYPE_GOOBER)
385 uint32 spellId = GetGOInfo()->goober.spellId;
387 if(spellId)
389 std::set<uint32>::iterator it = m_unique_users.begin();
390 std::set<uint32>::iterator end = m_unique_users.end();
391 for (; it != end; it++)
393 Unit* owner = Unit::GetUnit(*this, uint64(*it));
394 if (owner) owner->CastSpell(owner, spellId, false, 0, 0, GetGUID());
397 m_unique_users.clear();
398 m_usetimes = 0;
400 //any return here in case battleground traps
403 if(GetOwnerGUID())
405 m_respawnTime = 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(1);
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 uint32 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::UseDoorOrButton(uint32 time_to_restore)
818 if(m_lootState != GO_READY)
819 return;
821 if(!time_to_restore)
822 time_to_restore = GetAutoCloseTime();
824 SwitchDoorOrButton(true);
825 SetLootState(GO_ACTIVATED);
827 m_cooldownTime = time(NULL) + time_to_restore;
831 void GameObject::SwitchDoorOrButton(bool activate)
833 if(activate)
834 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
835 else
836 RemoveFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
838 if(GetGoState()) //if closed -> open
839 SetGoState(0);
840 else //if open -> close
841 SetGoState(1);
844 void GameObject::Use(Unit* user)
846 // by default spell caster is user
847 Unit* spellCaster = user;
848 uint32 spellId = 0;
849 bool triggered = false;
851 switch(GetGoType())
853 case GAMEOBJECT_TYPE_DOOR: //0
854 case GAMEOBJECT_TYPE_BUTTON: //1
855 //doors/buttons never really despawn, only reset to default state/flags
856 UseDoorOrButton();
858 // activate script
859 sWorld.ScriptsStart(sGameObjectScripts, GetDBTableGUIDLow(), spellCaster, this);
860 return;
862 case GAMEOBJECT_TYPE_QUESTGIVER: //2
864 if(user->GetTypeId()!=TYPEID_PLAYER)
865 return;
867 Player* player = (Player*)user;
869 player->PrepareQuestMenu( GetGUID() );
870 player->SendPreparedQuest( GetGUID() );
871 return;
873 //Sitting: Wooden bench, chairs enzz
874 case GAMEOBJECT_TYPE_CHAIR: //7
876 GameObjectInfo const* info = GetGOInfo();
877 if(!info)
878 return;
880 if(user->GetTypeId()!=TYPEID_PLAYER)
881 return;
883 Player* player = (Player*)user;
885 // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
887 // check if the db is sane
888 if(info->chair.slots > 0)
890 float lowestDist = DEFAULT_VISIBILITY_DISTANCE;
892 float x_lowest = GetPositionX();
893 float y_lowest = GetPositionY();
895 // the object orientation + 1/2 pi
896 // every slot will be on that straight line
897 float orthogonalOrientation = GetOrientation()+M_PI*0.5f;
898 // find nearest slot
899 for(uint32 i=0; i<info->chair.slots; i++)
901 // the distance between this slot and the center of the go - imagine a 1D space
902 float relativeDistance = (info->size*i)-(info->size*(info->chair.slots-1)/2.0f);
904 float x_i = GetPositionX() + relativeDistance * cos(orthogonalOrientation);
905 float y_i = GetPositionY() + relativeDistance * sin(orthogonalOrientation);
907 // calculate the distance between the player and this slot
908 float thisDistance = player->GetDistance2d(x_i, y_i);
910 /* debug code. It will spawn a npc on each slot to visualize them.
911 Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
912 std::ostringstream output;
913 output << i << ": thisDist: " << thisDistance;
914 helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
917 if(thisDistance <= lowestDist)
919 lowestDist = thisDistance;
920 x_lowest = x_i;
921 y_lowest = y_i;
924 player->TeleportTo(GetMapId(), x_lowest, y_lowest, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
926 else
928 // fallback, will always work
929 player->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
931 player->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR+info->chair.height);
932 return;
934 //big gun, its a spell/aura
935 case GAMEOBJECT_TYPE_GOOBER: //10
937 GameObjectInfo const* info = GetGOInfo();
939 if(user->GetTypeId()==TYPEID_PLAYER)
941 Player* player = (Player*)user;
943 // show page
944 if(info->goober.pageId)
946 WorldPacket data(SMSG_GAMEOBJECT_PAGETEXT, 8);
947 data << GetGUID();
948 player->GetSession()->SendPacket(&data);
951 // possible quest objective for active quests
952 player->CastedCreatureOrGO(info->id, GetGUID(), 0);
955 // cast this spell later if provided
956 spellId = info->goober.spellId;
958 break;
960 case GAMEOBJECT_TYPE_CAMERA: //13
962 GameObjectInfo const* info = GetGOInfo();
963 if(!info)
964 return;
966 if(user->GetTypeId()!=TYPEID_PLAYER)
967 return;
969 Player* player = (Player*)user;
971 if (info->camera.cinematicId)
972 player->SendCinematicStart(info->camera.cinematicId);
974 return;
976 //fishing bobber
977 case GAMEOBJECT_TYPE_FISHINGNODE: //17
979 if(user->GetTypeId()!=TYPEID_PLAYER)
980 return;
982 Player* player = (Player*)user;
984 if(player->GetGUID() != GetOwnerGUID())
985 return;
987 switch(getLootState())
989 case GO_READY: // ready for loot
991 // 1) skill must be >= base_zone_skill
992 // 2) if skill == base_zone_skill => 5% chance
993 // 3) chance is linear dependence from (base_zone_skill-skill)
995 uint32 zone, subzone;
996 GetZoneAndAreaId(zone,subzone);
998 int32 zone_skill = objmgr.GetFishingBaseSkillLevel( subzone );
999 if(!zone_skill)
1000 zone_skill = objmgr.GetFishingBaseSkillLevel( zone );
1002 //provide error, no fishable zone or area should be 0
1003 if(!zone_skill)
1004 sLog.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone);
1006 int32 skill = player->GetSkillValue(SKILL_FISHING);
1007 int32 chance = skill - zone_skill + 5;
1008 int32 roll = irand(1,100);
1010 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill,zone_skill,chance,roll);
1012 if(skill >= zone_skill && chance >= roll)
1014 // prevent removing GO at spell cancel
1015 player->RemoveGameObject(this,false);
1016 SetOwnerGUID(player->GetGUID());
1018 //fish catched
1019 player->UpdateFishingSkill();
1021 GameObject* ok = LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE);
1022 if (ok)
1024 player->SendLoot(ok->GetGUID(),LOOT_FISHINGHOLE);
1025 player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_FISH_IN_GAMEOBJECT, ok->GetGOInfo()->id);
1026 SetLootState(GO_JUST_DEACTIVATED);
1028 else
1029 player->SendLoot(GetGUID(),LOOT_FISHING);
1031 else
1033 // fish escaped, can be deleted now
1034 SetLootState(GO_JUST_DEACTIVATED);
1036 WorldPacket data(SMSG_FISH_ESCAPED, 0);
1037 player->GetSession()->SendPacket(&data);
1039 break;
1041 case GO_JUST_DEACTIVATED: // nothing to do, will be deleted at next update
1042 break;
1043 default:
1045 SetLootState(GO_JUST_DEACTIVATED);
1047 WorldPacket data(SMSG_FISH_NOT_HOOKED, 0);
1048 player->GetSession()->SendPacket(&data);
1049 break;
1053 if(player->m_currentSpells[CURRENT_CHANNELED_SPELL])
1055 player->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
1056 player->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
1058 return;
1061 case GAMEOBJECT_TYPE_SUMMONING_RITUAL: //18
1063 if(user->GetTypeId()!=TYPEID_PLAYER)
1064 return;
1066 Player* player = (Player*)user;
1068 Unit* caster = GetOwner();
1070 GameObjectInfo const* info = GetGOInfo();
1072 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1073 return;
1075 // accept only use by player from same group for caster except caster itself
1076 if(((Player*)caster)==player || !((Player*)caster)->IsInSameRaidWith(player))
1077 return;
1079 AddUniqueUse(player);
1081 // full amount unique participants including original summoner
1082 if(GetUniqueUseCount() < info->summoningRitual.reqParticipants)
1083 return;
1085 // in case summoning ritual caster is GO creator
1086 spellCaster = caster;
1088 if(!caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
1089 return;
1091 spellId = info->summoningRitual.spellId;
1092 if(spellId==62330) // GO store not existed spell, replace by expected
1094 // spell have reagent and mana cost but it not expected use its
1095 // it triggered spell in fact casted at currently channeled GO
1096 spellId = 61993;
1097 triggered = true;
1100 // finish spell
1101 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
1102 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
1104 // can be deleted now
1105 SetLootState(GO_JUST_DEACTIVATED);
1107 // go to end function to spell casting
1108 break;
1110 case GAMEOBJECT_TYPE_SPELLCASTER: //22
1112 SetUInt32Value(GAMEOBJECT_FLAGS,2);
1114 GameObjectInfo const* info = GetGOInfo();
1115 if(!info)
1116 return;
1118 if(info->spellcaster.partyOnly)
1120 Unit* caster = GetOwner();
1121 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1122 return;
1124 if(user->GetTypeId()!=TYPEID_PLAYER || !((Player*)user)->IsInSameRaidWith((Player*)caster))
1125 return;
1128 spellId = info->spellcaster.spellId;
1130 AddUse();
1131 break;
1133 case GAMEOBJECT_TYPE_MEETINGSTONE: //23
1135 GameObjectInfo const* info = GetGOInfo();
1137 if(user->GetTypeId()!=TYPEID_PLAYER)
1138 return;
1140 Player* player = (Player*)user;
1142 Player* targetPlayer = ObjectAccessor::FindPlayer(player->GetSelection());
1144 // accept only use by player from same group for caster except caster itself
1145 if(!targetPlayer || targetPlayer == player || !targetPlayer->IsInSameGroupWith(player))
1146 return;
1148 //required lvl checks!
1149 uint8 level = player->getLevel();
1150 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1151 return;
1152 level = targetPlayer->getLevel();
1153 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1154 return;
1156 if(info->id==194097)
1157 spellId = 61994; // Ritual of Summoning
1158 else
1159 spellId = 59782; // Summoning Stone Effect
1161 break;
1164 case GAMEOBJECT_TYPE_FLAGSTAND: // 24
1166 if(user->GetTypeId()!=TYPEID_PLAYER)
1167 return;
1169 Player* player = (Player*)user;
1171 if( player->CanUseBattleGroundObject() )
1173 // in battleground check
1174 BattleGround *bg = player->GetBattleGround();
1175 if(!bg)
1176 return;
1177 // BG flag click
1178 // AB:
1179 // 15001
1180 // 15002
1181 // 15003
1182 // 15004
1183 // 15005
1184 bg->EventPlayerClickedOnFlag(player, this);
1185 return; //we don;t need to delete flag ... it is despawned!
1187 break;
1189 case GAMEOBJECT_TYPE_FLAGDROP: // 26
1191 if(user->GetTypeId()!=TYPEID_PLAYER)
1192 return;
1194 Player* player = (Player*)user;
1196 if( player->CanUseBattleGroundObject() )
1198 // in battleground check
1199 BattleGround *bg = player->GetBattleGround();
1200 if(!bg)
1201 return;
1202 // BG flag dropped
1203 // WS:
1204 // 179785 - Silverwing Flag
1205 // 179786 - Warsong Flag
1206 // EotS:
1207 // 184142 - Netherstorm Flag
1208 GameObjectInfo const* info = GetGOInfo();
1209 if(info)
1211 switch(info->id)
1213 case 179785: // Silverwing Flag
1214 // check if it's correct bg
1215 if(bg->GetTypeID() == BATTLEGROUND_WS)
1216 bg->EventPlayerClickedOnFlag(player, this);
1217 break;
1218 case 179786: // Warsong Flag
1219 if(bg->GetTypeID() == BATTLEGROUND_WS)
1220 bg->EventPlayerClickedOnFlag(player, this);
1221 break;
1222 case 184142: // Netherstorm Flag
1223 if(bg->GetTypeID() == BATTLEGROUND_EY)
1224 bg->EventPlayerClickedOnFlag(player, this);
1225 break;
1228 //this cause to call return, all flags must be deleted here!!
1229 spellId = 0;
1230 Delete();
1232 break;
1234 case GAMEOBJECT_TYPE_BARBER_CHAIR: //32
1236 GameObjectInfo const* info = GetGOInfo();
1237 if(!info)
1238 return;
1240 if(user->GetTypeId()!=TYPEID_PLAYER)
1241 return;
1243 Player* player = (Player*)user;
1245 // fallback, will always work
1246 player->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
1248 WorldPacket data(SMSG_ENABLE_BARBER_SHOP, 0);
1249 player->GetSession()->SendPacket(&data);
1251 player->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR+info->barberChair.chairheight);
1252 return;
1254 default:
1255 sLog.outDebug("Unknown Object Type %u", GetGoType());
1256 break;
1259 if(!spellId)
1260 return;
1262 SpellEntry const *spellInfo = sSpellStore.LookupEntry( spellId );
1263 if(!spellInfo)
1265 sLog.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId,GetEntry(),GetGoType());
1266 return;
1269 Spell *spell = new Spell(spellCaster, spellInfo, triggered);
1271 // spell target is user of GO
1272 SpellCastTargets targets;
1273 targets.setUnitTarget( user );
1275 spell->prepare(&targets);
1278 // overwrite WorldObject function for proper name localization
1279 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx) const
1281 if (loc_idx >= 0)
1283 GameObjectLocale const *cl = objmgr.GetGameObjectLocale(GetEntry());
1284 if (cl)
1286 if (cl->Name.size() > loc_idx && !cl->Name[loc_idx].empty())
1287 return cl->Name[loc_idx].c_str();
1291 return GetName();
1294 void GameObject::UpdateRotationFields(float rotation2 /*=0.0f*/, float rotation3 /*=0.0f*/)
1296 static double const atan_pow = atan(pow(2.0f, -20.0f));
1298 SetFloatValue(GAMEOBJECT_FACING, GetOrientation());
1300 double f_rot1 = sin(GetOrientation() / 2.0f);
1301 double f_rot2 = cos(GetOrientation() / 2.0f);
1303 int64 i_rot1 = int64(f_rot1 / atan_pow *(f_rot2 >= 0 ? 1.0f : -1.0f));
1304 int64 rotation = (i_rot1 << 43 >> 43) & 0x00000000001FFFFF;
1305 SetUInt64Value(GAMEOBJECT_ROTATION, rotation);
1307 if(rotation2==0.0f && rotation3==0.0f)
1309 rotation2 = f_rot1;
1310 rotation3 = f_rot2;
1313 SetFloatValue(GAMEOBJECT_PARENTROTATION+2, rotation2);
1314 SetFloatValue(GAMEOBJECT_PARENTROTATION+3, rotation3);