TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ConditionMgr.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_CONDITIONMGR_H
20 #define TRINITY_CONDITIONMGR_H
21 
22 #include "Common.h"
23 
24 class Creature;
25 class Player;
26 class Unit;
27 class WorldObject;
28 class LootTemplate;
29 struct Condition;
31 
50 { // value1 value2 value3
51  CONDITION_NONE = 0, // 0 0 0 always true
52  CONDITION_AURA = 1, // spell_id effindex 0 true if target has aura of spell_id with effect effindex
53  CONDITION_ITEM = 2, // item_id count bank true if has #count of item_ids (if 'bank' is set it searches in bank slots too)
54  CONDITION_ITEM_EQUIPPED = 3, // item_id 0 0 true if has item_id equipped
55  CONDITION_ZONEID = 4, // zone_id 0 0 true if in zone_id
56  CONDITION_REPUTATION_RANK = 5, // faction_id rankMask 0 true if has min_rank for faction_id
57  CONDITION_TEAM = 6, // player_team 0, 0 469 - Alliance, 67 - Horde)
58  CONDITION_SKILL = 7, // skill_id skill_value 0 true if has skill_value for skill_id
59  CONDITION_QUESTREWARDED = 8, // quest_id 0 0 true if quest_id was rewarded before
60  CONDITION_QUESTTAKEN = 9, // quest_id 0, 0 true while quest active
61  CONDITION_DRUNKENSTATE = 10, // DrunkenState 0, 0 true if player is drunk enough
62  CONDITION_WORLD_STATE = 11, // index value 0 true if world has the value for the index
63  CONDITION_ACTIVE_EVENT = 12, // event_id 0 0 true if event is active
64  CONDITION_INSTANCE_INFO = 13, // entry data type true if the instance info defined by type (enum InstanceInfo) equals data.
65  CONDITION_QUEST_NONE = 14, // quest_id 0 0 true if doesn't have quest saved
66  CONDITION_CLASS = 15, // class 0 0 true if player's class is equal to class
67  CONDITION_RACE = 16, // race 0 0 true if player's race is equal to race
68  CONDITION_ACHIEVEMENT = 17, // achievement_id 0 0 true if achievement is complete
69  CONDITION_TITLE = 18, // title id 0 0 true if player has title
70  CONDITION_SPAWNMASK = 19, // spawnMask 0 0 true if in spawnMask
71  CONDITION_GENDER = 20, // gender 0 0 true if player's gender is equal to gender
72  CONDITION_UNIT_STATE = 21, // unitState 0 0 true if unit has unitState
73  CONDITION_MAPID = 22, // map_id 0 0 true if in map_id
74  CONDITION_AREAID = 23, // area_id 0 0 true if in area_id
75  CONDITION_CREATURE_TYPE = 24, // cinfo.type 0 0 true if creature_template.type = value1
76  CONDITION_SPELL = 25, // spell_id 0 0 true if player has learned spell
77  CONDITION_PHASEID = 26, // phaseid 0 0 true if object is in phaseid
78  CONDITION_LEVEL = 27, // level ComparisonType 0 true if unit's level is equal to param1 (param2 can modify the statement)
79  CONDITION_QUEST_COMPLETE = 28, // quest_id 0 0 true if player has quest_id with all objectives complete, but not yet rewarded
80  CONDITION_NEAR_CREATURE = 29, // creature entry distance dead (0/1) true if there is a creature of entry in range
81  CONDITION_NEAR_GAMEOBJECT = 30, // gameobject entry distance 0 true if there is a gameobject of entry in range
82  CONDITION_OBJECT_ENTRY_GUID = 31, // TypeID entry guid true if object is type TypeID and the entry is 0 or matches entry of the object or matches guid of the object
83  CONDITION_TYPE_MASK = 32, // TypeMask 0 0 true if object is type object's TypeMask matches provided TypeMask
84  CONDITION_RELATION_TO = 33, // ConditionTarget RelationType 0 true if object is in given relation with object specified by ConditionTarget
85  CONDITION_REACTION_TO = 34, // ConditionTarget rankMask 0 true if object's reaction matches rankMask object specified by ConditionTarget
86  CONDITION_DISTANCE_TO = 35, // ConditionTarget distance ComparisonType true if object and ConditionTarget are within distance given by parameters
87  CONDITION_ALIVE = 36, // 0 0 0 true if unit is alive
88  CONDITION_HP_VAL = 37, // hpVal ComparisonType 0 true if unit's hp matches given value
89  CONDITION_HP_PCT = 38, // hpPct ComparisonType 0 true if unit's hp matches given pct
90  CONDITION_REALM_ACHIEVEMENT = 39, // achievement_id 0 0 true if realm achievement is complete
91  CONDITION_IN_WATER = 40, // 0 0 0 true if unit in water
92  CONDITION_TERRAIN_SWAP = 41, // terrainSwap 0 0 true if object is in terrainswap
93  CONDITION_MAX = 42 // MAX
94 };
95 
126 {
155 };
156 
158 {
166 };
167 
169 {
174 };
175 
177 {
179 };
180 
182 {
183  WorldObject* mConditionTargets[MAX_CONDITION_TARGETS]; // an array of targets available for conditions
185  ConditionSourceInfo(WorldObject* target0, WorldObject* target1 = nullptr, WorldObject* target2 = nullptr)
186  {
187  mConditionTargets[0] = target0;
188  mConditionTargets[1] = target1;
189  mConditionTargets[2] = target2;
190  mLastFailedCondition = nullptr;
191  }
192 };
193 
195 {
196  ConditionSourceType SourceType; //SourceTypeOrReferenceId
199  uint32 SourceId; // So far, only used in CONDITION_SOURCE_TYPE_SMART_EVENT
201  ConditionTypes ConditionType; //ConditionTypeOrReference
211 
213  {
214  SourceType = CONDITION_SOURCE_TYPE_NONE;
215  SourceGroup = 0;
216  SourceEntry = 0;
217  SourceId = 0;
218  ElseGroup = 0;
219  ConditionType = CONDITION_NONE;
220  ConditionTarget = 0;
221  ConditionValue1 = 0;
222  ConditionValue2 = 0;
223  ConditionValue3 = 0;
224  ReferenceId = 0;
225  ErrorType = 0;
226  ErrorTextId = 0;
227  ScriptId = 0;
228  NegativeCondition = false;
229  }
230 
231  bool Meets(ConditionSourceInfo& sourceInfo) const;
232  uint32 GetSearcherTypeMaskForCondition() const;
233  bool isLoaded() const { return ConditionType > CONDITION_NONE || ReferenceId; }
234  uint32 GetMaxAvailableConditionTargets() const;
235 
236  std::string ToString(bool ext = false) const;
237 };
238 
239 typedef std::vector<Condition*> ConditionContainer;
240 typedef std::unordered_map<uint32 /*SourceEntry*/, ConditionContainer> ConditionsByEntryMap;
241 typedef std::array<ConditionsByEntryMap, CONDITION_SOURCE_TYPE_MAX> ConditionEntriesByTypeArray;
242 typedef std::unordered_map<uint32, ConditionsByEntryMap> ConditionEntriesByCreatureIdMap;
243 typedef std::unordered_map<std::pair<int32, uint32 /*SAI source_type*/>, ConditionsByEntryMap> SmartEventConditionContainer;
244 typedef std::unordered_map<uint32, ConditionContainer> ConditionReferenceContainer;//only used for references
245 
247 {
248  private:
249  ConditionMgr();
250  ~ConditionMgr();
251 
252  public:
253  static ConditionMgr* instance();
254 
255  void LoadConditions(bool isReload = false);
256  bool isConditionTypeValid(Condition* cond) const;
257 
258  uint32 GetSearcherTypeMaskForConditionList(ConditionContainer const& conditions) const;
259  bool IsObjectMeetToConditions(WorldObject* object, ConditionContainer const& conditions) const;
260  bool IsObjectMeetToConditions(WorldObject* object1, WorldObject* object2, ConditionContainer const& conditions) const;
261  bool IsObjectMeetToConditions(ConditionSourceInfo& sourceInfo, ConditionContainer const& conditions) const;
262  static bool CanHaveSourceGroupSet(ConditionSourceType sourceType);
263  static bool CanHaveSourceIdSet(ConditionSourceType sourceType);
264  bool IsObjectMeetingNotGroupedConditions(ConditionSourceType sourceType, uint32 entry, ConditionSourceInfo& sourceInfo) const;
265  bool IsObjectMeetingNotGroupedConditions(ConditionSourceType sourceType, uint32 entry, WorldObject* target0, WorldObject* target1 = nullptr, WorldObject* target2 = nullptr) const;
266  bool HasConditionsForNotGroupedEntry(ConditionSourceType sourceType, uint32 entry) const;
267  bool IsObjectMeetingSpellClickConditions(uint32 creatureId, uint32 spellId, WorldObject* clicker, WorldObject* target) const;
268  ConditionContainer const* GetConditionsForSpellClickEvent(uint32 creatureId, uint32 spellId) const;
269  bool IsObjectMeetingVehicleSpellConditions(uint32 creatureId, uint32 spellId, Player* player, Unit* vehicle) const;
270  bool IsObjectMeetingSmartEventConditions(int64 entryOrGuid, uint32 eventId, uint32 sourceType, Unit* unit, WorldObject* baseObject) const;
271  bool IsObjectMeetingVendorItemConditions(uint32 creatureId, uint32 itemId, Player* player, Creature* vendor) const;
272 
273  static bool IsPlayerMeetingCondition(Player* player, PlayerConditionEntry const* condition);
274 
276  {
277  char const* Name;
281  };
282  static char const* const StaticSourceTypeData[CONDITION_SOURCE_TYPE_MAX];
283  static ConditionTypeInfo const StaticConditionTypeData[CONDITION_MAX];
284 
285  private:
286  bool isSourceTypeValid(Condition* cond) const;
287  bool addToLootTemplate(Condition* cond, LootTemplate* loot) const;
288  bool addToGossipMenus(Condition* cond) const;
289  bool addToGossipMenuItems(Condition* cond) const;
290  bool addToSpellImplicitTargetConditions(Condition* cond) const;
291  bool addToPhases(Condition* cond) const;
292  bool IsObjectMeetToConditionList(ConditionSourceInfo& sourceInfo, ConditionContainer const& conditions) const;
293 
294  static void LogUselessConditionValue(Condition* cond, uint8 index, uint32 value);
295 
296  void Clean(); // free up resources
297  std::vector<Condition*> AllocatedMemoryStore; // some garbage collection :)
298 
305 };
306 
307 #define sConditionMgr ConditionMgr::instance()
308 
309 #endif
Definition: ConditionMgr.h:67
Definition: ConditionMgr.h:173
bool HasConditionValue2
Definition: ConditionMgr.h:279
Definition: ConditionMgr.h:55
uint32 ElseGroup
Definition: ConditionMgr.h:200
Definition: ConditionMgr.h:81
ConditionEntriesByTypeArray ConditionStore
Definition: ConditionMgr.h:299
ConditionSourceType
Definition: ConditionMgr.h:125
bool HasConditionValue3
Definition: ConditionMgr.h:280
Definition: ConditionMgr.h:132
Definition: ConditionMgr.h:52
ConditionEntriesByCreatureIdMap SpellClickEventConditionStore
Definition: ConditionMgr.h:302
Definition: ConditionMgr.h:194
Definition: ConditionMgr.h:83
Definition: ConditionMgr.h:69
int64_t int64
Definition: Define.h:145
std::vector< Condition * > AllocatedMemoryStore
Definition: ConditionMgr.h:297
Definition: ConditionMgr.h:75
Definition: ConditionMgr.h:134
MaxConditionTargets
Definition: ConditionMgr.h:176
Definition: ConditionMgr.h:59
Definition: ConditionMgr.h:162
Definition: ConditionMgr.h:85
Definition: ConditionMgr.h:91
ConditionEntriesByCreatureIdMap VehicleSpellConditionStore
Definition: ConditionMgr.h:301
Definition: ConditionMgr.h:62
Definition: ConditionMgr.h:72
Definition: ConditionMgr.h:92
Definition: ConditionMgr.h:76
Definition: ConditionMgr.h:139
Definition: ConditionMgr.h:172
int32 SourceEntry
Definition: ConditionMgr.h:198
Definition: Object.h:423
Definition: Creature.h:467
Definition: ConditionMgr.h:152
Definition: ConditionMgr.h:150
ConditionTypes
Definition: ConditionMgr.h:49
Definition: ConditionMgr.h:275
Definition: ConditionMgr.h:151
Definition: ConditionMgr.h:56
InstanceInfo
Definition: ConditionMgr.h:168
uint32 SourceId
Definition: ConditionMgr.h:199
uint32 ErrorTextId
Definition: ConditionMgr.h:206
Definition: ConditionMgr.h:128
Definition: ConditionMgr.h:146
Definition: ConditionMgr.h:80
bool isLoaded() const
Definition: ConditionMgr.h:233
Condition()
Definition: ConditionMgr.h:212
uint32 ErrorType
Definition: ConditionMgr.h:205
string ToString(int i)
Definition: strutil.h:491
Definition: ConditionMgr.h:93
Definition: ConditionMgr.h:70
Definition: ConditionMgr.h:54
Definition: ConditionMgr.h:246
Definition: ConditionMgr.h:163
std::unordered_map< uint32, ConditionContainer > ConditionReferenceContainer
Definition: ConditionMgr.h:244
uint32 SourceGroup
Definition: ConditionMgr.h:197
Definition: ConditionMgr.h:61
Definition: ConditionMgr.h:138
Definition: ConditionMgr.h:145
Definition: ConditionMgr.h:73
Definition: ConditionMgr.h:137
int32_t int32
Definition: Define.h:146
uint8 ConditionTarget
Definition: ConditionMgr.h:209
Definition: ConditionMgr.h:64
ConditionSourceInfo(WorldObject *target0, WorldObject *target1=nullptr, WorldObject *target2=nullptr)
Definition: ConditionMgr.h:185
uint32_t uint32
Definition: Define.h:150
Definition: ConditionMgr.h:66
Definition: ConditionMgr.h:159
Definition: ConditionMgr.h:51
Definition: ConditionMgr.h:181
Definition: ConditionMgr.h:78
Definition: ConditionMgr.h:58
Definition: ConditionMgr.h:165
uint32 ConditionValue3
Definition: ConditionMgr.h:204
Definition: ConditionMgr.h:57
std::unordered_map< uint32, ConditionsByEntryMap > ConditionEntriesByCreatureIdMap
Definition: ConditionMgr.h:242
Definition: ConditionMgr.h:142
bool HasConditionValue1
Definition: ConditionMgr.h:278
SmartEventConditionContainer SmartEventConditionStore
Definition: ConditionMgr.h:304
Definition: ConditionMgr.h:161
bool NegativeCondition
Definition: ConditionMgr.h:210
Definition: ConditionMgr.h:90
Definition: ConditionMgr.h:133
uint32 ConditionValue2
Definition: ConditionMgr.h:203
ConditionReferenceContainer ConditionReferenceStore
Definition: ConditionMgr.h:300
Definition: ConditionMgr.h:87
Definition: ConditionMgr.h:154
Definition: ConditionMgr.h:149
Definition: ConditionMgr.h:127
Definition: LootMgr.h:250
uint32 ScriptId
Definition: ConditionMgr.h:208
uint32 ConditionValue1
Definition: ConditionMgr.h:202
Definition: ConditionMgr.h:77
std::unordered_map< uint32, ConditionContainer > ConditionsByEntryMap
Definition: ConditionMgr.h:240
std::unordered_map< std::pair< int32, uint32 >, ConditionsByEntryMap > SmartEventConditionContainer
Definition: ConditionMgr.h:243
Definition: ConditionMgr.h:71
#define TC_GAME_API
Definition: Define.h:134
int32_t int32
Definition: g3dmath.h:167
uint8_t uint8
Definition: Define.h:152
Definition: ConditionMgr.h:141
Definition: DB2Structure.h:992
Definition: ConditionMgr.h:86
Definition: ConditionMgr.h:147
Definition: ConditionMgr.h:79
const FieldDescriptor value
Definition: descriptor.h:1522
Definition: ConditionMgr.h:153
Definition: ConditionMgr.h:144
Definition: ConditionMgr.h:160
Definition: ConditionMgr.h:164
RelationType
Definition: ConditionMgr.h:157
Definition: ConditionMgr.h:63
uint32 ReferenceId
Definition: ConditionMgr.h:207
std::vector< Condition * > ConditionContainer
Definition: ConditionMgr.h:239
Definition: ConditionMgr.h:171
Definition: ConditionMgr.h:170
std::array< ConditionsByEntryMap, CONDITION_SOURCE_TYPE_MAX > ConditionEntriesByTypeArray
Definition: ConditionMgr.h:241
Definition: ConditionMgr.h:88
Definition: Unit.h:1305
Definition: ConditionMgr.h:140
Definition: ConditionMgr.h:148
Definition: ConditionMgr.h:178
ConditionEntriesByCreatureIdMap NpcVendorConditionContainerStore
Definition: ConditionMgr.h:303
Definition: ConditionMgr.h:74
Definition: ConditionMgr.h:130
ConditionTypes ConditionType
Definition: ConditionMgr.h:201
Definition: ConditionMgr.h:84
Definition: ConditionMgr.h:89
Condition const * mLastFailedCondition
Definition: ConditionMgr.h:184
Definition: ConditionMgr.h:60
ConditionSourceType SourceType
Definition: ConditionMgr.h:196
Definition: ConditionMgr.h:53
char const * Name
Definition: ConditionMgr.h:277
Definition: ConditionMgr.h:68
Definition: ConditionMgr.h:82
Definition: ConditionMgr.h:65