Name | Type | is_array | initial_value |
Real | real | No | |
TrigLoc | location | No |
//TESH.scrollpos=108
//TESH.alwaysfold=0
library TimerUtils initializer init
//*********************************************************************
//* TimerUtils (red+blue+orange flavors for 1.24b+)
//* ----------
//*
//* To implement it , create a custom text trigger called TimerUtils
//* and paste the contents of this script there.
//*
//* To copy from a map to another, copy the trigger holding this
//* library to your map.
//*
//* (requires vJass) More scripts: htt://www.wc3c.net
//*
//* For your timer needs:
//* * Attaching
//* * Recycling (with double-free protection)
//*
//* set t=NewTimer() : Get a timer (alternative to CreateTimer)
//* ReleaseTimer(t) : Relese a timer (alt to DestroyTimer)
//* SetTimerData(t,2) : Attach value 2 to timer
//* GetTimerData(t) : Get the timer's value.
//* You can assume a timer's value is 0
//* after NewTimer.
//*
//* Multi-flavor:
//* Set USE_HASH_TABLE to true if you don't want to complicate your life.
//*
//* If you like speed and giberish try learning about the other flavors.
//*
//********************************************************************
//================================================================
globals
//How to tweak timer utils:
// USE_HASH_TABLE = true (new blue)
// * SAFEST
// * SLOWEST (though hash tables are kind of fast)
//
// USE_HASH_TABLE = false, USE_FLEXIBLE_OFFSET = true (orange)
// * kinda safe (except there is a limit in the number of timers)
// * ALMOST FAST
//
// USE_HASH_TABLE = false, USE_FLEXIBLE_OFFSET = false (red)
// * THE FASTEST (though is only faster than the previous method
// after using the optimizer on the map)
// * THE LEAST SAFE ( you may have to tweak OFSSET manually for it to
// work)
//
private constant boolean USE_HASH_TABLE = true
private constant boolean USE_FLEXIBLE_OFFSET = false
private constant integer OFFSET = 0x100000
private integer VOFFSET = OFFSET
//Timers to preload at map init:
private constant integer QUANTITY = 256
//Changing this to something big will allow you to keep recycling
// timers even when there are already AN INCREDIBLE AMOUNT of timers in
// the stack. But it will make things far slower so that's probably a bad idea...
private constant integer ARRAY_SIZE = 8190
endglobals
//==================================================================================================
globals
private integer array data[ARRAY_SIZE]
private hashtable ht
endglobals
//It is dependent on jasshelper's recent inlining optimization in order to perform correctly.
function SetTimerData takes timer t, integer value returns nothing
static if(USE_HASH_TABLE) then
// new blue
call SaveInteger(ht,0,GetHandleId(t), value)
elseif (USE_FLEXIBLE_OFFSET) then
// orange
static if (DEBUG_MODE) then
if(GetHandleId(t)-VOFFSET<0) then
call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
endif
endif
set data[GetHandleId(t)-VOFFSET]=value
else
// new red
static if (DEBUG_MODE) then
if(GetHandleId(t)-OFFSET<0) then
call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
endif
endif
set data[GetHandleId(t)-OFFSET]=value
endif
endfunction
function GetTimerData takes timer t returns integer
static if(USE_HASH_TABLE) then
// new blue
return LoadInteger(ht,0,GetHandleId(t) )
elseif (USE_FLEXIBLE_OFFSET) then
// orange
static if (DEBUG_MODE) then
if(GetHandleId(t)-VOFFSET<0) then
call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
endif
endif
return data[GetHandleId(t)-VOFFSET]
else
// new red
static if (DEBUG_MODE) then
if(GetHandleId(t)-OFFSET<0) then
call BJDebugMsg("SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer")
endif
endif
return data[GetHandleId(t)-OFFSET]
endif
endfunction
//==========================================================================================
globals
private timer array tT[ARRAY_SIZE]
private integer tN = 0
private constant integer HELD=0x28829022
//use a totally random number here, the more improbable someone uses it, the better.
endglobals
//==========================================================================================
function NewTimer takes nothing returns timer
if (tN==0) then
//If this happens then the QUANTITY rule has already been broken, try to fix the
// issue, else fail.
debug call BJDebugMsg("NewTimer: Warning, Exceeding TimerUtils_QUANTITY, make sure all timers are getting recycled correctly")
static if( not USE_HASH_TABLE) then
debug call BJDebugMsg("In case of errors, please increase it accordingly, or set TimerUtils_USE_HASH_TABLE to true")
set tT[0]=CreateTimer()
static if( USE_FLEXIBLE_OFFSET) then
if (GetHandleId(tT[0])-VOFFSET<0) or (GetHandleId(tT[0])-VOFFSET>=ARRAY_SIZE) then
//all right, couldn't fix it
call BJDebugMsg("NewTimer: Unable to allocate a timer, you should probably set TimerUtils_USE_HASH_TABLE to true or fix timer leaks.")
return null
endif
else
if (GetHandleId(tT[0])-OFFSET<0) or (GetHandleId(tT[0])-OFFSET>=ARRAY_SIZE) then
//all right, couldn't fix it
call BJDebugMsg("NewTimer: Unable to allocate a timer, you should probably set TimerUtils_USE_HASH_TABLE to true or fix timer leaks.")
return null
endif
endif
endif
else
set tN=tN-1
endif
call SetTimerData(tT[tN],0)
return tT[tN]
endfunction
//==========================================================================================
function ReleaseTimer takes timer t returns nothing
if(t==null) then
debug call BJDebugMsg("Warning: attempt to release a null timer")
return
endif
if (tN==ARRAY_SIZE) then
debug call BJDebugMsg("Warning: Timer stack is full, destroying timer!!")
//stack is full, the map already has much more troubles than the chance of bug
call DestroyTimer(t)
else
call PauseTimer(t)
if(GetTimerData(t)==HELD) then
debug call BJDebugMsg("Warning: ReleaseTimer: Double free!")
return
endif
call SetTimerData(t,HELD)
set tT[tN]=t
set tN=tN+1
endif
endfunction
private function init takes nothing returns nothing
local integer i=0
local integer o=-1
local boolean oops = false
static if( USE_HASH_TABLE ) then
set ht = InitHashtable()
loop
exitwhen(i==QUANTITY)
set tT[i]=CreateTimer()
call SetTimerData(tT[i], HELD)
set i=i+1
endloop
set tN = QUANTITY
else
loop
set i=0
loop
exitwhen (i==QUANTITY)
set tT[i] = CreateTimer()
if(i==0) then
set VOFFSET = GetHandleId(tT[i])
static if(USE_FLEXIBLE_OFFSET) then
set o=VOFFSET
else
set o=OFFSET
endif
endif
if (GetHandleId(tT[i])-o>=ARRAY_SIZE) then
exitwhen true
endif
if (GetHandleId(tT[i])-o>=0) then
set i=i+1
endif
endloop
set tN = i
exitwhen(tN == QUANTITY)
set oops = true
exitwhen not USE_FLEXIBLE_OFFSET
debug call BJDebugMsg("TimerUtils_init: Failed a initialization attempt, will try again")
endloop
if(oops) then
static if ( USE_FLEXIBLE_OFFSET) then
debug call BJDebugMsg("The problem has been fixed.")
//If this message doesn't appear then there is so much
//handle id fragmentation that it was impossible to preload
//so many timers and the thread crashed! Therefore this
//debug message is useful.
elseif(DEBUG_MODE) then
call BJDebugMsg("There were problems and the new timer limit is "+I2S(i))
call BJDebugMsg("This is a rare ocurrence, if the timer limit is too low:")
call BJDebugMsg("a) Change USE_FLEXIBLE_OFFSET to true (reduces performance a little)")
call BJDebugMsg("b) or try changing OFFSET to "+I2S(VOFFSET) )
endif
endif
endif
endfunction
endlibrary
//TESH.scrollpos=52
//TESH.alwaysfold=0
library BerserkersLeap initializer Init needs TimerUtils
globals
private constant integer RAWCODE = 'A000'//Put your spell's RawCode here:)
private group aGroup = CreateGroup()//Do not do anything with this:)
private constant string EFFECT = "Abilities\\Spells\\Human\\ThunderClap\\ThunderClapCaster.mdl"
private group dGroup = CreateGroup()//Do not edit this
private constant integer DUMMY_ID = 'h001'//Put the dummy RAWCODE here
private constant integer DA_RAWCODE = 'A002'// Put the Dummy Abillity Rawcode here
private constant real SPEED = 10.
private timer SpellTimer
private location SpellLoc = Location( 0., 0.)
endglobals
private function Damage takes integer level returns real
return (level * 125.)
endfunction
private struct leap
private unit Caster
integer level
real CasterX
real CasterY
real TargetX
real TargetY
real Angle
real RangeFromTarget
real RangeCheck
unit Target
static method GetParabolaZ takes real x, real d, real h returns real
return (4. * h * x * (d - x) / (d * d))
endmethod
static method Actions takes nothing returns nothing
local thistype this = GetTimerData(SpellTimer)
local unit Dummy//This is just a local we'll use in the DummyCreation.
local real DummyX
local real DummyY
local real CastX
local real CastY
local real DisMissle
local real Degrees
local real DummyAngle//This is the angle the Dummies are made to shoot towards, no need to do anything here...
local integer int = 12//this is the ammount of dummies thats created that will shoot the shockwaves
set .RangeCheck = .RangeCheck + SPEED
set .CasterX = .CasterX + SPEED * Cos(.Angle * bj_DEGTORAD)
set .CasterY = .CasterY + SPEED * Sin(.Angle * bj_DEGTORAD)
call SetUnitX(.Caster, .CasterX)
call SetUnitY(.Caster, .CasterY)
set DisMissle = (.TargetX - .CasterX) + (.TargetY - .CasterY)
if 0. >= DisMissle then
set DisMissle = DisMissle * -1.
endif
call SetUnitFlyHeight( .Caster, GetParabolaZ( DisMissle, .RangeFromTarget, 550.), 0.)
if(.RangeCheck >= .RangeFromTarget) then
set Degrees = .Angle
call SetUnitFlyHeight( .Caster, 0., 0)
call SetUnitTimeScale( .Caster, 1.)
call GroupEnumUnitsInRange(aGroup, .CasterX, .CasterY, 250., null)
call DestroyEffect(AddSpecialEffect(EFFECT, .CasterX,.CasterY))
//****************************************************************************************
//***************************************** DUMMY*****************************************
//***************************************** CREATION**************************************
//***************************************** Loop ****************************************
//****************************************************************************************
loop
set int = int - 1
exitwhen int == 0
set Degrees = Degrees + 30
set DummyX = .CasterX + 100 * Cos(Degrees)
set DummyY = .CasterY + 100 * Sin(Degrees)
set Dummy = CreateUnit(GetOwningPlayer(.Caster), DUMMY_ID, DummyX, DummyY, Cos(Atan2(DummyY - .CasterY, DummyX - .CasterX)))
set CastX = DummyX + Cos(Atan2( DummyY - .CasterY, DummyX - .CasterX))
set CastY = DummyY + Sin(Atan2( DummyY - .CasterY, DummyX - .CasterX))
call UnitAddAbility( Dummy, DA_RAWCODE)
call SetUnitAbilityLevel( Dummy, DA_RAWCODE, .level)
call IssuePointOrder( Dummy, "shockwave", CastX, CastY)
call UnitApplyTimedLife( Dummy, 'BTLF', 0.50)
endloop
//****************************************************************************************
//***************************************** TARGET****************************************
//***************************************** DAMAGING**************************************
//***************************************** Loop ****************************************
//****************************************************************************************
loop
set .Target = FirstOfGroup(aGroup)
exitwhen (.Target == null)
if(IsUnitEnemy(.Target, GetOwningPlayer(.Caster)) == true) then
call UnitDamageTarget(.Caster, .Target, Damage(.level), true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, null)
endif
call GroupRemoveUnit(aGroup, .Target)
set .Target = null
endloop
call ReleaseTimer(SpellTimer)
set Dummy = null
endif
endmethod
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set SpellTimer = NewTimer()
set .Caster = GetTriggerUnit()
set .CasterX = GetUnitX(.Caster)
set .CasterY = GetUnitY(.Caster)
set .TargetX = GetSpellTargetX()
set .TargetY = GetSpellTargetY()
set .level = GetUnitAbilityLevel(.Caster, RAWCODE)
set .Angle = bj_RADTODEG * Atan2(.TargetY - .CasterY, .TargetX - .CasterX)
set .RangeFromTarget = SquareRoot((.CasterX - .TargetX) * (.CasterX - .TargetX) + (.CasterY - .TargetY) * (.CasterY - .TargetY))
call MoveLocation( SpellLoc, .TargetX, .TargetY)
call UnitAddAbility(.Caster, 'Arav')
call UnitRemoveAbility(.Caster, 'Arav')
call SetTimerData(SpellTimer, this)
call SetUnitTimeScale( .Caster, 50. * 0.01)
call TimerStart(SpellTimer, 0.01, true, function leap.Actions)
return this
endmethod
endstruct
private function C takes nothing returns boolean
if (GetSpellAbilityId() == RAWCODE) then
call leap.create()
endif
return false
endfunction
//===========================================================================
function Init takes nothing returns nothing
local trigger Berserk = CreateTrigger( )
call TriggerRegisterAnyUnitEventBJ( Berserk, EVENT_PLAYER_UNIT_SPELL_CAST)
call TriggerAddCondition( Berserk, Condition(function C))
call Preload(EFFECT)
set bj_lastCreatedUnit = CreateUnit(Player(15), 'h001', 0., 0., 270.)
call UnitAddAbility(bj_lastCreatedUnit, RAWCODE)
call UnitRemoveAbility(bj_lastCreatedUnit, RAWCODE)
call RemoveUnit(bj_lastCreatedUnit)
endfunction
endlibrary