Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify script naming and implement AutoScript class #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions ScriptMgr.cpp
Expand Up @@ -302,20 +302,40 @@ void DoOrSimulateScriptTextForMap(int32 iTextEntry, uint32 uiCreatureEntry, Map*

void Script::RegisterSelf(bool bReportError)
{
if (uint32 id = GetScriptId(Name.c_str()))
if (uint32 id = GetScriptId(Name))
{
m_scripts[id] = this;
++num_sc_scripts;
}
else
{
if (bReportError)
script_error_log("Script registering but ScriptName %s is not assigned in database. Script will not be used.", Name.c_str());
script_error_log("Script registering but ScriptName %s is not assigned in database. Script will not be used.", Name);

delete this;
}
}

//*********************************
//******** AutoScript *************

Script* AutoScript::newScript(const char* scriptName, bool reportError/*=true*/)
{
Register(); // register last added script (if any)

m_script = new Script(scriptName);
m_reportError = reportError;
return m_script;
}

void AutoScript::Register()
{
if (!m_script)
return;
m_script->RegisterSelf(m_reportError);
m_script = NULL;
}

//********************************
//*** Functions to be Exported ***

Expand Down
81 changes: 53 additions & 28 deletions ScriptMgr.h
Expand Up @@ -61,7 +61,7 @@ enum EscortFaction

struct Script
{
Script() :
Script(const char* scriptName = NULL) : Name(scriptName),
pGossipHello(NULL), pGossipHelloGO(NULL), pGossipSelect(NULL), pGossipSelectGO(NULL),
pGossipSelectWithCode(NULL), pGossipSelectGOWithCode(NULL),
pDialogStatusNPC(NULL), pDialogStatusGO(NULL),
Expand All @@ -72,37 +72,62 @@ struct Script
pEffectAuraDummy(NULL), GetAI(NULL), GetInstanceData(NULL)
{}

std::string Name;

bool (*pGossipHello)(Player*, Creature*);
bool (*pGossipHelloGO)(Player*, GameObject*);
bool (*pGossipSelect)(Player*, Creature*, uint32, uint32);
bool (*pGossipSelectGO)(Player*, GameObject*, uint32, uint32);
bool (*pGossipSelectWithCode)(Player*, Creature*, uint32, uint32, const char*);
bool (*pGossipSelectGOWithCode)(Player*, GameObject*, uint32, uint32, const char*);
uint32(*pDialogStatusNPC)(Player*, Creature*);
uint32(*pDialogStatusGO)(Player*, GameObject*);
bool (*pQuestAcceptNPC)(Player*, Creature*, Quest const*);
bool (*pQuestAcceptGO)(Player*, GameObject*, Quest const*);
bool (*pQuestAcceptItem)(Player*, Item*, Quest const*);
bool (*pQuestRewardedNPC)(Player*, Creature*, Quest const*);
bool (*pQuestRewardedGO)(Player*, GameObject*, Quest const*);
bool (*pGOUse)(Player*, GameObject*);
bool (*pItemUse)(Player*, Item*, SpellCastTargets const&);
bool (*pAreaTrigger)(Player*, AreaTriggerEntry const*);
bool (*pProcessEventId)(uint32, Object*, Object*, bool);
bool (*pEffectDummyNPC)(Unit*, uint32, SpellEffectIndex, Creature*);
bool (*pEffectDummyGO)(Unit*, uint32, SpellEffectIndex, GameObject*);
bool (*pEffectDummyItem)(Unit*, uint32, SpellEffectIndex, Item*);
bool (*pEffectScriptEffectNPC)(Unit*, uint32, SpellEffectIndex, Creature*);
bool (*pEffectAuraDummy)(const Aura*, bool);

CreatureAI* (*GetAI)(Creature*);
InstanceData* (*GetInstanceData)(Map*);
const char* Name;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we going from std::string to const char*?


bool (*pGossipHello )(Player*, Creature*);
bool (*pGossipHelloGO )(Player*, GameObject*);
bool (*pGossipSelect )(Player*, Creature*, uint32, uint32);
bool (*pGossipSelectGO )(Player*, GameObject*, uint32, uint32);
bool (*pGossipSelectWithCode )(Player*, Creature*, uint32, uint32, const char*);
bool (*pGossipSelectGOWithCode )(Player*, GameObject*, uint32, uint32, const char*);
uint32 (*pDialogStatusNPC )(Player*, Creature*);
uint32 (*pDialogStatusGO )(Player*, GameObject*);
bool (*pQuestAcceptNPC )(Player*, Creature*, Quest const*);
bool (*pQuestAcceptGO )(Player*, GameObject*, Quest const*);
bool (*pQuestAcceptItem )(Player*, Item*, Quest const*);
bool (*pQuestRewardedNPC )(Player*, Creature*, Quest const*);
bool (*pQuestRewardedGO )(Player*, GameObject*, Quest const*);
bool (*pGOUse )(Player*, GameObject*);
bool (*pItemUse )(Player*, Item*, SpellCastTargets const&);
bool (*pAreaTrigger )(Player*, AreaTriggerEntry const*);
bool (*pProcessEventId )(uint32, Object*, Object*, bool);
bool (*pEffectDummyNPC )(Unit*, uint32, SpellEffectIndex, Creature*);
bool (*pEffectDummyGO )(Unit*, uint32, SpellEffectIndex, GameObject*);
bool (*pEffectDummyItem )(Unit*, uint32, SpellEffectIndex, Item*);
bool (*pEffectScriptEffectNPC )(Unit*, uint32, SpellEffectIndex, Creature*);
bool (*pEffectAuraDummy )(const Aura*, bool);

CreatureAI* (*GetAI )(Creature*);
InstanceData* (*GetInstanceData )(Map*);

void RegisterSelf(bool bReportError = true);
};

// *********************************************************
// ******************* AutoScript **************************

class AutoScript
{
private:
Script* m_script;
bool m_reportError;

void Register();

public:
AutoScript() : m_script(NULL), m_reportError(true) {}
AutoScript(const char* scriptName, bool reportError = true) : m_script(NULL) { newScript(scriptName, reportError); }
~AutoScript() { Register(); }

Script* newScript(const char* scriptName, bool reportError = true);

Script* operator -> ()
{
MANGOS_ASSERT(m_script != NULL && "AutoScript: use newScript() before!");
return m_script;
}
};

// *********************************************************
// ************* Some functions used globally **************

Expand Down
10 changes: 3 additions & 7 deletions scripts/battlegrounds/battleground.cpp
Expand Up @@ -104,11 +104,7 @@ CreatureAI* GetAI_npc_spirit_guide(Creature* pCreature)

void AddSC_battleground()
{
Script* pNewScript;

pNewScript = new Script;
pNewScript->Name = "npc_spirit_guide";
pNewScript->GetAI = &GetAI_npc_spirit_guide;
pNewScript->pGossipHello = &GossipHello_npc_spirit_guide;
pNewScript->RegisterSelf();
AutoScript s("npc_spirit_guide");
s->GetAI = &GetAI_npc_spirit_guide;
s->pGossipHello = &GossipHello_npc_spirit_guide;
}
12 changes: 4 additions & 8 deletions scripts/examples/example_creature.cpp
Expand Up @@ -280,12 +280,8 @@ bool GossipSelect_example_creature(Player* pPlayer, Creature* pCreature, uint32
// It must define all handled functions that are to be run in this script
void AddSC_example_creature()
{
Script* pNewScript;

pNewScript = new Script;
pNewScript->Name = "example_creature";
pNewScript->GetAI = &GetAI_example_creature;
pNewScript->pGossipHello = &GossipHello_example_creature;
pNewScript->pGossipSelect = &GossipSelect_example_creature;
pNewScript->RegisterSelf(false);
AutoScript s("example_creature", false);
s->GetAI = &GetAI_example_creature;
s->pGossipHello = &GossipHello_example_creature;
s->pGossipSelect = &GossipSelect_example_creature;
}
12 changes: 4 additions & 8 deletions scripts/examples/example_escort.cpp
Expand Up @@ -224,12 +224,8 @@ bool GossipSelect_example_escort(Player* pPlayer, Creature* pCreature, uint32 /*

void AddSC_example_escort()
{
Script* pNewScript;

pNewScript = new Script;
pNewScript->Name = "example_escort";
pNewScript->GetAI = &GetAI_example_escort;
pNewScript->pGossipHello = &GossipHello_example_escort;
pNewScript->pGossipSelect = &GossipSelect_example_escort;
pNewScript->RegisterSelf(false);
AutoScript s("example_escort", false);
s->GetAI = &GetAI_example_escort;
s->pGossipHello = &GossipHello_example_escort;
s->pGossipSelect = &GossipSelect_example_escort;
}
12 changes: 4 additions & 8 deletions scripts/examples/example_gossip_codebox.cpp
Expand Up @@ -89,12 +89,8 @@ bool GossipSelectWithCode_example_gossip_codebox(Player* pPlayer, Creature* pCre

void AddSC_example_gossip_codebox()
{
Script* pNewScript;

pNewScript = new Script;
pNewScript->Name = "example_gossip_codebox";
pNewScript->pGossipHello = &GossipHello_example_gossip_codebox;
pNewScript->pGossipSelect = &GossipSelect_example_gossip_codebox;
pNewScript->pGossipSelectWithCode = &GossipSelectWithCode_example_gossip_codebox;
pNewScript->RegisterSelf(false);
AutoScript s("example_gossip_codebox", false);
s->pGossipHello = &GossipHello_example_gossip_codebox;
s->pGossipSelect = &GossipSelect_example_gossip_codebox;
s->pGossipSelectWithCode = &GossipSelectWithCode_example_gossip_codebox;
}
20 changes: 7 additions & 13 deletions scripts/examples/example_misc.cpp
Expand Up @@ -49,20 +49,14 @@ bool GOUse_example_go_teleporter(Player* pPlayer, GameObject* /*pGo*/)

void AddSC_example_misc()
{
Script* pNewScript;
AutoScript s;

pNewScript = new Script;
pNewScript->Name = "at_example";
pNewScript->pAreaTrigger = &AreaTrigger_at_example;
pNewScript->RegisterSelf(false);
s.newScript("at_example", false);
s->pAreaTrigger = &AreaTrigger_at_example;

pNewScript = new Script;
pNewScript->Name = "example_item";
pNewScript->pItemUse = &ItemUse_example_item;
pNewScript->RegisterSelf(false);
s.newScript("example_item", false);
s->pItemUse = &ItemUse_example_item;

pNewScript = new Script;
pNewScript->Name = "example_go_teleporter";
pNewScript->pGOUse = &GOUse_example_go_teleporter;
pNewScript->RegisterSelf(false);
s.newScript("example_go_teleporter", false);
s->pGOUse = &GOUse_example_go_teleporter;
}
90 changes: 34 additions & 56 deletions scripts/world/areatrigger_scripts.cpp
Expand Up @@ -325,60 +325,38 @@ bool AreaTrigger_at_murkdeep(Player* pPlayer, AreaTriggerEntry const* pAt)

void AddSC_areatrigger_scripts()
{
Script* pNewScript;

pNewScript = new Script;
pNewScript->Name = "at_childrens_week_spot";
pNewScript->pAreaTrigger = &AreaTrigger_at_childrens_week_spot;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_aldurthar_gate";
pNewScript->pAreaTrigger = &AreaTrigger_at_aldurthar_gate;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_coilfang_waterfall";
pNewScript->pAreaTrigger = &AreaTrigger_at_coilfang_waterfall;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_legion_teleporter";
pNewScript->pAreaTrigger = &AreaTrigger_at_legion_teleporter;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_ravenholdt";
pNewScript->pAreaTrigger = &AreaTrigger_at_ravenholdt;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_spearborn_encampment";
pNewScript->pAreaTrigger = &AreaTrigger_at_spearborn_encampment;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_warsong_farms";
pNewScript->pAreaTrigger = &AreaTrigger_at_warsong_farms;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_waygate";
pNewScript->pAreaTrigger = &AreaTrigger_at_waygate;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_stormwright_shelf";
pNewScript->pAreaTrigger = &AreaTrigger_at_stormwright_shelf;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_scent_larkorwi";
pNewScript->pAreaTrigger = &AreaTrigger_at_scent_larkorwi;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "at_murkdeep";
pNewScript->pAreaTrigger = &AreaTrigger_at_murkdeep;
pNewScript->RegisterSelf();
AutoScript s;

s.newScript("at_childrens_week_spot");
s->pAreaTrigger = &AreaTrigger_at_childrens_week_spot;

s.newScript("at_aldurthar_gate");
s->pAreaTrigger = &AreaTrigger_at_aldurthar_gate;

s.newScript("at_coilfang_waterfall");
s->pAreaTrigger = &AreaTrigger_at_coilfang_waterfall;

s.newScript("at_legion_teleporter");
s->pAreaTrigger = &AreaTrigger_at_legion_teleporter;

s.newScript("at_ravenholdt");
s->pAreaTrigger = &AreaTrigger_at_ravenholdt;

s.newScript("at_spearborn_encampment");
s->pAreaTrigger = &AreaTrigger_at_spearborn_encampment;

s.newScript("at_warsong_farms");
s->pAreaTrigger = &AreaTrigger_at_warsong_farms;

s.newScript("at_waygate");
s->pAreaTrigger = &AreaTrigger_at_waygate;

s.newScript("at_stormwright_shelf");
s->pAreaTrigger = &AreaTrigger_at_stormwright_shelf;

s.newScript("at_scent_larkorwi");
s->pAreaTrigger = &AreaTrigger_at_scent_larkorwi;

s.newScript("at_murkdeep");
s->pAreaTrigger = &AreaTrigger_at_murkdeep;
}
58 changes: 22 additions & 36 deletions scripts/world/bosses_emerald_dragons.cpp
Expand Up @@ -608,40 +608,26 @@ CreatureAI* GetAI_mob_dementeddruids(Creature* pCreature)

void AddSC_bosses_emerald_dragons()
{
Script* pNewScript;

pNewScript = new Script;
pNewScript->Name = "boss_emeriss";
pNewScript->GetAI = &GetAI_boss_emeriss;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "boss_lethon";
pNewScript->GetAI = &GetAI_boss_lethon;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "npc_spirit_shade";
pNewScript->GetAI = &GetAI_npc_spirit_shade;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "boss_taerar";
pNewScript->GetAI = &GetAI_boss_taerar;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "boss_shade_of_taerar";
pNewScript->GetAI = &GetAI_boss_shadeoftaerar;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "boss_ysondre";
pNewScript->GetAI = &GetAI_boss_ysondre;
pNewScript->RegisterSelf();

pNewScript = new Script;
pNewScript->Name = "mob_dementeddruids";
pNewScript->GetAI = &GetAI_mob_dementeddruids;
pNewScript->RegisterSelf();
AutoScript s;

s.newScript("boss_emeriss");
s->GetAI = &GetAI_boss_emeriss;

s.newScript("boss_lethon");
s->GetAI = &GetAI_boss_lethon;

s.newScript("npc_spirit_shade");
s->GetAI = &GetAI_npc_spirit_shade;

s.newScript("boss_taerar");
s->GetAI = &GetAI_boss_taerar;

s.newScript("boss_shade_of_taerar");
s->GetAI = &GetAI_boss_shadeoftaerar;

s.newScript("boss_ysondre");
s->GetAI = &GetAI_boss_ysondre;

s.newScript("mob_dementeddruids");
s->GetAI = &GetAI_mob_dementeddruids;
}