TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SmartScript.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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_SMARTSCRIPT_H
19 #define TRINITY_SMARTSCRIPT_H
20 
21 #include "Common.h"
22 #include "Creature.h"
23 #include "CreatureAI.h"
24 #include "Unit.h"
25 #include "Spell.h"
26 #include "GridNotifiers.h"
27 
28 #include "SmartScriptMgr.h"
29 //#include "SmartAI.h"
30 
32 {
33  public:
34  SmartScript();
35  ~SmartScript();
36 
37  void OnInitialize(WorldObject* obj, AreaTriggerEntry const* at = NULL);
38  void GetScript();
39  void FillScript(SmartAIEventList e, WorldObject* obj, AreaTriggerEntry const* at);
40 
41  void ProcessEventsFor(SMART_EVENT e, Unit* unit = NULL, uint32 var0 = 0, uint32 var1 = 0, bool bvar = false, const SpellInfo* spell = NULL, GameObject* gob = NULL);
42  void ProcessEvent(SmartScriptHolder& e, Unit* unit = NULL, uint32 var0 = 0, uint32 var1 = 0, bool bvar = false, const SpellInfo* spell = NULL, GameObject* gob = NULL);
43  bool CheckTimer(SmartScriptHolder const& e) const;
44  void RecalcTimer(SmartScriptHolder& e, uint32 min, uint32 max);
45  void UpdateTimer(SmartScriptHolder& e, uint32 const diff);
46  void InitTimer(SmartScriptHolder& e);
47  void ProcessAction(SmartScriptHolder& e, Unit* unit = NULL, uint32 var0 = 0, uint32 var1 = 0, bool bvar = false, const SpellInfo* spell = NULL, GameObject* gob = NULL);
48  void ProcessTimedAction(SmartScriptHolder& e, uint32 const& min, uint32 const& max, Unit* unit = NULL, uint32 var0 = 0, uint32 var1 = 0, bool bvar = false, const SpellInfo* spell = NULL, GameObject* gob = NULL);
49  ObjectList* GetTargets(SmartScriptHolder const& e, Unit* invoker = NULL);
50  ObjectList* GetWorldObjectsInDist(float dist);
51  void InstallTemplate(SmartScriptHolder const& e);
52  SmartScriptHolder CreateEvent(SMART_EVENT e, uint32 event_flags, uint32 event_param1, uint32 event_param2, uint32 event_param3, uint32 event_param4, SMART_ACTION action, uint32 action_param1, uint32 action_param2, uint32 action_param3, uint32 action_param4, uint32 action_param5, uint32 action_param6, SMARTAI_TARGETS t, uint32 target_param1, uint32 target_param2, uint32 target_param3, uint32 phaseMask = 0);
53  void AddEvent(SMART_EVENT e, uint32 event_flags, uint32 event_param1, uint32 event_param2, uint32 event_param3, uint32 event_param4, SMART_ACTION action, uint32 action_param1, uint32 action_param2, uint32 action_param3, uint32 action_param4, uint32 action_param5, uint32 action_param6, SMARTAI_TARGETS t, uint32 target_param1, uint32 target_param2, uint32 target_param3, uint32 phaseMask = 0);
54  void SetPathId(uint32 id) { mPathId = id; }
55  uint32 GetPathId() const { return mPathId; }
57  {
58  WorldObject* obj = NULL;
59  if (me)
60  obj = me;
61  else if (go)
62  obj = go;
63  return obj;
64  }
65 
66  bool IsUnit(WorldObject* obj)
67  {
68  return obj && (obj->GetTypeId() == TYPEID_UNIT || obj->GetTypeId() == TYPEID_PLAYER);
69  }
70 
71  bool IsPlayer(WorldObject* obj)
72  {
73  return obj && obj->GetTypeId() == TYPEID_PLAYER;
74  }
75 
77  {
78  return obj && obj->GetTypeId() == TYPEID_UNIT;
79  }
80 
82  {
83  return obj && obj->GetTypeId() == TYPEID_GAMEOBJECT;
84  }
85 
86  void OnUpdate(const uint32 diff);
87  void OnMoveInLineOfSight(Unit* who);
88 
89  Unit* DoSelectLowestHpFriendly(float range, uint32 MinHPDiff);
90  void DoFindFriendlyCC(std::list<Creature*>& _list, float range);
91  void DoFindFriendlyMissingBuff(std::list<Creature*>& list, float range, uint32 spellid);
92  Unit* DoFindClosestFriendlyInRange(float range, bool playerOnly);
93 
94  void StoreTargetList(ObjectList* targets, uint32 id)
95  {
96  if (!targets)
97  return;
98 
99  if (mTargetStorage->find(id) != mTargetStorage->end())
100  {
101  // check if already stored
102  if ((*mTargetStorage)[id]->Equals(targets))
103  return;
104 
105  delete (*mTargetStorage)[id];
106  }
107 
108  (*mTargetStorage)[id] = new ObjectGuidList(targets, GetBaseObject());
109  }
110 
111  bool IsSmart(Creature* c = NULL)
112  {
113  bool smart = true;
114  if (c && c->GetAIName() != "SmartAI")
115  smart = false;
116 
117  if (!me || me->GetAIName() != "SmartAI")
118  smart = false;
119 
120  if (!smart)
121  TC_LOG_ERROR("sql.sql", "SmartScript: Action target Creature (GUID: " UI64FMTD " Entry: %u) is not using SmartAI, action called by Creature (GUID: " UI64FMTD " Entry: %u) skipped to prevent crash.", uint64(c ? c->GetSpawnId() : UI64LIT(0)), c ? c->GetEntry() : 0, uint64(me ? me->GetSpawnId() : UI64LIT(0)), me ? me->GetEntry() : 0);
122 
123  return smart;
124  }
125 
127  {
128  bool smart = true;
129  if (g && g->GetAIName() != "SmartGameObjectAI")
130  smart = false;
131 
132  if (!go || go->GetAIName() != "SmartGameObjectAI")
133  smart = false;
134  if (!smart)
135  TC_LOG_ERROR("sql.sql", "SmartScript: Action target GameObject (GUID: " UI64FMTD " Entry: %u) is not using SmartGameObjectAI, action called by GameObject (GUID: " UI64FMTD " Entry: %u) skipped to prevent crash.", uint64(g ? g->GetSpawnId() : UI64LIT(0)), g ? g->GetEntry() : 0, uint64(go ? go->GetSpawnId() : UI64LIT(0)), go ? go->GetEntry() : 0);
136 
137  return smart;
138  }
139 
141  {
142  ObjectListMap::iterator itr = mTargetStorage->find(id);
143  if (itr != mTargetStorage->end())
144  return (*itr).second->GetObjectList();
145  return NULL;
146  }
147 
149  {
150  CounterMap::const_iterator itr = mCounterList.find(id);
151  if (itr != mCounterList.end())
152  {
153  if (reset == 0)
154  value += GetCounterValue(id);
155  mCounterList.erase(id);
156  }
157 
158  mCounterList.insert(std::make_pair(id, value));
159  ProcessEventsFor(SMART_EVENT_COUNTER_SET);
160  }
161 
163  {
164  CounterMap::iterator itr = mCounterList.find(id);
165  if (itr != mCounterList.end())
166  return itr->first;
167  return 0;
168  }
169 
171  {
172  CounterMap::iterator itr = mCounterList.find(id);
173  if (itr != mCounterList.end())
174  return itr->second;
175  return 0;
176  }
177 
179  {
180  auto bounds = searchObject->GetMap()->GetGameObjectBySpawnIdStore().equal_range(guid);
181  if (bounds.first == bounds.second)
182  return nullptr;
183 
184  return bounds.first->second;
185  }
186 
188  {
189  auto bounds = searchObject->GetMap()->GetCreatureBySpawnIdStore().equal_range(guid);
190  if (bounds.first == bounds.second)
191  return nullptr;
192 
193  auto creatureItr = std::find_if(bounds.first, bounds.second, [](Map::CreatureBySpawnIdContainer::value_type const& pair)
194  {
195  return pair.second->IsAlive();
196  });
197 
198  return creatureItr != bounds.second ? creatureItr->second : bounds.first->second;
199  }
200 
202 
203  void OnReset();
205  {
206  WorldObject* lookupRoot = me;
207  if (!lookupRoot)
208  lookupRoot = go;
209 
210  if (lookupRoot)
211  {
212  if (!meOrigGUID.IsEmpty())
213  {
214  if (Creature* m = ObjectAccessor::GetCreature(*lookupRoot, meOrigGUID))
215  {
216  me = m;
217  go = NULL;
218  }
219  }
220  if (!goOrigGUID.IsEmpty())
221  {
222  if (GameObject* o = ObjectAccessor::GetGameObject(*lookupRoot, goOrigGUID))
223  {
224  me = NULL;
225  go = o;
226  }
227  }
228  }
229  goOrigGUID.Clear();
230  meOrigGUID.Clear();
231  }
232 
233  //TIMED_ACTIONLIST (script type 9 aka script9)
234  void SetScript9(SmartScriptHolder& e, uint32 entry);
235  Unit* GetLastInvoker();
237  typedef std::unordered_map<uint32, uint32> CounterMap;
238  CounterMap mCounterList;
239 
240  private:
241  void IncPhase(int32 p = 1)
242  {
243  if (p >= 0)
244  mEventPhase += (uint32)p;
245  else
246  DecPhase(abs(p));
247  }
248 
249  void DecPhase(int32 p = 1)
250  {
251  if (mEventPhase > (uint32)p)
252  mEventPhase -= (uint32)p;
253  else
254  mEventPhase = 0;
255  }
256 
257  bool IsInPhase(uint32 p) const { return ((1 << (mEventPhase - 1)) & p) != 0; }
258  void SetPhase(uint32 p = 0) { mEventPhase = p; }
259 
271 
274  std::list<uint32>mRemIDs;
275 
280 
282  void InstallEvents();
283 
285  {
286  if (!mStoredEvents.empty())
287  {
288  for (SmartAIEventList::iterator i = mStoredEvents.begin(); i != mStoredEvents.end(); ++i)
289  {
290  if (i->event_id == id)
291  {
292  mStoredEvents.erase(i);
293  return;
294  }
295  }
296  }
297  }
298 };
299 
300 #endif
void ResetBaseObject()
Definition: SmartScript.h:204
G3D::Matrix abs(const G3D::Matrix &M)
Definition: Matrix.h:632
SmartScriptType
Definition: SmartScriptMgr.h:1208
uint32 mLastTextID
Definition: SmartScript.h:277
uint32 mEventPhase
Definition: SmartScript.h:270
void StoreTargetList(ObjectList *targets, uint32 id)
Definition: SmartScript.h:94
SmartAIEventList mTimedActionList
Definition: SmartScript.h:262
Map * GetMap() const
Definition: Object.h:543
WorldObject * GetBaseObject()
Definition: SmartScript.h:56
Definition: SpellInfo.h:326
CounterMap mCounterList
Definition: SmartScript.h:238
std::unordered_map< uint32, uint32 > CounterMap
Definition: SmartScript.h:237
std::vector< SmartScriptHolder > SmartAIEventList
Definition: SmartScriptMgr.h:1468
SmartAIEventList mStoredEvents
Definition: SmartScript.h:273
ObjectListMap * mTargetStorage
Definition: SmartScript.h:201
SmartScriptType mScriptType
Definition: SmartScript.h:269
Definition: SmartScriptMgr.h:181
AreaTriggerEntry const * trigger
Definition: SmartScript.h:268
bool isProcessingTimedActionList
Definition: SmartScript.h:263
uint32 mTextTimer
Definition: SmartScript.h:276
bool IsSmart(Creature *c=NULL)
Definition: SmartScript.h:111
#define UI64LIT(N)
Definition: Define.h:138
Definition: ObjectGuid.h:32
bool IsInPhase(uint32 p) const
Definition: SmartScript.h:257
arena_t NULL
Definition: jemalloc_internal.h:624
SMART_ACTION
Definition: SmartScriptMgr.h:437
Definition: SmartScriptMgr.h:1393
void DecPhase(int32 p=1)
Definition: SmartScript.h:249
SMARTAI_TEMPLATE
Definition: SmartScriptMgr.h:1060
uint64_t uint64
Definition: g3dmath.h:170
Definition: Object.h:423
Definition: Creature.h:467
uint64 LowType
Definition: ObjectGuid.h:199
T max(const T &x, const T &y)
Definition: g3dmath.h:320
bool IsPlayer(WorldObject *obj)
Definition: SmartScript.h:71
TypeID GetTypeId() const
Definition: Object.h:113
std::list< WorldObject * > ObjectList
Definition: SmartScriptMgr.h:1391
T min(const T &x, const T &y)
Definition: g3dmath.h:305
void IncPhase(int32 p=1)
Definition: SmartScript.h:241
ObjectGuid meOrigGUID
Definition: SmartScript.h:265
ObjectGuid mLastInvoker
Definition: SmartScript.h:236
bool IsSmartGO(GameObject *g=NULL)
Definition: SmartScript.h:126
SMARTAI_TEMPLATE mTemplate
Definition: SmartScript.h:281
#define UI64FMTD
Definition: Define.h:137
Definition: ObjectGuid.h:34
int32_t int32
Definition: Define.h:146
SMART_EVENT
Definition: SmartScriptMgr.h:102
uint32_t uint32
Definition: Define.h:150
void SetPathId(uint32 id)
Definition: SmartScript.h:54
void RemoveStoredEvent(uint32 id)
Definition: SmartScript.h:284
Definition: GameObject.h:880
bool IsGameObject(WorldObject *obj)
Definition: SmartScript.h:81
std::unordered_map< uint32, ObjectGuidList * > ObjectListMap
Definition: SmartScriptMgr.h:1443
GameObject * go
Definition: SmartScript.h:266
void StoreCounter(uint32 id, uint32 value, uint32 reset)
Definition: SmartScript.h:148
uint32 mTalkerEntry
Definition: SmartScript.h:278
GameObject * FindGameObjectNear(WorldObject *searchObject, ObjectGuid::LowType guid) const
Definition: SmartScript.h:178
TC_GAME_API Creature * GetCreature(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:174
uint32 mPathId
Definition: SmartScript.h:272
uint32 GetPathId() const
Definition: SmartScript.h:55
Definition: ObjectGuid.h:33
#define TC_GAME_API
Definition: Define.h:134
bool IsUnit(WorldObject *obj)
Definition: SmartScript.h:66
void SetPhase(uint32 p=0)
Definition: SmartScript.h:258
TC_GAME_API GameObject * GetGameObject(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:143
CreatureBySpawnIdContainer & GetCreatureBySpawnIdStore()
Definition: Map.h:469
GameObjectBySpawnIdContainer & GetGameObjectBySpawnIdStore()
Definition: Map.h:472
ObjectGuid goOrigGUID
Definition: SmartScript.h:267
uint32 GetCounterId(uint32 id)
Definition: SmartScript.h:162
Definition: DBCStructure.h:74
uint32 GetCounterValue(uint32 id)
Definition: SmartScript.h:170
const FieldDescriptor value
Definition: descriptor.h:1522
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
Definition: ObjectGuid.h:189
Definition: SmartScriptMgr.h:1361
SmartAIEventList mEvents
Definition: SmartScript.h:260
uint32_t uint32
Definition: g3dmath.h:168
SmartAIEventList mInstallEvents
Definition: SmartScript.h:261
Creature * FindCreatureNear(WorldObject *searchObject, ObjectGuid::LowType guid) const
Definition: SmartScript.h:187
bool IsCreature(WorldObject *obj)
Definition: SmartScript.h:76
Creature * me
Definition: SmartScript.h:264
Definition: Unit.h:1305
bool mUseTextTimer
Definition: SmartScript.h:279
Definition: SmartScript.h:31
std::list< uint32 > mRemIDs
Definition: SmartScript.h:274
SMARTAI_TARGETS
Definition: SmartScriptMgr.h:1071
ObjectList * GetTargetList(uint32 id)
Definition: SmartScript.h:140