[6922] Whitespace and newline fixes
[getmangos.git] / src / game / GameObject.cpp
blobab412dcf2c255d435de067d2a2bdf8856120f079
1 /*
2 * Copyright (C) 2005-2008 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 "SpellMgr.h"
24 #include "Spell.h"
25 #include "UpdateMask.h"
26 #include "Opcodes.h"
27 #include "WorldPacket.h"
28 #include "WorldSession.h"
29 #include "World.h"
30 #include "Database/DatabaseEnv.h"
31 #include "MapManager.h"
32 #include "LootMgr.h"
33 #include "GridNotifiers.h"
34 #include "GridNotifiersImpl.h"
35 #include "CellImpl.h"
36 #include "InstanceData.h"
37 #include "BattleGround.h"
38 #include "Util.h"
40 GameObject::GameObject() : WorldObject()
42 m_objectType |= TYPEMASK_GAMEOBJECT;
43 m_objectTypeId = TYPEID_GAMEOBJECT;
44 // 2.3.2 - 0x58
45 m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HASPOSITION);
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_charges = 5;
55 m_cooldownTime = 0;
56 m_goInfo = NULL;
58 m_DBTableGuid = 0;
61 GameObject::~GameObject()
63 if(m_uint32Values) // field array can be not exist if GameOBject not loaded
65 // crash possible at access to deleted GO in Unit::m_gameobj
66 uint64 owner_guid = GetOwnerGUID();
67 if(owner_guid)
69 Unit* owner = ObjectAccessor::GetUnit(*this,owner_guid);
70 if(owner)
71 owner->RemoveGameObject(this,false);
72 else if(!IS_PLAYER_GUID(owner_guid))
73 sLog.outError("Delete GameObject (GUID: %u Entry: %u ) that have references in not found creature %u GO list. Crash possible later.",GetGUIDLow(),GetGOInfo()->id,GUID_LOPART(owner_guid));
78 void GameObject::AddToWorld()
80 ///- Register the gameobject for guid lookup
81 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
82 Object::AddToWorld();
85 void GameObject::RemoveFromWorld()
87 ///- Remove the gameobject from the accessor
88 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
89 Object::RemoveFromWorld();
92 bool GameObject::Create(uint32 guidlow, uint32 name_id, Map *map, float x, float y, float z, float ang, float rotation0, float rotation1, float rotation2, float rotation3, uint32 animprogress, uint32 go_state)
94 Relocate(x,y,z,ang);
95 SetMapId(map->GetId());
96 SetInstanceId(map->GetInstanceId());
98 if(!IsPositionValid())
100 sLog.outError("ERROR: 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);
124 SetFloatValue(GAMEOBJECT_FACING, ang); //this is not facing angle
126 SetFloatValue (GAMEOBJECT_ROTATION, rotation0);
127 SetFloatValue (GAMEOBJECT_ROTATION+1, rotation1);
128 SetFloatValue (GAMEOBJECT_ROTATION+2, rotation2);
129 SetFloatValue (GAMEOBJECT_ROTATION+3, rotation3);
131 SetFloatValue(OBJECT_FIELD_SCALE_X, goinfo->size);
133 SetUInt32Value(GAMEOBJECT_FACTION, goinfo->faction);
134 SetUInt32Value(GAMEOBJECT_FLAGS, goinfo->flags);
136 SetEntry(goinfo->id);
138 SetUInt32Value(GAMEOBJECT_DISPLAYID, goinfo->displayId);
140 SetGoState(go_state);
141 SetGoType(GameobjectTypes(goinfo->type));
143 SetGoAnimProgress(animprogress);
145 // Spell charges for GAMEOBJECT_TYPE_SPELLCASTER (22)
146 if (goinfo->type == GAMEOBJECT_TYPE_SPELLCASTER)
147 m_charges = goinfo->spellcaster.charges;
149 //Notify the map's instance data.
150 //Only works if you create the object in it, not if it is moves to that map.
151 //Normally non-players do not teleport to other maps.
152 if(map->IsDungeon() && ((InstanceMap*)map)->GetInstanceData())
154 ((InstanceMap*)map)->GetInstanceData()->OnObjectCreate(this);
157 return true;
160 void GameObject::Update(uint32 /*p_time*/)
162 if (IS_MO_TRANSPORT(GetGUID()))
164 //((Transport*)this)->Update(p_time);
165 return;
168 switch (m_lootState)
170 case GO_NOT_READY:
172 switch(GetGoType())
174 case GAMEOBJECT_TYPE_TRAP:
176 // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
177 Unit* owner = GetOwner();
178 if (owner && ((Player*)owner)->isInCombat())
179 m_cooldownTime = time(NULL) + GetGOInfo()->trap.startDelay;
180 m_lootState = GO_READY;
181 break;
183 case GAMEOBJECT_TYPE_FISHINGNODE:
185 // fishing code (bobber ready)
186 if( time(NULL) > m_respawnTime - FISHING_BOBBER_READY_TIME )
188 // splash bobber (bobber ready now)
189 Unit* caster = GetOwner();
190 if(caster && caster->GetTypeId()==TYPEID_PLAYER)
192 SetGoState(0);
193 SetUInt32Value(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
195 UpdateData udata;
196 WorldPacket packet;
197 BuildValuesUpdateBlockForPlayer(&udata,((Player*)caster));
198 udata.BuildPacket(&packet);
199 ((Player*)caster)->GetSession()->SendPacket(&packet);
201 WorldPacket data(SMSG_GAMEOBJECT_CUSTOM_ANIM,8+4);
202 data << GetGUID();
203 data << (uint32)(0);
204 ((Player*)caster)->SendMessageToSet(&data,true);
207 m_lootState = GO_READY; // can be successfully open with some chance
209 return;
211 default:
212 m_lootState = GO_READY; // for other GOis same switched without delay to GO_READY
213 break;
215 // NO BREAK for switch (m_lootState)
217 case GO_READY:
219 if (m_respawnTime > 0) // timer on
221 if (m_respawnTime <= time(NULL)) // timer expired
223 m_respawnTime = 0;
224 m_SkillupList.clear();
225 m_usetimes = 0;
227 switch (GetGoType())
229 case GAMEOBJECT_TYPE_FISHINGNODE: // can't fish now
231 Unit* caster = GetOwner();
232 if(caster && caster->GetTypeId()==TYPEID_PLAYER)
234 if(caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
236 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
237 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish(false);
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() )
251 SwitchDoorOrButton(false);
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 GetMap()->Add(this);
262 break;
267 // traps can have time and can not have
268 GameObjectInfo const* goInfo = GetGOInfo();
269 if(goInfo->type == GAMEOBJECT_TYPE_TRAP)
271 // traps
272 Unit* owner = GetOwner();
273 Unit* ok = NULL; // pointer to appropriate target if found any
275 if(m_cooldownTime >= time(NULL))
276 return;
278 bool IsBattleGroundTrap = false;
279 //FIXME: this is activation radius (in different casting radius that must be selected from spell data)
280 //TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
281 float radius = goInfo->trap.radius;
282 if(!radius)
284 if(goInfo->trap.cooldown != 3) // cast in other case (at some triggering/linked go/etc explicit call)
285 return;
286 else
288 if(m_respawnTime > 0)
289 break;
291 radius = goInfo->trap.cooldown; // battlegrounds gameobjects has data2 == 0 && data5 == 3
292 IsBattleGroundTrap = true;
296 bool NeedDespawn = (goInfo->trap.charges != 0);
298 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
299 Cell cell(p);
300 cell.data.Part.reserved = ALL_DISTRICT;
302 // Note: this hack with search required until GO casting not implemented
303 // search unfriendly creature
304 if(owner && NeedDespawn) // hunter trap
306 MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck u_check(this, owner, radius);
307 MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck> checker(ok, u_check);
309 CellLock<GridReadGuard> cell_lock(cell, p);
311 TypeContainerVisitor<MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck>, GridTypeMapContainer > grid_object_checker(checker);
312 cell_lock->Visit(cell_lock, grid_object_checker, *GetMap());
314 // or unfriendly player/pet
315 if(!ok)
317 TypeContainerVisitor<MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
318 cell_lock->Visit(cell_lock, world_object_checker, *GetMap());
321 else // environmental trap
323 // environmental damage spells already have around enemies targeting but this not help in case not existed GO casting support
325 // affect only players
326 Player* p_ok = NULL;
327 MaNGOS::AnyPlayerInObjectRangeCheck p_check(this, radius);
328 MaNGOS::PlayerSearcher<MaNGOS::AnyPlayerInObjectRangeCheck> checker(p_ok, p_check);
330 CellLock<GridReadGuard> cell_lock(cell, p);
332 TypeContainerVisitor<MaNGOS::PlayerSearcher<MaNGOS::AnyPlayerInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
333 cell_lock->Visit(cell_lock, world_object_checker, *GetMap());
334 ok = p_ok;
337 if (ok)
339 Unit *caster = owner ? owner : ok;
341 caster->CastSpell(ok, goInfo->trap.spellId, true);
342 m_cooldownTime = time(NULL) + 4; // 4 seconds
344 if(NeedDespawn)
345 SetLootState(GO_JUST_DEACTIVATED); // can be despawned or destroyed
347 if(IsBattleGroundTrap && ok->GetTypeId() == TYPEID_PLAYER)
349 //BattleGround gameobjects case
350 if(((Player*)ok)->InBattleGround())
351 if(BattleGround *bg = ((Player*)ok)->GetBattleGround())
352 bg->HandleTriggerBuff(GetGUID());
357 if (m_charges && m_usetimes >= m_charges)
358 SetLootState(GO_JUST_DEACTIVATED); // can be despawned or destroyed
360 break;
362 case GO_ACTIVATED:
364 switch(GetGoType())
366 case GAMEOBJECT_TYPE_DOOR:
367 case GAMEOBJECT_TYPE_BUTTON:
368 if(GetAutoCloseTime() && (m_cooldownTime < time(NULL)))
370 SwitchDoorOrButton(false);
371 SetLootState(GO_JUST_DEACTIVATED);
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>::iterator it = m_unique_users.begin();
387 std::set<uint32>::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);
394 m_unique_users.clear();
395 m_usetimes = 0;
397 //any return here in case battleground traps
400 if(GetOwnerGUID())
402 m_respawnTime = 0;
403 Delete();
404 return;
407 //burning flags in some battlegrounds, if you find better condition, just add it
408 if (GetGoAnimProgress() > 0)
410 SendObjectDeSpawnAnim(GetGUID());
411 //reset flags
412 SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
415 loot.clear();
416 SetLootState(GO_READY);
418 if(!m_respawnDelayTime)
419 return;
421 if(!m_spawnedByDefault)
423 m_respawnTime = 0;
424 return;
427 m_respawnTime = time(NULL) + m_respawnDelayTime;
429 // if option not set then object will be saved at grid unload
430 if(sWorld.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY))
431 SaveRespawnTime();
433 ObjectAccessor::UpdateObjectVisibility(this);
435 break;
440 void GameObject::Refresh()
442 // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
443 if(m_respawnTime > 0 && m_spawnedByDefault)
444 return;
446 if(isSpawned())
447 GetMap()->Add(this);
450 void GameObject::AddUniqueUse(Player* player)
452 AddUse();
453 m_unique_users.insert(player->GetGUIDLow());
456 void GameObject::Delete()
458 SendObjectDeSpawnAnim(GetGUID());
460 SetGoState(1);
461 SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
463 AddObjectToRemoveList();
466 void GameObject::getFishLoot(Loot *fishloot)
468 fishloot->clear();
470 uint32 subzone = GetAreaId();
472 // if subzone loot exist use it
473 if(LootTemplates_Fishing.HaveLootFor(subzone))
474 fishloot->FillLoot(subzone, LootTemplates_Fishing, NULL);
475 // else use zone loot
476 else
477 fishloot->FillLoot(GetZoneId(), LootTemplates_Fishing, NULL);
480 void GameObject::SaveToDB()
482 // this should only be used when the gameobject has already been loaded
483 // preferably after adding to map, because mapid may not be valid otherwise
484 GameObjectData const *data = objmgr.GetGOData(m_DBTableGuid);
485 if(!data)
487 sLog.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
488 return;
491 SaveToDB(GetMapId(), data->spawnMask);
494 void GameObject::SaveToDB(uint32 mapid, uint8 spawnMask)
496 const GameObjectInfo *goI = GetGOInfo();
498 if (!goI)
499 return;
501 if (!m_DBTableGuid)
502 m_DBTableGuid = GetGUIDLow();
503 // update in loaded data (changing data only in this place)
504 GameObjectData& data = objmgr.NewGOData(m_DBTableGuid);
506 // data->guid = guid don't must be update at save
507 data.id = GetEntry();
508 data.mapid = mapid;
509 data.posX = GetFloatValue(GAMEOBJECT_POS_X);
510 data.posY = GetFloatValue(GAMEOBJECT_POS_Y);
511 data.posZ = GetFloatValue(GAMEOBJECT_POS_Z);
512 data.orientation = GetFloatValue(GAMEOBJECT_FACING);
513 data.rotation0 = GetFloatValue(GAMEOBJECT_ROTATION+0);
514 data.rotation1 = GetFloatValue(GAMEOBJECT_ROTATION+1);
515 data.rotation2 = GetFloatValue(GAMEOBJECT_ROTATION+2);
516 data.rotation3 = GetFloatValue(GAMEOBJECT_ROTATION+3);
517 data.spawntimesecs = m_spawnedByDefault ? m_respawnDelayTime : -(int32)m_respawnDelayTime;
518 data.animprogress = GetGoAnimProgress();
519 data.go_state = GetGoState();
520 data.spawnMask = spawnMask;
522 // updated in DB
523 std::ostringstream ss;
524 ss << "INSERT INTO gameobject VALUES ( "
525 << m_DBTableGuid << ", "
526 << GetEntry() << ", "
527 << mapid << ", "
528 << (uint32)spawnMask << ", "
529 << GetFloatValue(GAMEOBJECT_POS_X) << ", "
530 << GetFloatValue(GAMEOBJECT_POS_Y) << ", "
531 << GetFloatValue(GAMEOBJECT_POS_Z) << ", "
532 << GetFloatValue(GAMEOBJECT_FACING) << ", "
533 << GetFloatValue(GAMEOBJECT_ROTATION) << ", "
534 << GetFloatValue(GAMEOBJECT_ROTATION+1) << ", "
535 << GetFloatValue(GAMEOBJECT_ROTATION+2) << ", "
536 << GetFloatValue(GAMEOBJECT_ROTATION+3) << ", "
537 << m_respawnDelayTime << ", "
538 << (uint32)GetGoAnimProgress() << ", "
539 << (uint32)GetGoState() << ")";
541 WorldDatabase.BeginTransaction();
542 WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid);
543 WorldDatabase.PExecuteLog( ss.str( ).c_str( ) );
544 WorldDatabase.CommitTransaction();
547 bool GameObject::LoadFromDB(uint32 guid, Map *map)
549 GameObjectData const* data = objmgr.GetGOData(guid);
551 if( !data )
553 sLog.outErrorDb("ERROR: Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid);
554 return false;
557 uint32 entry = data->id;
558 //uint32 map_id = data->mapid; // already used before call
559 float x = data->posX;
560 float y = data->posY;
561 float z = data->posZ;
562 float ang = data->orientation;
564 float rotation0 = data->rotation0;
565 float rotation1 = data->rotation1;
566 float rotation2 = data->rotation2;
567 float rotation3 = data->rotation3;
569 uint32 animprogress = data->animprogress;
570 uint32 go_state = data->go_state;
572 m_DBTableGuid = guid;
573 if (map->GetInstanceId() != 0) guid = objmgr.GenerateLowGuid(HIGHGUID_GAMEOBJECT);
575 if (!Create(guid,entry, map, x, y, z, ang, rotation0, rotation1, rotation2, rotation3, animprogress, go_state) )
576 return false;
578 switch(GetGOInfo()->type)
580 case GAMEOBJECT_TYPE_DOOR:
581 case GAMEOBJECT_TYPE_BUTTON:
582 /* this code (in comment) isn't correct because in battlegrounds we need despawnable doors and buttons, pls remove
583 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
584 m_spawnedByDefault = true;
585 m_respawnDelayTime = 0;
586 m_respawnTime = 0;
587 break;*/
588 default:
589 if(data->spawntimesecs >= 0)
591 m_spawnedByDefault = true;
592 m_respawnDelayTime = data->spawntimesecs;
593 m_respawnTime = objmgr.GetGORespawnTime(m_DBTableGuid, map->GetInstanceId());
595 // ready to respawn
596 if(m_respawnTime && m_respawnTime <= time(NULL))
598 m_respawnTime = 0;
599 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
602 else
604 m_spawnedByDefault = false;
605 m_respawnDelayTime = -data->spawntimesecs;
606 m_respawnTime = 0;
608 break;
611 return true;
614 void GameObject::DeleteFromDB()
616 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
617 objmgr.DeleteGOData(m_DBTableGuid);
618 WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid);
619 WorldDatabase.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid);
622 GameObject* GameObject::GetGameObject(WorldObject& object, uint64 guid)
624 return ObjectAccessor::GetGameObject(object,guid);
627 GameObjectInfo const *GameObject::GetGOInfo() const
629 return m_goInfo;
632 uint32 GameObject::GetLootId(GameObjectInfo const* ginfo)
634 if (!ginfo)
635 return 0;
637 switch(ginfo->type)
639 case GAMEOBJECT_TYPE_CHEST:
640 return ginfo->chest.lootId;
641 case GAMEOBJECT_TYPE_FISHINGHOLE:
642 return ginfo->fishinghole.lootId;
643 case GAMEOBJECT_TYPE_FISHINGNODE:
644 return ginfo->fishnode.lootId;
645 default:
646 return 0;
650 /*********************************************************/
651 /*** QUEST SYSTEM ***/
652 /*********************************************************/
653 bool GameObject::hasQuest(uint32 quest_id) const
655 QuestRelations const& qr = objmgr.mGOQuestRelations;
656 for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr)
658 if(itr->second==quest_id)
659 return true;
661 return false;
664 bool GameObject::hasInvolvedQuest(uint32 quest_id) const
666 QuestRelations const& qr = objmgr.mGOQuestInvolvedRelations;
667 for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr)
669 if(itr->second==quest_id)
670 return true;
672 return false;
675 bool GameObject::IsTransport() const
677 // If something is marked as a transport, don't transmit an out of range packet for it.
678 GameObjectInfo const * gInfo = GetGOInfo();
679 if(!gInfo) return false;
680 return gInfo->type == GAMEOBJECT_TYPE_TRANSPORT || gInfo->type == GAMEOBJECT_TYPE_MO_TRANSPORT;
683 Unit* GameObject::GetOwner() const
685 return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
688 void GameObject::SaveRespawnTime()
690 if(m_respawnTime > time(NULL) && m_spawnedByDefault)
691 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),m_respawnTime);
694 bool GameObject::isVisibleForInState(Player const* u, bool inVisibleList) const
696 // Not in world
697 if(!IsInWorld() || !u->IsInWorld())
698 return false;
700 // Transport always visible at this step implementation
701 if(IsTransport() && IsInMap(u))
702 return true;
704 // quick check visibility false cases for non-GM-mode
705 if(!u->isGameMaster())
707 // despawned and then not visible for non-GM in GM-mode
708 if(!isSpawned())
709 return false;
711 // special invisibility cases
712 /* TODO: implement trap stealth, take look at spell 2836
713 if(GetGOInfo()->type == GAMEOBJECT_TYPE_TRAP && GetGOInfo()->trap.stealthed && u->IsHostileTo(GetOwner()))
715 if(check stuff here)
716 return false;
719 // Smuggled Mana Cell required 10 invisibility type detection/state
720 if(GetEntry()==187039 && ((u->m_detectInvisibilityMask | u->m_invisibilityMask) & (1<<10))==0)
721 return false;
724 // check distance
725 return IsWithinDistInMap(u,World::GetMaxVisibleDistanceForObject() +
726 (inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false);
729 void GameObject::Respawn()
731 if(m_spawnedByDefault && m_respawnTime > 0)
733 m_respawnTime = time(NULL);
734 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
738 bool GameObject::ActivateToQuest( Player *pTarget)const
740 if(!objmgr.IsGameObjectForQuests(GetEntry()))
741 return false;
743 switch(GetGoType())
745 // scan GO chest with loot including quest items
746 case GAMEOBJECT_TYPE_CHEST:
748 if(LootTemplates_Gameobject.HaveQuestLootForPlayer(GetLootId(), pTarget))
749 return true;
750 break;
752 case GAMEOBJECT_TYPE_GOOBER:
754 if(pTarget->GetQuestStatus(GetGOInfo()->goober.questId) == QUEST_STATUS_INCOMPLETE)
755 return true;
756 break;
758 default:
759 break;
762 return false;
765 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry, Unit* target)
767 GameObjectInfo const* trapInfo = sGOStorage.LookupEntry<GameObjectInfo>(trapEntry);
768 if(!trapInfo || trapInfo->type!=GAMEOBJECT_TYPE_TRAP)
769 return;
771 SpellEntry const* trapSpell = sSpellStore.LookupEntry(trapInfo->trap.spellId);
772 if(!trapSpell) // checked at load already
773 return;
775 float range = GetSpellMaxRange(sSpellRangeStore.LookupEntry(trapSpell->rangeIndex));
777 // search nearest linked GO
778 GameObject* trapGO = NULL;
780 // using original GO distance
781 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
782 Cell cell(p);
783 cell.data.Part.reserved = ALL_DISTRICT;
785 MaNGOS::NearestGameObjectEntryInObjectRangeCheck go_check(*target,trapEntry,range);
786 MaNGOS::GameObjectLastSearcher<MaNGOS::NearestGameObjectEntryInObjectRangeCheck> checker(trapGO,go_check);
788 TypeContainerVisitor<MaNGOS::GameObjectLastSearcher<MaNGOS::NearestGameObjectEntryInObjectRangeCheck>, GridTypeMapContainer > object_checker(checker);
789 CellLock<GridReadGuard> cell_lock(cell, p);
790 cell_lock->Visit(cell_lock, object_checker, *GetMap());
793 // found correct GO
794 // FIXME: when GO casting will be implemented trap must cast spell to target
795 if(trapGO)
796 target->CastSpell(target,trapSpell,true);
799 GameObject* GameObject::LookupFishingHoleAround(float range)
801 GameObject* ok = NULL;
803 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
804 Cell cell(p);
805 cell.data.Part.reserved = ALL_DISTRICT;
806 MaNGOS::NearestGameObjectFishingHole u_check(*this, range);
807 MaNGOS::GameObjectSearcher<MaNGOS::NearestGameObjectFishingHole> checker(ok, u_check);
809 CellLock<GridReadGuard> cell_lock(cell, p);
811 TypeContainerVisitor<MaNGOS::GameObjectSearcher<MaNGOS::NearestGameObjectFishingHole>, GridTypeMapContainer > grid_object_checker(checker);
812 cell_lock->Visit(cell_lock, grid_object_checker, *GetMap());
814 return ok;
817 void GameObject::UseDoorOrButton(uint32 time_to_restore)
819 if(m_lootState != GO_READY)
820 return;
822 if(!time_to_restore)
823 time_to_restore = GetAutoCloseTime();
825 SwitchDoorOrButton(true);
826 SetLootState(GO_ACTIVATED);
828 m_cooldownTime = time(NULL) + time_to_restore;
832 void GameObject::SwitchDoorOrButton(bool activate)
834 if(activate)
835 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
836 else
837 RemoveFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
839 if(GetGoState()) //if closed -> open
840 SetGoState(0);
841 else //if open -> close
842 SetGoState(1);
845 void GameObject::Use(Unit* user)
847 // by default spell caster is user
848 Unit* spellCaster = user;
849 uint32 spellId = 0;
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(PLAYER_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)
973 WorldPacket data(SMSG_TRIGGER_CINEMATIC, 4);
974 data << info->camera.cinematicId;
975 player->GetSession()->SendPacket(&data);
977 return;
979 //fishing bobber
980 case GAMEOBJECT_TYPE_FISHINGNODE: //17
982 if(user->GetTypeId()!=TYPEID_PLAYER)
983 return;
985 Player* player = (Player*)user;
987 if(player->GetGUID() != GetOwnerGUID())
988 return;
990 switch(getLootState())
992 case GO_READY: // ready for loot
994 // 1) skill must be >= base_zone_skill
995 // 2) if skill == base_zone_skill => 5% chance
996 // 3) chance is linear dependence from (base_zone_skill-skill)
998 uint32 subzone = GetAreaId();
1000 int32 zone_skill = objmgr.GetFishingBaseSkillLevel( subzone );
1001 if(!zone_skill)
1002 zone_skill = objmgr.GetFishingBaseSkillLevel( GetZoneId() );
1004 //provide error, no fishable zone or area should be 0
1005 if(!zone_skill)
1006 sLog.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone);
1008 int32 skill = player->GetSkillValue(SKILL_FISHING);
1009 int32 chance = skill - zone_skill + 5;
1010 int32 roll = irand(1,100);
1012 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill,zone_skill,chance,roll);
1014 if(skill >= zone_skill && chance >= roll)
1016 // prevent removing GO at spell cancel
1017 player->RemoveGameObject(this,false);
1018 SetOwnerGUID(player->GetGUID());
1020 //fish catched
1021 player->UpdateFishingSkill();
1023 GameObject* ok = LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE);
1024 if (ok)
1026 player->SendLoot(ok->GetGUID(),LOOT_FISHINGHOLE);
1027 SetLootState(GO_JUST_DEACTIVATED);
1029 else
1030 player->SendLoot(GetGUID(),LOOT_FISHING);
1032 else
1034 // fish escaped, can be deleted now
1035 SetLootState(GO_JUST_DEACTIVATED);
1037 WorldPacket data(SMSG_FISH_ESCAPED, 0);
1038 player->GetSession()->SendPacket(&data);
1040 break;
1042 case GO_JUST_DEACTIVATED: // nothing to do, will be deleted at next update
1043 break;
1044 default:
1046 SetLootState(GO_JUST_DEACTIVATED);
1048 WorldPacket data(SMSG_FISH_NOT_HOOKED, 0);
1049 player->GetSession()->SendPacket(&data);
1050 break;
1054 if(player->m_currentSpells[CURRENT_CHANNELED_SPELL])
1056 player->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
1057 player->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
1059 return;
1062 case GAMEOBJECT_TYPE_SUMMONING_RITUAL: //18
1064 if(user->GetTypeId()!=TYPEID_PLAYER)
1065 return;
1067 Player* player = (Player*)user;
1069 Unit* caster = GetOwner();
1071 GameObjectInfo const* info = GetGOInfo();
1073 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1074 return;
1076 // accept only use by player from same group for caster except caster itself
1077 if(((Player*)caster)==player || !((Player*)caster)->IsInSameRaidWith(player))
1078 return;
1080 AddUniqueUse(player);
1082 // full amount unique participants including original summoner
1083 if(GetUniqueUseCount() < info->summoningRitual.reqParticipants)
1084 return;
1086 // in case summoning ritual caster is GO creator
1087 spellCaster = caster;
1089 if(!caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
1090 return;
1092 spellId = info->summoningRitual.spellId;
1094 // finish spell
1095 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
1096 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
1098 // can be deleted now
1099 SetLootState(GO_JUST_DEACTIVATED);
1101 // go to end function to spell casting
1102 break;
1104 case GAMEOBJECT_TYPE_SPELLCASTER: //22
1106 SetUInt32Value(GAMEOBJECT_FLAGS,2);
1108 GameObjectInfo const* info = GetGOInfo();
1109 if(!info)
1110 return;
1112 if(info->spellcaster.partyOnly)
1114 Unit* caster = GetOwner();
1115 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1116 return;
1118 if(user->GetTypeId()!=TYPEID_PLAYER || !((Player*)user)->IsInSameRaidWith((Player*)caster))
1119 return;
1122 spellId = info->spellcaster.spellId;
1124 AddUse();
1125 break;
1127 case GAMEOBJECT_TYPE_MEETINGSTONE: //23
1129 GameObjectInfo const* info = GetGOInfo();
1131 if(user->GetTypeId()!=TYPEID_PLAYER)
1132 return;
1134 Player* player = (Player*)user;
1136 Player* targetPlayer = ObjectAccessor::FindPlayer(player->GetSelection());
1138 // accept only use by player from same group for caster except caster itself
1139 if(!targetPlayer || targetPlayer == player || !targetPlayer->IsInSameGroupWith(player))
1140 return;
1142 //required lvl checks!
1143 uint8 level = player->getLevel();
1144 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1145 return;
1146 level = targetPlayer->getLevel();
1147 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1148 return;
1150 spellId = 23598;
1152 break;
1155 case GAMEOBJECT_TYPE_FLAGSTAND: // 24
1157 if(user->GetTypeId()!=TYPEID_PLAYER)
1158 return;
1160 Player* player = (Player*)user;
1162 if( player->isAllowUseBattleGroundObject() )
1164 // in battleground check
1165 BattleGround *bg = player->GetBattleGround();
1166 if(!bg)
1167 return;
1168 // BG flag click
1169 // AB:
1170 // 15001
1171 // 15002
1172 // 15003
1173 // 15004
1174 // 15005
1175 bg->EventPlayerClickedOnFlag(player, this);
1176 return; //we don;t need to delete flag ... it is despawned!
1178 break;
1180 case GAMEOBJECT_TYPE_FLAGDROP: // 26
1182 if(user->GetTypeId()!=TYPEID_PLAYER)
1183 return;
1185 Player* player = (Player*)user;
1187 if( player->isAllowUseBattleGroundObject() )
1189 // in battleground check
1190 BattleGround *bg = player->GetBattleGround();
1191 if(!bg)
1192 return;
1193 // BG flag dropped
1194 // WS:
1195 // 179785 - Silverwing Flag
1196 // 179786 - Warsong Flag
1197 // EotS:
1198 // 184142 - Netherstorm Flag
1199 GameObjectInfo const* info = GetGOInfo();
1200 if(info)
1202 switch(info->id)
1204 case 179785: // Silverwing Flag
1205 // check if it's correct bg
1206 if(bg->GetTypeID() == BATTLEGROUND_WS)
1207 bg->EventPlayerClickedOnFlag(player, this);
1208 break;
1209 case 179786: // Warsong Flag
1210 if(bg->GetTypeID() == BATTLEGROUND_WS)
1211 bg->EventPlayerClickedOnFlag(player, this);
1212 break;
1213 case 184142: // Netherstorm Flag
1214 if(bg->GetTypeID() == BATTLEGROUND_EY)
1215 bg->EventPlayerClickedOnFlag(player, this);
1216 break;
1219 //this cause to call return, all flags must be deleted here!!
1220 spellId = 0;
1221 Delete();
1223 break;
1225 default:
1226 sLog.outDebug("Unknown Object Type %u", GetGoType());
1227 break;
1230 if(!spellId)
1231 return;
1233 SpellEntry const *spellInfo = sSpellStore.LookupEntry( spellId );
1234 if(!spellInfo)
1236 sLog.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId,GetEntry(),GetGoType());
1237 return;
1240 Spell *spell = new Spell(spellCaster, spellInfo, false);
1242 // spell target is user of GO
1243 SpellCastTargets targets;
1244 targets.setUnitTarget( user );
1246 spell->prepare(&targets);
1249 // overwrite WorldObject function for proper name localization
1250 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx) const
1252 if (loc_idx >= 0)
1254 GameObjectLocale const *cl = objmgr.GetGameObjectLocale(GetEntry());
1255 if (cl)
1257 if (cl->Name.size() > loc_idx && !cl->Name[loc_idx].empty())
1258 return cl->Name[loc_idx].c_str();
1262 return GetName();