[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / GameObject.cpp
blobb852e2dcfa7dc75714151559152896fcb7fc2a1e
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 "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_HAS_POSITION);
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, uint32 phaseMask, 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());
97 SetPhaseMask(phaseMask,false);
99 if(!IsPositionValid())
101 sLog.outError("ERROR: Gameobject (GUID: %u Entry: %u ) not created. Suggested coordinates isn't valid (X: %f Y: %f)",guidlow,name_id,x,y);
102 return false;
105 GameObjectInfo const* goinfo = objmgr.GetGameObjectInfo(name_id);
106 if (!goinfo)
108 sLog.outErrorDb("Gameobject (GUID: %u Entry: %u) not created: it have not exist entry in `gameobject_template`. Map: %u (X: %f Y: %f Z: %f) ang: %f rotation0: %f rotation1: %f rotation2: %f rotation3: %f",guidlow, name_id, map->GetId(), x, y, z, ang, rotation0, rotation1, rotation2, rotation3);
109 return false;
112 Object::_Create(guidlow, goinfo->id, HIGHGUID_GAMEOBJECT);
114 m_goInfo = goinfo;
116 if (goinfo->type >= MAX_GAMEOBJECT_TYPE)
118 sLog.outErrorDb("Gameobject (GUID: %u Entry: %u) not created: it have not exist GO type '%u' in `gameobject_template`. It's will crash client if created.",guidlow,name_id,goinfo->type);
119 return false;
122 SetFloatValue(GAMEOBJECT_POS_X, x);
123 SetFloatValue(GAMEOBJECT_POS_Y, y);
124 SetFloatValue(GAMEOBJECT_POS_Z, z);
125 SetFloatValue(GAMEOBJECT_FACING, ang); //this is not facing angle
127 int64 rotation = 0;
129 float f_rot1 = sin(ang / 2.0f);
130 int64 i_rot1 = f_rot1 / atan(pow(2.0f, -20.0f));
131 rotation |= (i_rot1 << 43 >> 43) & 0x00000000001FFFFF;
133 //float f_rot2 = sin(0.0f / 2.0f);
134 //int64 i_rot2 = f_rot2 / atan(pow(2.0f, -20.0f));
135 //rotation |= (((i_rot2 << 22) >> 32) >> 11) & 0x000003FFFFE00000;
137 //float f_rot3 = sin(0.0f / 2.0f);
138 //int64 i_rot3 = f_rot3 / atan(pow(2.0f, -21.0f));
139 //rotation |= (i_rot3 >> 42) & 0x7FFFFC0000000000;
141 SetUInt64Value(GAMEOBJECT_ROTATION, rotation);
143 SetFloatValue(GAMEOBJECT_PARENTROTATION+0, rotation0);
144 SetFloatValue(GAMEOBJECT_PARENTROTATION+1, rotation1);
145 SetFloatValue(GAMEOBJECT_PARENTROTATION+2, rotation2);
146 SetFloatValue(GAMEOBJECT_PARENTROTATION+3, rotation3);
148 SetFloatValue(OBJECT_FIELD_SCALE_X, goinfo->size);
150 SetUInt32Value(GAMEOBJECT_FACTION, goinfo->faction);
151 SetUInt32Value(GAMEOBJECT_FLAGS, goinfo->flags);
153 SetEntry(goinfo->id);
155 SetUInt32Value(GAMEOBJECT_DISPLAYID, goinfo->displayId);
157 SetGoState(go_state);
158 SetGoType(GameobjectTypes(goinfo->type));
160 SetGoAnimProgress(animprogress);
162 // Spell charges for GAMEOBJECT_TYPE_SPELLCASTER (22)
163 if (goinfo->type == GAMEOBJECT_TYPE_SPELLCASTER)
164 m_charges = goinfo->spellcaster.charges;
166 //Notify the map's instance data.
167 //Only works if you create the object in it, not if it is moves to that map.
168 //Normally non-players do not teleport to other maps.
169 if(map->IsDungeon() && ((InstanceMap*)map)->GetInstanceData())
171 ((InstanceMap*)map)->GetInstanceData()->OnObjectCreate(this);
174 return true;
177 void GameObject::Update(uint32 /*p_time*/)
179 if (IS_MO_TRANSPORT(GetGUID()))
181 //((Transport*)this)->Update(p_time);
182 return;
185 switch (m_lootState)
187 case GO_NOT_READY:
189 switch(GetGoType())
191 case GAMEOBJECT_TYPE_TRAP:
193 // Arming Time for GAMEOBJECT_TYPE_TRAP (6)
194 Unit* owner = GetOwner();
195 if (owner && ((Player*)owner)->isInCombat())
196 m_cooldownTime = time(NULL) + GetGOInfo()->trap.startDelay;
197 m_lootState = GO_READY;
198 break;
200 case GAMEOBJECT_TYPE_FISHINGNODE:
202 // fishing code (bobber ready)
203 if( time(NULL) > m_respawnTime - FISHING_BOBBER_READY_TIME )
205 // splash bobber (bobber ready now)
206 Unit* caster = GetOwner();
207 if(caster && caster->GetTypeId()==TYPEID_PLAYER)
209 SetGoState(0);
210 SetUInt32Value(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
212 UpdateData udata;
213 WorldPacket packet;
214 BuildValuesUpdateBlockForPlayer(&udata,((Player*)caster));
215 udata.BuildPacket(&packet);
216 ((Player*)caster)->GetSession()->SendPacket(&packet);
218 WorldPacket data(SMSG_GAMEOBJECT_CUSTOM_ANIM,8+4);
219 data << GetGUID();
220 data << (uint32)(0);
221 ((Player*)caster)->SendMessageToSet(&data,true);
224 m_lootState = GO_READY; // can be successfully open with some chance
226 return;
228 default:
229 m_lootState = GO_READY; // for other GOis same switched without delay to GO_READY
230 break;
232 // NO BREAK for switch (m_lootState)
234 case GO_READY:
236 if (m_respawnTime > 0) // timer on
238 if (m_respawnTime <= time(NULL)) // timer expired
240 m_respawnTime = 0;
241 m_SkillupList.clear();
242 m_usetimes = 0;
244 switch (GetGoType())
246 case GAMEOBJECT_TYPE_FISHINGNODE: // can't fish now
248 Unit* caster = GetOwner();
249 if(caster && caster->GetTypeId()==TYPEID_PLAYER)
251 if(caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
253 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
254 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish(false);
257 WorldPacket data(SMSG_FISH_NOT_HOOKED,0);
258 ((Player*)caster)->GetSession()->SendPacket(&data);
260 // can be delete
261 m_lootState = GO_JUST_DEACTIVATED;
262 return;
264 case GAMEOBJECT_TYPE_DOOR:
265 case GAMEOBJECT_TYPE_BUTTON:
266 //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)
267 if( !GetGoState() )
268 SwitchDoorOrButton(false);
269 //flags in AB are type_button and we need to add them here so no break!
270 default:
271 if(!m_spawnedByDefault) // despawn timer
273 // can be despawned or destroyed
274 SetLootState(GO_JUST_DEACTIVATED);
275 return;
277 // respawn timer
278 GetMap()->Add(this);
279 break;
284 // traps can have time and can not have
285 GameObjectInfo const* goInfo = GetGOInfo();
286 if(goInfo->type == GAMEOBJECT_TYPE_TRAP)
288 // traps
289 Unit* owner = GetOwner();
290 Unit* ok = NULL; // pointer to appropriate target if found any
292 if(m_cooldownTime >= time(NULL))
293 return;
295 bool IsBattleGroundTrap = false;
296 //FIXME: this is activation radius (in different casting radius that must be selected from spell data)
297 //TODO: move activated state code (cast itself) to GO_ACTIVATED, in this place only check activating and set state
298 float radius = goInfo->trap.radius;
299 if(!radius)
301 if(goInfo->trap.cooldown != 3) // cast in other case (at some triggering/linked go/etc explicit call)
302 return;
303 else
305 if(m_respawnTime > 0)
306 break;
308 radius = goInfo->trap.cooldown; // battlegrounds gameobjects has data2 == 0 && data5 == 3
309 IsBattleGroundTrap = true;
313 bool NeedDespawn = (goInfo->trap.charges != 0);
315 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
316 Cell cell(p);
317 cell.data.Part.reserved = ALL_DISTRICT;
319 // Note: this hack with search required until GO casting not implemented
320 // search unfriendly creature
321 if(owner && NeedDespawn) // hunter trap
323 MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck u_check(this, owner, radius);
324 MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck> checker(this,ok, u_check);
326 CellLock<GridReadGuard> cell_lock(cell, p);
328 TypeContainerVisitor<MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck>, GridTypeMapContainer > grid_object_checker(checker);
329 cell_lock->Visit(cell_lock, grid_object_checker, *GetMap());
331 // or unfriendly player/pet
332 if(!ok)
334 TypeContainerVisitor<MaNGOS::UnitSearcher<MaNGOS::AnyUnfriendlyUnitInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
335 cell_lock->Visit(cell_lock, world_object_checker, *GetMap());
338 else // environmental trap
340 // environmental damage spells already have around enemies targeting but this not help in case not existed GO casting support
342 // affect only players
343 Player* p_ok = NULL;
344 MaNGOS::AnyPlayerInObjectRangeCheck p_check(this, radius);
345 MaNGOS::PlayerSearcher<MaNGOS::AnyPlayerInObjectRangeCheck> checker(this,p_ok, p_check);
347 CellLock<GridReadGuard> cell_lock(cell, p);
349 TypeContainerVisitor<MaNGOS::PlayerSearcher<MaNGOS::AnyPlayerInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
350 cell_lock->Visit(cell_lock, world_object_checker, *GetMap());
351 ok = p_ok;
354 if (ok)
356 Unit *caster = owner ? owner : ok;
358 caster->CastSpell(ok, goInfo->trap.spellId, true, 0, 0, GetGUID());
359 m_cooldownTime = time(NULL) + 4; // 4 seconds
361 if(NeedDespawn)
362 SetLootState(GO_JUST_DEACTIVATED); // can be despawned or destroyed
364 if(IsBattleGroundTrap && ok->GetTypeId() == TYPEID_PLAYER)
366 //BattleGround gameobjects case
367 if(((Player*)ok)->InBattleGround())
368 if(BattleGround *bg = ((Player*)ok)->GetBattleGround())
369 bg->HandleTriggerBuff(GetGUID());
374 if (m_charges && m_usetimes >= m_charges)
375 SetLootState(GO_JUST_DEACTIVATED); // can be despawned or destroyed
377 break;
379 case GO_ACTIVATED:
381 switch(GetGoType())
383 case GAMEOBJECT_TYPE_DOOR:
384 case GAMEOBJECT_TYPE_BUTTON:
385 if(GetAutoCloseTime() && (m_cooldownTime < time(NULL)))
387 SwitchDoorOrButton(false);
388 SetLootState(GO_JUST_DEACTIVATED);
390 break;
392 break;
394 case GO_JUST_DEACTIVATED:
396 //if Gameobject should cast spell, then this, but some GOs (type = 10) should be destroyed
397 if (GetGoType() == GAMEOBJECT_TYPE_GOOBER)
399 uint32 spellId = GetGOInfo()->goober.spellId;
401 if(spellId)
403 std::set<uint32>::iterator it = m_unique_users.begin();
404 std::set<uint32>::iterator end = m_unique_users.end();
405 for (; it != end; it++)
407 Unit* owner = Unit::GetUnit(*this, uint64(*it));
408 if (owner) owner->CastSpell(owner, spellId, false, 0, 0, GetGUID());
411 m_unique_users.clear();
412 m_usetimes = 0;
414 //any return here in case battleground traps
417 if(GetOwnerGUID())
419 m_respawnTime = 0;
420 Delete();
421 return;
424 //burning flags in some battlegrounds, if you find better condition, just add it
425 if (GetGoAnimProgress() > 0)
427 SendObjectDeSpawnAnim(GetGUID());
428 //reset flags
429 SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
432 loot.clear();
433 SetLootState(GO_READY);
435 if(!m_respawnDelayTime)
436 return;
438 if(!m_spawnedByDefault)
440 m_respawnTime = 0;
441 return;
444 m_respawnTime = time(NULL) + m_respawnDelayTime;
446 // if option not set then object will be saved at grid unload
447 if(sWorld.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY))
448 SaveRespawnTime();
450 ObjectAccessor::UpdateObjectVisibility(this);
452 break;
457 void GameObject::Refresh()
459 // not refresh despawned not casted GO (despawned casted GO destroyed in all cases anyway)
460 if(m_respawnTime > 0 && m_spawnedByDefault)
461 return;
463 if(isSpawned())
464 GetMap()->Add(this);
467 void GameObject::AddUniqueUse(Player* player)
469 AddUse();
470 m_unique_users.insert(player->GetGUIDLow());
473 void GameObject::Delete()
475 SendObjectDeSpawnAnim(GetGUID());
477 SetGoState(1);
478 SetUInt32Value(GAMEOBJECT_FLAGS, GetGOInfo()->flags);
480 AddObjectToRemoveList();
483 void GameObject::getFishLoot(Loot *fishloot, Player* loot_owner)
485 fishloot->clear();
487 uint32 subzone = GetAreaId();
489 // if subzone loot exist use it
490 if(LootTemplates_Fishing.HaveLootFor(subzone))
491 fishloot->FillLoot(subzone, LootTemplates_Fishing, loot_owner,true);
492 // else use zone loot
493 else
494 fishloot->FillLoot(GetZoneId(), LootTemplates_Fishing, loot_owner,true);
497 void GameObject::SaveToDB()
499 // this should only be used when the gameobject has already been loaded
500 // preferably after adding to map, because mapid may not be valid otherwise
501 GameObjectData const *data = objmgr.GetGOData(m_DBTableGuid);
502 if(!data)
504 sLog.outError("GameObject::SaveToDB failed, cannot get gameobject data!");
505 return;
508 SaveToDB(GetMapId(), data->spawnMask, data->phaseMask);
511 void GameObject::SaveToDB(uint32 mapid, uint8 spawnMask, uint32 phaseMask)
513 const GameObjectInfo *goI = GetGOInfo();
515 if (!goI)
516 return;
518 if (!m_DBTableGuid)
519 m_DBTableGuid = GetGUIDLow();
520 // update in loaded data (changing data only in this place)
521 GameObjectData& data = objmgr.NewGOData(m_DBTableGuid);
523 // data->guid = guid don't must be update at save
524 data.id = GetEntry();
525 data.mapid = mapid;
526 data.phaseMask = phaseMask;
527 data.posX = GetFloatValue(GAMEOBJECT_POS_X);
528 data.posY = GetFloatValue(GAMEOBJECT_POS_Y);
529 data.posZ = GetFloatValue(GAMEOBJECT_POS_Z);
530 data.orientation = GetFloatValue(GAMEOBJECT_FACING);
531 data.rotation0 = GetFloatValue(GAMEOBJECT_PARENTROTATION+0);
532 data.rotation1 = GetFloatValue(GAMEOBJECT_PARENTROTATION+1);
533 data.rotation2 = GetFloatValue(GAMEOBJECT_PARENTROTATION+2);
534 data.rotation3 = GetFloatValue(GAMEOBJECT_PARENTROTATION+3);
535 data.spawntimesecs = m_spawnedByDefault ? m_respawnDelayTime : -(int32)m_respawnDelayTime;
536 data.animprogress = GetGoAnimProgress();
537 data.go_state = GetGoState();
538 data.spawnMask = spawnMask;
540 // updated in DB
541 std::ostringstream ss;
542 ss << "INSERT INTO gameobject VALUES ( "
543 << m_DBTableGuid << ", "
544 << GetEntry() << ", "
545 << mapid << ", "
546 << (uint32)spawnMask << ", "
547 << (uint32)GetPhaseMask() << ","
548 << GetFloatValue(GAMEOBJECT_POS_X) << ", "
549 << GetFloatValue(GAMEOBJECT_POS_Y) << ", "
550 << GetFloatValue(GAMEOBJECT_POS_Z) << ", "
551 << GetFloatValue(GAMEOBJECT_FACING) << ", "
552 << GetFloatValue(GAMEOBJECT_PARENTROTATION) << ", "
553 << GetFloatValue(GAMEOBJECT_PARENTROTATION+1) << ", "
554 << GetFloatValue(GAMEOBJECT_PARENTROTATION+2) << ", "
555 << GetFloatValue(GAMEOBJECT_PARENTROTATION+3) << ", "
556 << m_respawnDelayTime << ", "
557 << (uint32)GetGoAnimProgress() << ", "
558 << (uint32)GetGoState() << ")";
560 WorldDatabase.BeginTransaction();
561 WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid);
562 WorldDatabase.PExecuteLog( ss.str( ).c_str( ) );
563 WorldDatabase.CommitTransaction();
566 bool GameObject::LoadFromDB(uint32 guid, Map *map)
568 GameObjectData const* data = objmgr.GetGOData(guid);
570 if( !data )
572 sLog.outErrorDb("ERROR: Gameobject (GUID: %u) not found in table `gameobject`, can't load. ",guid);
573 return false;
576 uint32 entry = data->id;
577 //uint32 map_id = data->mapid; // already used before call
578 uint32 phaseMask = data->phaseMask;
579 float x = data->posX;
580 float y = data->posY;
581 float z = data->posZ;
582 float ang = data->orientation;
584 float rotation0 = data->rotation0;
585 float rotation1 = data->rotation1;
586 float rotation2 = data->rotation2;
587 float rotation3 = data->rotation3;
589 uint32 animprogress = data->animprogress;
590 uint32 go_state = data->go_state;
592 m_DBTableGuid = guid;
593 if (map->GetInstanceId() != 0) guid = objmgr.GenerateLowGuid(HIGHGUID_GAMEOBJECT);
595 if (!Create(guid,entry, map, phaseMask, x, y, z, ang, rotation0, rotation1, rotation2, rotation3, animprogress, go_state) )
596 return false;
598 switch(GetGOInfo()->type)
600 case GAMEOBJECT_TYPE_DOOR:
601 case GAMEOBJECT_TYPE_BUTTON:
602 /* this code (in comment) isn't correct because in battlegrounds we need despawnable doors and buttons, pls remove
603 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_NODESPAWN);
604 m_spawnedByDefault = true;
605 m_respawnDelayTime = 0;
606 m_respawnTime = 0;
607 break;*/
608 default:
609 if(data->spawntimesecs >= 0)
611 m_spawnedByDefault = true;
612 m_respawnDelayTime = data->spawntimesecs;
613 m_respawnTime = objmgr.GetGORespawnTime(m_DBTableGuid, map->GetInstanceId());
615 // ready to respawn
616 if(m_respawnTime && m_respawnTime <= time(NULL))
618 m_respawnTime = 0;
619 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
622 else
624 m_spawnedByDefault = false;
625 m_respawnDelayTime = -data->spawntimesecs;
626 m_respawnTime = 0;
628 break;
631 return true;
634 void GameObject::DeleteFromDB()
636 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
637 objmgr.DeleteGOData(m_DBTableGuid);
638 WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid);
639 WorldDatabase.PExecuteLog("DELETE FROM game_event_gameobject WHERE guid = '%u'", m_DBTableGuid);
642 GameObject* GameObject::GetGameObject(WorldObject& object, uint64 guid)
644 return ObjectAccessor::GetGameObject(object,guid);
647 GameObjectInfo const *GameObject::GetGOInfo() const
649 return m_goInfo;
652 uint32 GameObject::GetLootId(GameObjectInfo const* ginfo)
654 if (!ginfo)
655 return 0;
657 switch(ginfo->type)
659 case GAMEOBJECT_TYPE_CHEST:
660 return ginfo->chest.lootId;
661 case GAMEOBJECT_TYPE_FISHINGHOLE:
662 return ginfo->fishinghole.lootId;
663 case GAMEOBJECT_TYPE_FISHINGNODE:
664 return ginfo->fishnode.lootId;
665 default:
666 return 0;
670 /*********************************************************/
671 /*** QUEST SYSTEM ***/
672 /*********************************************************/
673 bool GameObject::hasQuest(uint32 quest_id) const
675 QuestRelations const& qr = objmgr.mGOQuestRelations;
676 for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr)
678 if(itr->second==quest_id)
679 return true;
681 return false;
684 bool GameObject::hasInvolvedQuest(uint32 quest_id) const
686 QuestRelations const& qr = objmgr.mGOQuestInvolvedRelations;
687 for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr)
689 if(itr->second==quest_id)
690 return true;
692 return false;
695 bool GameObject::IsTransport() const
697 // If something is marked as a transport, don't transmit an out of range packet for it.
698 GameObjectInfo const * gInfo = GetGOInfo();
699 if(!gInfo) return false;
700 return gInfo->type == GAMEOBJECT_TYPE_TRANSPORT || gInfo->type == GAMEOBJECT_TYPE_MO_TRANSPORT;
703 Unit* GameObject::GetOwner() const
705 return ObjectAccessor::GetUnit(*this, GetOwnerGUID());
708 void GameObject::SaveRespawnTime()
710 if(m_respawnTime > time(NULL) && m_spawnedByDefault)
711 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),m_respawnTime);
714 bool GameObject::isVisibleForInState(Player const* u, bool inVisibleList) const
716 // Not in world
717 if(!IsInWorld() || !u->IsInWorld())
718 return false;
720 // Transport always visible at this step implementation
721 if(IsTransport() && IsInMap(u))
722 return true;
724 // quick check visibility false cases for non-GM-mode
725 if(!u->isGameMaster())
727 // despawned and then not visible for non-GM in GM-mode
728 if(!isSpawned())
729 return false;
731 // special invisibility cases
732 /* TODO: implement trap stealth, take look at spell 2836
733 if(GetGOInfo()->type == GAMEOBJECT_TYPE_TRAP && GetGOInfo()->trap.stealthed && u->IsHostileTo(GetOwner()))
735 if(check stuff here)
736 return false;
740 // check distance
741 return IsWithinDistInMap(u,World::GetMaxVisibleDistanceForObject() +
742 (inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false);
745 void GameObject::Respawn()
747 if(m_spawnedByDefault && m_respawnTime > 0)
749 m_respawnTime = time(NULL);
750 objmgr.SaveGORespawnTime(m_DBTableGuid,GetInstanceId(),0);
754 bool GameObject::ActivateToQuest( Player *pTarget)const
756 if(!objmgr.IsGameObjectForQuests(GetEntry()))
757 return false;
759 switch(GetGoType())
761 // scan GO chest with loot including quest items
762 case GAMEOBJECT_TYPE_CHEST:
764 if(LootTemplates_Gameobject.HaveQuestLootForPlayer(GetLootId(), pTarget))
765 return true;
766 break;
768 case GAMEOBJECT_TYPE_GOOBER:
770 if(pTarget->GetQuestStatus(GetGOInfo()->goober.questId) == QUEST_STATUS_INCOMPLETE)
771 return true;
772 break;
774 default:
775 break;
778 return false;
781 void GameObject::TriggeringLinkedGameObject( uint32 trapEntry, Unit* target)
783 GameObjectInfo const* trapInfo = sGOStorage.LookupEntry<GameObjectInfo>(trapEntry);
784 if(!trapInfo || trapInfo->type!=GAMEOBJECT_TYPE_TRAP)
785 return;
787 SpellEntry const* trapSpell = sSpellStore.LookupEntry(trapInfo->trap.spellId);
788 if(!trapSpell) // checked at load already
789 return;
791 float range = GetSpellMaxRange(sSpellRangeStore.LookupEntry(trapSpell->rangeIndex));
793 // search nearest linked GO
794 GameObject* trapGO = NULL;
796 // using original GO distance
797 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
798 Cell cell(p);
799 cell.data.Part.reserved = ALL_DISTRICT;
801 MaNGOS::NearestGameObjectEntryInObjectRangeCheck go_check(*target,trapEntry,range);
802 MaNGOS::GameObjectLastSearcher<MaNGOS::NearestGameObjectEntryInObjectRangeCheck> checker(this, trapGO,go_check);
804 TypeContainerVisitor<MaNGOS::GameObjectLastSearcher<MaNGOS::NearestGameObjectEntryInObjectRangeCheck>, GridTypeMapContainer > object_checker(checker);
805 CellLock<GridReadGuard> cell_lock(cell, p);
806 cell_lock->Visit(cell_lock, object_checker, *GetMap());
809 // found correct GO
810 // FIXME: when GO casting will be implemented trap must cast spell to target
811 if(trapGO)
812 target->CastSpell(target,trapSpell,true, 0, 0, GetGUID());
815 GameObject* GameObject::LookupFishingHoleAround(float range)
817 GameObject* ok = NULL;
819 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(),GetPositionY()));
820 Cell cell(p);
821 cell.data.Part.reserved = ALL_DISTRICT;
822 MaNGOS::NearestGameObjectFishingHole u_check(*this, range);
823 MaNGOS::GameObjectSearcher<MaNGOS::NearestGameObjectFishingHole> checker(this, ok, u_check);
825 CellLock<GridReadGuard> cell_lock(cell, p);
827 TypeContainerVisitor<MaNGOS::GameObjectSearcher<MaNGOS::NearestGameObjectFishingHole>, GridTypeMapContainer > grid_object_checker(checker);
828 cell_lock->Visit(cell_lock, grid_object_checker, *GetMap());
830 return ok;
833 void GameObject::UseDoorOrButton(uint32 time_to_restore)
835 if(m_lootState != GO_READY)
836 return;
838 if(!time_to_restore)
839 time_to_restore = GetAutoCloseTime();
841 SwitchDoorOrButton(true);
842 SetLootState(GO_ACTIVATED);
844 m_cooldownTime = time(NULL) + time_to_restore;
848 void GameObject::SwitchDoorOrButton(bool activate)
850 if(activate)
851 SetFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
852 else
853 RemoveFlag(GAMEOBJECT_FLAGS, GO_FLAG_IN_USE);
855 if(GetGoState()) //if closed -> open
856 SetGoState(0);
857 else //if open -> close
858 SetGoState(1);
861 void GameObject::Use(Unit* user)
863 // by default spell caster is user
864 Unit* spellCaster = user;
865 uint32 spellId = 0;
867 switch(GetGoType())
869 case GAMEOBJECT_TYPE_DOOR: //0
870 case GAMEOBJECT_TYPE_BUTTON: //1
871 //doors/buttons never really despawn, only reset to default state/flags
872 UseDoorOrButton();
874 // activate script
875 sWorld.ScriptsStart(sGameObjectScripts, GetDBTableGUIDLow(), spellCaster, this);
876 return;
878 case GAMEOBJECT_TYPE_QUESTGIVER: //2
880 if(user->GetTypeId()!=TYPEID_PLAYER)
881 return;
883 Player* player = (Player*)user;
885 player->PrepareQuestMenu( GetGUID() );
886 player->SendPreparedQuest( GetGUID() );
887 return;
889 //Sitting: Wooden bench, chairs enzz
890 case GAMEOBJECT_TYPE_CHAIR: //7
892 GameObjectInfo const* info = GetGOInfo();
893 if(!info)
894 return;
896 if(user->GetTypeId()!=TYPEID_PLAYER)
897 return;
899 Player* player = (Player*)user;
901 // a chair may have n slots. we have to calculate their positions and teleport the player to the nearest one
903 // check if the db is sane
904 if(info->chair.slots > 0)
906 float lowestDist = DEFAULT_VISIBILITY_DISTANCE;
908 float x_lowest = GetPositionX();
909 float y_lowest = GetPositionY();
911 // the object orientation + 1/2 pi
912 // every slot will be on that straight line
913 float orthogonalOrientation = GetOrientation()+M_PI*0.5f;
914 // find nearest slot
915 for(uint32 i=0; i<info->chair.slots; i++)
917 // the distance between this slot and the center of the go - imagine a 1D space
918 float relativeDistance = (info->size*i)-(info->size*(info->chair.slots-1)/2.0f);
920 float x_i = GetPositionX() + relativeDistance * cos(orthogonalOrientation);
921 float y_i = GetPositionY() + relativeDistance * sin(orthogonalOrientation);
923 // calculate the distance between the player and this slot
924 float thisDistance = player->GetDistance2d(x_i, y_i);
926 /* debug code. It will spawn a npc on each slot to visualize them.
927 Creature* helper = player->SummonCreature(14496, x_i, y_i, GetPositionZ(), GetOrientation(), TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, 10000);
928 std::ostringstream output;
929 output << i << ": thisDist: " << thisDistance;
930 helper->MonsterSay(output.str().c_str(), LANG_UNIVERSAL, 0);
933 if(thisDistance <= lowestDist)
935 lowestDist = thisDistance;
936 x_lowest = x_i;
937 y_lowest = y_i;
940 player->TeleportTo(GetMapId(), x_lowest, y_lowest, GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
942 else
944 // fallback, will always work
945 player->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
947 player->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR+info->chair.height);
948 return;
950 //big gun, its a spell/aura
951 case GAMEOBJECT_TYPE_GOOBER: //10
953 GameObjectInfo const* info = GetGOInfo();
955 if(user->GetTypeId()==TYPEID_PLAYER)
957 Player* player = (Player*)user;
959 // show page
960 if(info->goober.pageId)
962 WorldPacket data(SMSG_GAMEOBJECT_PAGETEXT, 8);
963 data << GetGUID();
964 player->GetSession()->SendPacket(&data);
967 // possible quest objective for active quests
968 player->CastedCreatureOrGO(info->id, GetGUID(), 0);
971 // cast this spell later if provided
972 spellId = info->goober.spellId;
974 break;
976 case GAMEOBJECT_TYPE_CAMERA: //13
978 GameObjectInfo const* info = GetGOInfo();
979 if(!info)
980 return;
982 if(user->GetTypeId()!=TYPEID_PLAYER)
983 return;
985 Player* player = (Player*)user;
987 if(info->camera.cinematicId)
989 WorldPacket data(SMSG_TRIGGER_CINEMATIC, 4);
990 data << info->camera.cinematicId;
991 player->GetSession()->SendPacket(&data);
993 return;
995 //fishing bobber
996 case GAMEOBJECT_TYPE_FISHINGNODE: //17
998 if(user->GetTypeId()!=TYPEID_PLAYER)
999 return;
1001 Player* player = (Player*)user;
1003 if(player->GetGUID() != GetOwnerGUID())
1004 return;
1006 switch(getLootState())
1008 case GO_READY: // ready for loot
1010 // 1) skill must be >= base_zone_skill
1011 // 2) if skill == base_zone_skill => 5% chance
1012 // 3) chance is linear dependence from (base_zone_skill-skill)
1014 uint32 subzone = GetAreaId();
1016 int32 zone_skill = objmgr.GetFishingBaseSkillLevel( subzone );
1017 if(!zone_skill)
1018 zone_skill = objmgr.GetFishingBaseSkillLevel( GetZoneId() );
1020 //provide error, no fishable zone or area should be 0
1021 if(!zone_skill)
1022 sLog.outErrorDb("Fishable areaId %u are not properly defined in `skill_fishing_base_level`.",subzone);
1024 int32 skill = player->GetSkillValue(SKILL_FISHING);
1025 int32 chance = skill - zone_skill + 5;
1026 int32 roll = irand(1,100);
1028 DEBUG_LOG("Fishing check (skill: %i zone min skill: %i chance %i roll: %i",skill,zone_skill,chance,roll);
1030 if(skill >= zone_skill && chance >= roll)
1032 // prevent removing GO at spell cancel
1033 player->RemoveGameObject(this,false);
1034 SetOwnerGUID(player->GetGUID());
1036 //fish catched
1037 player->UpdateFishingSkill();
1039 GameObject* ok = LookupFishingHoleAround(DEFAULT_VISIBILITY_DISTANCE);
1040 if (ok)
1042 player->SendLoot(ok->GetGUID(),LOOT_FISHINGHOLE);
1043 SetLootState(GO_JUST_DEACTIVATED);
1045 else
1046 player->SendLoot(GetGUID(),LOOT_FISHING);
1048 else
1050 // fish escaped, can be deleted now
1051 SetLootState(GO_JUST_DEACTIVATED);
1053 WorldPacket data(SMSG_FISH_ESCAPED, 0);
1054 player->GetSession()->SendPacket(&data);
1056 break;
1058 case GO_JUST_DEACTIVATED: // nothing to do, will be deleted at next update
1059 break;
1060 default:
1062 SetLootState(GO_JUST_DEACTIVATED);
1064 WorldPacket data(SMSG_FISH_NOT_HOOKED, 0);
1065 player->GetSession()->SendPacket(&data);
1066 break;
1070 if(player->m_currentSpells[CURRENT_CHANNELED_SPELL])
1072 player->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
1073 player->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
1075 return;
1078 case GAMEOBJECT_TYPE_SUMMONING_RITUAL: //18
1080 if(user->GetTypeId()!=TYPEID_PLAYER)
1081 return;
1083 Player* player = (Player*)user;
1085 Unit* caster = GetOwner();
1087 GameObjectInfo const* info = GetGOInfo();
1089 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1090 return;
1092 // accept only use by player from same group for caster except caster itself
1093 if(((Player*)caster)==player || !((Player*)caster)->IsInSameRaidWith(player))
1094 return;
1096 AddUniqueUse(player);
1098 // full amount unique participants including original summoner
1099 if(GetUniqueUseCount() < info->summoningRitual.reqParticipants)
1100 return;
1102 // in case summoning ritual caster is GO creator
1103 spellCaster = caster;
1105 if(!caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
1106 return;
1108 spellId = info->summoningRitual.spellId;
1110 // finish spell
1111 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
1112 caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
1114 // can be deleted now
1115 SetLootState(GO_JUST_DEACTIVATED);
1117 // go to end function to spell casting
1118 break;
1120 case GAMEOBJECT_TYPE_SPELLCASTER: //22
1122 SetUInt32Value(GAMEOBJECT_FLAGS,2);
1124 GameObjectInfo const* info = GetGOInfo();
1125 if(!info)
1126 return;
1128 if(info->spellcaster.partyOnly)
1130 Unit* caster = GetOwner();
1131 if( !caster || caster->GetTypeId()!=TYPEID_PLAYER )
1132 return;
1134 if(user->GetTypeId()!=TYPEID_PLAYER || !((Player*)user)->IsInSameRaidWith((Player*)caster))
1135 return;
1138 spellId = info->spellcaster.spellId;
1140 AddUse();
1141 break;
1143 case GAMEOBJECT_TYPE_MEETINGSTONE: //23
1145 GameObjectInfo const* info = GetGOInfo();
1147 if(user->GetTypeId()!=TYPEID_PLAYER)
1148 return;
1150 Player* player = (Player*)user;
1152 Player* targetPlayer = ObjectAccessor::FindPlayer(player->GetSelection());
1154 // accept only use by player from same group for caster except caster itself
1155 if(!targetPlayer || targetPlayer == player || !targetPlayer->IsInSameGroupWith(player))
1156 return;
1158 //required lvl checks!
1159 uint8 level = player->getLevel();
1160 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1161 return;
1162 level = targetPlayer->getLevel();
1163 if (level < info->meetingstone.minLevel || level > info->meetingstone.maxLevel)
1164 return;
1166 spellId = 23598;
1168 break;
1171 case GAMEOBJECT_TYPE_FLAGSTAND: // 24
1173 if(user->GetTypeId()!=TYPEID_PLAYER)
1174 return;
1176 Player* player = (Player*)user;
1178 if( player->isAllowUseBattleGroundObject() )
1180 // in battleground check
1181 BattleGround *bg = player->GetBattleGround();
1182 if(!bg)
1183 return;
1184 // BG flag click
1185 // AB:
1186 // 15001
1187 // 15002
1188 // 15003
1189 // 15004
1190 // 15005
1191 bg->EventPlayerClickedOnFlag(player, this);
1192 return; //we don;t need to delete flag ... it is despawned!
1194 break;
1196 case GAMEOBJECT_TYPE_FLAGDROP: // 26
1198 if(user->GetTypeId()!=TYPEID_PLAYER)
1199 return;
1201 Player* player = (Player*)user;
1203 if( player->isAllowUseBattleGroundObject() )
1205 // in battleground check
1206 BattleGround *bg = player->GetBattleGround();
1207 if(!bg)
1208 return;
1209 // BG flag dropped
1210 // WS:
1211 // 179785 - Silverwing Flag
1212 // 179786 - Warsong Flag
1213 // EotS:
1214 // 184142 - Netherstorm Flag
1215 GameObjectInfo const* info = GetGOInfo();
1216 if(info)
1218 switch(info->id)
1220 case 179785: // Silverwing Flag
1221 // check if it's correct bg
1222 if(bg->GetTypeID() == BATTLEGROUND_WS)
1223 bg->EventPlayerClickedOnFlag(player, this);
1224 break;
1225 case 179786: // Warsong Flag
1226 if(bg->GetTypeID() == BATTLEGROUND_WS)
1227 bg->EventPlayerClickedOnFlag(player, this);
1228 break;
1229 case 184142: // Netherstorm Flag
1230 if(bg->GetTypeID() == BATTLEGROUND_EY)
1231 bg->EventPlayerClickedOnFlag(player, this);
1232 break;
1235 //this cause to call return, all flags must be deleted here!!
1236 spellId = 0;
1237 Delete();
1239 break;
1241 case GAMEOBJECT_TYPE_BARBER_CHAIR: //32
1243 GameObjectInfo const* info = GetGOInfo();
1244 if(!info)
1245 return;
1247 if(user->GetTypeId()!=TYPEID_PLAYER)
1248 return;
1250 Player* player = (Player*)user;
1252 // fallback, will always work
1253 player->TeleportTo(GetMapId(), GetPositionX(), GetPositionY(), GetPositionZ(), GetOrientation(),TELE_TO_NOT_LEAVE_TRANSPORT | TELE_TO_NOT_LEAVE_COMBAT | TELE_TO_NOT_UNSUMMON_PET);
1255 WorldPacket data(SMSG_ENABLE_BARBER_SHOP, 0);
1256 player->GetSession()->SendPacket(&data);
1258 player->SetStandState(UNIT_STAND_STATE_SIT_LOW_CHAIR+info->barberChair.chairheight);
1259 return;
1261 default:
1262 sLog.outDebug("Unknown Object Type %u", GetGoType());
1263 break;
1266 if(!spellId)
1267 return;
1269 SpellEntry const *spellInfo = sSpellStore.LookupEntry( spellId );
1270 if(!spellInfo)
1272 sLog.outError("WORLD: unknown spell id %u at use action for gameobject (Entry: %u GoType: %u )", spellId,GetEntry(),GetGoType());
1273 return;
1276 Spell *spell = new Spell(spellCaster, spellInfo, false);
1278 // spell target is user of GO
1279 SpellCastTargets targets;
1280 targets.setUnitTarget( user );
1282 spell->prepare(&targets);
1285 // overwrite WorldObject function for proper name localization
1286 const char* GameObject::GetNameForLocaleIdx(int32 loc_idx) const
1288 if (loc_idx >= 0)
1290 GameObjectLocale const *cl = objmgr.GetGameObjectLocale(GetEntry());
1291 if (cl)
1293 if (cl->Name.size() > loc_idx && !cl->Name[loc_idx].empty())
1294 return cl->Name[loc_idx].c_str();
1298 return GetName();