# Effects

Effects
Any number of effects can be attached to an object. Effects can perform various tasks, thus eliminating the need for helper objects. This is especially of interest for magic spells that act with a given duration.
Introduction
Effects are, roughly put, dynamic timers with properties that can be attached to objects. Effects themselves have no visible or acoustic representation - for this you would use objects or particles instead - effects are pure scripting helpers. They also offer a general interface that can be used to resolve conflicts of temporary status changes made to objects by other objects at the same time.
Here an example of implementing an invisibility spell without effects:
/* Invisibility spell without effect system */

local remaining_time; // time the spell is still in effect local target; // the clonk that has been made invisible local old_visibility; // previous visibility local old_modulation; // previous color modulation

func Activate(object caster, object caster2) { // get caster if (caster2) caster = caster2; target = caster;

// save previous visibility and color modulation of the caster old_visibility = caster.Visibility; old_modulation = caster->GetClrModulation();

// make caster only visible to himself and allies, color him like a ghost caster.Visibility = VIS_Owner | VIS_Allies | VIS_God; caster->SetClrModulation(ModulateColor(old_modulation, RGBa(127,127,255,127)));

// start timer: 30 seconds invisible remaining_time = 30; }

func TimerCall() { // count down if (remaining_time--) return; // done: remove object RemoveObject(); }

func Destruction() { // spell is being removed: end invisibility target->SetClrModulation(old_modulation); target.Visibility = old_visibility; }
The magic spell object exists until the spell has ended and then makes the clonk visible again. Also, if the spell object is deleted for other reasons (e.g. a scenario section change), the clonk is made visible in the Destruction callback (if this wasn't so, the clonk would remain invisible for ever). Also there is a Timer (defined in the DefCore) called every second. Notice you couldn't just have a single timer call to mark the end of the spell because timer intervals are marked in the engine beginning with the start of the round and you wouldn't know at what point within an engine timer interval the spell would start.
However, there are some problems with this implementation: for example, the magician can not cast a second invisibility spell while he's already invisible - the second spell would have practically no effect, because the end of the first spell would make the clonk visible again. The spell script would have to do some special handling for this case - but not only for multiple invisibility spells, but also for any other spell or script that might affect visibility or coloration of the clonk. Even if this spell would remember the previous value e.g. for coloration it could not handle a situation in which other scripts change the color of their own in the middle of the spell. The same problems occur when multiple scripts modify temporary clonk physcials such as jumping, walking speed, fight strength or visibility range, energy, magic energy etc. Using effects, these conflicts can be avoided.
Application
Effects are created using AddEffect and removed with RemoveEffect. If an effect was successfully created, the callback Fx*Start is made (* is replaced with the effect name). Depending on the parameters, there can also be an Fx*Timer call for continuous activity such as casting sparks, adjusting energy etc. Finally, when the effect is deleted, the callback Fx*Stop is made. Now, the invisibility spell implemented using effects:
/* Invisibility spell with effect system */

// visibility - previous visibility // old_mod - previous color modulation

func Activate(object caster, object caster2) { // get caster if (caster2) caster = caster2;

// start effect AddEffect("InvisPSpell", caster, 200, 1111, nil, GetID()); // done - the spell object is not needed anymore RemoveObject(); return true; }

func FxInvisPSpellStart(object target, proplist effect) { // Save the casters previous visibility effect.visibility = target.Visibility; effect.old_mod = target->GetClrModulation(); // Make the caster invisible target.Visibility = VIS_Owner | VIS_Allies | VIS_God; // Semitransparent and slightly blue for owner and allies target->SetClrModulation(ModulateColor(effect.old_mod, RGBa(127,127,255,127))); // Fertig return true; }

func FxInvisPSpellStop(object target, proplist effect) { // restore previous values target->SetClrModulation(effect.old_mod); target.Visibility = effect.visibility; // done return true; }
In this case, the magic spell object only starts the effect, then deletes itself immediately. The engine ensures that there are no conflicts with multiple effects modifying the visibility: effects are stored in a stack which ensures that effects are always removed in the opposite order of their addition. For this, there are a couple of extra Start and Stop calls to be made which are explained in detail later.
This effects does not have a timer function. It does, however, define a timer interval of 1111 which will invoke the standard timer function after 1111 frames which will delete the effect. Alternatively, you could define a timer function as such:
func FxInvisPSpellTimer() { // return value of -1 means that the effect should be removed return -1; }
To store the previous status of the target object, properties of the effect are used. This is necessary because in this case the effect callbacks do not have any object script context. So you cannot access any object local variables in the effect callbacks - remember that the magic spell object which has created the effect is already deleted. If you require an object context in the effect callbacks you can specify one in AddEffect(). In that case, effect callbacks would be in object local context and the effect would automatically be deleted if the target object is destroyed.
Priorities
When creating an effect you always specify a priority value which determines the effect order. The engine ensures that effects with lower priority are added before effects with a higher priority - even if this means deleting an existing effect of higher priority. So if one effect colors the clonk green and another colors the clonk red, the result will be that of the effect with higher priority. If two effects have the same priority, the order is undefined. However, it is guaranteed that effects added later always notify the Fx*Effect callback of the same priority.
In the case of the red and green color, one effect could also determine the previous coloring and then mix a result using ModulateColor. But priorities also have another function: an effect of higher priority can prevent the addition of other effects of lower priority. This is done through the Fx*Effect callback. If any existing effect reacts to this callback with the return value -1, the new effect is not added (the same applies to the Start callback of the effect itself). Here an example:
/* Spell of immunity against fire */

func Activate(object caster, object caster2) { // get caster if (caster2) caster = caster2;

// start effect AddEffect("BanBurnPSpell", caster, 180, 1111, nil, GetID());

// done - the spell object is not needed anymore RemoveObject(); return true; }

func FxBanBurnPSpellStart(object target, proplist effect, bool temporary) { // On start of the effect: extinguish clonk if (!temporary) target->Extinguish(); return true; }

func FxBanBurnPSpellEffect(string new_name) { // block fire if (WildcardMatch(new_name, "*Fire*")) return -1; // everything else is ok return 0; }
This effect makes the clonk fire-proof for 30 seconds. The effect is implemented without any Timer or Stop callbacks as the complete functionality is achieved by simply blocking other effects which might have "Fire" as part of their name. This especially applies to the engine internal fire which has exactly the name "Fire". Of course, you could still add a Timer callback for graphic effects so the player can see that his clonk is immune. Also, you could create special visual effects when preventing incineration in FxBanBurnPSpellEffect. For the like:
[...]

func FxBanBurnPSpellEffect(string new_name, object target, proplist effect, var1, var2, var3) { // only handle fire if (!WildcardMatch(new_name, "*Fire*")) return 0; // with fire, the three extra parameters have the following meaning: // var1: caused_by - player that is responsible for the fire // var2: blasted - bool: if the fire has been created by an explosion // var3: burning_object - object: incineratable object // extinguish burning object if (var3 && GetType(var3) == C4V_C4Object) var3->Extinguish(); // block fire return -1; }
This would even delete all burning objects which would otherwise incinerate the target object. The type check for var3 avoids possible conflicts with other "Fire" effects that might have differing parameters. Obviously, conflict situations like this should be avoided at all cost.
The following table contains general guidelines for priorities in effects of the original pack:

Effect
Priority
Short special effects
300-350
Effects which cannot be banned
250-300
Magic ban spell
200-250
Permanent magic ban spell
180-200
Short term, benevolent magic effects
150-180
Short term, malevolent magic effects
120-150
Normal Effects
100-120
Fire as used by the engine
100
Permanent magic effects
50-100
Permanent other effects
20-50
Internal effects, data storage etc.
1

Generally, effect priorities should be chosen by dependency: if one effect should prevent another it needs a higher priority to do this (even if it is a permanent effect). Short term effects should have a higher priority than long term effects so that short term changes in the object are visible on top of long term effects.
The engine internal fire is of priority 100. So a magic fire which also uses the properties of the engine fire should have a slightly higher priority and should call the respective FxFire* functions within its callbacks. For proper functioning all effect callback (i.e. Start, Timer, and Stop) should be forwarded as each might depend on the action of the others. If this is not possible in your case, you should reimplement the complete fire functionality by script.
Effects with priority 1 are a special case: Other effects are never temporarily removed for them and they are never temporarily removed themselves.
Special Add/Remove Calls
For the engine to ensure that effects are always removed in opposite order, it might in some cases be necessary to temporarily remove and later re-add existing effects. In these situations, the scripter should obviously take care to remove any object changes and reapply them after re-adding so that other effects will behave accordingly.
Effects are also removed when the target object is deleted or dies - the cause for the removal is passed in the reason parameter to the Remove function of the effect. This can be used e.g. to reanimate a clonk immediately upon his death:
/* Resurrection spell */

func Activate(object caster, object caster2) { // get caster if (caster2) caster = caster2;

// start effect AddEffect("ReincarnationPSpell", caster, 180, 0, nil, GetID());

// done - the spell object is not needed anymore RemoveObject(); return true; }

func FxReincarnationPSpellStart(object target, proplist effect, bool temporary) { // Only at the first start: message if (!temporary) target->Message("%s gets an extra life", target->GetName()); return true; }

func FxReincarnationPSpellStop(object target, proplist effect, int reason, bool temporary) { // only when the clonk died if (reason != 4) return true;

// the clonk has already been resurrected if (target->GetAlive()) return -1; // resurrect clonk target->SetAlive(true); // give energy target->DoEnergy(100); // message target->Message("%s has been resurrected.", target->GetName());

// remove return true; }
This effect reanimates the clonk as many times as he has cast the reanimation spell.
Global Effects
Global effects are effects that are not bound to any target object. With global effects, too, priorities are observed and temporary Add/Remove calls might be necessary to ensure order. Simply imagine all global effects are attached to an imaginary object. Global effects are accessed whenever you specify `nil` for the target object.
This can be used to make changes to gravity, sky color, etc. Here's an example for a spell that temporarily reduces gravity and then resets the original value:
/* Gravitation spell */

// grav - previous gravitation // change - change by the spell

func Activate(object caster, object caster2) { // start global effect AddEffect("GravChangeUSpell", nil, 150, 37, nil, GetID(), -10); // done - the spell object is not needed anymore RemoveObject(); return true; }

func FxGravChangeUSpellStart(object target, proplist effect, bool temporary, change) { // search for other gravitation effects if (!temporary) { var otherEffect = GetEffect("GravChangeUSpell", target); if (otherEffect == num) iOtherEffect = GetEffect("GravChangeUSpell", target, 1); if (otherEffect) { // add gravitation change to other effect otherEffect.change += change; SetGravity(GetGravity() + change); // and remove self return -1; } } // save previous gravitation effect.grav = GetGravity(); // for non-temporary calls change is passed and added to the changed value if (change) effect.change += change; // set gravitation change // the change can be not equal to change in temporary calls SetGravity(effect.grav + effect.change);

return true; }

func FxGravChangeUSpellTimer(object target, proplist effect) { // slowly return to normal gravitation if (Inside(effect.change, -1, 1)) return -1; var iDir = -effect.change/Abs(effect.change); effect.change += iDir; SetGravity(GetGravity() + iDir); return true; }

func FxGravChangeUSpellStop(object target, proplist effect) { // restore gravitation SetGravity(effect.grav);

return true; }
target
will be `nil` in all these effect calls. You should still pass this parameter to calls such as EffectCall() for then it is also possible to attach effects to the magician or perhaps a magic tower. In this case, gravity would automatically be reset as soon as the magician dies or the magic tower is destroyed.
Adding Effects
In the previous example, several gravitational effects were combined so that the gravity change lasts longer if the spell is casted multiple times. Adding these effects cannot be done in the Effect callback because the gravitation effect might always be prevented by another effect with higher priority (e.g. a no-spells-whatsoever-effect). Through the special Fx*Add callback you can achieve the desired result more easily, or at least in a more structured fashion.
[...]

func FxGravChangeUSpellEffect(string new_name) { // If the newly added effect is also a gravitation change, ask to take over the effect if (new_name == "GravChangeUSpell") return -3; }

func FxGravChangeUSpellAdd(object target, proplist effect, string new_name, int new_timer, change) { // this is called when the effect has been taken over // add the gravitation change to this effect effect.change += change; SetGravity(GetGravity() + change);

return true; }
Returning -3 in the Fx*Effect callback will cause the Fx*Add callback to be invoked for the new effect. In this case the new effect is not actually created and the function AddEffect will return the effect which has taken on the consequences of the new effect instead. As opposed to the method above this has the advantage that the return value can now be used to determine whether the effect has been created at all.
Properties Reference
Effects have a number of standard properties:

Effect Properties
Data type
Name
Description
string
Name
Can be changed.
int
Priority
See Priorities
int
Interval
Of the Timer callback.
int
Time
The age of the effect in frames, used for the Timer callback. Can be changed.
proplist
CommandTarget
Either the command object or the command definition, depending on which is used.

User Defined Properties
Effects can be easily classified by name. In this way, e.g. all magic spell effects can easily be found through the respective wildcard string. If, however, you want to create user-defined properties which also apply to existing effects you can do this by defining additional effect functions:
global func FxFireIsHot() { return true; }

// Function that removes all "hot" effects from the caller global func RemoveAllHotEffects() { var target = this; if(!this) return;

var effect_num, i; while (effect = GetEffect("*", target, i++)) if (EffectCall(target, effect, "IsHot")) RemoveEffect(nil, target, effect); }
Using EffectCall() you can of course also call functions in the effect, e.g. to extend certain effects.
Blind Effects
Sometimes effects only need to be created in order to produce the respective callbacks in other effects - for example with magic spells which don't have any animation or long term effects but which nonetheless might be blocked by other effects. Example for the earthquake spell:
/* Earthquake spell */

func Activate(object caster, object caster2) { // check effect var result; if (result = CheckEffect("EarthquakeNSpell", nil, 150)) { RemoveObject(); return result != -1; } // execute effect if (caster->GetDir() == DIR_Left) CastLeft(); else CastRight();

// remove spell object RemoveObject(); return true; }
The return value of CheckEffect() is -1 if the effect was rejected and a positive value or -2 if the effect was accepted. In both cases the effect itself should not be executed, but in the latter case the Activate function may signal success by returning 1.
Extended Possibilities
As every effect has its own data storage, effects are also a way of attaching external data to objects without having to change the object definition for that. Also, simple calls can be delayed, e.g. for one frame after destruction of the object as is done at one place in the Knights pack:
// The call CastleChange must be delayed so that the castle part is really gone when it is called // otherwise, the FindObject()-calls would still find the castle part func CastlePartDestruction() { if (basement) basement->RemoveObject(); // Global temporary effect, if not already there if (!GetEffect("IntCPW2CastleChange")) AddEffect("IntCPW2CastleChange", nil, 1, 2, nil, CPW2); return true; }

func FxIntCPW2CastleChangeStop() { // notice all castle parts for(var obj in FindObjects(Find_OCF(OCF_Fullcon), Find_NoContainer()) obj->~CastleChange();

return true; }
For this application, the effect name should start with "Int" (especially if working with global callbacks) followed by the id of the object to avoid any kind of name conflict with other effects.
Also, certain action can be taken at the death of an object without having to modify that object's definition. A scenario script might contain:
/* scenario script */

func Initialize() { // manipulate all wipfs for(var obj in FindObjects(Find_ID(WIPF), Find_OCF(OCF_Alive))) AddEffect("ExplodeOnDeathCurse", obj, 20); }

global func FxExplodeOnDeathCurseStop(object target, proplist effect, int reason) { // died? boom! if (reason == 4) target->Explode(20); return true; }
All wipfs present at the beginning of the scenario will explode on death!
Naming
So that effects might properly recognize and manipulate each other you should stick to the following naming scheme ("*abc" means endings, "abc*" means prefixes, and "*abc*" means string parts which might occur anywhere in the name).

Name section
Meaning
*Spell
Magic effect
*PSpell
Benevolent magic effect
*NSpell
Malevolent magic effect
*USpell
Neutral magic effect
*Fire*
Fire effect - the function Extinguish() removes all effects of this name
*Curse*
Curse
*Ban*
Effect preventing other effects (e.g. fire proofness or immunity)
Int*
Internal effect (data storage etc.)
*Potion
Magic potion

Warning: as function names may not be more than 100 characters in length (and you will lose oversight eventually), you should not stuff too much information into the effect name. Effect names are case sensitive. Also, you should avoid using any of these identifiers in your effect names if your effect doesn't have anything to do with them.
Callback Reference
The following callbacks are made by the engine and should be implemented in your script according to necessity. * is to be replaced by your effect name.
Fx*Start
int Fx*Start (object target, proplist effect, int temporary, any var1, any var2, any var3, any var4);
Called at the start of the effect. target is the target object of the effect. `effect` is the effect itself. `effect` can be used to manipulate the effect, for example with `effect.Interval=newinterval`.
In normal operation the parameter temporary is 0. It is 1 if the effect is re-added after having been temporarily removed and 2 if the effect was temporarily removed and is now to be deleted (in this case a Remove call will follow).
If temporary is 0, var1 to var4 are the additional parameters passed to AddEffect().
If temporary is 0 and this callback returns -1 the effect is not created and the corrsponding AddEffect() call returns 0.
Fx*Stop
int Fx*Stop (object target, proplist effect, int reason, bool temporary);
When the effect is temporarily or permanently removed. target again is the target object and `effect` the effect itself.
reason contains the cause of the removal and can be one of the following values:

Script constant
reason
Meaning
FX_Call_Normal
0
Normal removal
FX_Call_Temp
1
Temporary removal (temporary is 1).
FX_Call_TempAddForRemoval
2
Not used
FX_Call_RemoveClear
3
The target object has been deleted
FX_Call_RemoveDeath
4
The target object has died

The effect can prevent removal by returning -1. This will not help, however, in temporary removals or if the target object has been deleted.
Fx*Timer
int Fx*Timer (object target, proplist effect, int time);
Periodic timer call, if a timer interval has been specified at effect creation. target and `effect` as usual.
time specifies how long the effect has now been active. This might alternatively be determined using effect.Time.
If this function is not implemented or returns -1, the effect will be deleted after this call.
Fx*Effect
int Fx*Effect (string new_name, object target, proplist effect, any var1, any var2, any var3, any var4);
A call to all effects of higher priority if a new effect is to be added to the same target object. new_name is the name of the new effect; `effect` is the effect being called.
Warning: the new effect is not yet properly initialized and should not be manipulated in any way. Especially the priority field might not yet have been set.
This function can return -1 to reject the new effect. As the new effect might also be rejected by other effects, this callback should not try to add effects or similar (see gravitation spell). Generally you should not try to manipulate any effects during this callback.
Return -2 or -3 to accept the new effect. As long as the new effect is not rejected by any other effect, the Fx*Add call is then made to the accepting effect, the new effect is not actually created, and the calling AddEffect function returns the effect index of the accepting effect. The return value -3 will also temporarily remove all higher prioriy effects just before the Fx*Add callback and re-add them later.
var1 bis var4 are the parameters passed to AddEffect()
Fx*Add
int Fx*Add (object target, proplist effect, string new_name, int new_timer, any var1, any var2, any var3, any var4);
Callback to the accepting effect if that has returned -2 or -3 to a prior Fx*Effect call. `effect` identifies the accepting effect to which the consequences of the new effect will be added; target is the target object (0 for global effects).
new_timer is the timer interval of the new effect; var1 to var4 are the parameters from AddEffect. Notice: in temporary calls, these parameters are not available - here they will be 0.
If -1 is returned, the accepting effect is deleted also. Logically, the calling AddEffect function will then return -2.
Fx*Damage
int Fx*Damage (object target, proplist effect, int damage, int cause);
Every effect receives this callback whenever the energy or damage value of the target object is to change. If the function is defined, it should then return whether to allow the change.
This callback is made upon life energy changes in living beings and damage value changes in non-livings - but not vice versa. cause contains the value change and reason:

Script constant
cause
Meaning
FX_Call_DmgScript
0
Damage by script call DoDamage()
FX_Call_DmgBlast
1
Damage by explosion
FX_Call_DmgFire
2
Damage by fire
FX_Call_DmgChop
3
Damage by chopping (only trees)
FX_Call_EngScript
32
Energy value change by script call DoEnergy()
FX_Call_EngBlast
33
Energy loss by explosion
FX_Call_EngObjHit
34
Energy loss by object hit
FX_Call_EngFire
35
Energy loss by fire
FX_Call_EngBaseRefresh
36
Energy recharge at the home base
FX_Call_EngAsphyxiation
37
Energy loss by suffocation
FX_Call_EngCorrosion
38
Energy loss through acid
FX_Call_EngStruct
39
Energy loss of buildings (only "living" buildings)
FX_Call_EngGetPunched
40
Energy loss through clonk-to-clonk battle

Generally, the expression "cause & 32" can be used to determine whether the energy or damage values were changed.
Using this callback, damage to an object can be prevented, lessened, or increased. You could deduct magic energy instead, transfer damage to other objects, or something similar.
Function Reference
There are the following functions for manipulation of effects:

- AddEffect
  () - for effect creation
- RemoveEffect
  () - for effect removal
- GetEffect
  () - to search for effects
- GetEffectCount
  () - for effect counting
- EffectCall
  () - for user defined calls in effects
- CheckEffect
  () - to cause effects callbacks without actually creating an effect

Sven2
2004-03