TrinityCore
InstanceScript.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_INSTANCE_DATA_H
19#define TRINITY_INSTANCE_DATA_H
20
21#include "ZoneScript.h"
22#include "Common.h"
23#include "Duration.h"
24#include "Optional.h"
25#include <array>
26#include <map>
27#include <set>
28#include <variant>
29
30#ifdef TRINITY_API_USE_DYNAMIC_LINKING
31#include <memory>
32#endif
33
34#define OUT_SAVE_INST_DATA TC_LOG_DEBUG("scripts", "Saving Instance Data for Instance {} (Map {}, Instance Id {})", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
35#define OUT_SAVE_INST_DATA_COMPLETE TC_LOG_DEBUG("scripts", "Saving Instance Data for Instance {} (Map {}, Instance Id {}) completed.", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
36#define OUT_LOAD_INST_DATA(a) TC_LOG_DEBUG("scripts", "Loading Instance Data for Instance {} (Map {}, Instance Id {}). Input is '{}'", instance->GetMapName(), instance->GetId(), instance->GetInstanceId(), a)
37#define OUT_LOAD_INST_DATA_COMPLETE TC_LOG_DEBUG("scripts", "Instance Data Load for Instance {} (Map {}, Instance Id: {}) is complete.", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
38#define OUT_LOAD_INST_DATA_FAIL TC_LOG_ERROR("scripts", "Unable to load Instance Data for Instance {} (Map {}, Instance Id: {}).", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
39
40class AreaBoundary;
41class Creature;
42class GameObject;
43class InstanceMap;
44class ModuleReference;
46class Player;
47class Unit;
50enum class CriteriaType : int16;
51enum class CriteriaStartEvent : uint8;
52enum Difficulty : uint8;
53
55{
64};
65
66// EnumUtils: DESCRIBE THIS
68{
71 FAIL = 2,
72 DONE = 3,
75};
76
78{
79 OpenWhenNotInProgress = 0, // open if encounter is not in progress
80 OpenWhenDone = 1, // open if encounter is done
81 OpenWhenInProgress = 2, // open if encounter is in progress, typically used for spawning places
82 OpenWhenNotDone = 3, // open if encounter is not done
83 Max
84};
85
87
89{
91 std::array<uint32, MAX_DUNGEON_ENCOUNTERS_PER_BOSS> DungeonEncounterId;
92};
93
95{
98};
99
101{
104};
105
107{
108 typedef std::vector<BossBoundaryEntry> StorageType;
109 typedef StorageType::const_iterator const_iterator;
110
111 BossBoundaryData(std::initializer_list<BossBoundaryEntry> data) : _data(data) { }
113 const_iterator begin() const { return _data.begin(); }
114 const_iterator end() const { return _data.end(); }
115
116 private:
118};
119
121{
123};
124
126{
129};
130
131typedef std::vector<AreaBoundary const*> CreatureBoundary;
132
134{
136
138
140 std::array<GuidSet, static_cast<uint8>(EncounterDoorBehavior::Max)> door;
143 std::array<DungeonEncounterEntry const*, MAX_DUNGEON_ENCOUNTERS_PER_BOSS> DungeonEncounters;
144};
145
147{
148 explicit DoorInfo(BossInfo* _bossInfo, EncounterDoorBehavior _behavior)
149 : bossInfo(_bossInfo), Behavior(_behavior) { }
152};
153
155{
156 explicit MinionInfo(BossInfo* _bossInfo) : bossInfo(_bossInfo) { }
158};
159
161{
165};
166
168{
169 explicit UpdateAdditionalSaveDataEvent(char const* key, std::variant<int64, double> value) : Key(key), Value(value) { }
170
171 char const* Key;
172 std::variant<int64, double> Value;
173};
174
175typedef std::multimap<uint32 /*entry*/, DoorInfo> DoorInfoMap;
176typedef std::pair<DoorInfoMap::const_iterator, DoorInfoMap::const_iterator> DoorInfoMapBounds;
177
178typedef std::map<uint32 /*entry*/, MinionInfo> MinionInfoMap;
179typedef std::map<uint32 /*type*/, ObjectGuid /*guid*/> ObjectGuidMap;
180typedef std::map<uint32 /*entry*/, uint32 /*type*/> ObjectInfoMap;
181
183{
184 public:
185 explicit InstanceScript(InstanceMap* map);
186 InstanceScript(InstanceScript const& right) = delete;
188 InstanceScript& operator=(InstanceScript const& right) = delete;
190 virtual ~InstanceScript();
191
193
194 // On instance load, exactly ONE of these methods will ALWAYS be called:
195 // if we're starting without any saved instance data
196 virtual void Create();
197 // if we're loading existing instance save data
198 void Load(char const* data);
199
200 // When save is needed, this function generates the data
201 std::string GetSaveData();
202
203 std::string UpdateBossStateSaveData(std::string const& oldData, UpdateBossStateSaveDataEvent const& event);
204 std::string UpdateAdditionalSaveData(std::string const& oldData, UpdateAdditionalSaveDataEvent const& event);
205 Optional<uint32> GetEntranceLocationForCompletedEncounters(uint32 completedEncountersMask) const;
206 virtual Optional<uint32> ComputeEntranceLocationForCompletedEncounters(uint32 completedEncountersMask) const;
207
208 virtual void Update(uint32 /*diff*/) { }
209 void UpdateCombatResurrection(uint32 diff);
210
211 // Used by the map's CannotEnter function.
212 // This is to prevent players from entering during boss encounters.
213 virtual bool IsEncounterInProgress() const;
214
215 // Called when a creature/gameobject is added to map or removed from map.
216 // Insert/Remove objectguid to dynamic guid store
217 virtual void OnCreatureCreate(Creature* creature) override;
218 virtual void OnCreatureRemove(Creature* creature) override;
219
220 virtual void OnGameObjectCreate(GameObject* go) override;
221 virtual void OnGameObjectRemove(GameObject* go) override;
222
223 ObjectGuid GetObjectGuid(uint32 type) const;
224 virtual ObjectGuid GetGuidData(uint32 type) const override;
225
226 // Triggers a GameEvent
227 // * If source is nullptr then event is triggered for each player in the instance as "source"
228 void TriggerGameEvent(uint32 gameEventId, WorldObject* source = nullptr, WorldObject* target = nullptr) override;
229
232
233 // Called when a player successfully enters the instance.
234 virtual void OnPlayerEnter(Player* /*player*/) { }
235 // Called when a player successfully leaves the instance.
236 virtual void OnPlayerLeave(Player* /*player*/) { }
237
238 // Handle open / close objects
239 // * use HandleGameObject(0, boolen, GO); in OnObjectCreate in instance scripts
240 // * use HandleGameObject(GUID, boolen, nullptr); in any other script
241 void HandleGameObject(ObjectGuid guid, bool open, GameObject* go = nullptr);
242
243 // Change active state of doors or buttons
244 void DoUseDoorOrButton(ObjectGuid guid, uint32 withRestoreTime = 0, bool useAlternativeState = false);
245 void DoCloseDoorOrButton(ObjectGuid guid);
246
247 // Respawns a GO having negative spawntimesecs in gameobject-table
248 void DoRespawnGameObject(ObjectGuid guid, Seconds timeToDespawn = 1min);
249
250 // Sends world state update to all players in instance
251 void DoUpdateWorldState(int32 worldStateId, int32 value);
252
253 // Send Notify to all players in instance
254 void DoSendNotifyToInstance(char const* format, ...);
255
256 // Update Achievement Criteria for all players in instance
257 void DoUpdateCriteria(CriteriaType type, uint32 miscValue1 = 0, uint32 miscValue2 = 0, Unit* unit = nullptr);
258
259 // Remove Auras due to Spell on all players in instance
260 void DoRemoveAurasDueToSpellOnPlayers(uint32 spell, bool includePets = false, bool includeControlled = false);
261 void DoRemoveAurasDueToSpellOnPlayer(Player* player, uint32 spell, bool includePets = false, bool includeControlled = false);
262
263 // Cast spell on all players in instance
264 void DoCastSpellOnPlayers(uint32 spell, bool includePets = false, bool includeControlled = false);
265 void DoCastSpellOnPlayer(Player* player, uint32 spell, bool includePets = false, bool includeControlled = false);
266
267 // Return wether server allow two side groups or not
268 static bool ServerAllowsTwoSideGroups();
269
270 virtual bool SetBossState(uint32 id, EncounterState state);
271 EncounterState GetBossState(uint32 id) const { return id < bosses.size() ? bosses[id].state : TO_BE_DECIDED; }
272 static char const* GetBossStateName(uint8 state);
273 CreatureBoundary const* GetBossBoundary(uint32 id) const { return id < bosses.size() ? &bosses[id].boundary : nullptr; }
274 DungeonEncounterEntry const* GetBossDungeonEncounter(uint32 id) const;
275 DungeonEncounterEntry const* GetBossDungeonEncounter(Creature const* creature) const;
276
277 // Achievement criteria additional requirements check
278 // NOTE: not use this if same can be checked existed requirement types from AchievementCriteriaRequirementType
279 virtual bool CheckAchievementCriteriaMeet(uint32 /*criteria_id*/, Player const* /*source*/, Unit const* /*target*/ = nullptr, uint32 /*miscvalue1*/ = 0);
280
281 // Checks boss requirements (one boss required to kill other)
282 virtual bool CheckRequiredBosses(uint32 /*bossId*/, Player const* /*player*/ = nullptr) const { return true; }
283
284 bool IsEncounterCompleted(uint32 dungeonEncounterId) const;
285 bool IsEncounterCompletedInMaskByBossId(uint32 completedEncountersMask, uint32 bossId) const;
286
287 uint32 GetEncounterCount() const { return uint32(bosses.size()); }
288
289 // Sets the entrance location (WorldSafeLoc) id
290 void SetEntranceLocation(uint32 worldSafeLocationId);
291
292 // Sets a temporary entrance that does not get saved to db
293 void SetTemporaryEntranceLocation(uint32 worldSafeLocationId) { _temporaryEntranceId = worldSafeLocationId; }
294
295 // Get's the current entrance id
296 uint32 GetEntranceLocation() const { return _temporaryEntranceId ? _temporaryEntranceId : _entranceId; }
297
298 // Only used by areatriggers that inherit from OnlyOnceAreaTriggerScript
299 void MarkAreaTriggerDone(uint32 id) { _activatedAreaTriggers.insert(id); }
300 void ResetAreaTriggerDone(uint32 id) { _activatedAreaTriggers.erase(id); }
301 bool IsAreaTriggerDone(uint32 id) const { return _activatedAreaTriggers.find(id) != _activatedAreaTriggers.end(); }
302
303 void SendEncounterUnit(EncounterFrameType type, Unit const* unit, Optional<int32> param1 = {}, Optional<int32> param2 = {});
304 void SendEncounterStart(uint32 inCombatResCount = 0, uint32 maxInCombatResCount = 0, uint32 inCombatResChargeRecovery = 0, uint32 nextCombatResChargeTime = 0);
305 void SendEncounterEnd();
306
307 void SendBossKillCredit(uint32 encounterId);
308
309 // ReCheck PhaseTemplate related conditions
310 void UpdatePhasing();
311
312 void InitializeCombatResurrections(uint8 charges = 1, uint32 interval = 0);
313 void AddCombatResurrectionCharge();
314 void UseCombatResurrection();
315 void ResetCombatResurrections();
316 uint8 GetCombatResurrectionCharges() const { return _combatResurrectionCharges; }
317 uint32 GetCombatResurrectionChargeInterval() const;
318
319 void RegisterPersistentScriptValue(PersistentInstanceScriptValueBase* value) { _persistentScriptValues.push_back(value); }
320 std::string const& GetHeader() const { return headers; }
321 std::vector<PersistentInstanceScriptValueBase*>& GetPersistentScriptValues() { return _persistentScriptValues; }
322
323 protected:
324 void SetHeaders(std::string const& dataHeaders);
325 void SetBossNumber(uint32 number) { bosses.resize(number); }
326 void LoadBossBoundaries(BossBoundaryData const& data);
327 void LoadDoorData(DoorData const* data);
328 void LoadMinionData(MinionData const* data);
329 void LoadObjectData(ObjectData const* creatureData, ObjectData const* gameObjectData);
330 template<typename T>
332 {
333 for (DungeonEncounterData const& encounter : encounters)
334 LoadDungeonEncounterData(encounter.BossId, encounter.DungeonEncounterId);
335 }
336
337 void AddObject(Creature* obj, bool add);
338 void AddObject(GameObject* obj, bool add);
339 void AddObject(WorldObject* obj, uint32 type, bool add);
340
341 virtual void AddDoor(GameObject* door, bool add);
342 void AddMinion(Creature* minion, bool add);
343
344 virtual void UpdateDoorState(GameObject* door);
345 void UpdateMinionState(Creature* minion, EncounterState state);
346
347 void UpdateSpawnGroups();
348
349 // Exposes private data that should never be modified unless exceptional cases.
350 // Pay very much attention at how the returned BossInfo data is modified to avoid issues.
351 BossInfo* GetBossInfo(uint32 id);
352
353 // Override this function to validate all additional data loads
354 virtual void AfterDataLoad() { }
355
356 bool _SkipCheckRequiredBosses(Player const* player = nullptr) const;
357
358 private:
359 static void LoadObjectData(ObjectData const* creatureData, ObjectInfoMap& objectInfo);
360 void LoadDungeonEncounterData(uint32 bossId, std::array<uint32, MAX_DUNGEON_ENCOUNTERS_PER_BOSS> const& dungeonEncounterIds);
361 void UpdateLfgEncounterState(BossInfo const* bossInfo);
362
363 std::string headers;
364 std::vector<BossInfo> bosses;
365 std::vector<PersistentInstanceScriptValueBase*> _persistentScriptValues;
371 std::vector<InstanceSpawnGroupInfo> const* const _instanceSpawnGroups;
372 std::unordered_set<uint32> _activatedAreaTriggers;
376 uint8 _combatResurrectionCharges; // the counter for available battle resurrections
378
379 #ifdef TRINITY_API_USE_DYNAMIC_LINKING
380 // Strong reference to the associated script module
381 std::shared_ptr<ModuleReference> module_reference;
382 #endif // #ifndef TRINITY_API_USE_DYNAMIC_LINKING
383
385};
386
388{
389protected:
390 PersistentInstanceScriptValueBase(InstanceScript& instance, char const* name, std::variant<int64, double> value);
391
392public:
394
395 char const* GetName() const { return _name; }
396
398 {
399 return UpdateAdditionalSaveDataEvent(_name, _value);
400 }
401
402 void LoadValue(int64 value)
403 {
404 _value.emplace<int64>(value);
405 }
406
407 void LoadValue(double value)
408 {
409 _value.emplace<double>(value);
410 }
411
412protected:
413 void NotifyValueChanged();
414
416 char const* _name;
417 std::variant<int64, double> _value;
418};
419
420template<typename T>
422{
423public:
424 PersistentInstanceScriptValue(InstanceScript& instance, char const* name, T value = {})
425 : PersistentInstanceScriptValueBase(instance, name, WrapValue(value))
426 {
427 }
428
429 operator T() const
430 {
431 return std::visit([](auto v) { return static_cast<T>(v); }, _value);
432 }
433
435 {
436 _value = WrapValue(value);
438 return *this;
439 }
440
441 void LoadValue(T value)
442 {
443 _value = WrapValue(value);
444 }
445
446private:
447 static std::variant<int64, double> WrapValue(T value)
448 {
449 if constexpr (std::is_integral_v<T> || std::is_enum_v<T>)
450 return int64(value);
451 else if constexpr (std::is_floating_point_v<T>)
452 return double(value);
453 else
454 return {};
455 }
456};
457
458#endif // TRINITY_INSTANCE_DATA_H
std::vector< AreaBoundary const * > CreatureBoundary
Definition: CreatureAI.h:37
Difficulty
Definition: DBCEnums.h:873
CriteriaStartEvent
Definition: DBCEnums.h:470
CriteriaType
Definition: DBCEnums.h:503
#define TC_GAME_API
Definition: Define.h:123
uint8_t uint8
Definition: Define.h:144
int64_t int64
Definition: Define.h:137
int16_t int16
Definition: Define.h:139
int32_t int32
Definition: Define.h:138
uint32_t uint32
Definition: Define.h:142
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition: Duration.h:32
static constexpr uint32 MAX_DUNGEON_ENCOUNTERS_PER_BOSS
EncounterState
@ IN_PROGRESS
@ FAIL
@ DONE
@ SPECIAL
@ NOT_STARTED
@ TO_BE_DECIDED
EncounterFrameType
@ ENCOUNTER_FRAME_ENABLE_OBJECTIVE
@ ENCOUNTER_FRAME_DISENGAGE
@ ENCOUNTER_FRAME_UPDATE_PRIORITY
@ ENCOUNTER_FRAME_DISABLE_OBJECTIVE
@ ENCOUNTER_FRAME_UPDATE_OBJECTIVE
@ ENCOUNTER_FRAME_ADD_TIMER
@ ENCOUNTER_FRAME_PHASE_SHIFT_CHANGED
@ ENCOUNTER_FRAME_ENGAGE
std::map< uint32, MinionInfo > MinionInfoMap
std::map< uint32, ObjectGuid > ObjectGuidMap
std::vector< AreaBoundary const * > CreatureBoundary
std::multimap< uint32, DoorInfo > DoorInfoMap
std::map< uint32, uint32 > ObjectInfoMap
std::pair< DoorInfoMap::const_iterator, DoorInfoMap::const_iterator > DoorInfoMapBounds
EncounterDoorBehavior
std::set< ObjectGuid > GuidSet
Definition: ObjectGuid.h:393
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
uint32 GetEntranceLocation() const
void SetBossNumber(uint32 number)
uint8 GetCombatResurrectionCharges() const
std::vector< PersistentInstanceScriptValueBase * > _persistentScriptValues
void LoadDungeonEncounterData(T const &encounters)
uint32 _combatResurrectionTimer
MinionInfoMap minions
std::vector< PersistentInstanceScriptValueBase * > & GetPersistentScriptValues()
bool IsAreaTriggerDone(uint32 id) const
InstanceMap * instance
InstanceScript & operator=(InstanceScript &&right)=delete
ObjectGuidMap _objectGuids
InstanceScript(InstanceScript &&right)=delete
CreatureBoundary const * GetBossBoundary(uint32 id) const
void SetTemporaryEntranceLocation(uint32 worldSafeLocationId)
DoorInfoMap doors
std::string const & GetHeader() const
ObjectInfoMap _gameObjectInfo
EncounterState GetBossState(uint32 id) const
virtual void OnPlayerLeave(Player *)
uint32 _temporaryEntranceId
InstanceScript(InstanceScript const &right)=delete
ObjectInfoMap _creatureInfo
bool _combatResurrectionTimerStarted
void RegisterPersistentScriptValue(PersistentInstanceScriptValueBase *value)
InstanceScript & operator=(InstanceScript const &right)=delete
std::string headers
virtual bool CheckRequiredBosses(uint32, Player const *=nullptr) const
std::unordered_set< uint32 > _activatedAreaTriggers
std::vector< BossInfo > bosses
virtual void OnPlayerEnter(Player *)
virtual void AfterDataLoad()
void MarkAreaTriggerDone(uint32 id)
uint32 GetEncounterCount() const
uint8 _combatResurrectionCharges
virtual void Update(uint32)
std::vector< InstanceSpawnGroupInfo > const *const _instanceSpawnGroups
void ResetAreaTriggerDone(uint32 id)
std::variant< int64, double > _value
PersistentInstanceScriptValueBase(InstanceScript &instance, char const *name, std::variant< int64, double > value)
UpdateAdditionalSaveDataEvent CreateEvent() const
PersistentInstanceScriptValue(InstanceScript &instance, char const *name, T value={})
PersistentInstanceScriptValue & operator=(T value)
static std::variant< int64, double > WrapValue(T value)
Definition: Unit.h:627
ObjectData const creatureData[]
DungeonEncounterData const encounters[]
ObjectData const gameObjectData[]
void AddObject(T *object)
TC_GAME_API GameObject * GetGameObject(WorldObject const &u, ObjectGuid const &guid)
TC_GAME_API Creature * GetCreature(WorldObject const &u, ObjectGuid const &guid)
std::unique_ptr< VignetteData > Create(VignetteEntry const *vignetteData, WorldObject const *owner)
Definition: Vignette.cpp:72
const_iterator begin() const
BossBoundaryData(std::initializer_list< BossBoundaryEntry > data)
std::vector< BossBoundaryEntry > StorageType
const_iterator end() const
StorageType::const_iterator const_iterator
StorageType _data
AreaBoundary const * Boundary
EncounterState state
DungeonEncounterEntry const * GetDungeonEncounterForDifficulty(Difficulty difficulty) const
std::array< DungeonEncounterEntry const *, MAX_DUNGEON_ENCOUNTERS_PER_BOSS > DungeonEncounters
GuidSet minion
std::array< GuidSet, static_cast< uint8 >(EncounterDoorBehavior::Max)> door
CreatureBoundary boundary
uint32 entry
EncounterDoorBehavior Behavior
uint32 bossId
BossInfo * bossInfo
DoorInfo(BossInfo *_bossInfo, EncounterDoorBehavior _behavior)
EncounterDoorBehavior Behavior
std::array< uint32, MAX_DUNGEON_ENCOUNTERS_PER_BOSS > DungeonEncounterId
BossInfo * bossInfo
MinionInfo(BossInfo *_bossInfo)
std::variant< int64, double > Value
UpdateAdditionalSaveDataEvent(char const *key, std::variant< int64, double > value)
DungeonEncounterEntry const * DungeonEncounter