/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
#ifndef SC_SCRIPTMGR_H
#define SC_SCRIPTMGR_H
#include "Common.h"
#include "ObjectGuid.h"
#include "Tuples.h"
#include "Types.h"
#include
class AccountMgr;
class AreaTrigger;
class AreaTriggerAI;
class AuctionHouseObject;
class Aura;
class AuraScript;
class Battlefield;
class Battleground;
class BattlegroundMap;
class Channel;
class ChatCommand;
class Conversation;
class Creature;
class CreatureAI;
class DynamicObject;
class GameObject;
class GameObjectAI;
class Guild;
class GridMap;
class Group;
class InstanceMap;
class InstanceScript;
class Item;
class Map;
class ModuleReference;
class OutdoorPvP;
class Player;
class Quest;
class ScriptMgr;
class Spell;
class SpellInfo;
class SpellScript;
class SpellCastTargets;
class Transport;
class Unit;
class Vehicle;
class Weather;
class WorldPacket;
class WorldSocket;
class WorldObject;
class WorldSession;
struct AreaTriggerEntry;
struct AuctionPosting;
struct ConditionSourceInfo;
struct Condition;
struct CreatureTemplate;
struct CreatureData;
struct ItemTemplate;
struct MapEntry;
struct Position;
struct QuestObjective;
struct SceneTemplate;
enum BattlegroundTypeId : uint32;
enum Difficulty : uint8;
enum DuelCompleteType : uint8;
enum Emote : uint32;
enum QuestStatus : uint8;
enum RemoveMethod : uint8;
enum ShutdownExitCode : uint32;
enum ShutdownMask : uint32;
enum SpellEffIndex : uint8;
enum WeatherState : uint32;
enum XPColorChar : uint8;
#define VISIBLE_RANGE 166.0f //MAX visible range (size of grid)
/*
@todo Add more script type classes.
MailScript
SessionScript
CollisionScript
ArenaTeamScript
*/
/*
Standard procedure when adding new script type classes:
First of all, define the actual class, and have it inherit from ScriptObject, like so:
class MyScriptType : public ScriptObject
{
uint32 _someId;
private:
void RegisterSelf();
protected:
MyScriptType(char const* name, uint32 someId)
: ScriptObject(name), _someId(someId)
{
ScriptRegistry::AddScript(this);
}
public:
// If a virtual function in your script type class is not necessarily
// required to be overridden, just declare it virtual with an empty
// body. If, on the other hand, it's logical only to override it (i.e.
// if it's the only method in the class), make it pure virtual, by adding
// = 0 to it.
virtual void OnSomeEvent(uint32 someArg1, std::string& someArg2) { }
// This is a pure virtual function:
virtual void OnAnotherEvent(uint32 someArg) = 0;
}
Next, you need to add a specialization for ScriptRegistry. Put this in the bottom of
ScriptMgr.cpp:
template class ScriptRegistry;
Now, add a cleanup routine in ScriptMgr::~ScriptMgr:
SCR_CLEAR(MyScriptType);
Now your script type is good to go with the script system. What you need to do now
is add functions to ScriptMgr that can be called from the core to actually trigger
certain events. For example, in ScriptMgr.h:
void OnSomeEvent(uint32 someArg1, std::string& someArg2);
void OnAnotherEvent(uint32 someArg);
In ScriptMgr.cpp:
void ScriptMgr::OnSomeEvent(uint32 someArg1, std::string& someArg2)
{
FOREACH_SCRIPT(MyScriptType)->OnSomeEvent(someArg1, someArg2);
}
void ScriptMgr::OnAnotherEvent(uint32 someArg)
{
FOREACH_SCRIPT(MyScriptType)->OnAnotherEvent(someArg1, someArg2);
}
Now you simply call these two functions from anywhere in the core to trigger the
event on all registered scripts of that type.
*/
class TC_GAME_API ScriptObject
{
friend class ScriptMgr;
public:
const std::string& GetName() const { return _name; }
protected:
ScriptObject(char const* name);
virtual ~ScriptObject();
private:
const std::string _name;
};
template class UpdatableScript
{
protected:
UpdatableScript()
{
}
virtual ~UpdatableScript() { }
public:
virtual void OnUpdate(TObject* /*obj*/, uint32 /*diff*/) { }
};
class TC_GAME_API SpellScriptLoader : public ScriptObject
{
protected:
SpellScriptLoader(char const* name);
public:
// Should return a fully valid SpellScript pointer.
virtual SpellScript* GetSpellScript() const { return nullptr; }
// Should return a fully valid AuraScript pointer.
virtual AuraScript* GetAuraScript() const { return nullptr; }
};
class TC_GAME_API ServerScript : public ScriptObject
{
protected:
ServerScript(char const* name);
public:
// Called when reactive socket I/O is started (WorldTcpSessionMgr).
virtual void OnNetworkStart() { }
// Called when reactive I/O is stopped.
virtual void OnNetworkStop() { }
// Called when a remote socket establishes a connection to the server. Do not store the socket object.
virtual void OnSocketOpen(std::shared_ptr /*socket*/) { }
// Called when a socket is closed. Do not store the socket object, and do not rely on the connection
// being open; it is not.
virtual void OnSocketClose(std::shared_ptr /*socket*/) { }
// Called when a packet is sent to a client. The packet object is a copy of the original packet, so reading
// and modifying it is safe.
virtual void OnPacketSend(WorldSession* /*session*/, WorldPacket& /*packet*/) { }
// Called when a (valid) packet is received by a client. The packet object is a copy of the original packet, so
// reading and modifying it is safe. Make sure to check WorldSession pointer before usage, it might be null in case of auth packets
virtual void OnPacketReceive(WorldSession* /*session*/, WorldPacket& /*packet*/) { }
};
class TC_GAME_API WorldScript : public ScriptObject
{
protected:
WorldScript(char const* name);
public:
// Called when the open/closed state of the world changes.
virtual void OnOpenStateChange(bool /*open*/) { }
// Called after the world configuration is (re)loaded.
virtual void OnConfigLoad(bool /*reload*/) { }
// Called before the message of the day is changed.
virtual void OnMotdChange(std::string& /*newMotd*/) { }
// Called when a world shutdown is initiated.
virtual void OnShutdownInitiate(ShutdownExitCode /*code*/, ShutdownMask /*mask*/) { }
// Called when a world shutdown is cancelled.
virtual void OnShutdownCancel() { }
// Called on every world tick (don't execute too heavy code here).
virtual void OnUpdate(uint32 /*diff*/) { }
// Called when the world is started.
virtual void OnStartup() { }
// Called when the world is actually shut down.
virtual void OnShutdown() { }
};
class TC_GAME_API FormulaScript : public ScriptObject
{
protected:
FormulaScript(char const* name);
public:
// Called after calculating honor.
virtual void OnHonorCalculation(float& /*honor*/, uint8 /*level*/, float /*multiplier*/) { }
// Called after gray level calculation.
virtual void OnGrayLevelCalculation(uint8& /*grayLevel*/, uint8 /*playerLevel*/) { }
// Called after calculating experience color.
virtual void OnColorCodeCalculation(XPColorChar& /*color*/, uint8 /*playerLevel*/, uint8 /*mobLevel*/) { }
// Called after calculating zero difference.
virtual void OnZeroDifferenceCalculation(uint8& /*diff*/, uint8 /*playerLevel*/) { }
// Called after calculating base experience gain.
virtual void OnBaseGainCalculation(uint32& /*gain*/, uint8 /*playerLevel*/, uint8 /*mobLevel*/) { }
// Called after calculating experience gain.
virtual void OnGainCalculation(uint32& /*gain*/, Player* /*player*/, Unit* /*unit*/) { }
// Called when calculating the experience rate for group experience.
virtual void OnGroupRateCalculation(float& /*rate*/, uint32 /*count*/, bool /*isRaid*/) { }
};
template
class MapScript : public UpdatableScript
{
MapEntry const* _mapEntry;
protected:
MapScript(MapEntry const* mapEntry) : _mapEntry(mapEntry) { }
public:
// Gets the MapEntry structure associated with this script. Can return NULL.
MapEntry const* GetEntry() { return _mapEntry; }
// Called when the map is created.
virtual void OnCreate(TMap* /*map*/) { }
// Called just before the map is destroyed.
virtual void OnDestroy(TMap* /*map*/) { }
// Called when a grid map is loaded.
virtual void OnLoadGridMap(TMap* /*map*/, GridMap* /*gmap*/, uint32 /*gx*/, uint32 /*gy*/) { }
// Called when a grid map is unloaded.
virtual void OnUnloadGridMap(TMap* /*map*/, GridMap* /*gmap*/, uint32 /*gx*/, uint32 /*gy*/) { }
// Called when a player enters the map.
virtual void OnPlayerEnter(TMap* /*map*/, Player* /*player*/) { }
// Called when a player leaves the map.
virtual void OnPlayerLeave(TMap* /*map*/, Player* /*player*/) { }
};
class TC_GAME_API WorldMapScript : public ScriptObject, public MapScript