- Joined
- Apr 30, 2011
- Messages
- 359
JASS:
//========================================================================================
//
// Dummy Cast
// -*- overcold_ice -*-
//
// -[*] Requirements:
// - JNGP
// - latest version of JassHelper
//
// For your dummy-casting needs
// Provides instant casting and normal casting functions.
//
// -[*] API:
//
// constant integer DUMMY_CAST_TARGET_NONE = 0
// constant integer DUMMY_CAST_TARGET_POINT = 1
// constant integer DUMMY_CAST_TARGET_UNIT = 2
//
// struct DummyCast extends array
//
// player owner // the owner of the dummy unit
// real x, y // the position of the dummy unit
// integer AbilId, AbilLv // the ability that will be casted
// string order // the ability's cast order
// boolean instant // true => instant cast (no casting time)
// // false => normal cast (casting time included)
//
// integer target // the targeting type of the spell
// // can be non-target, point-target, unit-target
// // (use constants above, or just their values)
// real tX, tY // location coordinates for point-target spell
// unit tU // target unit for unit-target spell
//
// static method allocate takes nothing returns DummyCast
// method deallocate takes nothing returns nothing
// creator/destructor of the struct
//
// method cast takes nothing returns boolean
// cast the spell
//
// method castRect takes rect r returns boolean
// cast the spell (on random locations in rect r). if the spell is unit
// targeting, casts the spell on all units in the rect
// method castRectUnit takes rect r, boolexpr f returns boolean
// cast the spell (positioned/targeted) on every units in rect r that
// pass filter f
//
// method castCircle takes real x, real y, real r returns boolean
// cast the spell (on random locations in a circle with center [x, y] and
// radius r). if the spell is unit targeting, casts the spell on all units
// in the circle
// method castCircleUnit takes real x, real y, real r, boolexpr f returns boolean
// cast the spell (positioned/targeted) on every units in a circle with
// center [x, y] and radius r that pass filter f
//
// they all returns true if at least 1 dummy caster casts the spell successfully
//
//========================================================================================
library DummyCast
globals
//================================================================================
// C A L I B R A T I O N S E C T I O N
//================================================================================
private constant player DEFAULT_DUMMY_OWNER = Player(15) // default owner
private constant boolean DEFAULT_DUMMY_CAST = false // default for instant casting
// true => instant cast
// false => normal cast
private constant integer DUMMY_CASTER_ID = 'dumy'
//================================================================================
constant integer DUMMY_CAST_TARGET_NONE = 0
constant integer DUMMY_CAST_TARGET_POINT = 1
constant integer DUMMY_CAST_TARGET_UNIT = 2
endglobals
private module InitDummyCast
private static method onInit takes nothing returns nothing
set .instantCaster = CreateUnit(DEFAULT_DUMMY_OWNER, DUMMY_CASTER_ID, 0, 0, 0)
call TriggerAddCondition(.t, Condition(function thistype.onEndCast))
endmethod
endmodule
struct DummyCast extends array
player owner
real x
real y
integer abilId
integer abilLv
string order
boolean instant
integer target
real tX
real tY
unit tU
private thistype r
private static integer c = 0
static method allocate takes nothing returns thistype
local thistype this = thistype(0).r
if this == 0 then
set .c = .c + 1
set this = .c
else
set thistype(0).r = this.r
endif
return this
endmethod
method deallocate takes nothing returns nothing
set this.r = thistype(0).r
set thistype(0).r = this
set this.owner = DEFAULT_DUMMY_OWNER
set this.x = 0
set this.y = 0
set this.abilId = 0
set this.abilLv = 1
set this.order = ""
set this.instant = DEFAULT_DUMMY_CAST
set this.tX = 0
set this.tY = 0
set this.tU = null
endmethod
private static unit instantCaster
private static group g = CreateGroup()
private static trigger t = CreateTrigger()
private static thistype array i [8190]
private method getCaster takes nothing returns unit
local unit u = FirstOfGroup(.g)
if u == null then
set u = CreateUnit(this.owner, DUMMY_CASTER_ID, this.x, this.y, 0)
call TriggerRegisterUnitEvent(.t, u, EVENT_UNIT_SPELL_ENDCAST)
else
call GroupRemoveUnit(.g, u)
endif
return u
endmethod
private static method onEndCast takes nothing returns boolean
local unit u = GetTriggerUnit()
call GroupAddUnit(.g, u)
call UnitRemoveAbility(u, .i [GetHandleId(u)].abilId)
set u = null
return false
endmethod
implement InitDummyCast
//================================================================================
// A P I
//================================================================================
private static group eg = CreateGroup()
method cast takes nothing returns boolean
local unit u
local boolean b
if instant then
set u = .instantCaster
call SetUnitX(u, this.x)
call SetUnitY(u, this.y)
else
set u = this.getCaster()
set .i [GetHandleId(u)] = this
endif
call UnitAddAbility(u, this.abilId)
call SetUnitAbilityLevel(u, this.abilId, this.abilLv)
if this.target == 0 then
set b = IssueImmediateOrder(u, this.order)
elseif this.target == 1 then
set b = IssuePointOrder(u, this.order, this.tX, this.tY)
else
set b = IssueTargetOrder(u, this.order, this.tU)
endif
if instant then
call UnitRemoveAbility(u, this.abilId)
endif
set u = null
return b
endmethod
//! textmacro DummyCast___OnUnitCastBasic
loop
set u = FirstOfGroup(.eg)
exitwhen u == null
call GroupRemoveUnit(.eg, u)
if this.target == 0 then
set tx = this.x
set ty = this.y
set this.x = GetUnitX(u)
set this.y = GetUnitY(u)
elseif this.target == 1 then
set tx = this.tX
set ty = this.tY
set this.tX = GetUnitX(u)
set this.tY = GetUnitY(u)
else
set tu = this.tU
set this.tU = u
endif
if not b then
set b = this.cast()
else
call this.cast()
endif
if this.target == 0 then
set this.x = tx
set this.y = ty
elseif this.target == 1 then
set this.tX = tx
set this.tY = ty
else
set this.tU = tu
endif
endloop
//! endtextmacro
//! textmacro DummyCast___OnLocCastBasic
if this.target == 0 then
set tx = this.x
set ty = this.y
set this.x = xc
set this.y = yc
set b = this.cast()
set this.x = tx
set this.y = ty
else
set tx = this.tX
set ty = this.tY
set this.tX = xc
set this.tY = yc
set b = this.cast()
set this.tX = tx
set this.tY = ty
endif
//! endtextmacro
method castRectUnit takes rect r, boolexpr f returns boolean
local unit u
local boolean b
local real tx
local real ty
local unit tu
call GroupEnumUnitsInRect(.eg, r, f)
//! runtextmacro DummyCast___OnUnitCastBasic()
call GroupClear(.eg)
set tu = null
return b
endmethod
method castRect takes rect r returns boolean
local real xc = GetRandomReal(GetRectMinX(r), GetRectMaxX(r))
local real yc = GetRandomReal(GetRectMinY(r), GetRectMaxY(r))
local real tx
local real ty
local boolean b
if this.target == 2 then
return this.castRectUnit(r, null)
endif
//! runtextmacro DummyCast___OnLocCastBasic()
return b
endmethod
method castCircleUnit takes real x, real y, real r, boolexpr f returns boolean
local unit u
local boolean b
local real tx
local real ty
local unit tu
call GroupEnumUnitsInRange(.eg, x, y, r, f)
//! runtextmacro DummyCast___OnUnitCastBasic()
set tu = null
return b
endmethod
method castCircle takes real x, real y, real r returns boolean
local real rr = GetRandomReal(0, r)
local real ra = GetRandomReal(0, 6,28)
local real xc = x + rr * Cos(ra)
local real yc = y + rr * Sin(ra)
local real tx
local real ty
local boolean b
if this.target == 2 then
return this.castCircleUnit(x, y, r, null)
endif
//! runtextmacro DummyCast___OnLocCastBasic()
return b
endmethod
//================================================================================
endstruct
endlibrary
Last edited: