Name | Type | is_array | initial_value |
R | rect | No |
//TESH.scrollpos=3
//TESH.alwaysfold=0
library UnitIndexer /* v4.0.2.5
*************************************************************************************
*
* Assigns unique indexes to units via unit user data.
*
*************************************************************************************
*
* */uses/*
* */ WorldBounds /* hiveworkshop.com/forums/jass-functions-413/snippet-worldbounds-180494/
* */ Event /* hiveworkshop.com/forums/submissions-414/snippet-event-186555/
*
************************************************************************************
*
* SETTINGS
*/
globals
constant integer ABILITIES_UNIT_INDEXER = 'A000'
endglobals
/*
************************************************************************************
*
* Functions
*
* function RegisterUnitIndexEvent takes boolexpr codeToRegister, Event unitIndexEvent returns nothing
* function TriggerRegisterUnitIndexEvent takes trigger triggerToRegister, Event unitIndexEvent returns nothing
*
* function GetUnitById takes integer index returns unit
* - Returns unit given a unit index
* function GetUnitId takes unit u returns integer
* - Returns unit index given a unit
*
* function IsUnitIndexed takes unit u returns boolean
* function IsUnitDeindexing takes unit u returns boolean
*
* function GetIndexedUnitId takes nothing returns integer
* function GetIndexedUnit takes nothing returns unit
*
************************************************************************************
*
* module UnitIndexStruct
*
* - A pseudo module interface that runs a set of methods if they exist and provides
* - a few fields and operators. Runs on static ifs to minimize code.
*
* static method operator [] takes unit u returns thistype
* - Return GetUnitUserData(u)
*
* readonly unit unit
* - The indexed unit of the struct
* readonly boolean allocated
* - Is unit allocated for the struct
*
* Interface:
*
* - These methods don't have to exist. If they don't exist, the code
* - that calls them won't even be in the module.
*
* private method index takes nothing returns nothing
* - called when a unit is indexed and passes the filter.
* -
* - thistype this: Unit's index
* private method deindex takes nothing returns nothing
* - called when a unit is deindexed and is allocated for struct
* -
* - thistype this: Unit's index
* private static method filter takes unit unitToIndex returns boolean
* - Determines whether or not to allocate struct for unit
* -
* - unit unitToIndex: Unit being filtered
*
************************************************************************************
*
* struct UnitIndexer extends array
*
* - Controls the unit indexer system.
*
* static constant Event UnitIndexer.INDEX
* static constant Event UnitIndexer.DEINDEX
* - Don't register functions and triggers directly to the events. Register them via
* - RegisterUnitIndexEvent and TriggerRegisterUnitIndexEvent.
*
* static boolean enabled
* - Enables and disables unit indexing. Useful for filtering out dummy units.
*
************************************************************************************
*
* struct UnitIndex extends array
*
* - Constrols specific unit indexes.
*
* method lock takes nothing returns nothing
* - Locks an index. When an index is locked, it will not be recycled
* - when the unit is deindexed until all locks are removed. Deindex
* - events still fire at the appropriate times, the index just doesn't
* - get thrown into the recycler.
* method unlock takes nothing returns nothing
* - Unlocks an index.
*
************************************************************************************/
globals
private trigger q=CreateTrigger()
private trigger l=CreateTrigger()
private unit array e
private integer r=0
private integer y=0
private integer o=0
private boolean a=false
private integer array n
private integer array p
private integer array lc
endglobals
function GetIndexedUnitId takes nothing returns integer
return o
endfunction
function GetIndexedUnit takes nothing returns unit
return e[o]
endfunction
//! runtextmacro optional UNIT_LIST_LIB()
private struct PreLoader extends array
public static method run takes nothing returns nothing
call DestroyTimer(GetExpiredTimer())
set a=true
endmethod
public static method eval takes trigger t returns nothing
local integer f=n[0]
local integer d=o
loop
exitwhen 0==f
if (IsTriggerEnabled(t)) then
set o=f
if (TriggerEvaluate(t)) then
call TriggerExecute(t)
endif
else
exitwhen true
endif
set f=n[f]
endloop
set o=d
endmethod
public static method evalb takes boolexpr c returns nothing
local trigger t=CreateTrigger()
local thistype f=n[0]
local integer d=o
call TriggerAddCondition(t,c)
loop
exitwhen 0==f
set o=f
call TriggerEvaluate(t)
set f=n[f]
endloop
call DestroyTrigger(t)
set t=null
set o=d
endmethod
endstruct
//! runtextmacro optional UNIT_EVENT_MACRO()
private module UnitIndexerInit
private static method onInit takes nothing returns nothing
local integer i=15
local boolexpr bc=Condition(function thistype.onLeave)
local boolexpr bc2=Condition(function thistype.onEnter)
local group g=CreateGroup()
local player p
set INDEX=CreateEvent()
set DEINDEX=CreateEvent()
call TriggerRegisterEnterRegion(q,WorldBounds.worldRegion,bc2)
loop
set p=Player(i)
call TriggerRegisterPlayerUnitEvent(l,p,EVENT_PLAYER_UNIT_ISSUED_ORDER,bc)
call SetPlayerAbilityAvailable(p,ABILITIES_UNIT_INDEXER,false)
call GroupEnumUnitsOfPlayer(g,p,bc2)
exitwhen 0==i
set i=i-1
endloop
call DestroyGroup(g)
set bc=null
set g=null
set bc2=null
set p=null
call TimerStart(CreateTimer(),0,false,function PreLoader.run)
endmethod
endmodule
struct UnitIndex extends array
method lock takes nothing returns nothing
debug if (null!=e[this]) then
set lc[this]=lc[this]+1
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"UNIT INDEXER ERROR: ATTEMPT TO LOCK NULL INDEX")
debug endif
endmethod
method unlock takes nothing returns nothing
debug if (0<lc[this]) then
set lc[this]=lc[this]-1
if (0==lc[this] and null==e[this]) then
set n[this]=y
set y=this
endif
debug else
debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"UNIT INDEXER ERROR: ATTEMPT TO UNLOCK UNLOCKED INDEX")
debug endif
endmethod
endstruct
struct UnitIndexer extends array
readonly static Event INDEX
readonly static Event DEINDEX
static boolean enabled=true
private static method onEnter takes nothing returns boolean
local unit Q=GetFilterUnit()
local integer i
local integer d=o
if (enabled and Q!=e[GetUnitUserData(Q)]) then
if (0==y) then
set r=r+1
set i=r
else
set i=y
set y=n[y]
endif
call UnitAddAbility(Q,ABILITIES_UNIT_INDEXER)
call UnitMakeAbilityPermanent(Q,true,ABILITIES_UNIT_INDEXER)
call SetUnitUserData(Q,i)
set e[i]=Q
static if not LIBRARY_UnitList then
if (not a)then
set p[i]=p[0]
set n[p[0]]=i
set n[i]=0
set p[0]=i
endif
else
set p[i]=p[0]
set n[p[0]]=i
set n[i]=0
set p[0]=i
call GroupAddUnit(g,e[i])
endif
set o=i
call FireEvent(INDEX)
set o=d
endif
set Q=null
return false
endmethod
private static method onLeave takes nothing returns boolean
static if LIBRARY_UnitEvent then
implement optional UnitEventModule
else
local unit u=GetFilterUnit()
local integer i=GetUnitUserData(u)
local integer d=o
if (0==GetUnitAbilityLevel(u,ABILITIES_UNIT_INDEXER) and u==e[i]) then
static if not LIBRARY_UnitList then
if (not a)then
set n[p[i]]=n[i]
set p[n[i]]=p[i]
endif
else
set n[p[i]]=n[i]
set p[n[i]]=p[i]
call GroupRemoveUnit(g,e[i])
endif
set o=i
call FireEvent(DEINDEX)
set o=d
if (0==lc[i]) then
set n[i]=y
set y=i
endif
set e[i]=null
endif
set u=null
endif
return false
endmethod
implement UnitIndexerInit
endstruct
//! runtextmacro optional UNIT_EVENT_MACRO_2()
function RegisterUnitIndexEvent takes boolexpr c,integer ev returns nothing
call RegisterEvent(c, ev)
if (not a and ev==UnitIndexer.INDEX and 0!=n[0]) then
call PreLoader.evalb(c)
endif
endfunction
function TriggerRegisterUnitIndexEvent takes trigger t,integer ev returns nothing
call TriggerRegisterEvent(t,ev)
if (not a and ev == UnitIndexer.INDEX and 0!=n[0]) then
call PreLoader.eval(t)
endif
endfunction
function GetUnitById takes integer W returns unit
return e[W]
endfunction
function GetUnitId takes unit u returns integer
return GetUnitUserData(u)
endfunction
function IsUnitIndexed takes unit u returns boolean
return u==e[GetUnitUserData(u)]
endfunction
function IsUnitDeindexing takes unit u returns boolean
return IsUnitIndexed(u) and 0==GetUnitAbilityLevel(u,ABILITIES_UNIT_INDEXER)
endfunction
module UnitIndexStruct
static method operator [] takes unit u returns thistype
return GetUnitUserData(u)
endmethod
method operator unit takes nothing returns unit
return e[this]
endmethod
static if thistype.filter.exists then
static if thistype.index.exists then
static if thistype.deindex.exists then
readonly boolean allocated
else
method operator allocated takes nothing returns boolean
return filter(e[this])
endmethod
endif
else
method operator allocated takes nothing returns boolean
return filter(e[this])
endmethod
endif
elseif (thistype.index.exists) then
static if thistype.deindex.exists then
readonly boolean allocated
else
method operator allocated takes nothing returns boolean
return this==GetUnitUserData(e[this])
endmethod
endif
else
method operator allocated takes nothing returns boolean
return this==GetUnitUserData(e[this])
endmethod
endif
static if thistype.index.exists then
private static method onIndexEvent takes nothing returns boolean
static if thistype.filter.exists then
if (filter(e[o])) then
static if thistype.deindex.exists then
set thistype(o).allocated=true
endif
call thistype(o).index()
endif
else
static if thistype.deindex.exists then
set thistype(o).allocated=true
endif
call thistype(o).index()
endif
return false
endmethod
endif
static if thistype.deindex.exists then
private static method onDeindexEvent takes nothing returns boolean
static if thistype.filter.exists then
static if thistype.index.exists then
if (thistype(o).allocated) then
set thistype(o).allocated=false
call thistype(o).deindex()
endif
else
if (filter(e[o])) then
call thistype(o).deindex()
endif
endif
else
static if thistype.index.exists then
set thistype(o).allocated=false
endif
call thistype(o).deindex()
endif
return false
endmethod
endif
static if thistype.index.exists then
static if thistype.deindex.exists then
private static method onInit takes nothing returns nothing
call RegisterUnitIndexEvent(Condition(function thistype.onIndexEvent),UnitIndexer.INDEX)
call RegisterUnitIndexEvent(Condition(function thistype.onDeindexEvent),UnitIndexer.DEINDEX)
endmethod
else
private static method onInit takes nothing returns nothing
call RegisterUnitIndexEvent(Condition(function thistype.onIndexEvent),UnitIndexer.INDEX)
endmethod
endif
elseif thistype.deindex.exists then
private static method onInit takes nothing returns nothing
call RegisterUnitIndexEvent(Condition(function thistype.onDeindexEvent),UnitIndexer.DEINDEX)
endmethod
endif
endmodule
endlibrary
//TESH.scrollpos=0
//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=0
//TESH.alwaysfold=0
library WorldBounds /* v2.0.0.0
************************************************************************************
*
* struct WorldBounds extends array
* readonly static integer maxX
* readonly static integer maxY
* readonly static integer minX
* readonly static integer minY
* readonly static integer centerX
* readonly static integer centerY
* readonly static rect world
* readonly static region worldRegion
*
************************************************************************************/
private module WorldBoundInit
private static method onInit takes nothing returns nothing
set world=GetWorldBounds()
set maxX=R2I(GetRectMaxX(world))
set maxY=R2I(GetRectMaxY(world))
set minX=R2I(GetRectMinX(world))
set minY=R2I(GetRectMinY(world))
set centerX=R2I((maxX+minX)/2)
set centerY=R2I((minY+maxY)/2)
set worldRegion=CreateRegion()
call RegionAddRect(worldRegion,world)
endmethod
endmodule
struct WorldBounds extends array
readonly static integer maxX
readonly static integer maxY
readonly static integer minX
readonly static integer minY
readonly static integer centerX
readonly static integer centerY
readonly static rect world
readonly static region worldRegion
implement WorldBoundInit
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
library Event
//2.0.1.1
/////////////////////////////////////////////////////////////////////////
//function CreateEvent takes nothing returns integer
//function TriggerRegisterEvent takes trigger t, integer ev returns nothing
//function RegisterEvent takes boolexpr c, integer ev returns nothing
//function FireEvent takes integer ev returns nothing
//function FireEventData takes Event ev, integer data returns nothing
//function GetEventData takes nothing returns integer
//function SetEventData takes integer data returns nothing
//struct Event extends array
//static integer data
//static method create takes nothing returns thistype
//method registerTrigger takes trigger t returns nothing
//method register takes boolexpr c returns nothing
//method fire takes nothing returns nothing
//method fireData takes integer data returns nothing
/////////////////////////////////////////////////////////////////////////
globals
private real eventv = 0
endglobals
struct Event extends array
private static integer count = 0
private static trigger array trig
static integer data = 0
static method create takes nothing returns thistype
set count = count + 1
set trig[count] = CreateTrigger()
return count
endmethod
method registerTrigger takes trigger t returns nothing
call TriggerRegisterVariableEvent(t, SCOPE_PRIVATE + "eventv", EQUAL, this)
endmethod
method register takes boolexpr c returns nothing
call TriggerAddCondition(trig[this], c)
endmethod
method fire takes nothing returns nothing
set eventv = 0
set eventv = this
call TriggerEvaluate(trig[this])
endmethod
method fireData takes integer data returns nothing
local integer prev = thistype.data
set thistype.data = data
set eventv = 0
set eventv = this
call TriggerEvaluate(trig[this])
set thistype.data = prev
endmethod
endstruct
function CreateEvent takes nothing returns Event
return Event.create()
endfunction
function TriggerRegisterEvent takes trigger t, Event ev returns nothing
call ev.registerTrigger(t)
endfunction
function RegisterEvent takes boolexpr c, Event ev returns nothing
call ev.register(c)
endfunction
function FireEvent takes Event ev returns nothing
call ev.fire()
endfunction
function FireEventData takes Event ev, integer data returns nothing
call ev.fireData(data)
endfunction
function GetEventData takes nothing returns integer
return Event.data
endfunction
function SetEventData takes integer data returns nothing
set Event.data = data
endfunction
endlibrary
//TESH.scrollpos=247
//TESH.alwaysfold=0
library HunterInstinct requires UnitIndexer, TimerUtils
//-------------------------------------------------------------------------------------------
// Hunter's Instinct
//
// by Dr.Killer
//
// The Warden uses her unatural instincts to locate wounded enemies.
// Then, she gains bonus movement speed and the ability to become invisible.
// She inflicts bonus damage upon her victim based on her distance from her
// farthest target at the time of casting.
//-------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------
// Configuration
//-------------------------------------------------------------------------------------------
globals
private constant integer MAIN_SPELL_ID = 'A001' //Raw code of spell
private constant integer INVIS_ID_1 = 'A003' //Raw codes for wind walk abilities (3 levels)
private constant integer INVIS_ID_2 = 'A004'
private constant integer INVIS_ID_3 = 'A005'
private constant integer INVIS_BUFF_ID = 'B001' //Wind walk buff raw code
private constant real RANGE = 5100. //The radius in which targets are picked
private constant real DIST_1 = 1700. //If Warden's distance to the farthest target is lower than this, a level 1 windwalk will be added
private constant real DIST_2 = 2400. //Same as above, just a level 2 windwalk will be added.
private constant real DURATION = 15. //Spell duration
private constant integer ARRAY_SIZE = 90 //Determines the number of spells which can be run simultaneously by different units
private constant real UNIT_MAX_SPEED = 522. //Unit's maximum speed in your map (neccessary)
private constant real WINDWALK_REMOVE_INTERVAL = 1. //The delay before Windwalk is removed from Warden's abilities, after a Windwalk attack
private group tempGroup = CreateGroup() //A group used for enumerationa and stuff.
endglobals
private function HpThreshold takes integer lvl returns real //Enemies whose hit points are below this percent, will be added to targets group
return (10.+10.*I2R(lvl))/100.
endfunction
private function SpeedBonus takes integer lvl returns real //Amount of bonus speed given to Warden upon casting (in percent)
return (20.+10.*I2R(lvl))/100.
endfunction
//-------------------------------------------------------------------------------------------
// End of Configuration
//-------------------------------------------------------------------------------------------
globals
private boolean array flag [ARRAY_SIZE][ARRAY_SIZE] //Each target unit has a flag to see if the corresponding spell' duration cast on him has ended. Makes it possible for this spell to be MUI.
private boolean array invis_flag //Checks whether the wind walk ability has been added to warden or not. Useful for preventing the Warden from getting more than 1 wind walk ability
endglobals
//-------------------------------------------------------------------------------------------
// Contains the onInit method
//-------------------------------------------------------------------------------------------
private module Init
private static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
local trigger t2 = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition(t, Condition(function thistype.onCast))
call TriggerRegisterAnyUnitEventBJ(t2, EVENT_PLAYER_UNIT_ATTACKED)
call TriggerAddCondition(t2, Condition(function thistype.onAttack))
call BJDebugMsg("asD")
set t = null
set t2 = null
endmethod
endmodule
//-------------------------------------------------------------------------------------------
// Spell's main struct, holds all of spell's properties for later use
//-------------------------------------------------------------------------------------------
private struct SpellData //Struct variable names should be pretty self-explanatory
private unit caster
private integer casterId
private player owner
private integer lvl
private group targets //A group which holds all the targets of the spell.
private timer main_timer
private real casterX
private real casterY
private real maxDist //To determine wind walk ability level, I store Warden's distance of the farthest target in this
private real speedBonus //Amount of speed bonus given to Warden (the real value which is actually added to her speed)
private boolean noTarget
//-------------------------------------------------------------------------------------------
// This method runs when spell duration ends; does cleanups, effect removal, etc.
//-------------------------------------------------------------------------------------------
private static method onFinish takes nothing returns nothing
local unit u = null //A temp unit used in some loops.
local SpellData object = GetTimerData(GetExpiredTimer()) //An instance of SpellData struct which holds all of spell's variables, such as caster, targets, etc.
local SpellData temp //Used in the Vision Management section. More info below.
local integer i = 0 //I & J are simple counters used in loops.
local integer j = 0
local integer target_id = 0 //UnitId of the currently picked target (More info below)
local boolean grant_vision = false //Determines whether shared vision of the currently picked should be denied or granted. Used in making this MUI.
local integer owner_id = GetPlayerId(object.owner)
//----------------------------------------------------------------------------------
// Falsing flag related to this spell. Pretty obvious. Prevents this spell
// from granting vision of targets any longer.
//----------------------------------------------------------------------------------
loop
set i=i+1
exitwhen i>ARRAY_SIZE
set flag[object][i] = false
endloop
set i=0
//----------------------------------------------------------------------------------
// Vision Management section. Here, I pick the targets one by one, check if
// they are under the effect of a same spell (of another source). If that's
// true, I see if the owning player of that spell is the same as owner of this
// one. If that's also true, vision of that target is granted to the corresponding
// player and the grant_vision boolean is set to true.
// If not, vision is denied because with no Hunter's Instinct active,
// there is no reason for them to have vision of enemy heroes.
// The 'temp' instance, is used to convey info of the other active spell to this
// method, so I can have access to its caster, owning player, etc.
//----------------------------------------------------------------------------------
loop
set u = FirstOfGroup(object.targets)
exitwhen u == null
set target_id = GetUnitId(u)
loop
set j=j+1
exitwhen (j>ARRAY_SIZE or grant_vision == true)
set temp = SpellData.create()
if (flag[j][target_id] == true) then
set temp = j
if (temp.owner == object.owner) then
set grant_vision = true
call UnitShareVision(u, object.owner, true)
endif
endif
call temp.destroy()
endloop
if (not grant_vision) then
call UnitShareVision(u, object.owner, false)
endif
call GroupRemoveUnit(object.targets, u)
set j=0
endloop
//----------------------------------------------------------------------------------
// Cleanup section. Removes leaks, nullifies handles, etc.
// I also set the invis_flag of the caster to false, cause she no longer
// possesses the wind walk ability and even if she is in the middle of one,
// the buff is removed, so she becomes visible.
// I also remove the speed bonus here.
//----------------------------------------------------------------------------------
set invis_flag[object.casterId] = false
call UnitRemoveAbility(object.caster, INVIS_ID_1)
call UnitRemoveAbility(object.caster, INVIS_ID_2)
call UnitRemoveAbility(object.caster, INVIS_ID_3)
call UnitRemoveAbility(object.caster, INVIS_BUFF_ID)
call SetUnitMoveSpeed(object.caster, GetUnitMoveSpeed(object.caster)-object.speedBonus)
call DestroyGroup(object.targets)
call PauseTimer(GetExpiredTimer())
call ReleaseTimer(GetExpiredTimer())
set object.caster = null
endmethod
//-------------------------------------------------------------------------------------------
// Core method; sets needed variables, runs the spell, etc.
//-------------------------------------------------------------------------------------------
private static method onCast takes nothing returns boolean
local SpellData object //Same as above, holds all needed variables for a spell.
local unit u = null //A temp unit used in loops.
local real dist = 0. //Warden's distance form the picked target. Mre info below.
local real finalSpeed = 0. //Warden's final speed, after applying the bonuses.
local real originalSpeed = 0. //Warden's original speed, before applying the bonuses.
local real x = 0.
local real y = 0.
if (GetSpellAbilityId() == MAIN_SPELL_ID) then
set object = SpellData.create()
set object.targets = CreateGroup()
set object.caster = GetTriggerUnit()
set object.casterId = GetUnitId(object.caster)
set object.owner = GetTriggerPlayer()
set object.casterX = GetUnitX(object.caster)
set object.casterY = GetUnitY(object.caster)
set object.lvl = GetUnitAbilityLevel(object.caster, MAIN_SPELL_ID)
call GroupEnumUnitsInRange(tempGroup, object.casterX, object.casterY, RANGE, null) //Aquires all potential targets. Invalid ones are filtered out below.
//----------------------------------------------------------------------------------
// Target Validation section. Throws non-hero and non-enemy units out. Also
// dissmissed units whose HP is more than the required threshold which differs
// for each level. Also sets required 'flag' slots to true. If a unit's flag
// is true, it means than they are under the effect of Hunter's Instinct.
// Besides, I store the farthest target's distance in maxDist variable, using
// some kind of Bubble Sort method to find it in the first place.
//----------------------------------------------------------------------------------
set object.noTarget = true
loop
set u = FirstOfGroup(tempGroup)
exitwhen u == null
if (IsUnitType(u, UNIT_TYPE_HERO) and (GetWidgetLife(u) <= (GetUnitState(u, UNIT_STATE_MAX_LIFE)*HpThreshold(object.lvl))) and IsUnitEnemy(u, object.owner)) then
call GroupAddUnit(object.targets, u)
set object.noTarget = false
//Finding the distance between caster and current target
set x = GetUnitX(u)
set y = GetUnitY(u)
set dist = (object.casterX-x)*(object.casterX-x) + (object.casterY-y)*(object.casterY-y)
if dist>(object.maxDist*object.maxDist) then
set object.maxDist = dist
endif
set dist = 0.
set flag[object][GetUnitId(u)] = true
else
set flag[object][GetUnitId(u)] = false
endif
call GroupRemoveUnit(tempGroup, u)
endloop
//----------------------------------------------------------------------------------
// This part adds the speed bonus. Also if the bonus causes Warden's speed
// to go beyond the maximum allowed speed, this corrects the bonus amount
// and removes the excess value.
//----------------------------------------------------------------------------------
if not object.noTarget then
set originalSpeed = GetUnitMoveSpeed(object.caster)
set object.speedBonus = originalSpeed*SpeedBonus(object.lvl)
set finalSpeed = object.speedBonus+originalSpeed
if (finalSpeed > UNIT_MAX_SPEED) then
set object.speedBonus = object.speedBonus - finalSpeed + UNIT_MAX_SPEED
set finalSpeed = UNIT_MAX_SPEED
call SetUnitMoveSpeed(object.caster, UNIT_MAX_SPEED)
else
call SetUnitMoveSpeed(object.caster, finalSpeed)
endif
endif
//----------------------------------------------------------------------------------
// Vision Management section, again. Picks each individual unit present
// in the 'targets' group and grants shared vision of that unit to
// the owning player of caster.
//----------------------------------------------------------------------------------
call GroupClear(tempGroup)
call GroupAddGroup(object.targets, tempGroup)
loop
set u = FirstOfGroup(tempGroup)
exitwhen u == null
call UnitShareVision(u, object.owner, true)
call GroupRemoveUnit(tempGroup, u)
endloop
//----------------------------------------------------------------------------------
// Here, I add the windwalk ability based on Warden's distance from her
// farthest target, which is stored in 'maxDist' variable.
//----------------------------------------------------------------------------------
if not invis_flag[object.casterId] then
if (object.maxDist <= DIST_1*DIST_1) then
call UnitAddAbility(object.caster, INVIS_ID_1)
elseif (object.maxDist <= DIST_2*DIST_2) then
call UnitAddAbility(object.caster, INVIS_ID_2)
else
call UnitAddAbility(object.caster, INVIS_ID_3)
endif
endif
set invis_flag[object.casterId] = true
//----------------------------------------------------------------------------------
// Fires a timer to call the onFinish method at the end of spell duration.
//----------------------------------------------------------------------------------
set object.main_timer = NewTimer()
call SetTimerData(object.main_timer, object)
call TimerStart(object.main_timer, DURATION, true, function thistype.onFinish)
set u = null
endif
return false
endmethod
//-------------------------------------------------------------------------------------------
// Removes the Windwalk ability after an attack
//-------------------------------------------------------------------------------------------
private static method removeWindwalk takes nothing returns nothing
local timer t = GetExpiredTimer()
local unit u = GetUnitById(GetTimerData(t))
call UnitRemoveAbility(u, INVIS_ID_1)
call UnitRemoveAbility(u, INVIS_ID_2)
call UnitRemoveAbility(u, INVIS_ID_3)
set u = null
call PauseTimer(t)
call ReleaseTimer(t)
set t = null
endmethod
//-------------------------------------------------------------------------------------------
// Fires a timer at the end of which Windwalk is removed
//-------------------------------------------------------------------------------------------
private static method onAttack takes nothing returns boolean
local timer t
local unit attacker = GetAttacker()
if (GetUnitAbilityLevel(attacker, INVIS_BUFF_ID)>0) then
set t = NewTimer()
call SetTimerData(t, GetUnitId(attacker))
call TimerStart(t, WINDWALK_REMOVE_INTERVAL, false, function thistype.removeWindwalk)
endif
set t = null
set attacker = null
return false
endmethod
//-------------------------------------------------------------------------------------------
// Self-explanatory
//-------------------------------------------------------------------------------------------
implement Init
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
call ClearTextMessages()
endfunction
//===========================================================================
function InitTrig_Test_Map takes nothing returns nothing
set gg_trg_Test_Map = CreateTrigger( )
call TriggerRegisterPlayerChatEvent( gg_trg_Test_Map, Player(0), "-clear", true )
call TriggerAddAction( gg_trg_Test_Map, function Trig_Untitled_Trigger_001_Actions )
endfunction