TrinityCore
Loading...
Searching...
No Matches
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 : uint8;
51enum class CriteriaStartEvent : uint8;
52enum Difficulty : uint8;
54
56{
66 ENCOUNTER_FRAME_UNK7 = 9, // Seems to have something to do with sorting the encounter units
68};
69
70// EnumUtils: DESCRIBE THIS
72{
75 FAIL = 2,
76 DONE = 3,
79};
80
82{
83 DOOR_TYPE_ROOM = 0, // Door can open if encounter is not in progress
84 DOOR_TYPE_PASSAGE = 1, // Door can open if encounter is done
85 DOOR_TYPE_SPAWN_HOLE = 2, // Door can open if encounter is in progress, typically used for spawning places
87};
88
90
92{
94 std::array<uint32, MAX_DUNGEON_ENCOUNTERS_PER_BOSS> DungeonEncounterId;
95};
96
98{
101};
102
104{
107};
108
110{
111 typedef std::vector<BossBoundaryEntry> StorageType;
112 typedef StorageType::const_iterator const_iterator;
113
114 BossBoundaryData(std::initializer_list<BossBoundaryEntry> data) : _data(data) { }
116 const_iterator begin() const { return _data.begin(); }
117 const_iterator end() const { return _data.end(); }
118
119 private:
121};
122
124{
126};
127
129{
132};
133
134typedef std::vector<AreaBoundary const*> CreatureBoundary;
135
137{
139
141
146 std::array<DungeonEncounterEntry const*, MAX_DUNGEON_ENCOUNTERS_PER_BOSS> DungeonEncounters;
147};
148
150{
151 explicit DoorInfo(BossInfo* _bossInfo, DoorType _type)
152 : bossInfo(_bossInfo), type(_type) { }
155};
156
158{
159 explicit MinionInfo(BossInfo* _bossInfo) : bossInfo(_bossInfo) { }
161};
162
164{
168};
169
171{
172 explicit UpdateAdditionalSaveDataEvent(char const* key, std::variant<int64, double> value) : Key(key), Value(value) { }
173
174 char const* Key;
175 std::variant<int64, double> Value;
176};
177
178typedef std::multimap<uint32 /*entry*/, DoorInfo> DoorInfoMap;
179typedef std::pair<DoorInfoMap::const_iterator, DoorInfoMap::const_iterator> DoorInfoMapBounds;
180
181typedef std::map<uint32 /*entry*/, MinionInfo> MinionInfoMap;
182typedef std::map<uint32 /*type*/, ObjectGuid /*guid*/> ObjectGuidMap;
183typedef std::map<uint32 /*entry*/, uint32 /*type*/> ObjectInfoMap;
184
186{
187 public:
188 explicit InstanceScript(InstanceMap* map);
189 InstanceScript(InstanceScript const& right) = delete;
191 InstanceScript& operator=(InstanceScript const& right) = delete;
193 virtual ~InstanceScript();
194
196
197 // On instance load, exactly ONE of these methods will ALWAYS be called:
198 // if we're starting without any saved instance data
199 virtual void Create();
200 // if we're loading existing instance save data
201 void Load(char const* data);
202
203 // When save is needed, this function generates the data
204 std::string GetSaveData();
205
206 std::string UpdateBossStateSaveData(std::string const& oldData, UpdateBossStateSaveDataEvent const& event);
207 std::string UpdateAdditionalSaveData(std::string const& oldData, UpdateAdditionalSaveDataEvent const& event);
208 Optional<uint32> GetEntranceLocationForCompletedEncounters(uint32 completedEncountersMask) const;
209 virtual Optional<uint32> ComputeEntranceLocationForCompletedEncounters(uint32 completedEncountersMask) const;
210
211 virtual void Update(uint32 /*diff*/) { }
212 void UpdateCombatResurrection(uint32 diff);
213
214 // Used by the map's CannotEnter function.
215 // This is to prevent players from entering during boss encounters.
216 virtual bool IsEncounterInProgress() const;
217
218 // Called when a creature/gameobject is added to map or removed from map.
219 // Insert/Remove objectguid to dynamic guid store
220 virtual void OnCreatureCreate(Creature* creature) override;
221 virtual void OnCreatureRemove(Creature* creature) override;
222
223 virtual void OnGameObjectCreate(GameObject* go) override;
224 virtual void OnGameObjectRemove(GameObject* go) override;
225
226 ObjectGuid GetObjectGuid(uint32 type) const;
227 virtual ObjectGuid GetGuidData(uint32 type) const override;
228
229 // Triggers a GameEvent
230 // * If source is nullptr then event is triggered for each player in the instance as "source"
231 void TriggerGameEvent(uint32 gameEventId, WorldObject* source = nullptr, WorldObject* target = nullptr) override;
232
233 Creature* GetCreature(uint32 type);
234 GameObject* GetGameObject(uint32 type);
235
236 // Called when a player successfully enters the instance.
237 virtual void OnPlayerEnter(Player* /*player*/) { }
238 // Called when a player successfully leaves the instance.
239 virtual void OnPlayerLeave(Player* /*player*/) { }
240
241 // Handle open / close objects
242 // * use HandleGameObject(0, boolen, GO); in OnObjectCreate in instance scripts
243 // * use HandleGameObject(GUID, boolen, nullptr); in any other script
244 void HandleGameObject(ObjectGuid guid, bool open, GameObject* go = nullptr);
245
246 // Change active state of doors or buttons
247 void DoUseDoorOrButton(ObjectGuid guid, uint32 withRestoreTime = 0, bool useAlternativeState = false);
248 void DoCloseDoorOrButton(ObjectGuid guid);
249
250 // Respawns a GO having negative spawntimesecs in gameobject-table
251 void DoRespawnGameObject(ObjectGuid guid, Seconds timeToDespawn = 1min);
252
253 // Sends world state update to all players in instance
254 void DoUpdateWorldState(int32 worldStateId, int32 value);
255
256 // Send Notify to all players in instance
257 void DoSendNotifyToInstance(char const* format, ...);
258
259 // Update Achievement Criteria for all players in instance
260 void DoUpdateCriteria(CriteriaType type, uint32 miscValue1 = 0, uint32 miscValue2 = 0, Unit* unit = nullptr);
261
262 // Remove Auras due to Spell on all players in instance
263 void DoRemoveAurasDueToSpellOnPlayers(uint32 spell, bool includePets = false, bool includeControlled = false);
264 void DoRemoveAurasDueToSpellOnPlayer(Player* player, uint32 spell, bool includePets = false, bool includeControlled = false);
265
266 // Cast spell on all players in instance
267 void DoCastSpellOnPlayers(uint32 spell, bool includePets = false, bool includeControlled = false);
268 void DoCastSpellOnPlayer(Player* player, uint32 spell, bool includePets = false, bool includeControlled = false);
269
270 // Return wether server allow two side groups or not
271 static bool ServerAllowsTwoSideGroups();
272
273 virtual bool SetBossState(uint32 id, EncounterState state);
274 EncounterState GetBossState(uint32 id) const { return id < bosses.size() ? bosses[id].state : TO_BE_DECIDED; }
275 static char const* GetBossStateName(uint8 state);
276 CreatureBoundary const* GetBossBoundary(uint32 id) const { return id < bosses.size() ? &bosses[id].boundary : nullptr; }
277 DungeonEncounterEntry const* GetBossDungeonEncounter(uint32 id) const;
278 DungeonEncounterEntry const* GetBossDungeonEncounter(Creature const* creature) const;
279
280 // Achievement criteria additional requirements check
281 // NOTE: not use this if same can be checked existed requirement types from AchievementCriteriaRequirementType
282 virtual bool CheckAchievementCriteriaMeet(uint32 /*criteria_id*/, Player const* /*source*/, Unit const* /*target*/ = nullptr, uint32 /*miscvalue1*/ = 0);
283
284 // Checks boss requirements (one boss required to kill other)
285 virtual bool CheckRequiredBosses(uint32 /*bossId*/, Player const* /*player*/ = nullptr) const { return true; }
286
287 // Checks encounter state at kill/spellcast
288 void UpdateEncounterStateForKilledCreature(uint32 creatureId, Unit* source);
289 void UpdateEncounterStateForSpellCast(uint32 spellId, Unit* source);
290
291 // Used only during loading
292 void SetCompletedEncountersMask(uint32 newMask);
293
294 // Returns completed encounters mask for packets
295 uint32 GetCompletedEncounterMask() const { return completedEncounters; }
296
297 bool IsEncounterCompleted(uint32 dungeonEncounterId) const;
298 bool IsEncounterCompletedInMaskByBossId(uint32 completedEncountersMask, uint32 bossId) const;
299
300 uint32 GetEncounterCount() const { return uint32(bosses.size()); }
301
302 // Sets the entrance location (WorldSafeLoc) id
303 void SetEntranceLocation(uint32 worldSafeLocationId);
304
305 // Sets a temporary entrance that does not get saved to db
306 void SetTemporaryEntranceLocation(uint32 worldSafeLocationId) { _temporaryEntranceId = worldSafeLocationId; }
307
308 // Get's the current entrance id
309 uint32 GetEntranceLocation() const { return _temporaryEntranceId ? _temporaryEntranceId : _entranceId; }
310
311 // Only used by areatriggers that inherit from OnlyOnceAreaTriggerScript
312 void MarkAreaTriggerDone(uint32 id) { _activatedAreaTriggers.insert(id); }
313 void ResetAreaTriggerDone(uint32 id) { _activatedAreaTriggers.erase(id); }
314 bool IsAreaTriggerDone(uint32 id) const { return _activatedAreaTriggers.find(id) != _activatedAreaTriggers.end(); }
315
316 void SendEncounterUnit(uint32 type, Unit* unit = nullptr, uint8 priority = 0);
317 void SendEncounterStart(uint32 inCombatResCount = 0, uint32 maxInCombatResCount = 0, uint32 inCombatResChargeRecovery = 0, uint32 nextCombatResChargeTime = 0);
318 void SendEncounterEnd();
319
320 void SendBossKillCredit(uint32 encounterId);
321
322 // ReCheck PhaseTemplate related conditions
323 void UpdatePhasing();
324
325 void InitializeCombatResurrections(uint8 charges = 1, uint32 interval = 0);
326 void AddCombatResurrectionCharge();
327 void UseCombatResurrection();
328 void ResetCombatResurrections();
329 uint8 GetCombatResurrectionCharges() const { return _combatResurrectionCharges; }
330 uint32 GetCombatResurrectionChargeInterval() const;
331
332 void RegisterPersistentScriptValue(PersistentInstanceScriptValueBase* value) { _persistentScriptValues.push_back(value); }
333 std::string const& GetHeader() const { return headers; }
334 std::vector<PersistentInstanceScriptValueBase*>& GetPersistentScriptValues() { return _persistentScriptValues; }
335
336 protected:
337 void SetHeaders(std::string const& dataHeaders);
338 void SetBossNumber(uint32 number) { bosses.resize(number); }
339 void LoadBossBoundaries(BossBoundaryData const& data);
340 void LoadDoorData(DoorData const* data);
341 void LoadMinionData(MinionData const* data);
342 void LoadObjectData(ObjectData const* creatureData, ObjectData const* gameObjectData);
343 template<typename T>
345 {
346 for (DungeonEncounterData const& encounter : encounters)
347 LoadDungeonEncounterData(encounter.BossId, encounter.DungeonEncounterId);
348 }
349
350 void AddObject(Creature* obj, bool add);
351 void AddObject(GameObject* obj, bool add);
352 void AddObject(WorldObject* obj, uint32 type, bool add);
353
354 virtual void AddDoor(GameObject* door, bool add);
355 void AddMinion(Creature* minion, bool add);
356
357 virtual void UpdateDoorState(GameObject* door);
358 void UpdateMinionState(Creature* minion, EncounterState state);
359
360 void UpdateSpawnGroups();
361
362 // Exposes private data that should never be modified unless exceptional cases.
363 // Pay very much attention at how the returned BossInfo data is modified to avoid issues.
364 BossInfo* GetBossInfo(uint32 id);
365
366 // Override this function to validate all additional data loads
367 virtual void AfterDataLoad() { }
368
369 bool _SkipCheckRequiredBosses(Player const* player = nullptr) const;
370
371 private:
372 static void LoadObjectData(ObjectData const* creatureData, ObjectInfoMap& objectInfo);
373 void LoadDungeonEncounterData(uint32 bossId, std::array<uint32, MAX_DUNGEON_ENCOUNTERS_PER_BOSS> const& dungeonEncounterIds);
374 void UpdateEncounterState(EncounterCreditType type, uint32 creditEntry, Unit* source);
375
376 std::string headers;
377 std::vector<BossInfo> bosses;
378 std::vector<PersistentInstanceScriptValueBase*> _persistentScriptValues;
384 uint32 completedEncounters; // DEPRECATED, REMOVE
385 std::vector<InstanceSpawnGroupInfo> const* const _instanceSpawnGroups;
386 std::unordered_set<uint32> _activatedAreaTriggers;
390 uint8 _combatResurrectionCharges; // the counter for available battle resurrections
392
393 #ifdef TRINITY_API_USE_DYNAMIC_LINKING
394 // Strong reference to the associated script module
395 std::shared_ptr<ModuleReference> module_reference;
396 #endif // #ifndef TRINITY_API_USE_DYNAMIC_LINKING
397
399};
400
402{
403protected:
404 PersistentInstanceScriptValueBase(InstanceScript& instance, char const* name, std::variant<int64, double> value);
405
406public:
408
409 char const* GetName() const { return _name; }
410
412 {
413 return UpdateAdditionalSaveDataEvent(_name, _value);
414 }
415
416 void LoadValue(int64 value)
417 {
418 _value.emplace<int64>(value);
419 }
420
421 void LoadValue(double value)
422 {
423 _value.emplace<double>(value);
424 }
425
426protected:
427 void NotifyValueChanged();
428
430 char const* _name;
431 std::variant<int64, double> _value;
432};
433
434template<typename T>
436{
437public:
438 PersistentInstanceScriptValue(InstanceScript& instance, char const* name, T value = {})
439 : PersistentInstanceScriptValueBase(instance, name, WrapValue(value))
440 {
441 }
442
443 operator T() const
444 {
445 return std::visit([](auto v) { return static_cast<T>(v); }, _value);
446 }
447
449 {
450 _value = WrapValue(value);
452 return *this;
453 }
454
455 void LoadValue(T value)
456 {
457 _value = WrapValue(value);
458 }
459
460private:
461 static std::variant<int64, double> WrapValue(T value)
462 {
463 if constexpr (std::is_integral_v<T> || std::is_enum_v<T>)
464 return int64(value);
465 else if constexpr (std::is_floating_point_v<T>)
466 return double(value);
467 else
468 return {};
469 }
470};
471
472#endif // TRINITY_INSTANCE_DATA_H
Difficulty
Definition: DBCEnums.h:732
CriteriaType
Definition: DBCEnums.h:376
CriteriaStartEvent
Definition: DBCEnums.h:343
#define TC_GAME_API
Definition: Define.h:124
uint8_t uint8
Definition: Define.h:145
int64_t int64
Definition: Define.h:138
int32_t int32
Definition: Define.h:139
uint32_t uint32
Definition: Define.h:143
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_RESET_COMBAT_RES_LIMIT
@ ENCOUNTER_FRAME_ENABLE_OBJECTIVE
@ ENCOUNTER_FRAME_SET_COMBAT_RES_LIMIT
@ ENCOUNTER_FRAME_UNK7
@ ENCOUNTER_FRAME_DISENGAGE
@ ENCOUNTER_FRAME_ADD_COMBAT_RES_LIMIT
@ ENCOUNTER_FRAME_UPDATE_PRIORITY
@ ENCOUNTER_FRAME_DISABLE_OBJECTIVE
@ ENCOUNTER_FRAME_UPDATE_OBJECTIVE
@ ENCOUNTER_FRAME_ADD_TIMER
@ 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
DoorType
@ DOOR_TYPE_SPAWN_HOLE
@ DOOR_TYPE_ROOM
@ DOOR_TYPE_PASSAGE
@ MAX_DOOR_TYPES
std::pair< DoorInfoMap::const_iterator, DoorInfoMap::const_iterator > DoorInfoMapBounds
std::set< ObjectGuid > GuidSet
Definition: ObjectGuid.h:384
EncounterCreditType
Definition: ObjectMgr.h:997
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
uint32 GetEntranceLocation() const
uint32 GetCompletedEncounterMask() 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
uint32 completedEncounters
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
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:745
ObjectData const creatureData[]
DungeonEncounterData const encounters[]
ObjectData const gameObjectData[]
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
GuidSet door[MAX_DOOR_TYPES]
DungeonEncounterEntry const * GetDungeonEncounterForDifficulty(Difficulty difficulty) const
std::array< DungeonEncounterEntry const *, MAX_DUNGEON_ENCOUNTERS_PER_BOSS > DungeonEncounters
GuidSet minion
CreatureBoundary boundary
uint32 entry
DoorType type
uint32 bossId
BossInfo * bossInfo
DoorInfo(BossInfo *_bossInfo, DoorType _type)
DoorType type
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
uint32 id
Definition: EventMap.cpp:184