TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
InstanceScript.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_INSTANCE_DATA_H
20 #define TRINITY_INSTANCE_DATA_H
21 
22 #include <set>
23 #include "ZoneScript.h"
24 #include "World.h"
25 #include "ObjectMgr.h"
26 #include "CreatureAI.h"
28 
29 #define OUT_SAVE_INST_DATA TC_LOG_DEBUG("scripts", "Saving Instance Data for Instance %s (Map %d, Instance Id %d)", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
30 #define OUT_SAVE_INST_DATA_COMPLETE TC_LOG_DEBUG("scripts", "Saving Instance Data for Instance %s (Map %d, Instance Id %d) completed.", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
31 #define OUT_LOAD_INST_DATA(a) TC_LOG_DEBUG("scripts", "Loading Instance Data for Instance %s (Map %d, Instance Id %d). Input is '%s'", instance->GetMapName(), instance->GetId(), instance->GetInstanceId(), a)
32 #define OUT_LOAD_INST_DATA_COMPLETE TC_LOG_DEBUG("scripts", "Instance Data Load for Instance %s (Map %d, Instance Id: %d) is complete.", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
33 #define OUT_LOAD_INST_DATA_FAIL TC_LOG_ERROR("scripts", "Unable to load Instance Data for Instance %s (Map %d, Instance Id: %d).", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
34 
35 class Map;
36 class Unit;
37 class Player;
38 class GameObject;
39 class Creature;
40 
42 {
52  ENCOUNTER_FRAME_UNK7 = 9, // Seems to have something to do with sorting the encounter units
54 };
55 
57 {
60  FAIL = 2,
61  DONE = 3,
62  SPECIAL = 4,
64 };
65 
67 {
68  DOOR_TYPE_ROOM = 0, // Door can open if encounter is not in progress
69  DOOR_TYPE_PASSAGE = 1, // Door can open if encounter is done
70  DOOR_TYPE_SPAWN_HOLE = 2, // Door can open if encounter is in progress, typically used for spawning places
72 };
73 
74 struct DoorData
75 {
78 };
79 
81 {
84 };
85 
87 {
88  typedef std::vector<BossBoundaryEntry> StorageType;
89  typedef StorageType::const_iterator const_iterator;
90 
91  BossBoundaryData(std::initializer_list<BossBoundaryEntry> data) : _data(data) { }
93  const_iterator begin() const { return _data.begin(); }
94  const_iterator end() const { return _data.end(); }
95 
96  private:
97  StorageType _data;
98 };
99 
101 {
103 };
104 
106 {
109 };
110 
111 struct BossInfo
112 {
118 };
119 
120 struct DoorInfo
121 {
122  explicit DoorInfo(BossInfo* _bossInfo, DoorType _type)
123  : bossInfo(_bossInfo), type(_type) { }
126 };
127 
129 {
130  explicit MinionInfo(BossInfo* _bossInfo) : bossInfo(_bossInfo) { }
132 };
133 
134 typedef std::multimap<uint32 /*entry*/, DoorInfo> DoorInfoMap;
135 typedef std::pair<DoorInfoMap::const_iterator, DoorInfoMap::const_iterator> DoorInfoMapBounds;
136 
137 typedef std::map<uint32 /*entry*/, MinionInfo> MinionInfoMap;
138 typedef std::map<uint32 /*type*/, ObjectGuid /*guid*/> ObjectGuidMap;
139 typedef std::map<uint32 /*entry*/, uint32 /*type*/> ObjectInfoMap;
140 
142 {
143  public:
144  explicit InstanceScript(Map* map) : instance(map), completedEncounters(0) { }
145 
146  virtual ~InstanceScript() { }
147 
149 
150  // On creation, NOT load.
151  // PLEASE INITIALIZE FIELDS IN THE CONSTRUCTOR INSTEAD !!!
152  // KEEPING THIS METHOD ONLY FOR BACKWARD COMPATIBILITY !!!
153  virtual void Initialize() { }
154 
155  // On load
156  virtual void Load(char const* data);
157 
158  // When save is needed, this function generates the data
159  virtual std::string GetSaveData();
160 
161  void SaveToDB();
162 
163  virtual void Update(uint32 /*diff*/) { }
164 
165  // Used by the map's CannotEnter function.
166  // This is to prevent players from entering during boss encounters.
167  virtual bool IsEncounterInProgress() const;
168 
169  // Called when a creature/gameobject is added to map or removed from map.
170  // Insert/Remove objectguid to dynamic guid store
171  virtual void OnCreatureCreate(Creature* creature) override;
172  virtual void OnCreatureRemove(Creature* creature) override;
173 
174  virtual void OnGameObjectCreate(GameObject* go) override;
175  virtual void OnGameObjectRemove(GameObject* go) override;
176 
177  ObjectGuid GetObjectGuid(uint32 type) const;
178  virtual ObjectGuid GetGuidData(uint32 type) const override;
179 
181  {
182  return instance->GetCreature(GetObjectGuid(type));
183  }
185  {
186  return instance->GetGameObject(GetObjectGuid(type));
187  }
188 
189  // Called when a player successfully enters the instance.
190  virtual void OnPlayerEnter(Player* /*player*/) { }
191 
192  // Handle open / close objects
193  // * use HandleGameObject(0, boolen, GO); in OnObjectCreate in instance scripts
194  // * use HandleGameObject(GUID, boolen, NULL); in any other script
195  void HandleGameObject(ObjectGuid guid, bool open, GameObject* go = nullptr);
196 
197  // Change active state of doors or buttons
198  void DoUseDoorOrButton(ObjectGuid guid, uint32 withRestoreTime = 0, bool useAlternativeState = false);
199  void DoCloseDoorOrButton(ObjectGuid guid);
200 
201  // Respawns a GO having negative spawntimesecs in gameobject-table
202  void DoRespawnGameObject(ObjectGuid guid, uint32 timeToDespawn = MINUTE);
203 
204  // Sends world state update to all players in instance
205  void DoUpdateWorldState(uint32 worldstateId, uint32 worldstateValue);
206 
207  // Send Notify to all players in instance
208  void DoSendNotifyToInstance(char const* format, ...);
209 
210  // Update Achievement Criteria for all players in instance
211  void DoUpdateAchievementCriteria(AchievementCriteriaTypes type, uint32 miscValue1 = 0, uint32 miscValue2 = 0, Unit* unit = NULL);
212 
213  // Start/Stop Timed Achievement Criteria for all players in instance
214  void DoStartTimedAchievement(AchievementCriteriaTimedTypes type, uint32 entry);
215  void DoStopTimedAchievement(AchievementCriteriaTimedTypes type, uint32 entry);
216 
217  // Remove Auras due to Spell on all players in instance
218  void DoRemoveAurasDueToSpellOnPlayers(uint32 spell);
219 
220  // Cast spell on all players in instance
221  void DoCastSpellOnPlayers(uint32 spell);
222 
223  // Return wether server allow two side groups or not
225 
226  virtual bool SetBossState(uint32 id, EncounterState state);
227  EncounterState GetBossState(uint32 id) const { return id < bosses.size() ? bosses[id].state : TO_BE_DECIDED; }
228  static std::string GetBossStateName(uint8 state);
229  CreatureBoundary const* GetBossBoundary(uint32 id) const { return id < bosses.size() ? &bosses[id].boundary : NULL; }
230 
231  // Achievement criteria additional requirements check
232  // NOTE: not use this if same can be checked existed requirement types from AchievementCriteriaRequirementType
233  virtual bool CheckAchievementCriteriaMeet(uint32 /*criteria_id*/, Player const* /*source*/, Unit const* /*target*/ = NULL, uint32 /*miscvalue1*/ = 0);
234 
235  // Checks boss requirements (one boss required to kill other)
236  virtual bool CheckRequiredBosses(uint32 /*bossId*/, Player const* /*player*/ = nullptr) const { return true; }
237 
238  // Checks encounter state at kill/spellcast
239  void UpdateEncounterState(EncounterCreditType type, uint32 creditEntry, Unit* source);
240 
241  // Used only during loading
242  void SetCompletedEncountersMask(uint32 newMask) { completedEncounters = newMask; }
243 
244  // Returns completed encounters mask for packets
245  uint32 GetCompletedEncounterMask() const { return completedEncounters; }
246 
247  void SendEncounterUnit(uint32 type, Unit* unit = NULL, uint8 param1 = 0, uint8 param2 = 0);
248 
250 
251  // ReCheck PhaseTemplate related conditions
252  void UpdatePhasing();
253 
254  uint32 GetEncounterCount() const { return uint32(bosses.size()); }
255 
256  protected:
257  void SetHeaders(std::string const& dataHeaders);
258  void SetBossNumber(uint32 number) { bosses.resize(number); }
259  void LoadBossBoundaries(BossBoundaryData const& data);
260  void LoadDoorData(DoorData const* data);
261  void LoadMinionData(MinionData const* data);
262  void LoadObjectData(ObjectData const* creatureData, ObjectData const* gameObjectData);
263 
264  void AddObject(Creature* obj, bool add);
265  void AddObject(GameObject* obj, bool add);
266  void AddObject(WorldObject* obj, uint32 type, bool add);
267 
268  virtual void AddDoor(GameObject* door, bool add);
269  void AddMinion(Creature* minion, bool add);
270 
271  virtual void UpdateDoorState(GameObject* door);
272  void UpdateMinionState(Creature* minion, EncounterState state);
273 
274  // Exposes private data that should never be modified unless exceptional cases.
275  // Pay very much attention at how the returned BossInfo data is modified to avoid issues.
276  BossInfo* GetBossInfo(uint32 id);
277 
278  // Instance Load and Save
279  bool ReadSaveDataHeaders(std::istringstream& data);
280  void ReadSaveDataBossStates(std::istringstream& data);
281  virtual void ReadSaveDataMore(std::istringstream& /*data*/) { }
282  void WriteSaveDataHeaders(std::ostringstream& data);
283  void WriteSaveDataBossStates(std::ostringstream& data);
284  virtual void WriteSaveDataMore(std::ostringstream& /*data*/) { }
285 
286  bool _SkipCheckRequiredBosses(Player const* player = nullptr) const;
287 
288  private:
289  static void LoadObjectData(ObjectData const* creatureData, ObjectInfoMap& objectInfo);
290 
291  std::vector<char> headers;
292  std::vector<BossInfo> bosses;
298  uint32 completedEncounters; // completed encounter mask, bit indexes are DungeonEncounter.dbc boss numbers, used for packets
299 };
300 
301 template<class AI, class T>
302 AI* GetInstanceAI(T* obj, char const* scriptName)
303 {
304  if (InstanceMap* instance = obj->GetMap()->ToInstanceMap())
305  if (instance->GetInstanceScript())
306  if (instance->GetScriptId() == sObjectMgr->GetScriptId(scriptName))
307  return new AI(obj);
308 
309  return NULL;
310 }
311 
312 template<class AI, class T>
313 AI* GetInstanceAI(T* obj)
314 {
315  if (InstanceMap* instance = obj->GetMap()->ToInstanceMap())
316  if (instance->GetInstanceScript())
317  return new AI(obj);
318 
319  return NULL;
320 }
321 
322 #endif // TRINITY_INSTANCE_DATA_H
Definition: InstanceScript.h:46
StorageType _data
Definition: InstanceScript.h:97
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
CreatureBoundary boundary
Definition: InstanceScript.h:117
CreatureBoundary const * GetBossBoundary(uint32 id) const
Definition: InstanceScript.h:229
const_iterator begin() const
Definition: InstanceScript.h:93
uint32 BossId
Definition: InstanceScript.h:82
bool ServerAllowsTwoSideGroups()
Definition: InstanceScript.h:224
EncounterFrameType
Definition: InstanceScript.h:41
virtual ~InstanceScript()
Definition: InstanceScript.h:146
Definition: InstanceScript.h:100
void SetCompletedEncountersMask(uint32 newMask)
Definition: InstanceScript.h:242
EncounterCreditType
Definition: ObjectMgr.h:670
virtual void Update(uint32)
Definition: InstanceScript.h:163
Definition: InstanceScript.h:50
Definition: InstanceScript.h:69
DoorType type
Definition: InstanceScript.h:77
ObjectData const gameObjectData[]
Definition: instance_gundrak.cpp:40
Definition: InstanceScript.h:53
ObjectGuidMap _objectGuids
Definition: InstanceScript.h:297
virtual void OnGameObjectCreate(GameObject *)
Definition: ZoneScript.h:38
Definition: InstanceScript.h:48
Definition: InstanceScript.h:47
BossInfo()
Definition: InstanceScript.h:113
Definition: InstanceScript.h:68
void SetBossNumber(uint32 number)
Definition: InstanceScript.h:258
Creature * GetCreature(uint32 type)
Definition: InstanceScript.h:180
DoorType
Definition: InstanceScript.h:66
Definition: InstanceScript.h:43
Definition: InstanceScript.h:52
arena_t NULL
Definition: jemalloc_internal.h:624
ObjectInfoMap _creatureInfo
Definition: InstanceScript.h:295
virtual void OnPlayerEnter(Player *)
Definition: InstanceScript.h:190
Definition: InstanceScript.h:44
uint32 type
Definition: InstanceScript.h:108
ObjectData const creatureData[]
Definition: instance_blackrock_caverns.cpp:22
std::vector< BossBoundaryEntry > StorageType
Definition: InstanceScript.h:88
MinionInfoMap minions
Definition: InstanceScript.h:294
Definition: Object.h:423
Definition: Creature.h:467
const_iterator end() const
Definition: InstanceScript.h:94
EncounterState
Definition: InstanceScript.h:56
#define sWorld
Definition: World.h:887
virtual void FillInitialWorldStates(WorldPackets::WorldState::InitWorldStates &)
Definition: InstanceScript.h:249
Definition: InstanceScript.h:61
GuidSet door[MAX_DOOR_TYPES]
Definition: InstanceScript.h:115
Definition: InstanceScript.h:60
#define sObjectMgr
Definition: ObjectMgr.h:1567
virtual ObjectGuid GetGuidData(uint32) const
Definition: ZoneScript.h:44
Definition: Common.h:97
virtual void WriteSaveDataMore(std::ostringstream &)
Definition: InstanceScript.h:284
uint32 entry
Definition: InstanceScript.h:76
std::pair< DoorInfoMap::const_iterator, DoorInfoMap::const_iterator > DoorInfoMapBounds
Definition: InstanceScript.h:135
ObjectInfoMap _gameObjectInfo
Definition: InstanceScript.h:296
Definition: InstanceScript.h:51
Definition: InstanceScript.h:74
Definition: Map.h:758
Definition: InstanceScript.h:45
Definition: ZoneScript.h:26
uint32 completedEncounters
Definition: InstanceScript.h:298
std::vector< char > headers
Definition: InstanceScript.h:291
uint32 bossId
Definition: InstanceScript.h:102
virtual void OnGameObjectRemove(GameObject *)
Definition: ZoneScript.h:39
Definition: InstanceScript.h:59
InstanceScript(Map *map)
Definition: InstanceScript.h:144
AchievementCriteriaTimedTypes
Definition: DBCEnums.h:181
std::set< AreaBoundary const * > CreatureBoundary
Definition: CreatureAI.h:67
AchievementCriteriaTypes
Definition: DBCEnums.h:196
Definition: InstanceScript.h:58
void AddObject(T *object)
Definition: ObjectAccessor.h:98
InstanceMap * ToInstanceMap()
Definition: Map.h:495
GameObject * GetGameObject(uint32 type)
Definition: InstanceScript.h:184
uint32_t uint32
Definition: Define.h:150
Definition: WorldStatePackets.h:27
uint32 bossId
Definition: InstanceScript.h:76
Map * instance
Definition: InstanceScript.h:148
Definition: InstanceScript.h:62
Definition: GameObject.h:880
StorageType::const_iterator const_iterator
Definition: InstanceScript.h:89
Definition: AreaBoundary.h:23
Definition: InstanceScript.h:105
Definition: Map.h:259
virtual void OnCreatureRemove(Creature *)
Definition: ZoneScript.h:36
uint32 GetEncounterCount() const
Definition: InstanceScript.h:254
std::multimap< uint32, DoorInfo > DoorInfoMap
Definition: InstanceScript.h:134
virtual void ReadSaveDataMore(std::istringstream &)
Definition: InstanceScript.h:281
AreaBoundary const * Boundary
Definition: InstanceScript.h:83
std::map< uint32, MinionInfo > MinionInfoMap
Definition: InstanceScript.h:137
Definition: InstanceScript.h:80
EncounterState state
Definition: InstanceScript.h:114
Definition: InstanceScript.h:111
AI * GetInstanceAI(T *obj, char const *scriptName)
Definition: InstanceScript.h:302
Definition: InstanceScript.h:120
Creature * GetCreature(ObjectGuid const &guid)
Definition: Map.cpp:3535
DoorInfo(BossInfo *_bossInfo, DoorType _type)
Definition: InstanceScript.h:122
uint32 GetCompletedEncounterMask() const
Definition: InstanceScript.h:245
std::set< ObjectGuid > GuidSet
Definition: ObjectGuid.h:332
#define TC_GAME_API
Definition: Define.h:134
virtual void Initialize()
Definition: InstanceScript.h:153
Definition: InstanceScript.h:71
Definition: InstanceScript.h:141
Definition: InstanceScript.h:63
uint8_t uint8
Definition: Define.h:152
MinionInfo(BossInfo *_bossInfo)
Definition: InstanceScript.h:130
Definition: InstanceScript.h:128
BossBoundaryData(std::initializer_list< BossBoundaryEntry > data)
Definition: InstanceScript.h:91
Definition: InstanceScript.h:49
BossInfo * bossInfo
Definition: InstanceScript.h:124
Definition: ObjectGuid.h:189
uint32_t uint32
Definition: g3dmath.h:168
GameObject * GetGameObject(ObjectGuid const &guid)
Definition: Map.cpp:3545
EncounterState GetBossState(uint32 id) const
Definition: InstanceScript.h:227
std::map< uint32, ObjectGuid > ObjectGuidMap
Definition: InstanceScript.h:138
Definition: Unit.h:1305
virtual bool CheckRequiredBosses(uint32, Player const *=nullptr) const
Definition: InstanceScript.h:236
uint32 entry
Definition: InstanceScript.h:102
Definition: InstanceScript.h:70
GuidSet minion
Definition: InstanceScript.h:116
DoorType type
Definition: InstanceScript.h:125
virtual void OnCreatureCreate(Creature *)
Definition: ZoneScript.h:35
std::vector< BossInfo > bosses
Definition: InstanceScript.h:292
uint32 entry
Definition: InstanceScript.h:107
BossInfo * bossInfo
Definition: InstanceScript.h:131
DoorInfoMap doors
Definition: InstanceScript.h:293
Definition: InstanceScript.h:86
std::map< uint32, uint32 > ObjectInfoMap
Definition: InstanceScript.h:139