[9033] Fixed percent mana regneration from spell 53228 and ranks buff.
[getmangos.git] / src / game / GameObject.cpp
blobfa5ada1aa13b2553284b3d5ec8b13924471c1b17
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 "PoolManager.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 "BattleGroundAV.h"
38 #include "Util.h"
40 GameObject::GameObject() : WorldObject()
42 m_objectType |= TYPEMASK_GAMEOBJECT;
43 m_objectTypeId = TYPEID_GAMEOBJECT;
45 m_updateFlag = (UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION | UPDATEFLAG_POSITION | UPDATEFLAG_ROTATION);
47 m_valuesCount = GAMEOBJECT_END;
48 m_respawnTime = 0;
49 m_respawnDelayTime = 25;
50 m_lootState = GO_NOT_READY;
51 m_spawnedByDefault = true;
52 m_usetimes = 0;
53 m_spellId = 0;
54 m_cooldownTime = 0;
55 m_goInfo = NULL;
57 m_DBTableGuid = 0;
58 m_rotation = 0;
61 GameObject::~GameObject()
65 void GameObject::AddToWorld()
67 ///- Register the gameobject for guid lookup
68 if(!IsInWorld())
69 GetMap()->GetObjectsStore().insert<GameObject>(GetGUID(), (GameObject*)this);
71 Object::AddToWorld();
74 void GameObject::RemoveFromWorld()
76 ///- Remove the gameobject from the accessor
77 if(IsInWorld())
79 // Remove GO from owner
80 if(uint64 owner_guid = GetOwnerGUID())
82 if (Unit* owner = ObjectAccessor::GetUnit(*this,owner_guid))
83 owner->RemoveGameObject(this,false);
84 else
86 const char * ownerType = "creature";
87 if(IS_PLAYER_GUID(owner_guid))
88 ownerType = "player";
89 else if(IS_PET_GUID(owner_guid))
90 ownerType = "pet";
92 sLog.outError("Delete GameObject (GUID: %u Entry: %u SpellId %u LinkedGO %u) that lost references to owner (GUID %u Type '%s') GO list. Crash possible later.",
93 GetGUIDLow(), GetGOInfo()->id, m_spellId, GetGOInfo()->GetLinkedGameObjectEntry(), GUID_LOPART(owner_guid), ownerType);
97 GetMap()->GetObjectsStore().erase<GameObject>(GetGUID(), (GameObject*)NULL);
100 Object::RemoveFromWorld();
103 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)
105 ASSERT(map);
106 Relocate(x,y,z,ang);
107 SetMap(map);
108 SetPhaseMask(phaseMask,false);
110 if(!IsPositionValid())
112 sLog.outError("Gameobject (GUID: %u Entry: %u ) not created. Suggested coordinates isn't valid (X: %f Y: %f)",guidlow,name_id,x,y);
113 return false;
116 GameObjectInfo const* goinfo = ObjectMgr::GetGameObjectInfo(name_id);
117 if (!goinfo)
119 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);
120 return false;
123 Object::_Create(guidlow, goinfo->id, HIGHGUID_GAMEOBJECT);
125 m_goInfo = goinfo;
127 if (goinfo->type >= MAX_GAMEOBJECT_TYPE)
129 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);
130 return false;
133 SetFloatValue(GAMEOBJECT_PARENTROTATION+0, rotation0);
134 SetFloatValue(GAMEOBJECT_PARENTROTATION+1, rotation1);
136 UpdateRotationFields(rotation2,rotation3); // GAMEOBJECT_FACING, GAMEOBJECT_ROTATION, GAMEOBJECT_PARENTROTATION+2/3
138 SetFloatValue(OBJECT_FIELD_SCALE_X, goinfo->size);
140 SetUInt32Value(GAMEOBJECT_FACTION, goinfo->faction);
141 SetUInt32Value(GAMEOBJECT_FLAGS, goinfo->flags);
143 SetEntry(goinfo->id);
145 SetUInt32Value(GAMEOBJECT_DISPLAYID, goinfo->displayId);
147 // GAMEOBJECT_BYTES_1, index at 0, 1, 2 and 3
148 SetGoState(go_state);
149 SetGoType(GameobjectTypes(goinfo->type));
150 SetGoArtKit(0); // unknown what this is
151 SetGoAnimProgress(animprogress);
153 //Notify the map's instance data.
154 //Only works if you create the object in it, not if it is moves to that map.
155 //Normally non-players do not teleport to other maps.
156 if(map->IsDungeon() && ((InstanceMap*)map)->GetInstanceData())
158 ((InstanceMap*)map)->GetInstanceData()->OnObjectCreate(this);
161 return true;
164 void GameObject::Update(uint32 /*p_time*/)
166 if (IS_MO_TRANSPORT(GetGUID()))
168 //((Transport*)this)->Update(p_time);
169 return;
172 switch (m_lootState)
174 case GO_NOT_READY:
176 switch(GetGoType())
178 case GAMEOBJECT_TYPE_TRAP:
180 // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
181 Unit* owner = GetOwner();
182 if (owner && ((Player*)owner)->isInCombat())
183 m_cooldownTime = time(NULL) + GetGOInfo()->trap.startDelay;
184 m_lootState = GO_READY;
185 break;
187 case GAMEOBJECT_TYPE_FISHINGNODE:
189 // fishing code (bobber ready)
190 if( time(NULL) > m_respawnTime - FISHING_BOBBER_READY_TIME )
192 // splash bobber (bobber ready now)
193 Unit* caster = GetOwner();
194 if(caster && caster->GetTypeId()==TYPEID_PLAYER)
196 SetGoState(GO_STATE_ACTIVE);
197 SetUInt32Value(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
199 UpdateData udata;
200 WorldPacket packet;
201 BuildValuesUpdateBlockForPlayer(&udata,((Player*)caster));
202 udata.BuildPacket(&packet);
203 ((Player*)caster)->GetSession()->SendPacket(&packet);
205 WorldPacket data(SMSG_GAMEOBJECT_CUSTOM_ANIM,8+4);
206 data << GetGUID();
207 data << (uint32)(0);
208 ((Player*)caster)->SendMessageToSet(&data,true);
211 m_lootState = GO_READY; // can be successfully open with some chance
213 return;
215 default:
216 m_lootState = GO_READY; // for other GOis same switched without delay to GO_READY
217 break;
219 // NO BREAK for switch (m_lootState)
221 case GO_READY:
223 if (m_respawnTime > 0) // timer on
225 if (m_respawnTime <= time(NULL)) // timer expired
227 m_respawnTime = 0;
228 m_SkillupList.clear();
229 m_usetimes = 0;
231 switch (GetGoType())
233 case GAMEOBJECT_TYPE_FISHINGNODE: // can't fish now
235 Unit* caster = GetOwner();
236 if(caster && caster->GetTypeId()==TYPEID_PLAYER)
238 caster->FinishSpell(CURRENT_CHANNELED_SPELL);
240 WorldPacket data(SMSG_FISH_NOT_HOOKED,0);
241 ((Player*)caster)->GetSession()->SendPacket(&data);
243 // can be delete
244 m_lootState = GO_JUST_DEACTIVATED;
245 return;
247 case GAMEOBJECT_TYPE_DOOR:
248 case GAMEOBJECT_TYPE_BUTTON:
249 //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)
250 if (GetGoState() != GO_STATE_READY)
251 ResetDoorOrButton();
252 //flags in AB are type_button and we need to add them here so no break!
253 default:
254 if (!m_spawnedByDefault) // despawn timer
256 // can be despawned or destroyed
257 SetLootState(GO_JUST_DEACTIVATED);
258 return;
260 // respawn timer
261 uint16 poolid = sPoolMgr.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT);
262 if (poolid)
263 sPoolMgr.UpdatePool(poolid, GetGUIDLow(), TYPEID_GAMEOBJECT);
264 else
265 GetMap()->Add(this);
266 break;
271 if(isSpawned())
273 // traps can have time and can not have
274 GameObjectInfo const* goInfo = GetGOInfo();
275 if(goInfo->type == GAMEOBJECT_TYPE_TRAP)
277 if(m_cooldownTime >= time(NULL))
278 return;
280 // traps
281 Unit* owner = GetOwner();
282 Unit* ok = NULL; // pointer to appropriate target if found any
284 bool IsBattleGroundTrap = false;
285 //FIXME: this is activation radius (in different casting radius that must be selected from spell data)
286 //TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
287 float radius = goInfo->trap.radius;
288 if(!radius)
290 if(goInfo->trap.cooldown != 3) // cast in other case (at some triggering/linked go/etc explicit call)
291 return;
292 else
294 if(m_respawnTime > 0)
295 break;
297 radius = goInfo->trap.cooldown; // battlegrounds gameobjects has data2 == 0 && data5 == 3
298 IsBattleGroundTrap = true;
302 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
303 Cell cell(p);
304 cell.data.Part.reserved = ALL_DISTRICT;
306 // Note: this hack with search required until GO casting not implemented
307 // search unfriendly creature
308 if(owner && goInfo->trap.charges > 0) // hunter trap
310 MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck u_check(this, owner, radius);
311 MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck> checker(this,ok, u_check);
313 CellLock<GridReadGuard> cell_lock(cell, p);
315 TypeContainerVisitor<MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck>, GridTypeMapContainer > grid_object_checker(checker);
316 cell_lock->Visit(cell_lock, grid_object_checker, *GetMap(), *this, radius);
318 // or unfriendly player/pet
319 if(!ok)
321 TypeContainerVisitor<MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
322 cell_lock->Visit(cell_lock, world_object_checker, *GetMap(), *this, radius);
325 else // environmental trap
327 // environmental damage spells already have around enemies targeting but this not help in case not existed GO casting support
329 // affect only players
330 Player* p_ok = NULL;
331 MaNGOS::AnyPlayerInObjectRangeCheck p_check(this, radius);
332 MaNGOS::PlayerSearcher<MaNGOS::AnyPlayerInObjectRangeCheck> checker(this,p_ok, p_check);
334 CellLock<GridReadGuard> cell_lock(cell, p);
336 TypeContainerVisitor<MaNGOS::PlayerSearcher<MaNGOS::AnyPlayerInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
337 cell_lock->Visit(cell_lock, world_object_checker, *GetMap(), *this, radius);
338 ok = p_ok;
341 if (ok)
343 Unit *caster = owner ? owner : ok;
345 caster->CastSpell(ok, goInfo->trap.spellId, true, 0, 0, GetGUID());
346 m_cooldownTime = time(NULL) + 4; // 4 seconds
348 // count charges
349 if(goInfo->trap.charges > 0)
350 AddUse();
352 if(IsBattleGroundTrap && ok->GetTypeId() == TYPEID_PLAYER)
354 //BattleGround gameobjects case
355 if(((Player*)ok)->InBattleGround())
356 if(BattleGround *bg = ((Player*)ok)->GetBattleGround())
357 bg->HandleTriggerBuff(GetGUID());
362 if(uint32 max_charges = goInfo->GetCharges())
364 if (m_usetimes >= max_charges)
366 m_usetimes = 0;
367 SetLootState(GO_JUST_DEACTIVATED); // can be despawned or destroyed
372 break;
374 case GO_ACTIVATED:
376 switch(GetGoType())
378 case GAMEOBJECT_TYPE_DOOR:
379 case GAMEOBJECT_TYPE_BUTTON:
380 if (GetGOInfo()->GetAutoCloseTime() && (m_cooldownTime < time(NULL)))
381 ResetDoorOrButton();
382 break;
383 default: break;
385 break;
387 case GO_JUST_DEACTIVATED:
389 //if Gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
390 if (GetGoType() == GAMEOBJECT_TYPE_GOOBER)
392 uint32 spellId = GetGOInfo()->goober.spellId;
394 if(spellId)
396 std::set<uint32>::const_iterator it = m_unique_users.begin();
397 std::set<uint32>::const_iterator end = m_unique_users.end();
398 for (; it != end; it++)
400 Unit* owner = Unit::GetUnit(*this, uint64(*it));
401 if (owner) owner->CastSpell(owner, spellId, false, 0, 0, GetGUID());
404 m_unique_users.clear();
405 m_usetimes = 0;
407 //any return here in case battleground traps
410 if(GetOwnerGUID())
412 if(Unit* owner = GetOwner())
413 owner->RemoveGameObject(this, false);
415 SetRespawnTime(0);
416 Delete();
417 return;
420 //burning flags in some battlegrounds, if you find better condition, just add it
421 if (GetGOInfo()->IsDespawnAtAction() || GetGoAnimProgress() > 0)
423 SendObjectDeSpawnAnim(GetGUID());
424 //reset flags
425 SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
428 loot.clear();
429 SetLootState(GO_READY);
431 if(!m_respawnDelayTime)
432 return;
434 if(!m_spawnedByDefault)
436 m_respawnTime = 0;
437 return;
440 m_respawnTime = time(NULL) + m_respawnDelayTime;
442 // if option not set then object will be saved at grid unload
443 if(sWorld.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY))
444 SaveRespawnTime();
446 UpdateObjectVisibility();
448 break;
453 void GameObject::Refresh()
455 // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
456 if(m_respawnTime > 0 && m_spawnedByDefault)
457 return;
459 if(isSpawned())
460 GetMap()->Add(this);
463 void GameObject::AddUniqueUse(Player* player)
465 AddUse();
466 m_unique_users.insert(player->GetGUIDLow());
469 void GameObject::Delete()
471 SendObjectDeSpawnAnim(GetGUID());
473 SetGoState(GO_STATE_READY);
474 SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
476 uint16 poolid = sPoolMgr.IsPartOfAPool(GetGUIDLow(), TYPEID_GAMEOBJECT);
477 if (poolid)
478 sPoolMgr.UpdatePool(poolid, GetGUIDLow(), TYPEID_GAMEOBJECT);
479 else
480 AddObjectToRemoveList();
483 void GameObject::getFishLoot(Loot *fishloot, Player* loot_owner)
485 fishloot->clear();
487 uint32 zone, subzone;
488 GetZoneAndAreaId(zone,subzone);
490 // if subzone loot exist use it
491 if (!fishloot->FillLoot(subzone, LootTemplates_Fishing, loot_owner, true, true))
492 // else use zone loot (must exist in like case)
493 fishloot->FillLoot(zone, LootTemplates_Fishing, loot_owner,true);
496 void GameObject::SaveToDB()
498 // this should only be used when the gameobject has already been loaded
499 // preferably after adding to map, because mapid may not be valid otherwise
500 GameObjectData const *data = sObjectMgr.GetGOData(m_DBTableGuid);
501 if(!data)
503 sLog.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
504 return;
507 SaveToDB(GetMapId(), data->spawnMask, data->phaseMask);
510 void GameObject::SaveToDB(uint32 mapid, uint8 spawnMask, uint32 phaseMask)
512 const GameObjectInfo *goI = GetGOInfo();
514 if (!goI)
515 return;
517 if (!m_DBTableGuid)
518 m_DBTableGuid = GetGUIDLow();
519 // update in loaded data (changing data only in this place)
520 GameObjectData& data = sObjectMgr.NewGOData(m_DBTableGuid);
522 // data->guid = guid don't must be update at save
523 data.id = GetEntry();
524 data.mapid = mapid;
525 data.phaseMask = phaseMask;
526 data.posX = GetPositionX();
527 data.posY = GetPositionY();
528 data.posZ = GetPositionZ();
529 data.orientation = GetOrientation();
530 data.rotation0 = GetFloatValue(GAMEOBJECT_PARENTROTATION+0);
531 data.rotation1 = GetFloatValue(GAMEOBJECT_PARENTROTATION+1);
532 data.rotation2 = GetFloatValue(GAMEOBJECT_PARENTROTATION+2);
533 data.rotation3 = GetFloatValue(GAMEOBJECT_PARENTROTATION+3);
534 data.spawntimesecs = m_spawnedByDefault ? m_respawnDelayTime : -(int32)m_respawnDelayTime;
535 data.animprogress = GetGoAnimProgress();
536 data.go_state = GetGoState();
537 data.spawnMask = spawnMask;
539 // updated in DB
540 std::ostringstream ss;
541 ss << "INSERT INTO gameobject VALUES ( "
542 << m_DBTableGuid << ", "
543 << GetEntry() << ", "
544 << mapid << ", "
545 << uint32(spawnMask) << "," // cast to prevent save as symbol
546 << uint16(GetPhaseMask()) << "," // prevent out of range error
547 << GetPositionX() << ", "
548 << GetPositionY() << ", "
549 << GetPositionZ() << ", "
550 << GetOrientation() << ", "
551 << GetFloatValue(GAMEOBJECT_PARENTROTATION) << ", "
552 << GetFloatValue(GAMEOBJECT_PARENTROTATION+1) << ", "
553 << GetFloatValue(GAMEOBJECT_PARENTROTATION+2) << ", "
554 << GetFloatValue(GAMEOBJECT_PARENTROTATION+3) << ", "
555 << m_respawnDelayTime << ", "
556 << uint32(GetGoAnimProgress()) << ", "
557 << uint32(GetGoState()) << ")";
559 WorldDatabase.BeginTransaction();
560 WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid);
561 WorldDatabase.PExecuteLog( ss.str( ).c_str( ) );
562 WorldDatabase.CommitTransaction();
565 bool GameObject::LoadFromDB(uint32 guid, Map *map)
567 GameObjectData const* data = sObjectMgr.GetGOData(guid);
569 if( !data )
571 sLog.outErrorDb("Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid);
572 return false;
575 uint32 entry = data->id;
576 //uint32 map_id = data->mapid; // already used before call
577 uint32 phaseMask = data->phaseMask;
578 float x = data->posX;
579 float y = data->posY;
580 float z = data->posZ;
581 float ang = data->orientation;
583 float rotation0 = data->rotation0;
584 float rotation1 = data->rotation1;
585 float rotation2 = data->rotation2;
586 float rotation3 = data->rotation3;
588 uint32 animprogress = data->animprogress;
589 GOState go_state = data->go_state;
591 m_DBTableGuid = guid;
592 if (map->GetInstanceId() != 0) guid = sObjectMgr.GenerateLowGuid(HIGHGUID_GAMEOBJECT);
594 if (!Create(guid,entry, map, phaseMask, x, y, z, ang, rotation0, rotation1, rotation2, rotation3, animprogress, go_state) )
595 return false;
597 if (!GetGOInfo()->GetDespawnPossibility() && !GetGOInfo()->IsDespawnAtAction() && data->spawntimesecs >= 0)
599 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
600 m_spawnedByDefault = true;
601 m_respawnDelayTime = 0;
602 m_respawnTime = 0;
604 else
606 if(data->spawntimesecs >= 0)
608 m_spawnedByDefault = true;
609 m_respawnDelayTime = data->spawntimesecs;
610 m_respawnTime = sObjectMgr.GetGORespawnTime(m_DBTableGuid, map->GetInstanceId());
612 // ready to respawn
613 if(m_respawnTime && m_respawnTime <= time(NULL))
615 m_respawnTime = 0;
616 sObjectMgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
619 else
621 m_spawnedByDefault = false;
622 m_respawnDelayTime = -data->spawntimesecs;
623 m_respawnTime = 0;
627 return true;
630 void GameObject::DeleteFromDB()
632 sObjectMgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
633 sObjectMgr.DeleteGOData(m_DBTableGuid);
634 WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid);
635 WorldDatabase.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid);
636 WorldDatabase.PExecuteLog("DELETE FROM gameobject_battleground WHERE guid = '%u'", m_DBTableGuid);
639 GameObjectInfo const *GameObject::GetGOInfo() const
641 return m_goInfo;
644 /*********************************************************/
645 /*** QUEST SYSTEM ***/
646 /*********************************************************/
647 bool GameObject::hasQuest(uint32 quest_id) const
649 QuestRelations const& qr = sObjectMgr.mGOQuestRelations;
650 for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr)
652 if(itr->second==quest_id)
653 return true;
655 return false;
658 bool GameObject::hasInvolvedQuest(uint32 quest_id) const
660 QuestRelations const& qr = sObjectMgr.mGOQuestInvolvedRelations;
661 for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr)
663 if(itr->second==quest_id)
664 return true;
666 return false;
669 bool GameObject::IsTransport() const
671 // If something is marked as a transport, don't transmit an out of range packet for it.
672 GameObjectInfo const * gInfo = GetGOInfo();
673 if(!gInfo) return false;
674 return gInfo->type == GAMEOBJECT_TYPE_TRANSPORT || gInfo->type == GAMEOBJECT_TYPE_MO_TRANSPORT;
677 Unit* GameObject::GetOwner() const
679 return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
682 void GameObject::SaveRespawnTime()
684 if(m_respawnTime > time(NULL) && m_spawnedByDefault)
685 sObjectMgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),m_respawnTime);
688 bool GameObject::isVisibleForInState(Player const* u, WorldObject const* viewPoint, bool inVisibleList) const
690 // Not in world
691 if(!IsInWorld() || !u->IsInWorld())
692 return false;
694 // invisible at client always
695 if(!GetGOInfo()->displayId)
696 return false;
698 // Transport always visible at this step implementation
699 if(IsTransport() && IsInMap(u))
700 return true;
702 // quick check visibility false cases for non-GM-mode
703 if(!u->isGameMaster())
705 // despawned and then not visible for non-GM in GM-mode
706 if(!isSpawned())
707 return false;
709 // special invisibility cases
710 /* TODO: implement trap stealth, take look at spell 2836
711 if(GetGOInfo()->type == GAMEOBJECT_TYPE_TRAP && GetGOInfo()->trap.stealthed && u->IsHostileTo(GetOwner()))
713 if(check stuff here)
714 return false;
718 // check distance
719 return IsWithinDistInMap(viewPoint,World::GetMaxVisibleDistanceForObject() +
720 (inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false);
723 void GameObject::Respawn()
725 if(m_spawnedByDefault && m_respawnTime > 0)
727 m_respawnTime = time(NULL);
728 sObjectMgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
732 bool GameObject::ActivateToQuest( Player *pTarget)const
734 if(!sObjectMgr.IsGameObjectForQuests(GetEntry()))
735 return false;
737 switch(GetGoType())
739 // scan GO chest with loot including quest items
740 case GAMEOBJECT_TYPE_CHEST:
742 if(LootTemplates_Gameobject.HaveQuestLootForPlayer(GetGOInfo()->GetLootId(), pTarget))
744 //look for battlegroundAV for some objects which are only activated after mine gots captured by own team
745 if (GetEntry() == BG_AV_OBJECTID_MINE_N || GetEntry() == BG_AV_OBJECTID_MINE_S)
746 if (BattleGround *bg = pTarget->GetBattleGround())
747 if (bg->GetTypeID() == BATTLEGROUND_AV && !(((BattleGroundAV*)bg)->PlayerCanDoMineQuest(GetEntry(),pTarget->GetTeam())))
748 return false;
749 return true;
751 break;
753 case GAMEOBJECT_TYPE_GOOBER:
755 if(pTarget->GetQuestStatus(GetGOInfo()->goober.questId) == QUEST_STATUS_INCOMPLETE)
756 return true;
757 break;
759 default:
760 break;
763 return false;
766 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry, Unit* target)
768 GameObjectInfo const* trapInfo = sGOStorage.LookupEntry<GameObjectInfo>(trapEntry);
769 if(!trapInfo || trapInfo->type!=GAMEOBJECT_TYPE_TRAP)
770 return;
772 SpellEntry const* trapSpell = sSpellStore.LookupEntry(trapInfo->trap.spellId);
773 if(!trapSpell) // checked at load already
774 return;
776 float range = GetSpellMaxRange(sSpellRangeStore.LookupEntry(trapSpell->rangeIndex));
778 // search nearest linked GO
779 GameObject* trapGO = NULL;
781 // using original GO distance
782 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
783 Cell cell(p);
784 cell.data.Part.reserved = ALL_DISTRICT;
786 MaNGOS::NearestGameObjectEntryInObjectRangeCheck go_check(*target,trapEntry,range);
787 MaNGOS::GameObjectLastSearcher<MaNGOS::NearestGameObjectEntryInObjectRangeCheck> checker(this, trapGO,go_check);
789 TypeContainerVisitor<MaNGOS::GameObjectLastSearcher<MaNGOS::NearestGameObjectEntryInObjectRangeCheck>, GridTypeMapContainer > object_checker(checker);
790 CellLock<GridReadGuard> cell_lock(cell, p);
791 cell_lock->Visit(cell_lock, object_checker, *GetMap(), *target, range);
794 // found correct GO
795 // FIXME: when GO casting will be implemented trap must cast spell to target
796 if(trapGO)
797 target->CastSpell(target,trapSpell,true, 0, 0, GetGUID());
800 GameObject* GameObject::LookupFishingHoleAround(float range)
802 GameObject* ok = NULL;
804 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
805 Cell cell(p);
806 cell.data.Part.reserved = ALL_DISTRICT;
807 MaNGOS::NearestGameObjectFishingHole u_check(*this, range);
808 MaNGOS::GameObjectSearcher<MaNGOS::NearestGameObjectFishingHole> checker(this, ok, u_check);
810 CellLock<GridReadGuard> cell_lock(cell, p);
812 TypeContainerVisitor<MaNGOS::GameObjectSearcher<MaNGOS::NearestGameObjectFishingHole>, GridTypeMapContainer > grid_object_checker(checker);
813 cell_lock->Visit(cell_lock, grid_object_checker, *GetMap(), *this, range);
815 return ok;
818 void GameObject::ResetDoorOrButton()
820 if (m_lootState == GO_READY || m_lootState == GO_JUST_DEACTIVATED)
821 return;
823 SwitchDoorOrButton(false);
824 SetLootState(GO_JUST_DEACTIVATED);
825 m_cooldownTime = 0;
828 void GameObject::UseDoorOrButton(uint32 time_to_restore, bool alternative /* = false */)
830 if(m_lootState != GO_READY)
831 return;
833 if(!time_to_restore)
834 time_to_restore = GetGOInfo()->GetAutoCloseTime();
836 SwitchDoorOrButton(true,alternative);
837 SetLootState(GO_ACTIVATED);
839 m_cooldownTime = time(NULL) + time_to_restore;
842 void GameObject::SwitchDoorOrButton(bool activate, bool alternative /* = false */)
844 if(activate)
845 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
846 else
847 RemoveFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
849 if(GetGoState() == GO_STATE_READY) //if closed -> open
850 SetGoState(alternative ? GO_STATE_ACTIVE_ALTERNATIVE : GO_STATE_ACTIVE);
851 else //if open -> close
852 SetGoState(GO_STATE_READY);
855 void GameObject::Use(Unit* user)
857 // by default spell caster is user
858 Unit* spellCaster = user;
859 uint32 spellId = 0;
860 bool triggered = false;
862 switch(GetGoType())
864 case GAMEOBJECT_TYPE_DOOR: //0
865 case GAMEOBJECT_TYPE_BUTTON: //1
866 //doors/buttons never really despawn, only reset to default state/flags
867 UseDoorOrButton();
869 // activate script
870 GetMap()->ScriptsStart(sGameObjectScripts, GetDBTableGUIDLow(), spellCaster, this);
871 return;
873 case GAMEOBJECT_TYPE_QUESTGIVER: //2
875 if (user->GetTypeId() != TYPEID_PLAYER)
876 return;
878 Player* player = (Player*)user;
880 player->PrepareGossipMenu(this, GetGOInfo()->questgiver.gossipID);
881 player->SendPreparedGossip(this);
882 return;
884 //Sitting: Wooden bench, chairs enzz
885 case GAMEOBJECT_TYPE_CHAIR: //7
887 GameObjectInfo const* info = GetGOInfo();
888 if(!info)
889 return;
891 if(user->GetTypeId()!=TYPEID_PLAYER)
892 return;
894 Player* player = (Player*)user;
896 // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
898 // check if the db is sane
899 if(info->chair.slots > 0)
901 float lowestDist = DEFAULT_VISIBILITY_DISTANCE;
903 float x_lowest = GetPositionX();
904 float y_lowest = GetPositionY();
906 // the object orientation + 1/2 pi
907 // every slot will be on that straight line
908 float orthogonalOrientation = GetOrientation()+M_PI*0.5f;
909 // find nearest slot
910 for(uint32 i=0; i<info->chair.slots; ++i)
912 // the distance between this slot and the center of the go - imagine a 1D space
913 float relativeDistance = (info->size*i)-(info->size*(info->chair.slots-1)/2.0f);
915 float x_i = GetPositionX() + relativeDistance * cos(orthogonalOrientation);
916 float y_i = GetPositionY() + relativeDistance * sin(orthogonalOrientation);
918 // calculate the distance between the player and this slot
919 float thisDistance = player->GetDistance2d(x_i, y_i);
921 /* debug code. It will spawn a npc on each slot to visualize them.
922 Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
923 std::ostringstream output;
924 output << i << ": thisDist: " << thisDistance;
925 helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
928 if(thisDistance <= lowestDist)
930 lowestDist = thisDistance;
931 x_lowest = x_i;
932 y_lowest = y_i;
935 player->TeleportTo(GetMapId(), x_lowest, y_lowest, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
937 else
939 // fallback, will always work
940 player->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
942 player->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR+info->chair.height);
943 return;
945 //big gun, its a spell/aura
946 case GAMEOBJECT_TYPE_GOOBER: //10
948 GameObjectInfo const* info = GetGOInfo();
950 if(user->GetTypeId()==TYPEID_PLAYER)
952 Player* player = (Player*)user;
954 if (info->goober.pageId) // show page...
956 WorldPacket data(SMSG_GAMEOBJECT_PAGETEXT, 8);
957 data << GetGUID();
958 player->GetSession()->SendPacket(&data);
960 else if (info->goober.gossipID) // ...or gossip, if page does not exist
962 player->PrepareGossipMenu(this, info->goober.gossipID);
963 player->SendPreparedGossip(this);
966 // possible quest objective for active quests
967 player->CastedCreatureOrGO(info->id, GetGUID(), 0);
969 if (info->goober.eventId)
970 GetMap()->ScriptsStart(sEventScripts, info->goober.eventId, player, this);
973 // cast this spell later if provided
974 spellId = info->goober.spellId;
976 break;
978 case GAMEOBJECT_TYPE_CAMERA: //13
980 GameObjectInfo const* info = GetGOInfo();
981 if(!info)
982 return;
984 if(user->GetTypeId()!=TYPEID_PLAYER)
985 return;
987 Player* player = (Player*)user;
989 if (info->camera.cinematicId)
990 player->SendCinematicStart(info->camera.cinematicId);
992 if (info->camera.eventID)
993 GetMap()->ScriptsStart(sEventScripts, info->camera.eventID, player, this);
995 return;
997 //fishing bobber
998 case GAMEOBJECT_TYPE_FISHINGNODE: //17
1000 if(user->GetTypeId()!=TYPEID_PLAYER)
1001 return;
1003 Player* player = (Player*)user;
1005 if(player->GetGUID() != GetOwnerGUID())
1006 return;
1008 switch(getLootState())
1010 case GO_READY: // ready for loot
1012 // 1) skill must be >= base_zone_skill
1013 // 2) if skill == base_zone_skill => 5% chance
1014 // 3) chance is linear dependence from (base_zone_skill-skill)
1016 uint32 zone, subzone;
1017 GetZoneAndAreaId(zone,subzone);
1019 int32 zone_skill = sObjectMgr.GetFishingBaseSkillLevel( subzone );
1020 if(!zone_skill)
1021 zone_skill = sObjectMgr.GetFishingBaseSkillLevel( zone );
1023 //provide error, no fishable zone or area should be 0
1024 if(!zone_skill)
1025 sLog.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone);
1027 int32 skill = player->GetSkillValue(SKILL_FISHING);
1028 int32 chance = skill - zone_skill + 5;
1029 int32 roll = irand(1,100);
1031 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill,zone_skill,chance,roll);
1033 if(skill >= zone_skill && chance >= roll)
1035 // prevent removing GO at spell cancel
1036 player->RemoveGameObject(this,false);
1037 SetOwnerGUID(player->GetGUID());
1039 //fish catched
1040 player->UpdateFishingSkill();
1042 //TODO: find reasonable value for fishing hole search
1043 GameObject* ok = LookupFishingHoleAround(20.0f + CONTACT_DISTANCE);
1044 if (ok)
1046 player->SendLoot(ok->GetGUID(),LOOT_FISHINGHOLE);
1047 player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_FISH_IN_GAMEOBJECT, ok->GetGOInfo()->id);
1048 SetLootState(GO_JUST_DEACTIVATED);
1050 else
1051 player->SendLoot(GetGUID(),LOOT_FISHING);
1053 else
1055 // fish escaped, can be deleted now
1056 SetLootState(GO_JUST_DEACTIVATED);
1058 WorldPacket data(SMSG_FISH_ESCAPED, 0);
1059 player->GetSession()->SendPacket(&data);
1061 break;
1063 case GO_JUST_DEACTIVATED: // nothing to do, will be deleted at next update
1064 break;
1065 default:
1067 SetLootState(GO_JUST_DEACTIVATED);
1069 WorldPacket data(SMSG_FISH_NOT_HOOKED, 0);
1070 player->GetSession()->SendPacket(&data);
1071 break;
1075 player->FinishSpell(CURRENT_CHANNELED_SPELL);
1076 return;
1079 case GAMEOBJECT_TYPE_SUMMONING_RITUAL: //18
1081 if(user->GetTypeId()!=TYPEID_PLAYER)
1082 return;
1084 Player* player = (Player*)user;
1086 Unit* caster = GetOwner();
1088 GameObjectInfo const* info = GetGOInfo();
1090 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1091 return;
1093 // accept only use by player from same group for caster except caster itself
1094 if(((Player*)caster)==player || !((Player*)caster)->IsInSameRaidWith(player))
1095 return;
1097 AddUniqueUse(player);
1099 // full amount unique participants including original summoner
1100 if(GetUniqueUseCount() < info->summoningRitual.reqParticipants)
1101 return;
1103 // in case summoning ritual caster is GO creator
1104 spellCaster = caster;
1106 if(!caster->GetCurrentSpell(CURRENT_CHANNELED_SPELL))
1107 return;
1109 spellId = info->summoningRitual.spellId;
1110 if(spellId==62330) // GO store not existed spell, replace by expected
1112 // spell have reagent and mana cost but it not expected use its
1113 // it triggered spell in fact casted at currently channeled GO
1114 spellId = 61993;
1115 triggered = true;
1118 // finish spell
1119 player->FinishSpell(CURRENT_CHANNELED_SPELL);
1121 // can be deleted now
1122 SetLootState(GO_JUST_DEACTIVATED);
1124 // go to end function to spell casting
1125 break;
1127 case GAMEOBJECT_TYPE_SPELLCASTER: //22
1129 SetUInt32Value(GAMEOBJECT_FLAGS,2);
1131 GameObjectInfo const* info = GetGOInfo();
1132 if(!info)
1133 return;
1135 if(info->spellcaster.partyOnly)
1137 Unit* caster = GetOwner();
1138 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1139 return;
1141 if(user->GetTypeId()!=TYPEID_PLAYER || !((Player*)user)->IsInSameRaidWith((Player*)caster))
1142 return;
1145 spellId = info->spellcaster.spellId;
1147 AddUse();
1148 break;
1150 case GAMEOBJECT_TYPE_MEETINGSTONE: //23
1152 GameObjectInfo const* info = GetGOInfo();
1154 if(user->GetTypeId()!=TYPEID_PLAYER)
1155 return;
1157 Player* player = (Player*)user;
1159 Player* targetPlayer = ObjectAccessor::FindPlayer(player->GetSelection());
1161 // accept only use by player from same group for caster except caster itself
1162 if(!targetPlayer || targetPlayer == player || !targetPlayer->IsInSameGroupWith(player))
1163 return;
1165 //required lvl checks!
1166 uint8 level = player->getLevel();
1167 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1168 return;
1169 level = targetPlayer->getLevel();
1170 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1171 return;
1173 if(info->id==194097)
1174 spellId = 61994; // Ritual of Summoning
1175 else
1176 spellId = 59782; // Summoning Stone Effect
1178 break;
1181 case GAMEOBJECT_TYPE_FLAGSTAND: // 24
1183 if(user->GetTypeId()!=TYPEID_PLAYER)
1184 return;
1186 Player* player = (Player*)user;
1188 if( player->CanUseBattleGroundObject() )
1190 // in battleground check
1191 BattleGround *bg = player->GetBattleGround();
1192 if(!bg)
1193 return;
1194 // BG flag click
1195 // AB:
1196 // 15001
1197 // 15002
1198 // 15003
1199 // 15004
1200 // 15005
1201 bg->EventPlayerClickedOnFlag(player, this);
1202 return; //we don;t need to delete flag ... it is despawned!
1204 break;
1206 case GAMEOBJECT_TYPE_FLAGDROP: // 26
1208 if(user->GetTypeId()!=TYPEID_PLAYER)
1209 return;
1211 Player* player = (Player*)user;
1213 if( player->CanUseBattleGroundObject() )
1215 // in battleground check
1216 BattleGround *bg = player->GetBattleGround();
1217 if(!bg)
1218 return;
1219 // BG flag dropped
1220 // WS:
1221 // 179785 - Silverwing Flag
1222 // 179786 - Warsong Flag
1223 // EotS:
1224 // 184142 - Netherstorm Flag
1225 GameObjectInfo const* info = GetGOInfo();
1226 if(info)
1228 switch(info->id)
1230 case 179785: // Silverwing Flag
1231 // check if it's correct bg
1232 if(bg->GetTypeID() == BATTLEGROUND_WS)
1233 bg->EventPlayerClickedOnFlag(player, this);
1234 break;
1235 case 179786: // Warsong Flag
1236 if(bg->GetTypeID() == BATTLEGROUND_WS)
1237 bg->EventPlayerClickedOnFlag(player, this);
1238 break;
1239 case 184142: // Netherstorm Flag
1240 if(bg->GetTypeID() == BATTLEGROUND_EY)
1241 bg->EventPlayerClickedOnFlag(player, this);
1242 break;
1245 //this cause to call return, all flags must be deleted here!!
1246 spellId = 0;
1247 Delete();
1249 break;
1251 case GAMEOBJECT_TYPE_BARBER_CHAIR: //32
1253 GameObjectInfo const* info = GetGOInfo();
1254 if(!info)
1255 return;
1257 if(user->GetTypeId()!=TYPEID_PLAYER)
1258 return;
1260 Player* player = (Player*)user;
1262 // fallback, will always work
1263 player->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
1265 WorldPacket data(SMSG_ENABLE_BARBER_SHOP, 0);
1266 player->GetSession()->SendPacket(&data);
1268 player->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR+info->barberChair.chairheight);
1269 return;
1271 default:
1272 sLog.outDebug("Unknown Object Type %u", GetGoType());
1273 break;
1276 if(!spellId)
1277 return;
1279 SpellEntry const *spellInfo = sSpellStore.LookupEntry( spellId );
1280 if(!spellInfo)
1282 sLog.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId,GetEntry(),GetGoType());
1283 return;
1286 Spell *spell = new Spell(spellCaster, spellInfo, triggered,GetGUID());
1288 // spell target is user of GO
1289 SpellCastTargets targets;
1290 targets.setUnitTarget( user );
1292 spell->prepare(&targets);
1295 // overwrite WorldObject function for proper name localization
1296 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx) const
1298 if (loc_idx >= 0)
1300 GameObjectLocale const *cl = sObjectMgr.GetGameObjectLocale(GetEntry());
1301 if (cl)
1303 if (cl->Name.size() > (size_t)loc_idx && !cl->Name[loc_idx].empty())
1304 return cl->Name[loc_idx].c_str();
1308 return GetName();
1311 void GameObject::UpdateRotationFields(float rotation2 /*=0.0f*/, float rotation3 /*=0.0f*/)
1313 static double const atan_pow = atan(pow(2.0f, -20.0f));
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;
1321 //float f_rot2 = sin(0.0f / 2.0f);
1322 //int64 i_rot2 = f_rot2 / atan(pow(2.0f, -20.0f));
1323 //rotation |= (((i_rot2 << 22) >> 32) >> 11) & 0x000003FFFFE00000;
1325 //float f_rot3 = sin(0.0f / 2.0f);
1326 //int64 i_rot3 = f_rot3 / atan(pow(2.0f, -21.0f));
1327 //rotation |= (i_rot3 >> 42) & 0x7FFFFC0000000000;
1329 m_rotation = rotation;
1331 if(rotation2==0.0f && rotation3==0.0f)
1333 rotation2 = f_rot1;
1334 rotation3 = f_rot2;
1337 SetFloatValue(GAMEOBJECT_PARENTROTATION+2, rotation2);
1338 SetFloatValue(GAMEOBJECT_PARENTROTATION+3, rotation3);