Help me decipher this snippet of code
Posted: Fri Apr 29, 2011 2:02 pm
Trying to update some old, poorly commented code from (believe it or not) 093 to the newest version. Having trouble understanding what exactly this function is doing:
-attack_type is an integer that gets set by the DealDamage function in a hitscript, defaulting to 0x0100 to represent physical damage. More on this below.
-PROPID_MOBILE_ATTACK_TYPE_IMMUNITIES is an integer stored in a CProp on the mobile. That CProp is either i256 or i639. No other values appear in any npc definitions.
At the top of the function there exist some declarations:
At first it seemed to make sense because on most mobiles with that cprop, its value is i256 which in hex is of course 0x100 which seems to correspond to a monster being immune to physical damage. However i639 in hex is 0x27f which seems like an oddball value and certainly doesn't appear in the above chart.
These constants are also the values which the variable attack_type can take.
Code: Select all
function IsImmunedFromThisDamageType( who, byref dmg, attack_type )
var immunities := GetObjProperty( who, PROPID_MOBILE_ATTACK_TYPE_IMMUNITIES );
if( !immunities )
return 0;
endif
if( attack_type & DMGID_NO_RESIST )
return 0;
endif
var attack_amount := 0;
var is_immuned_amount := 0;
foreach bit in { DMGID_FIRE, DMGID_AIR, DMGID_EARTH, DMGID_WATER, DMGID_NECRO, DMGID_HOLY, DMGID_POISON, DMGID_ACID, DMGID_PHYSICAL, DMGID_MAGIC, DMGID_ASTRAL }
if( attack_type & bit )
attack_amount := attack_amount + 1;
if( immunities & bit )
is_immuned_amount := is_immuned_amount + 1;
endif
endif
endforeach
if( !attack_amount || attack_amount = is_immuned_amount )
return 1;
else
dmg := Cint( dmg * Cint(attack_amount-is_immuned_amount) / attack_amount );
return 0;
endif
endfunction-PROPID_MOBILE_ATTACK_TYPE_IMMUNITIES is an integer stored in a CProp on the mobile. That CProp is either i256 or i639. No other values appear in any npc definitions.
At the top of the function there exist some declarations:
Code: Select all
const DMGID_FIRE := 0x0001;
const DMGID_AIR := 0x0002;
const DMGID_EARTH := 0x0004;
const DMGID_WATER := 0x0008;
const DMGID_NECRO := 0x0010;
const DMGID_HOLY := 0x0020;
const DMGID_POISON := 0x0040;
const DMGID_ACID := 0x0080;
const DMGID_PHYSICAL := 0x0100;
const DMGID_MAGIC := 0x0200;
const DMGID_ASTRAL := 0x0400;
const DMGID_NO_RESIST := 0x0800;These constants are also the values which the variable attack_type can take.