1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "EffectQueue.h"
23 #include "DisplayMessage.h"
26 #include "Interface.h"
28 #include "SymbolMgr.h"
29 #include "Scriptable/Actor.h"
30 #include "Spell.h" //needs for the source flags bitfield
36 EffectFunction Function
;
38 } Opcodes
[MAX_EFFECTS
];
40 static int initialized
= 0;
42 static EffectRef
*effectnames
= NULL
;
43 static int effectnames_count
= 0;
45 bool EffectQueue::match_ids(Actor
*target
, int table
, ieDword value
)
57 stat
= IE_GENERAL
; break;
59 stat
= IE_RACE
; break;
61 stat
= IE_CLASS
; break;
63 stat
= IE_SPECIFIC
; break;
67 stat
= target
->GetStat(IE_ALIGNMENT
);
70 if( a
!= ( stat
& 15 )) {
76 if( a
!= ( stat
& 0xf0 )) {
84 if( target
->GetStat(stat
)==value
) {
90 static const bool fx_instant
[MAX_TIMING_MODE
]={true,true,true,false,false,false,false,false,true,true,true};
92 inline bool IsInstant(ieByte timingmode
)
94 if( timingmode
>=MAX_TIMING_MODE
) return false;
95 return fx_instant
[timingmode
];
98 static const bool fx_equipped
[MAX_TIMING_MODE
]={false,false,true,false,false,true,false,false,true,false,false};
100 inline bool IsEquipped(ieByte timingmode
)
102 if( timingmode
>=MAX_TIMING_MODE
) return false;
103 return fx_equipped
[timingmode
];
106 // 0 1 2 3 4 5 6 7 8 9 10
107 static const bool fx_relative
[MAX_TIMING_MODE
]={true,false,false,true,true,true,false,false,false,false,false};
109 inline bool NeedPrepare(ieWord timingmode
)
111 if( timingmode
>=MAX_TIMING_MODE
) return false;
112 return fx_relative
[timingmode
];
120 static const int fx_prepared
[MAX_TIMING_MODE
]={DURATION
,PERMANENT
,PERMANENT
,DELAYED
, //0-3
121 DELAYED
,DELAYED
,DELAYED
,DELAYED
,PERMANENT
,PERMANENT
,PERMANENT
}; //4-7
123 inline int DelayType(ieByte timingmode
)
125 if( timingmode
>=MAX_TIMING_MODE
) return INVALID
;
126 return fx_prepared
[timingmode
];
129 //which effects are removable
130 static const bool fx_removable
[MAX_TIMING_MODE
]={true,true,false,true,true,false,true,true,false,false,true};
132 inline int IsRemovable(ieByte timingmode
)
134 if( timingmode
>=MAX_TIMING_MODE
) return INVALID
;
135 return fx_removable
[timingmode
];
138 //change the timing method after the effect triggered
139 static const ieByte fx_triggered
[MAX_TIMING_MODE
]={FX_DURATION_JUST_EXPIRED
,FX_DURATION_INSTANT_PERMANENT
,//0,1
140 FX_DURATION_INSTANT_WHILE_EQUIPPED
,FX_DURATION_DELAY_LIMITED_PENDING
,//2,3
141 FX_DURATION_AFTER_EXPIRES
,FX_DURATION_PERMANENT_UNSAVED
, //4,5
142 FX_DURATION_INSTANT_LIMITED
,FX_DURATION_JUST_EXPIRED
,FX_DURATION_PERMANENT_UNSAVED
,//6,8
143 FX_DURATION_INSTANT_PERMANENT_AFTER_BONUSES
,FX_DURATION_JUST_EXPIRED
};//9,10
145 //change the timing method for effect that should trigger after this effect expired
146 static const ieDword fx_to_delayed
[]={FX_DURATION_JUST_EXPIRED
,FX_DURATION_JUST_EXPIRED
,
147 FX_DURATION_PERMANENT_UNSAVED
,FX_DURATION_DELAY_LIMITED_PENDING
,
148 FX_DURATION_AFTER_EXPIRES
,FX_DURATION_PERMANENT_UNSAVED
, //4,5
149 FX_DURATION_JUST_EXPIRED
,FX_DURATION_JUST_EXPIRED
,FX_DURATION_JUST_EXPIRED
,//6,8
150 FX_DURATION_JUST_EXPIRED
,FX_DURATION_JUST_EXPIRED
};//9,10
152 inline ieByte
TriggeredEffect(ieByte timingmode
)
154 if( timingmode
>=MAX_TIMING_MODE
) return false;
155 return fx_triggered
[timingmode
];
158 int compare_effects(const void *a
, const void *b
)
160 return stricmp(((EffectRef
*) a
)->Name
,((EffectRef
*) b
)->Name
);
163 int find_effect(const void *a
, const void *b
)
165 return stricmp((const char *) a
,((const EffectRef
*) b
)->Name
);
168 static EffectRef
* FindEffect(const char* effectname
)
170 if( !effectname
|| !effectnames
) {
173 void *tmp
= bsearch(effectname
, effectnames
, effectnames_count
, sizeof(EffectRef
), find_effect
);
175 printMessage( "EffectQueue", "", YELLOW
);
176 printf("Couldn't assign effect: %s\n", effectname
);
178 return (EffectRef
*) tmp
;
181 static EffectRef fx_protection_from_display_string_ref
={"Protection:String",NULL
,-1};
183 //special effects without level check (but with damage dices precalculated)
184 static EffectRef diced_effects
[] = {
187 {"CurrentHPModifier",NULL
,-1},
188 {"MaximumHPModifier",NULL
,-1},
190 {"BurningBlood",NULL
,-1}, //iwd
191 {"ColdDamage",NULL
,-1},
192 {"CrushingDamage",NULL
,-1},
193 {"VampiricTouch",NULL
,-1},
194 {"VitriolicSphere",NULL
,-1},
196 {"TransferHP",NULL
,-1},
199 //special effects without level check (but with damage dices not precalculated)
200 static EffectRef diced_effects2
[] = {
201 {"BurningBlood2",NULL
,-1}, //how/iwd2
202 {"StaticCharge",NULL
,-1}, //how/iwd2
203 {"SoulEater",NULL
,-1}, //how/iwd2
204 {"LichTouch",NULL
,-1}, //how
207 inline static void ResolveEffectRef(EffectRef
&effect_reference
)
209 if( effect_reference
.opcode
==-1) {
210 EffectRef
* ref
= FindEffect(effect_reference
.Name
);
211 if( ref
&& ref
->opcode
>=0) {
212 effect_reference
.opcode
= ref
->opcode
;
215 effect_reference
.opcode
= -2;
219 bool Init_EffectQueue()
226 memset( Opcodes
, 0, sizeof( Opcodes
) );
227 for(i
=0;i
<MAX_EFFECTS
;i
++) {
228 Opcodes
[i
].Strref
=-1;
233 AutoTable
efftextTable("efftext");
235 int eT
= core
->LoadSymbol( "effects" );
237 printMessage( "EffectQueue","A critical scripting file is missing!\n",LIGHT_RED
);
240 Holder
<SymbolMgr
> effectsTable
= core
->GetSymbol( eT
);
242 printMessage( "EffectQueue","A critical scripting file is damaged!\n",LIGHT_RED
);
246 for (i
= 0; i
< MAX_EFFECTS
; i
++) {
247 const char* effectname
= effectsTable
->GetValue( i
);
249 int row
= efftextTable
->GetRowCount();
251 const char* ret
= efftextTable
->GetRowName( row
);
253 if( valid_number( ret
, val
) && (i
== val
) ) {
254 Opcodes
[i
].Strref
= atoi( efftextTable
->QueryField( row
, 1 ) );
259 EffectRef
* poi
= FindEffect( effectname
);
261 Opcodes
[i
].Function
= poi
->Function
;
262 Opcodes
[i
].Name
= poi
->Name
;
263 //reverse linking opcode number
264 //using this unused field
265 if( (poi
->opcode
!=-1) && effectname
[0]!='*') {
266 printf("Clashing Opcodes FN: %d vs. %d, %s\n", i
, poi
->opcode
, effectname
);
271 //printf("-------- FN: %d, %s\n", i, effectname);
273 core
->DelSymbol( eT
);
275 //additional initialisations
276 for (i
=0;diced_effects
[i
].Name
;i
++) {
277 ResolveEffectRef(diced_effects
[i
]);
279 for (i
=0;diced_effects2
[i
].Name
;i
++) {
280 ResolveEffectRef(diced_effects2
[i
]);
286 void EffectQueue_ReleaseMemory()
291 effectnames_count
= 0;
295 void EffectQueue_RegisterOpcodes(int count
, const EffectRef
* opcodes
)
298 effectnames
= (EffectRef
*) malloc( (count
+1) * sizeof( EffectRef
) );
300 effectnames
= (EffectRef
*) realloc( effectnames
, (effectnames_count
+ count
+ 1) * sizeof( EffectRef
) );
303 memcpy( effectnames
+ effectnames_count
, opcodes
, count
* sizeof( EffectRef
));
304 effectnames_count
+= count
;
305 effectnames
[effectnames_count
].Name
= NULL
;
306 //if we merge two effect lists, then we need to sort their effect tables
307 //actually, we might always want to sort this list, so there is no
308 //need to do it manually (sorted table is needed if we use bsearch)
309 qsort(effectnames
, effectnames_count
, sizeof(EffectRef
), compare_effects
);
312 EffectQueue::EffectQueue()
317 EffectQueue::~EffectQueue()
319 std::list
< Effect
* >::iterator f
;
321 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
326 Effect
*EffectQueue::CreateEffect(ieDword opcode
, ieDword param1
, ieDword param2
, ieWord timing
)
328 if( opcode
==0xffffffff) {
331 Effect
*fx
= new Effect();
335 memset(fx
,0,sizeof(Effect
));
336 fx
->Target
= FX_TARGET_SELF
;
338 //probability2 is the low number (by effectqueue 331)
339 fx
->Probability1
= 100;
340 fx
->Parameter1
= param1
;
341 fx
->Parameter2
= param2
;
342 fx
->TimingMode
= timing
;
343 fx
->PosX
= 0xffffffff;
344 fx
->PosY
= 0xffffffff;
348 //return the count of effects with matching parameters
349 //useful for effects where there is no separate stat to see
350 ieDword
EffectQueue::CountEffects(EffectRef
&effect_reference
, ieDword param1
, ieDword param2
, const char *resource
) const
352 ResolveEffectRef(effect_reference
);
353 if( effect_reference
.opcode
<0) {
356 return CountEffects(effect_reference
.opcode
, param1
, param2
, resource
);
359 //Change the location of an existing effect
360 //this is used when some external code needs to adjust the effect's location
361 //used when the gui sets the effect's final target
362 void EffectQueue::ModifyEffectPoint(EffectRef
&effect_reference
, ieDword x
, ieDword y
) const
364 ResolveEffectRef(effect_reference
);
365 if( effect_reference
.opcode
<0) {
368 ModifyEffectPoint(effect_reference
.opcode
, x
, y
);
371 Effect
*EffectQueue::CreateEffect(EffectRef
&effect_reference
, ieDword param1
, ieDword param2
, ieWord timing
)
373 ResolveEffectRef(effect_reference
);
374 if( effect_reference
.opcode
<0) {
377 return CreateEffect(effect_reference
.opcode
, param1
, param2
, timing
);
380 //copies the whole effectqueue (area projectiles use it)
381 EffectQueue
*EffectQueue::CopySelf() const
383 EffectQueue
*effects
;
385 effects
= new EffectQueue();
386 std::list
< Effect
* >::const_iterator fxit
= GetFirstEffect();
389 while( (fx
= GetNextEffect(fxit
))) {
390 effects
->AddEffect(fx
, false);
392 effects
->SetOwner(GetOwner());
396 //create a new effect with most of the characteristics of the old effect
397 //only opcode and parameters are changed
398 //This is used mostly inside effects, when an effect needs to spawn
399 //other effects with the same coordinates, source, duration, etc.
400 Effect
*EffectQueue::CreateEffectCopy(Effect
*oldfx
, ieDword opcode
, ieDword param1
, ieDword param2
)
402 if( opcode
==0xffffffff) {
405 Effect
*fx
= new Effect();
409 memcpy(fx
,oldfx
,sizeof(Effect
) );
411 fx
->Parameter1
=param1
;
412 fx
->Parameter2
=param2
;
416 Effect
*EffectQueue::CreateEffectCopy(Effect
*oldfx
, EffectRef
&effect_reference
, ieDword param1
, ieDword param2
)
418 ResolveEffectRef(effect_reference
);
419 if( effect_reference
.opcode
<0) {
422 return CreateEffectCopy(oldfx
, effect_reference
.opcode
, param1
, param2
);
425 static EffectRef fx_unsummon_creature_ref
={"UnsummonCreature",NULL
,-1};
427 Effect
*EffectQueue::CreateUnsummonEffect(Effect
*fx
)
429 Effect
*newfx
= NULL
;
430 if( (fx
->TimingMode
&0xff) == FX_DURATION_INSTANT_LIMITED
) {
431 newfx
= CreateEffectCopy(fx
, fx_unsummon_creature_ref
, 0, 0);
432 newfx
->TimingMode
= FX_DURATION_DELAY_PERMANENT
;
433 if( newfx
->Resource3
[0]) {
434 strnuprcpy(newfx
->Resource
,newfx
->Resource3
, sizeof(ieResRef
)-1 );
436 strnuprcpy(newfx
->Resource
,"SPGFLSH1", sizeof(ieResRef
)-1 );
438 if( fx
->TimingMode
== FX_DURATION_ABSOLUTE
) {
440 newfx
->Duration
= (newfx
->Duration
-core
->GetGame()->GameTime
)/AI_UPDATE_TIME
;
447 void EffectQueue::AddEffect(Effect
* fx
, bool insert
)
449 Effect
* new_fx
= new Effect
;
450 memcpy( new_fx
, fx
, sizeof( Effect
) );
452 effects
.insert( effects
.begin(), new_fx
);
454 effects
.push_back( new_fx
);
458 //This method can remove an effect described by a pointer to it, or
459 //an exact matching effect
460 bool EffectQueue::RemoveEffect(Effect
* fx
)
462 int invariant_size
= offsetof( Effect
, random_value
);
464 for (std::list
< Effect
* >::iterator f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
467 if( (fx
==fx2
) || !memcmp( fx
, fx2
, invariant_size
)) {
476 //this is where we reapply all effects when loading a saved game
477 //The effects are already in the fxqueue of the target
478 void EffectQueue::ApplyAllEffects(Actor
* target
) const
480 std::list
< Effect
* >::const_iterator f
;
481 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
482 ApplyEffect( target
, *f
, 0 );
486 void EffectQueue::Cleanup()
488 std::list
< Effect
* >::iterator f
;
490 for ( f
= effects
.begin(); f
!= effects
.end(); ) {
491 if( (*f
)->TimingMode
== FX_DURATION_JUST_EXPIRED
) {
500 //Handle the target flag when the effect is applied first
501 int EffectQueue::AddEffect(Effect
* fx
, Scriptable
* self
, Actor
* pretarget
, const Point
&dest
) const
508 Actor
*st
= (self
&& (self
->Type
==ST_ACTOR
)) ?(Actor
*) self
:NULL
;
510 switch (fx
->Target
) {
511 case FX_TARGET_ORIGINAL
:
512 fx
->SetPosition(self
->Pos
);
514 flg
= ApplyEffect( st
, fx
, 1 );
515 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
517 st
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
522 fx
->SetPosition(dest
);
524 flg
= ApplyEffect( st
, fx
, 1 );
525 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
527 st
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
532 case FX_TARGET_ALL_BUT_SELF
:
533 map
=self
->GetCurrentArea();
534 i
= map
->GetActorCount(true);
536 Actor
* actor
= map
->GetActor( i
, true );
537 //don't pick ourselves
541 fx
->SetPosition(actor
->Pos
);
543 flg
= ApplyEffect( actor
, fx
, 1 );
544 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
545 actor
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
551 case FX_TARGET_OWN_SIDE
:
552 if( !st
|| st
->InParty
) {
555 map
= self
->GetCurrentArea();
556 spec
= st
->GetStat(IE_SPECIFIC
);
558 //GetActorCount(false) returns all nonparty critters
559 i
= map
->GetActorCount(false);
561 Actor
* actor
= map
->GetActor( i
, false );
562 if( actor
->GetStat(IE_SPECIFIC
)!=spec
) {
565 fx
->SetPosition(actor
->Pos
);
567 flg
= ApplyEffect( actor
, fx
, 1 );
568 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
569 actor
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
574 case FX_TARGET_OTHER_SIDE
:
575 if( !pretarget
|| pretarget
->InParty
) {
578 map
= self
->GetCurrentArea();
579 spec
= pretarget
->GetStat(IE_SPECIFIC
);
581 //GetActorCount(false) returns all nonparty critters
582 i
= map
->GetActorCount(false);
584 Actor
* actor
= map
->GetActor( i
, false );
585 if( actor
->GetStat(IE_SPECIFIC
)!=spec
) {
588 fx
->SetPosition(actor
->Pos
);
590 flg
= ApplyEffect( actor
, fx
, 1 );
591 //GetActorCount can now return all nonparty critters
592 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
593 actor
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
598 case FX_TARGET_PRESET
:
599 fx
->SetPosition(pretarget
->Pos
);
601 flg
= ApplyEffect( pretarget
, fx
, 1 );
602 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
604 pretarget
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
609 case FX_TARGET_PARTY
:
611 game
= core
->GetGame();
612 i
= game
->GetPartySize(true);
614 Actor
* actor
= game
->GetPC( i
, true );
615 fx
->SetPosition(actor
->Pos
);
617 flg
= ApplyEffect( actor
, fx
, 1 );
618 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
619 actor
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
626 map
= self
->GetCurrentArea();
627 i
= map
->GetActorCount(true);
629 Actor
* actor
= map
->GetActor( i
, true );
630 fx
->SetPosition(actor
->Pos
);
632 flg
= ApplyEffect( actor
, fx
, 1 );
633 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
634 actor
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
640 case FX_TARGET_ALL_BUT_PARTY
:
641 map
= self
->GetCurrentArea();
642 i
= map
->GetActorCount(false);
644 Actor
* actor
= map
->GetActor( i
, false );
645 fx
->SetPosition(actor
->Pos
);
647 flg
= ApplyEffect( actor
, fx
, 1 );
648 //GetActorCount can now return all nonparty critters
649 if( fx
->TimingMode
!= FX_DURATION_JUST_EXPIRED
) {
650 actor
->fxqueue
.AddEffect( fx
, flg
==FX_INSERT
);
656 case FX_TARGET_UNKNOWN
:
658 printf( "Unknown FX target type: %d\n", fx
->Target
);
666 //this is where effects from spells first get in touch with the target
667 //the effects are currently NOT in the target's fxqueue, those that stick
668 //will get copied (hence the fxqueue.AddEffect call)
669 //if this returns FX_NOT_APPLIED, then the whole stack was resisted
671 int EffectQueue::AddAllEffects(Actor
* target
, const Point
&destination
) const
673 int res
= FX_NOT_APPLIED
;
674 // pre-roll dice for fx needing them and stow them in the effect
675 ieDword random_value
= core
->Roll( 1, 100, 0 );
680 std::list
< Effect
* >::const_iterator f
;
681 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
682 //handle resistances and saving throws here
683 (*f
)->random_value
= random_value
;
684 //if applyeffect returns true, we stop adding the future effects
685 //this is to simulate iwd2's on the fly spell resistance
687 int tmp
= AddEffect(*f
, Owner
, target
, destination
);
688 //lets try without Owner, any crash?
689 //If yes, then try to fix the individual effect
690 //If you use target for Owner here, the wand in chateau irenicus will work
691 //the same way as Imoen's monster summoning, which is a BAD THING (TM)
692 //int tmp = AddEffect(*f, Owner?Owner:target, target, destination);
693 if( tmp
== FX_ABORT
) {
694 res
= FX_NOT_APPLIED
;
697 if( tmp
!= FX_NOT_APPLIED
) {
704 //check if an effect has no level based resistance, but instead the dice sizes/count
705 //adjusts Parameter1 (like a damage causing effect)
706 inline static bool IsDicedEffect(int opcode
)
710 for(i
=0;diced_effects
[i
].Name
;i
++) {
711 if( diced_effects
[i
].opcode
==opcode
) {
718 //there is no level based resistance, but Parameter1 cannot be precalculated
719 //these effects use the Dice fields in a special way
720 inline static bool IsDicedEffect2(int opcode
)
724 for(i
=0;diced_effects2
[i
].Name
;i
++) {
725 if( diced_effects2
[i
].opcode
==opcode
) {
732 //resisted effect based on level
733 inline bool check_level(Actor
*target
, Effect
*fx
)
735 //skip non level based effects
736 if( IsDicedEffect((int) fx
->Opcode
)) {
737 fx
->Parameter1
= DICE_ROLL((signed)fx
->Parameter1
);
738 //this is a hack for PST style diced effects
739 if( core
->HasFeature(GF_SAVE_FOR_HALF
) ) {
740 if( memcmp(fx
->Resource
,"NEG",4) ) {
741 fx
->IsSaveForHalfDamage
=1;
744 if( (fx
->Parameter2
&3)==3) {
745 fx
->IsSaveForHalfDamage
=1;
750 if( IsDicedEffect2((int) fx
->Opcode
)) {
757 if(fx
->Target
== FX_TARGET_SELF
) {
761 ieDword level
= (ieDword
) target
->GetXPLevel( true );
762 //return true if resisted
763 //level resistance is checked when DiceSides or DiceThrown
764 //are greater than 0 (sometimes they used -1 for our amusement)
765 //if level>than maximum affected or level<than minimum affected, then the
767 if( (fx
->DiceSides
> 0 || fx
->DiceThrown
> 0) && (level
> fx
->DiceSides
|| level
< fx
->DiceThrown
)) {
773 //roll for the effect probability, there is a high and a low treshold, the d100
774 //roll should hit in the middle
775 inline bool check_probability(Effect
* fx
)
777 //watch for this, probability1 is the high number
778 //probability2 is the low number
779 //random value is 1-100
780 if( fx
->random_value
<=fx
->Probability2
|| fx
->random_value
>fx
->Probability1
) {
787 static EffectRef fx_level_immunity_ref
={"Protection:Spelllevel",NULL
,-1};
788 static EffectRef fx_opcode_immunity_ref
={"Protection:Opcode",NULL
,-1}; //bg2
789 static EffectRef fx_opcode_immunity2_ref
={"Protection:Opcode2",NULL
,-1};//iwd
790 static EffectRef fx_spell_immunity_ref
={"Protection:Spell",NULL
,-1}; //bg2
791 static EffectRef fx_spell_immunity2_ref
={"Protection:Spell2",NULL
,-1};//iwd
792 static EffectRef fx_school_immunity_ref
={"Protection:School",NULL
,-1};
793 static EffectRef fx_secondary_type_immunity_ref
={"Protection:SecondaryType",NULL
,-1};
795 //decrementing immunity effects
796 static EffectRef fx_level_immunity_dec_ref
={"Protection:SpellLevelDec",NULL
,-1};
797 static EffectRef fx_spell_immunity_dec_ref
={"Protection:SpellDec",NULL
,-1};
798 static EffectRef fx_school_immunity_dec_ref
={"Protection:SchoolDec",NULL
,-1};
799 static EffectRef fx_secondary_type_immunity_dec_ref
={"Protection:SecondaryTypeDec",NULL
,-1};
802 static EffectRef fx_level_bounce_ref
={"Bounce:SpellLevel",NULL
,-1};
803 //static EffectRef fx_opcode_bounce_ref={"Bounce:Opcode",NULL,-1};
804 static EffectRef fx_spell_bounce_ref
={"Bounce:Spell",NULL
,-1};
805 static EffectRef fx_school_bounce_ref
={"Bounce:School",NULL
,-1};
806 static EffectRef fx_secondary_type_bounce_ref
={"Bounce:SecondaryType",NULL
,-1};
808 //decrementing bounce effects
809 static EffectRef fx_level_bounce_dec_ref
={"Bounce:SpellLevelDec",NULL
,-1};
810 static EffectRef fx_spell_bounce_dec_ref
={"Bounce:SpellDec",NULL
,-1};
811 static EffectRef fx_school_bounce_dec_ref
={"Bounce:SchoolDec",NULL
,-1};
812 static EffectRef fx_secondary_type_bounce_dec_ref
={"Bounce:SecondaryTypeDec",NULL
,-1};
814 //spelltrap (multiple decrementing immunity)
815 static EffectRef fx_spelltrap
={"SpellTrap", NULL
,-1};
817 //this is for whole spell immunity/bounce
818 inline static void DecreaseEffect(Effect
*efx
)
821 if( (int) efx
->Parameter1
<1) {
822 //don't remove effects directly!!!
823 efx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
827 //lower decreasing immunities/bounces
828 static int check_type(Actor
* actor
, Effect
* fx
)
830 //the protective effect (if any)
833 ieDword bounce
= actor
->GetStat(IE_BOUNCE
);
836 /*opcode immunity is in the per opcode checks
837 if( actor->fxqueue.HasEffectWithParam(fx_opcode_immunity_ref, fx->Opcode) ) {
840 if( actor->fxqueue.HasEffectWithParam(fx_opcode_immunity2_ref, fx->Opcode) ) {
844 //spell level immunity
845 if(fx
->Power
&& actor
->fxqueue
.HasEffectWithParamPair(fx_level_immunity_ref
, fx
->Power
, 0) ) {
849 //source immunity (spell name)
850 //if source is unspecified, don't resist it
852 if( actor
->fxqueue
.HasEffectWithResource(fx_spell_immunity_ref
, fx
->Source
) ) {
855 if( actor
->fxqueue
.HasEffectWithResource(fx_spell_immunity2_ref
, fx
->Source
) ) {
860 //primary type immunity (school)
861 if( fx
->PrimaryType
) {
862 if( actor
->fxqueue
.HasEffectWithParam(fx_school_immunity_ref
, fx
->PrimaryType
)) {
867 //secondary type immunity (usage)
868 if( fx
->SecondaryType
) {
869 if( actor
->fxqueue
.HasEffectWithParam(fx_secondary_type_immunity_ref
, fx
->SecondaryType
) ) {
874 //decrementing immunity checks
875 //decrementing level immunity
876 efx
= actor
->fxqueue
.HasEffectWithParamPair(fx_level_immunity_dec_ref
, fx
->Power
, 0);
882 //decrementing spell immunity
884 efx
= actor
->fxqueue
.HasEffectWithResource(fx_spell_immunity_dec_ref
, fx
->Source
);
890 //decrementing primary type immunity (school)
891 if( fx
->PrimaryType
) {
892 efx
= actor
->fxqueue
.HasEffectWithParam(fx_school_immunity_dec_ref
, fx
->PrimaryType
);
899 //decrementing secondary type immunity (usage)
900 if( fx
->SecondaryType
) {
901 efx
= actor
->fxqueue
.HasEffectWithParam(fx_secondary_type_immunity_dec_ref
, fx
->SecondaryType
);
910 //if the spelltrap effect already absorbed enough levels
911 //but still didn't get removed, it will absorb levels it shouldn't
912 //it will also absorb multiple spells in a single round
913 efx
=actor
->fxqueue
.HasEffectWithParamPair(fx_spelltrap
, 0, fx
->Power
);
915 //storing the absorbed spell level
916 efx
->Parameter3
+=fx
->Power
;
917 //instead of a single effect, they had to create an effect for each level
919 //if decrease needs the spell level, use fx->Power here
920 actor
->fxqueue
.DecreaseParam1OfEffect(fx_spelltrap
, 1);
926 if( (bounce
&BNC_LEVEL
) && actor
->fxqueue
.HasEffectWithParamPair(fx_level_bounce_ref
, fx
->Power
, 0) ) {
930 if( fx
->Source
[0] && (bounce
&BNC_RESOURCE
) && actor
->fxqueue
.HasEffectWithResource(fx_spell_bounce_ref
, fx
->Source
) ) {
934 if( fx
->PrimaryType
&& (bounce
&BNC_SCHOOL
) ) {
935 if( actor
->fxqueue
.HasEffectWithParam(fx_school_bounce_ref
, fx
->PrimaryType
)) {
940 if( fx
->SecondaryType
&& (bounce
&BNC_SECTYPE
) ) {
941 if( actor
->fxqueue
.HasEffectWithParam(fx_secondary_type_bounce_ref
, fx
->SecondaryType
)) {
945 //decrementing bounce checks
947 //level decrementing bounce check
948 if( (bounce
&BNC_LEVEL_DEC
)) {
949 efx
=actor
->fxqueue
.HasEffectWithParamPair(fx_level_bounce_dec_ref
, fx
->Power
, 0);
956 if( fx
->Source
[0] && (bounce
&BNC_RESOURCE_DEC
)) {
957 efx
=actor
->fxqueue
.HasEffectWithResource(fx_spell_bounce_dec_ref
, fx
->Resource
);
964 if( fx
->PrimaryType
&& (bounce
&BNC_SCHOOL_DEC
) ) {
965 efx
=actor
->fxqueue
.HasEffectWithParam(fx_school_bounce_dec_ref
, fx
->PrimaryType
);
972 if( fx
->SecondaryType
&& (bounce
&BNC_SECTYPE_DEC
) ) {
973 efx
=actor
->fxqueue
.HasEffectWithParam(fx_secondary_type_bounce_dec_ref
, fx
->SecondaryType
);
983 //check resistances, saving throws
984 static bool check_resistance(Actor
* actor
, Effect
* fx
)
991 if( actor
->fxqueue
.HasEffectWithParam(fx_opcode_immunity_ref
, fx
->Opcode
) ) {
992 printf ("immune to effect: %s\n", (char*) Opcodes
[fx
->Opcode
].Name
);
995 if( actor
->fxqueue
.HasEffectWithParam(fx_opcode_immunity2_ref
, fx
->Opcode
) ) {
996 printf ("immune2 to effect: %s\n", (char*) Opcodes
[fx
->Opcode
].Name
);
1000 /* opcode bouncing isn't implemented?
1002 if( actor->fxqueue.HasEffectWithParam(fx_opcode_bounce_ref, fx->Opcode) ) {
1007 //not resistable (no saves either?)
1008 if( fx
->Resistance
!= FX_CAN_RESIST_CAN_DISPEL
) {
1013 if (fx
->Target
==FX_TARGET_SELF
) {
1014 if (core
->HasFeature(GF_SELECTIVE_MAGIC_RES
) ) {
1020 ieDword val
= actor
->GetStat(IE_RESISTMAGIC
);
1021 if( fx
->random_value
< val
) {
1022 printf ("effect resisted: %s\n", (char*) Opcodes
[fx
->Opcode
].Name
);
1028 for (int i
=0;i
<5;i
++) {
1029 if( fx
->SavingThrowType
&(1<<i
)) {
1030 saved
= actor
->GetSavingThrow(i
, fx
->SavingThrowBonus
);
1037 if( fx
->IsSaveForHalfDamage
) {
1040 printf ("%s saved against effect: %s\n", actor
->GetName(1), (char*) Opcodes
[fx
->Opcode
].Name
);
1047 // this function is called two different ways
1048 // when FirstApply is set, then the effect isn't stuck on the target
1049 // this happens when a new effect comes in contact with the target.
1050 // if the effect returns FX_DURATION_JUST_EXPIRED then it won't stick
1051 // when first_apply is unset, the effect is already on the target
1052 // this happens on load time too!
1053 // returns FX_NOT_APPLIED if the process shouldn't be calling applyeffect anymore
1054 // returns FX_ABORT if the whole spell this effect is in should be aborted
1055 // it will disable all future effects of same source (only on first apply)
1057 int EffectQueue::ApplyEffect(Actor
* target
, Effect
* fx
, ieDword first_apply
) const
1059 //printf( "FX 0x%02x: %s(%d, %d)\n", fx->Opcode, effectnames[fx->Opcode].Name, fx->Parameter1, fx->Parameter2 );
1060 if( fx
->Opcode
>= MAX_EFFECTS
) {
1061 fx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1062 return FX_NOT_APPLIED
;
1065 ieDword GameTime
= core
->GetGame()->GameTime
;
1067 fx
->FirstApply
=first_apply
;
1069 if( (fx
->PosX
==0xffffffff) && (fx
->PosY
==0xffffffff)) {
1070 fx
->PosX
= target
->Pos
.x
;
1071 fx
->PosY
= target
->Pos
.y
;
1074 //gemrb specific, stat based chance
1075 if ((fx
->Probability2
== 100) && Owner
&& (Owner
->Type
==ST_ACTOR
) ) {
1076 fx
->Probability2
= 0;
1077 fx
->Probability1
= ((Actor
*) Owner
)->GetSafeStat(fx
->Probability1
);
1080 //the effect didn't pass the probability check
1081 if( !check_probability(fx
) ) {
1082 fx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1083 return FX_NOT_APPLIED
;
1086 //the effect didn't pass the target level check
1087 if( check_level(target
, fx
) ) {
1088 fx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1089 return FX_NOT_APPLIED
;
1092 //the effect didn't pass the resistance check
1093 if( check_resistance(target
, fx
) ) {
1094 fx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1095 return FX_NOT_APPLIED
;
1098 //Same as in items and spells
1099 if (fx
->SourceFlags
& SF_HOSTILE
) {
1100 if (target
&& (target
!= Owner
) && Owner
&& (Owner
->Type
==ST_ACTOR
) ) {
1101 target
->AttackedBy((Actor
*) Owner
);
1105 if( NeedPrepare(fx
->TimingMode
) ) {
1106 //save delay for later
1107 fx
->SecondaryDelay
= fx
->Duration
;
1108 if( fx
->TimingMode
== FX_DURATION_INSTANT_LIMITED
) {
1109 fx
->TimingMode
= FX_DURATION_ABSOLUTE
;
1111 PrepareDuration(fx
);
1114 //check if the effect has triggered or expired
1115 switch (DelayType(fx
->TimingMode
&0xff) ) {
1117 if( fx
->Duration
>GameTime
) {
1118 return FX_NOT_APPLIED
;
1121 //delayed duration (3)
1122 if( NeedPrepare(fx
->TimingMode
) ) {
1123 //prepare for delayed duration effects
1124 fx
->Duration
= fx
->SecondaryDelay
;
1125 PrepareDuration(fx
);
1127 fx
->TimingMode
=TriggeredEffect(fx
->TimingMode
);
1130 if( fx
->Duration
<=GameTime
) {
1131 fx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1132 //add a return here, if 0 duration effects shouldn't work
1135 //permanent effect (so there is no warning)
1138 //this shouldn't happen
1140 printf("Unknown delay type: %d (from %d)\n", DelayType(fx
->TimingMode
&0xff), fx
->TimingMode
);
1144 EffectFunction fn
= 0;
1145 if( fx
->Opcode
<MAX_EFFECTS
) {
1146 fn
= Opcodes
[fx
->Opcode
].Function
;
1150 if( target
&& first_apply
) {
1151 if( !target
->fxqueue
.HasEffectWithParamPair(fx_protection_from_display_string_ref
, fx
->Parameter1
, 0) ) {
1152 displaymsg
->DisplayStringName( Opcodes
[fx
->Opcode
].Strref
, 0xf0f0f0,
1153 target
, IE_STR_SOUND
);
1157 res
=fn( Owner
, target
, fx
);
1159 //if there is no owner, we assume it is the target
1162 //normal effect with duration
1164 case FX_NOT_APPLIED
:
1165 //instant effect, pending removal
1166 //for example, a damage effect
1167 fx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1170 //put this effect in the beginning of the queue
1171 //all known insert effects are 'permanent' too
1172 //that is the AC effect only
1173 //actually, permanent effects seem to be
1174 //inserted by the game engine too
1176 //don't stick around if it was executed permanently
1177 //for example, a permanent strength modifier effect
1178 if( (fx
->TimingMode
== FX_DURATION_INSTANT_PERMANENT
) ) {
1179 fx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1188 //effect not found, it is going to be discarded
1189 fx
->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1194 // looks for opcode with param2
1196 #define MATCH_OPCODE() if((*f)->Opcode!=opcode) { continue; }
1198 // useful for: remove equipped item
1199 #define MATCH_SLOTCODE() if((*f)->InventorySlot!=slotcode) { continue; }
1201 // useful for: remove projectile type
1202 #define MATCH_PROJECTILE() if((*f)->Projectile!=projectile) { continue; }
1204 static const bool fx_live
[MAX_TIMING_MODE
]={true,true,true,false,false,false,false,false,true,true,false};
1205 inline bool IsLive(ieByte timingmode
)
1207 if( timingmode
>=MAX_TIMING_MODE
) return false;
1208 return fx_live
[timingmode
];
1211 #define MATCH_LIVE_FX() if(!IsLive((*f)->TimingMode)) { continue; }
1212 #define MATCH_PARAM1() if((*f)->Parameter1!=param1) { continue; }
1213 #define MATCH_PARAM2() if((*f)->Parameter2!=param2) { continue; }
1214 #define MATCH_RESOURCE() if( strnicmp( (*f)->Resource, resource, 8) ) { continue; }
1215 #define MATCH_SOURCE() if( strnicmp( (*f)->Source, Removed, 8) ) { continue; }
1216 #define MATCH_TIMING() if( (*f)->TimingMode!=timing) { continue; }
1218 //call this from an applied effect, after it returns, these effects
1219 //will be killed along with it
1220 void EffectQueue::RemoveAllEffects(ieDword opcode
) const
1222 std::list
< Effect
* >::const_iterator f
;
1223 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1227 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1231 //removes all equipping effects that match slotcode
1232 void EffectQueue::RemoveEquippingEffects(ieDwordSigned slotcode
) const
1234 std::list
< Effect
* >::const_iterator f
;
1235 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1236 if( !IsEquipped((*f
)->TimingMode
)) continue;
1239 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1243 //removes all effects that match projectile
1244 void EffectQueue::RemoveAllEffectsWithProjectile(ieDword projectile
) const
1246 std::list
< Effect
* >::const_iterator f
;
1247 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1250 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1254 //remove effects belonging to a given spell
1255 void EffectQueue::RemoveAllEffects(const ieResRef Removed
) const
1257 std::list
< Effect
* >::const_iterator f
;
1258 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1262 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1266 //remove effects belonging to a given spell, but only if they match timing method x
1267 void EffectQueue::RemoveAllEffects(const ieResRef Removed
, ieByte timing
) const
1269 std::list
< Effect
* >::const_iterator f
;
1270 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1274 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1278 //this will modify effect reference
1279 void EffectQueue::RemoveAllEffects(EffectRef
&effect_reference
) const
1281 ResolveEffectRef(effect_reference
);
1282 if( effect_reference
.opcode
<0) {
1285 RemoveAllEffects(effect_reference
.opcode
);
1288 //Removes all effects with a matching resource field
1289 void EffectQueue::RemoveAllEffectsWithResource(ieDword opcode
, const ieResRef resource
) const
1291 std::list
< Effect
* >::const_iterator f
;
1292 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1297 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1301 void EffectQueue::RemoveAllEffectsWithResource(EffectRef
&effect_reference
, const ieResRef resource
) const
1303 ResolveEffectRef(effect_reference
);
1304 RemoveAllEffectsWithResource(effect_reference
.opcode
, resource
);
1307 //This method could be used to remove stat modifiers that would lower a stat
1308 //(works only if a higher stat means good for the target)
1309 void EffectQueue::RemoveAllDetrimentalEffects(ieDword opcode
, ieDword current
) const
1311 std::list
< Effect
* >::const_iterator f
;
1312 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1315 switch((*f
)->Parameter2
) {
1317 if( ((signed) (*f
)->Parameter1
)>=0) continue;
1320 if( ((signed) (*f
)->Parameter1
)>=(signed) current
) continue;
1323 if( ((signed) (*f
)->Parameter1
)>=100) continue;
1328 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1332 //Removes all effects with a matching param2
1333 //param2 is usually an effect's subclass (quality) while param1 is more like quantity.
1334 //So opcode+param2 usually pinpoints an effect better when not all effects of a given
1335 //opcode need to be removed (see removal of portrait icon)
1336 void EffectQueue::RemoveAllEffectsWithParam(ieDword opcode
, ieDword param2
) const
1338 std::list
< Effect
* >::const_iterator f
;
1339 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1344 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1348 //this function is called by FakeEffectExpiryCheck
1349 //probably also called by rest
1350 void EffectQueue::RemoveExpiredEffects(ieDword futuretime
) const
1352 ieDword GameTime
= core
->GetGame()->GameTime
;
1353 if( GameTime
+futuretime
*AI_UPDATE_TIME
<GameTime
) {
1354 GameTime
=0xffffffff;
1356 GameTime
+=futuretime
*AI_UPDATE_TIME
;
1359 std::list
< Effect
* >::const_iterator f
;
1360 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1361 //FIXME: how this method handles delayed effects???
1362 //it should remove them as well, i think
1363 if( DelayType( ((*f
)->TimingMode
) )!=PERMANENT
) {
1364 if( (*f
)->Duration
<=GameTime
) {
1365 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1371 //this effect will expire all effects that are not truly permanent
1372 //which i call permanent after death (iesdp calls it permanent after bonuses)
1373 void EffectQueue::RemoveAllNonPermanentEffects() const
1375 std::list
< Effect
* >::const_iterator f
;
1376 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1377 if( IsRemovable((*f
)->TimingMode
) ) {
1378 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1383 //this will modify effect reference
1385 void EffectQueue::RemoveAllDetrimentalEffects(EffectRef
&effect_reference
, ieDword current
) const
1387 ResolveEffectRef(effect_reference
);
1388 RemoveAllDetrimentalEffects(effect_reference
.opcode
, current
);
1391 void EffectQueue::RemoveAllEffectsWithParam(EffectRef
&effect_reference
, ieDword param2
) const
1393 ResolveEffectRef(effect_reference
);
1394 RemoveAllEffectsWithParam(effect_reference
.opcode
, param2
);
1397 //remove certain levels of effects, possibly matching school/secondary type
1398 //this method removes whole spells (tied together by their source)
1399 //FIXME: probably this isn't perfect
1400 void EffectQueue::RemoveLevelEffects(ieResRef
&Removed
, ieDword level
, ieDword Flags
, ieDword match
) const
1403 std::list
< Effect
* >::const_iterator f
;
1404 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1405 if( (*f
)->Power
>level
) {
1412 if( Flags
&RL_MATCHSCHOOL
) {
1413 if( (*f
)->PrimaryType
!=match
) {
1417 if( Flags
&RL_MATCHSECTYPE
) {
1418 if( (*f
)->SecondaryType
!=match
) {
1422 //if dispellable was not set, or the effect is dispellable
1424 if( Flags
&RL_DISPELLABLE
) {
1425 if( !((*f
)->Resistance
&FX_CAN_DISPEL
)) {
1429 (*f
)->TimingMode
= FX_DURATION_JUST_EXPIRED
;
1430 if( Flags
&RL_REMOVEFIRST
) {
1431 memcpy(Removed
,(*f
)->Source
, sizeof(Removed
));
1436 Effect
*EffectQueue::HasOpcode(ieDword opcode
) const
1438 std::list
< Effect
* >::const_iterator f
;
1439 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1448 Effect
*EffectQueue::HasEffect(EffectRef
&effect_reference
) const
1450 ResolveEffectRef(effect_reference
);
1451 if( effect_reference
.opcode
<0) {
1454 return HasOpcode(effect_reference
.opcode
);
1457 Effect
*EffectQueue::HasOpcodeWithParam(ieDword opcode
, ieDword param2
) const
1459 std::list
< Effect
* >::const_iterator f
;
1460 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1470 Effect
*EffectQueue::HasEffectWithParam(EffectRef
&effect_reference
, ieDword param2
) const
1472 ResolveEffectRef(effect_reference
);
1473 if( effect_reference
.opcode
<0) {
1476 return HasOpcodeWithParam(effect_reference
.opcode
, param2
);
1479 //looks for opcode with pairs of parameters (useful for protection against creature, extra damage or extra thac0 against creature)
1480 //generally an IDS targeting
1482 Effect
*EffectQueue::HasOpcodeWithParamPair(ieDword opcode
, ieDword param1
, ieDword param2
) const
1484 std::list
< Effect
* >::const_iterator f
;
1485 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1489 //0 is always accepted as first parameter
1499 Effect
*EffectQueue::HasEffectWithParamPair(EffectRef
&effect_reference
, ieDword param1
, ieDword param2
) const
1501 ResolveEffectRef(effect_reference
);
1502 if( effect_reference
.opcode
<0) {
1505 return HasOpcodeWithParamPair(effect_reference
.opcode
, param1
, param2
);
1508 // sums all the values of the specific damage bonus effects of the passed "damage type"
1509 int EffectQueue::SpecificDamageBonus(ieDword opcode
, ieDword param2
) const
1512 std::list
< Effect
* >::const_iterator f
;
1513 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1517 bonus
+= (signed) (*f
)->Parameter1
;
1522 static EffectRef fx_damage_bonus_modifier_ref
={"DamageBonusModifier",NULL
,-1};
1523 int EffectQueue::SpecificDamageBonus(ieDword damage_type
) const
1525 ResolveEffectRef(fx_damage_bonus_modifier_ref
);
1526 if(fx_damage_bonus_modifier_ref
.opcode
< 0) {
1529 return SpecificDamageBonus(fx_damage_bonus_modifier_ref
.opcode
, damage_type
);
1532 //this could be used for stoneskins and mirror images as well
1533 void EffectQueue::DecreaseParam1OfEffect(ieDword opcode
, ieDword amount
) const
1535 std::list
< Effect
* >::const_iterator f
;
1536 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1539 ieDword value
= (*f
)->Parameter1
;
1540 if( value
>amount
) value
-=amount
;
1542 (*f
)->Parameter1
=value
;
1546 void EffectQueue::DecreaseParam1OfEffect(EffectRef
&effect_reference
, ieDword amount
) const
1548 ResolveEffectRef(effect_reference
);
1549 if( effect_reference
.opcode
<0) {
1552 DecreaseParam1OfEffect(effect_reference
.opcode
, amount
);
1556 //this function does IDS targeting for effects (extra damage/thac0 against creature)
1557 static const int ids_stats
[7]={IE_EA
, IE_GENERAL
, IE_RACE
, IE_CLASS
, IE_SPECIFIC
, IE_SEX
, IE_ALIGNMENT
};
1559 int EffectQueue::BonusAgainstCreature(ieDword opcode
, Actor
*actor
) const
1562 std::list
< Effect
* >::const_iterator f
;
1563 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1566 if( (*f
)->Parameter1
) {
1567 ieDword ids
= (*f
)->Parameter2
;
1568 if( ids
<2 || ids
>8) {
1571 ieDword param1
= actor
->GetStat(ids_stats
[ids
-2]);
1574 int val
= (int) (*f
)->Parameter3
;
1581 int EffectQueue::BonusAgainstCreature(EffectRef
&effect_reference
, Actor
*actor
) const
1583 ResolveEffectRef(effect_reference
);
1584 if( effect_reference
.opcode
<0) {
1587 return BonusAgainstCreature(effect_reference
.opcode
, actor
);
1590 bool EffectQueue::WeaponImmunity(ieDword opcode
, int enchantment
, ieDword weapontype
) const
1592 std::list
< Effect
* >::const_iterator f
;
1593 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1597 int magic
= (int) (*f
)->Parameter1
;
1598 ieDword mask
= (*f
)->Parameter3
;
1599 ieDword value
= (*f
)->Parameter4
;
1601 if( enchantment
) continue;
1602 } else if( magic
>0) {
1603 if( enchantment
>magic
) continue;
1606 if( (weapontype
&mask
) != value
) {
1614 static EffectRef fx_weapon_immunity_ref
={"Protection:Weapons",NULL
,-1};
1616 bool EffectQueue::WeaponImmunity(int enchantment
, ieDword weapontype
) const
1618 ResolveEffectRef(fx_weapon_immunity_ref
);
1619 if( fx_weapon_immunity_ref
.opcode
<0) {
1622 return WeaponImmunity(fx_weapon_immunity_ref
.opcode
, enchantment
, weapontype
);
1625 static EffectRef fx_disable_spellcasting_ref
={ "DisableCasting", NULL
, -1 };
1626 int EffectQueue::DisabledSpellcasting(int types
) const
1628 ResolveEffectRef(fx_disable_spellcasting_ref
);
1629 if( fx_disable_spellcasting_ref
.opcode
< 0) {
1633 unsigned int spelltype_mask
= 0;
1634 bool iwd2
= !!core
->HasFeature(GF_ENHANCED_EFFECTS
);
1635 ieDword opcode
= fx_disable_spellcasting_ref
.opcode
;
1636 std::list
< Effect
* >::const_iterator f
;
1637 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1642 switch((*f
)->Parameter2
) {
1644 spelltype_mask
|= 7;
1646 case 1: // mage and cleric
1647 spelltype_mask
|= 3;
1650 spelltype_mask
|= 2;
1653 spelltype_mask
|= 1;
1656 spelltype_mask
|= 4;
1660 switch((*f
)->Parameter2
) {
1662 spelltype_mask
|= 2;
1665 spelltype_mask
|= 1;
1668 spelltype_mask
|= 4;
1673 return spelltype_mask
& types
;
1676 //useful for immunity vs spell, can't use item, etc.
1677 Effect
*EffectQueue::HasOpcodeWithResource(ieDword opcode
, const ieResRef resource
) const
1679 std::list
< Effect
* >::const_iterator f
;
1680 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1690 Effect
*EffectQueue::HasEffectWithResource(EffectRef
&effect_reference
, const ieResRef resource
) const
1692 ResolveEffectRef(effect_reference
);
1693 return HasOpcodeWithResource(effect_reference
.opcode
, resource
);
1696 //used in contingency/sequencer code (cannot have the same contingency twice)
1697 Effect
*EffectQueue::HasOpcodeWithSource(ieDword opcode
, const ieResRef Removed
) const
1699 std::list
< Effect
* >::const_iterator f
;
1700 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1710 Effect
*EffectQueue::HasEffectWithSource(EffectRef
&effect_reference
, const ieResRef resource
) const
1712 ResolveEffectRef(effect_reference
);
1713 return HasOpcodeWithSource(effect_reference
.opcode
, resource
);
1716 bool EffectQueue::HasAnyDispellableEffect() const
1718 std::list
< Effect
* >::const_iterator f
;
1719 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1720 if( (*f
)->Resistance
&FX_CAN_DISPEL
) {
1727 void EffectQueue::dump() const
1729 printf( "EFFECT QUEUE:\n" );
1731 std::list
< Effect
* >::const_iterator f
;
1732 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1736 if( fx
->Opcode
< MAX_EFFECTS
)
1737 Name
= (char*) Opcodes
[fx
->Opcode
].Name
;
1739 printf( " %2d: 0x%02x: %s (%d, %d) S:%s\n", i
++, fx
->Opcode
, Name
, fx
->Parameter1
, fx
->Parameter2
, fx
->Source
);
1744 Effect *EffectQueue::GetEffect(ieDword idx) const
1746 if( effects.size()<=idx) {
1749 return effects[idx];
1753 //returns true if the effect supports simplified duration
1754 bool EffectQueue::HasDuration(Effect
*fx
)
1756 switch(fx
->TimingMode
) {
1757 case FX_DURATION_INSTANT_LIMITED
: //simple duration
1758 case FX_DURATION_DELAY_LIMITED
: //delayed duration
1759 case FX_DURATION_DELAY_PERMANENT
: //simple delayed
1765 static EffectRef fx_variable_ref
={"Variable:StoreLocalVariable",NULL
,-1};
1767 //returns true if the effect must be saved
1768 //variables are saved differently
1769 bool EffectQueue::Persistent(Effect
* fx
)
1771 //we save this as variable
1772 if( fx
->Opcode
==(ieDword
) ResolveEffect(fx_variable_ref
)) {
1776 switch (fx
->TimingMode
) {
1777 //normal equipping fx of items
1778 case FX_DURATION_INSTANT_WHILE_EQUIPPED
:
1779 //delayed effect not saved
1780 case FX_DURATION_DELAY_UNSAVED
:
1781 //permanent effect not saved
1782 case FX_DURATION_PERMANENT_UNSAVED
:
1783 //just expired effect
1784 case FX_DURATION_JUST_EXPIRED
:
1790 //alter the color effect in case the item is equipped in the shield slot
1791 void EffectQueue::HackColorEffects(Actor
*Owner
, Effect
*fx
)
1793 if( fx
->InventorySlot
!=Owner
->inventory
.GetShieldSlot()) return;
1795 unsigned int gradienttype
= fx
->Parameter2
& 0xF0;
1796 if( gradienttype
== 0x10) {
1797 gradienttype
= 0x20; // off-hand
1798 fx
->Parameter2
&= ~0xF0;
1799 fx
->Parameter2
|= gradienttype
;
1803 //iterate through saved effects
1804 const Effect
*EffectQueue::GetNextSavedEffect(std::list
< Effect
* >::const_iterator
&f
) const
1806 while(f
!=effects
.end()) {
1807 Effect
*effect
= *f
;
1809 if( Persistent(effect
)) {
1816 Effect
*EffectQueue::GetNextEffect(std::list
< Effect
* >::const_iterator
&f
) const
1818 if( f
!=effects
.end()) return *f
++;
1822 ieDword
EffectQueue::CountEffects(ieDword opcode
, ieDword param1
, ieDword param2
, const char *resource
) const
1826 std::list
< Effect
* >::const_iterator f
;
1828 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1830 if( param1
!=0xffffffff)
1832 if( param2
!=0xffffffff)
1842 void EffectQueue::ModifyEffectPoint(ieDword opcode
, ieDword x
, ieDword y
) const
1844 std::list
< Effect
* >::const_iterator f
;
1846 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1855 //count effects that get saved
1856 ieDword
EffectQueue::GetSavedEffectsCount() const
1860 std::list
< Effect
* >::const_iterator f
;
1862 for ( f
= effects
.begin(); f
!= effects
.end(); f
++ ) {
1870 void EffectQueue::TransformToDelay(ieByte
&TimingMode
)
1872 if( TimingMode
<MAX_TIMING_MODE
) {;
1873 TimingMode
= fx_to_delayed
[TimingMode
];
1875 TimingMode
= FX_DURATION_JUST_EXPIRED
;
1879 int EffectQueue::ResolveEffect(EffectRef
&effect_reference
)
1881 ResolveEffectRef(effect_reference
);
1882 return effect_reference
.opcode
;
1885 // this check goes for the whole effect block, not individual effects
1886 // But it takes the first effect of the block for the common fields
1888 //returns 1 if effect block applicable
1889 //returns 0 if effect block disabled
1890 //returns -1 if effect block bounced
1891 int EffectQueue::CheckImmunity(Actor
*target
) const
1893 //don't resist if target is non living
1898 if( effects
.size() ) {
1899 Effect
* fx
= *effects
.begin();
1901 //projectile immunity
1902 if( target
->ImmuneToProjectile(fx
->Projectile
)) return 0;
1904 //don't resist item projectile payloads based on spell school, bounce, etc.
1905 if( fx
->InventorySlot
) {
1909 //check level resistances
1910 //check specific spell immunity
1911 //check school/sectype immunity
1912 return check_type(target
, fx
);
1917 void EffectQueue::AffectAllInRange(Map
*map
, const Point
&pos
, int idstype
, int idsvalue
,
1918 unsigned int range
, Actor
*except
)
1920 int cnt
= map
->GetActorCount(true);
1922 Actor
*actor
= map
->GetActor(cnt
,true);
1923 if( except
==actor
) {
1927 if( Distance(pos
, actor
)>range
) {
1931 if( !match_ids(actor
, idstype
, idsvalue
)) {
1935 if( !map
->IsVisible(actor
->Pos
, pos
)) {
1938 AddAllEffects(actor
, actor
->Pos
);