- Joined
- Apr 27, 2011
- Messages
- 272
JASS:
library DummyCasterUtils // v.1.0.3
//===========================================================================
// DummyCasterUtils by Alain.Mark
//
// -Info-
// -This library's purpose is to make dummycasting easier.
//
// -Pros and Cons-
// -Pros-
// -small code
// -only uses a minimal amount of if's
// -only uses one dummy caster when casting a particular instant spell on multiple targets
// at once
// -uses a straight forward but safe auto-deallocation system for dummy casters that does
// not use any timers or any condition checks (therefore fast :D)
//
// -Cons-
// -there's a little restriction in the order of casting spells but it's not that
// bothersome: ALL INSTANT SPELLS MUST BE CASTED FIRST BEFORE CHANNELING SPELLS IF THEY ARE INSIDE A SINGLE FUNCTION.
//
// -API-
// -function AllocateCaster takes player pl returns nothing
// -allocates a dummy caster for use.
//
// -function CastOnTargetLvl takes unit u1, unit u2, integer id, integer oid, integer lvl returns nothing
// -cast a single target spell on a single target.
//
// -function CastOnAreaLvl takes unit u1, real x, real y, real r, integer id, integer oid, boolean v, integer lvl returns nothing
// -cast a single target spell on multiple targets within an area. one good feature of this function is that
// it only uses one dummy caster if the spell was instant, and will only allocate extra dummy casters for channeled spells.
//
// -function CastOnPointLvl takes unit u1, real x, real y, real r, real a, integer id, integer oid, integer lvl returns nothing
// -cast a point target spell. this uses the "Polar Coordinate System", x & y are the coordinates of the spell origin.
// "r" is the radius(range) while "a" is the polar angle. "http://en.wikipedia.org/wiki/Polar_coordinate_system"
//
// -function CastPointBlankLvl takes unit u1, real x, real y, integer id, integer oid, integer lvl returns nothing
// -cast an instant spell.
//
//===========================================================================
//=======================================================================
// Configurable Stuff
//=======================================================================
globals
private integer DUMMY_CASTER_ID = 'n000' // the id of your dummy caster
private player DEFAULT_OWNER = Player(15) // default owner of the dummy after it was released
endglobals
private function CastOnAreaLvl_CHECK takes unit u returns boolean
// i made this configurable so you can add your own touch for CastOnAreaLvl function's filter
// note: keep this function a one-liner so it can be inlined.
return not IsUnitType(u,UNIT_TYPE_DEAD)
endfunction
//=======================================================================
// dummy caster allocator & deallocator
//=======================================================================
globals
private integer w = 0
private unit cs
private unit array rcs
private trigger DEALLOCATOR=CreateTrigger()
endglobals
//=======================================================================
function AllocateCaster takes player pl returns unit
if(w==0)then
set cs=CreateUnit(pl,DUMMY_CASTER_ID,0,0,0)
call TriggerRegisterUnitEvent(DEALLOCATOR,cs,EVENT_UNIT_SPELL_ENDCAST)
else
set w=w-1
set cs=rcs[w]
call SetUnitOwner(cs,pl,false)
endif
return cs
endfunction
//=======================================================================
private function DeallocationProcess takes nothing returns boolean
set cs=GetTriggerUnit()
set rcs[w]=cs
set w=w+1
call UnitRemoveAbility(cs,GetSpellAbilityId())
call SetUnitOwner(cs,DEFAULT_OWNER,false)
return false
endfunction
//=======================================================================
private module moltres
private static method onInit takes nothing returns nothing
call TriggerAddCondition(DEALLOCATOR,Condition(function DeallocationProcess))
endmethod
endmodule
//=======================================================================
// support function for CastOnAreaLvl...
//=======================================================================
globals
private group g=CreateGroup()
endglobals
//=======================================================================
private function CreateTargetGroup takes real x, real y, real r returns nothing
call GroupEnumUnitsInRange(g,x,y,r,null)
endfunction
//=======================================================================
//! textmacro DummyCasterUtils__AddSpellLeveled takes UNIT, ID, LVL
call UnitAddAbility($UNIT$,$ID$)
call SetUnitAbilityLevel($UNIT$,$ID$,$LVL$)
//! endtextmacro
//=======================================================================
//! textmacro DummyCasterUtils__SetUnitXY takes UNIT, X, Y
call SetUnitX($UNIT$,$X$)
call SetUnitY($UNIT$,$Y$)
//! endtextmacro
//=======================================================================
//===========================================================================
function CastOnTargetLvl takes unit u1, unit u2, integer id, integer oid, integer lvl returns nothing
//! runtextmacro DummyCasterUtils__AddSpellLeveled("u1","id","lvl")
//! runtextmacro DummyCasterUtils__SetUnitXY("u1","GetUnitX(u2)","GetUnitY(u2)")
call IssueTargetOrderById(u1,oid,u2)
endfunction
//=======================================================================
function CastOnTarget takes unit u1, unit u2, integer id, integer oid returns nothing
call CastOnTargetLvl(u1,u2,id,oid,1)
endfunction
//=======================================================================
function CastOnAreaLvl takes unit u1, real x, real y, real r, integer id, integer oid, boolean v, integer lvl returns nothing
local player pl=GetOwningPlayer(u1)
set rcs[w]=u1
set w=w+1
call SetUnitOwner(u1,DEFAULT_OWNER,false)
call CreateTargetGroup(x,y,r)
loop
set u1=FirstOfGroup(g)
exitwhen u1==null
exitwhen not CastOnAreaLvl_CHECK(u1)
if((IsUnitAlly(u1,pl) and v) or (IsUnitEnemy(u1,pl) and not v))then
call CastOnTargetLvl(AllocateCaster(pl),u1,id,oid,lvl)
endif
call GroupRemoveUnit(g,u1)
endloop
endfunction
//=======================================================================
function CastOnArea takes unit u1, real x, real y, real r, integer id, integer oid, boolean v returns nothing
call CastOnAreaLvl(u1,x,y,r,id,oid,v,1)
endfunction
//=======================================================================
function CastOnPointLvl takes unit u1, real x, real y, real r, real a, integer id, integer oid, integer lvl returns nothing
//! runtextmacro DummyCasterUtils__AddSpellLeveled("u1","id","lvl")
//! runtextmacro DummyCasterUtils__SetUnitXY("u1","x","y")
call IssuePointOrderById(u1,oid,Cos(a)*r,Sin(a)*r)
endfunction
//=======================================================================
function CastOnPoint takes unit u1, real x, real y, real r, real a, integer id, integer oid returns nothing
call CastOnPointLvl(u1,x,y,r,a,id,oid,1)
endfunction
//=======================================================================
function CastPointBlankLvl takes unit u1, real x, real y, integer id, integer oid, integer lvl returns nothing
//! runtextmacro DummyCasterUtils__AddSpellLeveled("u1","id","lvl")
//! runtextmacro DummyCasterUtils__SetUnitXY("u1","x","y")
call IssueImmediateOrderById(u1,oid)
endfunction
//=======================================================================
function CastPointBlank takes unit u1, real x, real y, integer id, integer oid returns nothing
call CastPointBlankLvl(u1,x,y,id,oid,1)
endfunction
//===========================================================================
private struct s extends array
implement moltres
endstruct
//===========================================================================
endlibrary
Last edited: