PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/server/game/Scripting/ScriptMgr.h

https://gitlab.com/IlluminatiCore/IlluminatiCore
C Header | 1137 lines | 591 code | 316 blank | 230 comment | 1 complexity | d324d289d6069b19ccda316a5b815e47 MD5 | raw file
Possible License(s): GPL-2.0, BSD-2-Clause
  1. /*
  2. * Copyright (C) 2008-2014 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. #ifndef SC_SCRIPTMGR_H
  19. #define SC_SCRIPTMGR_H
  20. #include "Common.h"
  21. #include <atomic>
  22. #include "DBCStores.h"
  23. #include "QuestDef.h"
  24. #include "SharedDefines.h"
  25. #include "World.h"
  26. #include "Weather.h"
  27. class AccountMgr;
  28. class AuctionHouseObject;
  29. class AuraScript;
  30. class Battleground;
  31. class BattlegroundMap;
  32. class Channel;
  33. class ChatCommand;
  34. class Creature;
  35. class CreatureAI;
  36. class DynamicObject;
  37. class GameObject;
  38. class GameObjectAI;
  39. class Guild;
  40. class GridMap;
  41. class Group;
  42. class InstanceMap;
  43. class InstanceScript;
  44. class Item;
  45. class Map;
  46. class OutdoorPvP;
  47. class Player;
  48. class Quest;
  49. class ScriptMgr;
  50. class Spell;
  51. class SpellScript;
  52. class SpellCastTargets;
  53. class Transport;
  54. class Unit;
  55. class Vehicle;
  56. class WorldPacket;
  57. class WorldSocket;
  58. class WorldObject;
  59. class WorldSession;
  60. struct AuctionEntry;
  61. struct ConditionSourceInfo;
  62. struct Condition;
  63. struct ItemTemplate;
  64. struct OutdoorPvPData;
  65. #define VISIBLE_RANGE 166.0f //MAX visible range (size of grid)
  66. /*
  67. @todo Add more script type classes.
  68. MailScript
  69. SessionScript
  70. CollisionScript
  71. ArenaTeamScript
  72. */
  73. /*
  74. Standard procedure when adding new script type classes:
  75. First of all, define the actual class, and have it inherit from ScriptObject, like so:
  76. class MyScriptType : public ScriptObject
  77. {
  78. uint32 _someId;
  79. private:
  80. void RegisterSelf();
  81. protected:
  82. MyScriptType(const char* name, uint32 someId)
  83. : ScriptObject(name), _someId(someId)
  84. {
  85. ScriptRegistry<MyScriptType>::AddScript(this);
  86. }
  87. public:
  88. // If a virtual function in your script type class is not necessarily
  89. // required to be overridden, just declare it virtual with an empty
  90. // body. If, on the other hand, it's logical only to override it (i.e.
  91. // if it's the only method in the class), make it pure virtual, by adding
  92. // = 0 to it.
  93. virtual void OnSomeEvent(uint32 someArg1, std::string& someArg2) { }
  94. // This is a pure virtual function:
  95. virtual void OnAnotherEvent(uint32 someArg) = 0;
  96. }
  97. Next, you need to add a specialization for ScriptRegistry. Put this in the bottom of
  98. ScriptMgr.cpp:
  99. template class ScriptRegistry<MyScriptType>;
  100. Now, add a cleanup routine in ScriptMgr::~ScriptMgr:
  101. SCR_CLEAR(MyScriptType);
  102. Now your script type is good to go with the script system. What you need to do now
  103. is add functions to ScriptMgr that can be called from the core to actually trigger
  104. certain events. For example, in ScriptMgr.h:
  105. void OnSomeEvent(uint32 someArg1, std::string& someArg2);
  106. void OnAnotherEvent(uint32 someArg);
  107. In ScriptMgr.cpp:
  108. void ScriptMgr::OnSomeEvent(uint32 someArg1, std::string& someArg2)
  109. {
  110. FOREACH_SCRIPT(MyScriptType)->OnSomeEvent(someArg1, someArg2);
  111. }
  112. void ScriptMgr::OnAnotherEvent(uint32 someArg)
  113. {
  114. FOREACH_SCRIPT(MyScriptType)->OnAnotherEvent(someArg1, someArg2);
  115. }
  116. Now you simply call these two functions from anywhere in the core to trigger the
  117. event on all registered scripts of that type.
  118. */
  119. class ScriptObject
  120. {
  121. friend class ScriptMgr;
  122. public:
  123. // Do not override this in scripts; it should be overridden by the various script type classes. It indicates
  124. // whether or not this script type must be assigned in the database.
  125. virtual bool IsDatabaseBound() const { return false; }
  126. const std::string& GetName() const { return _name; }
  127. protected:
  128. ScriptObject(const char* name)
  129. : _name(name)
  130. {
  131. }
  132. virtual ~ScriptObject()
  133. {
  134. }
  135. private:
  136. const std::string _name;
  137. };
  138. template<class TObject> class UpdatableScript
  139. {
  140. protected:
  141. UpdatableScript()
  142. {
  143. }
  144. virtual ~UpdatableScript() { }
  145. public:
  146. virtual void OnUpdate(TObject* /*obj*/, uint32 /*diff*/) { }
  147. };
  148. class SpellScriptLoader : public ScriptObject
  149. {
  150. protected:
  151. SpellScriptLoader(const char* name);
  152. public:
  153. bool IsDatabaseBound() const final override { return true; }
  154. // Should return a fully valid SpellScript pointer.
  155. virtual SpellScript* GetSpellScript() const { return NULL; }
  156. // Should return a fully valid AuraScript pointer.
  157. virtual AuraScript* GetAuraScript() const { return NULL; }
  158. };
  159. class ServerScript : public ScriptObject
  160. {
  161. protected:
  162. ServerScript(const char* name);
  163. public:
  164. // Called when reactive socket I/O is started (WorldTcpSessionMgr).
  165. virtual void OnNetworkStart() { }
  166. // Called when reactive I/O is stopped.
  167. virtual void OnNetworkStop() { }
  168. // Called when a remote socket establishes a connection to the server. Do not store the socket object.
  169. virtual void OnSocketOpen(std::shared_ptr<WorldSocket> /*socket*/) { }
  170. // Called when a socket is closed. Do not store the socket object, and do not rely on the connection
  171. // being open; it is not.
  172. virtual void OnSocketClose(std::shared_ptr<WorldSocket> /*socket*/) { }
  173. // Called when a packet is sent to a client. The packet object is a copy of the original packet, so reading
  174. // and modifying it is safe.
  175. virtual void OnPacketSend(WorldSession* /*session*/, WorldPacket& /*packet*/) { }
  176. // Called when a (valid) packet is received by a client. The packet object is a copy of the original packet, so
  177. // reading and modifying it is safe. Make sure to check WorldSession pointer before usage, it might be null in case of auth packets
  178. virtual void OnPacketReceive(WorldSession* /*session*/, WorldPacket& /*packet*/) { }
  179. // Called when an invalid (unknown opcode) packet is received by a client. The packet is a reference to the orignal
  180. // packet; not a copy. This allows you to actually handle unknown packets (for whatever purpose).
  181. virtual void OnUnknownPacketReceive(WorldSession* /*session*/, WorldPacket& /*packet*/) { }
  182. };
  183. class WorldScript : public ScriptObject
  184. {
  185. protected:
  186. WorldScript(const char* name);
  187. public:
  188. // Called when the open/closed state of the world changes.
  189. virtual void OnOpenStateChange(bool /*open*/) { }
  190. // Called after the world configuration is (re)loaded.
  191. virtual void OnConfigLoad(bool /*reload*/) { }
  192. // Called before the message of the day is changed.
  193. virtual void OnMotdChange(std::string& /*newMotd*/) { }
  194. // Called when a world shutdown is initiated.
  195. virtual void OnShutdownInitiate(ShutdownExitCode /*code*/, ShutdownMask /*mask*/) { }
  196. // Called when a world shutdown is cancelled.
  197. virtual void OnShutdownCancel() { }
  198. // Called on every world tick (don't execute too heavy code here).
  199. virtual void OnUpdate(uint32 /*diff*/) { }
  200. // Called when the world is started.
  201. virtual void OnStartup() { }
  202. // Called when the world is actually shut down.
  203. virtual void OnShutdown() { }
  204. };
  205. class FormulaScript : public ScriptObject
  206. {
  207. protected:
  208. FormulaScript(const char* name);
  209. public:
  210. // Called after calculating honor.
  211. virtual void OnHonorCalculation(float& /*honor*/, uint8 /*level*/, float /*multiplier*/) { }
  212. // Called after gray level calculation.
  213. virtual void OnGrayLevelCalculation(uint8& /*grayLevel*/, uint8 /*playerLevel*/) { }
  214. // Called after calculating experience color.
  215. virtual void OnColorCodeCalculation(XPColorChar& /*color*/, uint8 /*playerLevel*/, uint8 /*mobLevel*/) { }
  216. // Called after calculating zero difference.
  217. virtual void OnZeroDifferenceCalculation(uint8& /*diff*/, uint8 /*playerLevel*/) { }
  218. // Called after calculating base experience gain.
  219. virtual void OnBaseGainCalculation(uint32& /*gain*/, uint8 /*playerLevel*/, uint8 /*mobLevel*/, ContentLevels /*content*/) { }
  220. // Called after calculating experience gain.
  221. virtual void OnGainCalculation(uint32& /*gain*/, Player* /*player*/, Unit* /*unit*/) { }
  222. // Called when calculating the experience rate for group experience.
  223. virtual void OnGroupRateCalculation(float& /*rate*/, uint32 /*count*/, bool /*isRaid*/) { }
  224. };
  225. template<class TMap> class MapScript : public UpdatableScript<TMap>
  226. {
  227. MapEntry const* _mapEntry;
  228. protected:
  229. MapScript(uint32 mapId)
  230. : _mapEntry(sMapStore.LookupEntry(mapId))
  231. {
  232. if (!_mapEntry)
  233. TC_LOG_ERROR("scripts", "Invalid MapScript for %u; no such map ID.", mapId);
  234. }
  235. public:
  236. // Gets the MapEntry structure associated with this script. Can return NULL.
  237. MapEntry const* GetEntry() { return _mapEntry; }
  238. // Called when the map is created.
  239. virtual void OnCreate(TMap* /*map*/) { }
  240. // Called just before the map is destroyed.
  241. virtual void OnDestroy(TMap* /*map*/) { }
  242. // Called when a grid map is loaded.
  243. virtual void OnLoadGridMap(TMap* /*map*/, GridMap* /*gmap*/, uint32 /*gx*/, uint32 /*gy*/) { }
  244. // Called when a grid map is unloaded.
  245. virtual void OnUnloadGridMap(TMap* /*map*/, GridMap* /*gmap*/, uint32 /*gx*/, uint32 /*gy*/) { }
  246. // Called when a player enters the map.
  247. virtual void OnPlayerEnter(TMap* /*map*/, Player* /*player*/) { }
  248. // Called when a player leaves the map.
  249. virtual void OnPlayerLeave(TMap* /*map*/, Player* /*player*/) { }
  250. };
  251. class WorldMapScript : public ScriptObject, public MapScript<Map>
  252. {
  253. protected:
  254. WorldMapScript(const char* name, uint32 mapId);
  255. };
  256. class InstanceMapScript : public ScriptObject, public MapScript<InstanceMap>
  257. {
  258. protected:
  259. InstanceMapScript(const char* name, uint32 mapId);
  260. public:
  261. bool IsDatabaseBound() const final override { return true; }
  262. // Gets an InstanceScript object for this instance.
  263. virtual InstanceScript* GetInstanceScript(InstanceMap* /*map*/) const { return NULL; }
  264. };
  265. class BattlegroundMapScript : public ScriptObject, public MapScript<BattlegroundMap>
  266. {
  267. protected:
  268. BattlegroundMapScript(const char* name, uint32 mapId);
  269. };
  270. class ItemScript : public ScriptObject
  271. {
  272. protected:
  273. ItemScript(const char* name);
  274. public:
  275. bool IsDatabaseBound() const final override { return true; }
  276. // Called when a dummy spell effect is triggered on the item.
  277. virtual bool OnDummyEffect(Unit* /*caster*/, uint32 /*spellId*/, SpellEffIndex /*effIndex*/, Item* /*target*/) { return false; }
  278. // Called when a player accepts a quest from the item.
  279. virtual bool OnQuestAccept(Player* /*player*/, Item* /*item*/, Quest const* /*quest*/) { return false; }
  280. // Called when a player uses the item.
  281. virtual bool OnUse(Player* /*player*/, Item* /*item*/, SpellCastTargets const& /*targets*/) { return false; }
  282. // Called when the item expires (is destroyed).
  283. virtual bool OnExpire(Player* /*player*/, ItemTemplate const* /*proto*/) { return false; }
  284. // Called when the item is destroyed.
  285. virtual bool OnRemove(Player* /*player*/, Item* /*item*/) { return false; }
  286. };
  287. class UnitScript : public ScriptObject
  288. {
  289. protected:
  290. UnitScript(const char* name, bool addToScripts = true);
  291. public:
  292. // Called when a unit deals healing to another unit
  293. virtual void OnHeal(Unit* /*healer*/, Unit* /*reciever*/, uint32& /*gain*/) { }
  294. // Called when a unit deals damage to another unit
  295. virtual void OnDamage(Unit* /*attacker*/, Unit* /*victim*/, uint32& /*damage*/) { }
  296. // Called when DoT's Tick Damage is being Dealt
  297. virtual void ModifyPeriodicDamageAurasTick(Unit* /*target*/, Unit* /*attacker*/, uint32& /*damage*/) { }
  298. // Called when Melee Damage is being Dealt
  299. virtual void ModifyMeleeDamage(Unit* /*target*/, Unit* /*attacker*/, uint32& /*damage*/) { }
  300. // Called when Spell Damage is being Dealt
  301. virtual void ModifySpellDamageTaken(Unit* /*target*/, Unit* /*attacker*/, int32& /*damage*/) { }
  302. };
  303. class CreatureScript : public UnitScript, public UpdatableScript<Creature>
  304. {
  305. protected:
  306. CreatureScript(const char* name);
  307. public:
  308. bool IsDatabaseBound() const final override { return true; }
  309. // Called when a dummy spell effect is triggered on the creature.
  310. virtual bool OnDummyEffect(Unit* /*caster*/, uint32 /*spellId*/, SpellEffIndex /*effIndex*/, Creature* /*target*/) { return false; }
  311. // Called when a player opens a gossip dialog with the creature.
  312. virtual bool OnGossipHello(Player* /*player*/, Creature* /*creature*/) { return false; }
  313. // Called when a player selects a gossip item in the creature's gossip menu.
  314. virtual bool OnGossipSelect(Player* /*player*/, Creature* /*creature*/, uint32 /*sender*/, uint32 /*action*/) { return false; }
  315. // Called when a player selects a gossip with a code in the creature's gossip menu.
  316. virtual bool OnGossipSelectCode(Player* /*player*/, Creature* /*creature*/, uint32 /*sender*/, uint32 /*action*/, const char* /*code*/) { return false; }
  317. // Called when a player accepts a quest from the creature.
  318. virtual bool OnQuestAccept(Player* /*player*/, Creature* /*creature*/, Quest const* /*quest*/) { return false; }
  319. // Called when a player selects a quest in the creature's quest menu.
  320. virtual bool OnQuestSelect(Player* /*player*/, Creature* /*creature*/, Quest const* /*quest*/) { return false; }
  321. // Called when a player completes a quest and is rewarded, opt is the selected item's index or 0
  322. virtual bool OnQuestReward(Player* /*player*/, Creature* /*creature*/, Quest const* /*quest*/, uint32 /*opt*/) { return false; }
  323. // Called when the dialog status between a player and the creature is requested.
  324. virtual uint32 GetDialogStatus(Player* /*player*/, Creature* /*creature*/) { return DIALOG_STATUS_SCRIPTED_NO_STATUS; }
  325. // Called when a CreatureAI object is needed for the creature.
  326. virtual CreatureAI* GetAI(Creature* /*creature*/) const { return NULL; }
  327. };
  328. class GameObjectScript : public ScriptObject, public UpdatableScript<GameObject>
  329. {
  330. protected:
  331. GameObjectScript(const char* name);
  332. public:
  333. bool IsDatabaseBound() const final override { return true; }
  334. // Called when a dummy spell effect is triggered on the gameobject.
  335. virtual bool OnDummyEffect(Unit* /*caster*/, uint32 /*spellId*/, SpellEffIndex /*effIndex*/, GameObject* /*target*/) { return false; }
  336. // Called when a player opens a gossip dialog with the gameobject.
  337. virtual bool OnGossipHello(Player* /*player*/, GameObject* /*go*/) { return false; }
  338. // Called when a player selects a gossip item in the gameobject's gossip menu.
  339. virtual bool OnGossipSelect(Player* /*player*/, GameObject* /*go*/, uint32 /*sender*/, uint32 /*action*/) { return false; }
  340. // Called when a player selects a gossip with a code in the gameobject's gossip menu.
  341. virtual bool OnGossipSelectCode(Player* /*player*/, GameObject* /*go*/, uint32 /*sender*/, uint32 /*action*/, const char* /*code*/) { return false; }
  342. // Called when a player accepts a quest from the gameobject.
  343. virtual bool OnQuestAccept(Player* /*player*/, GameObject* /*go*/, Quest const* /*quest*/) { return false; }
  344. // Called when a player completes a quest and is rewarded, opt is the selected item's index or 0
  345. virtual bool OnQuestReward(Player* /*player*/, GameObject* /*go*/, Quest const* /*quest*/, uint32 /*opt*/) { return false; }
  346. // Called when the dialog status between a player and the gameobject is requested.
  347. virtual uint32 GetDialogStatus(Player* /*player*/, GameObject* /*go*/) { return DIALOG_STATUS_SCRIPTED_NO_STATUS; }
  348. // Called when the game object is destroyed (destructible buildings only).
  349. virtual void OnDestroyed(GameObject* /*go*/, Player* /*player*/) { }
  350. // Called when the game object is damaged (destructible buildings only).
  351. virtual void OnDamaged(GameObject* /*go*/, Player* /*player*/) { }
  352. // Called when the game object loot state is changed.
  353. virtual void OnLootStateChanged(GameObject* /*go*/, uint32 /*state*/, Unit* /*unit*/) { }
  354. // Called when the game object state is changed.
  355. virtual void OnGameObjectStateChanged(GameObject* /*go*/, uint32 /*state*/) { }
  356. // Called when a GameObjectAI object is needed for the gameobject.
  357. virtual GameObjectAI* GetAI(GameObject* /*go*/) const { return NULL; }
  358. };
  359. class AreaTriggerScript : public ScriptObject
  360. {
  361. protected:
  362. AreaTriggerScript(const char* name);
  363. public:
  364. bool IsDatabaseBound() const final override { return true; }
  365. // Called when the area trigger is activated by a player.
  366. virtual bool OnTrigger(Player* /*player*/, AreaTriggerEntry const* /*trigger*/) { return false; }
  367. };
  368. class BattlegroundScript : public ScriptObject
  369. {
  370. protected:
  371. BattlegroundScript(const char* name);
  372. public:
  373. bool IsDatabaseBound() const final override { return true; }
  374. // Should return a fully valid Battleground object for the type ID.
  375. virtual Battleground* GetBattleground() const = 0;
  376. };
  377. class OutdoorPvPScript : public ScriptObject
  378. {
  379. protected:
  380. OutdoorPvPScript(const char* name);
  381. public:
  382. bool IsDatabaseBound() const final override { return true; }
  383. // Should return a fully valid OutdoorPvP object for the type ID.
  384. virtual OutdoorPvP* GetOutdoorPvP() const = 0;
  385. };
  386. class CommandScript : public ScriptObject
  387. {
  388. protected:
  389. CommandScript(const char* name);
  390. public:
  391. // Should return a pointer to a valid command table (ChatCommand array) to be used by ChatHandler.
  392. virtual ChatCommand* GetCommands() const = 0;
  393. };
  394. class WeatherScript : public ScriptObject, public UpdatableScript<Weather>
  395. {
  396. protected:
  397. WeatherScript(const char* name);
  398. public:
  399. bool IsDatabaseBound() const final override { return true; }
  400. // Called when the weather changes in the zone this script is associated with.
  401. virtual void OnChange(Weather* /*weather*/, WeatherState /*state*/, float /*grade*/) { }
  402. };
  403. class AuctionHouseScript : public ScriptObject
  404. {
  405. protected:
  406. AuctionHouseScript(const char* name);
  407. public:
  408. // Called when an auction is added to an auction house.
  409. virtual void OnAuctionAdd(AuctionHouseObject* /*ah*/, AuctionEntry* /*entry*/) { }
  410. // Called when an auction is removed from an auction house.
  411. virtual void OnAuctionRemove(AuctionHouseObject* /*ah*/, AuctionEntry* /*entry*/) { }
  412. // Called when an auction was succesfully completed.
  413. virtual void OnAuctionSuccessful(AuctionHouseObject* /*ah*/, AuctionEntry* /*entry*/) { }
  414. // Called when an auction expires.
  415. virtual void OnAuctionExpire(AuctionHouseObject* /*ah*/, AuctionEntry* /*entry*/) { }
  416. };
  417. class ConditionScript : public ScriptObject
  418. {
  419. protected:
  420. ConditionScript(const char* name);
  421. public:
  422. bool IsDatabaseBound() const final override { return true; }
  423. // Called when a single condition is checked for a player.
  424. virtual bool OnConditionCheck(Condition* /*condition*/, ConditionSourceInfo& /*sourceInfo*/) { return true; }
  425. };
  426. class VehicleScript : public ScriptObject
  427. {
  428. protected:
  429. VehicleScript(const char* name);
  430. public:
  431. // Called after a vehicle is installed.
  432. virtual void OnInstall(Vehicle* /*veh*/) { }
  433. // Called after a vehicle is uninstalled.
  434. virtual void OnUninstall(Vehicle* /*veh*/) { }
  435. // Called when a vehicle resets.
  436. virtual void OnReset(Vehicle* /*veh*/) { }
  437. // Called after an accessory is installed in a vehicle.
  438. virtual void OnInstallAccessory(Vehicle* /*veh*/, Creature* /*accessory*/) { }
  439. // Called after a passenger is added to a vehicle.
  440. virtual void OnAddPassenger(Vehicle* /*veh*/, Unit* /*passenger*/, int8 /*seatId*/) { }
  441. // Called after a passenger is removed from a vehicle.
  442. virtual void OnRemovePassenger(Vehicle* /*veh*/, Unit* /*passenger*/) { }
  443. };
  444. class DynamicObjectScript : public ScriptObject, public UpdatableScript<DynamicObject>
  445. {
  446. protected:
  447. DynamicObjectScript(const char* name);
  448. };
  449. class TransportScript : public ScriptObject, public UpdatableScript<Transport>
  450. {
  451. protected:
  452. TransportScript(const char* name);
  453. public:
  454. bool IsDatabaseBound() const final override { return true; }
  455. // Called when a player boards the transport.
  456. virtual void OnAddPassenger(Transport* /*transport*/, Player* /*player*/) { }
  457. // Called when a creature boards the transport.
  458. virtual void OnAddCreaturePassenger(Transport* /*transport*/, Creature* /*creature*/) { }
  459. // Called when a player exits the transport.
  460. virtual void OnRemovePassenger(Transport* /*transport*/, Player* /*player*/) { }
  461. // Called when a transport moves.
  462. virtual void OnRelocate(Transport* /*transport*/, uint32 /*waypointId*/, uint32 /*mapId*/, float /*x*/, float /*y*/, float /*z*/) { }
  463. };
  464. class AchievementCriteriaScript : public ScriptObject
  465. {
  466. protected:
  467. AchievementCriteriaScript(const char* name);
  468. public:
  469. bool IsDatabaseBound() const final override { return true; }
  470. // Called when an additional criteria is checked.
  471. virtual bool OnCheck(Player* source, Unit* target) = 0;
  472. };
  473. class PlayerScript : public UnitScript
  474. {
  475. protected:
  476. PlayerScript(const char* name);
  477. public:
  478. // Called when a player kills another player
  479. virtual void OnPVPKill(Player* /*killer*/, Player* /*killed*/) { }
  480. // Called when a player kills a creature
  481. virtual void OnCreatureKill(Player* /*killer*/, Creature* /*killed*/) { }
  482. // Called when a player is killed by a creature
  483. virtual void OnPlayerKilledByCreature(Creature* /*killer*/, Player* /*killed*/) { }
  484. // Called when a player's level changes (after the level is applied)
  485. virtual void OnLevelChanged(Player* /*player*/, uint8 /*oldLevel*/) { }
  486. // Called when a player's free talent points change (right before the change is applied)
  487. virtual void OnFreeTalentPointsChanged(Player* /*player*/, uint32 /*points*/) { }
  488. // Called when a player's talent points are reset (right before the reset is done)
  489. virtual void OnTalentsReset(Player* /*player*/, bool /*noCost*/) { }
  490. // Called when a player's money is modified (before the modification is done)
  491. virtual void OnMoneyChanged(Player* /*player*/, int64& /*amount*/) { }
  492. // Called when a player's money is at limit (amount = money tried to add)
  493. virtual void OnMoneyLimit(Player* /*player*/, int64 /*amount*/) { }
  494. // Called when a player gains XP (before anything is given)
  495. virtual void OnGiveXP(Player* /*player*/, uint32& /*amount*/, Unit* /*victim*/) { }
  496. // Called when a player's reputation changes (before it is actually changed)
  497. virtual void OnReputationChange(Player* /*player*/, uint32 /*factionId*/, int32& /*standing*/, bool /*incremental*/) { }
  498. // Called when a duel is requested
  499. virtual void OnDuelRequest(Player* /*target*/, Player* /*challenger*/) { }
  500. // Called when a duel starts (after 3s countdown)
  501. virtual void OnDuelStart(Player* /*player1*/, Player* /*player2*/) { }
  502. // Called when a duel ends
  503. virtual void OnDuelEnd(Player* /*winner*/, Player* /*loser*/, DuelCompleteType /*type*/) { }
  504. // The following methods are called when a player sends a chat message.
  505. virtual void OnChat(Player* /*player*/, uint32 /*type*/, uint32 /*lang*/, std::string& /*msg*/) { }
  506. virtual void OnChat(Player* /*player*/, uint32 /*type*/, uint32 /*lang*/, std::string& /*msg*/, Player* /*receiver*/) { }
  507. virtual void OnChat(Player* /*player*/, uint32 /*type*/, uint32 /*lang*/, std::string& /*msg*/, Group* /*group*/) { }
  508. virtual void OnChat(Player* /*player*/, uint32 /*type*/, uint32 /*lang*/, std::string& /*msg*/, Guild* /*guild*/) { }
  509. virtual void OnChat(Player* /*player*/, uint32 /*type*/, uint32 /*lang*/, std::string& /*msg*/, Channel* /*channel*/) { }
  510. // Both of the below are called on emote opcodes.
  511. virtual void OnEmote(Player* /*player*/, uint32 /*emote*/) { }
  512. virtual void OnTextEmote(Player* /*player*/, uint32 /*textEmote*/, uint32 /*emoteNum*/, ObjectGuid /*guid*/) { }
  513. // Called in Spell::Cast.
  514. virtual void OnSpellCast(Player* /*player*/, Spell* /*spell*/, bool /*skipCheck*/) { }
  515. // Called when a player logs in.
  516. virtual void OnLogin(Player* /*player*/, bool /*firstLogin*/) { }
  517. // Called when a player logs out.
  518. virtual void OnLogout(Player* /*player*/) { }
  519. // Called when a player is created.
  520. virtual void OnCreate(Player* /*player*/) { }
  521. // Called when a player is deleted.
  522. virtual void OnDelete(ObjectGuid /*guid*/, uint32 /*accountId*/) { }
  523. // Called when a player delete failed
  524. virtual void OnFailedDelete(ObjectGuid /*guid*/, uint32 /*accountId*/) { }
  525. // Called when a player is about to be saved.
  526. virtual void OnSave(Player* /*player*/) { }
  527. // Called when a player is bound to an instance
  528. virtual void OnBindToInstance(Player* /*player*/, Difficulty /*difficulty*/, uint32 /*mapId*/, bool /*permanent*/) { }
  529. // Called when a player switches to a new zone
  530. virtual void OnUpdateZone(Player* /*player*/, uint32 /*newZone*/, uint32 /*newArea*/) { }
  531. // Called when a player changes to a new map (after moving to new map)
  532. virtual void OnMapChanged(Player* /*player*/) { }
  533. // Called after a player's quest status has been changed
  534. virtual void OnQuestStatusChange(Player* /*player*/, uint32 /*questId*/, QuestStatus /*status*/) { }
  535. };
  536. class AccountScript : public ScriptObject
  537. {
  538. protected:
  539. AccountScript(const char* name);
  540. public:
  541. // Called when an account logged in succesfully
  542. virtual void OnAccountLogin(uint32 /*accountId*/) {}
  543. // Called when an account login failed
  544. virtual void OnFailedAccountLogin(uint32 /*accountId*/) {}
  545. // Called when Email is successfully changed for Account
  546. virtual void OnEmailChange(uint32 /*accountId*/) {}
  547. // Called when Email failed to change for Account
  548. virtual void OnFailedEmailChange(uint32 /*accountId*/) {}
  549. // Called when Password is successfully changed for Account
  550. virtual void OnPasswordChange(uint32 /*accountId*/) {}
  551. // Called when Password failed to change for Account
  552. virtual void OnFailedPasswordChange(uint32 /*accountId*/) {}
  553. };
  554. class GuildScript : public ScriptObject
  555. {
  556. protected:
  557. GuildScript(const char* name);
  558. public:
  559. bool IsDatabaseBound() const final override { return false; }
  560. // Called when a member is added to the guild.
  561. virtual void OnAddMember(Guild* /*guild*/, Player* /*player*/, uint8& /*plRank*/) { }
  562. // Called when a member is removed from the guild.
  563. virtual void OnRemoveMember(Guild* /*guild*/, Player* /*player*/, bool /*isDisbanding*/, bool /*isKicked*/) { }
  564. // Called when the guild MOTD (message of the day) changes.
  565. virtual void OnMOTDChanged(Guild* /*guild*/, const std::string& /*newMotd*/) { }
  566. // Called when the guild info is altered.
  567. virtual void OnInfoChanged(Guild* /*guild*/, const std::string& /*newInfo*/) { }
  568. // Called when a guild is created.
  569. virtual void OnCreate(Guild* /*guild*/, Player* /*leader*/, const std::string& /*name*/) { }
  570. // Called when a guild is disbanded.
  571. virtual void OnDisband(Guild* /*guild*/) { }
  572. // Called when a guild member withdraws money from a guild bank.
  573. virtual void OnMemberWitdrawMoney(Guild* /*guild*/, Player* /*player*/, uint64& /*amount*/, bool /*isRepair*/) { }
  574. // Called when a guild member deposits money in a guild bank.
  575. virtual void OnMemberDepositMoney(Guild* /*guild*/, Player* /*player*/, uint64& /*amount*/) { }
  576. // Called when a guild member moves an item in a guild bank.
  577. virtual void OnItemMove(Guild* /*guild*/, Player* /*player*/, Item* /*pItem*/, bool /*isSrcBank*/, uint8 /*srcContainer*/, uint8 /*srcSlotId*/,
  578. bool /*isDestBank*/, uint8 /*destContainer*/, uint8 /*destSlotId*/) { }
  579. virtual void OnEvent(Guild* /*guild*/, uint8 /*eventType*/, uint32 /*playerGuid1*/, uint32 /*playerGuid2*/, uint8 /*newRank*/) { }
  580. virtual void OnBankEvent(Guild* /*guild*/, uint8 /*eventType*/, uint8 /*tabId*/, uint32 /*playerGuid*/, uint32 /*itemOrMoney*/, uint16 /*itemStackCount*/, uint8 /*destTabId*/) { }
  581. };
  582. class GroupScript : public ScriptObject
  583. {
  584. protected:
  585. GroupScript(const char* name);
  586. public:
  587. bool IsDatabaseBound() const final override { return false; }
  588. // Called when a member is added to a group.
  589. virtual void OnAddMember(Group* /*group*/, ObjectGuid /*guid*/) { }
  590. // Called when a member is invited to join a group.
  591. virtual void OnInviteMember(Group* /*group*/, ObjectGuid /*guid*/) { }
  592. // Called when a member is removed from a group.
  593. virtual void OnRemoveMember(Group* /*group*/, ObjectGuid /*guid*/, RemoveMethod /*method*/, ObjectGuid /*kicker*/, const char* /*reason*/) { }
  594. // Called when the leader of a group is changed.
  595. virtual void OnChangeLeader(Group* /*group*/, ObjectGuid /*newLeaderGuid*/, ObjectGuid /*oldLeaderGuid*/) { }
  596. // Called when a group is disbanded.
  597. virtual void OnDisband(Group* /*group*/) { }
  598. };
  599. // Placed here due to ScriptRegistry::AddScript dependency.
  600. #define sScriptMgr ScriptMgr::instance()
  601. // Manages registration, loading, and execution of scripts.
  602. class ScriptMgr
  603. {
  604. friend class ScriptObject;
  605. private:
  606. ScriptMgr();
  607. virtual ~ScriptMgr();
  608. public: /* Initialization */
  609. static ScriptMgr* instance()
  610. {
  611. static ScriptMgr instance;
  612. return &instance;
  613. }
  614. void Initialize();
  615. void LoadDatabase();
  616. void FillSpellSummary();
  617. const char* ScriptsVersion() const { return "Integrated Trinity Scripts"; }
  618. void IncrementScriptCount() { ++_scriptCount; }
  619. uint32 GetScriptCount() const { return _scriptCount; }
  620. public: /* Unloading */
  621. void Unload();
  622. public: /* SpellScriptLoader */
  623. void CreateSpellScripts(uint32 spellId, std::list<SpellScript*>& scriptVector);
  624. void CreateAuraScripts(uint32 spellId, std::list<AuraScript*>& scriptVector);
  625. void CreateSpellScriptLoaders(uint32 spellId, std::vector<std::pair<SpellScriptLoader*, std::multimap<uint32, uint32>::iterator> >& scriptVector);
  626. public: /* ServerScript */
  627. void OnNetworkStart();
  628. void OnNetworkStop();
  629. void OnSocketOpen(std::shared_ptr<WorldSocket> socket);
  630. void OnSocketClose(std::shared_ptr<WorldSocket> socket);
  631. void OnPacketReceive(WorldSession* session, WorldPacket const& packet);
  632. void OnPacketSend(WorldSession* session, WorldPacket const& packet);
  633. void OnUnknownPacketReceive(WorldSession* session, WorldPacket const& packet);
  634. public: /* WorldScript */
  635. void OnOpenStateChange(bool open);
  636. void OnConfigLoad(bool reload);
  637. void OnMotdChange(std::string& newMotd);
  638. void OnShutdownInitiate(ShutdownExitCode code, ShutdownMask mask);
  639. void OnShutdownCancel();
  640. void OnWorldUpdate(uint32 diff);
  641. void OnStartup();
  642. void OnShutdown();
  643. public: /* FormulaScript */
  644. void OnHonorCalculation(float& honor, uint8 level, float multiplier);
  645. void OnGrayLevelCalculation(uint8& grayLevel, uint8 playerLevel);
  646. void OnColorCodeCalculation(XPColorChar& color, uint8 playerLevel, uint8 mobLevel);
  647. void OnZeroDifferenceCalculation(uint8& diff, uint8 playerLevel);
  648. void OnBaseGainCalculation(uint32& gain, uint8 playerLevel, uint8 mobLevel, ContentLevels content);
  649. void OnGainCalculation(uint32& gain, Player* player, Unit* unit);
  650. void OnGroupRateCalculation(float& rate, uint32 count, bool isRaid);
  651. public: /* MapScript */
  652. void OnCreateMap(Map* map);
  653. void OnDestroyMap(Map* map);
  654. void OnLoadGridMap(Map* map, GridMap* gmap, uint32 gx, uint32 gy);
  655. void OnUnloadGridMap(Map* map, GridMap* gmap, uint32 gx, uint32 gy);
  656. void OnPlayerEnterMap(Map* map, Player* player);
  657. void OnPlayerLeaveMap(Map* map, Player* player);
  658. void OnMapUpdate(Map* map, uint32 diff);
  659. public: /* InstanceMapScript */
  660. InstanceScript* CreateInstanceData(InstanceMap* map);
  661. public: /* ItemScript */
  662. bool OnDummyEffect(Unit* caster, uint32 spellId, SpellEffIndex effIndex, Item* target);
  663. bool OnQuestAccept(Player* player, Item* item, Quest const* quest);
  664. bool OnItemUse(Player* player, Item* item, SpellCastTargets const& targets);
  665. bool OnItemExpire(Player* player, ItemTemplate const* proto);
  666. bool OnItemRemove(Player* player, Item* item);
  667. public: /* CreatureScript */
  668. bool OnDummyEffect(Unit* caster, uint32 spellId, SpellEffIndex effIndex, Creature* target);
  669. bool OnGossipHello(Player* player, Creature* creature);
  670. bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action);
  671. bool OnGossipSelectCode(Player* player, Creature* creature, uint32 sender, uint32 action, const char* code);
  672. bool OnQuestAccept(Player* player, Creature* creature, Quest const* quest);
  673. bool OnQuestSelect(Player* player, Creature* creature, Quest const* quest);
  674. bool OnQuestReward(Player* player, Creature* creature, Quest const* quest, uint32 opt);
  675. uint32 GetDialogStatus(Player* player, Creature* creature);
  676. CreatureAI* GetCreatureAI(Creature* creature);
  677. void OnCreatureUpdate(Creature* creature, uint32 diff);
  678. public: /* GameObjectScript */
  679. bool OnDummyEffect(Unit* caster, uint32 spellId, SpellEffIndex effIndex, GameObject* target);
  680. bool OnGossipHello(Player* player, GameObject* go);
  681. bool OnGossipSelect(Player* player, GameObject* go, uint32 sender, uint32 action);
  682. bool OnGossipSelectCode(Player* player, GameObject* go, uint32 sender, uint32 action, const char* code);
  683. bool OnQuestAccept(Player* player, GameObject* go, Quest const* quest);
  684. bool OnQuestReward(Player* player, GameObject* go, Quest const* quest, uint32 opt);
  685. uint32 GetDialogStatus(Player* player, GameObject* go);
  686. void OnGameObjectDestroyed(GameObject* go, Player* player);
  687. void OnGameObjectDamaged(GameObject* go, Player* player);
  688. void OnGameObjectLootStateChanged(GameObject* go, uint32 state, Unit* unit);
  689. void OnGameObjectStateChanged(GameObject* go, uint32 state);
  690. void OnGameObjectUpdate(GameObject* go, uint32 diff);
  691. GameObjectAI* GetGameObjectAI(GameObject* go);
  692. public: /* AreaTriggerScript */
  693. bool OnAreaTrigger(Player* player, AreaTriggerEntry const* trigger);
  694. public: /* BattlegroundScript */
  695. Battleground* CreateBattleground(BattlegroundTypeId typeId);
  696. public: /* OutdoorPvPScript */
  697. OutdoorPvP* CreateOutdoorPvP(OutdoorPvPData const* data);
  698. public: /* CommandScript */
  699. std::vector<ChatCommand*> GetChatCommands();
  700. public: /* WeatherScript */
  701. void OnWeatherChange(Weather* weather, WeatherState state, float grade);
  702. void OnWeatherUpdate(Weather* weather, uint32 diff);
  703. public: /* AuctionHouseScript */
  704. void OnAuctionAdd(AuctionHouseObject* ah, AuctionEntry* entry);
  705. void OnAuctionRemove(AuctionHouseObject* ah, AuctionEntry* entry);
  706. void OnAuctionSuccessful(AuctionHouseObject* ah, AuctionEntry* entry);
  707. void OnAuctionExpire(AuctionHouseObject* ah, AuctionEntry* entry);
  708. public: /* ConditionScript */
  709. bool OnConditionCheck(Condition* condition, ConditionSourceInfo& sourceInfo);
  710. public: /* VehicleScript */
  711. void OnInstall(Vehicle* veh);
  712. void OnUninstall(Vehicle* veh);
  713. void OnReset(Vehicle* veh);
  714. void OnInstallAccessory(Vehicle* veh, Creature* accessory);
  715. void OnAddPassenger(Vehicle* veh, Unit* passenger, int8 seatId);
  716. void OnRemovePassenger(Vehicle* veh, Unit* passenger);
  717. public: /* DynamicObjectScript */
  718. void OnDynamicObjectUpdate(DynamicObject* dynobj, uint32 diff);
  719. public: /* TransportScript */
  720. void OnAddPassenger(Transport* transport, Player* player);
  721. void OnAddCreaturePassenger(Transport* transport, Creature* creature);
  722. void OnRemovePassenger(Transport* transport, Player* player);
  723. void OnTransportUpdate(Transport* transport, uint32 diff);
  724. void OnRelocate(Transport* transport, uint32 waypointId, uint32 mapId, float x, float y, float z);
  725. public: /* AchievementCriteriaScript */
  726. bool OnCriteriaCheck(uint32 scriptId, Player* source, Unit* target);
  727. public: /* PlayerScript */
  728. void OnPVPKill(Player* killer, Player* killed);
  729. void OnCreatureKill(Player* killer, Creature* killed);
  730. void OnPlayerKilledByCreature(Creature* killer, Player* killed);
  731. void OnPlayerLevelChanged(Player* player, uint8 oldLevel);
  732. void OnPlayerFreeTalentPointsChanged(Player* player, uint32 newPoints);
  733. void OnPlayerTalentsReset(Player* player, bool noCost);
  734. void OnPlayerMoneyChanged(Player* player, int64& amount);
  735. void OnPlayerMoneyLimit(Player* player, int64 amount);
  736. void OnGivePlayerXP(Player* player, uint32& amount, Unit* victim);
  737. void OnPlayerReputationChange(Player* player, uint32 factionID, int32& standing, bool incremental);
  738. void OnPlayerDuelRequest(Player* target, Player* challenger);
  739. void OnPlayerDuelStart(Player* player1, Player* player2);
  740. void OnPlayerDuelEnd(Player* winner, Player* loser, DuelCompleteType type);
  741. void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg);
  742. void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg, Player* receiver);
  743. void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg, Group* group);
  744. void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg, Guild* guild);
  745. void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg, Channel* channel);
  746. void OnPlayerEmote(Player* player, uint32 emote);
  747. void OnPlayerTextEmote(Player* player, uint32 textEmote, uint32 emoteNum, ObjectGuid guid);
  748. void OnPlayerSpellCast(Player* player, Spell* spell, bool skipCheck);
  749. void OnPlayerLogin(Player* player, bool firstLogin);
  750. void OnPlayerLogout(Player* player);
  751. void OnPlayerCreate(Player* player);
  752. void OnPlayerDelete(ObjectGuid guid, uint32 accountId);
  753. void OnPlayerFailedDelete(ObjectGuid guid, uint32 accountId);
  754. void OnPlayerSave(Player* player);
  755. void OnPlayerBindToInstance(Player* player, Difficulty difficulty, uint32 mapid, bool permanent);
  756. void OnPlayerUpdateZone(Player* player, uint32 newZone, uint32 newArea);
  757. void OnQuestStatusChange(Player* player, uint32 questId, QuestStatus status);
  758. public: /* AccountScript */
  759. void OnAccountLogin(uint32 accountId);
  760. void OnFailedAccountLogin(uint32 accountId);
  761. void OnEmailChange(uint32 accountId);
  762. void OnFailedEmailChange(uint32 accountId);
  763. void OnPasswordChange(uint32 accountId);
  764. void OnFailedPasswordChange(uint32 accountId);
  765. public: /* GuildScript */
  766. void OnGuildAddMember(Guild* guild, Player* player, uint8& plRank);
  767. void OnGuildRemoveMember(Guild* guild, Player* player, bool isDisbanding, bool isKicked);
  768. void OnGuildMOTDChanged(Guild* guild, const std::string& newMotd);
  769. void OnGuildInfoChanged(Guild* guild, const std::string& newInfo);
  770. void OnGuildCreate(Guild* guild, Player* leader, const std::string& name);
  771. void OnGuildDisband(Guild* guild);
  772. void OnGuildMemberWitdrawMoney(Guild* guild, Player* player, uint64 &amount, bool isRepair);
  773. void OnGuildMemberDepositMoney(Guild* guild, Player* player, uint64 &amount);
  774. void OnGuildItemMove(Guild* guild, Player* player, Item* pItem, bool isSrcBank, uint8 srcContainer, uint8 srcSlotId,
  775. bool isDestBank, uint8 destContainer, uint8 destSlotId);
  776. void OnGuildEvent(Guild* guild, uint8 eventType, uint32 playerGuid1, uint32 playerGuid2, uint8 newRank);
  777. void OnGuildBankEvent(Guild* guild, uint8 eventType, uint8 tabId, uint32 playerGuid, uint32 itemOrMoney, uint16 itemStackCount, uint8 destTabId);
  778. public: /* GroupScript */
  779. void OnGroupAddMember(Group* group, ObjectGuid guid);
  780. void OnGroupInviteMember(Group* group, ObjectGuid guid);
  781. void OnGroupRemoveMember(Group* group, ObjectGuid guid, RemoveMethod method, ObjectGuid kicker, const char* reason);
  782. void OnGroupChangeLeader(Group* group, ObjectGuid newLeaderGuid, ObjectGuid oldLeaderGuid);
  783. void OnGroupDisband(Group* group);
  784. public: /* UnitScript */
  785. void OnHeal(Unit* healer, Unit* reciever, uint32& gain);
  786. void OnDamage(Unit* attacker, Unit* victim, uint32& damage);
  787. void ModifyPeriodicDamageAurasTick(Unit* target, Unit* attacker, uint32& damage);
  788. void ModifyMeleeDamage(Unit* target, Unit* attacker, uint32& damage);
  789. void ModifySpellDamageTaken(Unit* target, Unit* attacker, int32& damage);
  790. public: /* Scheduled scripts */
  791. uint32 IncreaseScheduledScriptsCount() { return ++_scheduledScripts; }
  792. uint32 DecreaseScheduledScriptCount() { return --_scheduledScripts; }
  793. uint32 DecreaseScheduledScriptCount(size_t count) { return _scheduledScripts -= count; }
  794. bool IsScriptScheduled() const { return _scheduledScripts > 0; }
  795. private:
  796. uint32 _scriptCount;
  797. //atomic op counter for active scripts amount
  798. std::atomic_long _scheduledScripts;
  799. };
  800. #endif