| 09-03-2009, 07:46 PM | #1 |
Well, not exactly. What I am trying to do is detect when a certain unit type (Footman to be exact) gets damaged and see if the damage comes from in front of the Footman so I can block it. I'm doing that by comparing the angle between the damager and damaged unit and checking if the footman is facing that angle. If he is, then heal him for a maximum of 7. The problem is that it will block damage only if the footman is facing north and he gets damaged in front. If I turn him south and attack him directly in the face, it does nothing. I'm pretty sure it is because of my math but I'm really bad at it and can't really fix it... JASS:scope DefensiveStance initializer Init globals private constant integer UID_FOOTMAN = 'h006' private constant real ANGLE = (90. * 0.5) // it is actually 90 but since it's spread in 2 directions, it has to be halved endglobals private function OnDamage takes nothing returns boolean local unit cast = GetEventDamageSource() local unit targ = GetTriggerUnit() local real ang local real face local real damage = GetEventDamage() local real life call BJDebugMsg("hello?..") if GetUnitTypeId(targ) == UID_FOOTMAN then call BJDebugMsg("passed footman check..") set ang = Atan2(GetWidgetY(cast) - GetWidgetY(targ), GetWidgetX(cast) - GetWidgetX(targ)) * bj_RADTODEG set face = GetUnitFacing(targ) if (ang > face - ANGLE and ang < face + ANGLE) or (ang < face + ANGLE and ang > face - ANGLE) then // check if damage can be blocked if damaged in front if damage > 7. then // can reduce a maximum of 7 set damage = 7. endif set life = GetWidgetLife(targ) call BJDebugMsg(R2S(life)) call BJDebugMsg(R2S(life + damage)) call SetWidgetLife(targ, GetWidgetLife(targ) + damage + damage) endif endif set cast = null set targ = null return false endfunction private function Init takes nothing returns nothing call AddOnDamageFunc(Condition(function OnDamage)) endfunction endscope |
| 09-03-2009, 08:00 PM | #2 |
JASS://! textmacro Modulo takes VAR, PA, PB set $VAR$ = $PA$ - I2S(R2I($PA$/$PB$)) * $PB$ if $VAR$ < 0 then set $VAR$ = $VAR$ + $PB$ endif //! endtextmacro function GetAngleDifference takes real a1, real a2 returns real local real x //! runtextmacro Modulo("a1","a1","bj_PI*2") //! runtextmacro Modulo("a2","a2","bj_PI*2") if a1>a2 then set x=a1 set a1=a2 set a2=x endif set x=a2-bj_PI*2 if a2-a1 > a1-x then set a2=x endif set x=a1-a2 if (x<0) then return -x endif return x endfunction You would do if GetAngleDifference(face,ang) <= ANGLE then Uses radians ofc |
| 09-03-2009, 08:19 PM | #3 |
Thanks, but this thing can't actually compile when it's using string conversion ... Says "bad types for binary operator" |
| 09-03-2009, 08:22 PM | #4 |
JASS://! textmacro Modulo takes VAR, PA, PB set $VAR$ = ($PA$) - I2R(R2I(($PA$)/($PB$))) * ($PB$) if $VAR$ < 0 then set $VAR$ = ($VAR$) + ($PB$) endif //! endtextmacro function GetAngleDifference takes real a1, real a2 returns real local real x //! runtextmacro Modulo("a1","a1","bj_PI*2") //! runtextmacro Modulo("a2","a2","bj_PI*2") if a1>a2 then set x=a1 set a1=a2 set a2=x endif set x=a2-bj_PI*2 if a2-a1 > a1-x then set a2=x endif set x=a1-a2 if (x<0) then return -x endif return x endfunction |
| 09-03-2009, 08:29 PM | #6 |
Thanks, both. Got it working. |
| 09-04-2009, 05:00 AM | #7 |
dude.. silvenon.. you deserve a +rep for the pics |
