forked from TrinityCore/TrinityCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_spell.cpp
More file actions
489 lines (428 loc) · 21.8 KB
/
example_spell.cpp
File metadata and controls
489 lines (428 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* Copyright (C) 2008-2012 TrinityCore <http://www.trinitycore.org/>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/*
* An example of a spell script file
* to bind a script to spell you have to add entry for it in `spell_script_names`
* where `spell_id` is id of the spell to bind
* and `ScriptName` is the name of a script assigned on registration
*/
#include "ScriptPCH.h"
#include "SpellAuras.h"
#include "SpellAuraEffects.h"
enum Spells
{
SPELL_TRIGGERED = 18282
};
class spell_ex_5581 : public SpellScriptLoader
{
public:
spell_ex_5581() : SpellScriptLoader("spell_ex_5581") { }
class spell_ex_5581SpellScript : public SpellScript
{
// initialize script, this macro does compile time check for type of the function - prevents possible issues
// if you have assigned wrong type of function to a hook you'll receive type conversion error during build
// this line is required, otherwise you'll get XXXHandlerFunction - identifier not found errors
PrepareSpellScript(spell_ex_5581SpellScript);
std::string localVariable;
char* localVariable2;
// function called on server startup
// checks if script has data required for it to work
bool Validate(SpellInfo const* /*spellEntry*/)
{
// check if spellid 70522 exists in dbc, we will trigger it later
if (!sSpellMgr->GetSpellInfo(SPELL_TRIGGERED))
return false;
return true;
}
// function called just after script is added to spell
// we initialize local variables if needed
bool Load()
{
localVariable = "we're using local variable";
localVariable2 = new char();
return true;
// return false - script will be immediately removed from the spell
// for example - we don't want this script to be executed on a creature
// if (GetCaster()->GetTypeID() != TYPEID_PLAYER)
// return false;
}
// function called just before script delete
// we free allocated memory
void Unload()
{
delete localVariable2;
}
void HandleBeforeCast()
{
// this hook is executed before anything about casting the spell is done
// after this hook is executed all the machinery starts
sLog->outString("Caster just finished preparing the spell (cast bar has expired)");
}
void HandleOnCast()
{
// cast is validated and spell targets are selected at this moment
// this is a last place when the spell can be safely interrupted
sLog->outString("Spell is about to do take reagents, power, launch missile, do visuals and instant spell effects");
}
void HandleAfterCast()
{
sLog->outString("All immediate actions for the spell are finished now");
// this is a safe for triggering additional effects for a spell without interfering
// with visuals or with other effects of the spell
//GetCaster()->CastSpell(target, SPELL_TRIGGERED, true);
}
SpellCastResult CheckRequirement()
{
// in this hook you can add additional requirements for spell caster (and throw a client error if reqs're not passed)
// in this case we're disallowing to select non-player as a target of the spell
//if (!GetTargetUnit() || GetTargetUnit()->ToPlayer())
//return SPELL_FAILED_BAD_TARGETS;
return SPELL_CAST_OK;
}
void HandleDummyLaunch(SpellEffIndex /*effIndex*/)
{
sLog->outString("Spell %u with SPELL_EFFECT_DUMMY is just launched!", GetSpellInfo()->Id);
}
void HandleDummyLaunchTarget(SpellEffIndex /*effIndex*/)
{
uint64 targetGUID = 0;
if (Unit* unitTarget = GetHitUnit())
targetGUID = unitTarget->GetGUID();
// we're handling SPELL_EFFECT_DUMMY in effIndex 0 here
sLog->outString("Spell %u with SPELL_EFFECT_DUMMY is just launched at it's target: " UI64FMTD "!", GetSpellInfo()->Id, targetGUID);
}
void HandleDummyHit(SpellEffIndex /*effIndex*/)
{
sLog->outString("Spell %u with SPELL_EFFECT_DUMMY has hit!", GetSpellInfo()->Id);
}
void HandleDummyHitTarget(SpellEffIndex /*effIndex*/)
{
sLog->outString("SPELL_EFFECT_DUMMY is hits it's target!");
// make caster cast a spell on a unit target of effect
if (Unit* target = GetHitUnit())
GetCaster()->CastSpell(target, SPELL_TRIGGERED, true);
}
void HandleBeforeHit()
{
sLog->outString("Spell is about to hit target!");
}
void HandleOnHit()
{
sLog->outString("Spell just hit target!");
}
void HandleAfterHit()
{
sLog->outString("Spell just finished hitting target!");
}
void FilterTargets(std::list<Unit*>& /*targetList*/)
{
// usually you want this call for Area Target spells
sLog->outString("Spell is about to add targets from targetList to final targets!");
}
// register functions used in spell script - names of these functions do not matter
void Register()
{
// we're registering our functions here
BeforeCast += SpellCastFn(spell_ex_5581SpellScript::HandleBeforeCast);
OnCast += SpellCastFn(spell_ex_5581SpellScript::HandleOnCast);
AfterCast += SpellCastFn(spell_ex_5581SpellScript::HandleAfterCast);
OnCheckCast += SpellCheckCastFn(spell_ex_5581SpellScript::CheckRequirement);
// function HandleDummy will be called when spell is launched, independant from targets selected for spell, just before default effect 0 launch handler
OnEffectLaunch += SpellEffectFn(spell_ex_5581SpellScript::HandleDummyLaunch, EFFECT_0, SPELL_EFFECT_DUMMY);
// function HandleDummy will be called when spell is launched at target, just before default effect 0 launch at target handler
OnEffectLaunchTarget += SpellEffectFn(spell_ex_5581SpellScript::HandleDummyLaunchTarget, EFFECT_0, SPELL_EFFECT_DUMMY);
// function HandleDummy will be called when spell hits it's destination, independant from targets selected for spell, just before default effect 0 hit handler
OnEffectHit += SpellEffectFn(spell_ex_5581SpellScript::HandleDummyHit, EFFECT_0, SPELL_EFFECT_DUMMY);
// function HandleDummy will be called when unit is hit by spell, just before default effect 0 hit target handler
OnEffectHitTarget += SpellEffectFn(spell_ex_5581SpellScript::HandleDummyHitTarget, EFFECT_0, SPELL_EFFECT_DUMMY);
// this will prompt an error on startup because effect 0 of spell 49375 is set to SPELL_EFFECT_DUMMY, not SPELL_EFFECT_APPLY_AURA
//OnEffectHitTarget += SpellEffectFn(spell_gen_49375SpellScript::HandleDummy, EFFECT_0, SPELL_EFFECT_APPLY_AURA);
// this will make HandleDummy function to be called on first != 0 effect of spell 49375
//OnEffectHitTarget += SpellEffectFn(spell_gen_49375SpellScript::HandleDummy, EFFECT_FIRST_FOUND, SPELL_EFFECT_ANY);
// this will make HandleDummy function to be called on all != 0 effect of spell 49375
//OnEffectHitTarget += SpellEffectFn(spell_gen_49375SpellScript::HandleDummy, EFFECT_ALL, SPELL_EFFECT_ANY);
// bind handler to BeforeHit event of the spell
BeforeHit += SpellHitFn(spell_ex_5581SpellScript::HandleBeforeHit);
// bind handler to OnHit event of the spell
OnHit += SpellHitFn(spell_ex_5581SpellScript::HandleOnHit);
// bind handler to AfterHit event of the spell
AfterHit += SpellHitFn(spell_ex_5581SpellScript::HandleAfterHit);
// bind handler to OnUnitTargetSelect event of the spell
//OnUnitTargetSelect += SpellUnitTargetFn(spell_ex_5581SpellScript::FilterTargets, EFFECT_0, TARGET_UNIT_CASTER);
}
};
// function which creates SpellScript
SpellScript* GetSpellScript() const
{
return new spell_ex_5581SpellScript();
}
};
class spell_ex_66244 : public SpellScriptLoader
{
public:
spell_ex_66244() : SpellScriptLoader("spell_ex_66244") { }
class spell_ex_66244AuraScript : public AuraScript
{
PrepareAuraScript(spell_ex_66244AuraScript);
// function called on server startup
// checks if script has data required for it to work
bool Validate(SpellInfo const* /*spellEntry*/)
{
// check if spellid exists in dbc, we will trigger it later
if (!sSpellMgr->GetSpellInfo(SPELL_TRIGGERED))
return false;
return true;
}
// function called in aura constructor
// we initialize local variables if needed
bool Load()
{
// do not load script if aura is casted by player or caster not avalible
if (Unit* caster = GetCaster())
if (caster->GetTypeId() == TYPEID_PLAYER)
return true;
return false;
}
void HandleOnEffectApply(AuraEffect const* /*aurEff*/, AuraEffectHandleModes /*mode*/)
{
sLog->outString("Aura Effect is about to be applied on target!");
// this hook allows you to prevent execution of AuraEffect handler, or to replace it with your own handler
//PreventDefaultAction();
}
void HandleOnEffectRemove(AuraEffect const* /*aurEff*/, AuraEffectHandleModes /*mode*/)
{
sLog->outString("Aura Effect is about to be removed from target!");
// this hook allows you to prevent execution of AuraEffect handler, or to replace it with your own handler
//PreventDefaultAction();
}
void HandleAfterEffectApply(AuraEffect const* /*aurEff*/, AuraEffectHandleModes /*mode*/)
{
sLog->outString("Aura Effect has just been applied on target!");
Unit* target = GetTarget();
// cast spell on target on aura apply
target->CastSpell(target, SPELL_TRIGGERED, true);
}
void HandleAfterEffectRemove(AuraEffect const* /*aurEff*/, AuraEffectHandleModes /*mode*/)
{
sLog->outString("Aura Effect has just been just removed from target!");
Unit* target = GetTarget();
Unit* caster = GetCaster();
// caster may be not avalible (logged out for example)
if (!caster)
return;
// cast spell on caster on aura remove
target->CastSpell(caster, SPELL_TRIGGERED, true);
}
void HandleEffectPeriodic(AuraEffect const* /*aurEff*/)
{
sLog->outString("Perioidic Aura Effect is does a tick on target!");
Unit* target = GetTarget();
// aura targets damage self on tick
target->DealDamage(target, 100);
}
void HandleEffectPeriodicUpdate(AuraEffect* aurEff)
{
sLog->outString("Perioidic Aura Effect is now updated!");
// we're doubling aura amount every tick
aurEff->ChangeAmount(aurEff->GetAmount() * 2);
}
void HandleEffectCalcAmount(AuraEffect const* /*aurEff*/, int32& amount, bool& canBeRecalculated)
{
sLog->outString("Amount of Aura Effect is being calculated now!");
// we're setting amount to 100
amount = 100;
// amount will be never recalculated due to applying passive aura
canBeRecalculated = false;
}
void HandleEffectCalcPeriodic(AuraEffect const* /*aurEff*/, bool& isPeriodic, int32& amplitude)
{
sLog->outString("Periodic data of Aura Effect is being calculated now!");
// we're setting aura to be periodic and tick every 10 seconds
isPeriodic = true;
amplitude = 2 * IN_MILLISECONDS;
}
void HandleEffectCalcSpellMod(AuraEffect const* /*aurEff*/, SpellModifier*& spellMod)
{
sLog->outString("SpellMod data of Aura Effect is being calculated now!");
// we don't want spellmod for example
if (spellMod)
{
delete spellMod;
spellMod = NULL;
}
/*
// alternative: we want spellmod for spell which doesn't have it
if (!spellMod)
{
spellMod = new SpellModifier(GetAura());
spellMod->op = SPELLMOD_DOT;
spellMod->type = SPELLMOD_PCT;
spellMod->spellId = GetId();
spellMod->mask[1] = 0x00002000;
}
*/
}
// function registering
void Register()
{
OnEffectApply += AuraEffectApplyFn(spell_ex_66244AuraScript::HandleOnEffectApply, EFFECT_0, SPELL_AURA_DUMMY, AURA_EFFECT_HANDLE_REAL);
OnEffectRemove += AuraEffectRemoveFn(spell_ex_66244AuraScript::HandleOnEffectRemove, EFFECT_0, SPELL_AURA_DUMMY, AURA_EFFECT_HANDLE_REAL);
// AURA_EFFECT_HANDLE_REAL_OR_REAPPLY_MASK - makes handler to be called when aura is reapplied on target
AfterEffectApply += AuraEffectApplyFn(spell_ex_66244AuraScript::HandleAfterEffectApply, EFFECT_0, SPELL_AURA_DUMMY, AURA_EFFECT_HANDLE_REAL_OR_REAPPLY_MASK);
AfterEffectRemove += AuraEffectRemoveFn(spell_ex_66244AuraScript::HandleAfterEffectRemove, EFFECT_0, SPELL_AURA_DUMMY, AURA_EFFECT_HANDLE_REAL);
OnEffectPeriodic += AuraEffectPeriodicFn(spell_ex_66244AuraScript::HandleEffectPeriodic, EFFECT_0, SPELL_AURA_DUMMY);
OnEffectUpdatePeriodic += AuraEffectUpdatePeriodicFn(spell_ex_66244AuraScript::HandleEffectPeriodicUpdate, EFFECT_0, SPELL_AURA_DUMMY);
DoEffectCalcAmount += AuraEffectCalcAmountFn(spell_ex_66244AuraScript::HandleEffectCalcAmount, EFFECT_0, SPELL_AURA_DUMMY);
DoEffectCalcPeriodic += AuraEffectCalcPeriodicFn(spell_ex_66244AuraScript::HandleEffectCalcPeriodic, EFFECT_0, SPELL_AURA_DUMMY);
DoEffectCalcSpellMod += AuraEffectCalcSpellModFn(spell_ex_66244AuraScript::HandleEffectCalcSpellMod, EFFECT_0, SPELL_AURA_DUMMY);
/*OnApply += AuraEffectApplyFn();
OnRemove += AuraEffectRemoveFn();
DoCheckAreaTarget += AuraCheckAreaTargetFn();*/
}
/*
void OnApply()
{
}
void OnRemove()
{
}
bool DoCheckAreaTarget(Unit* proposedTarget)
{
}*/
};
// function which creates AuraScript
AuraScript* GetAuraScript() const
{
return new spell_ex_66244AuraScript();
}
};
// example usage of OnEffectManaShield and AfterEffectManaShield hooks
// see spell_ex_absorb_aura, these hooks work the same as OnEffectAbsorb and AfterEffectAbsorb
// example usage of OnEffectAbsorb and AfterEffectAbsorb hooks
class spell_ex_absorb_aura : public SpellScriptLoader
{
public:
spell_ex_absorb_aura() : SpellScriptLoader("spell_ex_absorb_aura") { }
class spell_ex_absorb_auraAuraScript : public AuraScript
{
PrepareAuraScript(spell_ex_absorb_auraAuraScript);
void HandleOnEffectAbsorb(AuraEffect* /*aurEff*/, DamageInfo & dmgInfo, uint32 & absorbAmount)
{
sLog->outString("Our aura is now absorbing damage done to us!");
// absorb whole damage done to us
absorbAmount = dmgInfo.GetDamage();
}
void HandleAfterEffectAbsorb(AuraEffect* /*aurEff*/, DamageInfo & /*dmgInfo*/, uint32 & absorbAmount)
{
sLog->outString("Our aura has absorbed %u damage!", absorbAmount);
}
// function registering
void Register()
{
OnEffectAbsorb += AuraEffectAbsorbFn(spell_ex_absorb_auraAuraScript::HandleOnEffectAbsorb, EFFECT_0);
AfterEffectAbsorb += AuraEffectAbsorbFn(spell_ex_absorb_auraAuraScript::HandleAfterEffectAbsorb, EFFECT_0);
}
};
// function which creates AuraScript
AuraScript* GetAuraScript() const
{
return new spell_ex_absorb_auraAuraScript();
}
};
class spell_ex_463 : public SpellScriptLoader
{
public:
spell_ex_463() : SpellScriptLoader("spell_ex_463") { }
class spell_ex_463AuraScript : public AuraScript
{
PrepareAuraScript(spell_ex_463AuraScript);
bool CheckAreaTarget(Unit* target)
{
sLog->outString("Area aura checks if unit is a valid target for it!");
// in our script we allow only players to be affected
return target->GetTypeId() == TYPEID_PLAYER;
}
void Register()
{
DoCheckAreaTarget += AuraCheckAreaTargetFn(spell_ex_463AuraScript::CheckAreaTarget);
}
};
// function which creates AuraScript
AuraScript* GetAuraScript() const
{
return new spell_ex_463AuraScript();
}
};
// this function has to be added to function set in ScriptLoader.cpp
void AddSC_example_spell_scripts()
{
new spell_ex_5581;
new spell_ex_66244;
new spell_ex_absorb_aura;
new spell_ex_463;
}
/* empty script for copypasting
class spell_ex : public SpellScriptLoader
{
public:
spell_ex() : SpellScriptLoader("spell_ex") { }
class spell_ex_SpellScript : public SpellScript
{
PrepareSpellScript(spell_ex_SpellScript);
//bool Validate(SpellInfo const* spellEntry){return true;}
//bool Load(){return true;}
//void Unload(){}
//void Function(SpellEffIndex effIndex) //OnEffect += SpellEffectFn(spell_ex_SpellScript::Function, EFFECT_ANY, SPELL_EFFECT_ANY);
//void Function() //OnHit += SpellEffectFn(spell_ex_SpellScript::Function);
void Register()
{
}
};
SpellScript* GetSpellScript() const
{
return new spell_ex_SpellScript();
}
};
*/
/* empty script for copypasting
class spell_ex : public SpellScriptLoader
{
public:
spell_ex() : SpellScriptLoader("spell_ex") { }
class spell_ex_AuraScript : public AuraScript
{
PrepareAuraScript(spell_ex)
//bool Validate(SpellInfo const* spellEntry){return true;}
//bool Load(){return true;}
//void Unload(){}
//void spell_ex_SpellScript::Function(AuraEffect const* aurEff, AuraEffectHandleModes mode) //OnEffectApply += AuraEffectApplyFn(spell_ex_SpellScript::Function, EFFECT_ANY, SPELL_AURA_ANY, AURA_EFFECT_HANDLE_REAL);
//void spell_ex_SpellScript::Function(AuraEffect const* aurEff, AuraEffectHandleModes mode) //OnEffectRemove += AuraEffectRemoveFn(spell_ex_SpellScript::Function, EFFECT_ANY, SPELL_AURA_ANY, AURA_EFFECT_HANDLE_REAL);
//void spell_ex_SpellScript::Function(AuraEffect const* aurEff) //OnEffectPeriodic += AuraEffectPeriodicFn(spell_ex_SpellScript::Function, EFFECT_ANY, SPELL_AURA_ANY);
//void spell_ex_SpellScript::Function(AuraEffect* aurEff) //OnEffectUpdatePeriodic += AuraEffectUpdatePeriodicFn(spell_ex_SpellScript::Function, EFFECT_ANY, SPELL_AURA_ANY);
//void spell_ex_SpellScript::Function(AuraEffect const* aurEff, int32& amount, bool& canBeRecalculated) //DoEffectCalcAmount += AuraEffectCalcAmountFn(spell_ex_SpellScript::Function, EFFECT_ANY, SPELL_AURA_ANY);
//void spell_ex_SpellScript::Function(AuraEffect const* aurEff, bool& isPeriodic, int32& amplitude) //OnEffectCalcPeriodic += AuraEffectCalcPeriodicFn(spell_ex_SpellScript::Function, EFFECT_ANY, SPELL_AURA_ANY);
//void spell_ex_SpellScript::Function(AuraEffect const* aurEff, SpellModifier*& spellMod) //OnEffectCalcSpellMod += AuraEffectCalcSpellModFn(spell_ex_SpellScript::Function, EFFECT_ANY, SPELL_AURA_ANY);
void Register()
{
}
};
AuraScript* GetAuraScript() const
{
return new spell_ex_AuraScript();
}
};
*/