- Joined
- Sep 14, 2012
- Messages
- 3,413
Hi everyone,
JASS:
library GetUnitArmor initializer init
/****************************************************************************************************************
* ~How to configure
* - Copy the ability
*
* ~How to use
* - call GetUnitArmor(unit)
****************************************************************************************************************/
globals
//The ID of the item
private constant integer ITEM_ID = 'A000'
//Half the amount of life the ability adds to allow for both positive and negative damage
private constant integer ITEM_STRENGTH = 50000
//The armor constant on gameplay constant
private constant real ARMOR_CONSTANT = 0.06
//The factor for Ethereal and Magic Damage
private constant real ETHEREAL_FACTOR = 1.66
//The test value, the higher the more precise (too big can bug and instant kill on negative armor unit)
private constant real TEST_VALUE = 1000.
endglobals
globals
private constant real E = 2.718282
private constant real INV_E = 1/E
//Cache for the Ln armor constant used to compute negative armor.
private real ARMOR_NEGATIVE_LNCACHE
endglobals
//Thanks to looking_for_help !
private function internLn takes real r returns real
local real sum = 0.0
local real sign = 1.0
if r < 1.0 then
set r = 1.0/r
set sign = -1.0
endif
loop
exitwhen r < E
set r = r*INV_E
set sum = sum + 1.0
endloop
loop
exitwhen r < 1.2840254
set r = r*0.778808
set sum = sum + 0.25
endloop
return sign*(sum + 0.125*(r - 1.0)*(1 + 9.0/(2.0 + r) + 4.5/(0.5 + r) + 1.0/r))
endfunction
function GetUnitArmor takes unit u returns real
local real lold
local real llost
local real mul = 1
set lold = GetWidgetLife(u)
call UnitAddAbility(u, ITEM_ID)
call SetWidgetLife(u, ITEM_STRENGTH)
if (IsUnitType(u, UNIT_TYPE_ETHEREAL)) then
call UnitDamageTarget(u, u, TEST_VALUE, true, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_NORMAL, null)
set mul = ETHEREAL_FACTOR
else
call UnitDamageTarget(u, u, TEST_VALUE, true, false, ATTACK_TYPE_CHAOS, DAMAGE_TYPE_NORMAL, null)
endif
set llost = (ITEM_STRENGTH - GetWidgetLife(u))/mul
// apply armor type and damage type corrections here
call UnitRemoveAbility(u, ITEM_ID)
call SetWidgetLife(u,lold)
if llost > TEST_VALUE then
return -internLn(2 - llost / TEST_VALUE) / ARMOR_NEGATIVE_LNCACHE
endif
return (TEST_VALUE - llost) / (ARMOR_CONSTANT * llost)
endfunction
private function init takes nothing returns nothing
//Generate cache
set ARMOR_NEGATIVE_LNCACHE = internLn(1 - ARMOR_CONSTANT)
endfunction
endlibrary
Attachments
Last edited: