Moderator
M
Moderator
20:26, 3rd Nov 2010
The_Reborn_Devil:
Pretty simple, but neat.
Status: Approved
Rating: Useful
The_Reborn_Devil:
Pretty simple, but neat.
Status: Approved
Rating: Useful
//This is a custom Damage Reduction system that provides easy setting and changing of
//a unit's damage reduction and spell reduction by just using some function calls
//No object data needed to be created in the Object editor
//This system is useful for maps that will be having a custom damage system
//////////////////////////////////////////////////////////////////////////////
//--------------HOW TO USE--------------------------------------------------//
//--------------HOW TO INITIALIZE-------------------------------------------//
//First you need to initialize each unit that will use this system using //
// //
//DamageReduction.create(unit target, real damagered, real spellred) //
// //
//unit target = unit to be initialized //
// //
//real damagered = damage reduction value //
// //
//real spellred = spell reduction value //
// //
//-------------HOW TO GET VALUES--------------------------------------------//
// //
//To get the value of spell or damage reduction just use these 2 functions: //
// //
//DamageReduction.GetDamageReduction(unit u) //
// //
//DamageReduction.GetSpellReduction(unit u) //
// //
//-------------HOW TO MODIFY VALUES IN-GAME---------------------------------//
// //
//To modify values in-game just use these 2 functions: //
// //
//DamageReduction.AddSpellReduction(unit target, real value, boolean add) //
// //
//DamageReduction.AddDamageReduction(unit target, real value, boolean add) //
// //
//unit target is the unit whose reduction value you want to change //
// //
//real value is the value of reduction to be used //
// //
//boolean add if true will add real value to the current reduction //
// //
//while if false will set reduction to real value //
// //
//NOTE: these two functions returns the new reduction values so if you need //
//to use those right away, you can directly use these function rather than //
//using the Add function and then using the Get functions afterwards //
// //
//------------REMOVING INSTANCES--------------------------------------------//
// //
//The system already checks every UPDATE_TIME interval for instances that //
// //
//are already unused due to the unit being dead and destroys that instance //
// //
//automatically. //
// //
//You can destroy an instance directly by using //
//DamageReduction.Clear(unit u) //
//////////////////////////////////////////////////////////////////////////////
library DamageReductionTable
//Version 1.1c
//By: Adiktuz
globals
private constant real UPDATE_TIME = 1.00
//This is the timer interval between the system's check for dead units to remove their instances
private integer array DRI
private integer TOTAL = 0
private timer DRT_TIMER = CreateTimer()
endglobals
struct DamageReduction
real DAMAGE_REDUCTION
real SPELL_REDUCTION
unit u
static thistype data
//Used by the methods in this system to translate the unit parameter
//into its struct instance
static method GetInstance takes unit target returns thistype
local integer i = 0
loop
exitwhen i == TOTAL
set data = DRI[i]
if target == data.u then
set i = TOTAL
else
set i = i + 1
set data = 0
endif
endloop
return data
endmethod
//Methods for Getting and setting reduction data
static method GetDamageReduction takes unit u returns real
return data.GetInstance(u).DAMAGE_REDUCTION
endmethod
static method GetSpellReduction takes unit u returns real
return data.GetInstance(u).SPELL_REDUCTION
endmethod
static method AddSpellReduction takes unit target, real value ,boolean add returns real
set data = DamageReduction.GetInstance(target)
if add then
set data.SPELL_REDUCTION = data.SPELL_REDUCTION + value
else
set data.SPELL_REDUCTION = value
endif
return data.SPELL_REDUCTION
endmethod
static method AddDamageReduction takes unit target, real value ,boolean add returns real
set data = DamageReduction.GetInstance(target)
if add then
set data.DAMAGE_REDUCTION = data.DAMAGE_REDUCTION + value
else
set data.DAMAGE_REDUCTION = value
endif
return data.DAMAGE_REDUCTION
endmethod
//Method for clearing a unit's instance immediately
static method Clear takes unit u returns nothing
local integer i = 0
set data = DamageReduction.GetInstance(u)
loop
exitwhen i == TOTAL
if data == DRI[i] then
set TOTAL = TOTAL - 1
set DRI[i] = DRI[TOTAL]
set i = TOTAL
call data.destroy()
else
set i = i + 1
endif
endloop
endmethod
//This method scans for instanced units that are already dead
//and destroy's their instance
static method update takes nothing returns nothing
local integer i = 0
loop
exitwhen i == TOTAL
set data = DRI[i]
if data.u == null or GetWidgetLife(data.u) < .405 then
set TOTAL = TOTAL - 1
set DRI[i] = DRI[TOTAL]
set i = i - 1
call data.destroy()
endif
set i = i + 1
endloop
if TOTAL == 0 then
call PauseTimer(DRT_TIMER)
endif
endmethod
//This method is used to initialize each unit for the system
static method create takes unit target, real damagered, real spellred returns thistype
set data = DamageReduction.allocate()
set data.DAMAGE_REDUCTION = damagered
set data.SPELL_REDUCTION = spellred
set data.u = target
set DRI[TOTAL] = data
if TOTAL == 0 then
call TimerStart(DRT_TIMER, UPDATE_TIME, true, function DamageReduction.update)
endif
set TOTAL = TOTAL + 1
return data
endmethod
endstruct
endlibrary