AzerothCore
Pages :
Contents:
  1. Core Scripts
    1. Creature Scripts
      1. World Spawned Creatures
      2. Instance Spawned Creatures
      3. Different types of Creature AI's
    2. GameObject Scripts
      1. World Spawned GameObjects
      2. Instance Spawned GameObject
    3. Spell Scripts
      1. Validation of spells used in a script
      2. Stand alone SpellScripts and AuraScripts
      3. Spell- and Aurascript Pairs
      4. SpellScripts with Args
    4. Assigning Script in the Database
      1. CreatureScripts
      2. SpellScripts

Core Scripts

When dealing with Creature-, GameObject- and Spell scripts we should always use the new registry macros introduced in this commit.

Creature Scripts

World Spawned Creatures

struct npc_scripted_creature : public CreatureAI
{
public:
    npc_scripted_creature(Creature* creature) : CreatureAI(creature) { }

    void EnterCombat(Unit* /*who*/) override
    {
        /* Some code */
    }
}

/* At the end of a script file you will find a place where we register all
 * the scripts. */
void AddSC_scripts()
{
    /* RegisterCreatureAI(creatureScript); */
    RegisterCreatureAI(npc_scripted_creature);
}

Instance Spawned Creatures

If a creature is scripted inside an instance we register them as factory.

To do this, in the headerfile of the instance we add this define:

#define Register"IntanceName"CreatureAI(ai_name) RegisterCreatureAIWithFactory(ai_name, Get"InstanceName"AI)

/* Example from icecrown_citadel.h */
#define RegisterIcecrownCitadelCreatureAI(ai_name) RegisterCreatureAIWithFactory(ai_name, GetIcecrownCitadelAI)

And within the script file it would be registered like this:

/* Example from boss_lord_marrowgar.cpp */
struct boss_lord_marrowgar : public BossAI
{
public:
    boss_lord_marrowgar(Creature* creature) : BossAI(creature, DATA_LORD_MARROWGAR) { }

    void EnterCombat(Unit* /*who*/) override
    {
        /* Some code */
    }
}

void AddSC_boss_lord_marrowgar()
{
    /* RegisterIcecrownCitadelCreatureAI(creatureScript); */
    RegisterIcecrownCitadelCreatureAI(boss_lord_marrowgar);
}

Different types of Creature AI's

Name
CreatureAI
BossAI
GuardianAI
PetAI
NullCreatureAI

GameObject Scripts

World Spawned GameObjects

struct go_scripted_gameobject : public GameObjectAI
{
    go_scripted_gameobject(GameObject* go) : GameObjectAI(go) { }

    void UpdateAI(uint32 const /*diff*/) override
    {
        /* Some code */
    }
}

/* At the end of a script file you will find a place where we register all
 * the scripts. */
void AddSC_scripts()
{
    /* RegisterGameObjectAI(gameObjectScript); */
    RegisterGameObjectAI(go_scripted_gameobject);
}

Instance Spawned GameObject

If a gameobject is scripted inside an instance we register them as factory.

To do this, in the headerfile of the instance we add this define:

#define Register"IntanceName"GameObjectAI(ai_name) RegisterGameObjectAIWithFactory(ai_name, Get"InstanceName"AI)

Spell Scripts

Validation of spells used in a script

Spells used within the SpellScript should always be validated at the top of the script.

This will prevent potential crashes if we try to use a spell that doesn't exist in a script.

class spell_pri_power_word_shield_aura : public AuraScript
{
    PrepareAuraScript(spell_pri_power_word_shield_aura);

    bool Validate(SpellInfo const* /*spellInfo*/) override
    {
        return ValidateSpellInfo({ SPELL_1, SPELL_2 });
    }
}

Stand alone SpellScripts and AuraScripts

For stand alone SpellScripts and AuraScripts that do not require any args, and just work once you have assigned them in the databases are handled like below:

class spell_pri_shadow_word_death : public SpellScript
{
public:
    PrepareSpellScript(spell_pri_shadow_word_death);

    void HandleDamage()
    {
        /* Some code */
    }

    void Register() override
    {
        OnHit += SpellHitFn(spell_pri_shadow_word_death::HandleDamage);
    }
}

void AddSC_priest_spell_scripts()
{
    /* RegisterSpellScript(spell/auraScript); */
    RegisterSpellScript(spell_pri_shadow_word_death);
}

Spell- and Aurascript Pairs

For SpellScripts and AuraScripts that pair together, we script them by themselves and link them at the registry.

When two scripts are going to be linked, they will always share the same name. Therefore we always suffix the AuraScript with _aura in these cases.

class spell_pri_power_word_shield_aura : public AuraScript
{
    PrepareAuraScript(spell_pri_power_word_shield_aura);

    bool Validate(SpellInfo const* /*spellInfo*/) override
    {
        return ValidateSpellInfo({ SPELL_1 });
    }

    void CalculateAmount(AuraEffect const* aurEff, int32& amount, bool& canBeRecalculated)
    {
        /* Some code */
    }

    void Register() override
    {
        DoEffectCalcAmount += AuraEffectCalcAmountFn(spell_pri_power_word_shield_aura::CalculateAmount, EFFECT_0, SPELL_AURA_SCHOOL_ABSORB);
    }
}

class spell_pri_power_word_shield : public SpellScript
{
    PrepareSpellScript(spell_pri_power_word_shield);

    SpellCastResult CheckCast()
    {
        /* Some code */
    }

    void Register() override
    {
        OnCheckCast += SpellCheckCastFn(spell_pri_power_word_shield::CheckCast);
    }
}

void AddSC_priest_spell_scripts()
{
    /* We link the two scripts together with RegisterSpellAndAuraScriptPair.
     * RegisterSpellAndAuraScriptPair(spellScript, auraScript) */
    RegisterSpellAndAuraScriptPair(spell_pri_power_word_shield, spell_pri_power_word_shield_aura);
}

SpellScripts with Args

Sometimes we make more generic SpellScripts that we later assign and pass other args to in the registry.

class spell_item_defibrillate : public SpellScript
{
    PrepareSpellScript(spell_item_defibrillate);

public:
    spell_item_defibrillate(uint8 chance, uint32 failSpell = 0) : SpellScript(), _chance(chance), _failSpell(failSpell) { }

    void HandleScript(SpellEffIndex effIndex)
    {
        /* Some code */
    }

    void Register() override
    {
        OnEffectHitTarget += SpellEffectFn(spell_item_defibrillate::HandleScript, EFFECT_0, SPELL_EFFECT_RESURRECT);
    }
}

void AddSC_item_spell_scripts()
{
    /* RegisterSpellScriptWithArgs(spellScript, scriptName, args...); */
    RegisterSpellScriptWithArgs(spell_item_defibrillate, "spell_item_goblin_jumper_cables", 67, SPELL_GOBLIN_JUMPER_CABLES_FAIL);
    RegisterSpellScriptWithArgs(spell_item_defibrillate, "spell_item_goblin_jumper_cables_xl", 50, SPELL_GOBLIN_JUMPER_CABLES_XL_FAIL);
    RegisterSpellScriptWithArgs(spell_item_defibrillate, "spell_item_gnomish_army_knife", 33);
}

Assigning Script in the Database

All scripts are assigned in the world database.

CreatureScripts

CreatureScripts can be assigned in two tables.

Table Column
creature_template ScriptName
creature ScriptName

In creature_template we assign the Script to the creature entry, meaning all spawned creatures with that entry will use the script.

In creature we assign the Script to the creature GUID, meaning that specific spawn will use the script.

The ScriptName is the same as the assigned script name in the core. F.ex boss_lord_marrowgar.

SpellScripts

SpellScripts are assigned in one table.

Table Column1 Column2
spell_script_names spell_id ScriptName

The spell_id is the id of the spell you want to assign a script to. One Spell id can assign multiple Scripts.

The ScriptName is depending on what type of registry you used. One ScriptName can be assigned to multiple spells.

Registry ScriptName
RegisterSpellScript(spellScript) spellScript
RegisterSpellAndAuraScriptPair(spellScript, auraScript) spellScript
RegisterSpellScriptWithArgs(spellScript, scriptName, args...) scriptName

SQL part

As a Script can be assigned to multiple spells we always want to delete both the spell_id and the ScriptName to avoid messing up other spells.

DELETE FROM `spell_script_names` WHERE `spell_id` = 1 AND `ScriptName` = 'spell_pri_death_touch';

The only time it is okay to only delete the ScriptName is when we are deleting or adding a new script in the core.