Name | Type | is_array | initial_value |
u | unit | No |
//TESH.scrollpos=125
//TESH.alwaysfold=0
library TimerUtils initializer init
//*********************************************************************
//* TimerUtils (red+blue+orange flavors for 1.24b+) 2.0
//* ----------
//*
//* 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)
//* set t=NewTimerEx(x) : Get a timer (alternative to CreateTimer), call
//* Initialize timer data as x, instead of 0.
//*
//* 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.
private boolean didinit = false
endglobals
private keyword init
//==========================================================================================
// I needed to decide between duplicating code ignoring the "Once and only once" rule
// and using the ugly textmacros. I guess textmacros won.
//
//! textmacro TIMERUTIS_PRIVATE_NewTimerCommon takes VALUE
// On second thought, no.
//! endtextmacro
function NewTimerEx takes integer value returns timer
if (tN==0) then
if (not didinit) then
//This extra if shouldn't represent a major performance drawback
//because QUANTITY rule is not supposed to be broken every day.
call init.evaluate()
set tN = tN - 1
else
//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")
set tT[0]=CreateTimer()
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")
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
endif
else
set tN=tN-1
endif
call SetTimerData(tT[tN],value)
return tT[tN]
endfunction
function NewTimer takes nothing returns timer
return NewTimerEx(0)
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
if ( didinit ) then
return
else
set didinit = true
endif
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=62
//TESH.alwaysfold=0
/*
=====Spawn System v1.1
=====by: Mckill2009
HOW TO INSTALL:
- Make a new trigger and convert to custom text via EDIT >>> CONVERT CUSTOM TEXT
- Copy ALL that is written here and overwrite the existing texts in the custom text
- Copy the TimerUtils 2.0 by Vexorian
REQUIREMENTS:
- Jass New Gen Pack by Vexorian
- TimerUtils by Vexorian
HOW TO USE:
- Create a region(rect in Jass) in your map, this is the "r" parameter
- call SpawnSystem.create(p, unittypeid, duration, interval, sfx, r, maxcount, centerregion, life)
PARAMETER EXPLANATION:
- p (Player) = The Player where the spawned unit belong
- unittypeid (Integer) = The UnitType, this is an integer RAW CODE of a unit
- duration (Real) = The duration of the spell
- interval (Real) = The interval of the creation of the spawned unit
- life (Real) = sets the life of created unit
- sfx (String) = Used for Special Effect, strings like "Abilities\\Spells\\Undead\\RaiseSkeletonWarrior\\RaiseSkeleton.mdl" is an example of such
- r (Rect) = Is the rect or region square on which your unit will spawn, example like gg_trg_Region_000
- maxcount (Integer) = the maximum units created
- centerregion (Boolean) = creates center fo the region or in random places
- trig (Trigger) = creates a condition for premature stop, calls TriggerEvaluate that returns boolean, set to null to disable condition
- x (Real) = picks a coordinate (r=rect should be null if this is used)
- y (Real) = picks a coordinate (r=rect should be null if this is used)
HOW TO GET THE RAW CODE:
- You can view the raw codes by pressing CTRL+D in the object editor
- Examples of raw codes are 'uskm', 'hpea', if it's a custom unit then 'h000', 'H000' and so on...
CREDITS: For their suggestions
- watermelon_1234
- Magtheridon96
*/
library SpawnSystem uses TimerUtils
globals
//===CONFIGURABLES:
private constant integer FIXED_MAX_COUNT = 500 //sets the max unit created
endglobals
struct SpawnSystem
player p
integer uType
integer maxcount
real duration
real interval
real life
real xLoc
real yLoc
string sfx
rect r
private boolean center
private trigger trg
static method onSpawnThem takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
local unit dummy
if .duration > 0 and .maxcount > 0 and (TriggerEvaluate(.trg) or .trg==null) then
set .maxcount = .maxcount - 1
set .duration = .duration - .interval
if .r==null then
set dummy = CreateUnit(.p, .uType, .xLoc, .yLoc, 0)
else
if not .center then
set dummy = CreateUnit(.p, .uType, GetRandomReal(GetRectMinX(.r), GetRectMaxX(.r)), GetRandomReal(GetRectMinY(.r), GetRectMaxY(.r)), 0)
else
set dummy = CreateUnit(.p, .uType, GetRectCenterX(.r), GetRectCenterY(.r), 0)
endif
endif
if not IsUnitType(dummy, UNIT_TYPE_FLYING) then
call DestroyEffect(AddSpecialEffect(.sfx, GetUnitX(dummy), GetUnitY(dummy)))
else
call DestroyEffect(AddSpecialEffectTarget(.sfx, dummy,"origin"))
endif
call UnitApplyTimedLife(dummy, 'BTLF', .life)
else
call DestroyTrigger(.trg)
call ReleaseTimer(t)
call .destroy()
endif
set t = null
set dummy = null
endmethod
static method create takes player p, integer unittypeid, real duration, real interval, real lifeduration, string sfx, rect r, integer maxcount, boolean centerregion, trigger trig, real x, real y returns thistype
local thistype this = thistype.allocate()
set .p = p
set .uType = unittypeid
set .duration = duration
set .interval = interval
set .sfx = sfx
set .life = lifeduration
set .r = r
if maxcount > FIXED_MAX_COUNT then
set .maxcount = FIXED_MAX_COUNT
else
set .maxcount = maxcount
endif
set .center = centerregion
set .life = life
set .trg = trig
set .xLoc = x
set .yLoc = y
call TimerStart(NewTimerEx(this), interval, true, function thistype.onSpawnThem)
return this
endmethod
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope SStest initializer init
private function Alive takes nothing returns boolean
return not IsUnitType(udg_u, UNIT_TYPE_DEAD)
endfunction
private function RandomNumbers takes nothing returns boolean
local integer i = GetRandomInt(1,10)
if i==3 then
call BJDebugMsg("Spawning Stop coz integer is "+I2S(i))
return false
endif
return true
endfunction
private function init takes nothing returns nothing
local string s = "Abilities\\Spells\\Undead\\RaiseSkeletonWarrior\\RaiseSkeleton.mdl"
local trigger t = CreateTrigger()
call TriggerAddCondition(t, function RandomNumbers)
set udg_u = gg_unit_hpea_0002
//Spawning will stop if the integer is 3
call SpawnSystem.create(Player(0), 'uskm', 100, 1, 0, s, gg_rct_loc1, 500, false, t,0,0)
set t = CreateTrigger()
call TriggerAddCondition(t, function Alive)
//Spawning will stop if the peasant is killed
call SpawnSystem.create(Player(0), 'hfoo', 50, 0.5, 0, s, gg_rct_loc2, 500, true, t,0,0)
//Sfx for flying unit, null trigger to disable condition and life timer
call SpawnSystem.create(Player(0), 'hgry', 75, 1.5, 1, s, null, 500, false, null, GetUnitX(udg_u), GetUnitY(udg_u))
set t = null
endfunction
endscope