OregonCore  revision fb2a440-git
Your Favourite TBC server
SmartScript.h
Go to the documentation of this file.
1 /*
2 * This file is part of the OregonCore 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 OREGON_SMARTSCRIPT_H
19 #define OREGON_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();
40 
41  void ProcessEventsFor(SMART_EVENT e, Unit* unit = NULL, uint32 var0 = 0, uint32 var1 = 0, bool bvar = false, const SpellEntry* spell = NULL, GameObject* gob = NULL);
42  void ProcessEvent(SmartScriptHolder& e, Unit* unit = NULL, uint32 var0 = 0, uint32 var1 = 0, bool bvar = false, const SpellEntry* 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);
47  void ProcessAction(SmartScriptHolder& e, Unit* unit = NULL, uint32 var0 = 0, uint32 var1 = 0, bool bvar = false, const SpellEntry* 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 SpellEntry* 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  sLog.outError("SmartScript: Action target Creature (GUID: %u Entry: %u) is not using SmartAI, action called by Creature (GUID: %u Entry: %u) skipped to prevent crash.", c ? c->GetDBTableGUIDLow() : 0, c ? c->GetEntry() : 0, me ? me->GetDBTableGUIDLow() : 0, me ? me->GetEntry() : 0);
122 
123  return smart;
124  }
125 
126  bool IsSmartGO(GameObject* g = NULL)
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  sLog.outError("SmartScript: Action target GameObject (GUID: %u Entry: %u) is not using SmartGameObjectAI, action called by GameObject (GUID: %u Entry: %u) skipped to prevent crash.", g ? g->GetDBTableGUIDLow() : 0, g ? g->GetEntry() : 0, go ? go->GetDBTableGUIDLow() : 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));
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 
178  GameObject* FindGameObjectNear(WorldObject* searchObject, uint32 guid) const
179  {
180  GameObject* gameObject = NULL;
181 
182  CellCoord p(Oregon::ComputeCellCoord(searchObject->GetPositionX(), searchObject->GetPositionY()));
183  Cell cell(p);
184 
185  Oregon::GameObjectWithDbGUIDCheck goCheck(*searchObject, guid);
186  Oregon::GameObjectSearcher<Oregon::GameObjectWithDbGUIDCheck> checker(searchObject, gameObject, goCheck);
187 
189  cell.Visit(p, objectChecker, *searchObject->GetMap(), *searchObject, searchObject->GetGridActivationRange());
190 
191  return gameObject;
192  }
193 
194  Creature* FindCreatureNear(WorldObject* searchObject, uint32 guid) const
195  {
196  Creature* creature = NULL;
197 
198  CellCoord p(Oregon::ComputeCellCoord(searchObject->GetPositionX(), searchObject->GetPositionY()));
199  Cell cell(p);
200 
201  Oregon::CreatureWithDbGUIDCheck target_check(searchObject, guid);
202  Oregon::CreatureSearcher<Oregon::CreatureWithDbGUIDCheck> checker(searchObject, creature, target_check);
203 
205  cell.Visit(p, unit_checker, *searchObject->GetMap(), *searchObject, searchObject->GetGridActivationRange());
206 
207  return creature;
208  }
209 
211 
212  void OnReset();
214  {
215  if (meOrigGUID)
216  {
218  {
219  me = m;
220  go = NULL;
221  }
222  }
223  if (goOrigGUID)
224  {
226  {
227  me = NULL;
228  go = o;
229  }
230  }
231  goOrigGUID.Clear();
232  meOrigGUID.Clear();
233  }
234 
235  //TIMED_ACTIONLIST (script type 9 aka script9)
236  void SetScript9(SmartScriptHolder& e, uint32 entry);
237  Unit* GetLastInvoker();
239  typedef UNORDERED_MAP<uint32, uint32> CounterMap;
240  CounterMap mCounterList;
241 
242  private:
243  void IncPhase(int32 p = 1)
244  {
245  if (p >= 0)
246  mEventPhase += (uint32)p;
247  else
248  DecPhase(abs(p));
249  }
250 
251  void DecPhase(int32 p = 1)
252  {
253  if(mEventPhase > (uint32)p)
254  mEventPhase -= (uint32)p;
255  else
256  mEventPhase = 0;
257  }
258 
259  bool IsInPhase(uint32 p) const { return ((1 << (mEventPhase - 1)) & p) != 0; }
260  void SetPhase(uint32 p = 0) { mEventPhase = p; }
261 
273 
276  std::list<uint32>mRemIDs;
277 
282 
284  void InstallEvents();
285 
287  {
288  if (!mStoredEvents.empty())
289  {
290  for (SmartAIEventList::iterator i = mStoredEvents.begin(); i != mStoredEvents.end(); ++i)
291  {
292  if (i->event_id == id)
293  {
294  mStoredEvents.erase(i);
295  return;
296  }
297  }
298  }
299  }
300 };
301 
302 #endif
float GetGridActivationRange() const
Definition: Object.cpp:1531
void ResetBaseObject()
Definition: SmartScript.h:213
void ProcessTimedAction(SmartScriptHolder &e, uint32 const &min, uint32 const &max, Unit *unit=NULL, uint32 var0=0, uint32 var1=0, bool bvar=false, const SpellEntry *spell=NULL, GameObject *gob=NULL)
void OnUpdate(const uint32 diff)
void FillScript(SmartAIEventList e, WorldObject *obj, AreaTriggerEntry const *at)
SmartScriptType
uint32 mLastTextID
Definition: SmartScript.h:279
uint32 mEventPhase
Definition: SmartScript.h:272
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)
void StoreTargetList(ObjectList *targets, uint32 id)
Definition: SmartScript.h:94
SmartAIEventList mTimedActionList
Definition: SmartScript.h:264
void InstallTemplate(SmartScriptHolder const &e)
Map * GetMap() const
Definition: Object.h:841
uint32 GetDBTableGUIDLow() const
Definition: Creature.h:476
WorldObject * GetBaseObject()
Definition: SmartScript.h:56
CounterMap mCounterList
Definition: SmartScript.h:240
std::vector< SmartScriptHolder > SmartAIEventList
SmartAIEventList mStoredEvents
Definition: SmartScript.h:275
bool CheckTimer(SmartScriptHolder const &e) const
ObjectListMap * mTargetStorage
Definition: SmartScript.h:210
SmartScriptType mScriptType
Definition: SmartScript.h:271
AreaTriggerEntry const * trigger
Definition: SmartScript.h:270
bool isProcessingTimedActionList
Definition: SmartScript.h:265
uint32 mTextTimer
Definition: SmartScript.h:278
bool IsSmart(Creature *c=NULL)
Definition: SmartScript.h:111
#define sLog
Log class singleton.
Definition: Log.h:187
void OnReset()
Definition: SmartScript.cpp:87
bool IsInPhase(uint32 p) const
Definition: SmartScript.h:259
ACE_INT32 int32
Definition: Define.h:67
void InitTimer(SmartScriptHolder &e)
SMART_ACTION
void DecPhase(int32 p=1)
Definition: SmartScript.h:251
SMARTAI_TEMPLATE
ObjectList * GetTargets(SmartScriptHolder const &e, Unit *invoker=NULL)
void DoFindFriendlyMissingBuff(std::list< Creature * > &list, float range, uint32 spellid)
std::string GetAIName() const
bool IsPlayer(WorldObject *obj)
Definition: SmartScript.h:71
void ProcessAction(SmartScriptHolder &e, Unit *unit=NULL, uint32 var0=0, uint32 var1=0, bool bvar=false, const SpellEntry *spell=NULL, GameObject *gob=NULL)
uint8 GetTypeId() const
Definition: Object.h:210
void InstallEvents()
std::list< WorldObject * > ObjectList
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)
void OnInitialize(WorldObject *obj, AreaTriggerEntry const *at=NULL)
Unit * DoSelectLowestHpFriendly(float range, uint32 MinHPDiff)
void IncPhase(int32 p=1)
Definition: SmartScript.h:243
void GetScript()
UNORDERED_MAP< uint32, ObjectGuidList * > ObjectListMap
ObjectGuid meOrigGUID
Definition: SmartScript.h:267
ObjectGuid mLastInvoker
Definition: SmartScript.h:238
bool IsSmartGO(GameObject *g=NULL)
Definition: SmartScript.h:126
float GetPositionY() const
Definition: Position.h:98
SMARTAI_TEMPLATE mTemplate
Definition: SmartScript.h:283
UNORDERED_MAP< uint32, uint32 > CounterMap
Definition: SmartScript.h:239
void RecalcTimer(SmartScriptHolder &e, uint32 min, uint32 max)
etc mysql my cnf *Then change max_allowed_packet to a bigger value
void ProcessEvent(SmartScriptHolder &e, Unit *unit=NULL, uint32 var0=0, uint32 var1=0, bool bvar=false, const SpellEntry *spell=NULL, GameObject *gob=NULL)
SMART_EVENT
void SetPathId(uint32 id)
Definition: SmartScript.h:54
void RemoveStoredEvent(uint32 id)
Definition: SmartScript.h:286
bool IsGameObject(WorldObject *obj)
Definition: SmartScript.h:81
GameObject * go
Definition: SmartScript.h:268
void StoreCounter(uint32 id, uint32 value, uint32 reset)
Definition: SmartScript.h:148
void OnMoveInLineOfSight(Unit *who)
void UpdateTimer(SmartScriptHolder &e, uint32 const diff)
uint32 mTalkerEntry
Definition: SmartScript.h:280
Unit * GetLastInvoker()
CellCoord ComputeCellCoord(float x, float y)
Definition: GridDefines.h:167
ObjectList * GetWorldObjectsInDist(float dist)
void Clear()
Definition: ObjectGuid.h:141
Definition: Cell.h:46
uint32 mPathId
Definition: SmartScript.h:274
uint32 GetPathId() const
Definition: SmartScript.h:55
Creature * FindCreatureNear(WorldObject *searchObject, uint32 guid) const
Definition: SmartScript.h:194
void SetScript9(SmartScriptHolder &e, uint32 entry)
bool IsUnit(WorldObject *obj)
Definition: SmartScript.h:66
void SetPhase(uint32 p=0)
Definition: SmartScript.h:260
void ProcessEventsFor(SMART_EVENT e, Unit *unit=NULL, uint32 var0=0, uint32 var1=0, bool bvar=false, const SpellEntry *spell=NULL, GameObject *gob=NULL)
void DoFindFriendlyCC(std::list< Creature * > &_list, float range)
ObjectGuid goOrigGUID
Definition: SmartScript.h:269
uint32 GetCounterId(uint32 id)
Definition: SmartScript.h:162
uint32 GetCounterValue(uint32 id)
Definition: SmartScript.h:170
uint32 GetEntry() const
Definition: Object.h:192
uint32 GetDBTableGUIDLow() const
Definition: GameObject.h:607
SmartAIEventList mEvents
Definition: SmartScript.h:262
SmartAIEventList mInstallEvents
Definition: SmartScript.h:263
ACE_UINT32 uint32
Definition: Define.h:71
float GetPositionX() const
Definition: Position.h:97
bool IsCreature(WorldObject *obj)
Definition: SmartScript.h:76
Creature * me
Definition: SmartScript.h:266
std::string GetAIName() const
Definition: Creature.cpp:2383
Definition: Unit.h:908
void Visit(CellCoord const &, TypeContainerVisitor< T, CONTAINER > &visitor, Map &, WorldObject const &, float) const
Definition: CellImpl.h:121
bool mUseTextTimer
Definition: SmartScript.h:281
std::list< uint32 > mRemIDs
Definition: SmartScript.h:276
SMARTAI_TARGETS
GameObject * FindGameObjectNear(WorldObject *searchObject, uint32 guid) const
Definition: SmartScript.h:178
Unit * DoFindClosestFriendlyInRange(float range, bool playerOnly)
ObjectList * GetTargetList(uint32 id)
Definition: SmartScript.h:140