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 <map>
25#include <set>
26
27#ifdef TRINITY_API_USE_DYNAMIC_LINKING
28#include <memory>
29#endif
30
31#define OUT_SAVE_INST_DATA TC_LOG_DEBUG("scripts", "Saving Instance Data for Instance {} (Map {}, Instance Id {})", instance->GetMapName(), instance->GetId(), instance->GetInstanceId())
32#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())
33#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)
34#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())
35#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())
36
37namespace WorldPackets
38{
39 namespace WorldState
40 {
41 class InitWorldStates;
42 }
43}
44
45class AreaBoundary;
46class Creature;
47class GameObject;
48class InstanceMap;
50class ModuleReference;
51class Player;
52class Unit;
53class WorldPacket;
57
69
70// EnumUtils: DESCRIBE THIS
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
94
100
102{
103 typedef std::vector<BossBoundaryEntry> StorageType;
104 typedef StorageType::const_iterator const_iterator;
105
106 BossBoundaryData(std::initializer_list<BossBoundaryEntry> data) : _data(data) { }
108 const_iterator begin() const { return _data.begin(); }
109 const_iterator end() const { return _data.end(); }
110
111 private:
113};
114
116{
118};
119
125
126typedef std::vector<AreaBoundary const*> CreatureBoundary;
127
136
138{
139 explicit DoorInfo(BossInfo* _bossInfo, DoorType _type)
140 : bossInfo(_bossInfo), type(_type) { }
143};
144
146{
147 explicit MinionInfo(BossInfo* _bossInfo) : bossInfo(_bossInfo) { }
149};
150
151typedef std::multimap<uint32 /*entry*/, DoorInfo> DoorInfoMap;
152typedef std::pair<DoorInfoMap::const_iterator, DoorInfoMap::const_iterator> DoorInfoMapBounds;
153
154typedef std::map<uint32 /*entry*/, MinionInfo> MinionInfoMap;
155typedef std::map<uint32 /*type*/, ObjectGuid /*guid*/> ObjectGuidMap;
156typedef std::map<uint32 /*entry*/, uint32 /*type*/> ObjectInfoMap;
157
159{
160 public:
161 explicit InstanceScript(InstanceMap* map);
162
163 virtual ~InstanceScript() { }
164
166
167 // On instance load, exactly ONE of these methods will ALWAYS be called:
168 // if we're starting without any saved instance data
169 virtual void Create();
170 // if we're loading existing instance save data
171 virtual void Load(char const* data);
172
173 // When save is needed, this function generates the data
174 virtual std::string GetSaveData();
175
176 void SaveToDB();
177
178 virtual void Update(uint32 /*diff*/) { }
179
180 // Used by the map's CannotEnter function.
181 // This is to prevent players from entering during boss encounters.
182 virtual bool IsEncounterInProgress() const;
183
184 // Called when a creature/gameobject is added to map or removed from map.
185 // Insert/Remove objectguid to dynamic guid store
186 virtual void OnCreatureCreate(Creature* creature) override;
187 virtual void OnCreatureRemove(Creature* creature) override;
188
189 virtual void OnGameObjectCreate(GameObject* go) override;
190 virtual void OnGameObjectRemove(GameObject* go) override;
191
192 ObjectGuid GetObjectGuid(uint32 type) const;
193 virtual ObjectGuid GetGuidData(uint32 type) const override;
194
195 Creature* GetCreature(uint32 type);
196 GameObject* GetGameObject(uint32 type);
197
198 // Called when a player successfully enters the instance.
199 virtual void OnPlayerEnter(Player* /*player*/) { }
200 // Called when a player successfully leaves the instance.
201 virtual void OnPlayerLeave(Player* /*player*/) { }
202
203 // Handle open / close objects
204 // * use HandleGameObject(0, boolen, GO); in OnObjectCreate in instance scripts
205 // * use HandleGameObject(GUID, boolen, nullptr); in any other script
206 void HandleGameObject(ObjectGuid guid, bool open, GameObject* go = nullptr);
207
208 // Change active state of doors or buttons
209 void DoUseDoorOrButton(ObjectGuid guid, uint32 withRestoreTime = 0, bool useAlternativeState = false);
210 void DoCloseDoorOrButton(ObjectGuid guid);
211
212 // Respawns a GO having negative spawntimesecs in gameobject-table
213 void DoRespawnGameObject(ObjectGuid guid, Seconds timeToDespawn = 1min);
214
215 // Sends world state update to all players in instance
216 void DoUpdateWorldState(uint32 worldstateId, uint32 worldstateValue);
217
218 // Send Notify to all players in instance
219 void DoSendNotifyToInstance(char const* format, ...);
220
221 // Update Achievement Criteria for all players in instance
222 void DoUpdateAchievementCriteria(AchievementCriteriaTypes type, uint32 miscValue1 = 0, uint32 miscValue2 = 0, Unit* unit = nullptr);
223
224 // Start/Stop Timed Achievement Criteria for all players in instance
225 void DoStartTimedAchievement(AchievementCriteriaTimedTypes type, uint32 entry);
226 void DoStopTimedAchievement(AchievementCriteriaTimedTypes type, uint32 entry);
227
228 // Remove Auras due to Spell on all players in instance
229 void DoRemoveAurasDueToSpellOnPlayers(uint32 spell, bool includePets = false, bool includeControlled = false);
230 void DoRemoveAurasDueToSpellOnPlayer(Player* player, uint32 spell, bool includePets = false, bool includeControlled = false);
231
232 // Cast spell on all players in instance
233 void DoCastSpellOnPlayers(uint32 spell, bool includePets = false, bool includeControlled = false);
234 void DoCastSpellOnPlayer(Player* player, uint32 spell, bool includePets = false, bool includeControlled = false);
235
236 // Return wether server allow two side groups or not
237 static bool ServerAllowsTwoSideGroups();
238
239 virtual bool SetBossState(uint32 id, EncounterState state);
240 EncounterState GetBossState(uint32 id) const { return id < bosses.size() ? bosses[id].state : TO_BE_DECIDED; }
241 static char const* GetBossStateName(uint8 state);
242 CreatureBoundary const* GetBossBoundary(uint32 id) const { return id < bosses.size() ? &bosses[id].boundary : nullptr; }
243
244 // Achievement criteria additional requirements check
245 // NOTE: not use this if same can be checked existed requirement types from AchievementCriteriaRequirementType
246 virtual bool CheckAchievementCriteriaMeet(uint32 /*criteria_id*/, Player const* /*source*/, Unit const* /*target*/ = nullptr, uint32 /*miscvalue1*/ = 0);
247
248 // Checks boss requirements (one boss required to kill other)
249 virtual bool CheckRequiredBosses(uint32 /*bossId*/, Player const* /*player*/ = nullptr) const { return true; }
250
251 // Checks encounter state at kill/spellcast
252 void UpdateEncounterStateForKilledCreature(uint32 creatureId, Unit* source);
253 void UpdateEncounterStateForSpellCast(uint32 spellId, Unit* source);
254
255 // Used only during loading
256 void SetCompletedEncountersMask(uint32 newMask) { completedEncounters = newMask; }
257
258 // Returns completed encounters mask for packets
259 uint32 GetCompletedEncounterMask() const { return completedEncounters; }
260
261 void SendEncounterUnit(EncounterFrameType type, Unit const* unit = nullptr, uint8 param1 = 0, uint8 param2 = 0);
262
264
265 uint32 GetEncounterCount() const { return bosses.size(); }
266
267 // Only used by areatriggers that inherit from OnlyOnceAreaTriggerScript
268 void MarkAreaTriggerDone(uint32 id) { _activatedAreaTriggers.insert(id); }
269 void ResetAreaTriggerDone(uint32 id) { _activatedAreaTriggers.erase(id); }
270 bool IsAreaTriggerDone(uint32 id) const { return _activatedAreaTriggers.find(id) != _activatedAreaTriggers.end(); }
271
272 protected:
273 void SetHeaders(std::string const& dataHeaders);
274 void SetBossNumber(uint32 number) { bosses.resize(number); }
275 void LoadBossBoundaries(BossBoundaryData const& data);
276 void LoadDoorData(DoorData const* data);
277 void LoadMinionData(MinionData const* data);
278 void LoadObjectData(ObjectData const* creatureData, ObjectData const* gameObjectData);
279
280 void AddObject(Creature* obj, bool add);
281 void AddObject(GameObject* obj, bool add);
282 void AddObject(WorldObject* obj, uint32 type, bool add);
283
284 virtual void AddDoor(GameObject* door, bool add);
285 void AddMinion(Creature* minion, bool add);
286
287 virtual void UpdateDoorState(GameObject* door);
288 void UpdateMinionState(Creature* minion, EncounterState state);
289
290 void UpdateSpawnGroups();
291
292 // Exposes private data that should never be modified unless exceptional cases.
293 // Pay very much attention at how the returned BossInfo data is modified to avoid issues.
294 BossInfo* GetBossInfo(uint32 id);
295
296 // Instance Load and Save
297 bool ReadSaveDataHeaders(std::istringstream& data);
298 void ReadSaveDataBossStates(std::istringstream& data);
299 virtual void ReadSaveDataMore(std::istringstream& /*data*/) { }
300 void WriteSaveDataHeaders(std::ostringstream& data);
301 void WriteSaveDataBossStates(std::ostringstream& data);
302 virtual void WriteSaveDataMore(std::ostringstream& /*data*/) { }
303
304 bool _SkipCheckRequiredBosses(Player const* player = nullptr) const;
305
306 private:
307 static void LoadObjectData(ObjectData const* creatureData, ObjectInfoMap& objectInfo);
308 void UpdateEncounterState(EncounterCreditType type, uint32 creditEntry, Unit* source);
309
310 std::vector<char> headers;
311 std::vector<BossInfo> bosses;
317 uint32 completedEncounters; // completed encounter mask, bit indexes are DungeonEncounter.dbc boss numbers, used for packets
318 std::vector<InstanceSpawnGroupInfo> const* const _instanceSpawnGroups;
319 std::unordered_set<uint32> _activatedAreaTriggers;
320
321 #ifdef TRINITY_API_USE_DYNAMIC_LINKING
322 // Strong reference to the associated script module
323 std::shared_ptr<ModuleReference> module_reference;
324 #endif // #ifndef TRINITY_API_USE_DYNAMIC_LINKING
325
327};
328
329#endif // TRINITY_INSTANCE_DATA_H
AchievementCriteriaTimedTypes
Definition DBCEnums.h:121
AchievementCriteriaTypes
Definition DBCEnums.h:133
#define TC_GAME_API
Definition Define.h:114
uint8_t uint8
Definition Define.h:135
uint32_t uint32
Definition Define.h:133
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition Duration.h:27
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
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:260
EncounterCreditType
Definition ObjectMgr.h:895
static void SaveToDB(QuestPool const &pool, CharacterDatabaseTransaction trans)
WorldState
uint32 GetCompletedEncounterMask() const
void SetBossNumber(uint32 number)
std::vector< char > headers
MinionInfoMap minions
virtual void ReadSaveDataMore(std::istringstream &)
bool IsAreaTriggerDone(uint32 id) const
uint32 completedEncounters
InstanceMap * instance
ObjectGuidMap _objectGuids
CreatureBoundary const * GetBossBoundary(uint32 id) const
DoorInfoMap doors
ObjectInfoMap _gameObjectInfo
EncounterState GetBossState(uint32 id) const
virtual void OnPlayerLeave(Player *)
virtual void FillInitialWorldStates(WorldPackets::WorldState::InitWorldStates &)
ObjectInfoMap _creatureInfo
virtual bool CheckRequiredBosses(uint32, Player const *=nullptr) const
virtual ~InstanceScript()
std::unordered_set< uint32 > _activatedAreaTriggers
std::vector< BossInfo > bosses
virtual void OnPlayerEnter(Player *)
void MarkAreaTriggerDone(uint32 id)
uint32 GetEncounterCount() const
virtual void Update(uint32)
void SetCompletedEncountersMask(uint32 newMask)
std::vector< InstanceSpawnGroupInfo > const *const _instanceSpawnGroups
void ResetAreaTriggerDone(uint32 id)
virtual void WriteSaveDataMore(std::ostringstream &)
Definition Unit.h:769
ObjectData const gameObjectData[]
ObjectData const creatureData[]
const_iterator begin() const
BossBoundaryData(std::initializer_list< BossBoundaryEntry > data)
std::vector< BossBoundaryEntry > StorageType
const_iterator end() const
StorageType::const_iterator const_iterator
AreaBoundary const * Boundary
EncounterState state
GuidSet door[MAX_DOOR_TYPES]
GuidSet minion
CreatureBoundary boundary
uint32 entry
DoorType type
uint32 bossId
BossInfo * bossInfo
DoorInfo(BossInfo *_bossInfo, DoorType _type)
DoorType type
BossInfo * bossInfo
MinionInfo(BossInfo *_bossInfo)