- Joined
- Dec 4, 2006
- Messages
- 110
UnitInRangeOfType
Version 1.03
Version 1.03
Requirements
- JASS NewGen
- Event
- AIDS
- Table
JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ UnitInRangeOfType ~~ By Sevion ~~ Version 1.03 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// What is UnitInRangeOfType?
// - UnitInRangeOfType implements an intuitive method of registering InRange events for unit-types.
//
// =Pros=
// - Blizzard-like Event Registration.
// - Very intuitive to use.
//
// =Cons=
// - Fairly inefficient at the moment, but this isn't designed for heavy use right now anyhow.
//
// Functions:
// - TriggerRegisterUnitInRangeOfType( trigger whichTrigger, integer whichType, real range, boolexpr filter )
//
// This function is used exactly as other event registration functions are. Returns an Event.
//
// - GetCenterUnit()
//
// - This function is used just as GetEnteringUnit is used. It's for retrieving the unit that was approached.
// This can be used in the filter function as well.
//
// Details:
// - UnitInRangeOfType is used just as other Blizzard event registration functions are.
//
// - Very intuitive to use because of this fact.
//
// - I am still in the process of optimization, which should come in later updates.
//
// How to import:
// - Create a trigger named UnitInRangeOfType.
// - Convert it to custom text and replace the whole trigger text with this.
//
// Thanks:
// - Bribe for some optimization suggestions.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library UnitInRangeOfType requires AIDS, Event, Table
globals
private key EVENT_KEY
private key TRIGGER_KEY
private key RANGE_KEY
private key TYPE_KEY
private key UNIT_KEY
private key DESTROY_KEY
endglobals
private struct data extends array
readonly static trigger filterCheck
public static hashtable hash
readonly static unit lastcenter
public static Table idlist
private static method AIDS_filter takes unit u returns boolean
return thistype.idlist.has(GetUnitTypeId(u))
endmethod
private static method OnEvent takes nothing returns boolean
local unit u
set u = thistype.lastcenter
set thistype.lastcenter = LoadUnitHandle(thistype.hash, GetHandleId(GetTriggeringTrigger()), UNIT_KEY)
if ( TriggerEvaluate(data.filterCheck) ) then
call FireEvent(Event(LoadInteger(data.hash, LoadInteger(thistype.hash, GetHandleId(GetTriggeringTrigger()), TYPE_KEY), EVENT_KEY)))
else
set thistype.lastcenter = u
endif
set u = null
return false
endmethod
private method AIDS_onCreate takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = GetUnitTypeId(this.unit)
call TriggerRegisterUnitInRange(t, this.unit, LoadReal(thistype.hash, i, RANGE_KEY), null)
call TriggerAddCondition(t, Condition(function thistype.OnEvent))
call SaveInteger(thistype.hash, GetHandleId(t), TYPE_KEY, i)
call SaveUnitHandle(thistype.hash, GetHandleId(t), UNIT_KEY, this.unit)
call SaveTriggerHandle(thistype.hash, i, DESTROY_KEY, t)
set t = null
endmethod
private static method AIDS_onInit takes nothing returns nothing
set thistype.hash = InitHashtable()
set thistype.lastcenter = null
set thistype.filterCheck = CreateTrigger()
set thistype.idlist = Table.create()
endmethod
private method AIDS_onDestroy takes nothing returns nothing
call DestroyTrigger(LoadTriggerHandle(thistype.hash, GetHandleId(this.unit), DESTROY_KEY))
endmethod
//! runtextmacro AIDS()
endstruct
function GetCenterUnit takes nothing returns unit
return data.lastcenter
endfunction
function TriggerRegisterUnitInRangeOfType takes trigger whichTrigger, integer whichType, real range, boolexpr filter returns Event
local Event e
if ( HaveSavedInteger(data.hash, whichType, EVENT_KEY) ) then
set e = LoadInteger(data.hash, whichType, EVENT_KEY)
else
set e = Event.create()
call SaveInteger(data.hash, whichType, EVENT_KEY, e)
endif
call TriggerRegisterEvent(whichTrigger, e)
set data.idlist[whichType] = 1
call SaveTriggerHandle(data.hash, whichType, TRIGGER_KEY, whichTrigger)
call SaveReal(data.hash, whichType, RANGE_KEY, range)
call TriggerAddCondition(data.filterCheck, filter)
return e
endfunction
endlibrary
JASS:
struct a extends array
private static unit u
implement Alloc
private static method filt2 takes nothing returns boolean
call BJDebugMsg("Filter 2")
return true
endmethod
private static method filt takes nothing returns boolean
call BJDebugMsg("Filter")
return true
endmethod
private static method act takes nothing returns nothing
call BJDebugMsg("Act")
call KillUnit(GetEnteringUnit()) // Kills the Paladin
//call KillUnit(GetCenterUnit()) // Kills the Footman
endmethod
private static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
set thistype.u = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
call CreateUnit(Player(0), 'Hpal', 550, 0, 0)
call CreateUnit(Player(0), 'Hpal', -550, 0, 0)
call TriggerRegisterUnitInRangeOfType(t, 'hfoo', 500, Condition(function thistype.filt))
call TriggerRegisterUnitInRangeOfType(t, 'hfoo', 500, Condition(function thistype.filt2))
call TriggerAddAction(t, function thistype.act)
endmethod
endstruct
Still needs optimization work, but works nonetheless
Updates
- Version 1.03: Removed old code that was causing syntax errors.
- Version 1.02: Removed idlist, filterlist, and Alloc. Added Table and a static trigger to replace them.
- Version 1.01: Moved
//! runtextmacro AIDS()
to the bottom of the struct. Nulled some things.- Version 1.00: Release.
Last edited: