//TESH.scrollpos=0
//TESH.alwaysfold=0
Name | Type | is_array | initial_value |
Loc | location | No | |
Swash | unit | No |
//TESH.scrollpos=0
//TESH.alwaysfold=0
library Missile /* version 2.5.1
*************************************************************************************
*
* Creating custom projectiles in Warcraft III.
*
* Major goal:
* No unessary external requirements.
* Implements code optional.
*
* Philosophy:
* I want that feature --> Compiler writes that code into your map script.
* I don't want that --> Compiler ignores that code completly.
*
* Important:
* Take yourself 2 minutes time to setup Missile correctly.
* Otherwise I can't guarantee, that Missile works the way you want.
* Once the setup is done, you can check out some examples and Missile will be easy
* to use for everyone. I promise it.
*
* Do the setup at:
*
* 1.) Import instruction
* 2.) Global configuration
* 3.) Function configuration
*
* Credits to Dirac, emjlr3, AceHart, Bribe, Wietlol,
* Nestharus, Maghteridon96, Vexorian and Zwiebelchen.
*
*************************************************************************************
*
* */ requires /*
*
* - Missile requires nothing.
*
*************************************************************************************
*
* Optional requirements listed can reduce overall code generation,
* add safety mechanisms, decrease overhead and optimize handle management.
* For a better overview I put them into blocks.
*
* I recommend to use at least one per block in your map.
*
* a) For best debug results: ( Useful )
* */ optional ErrorMessage /* github.com/nestharus/JASS/tree/master/jass/Systems/ErrorMessage
*
* b) Fatal error protection: ( Case: unit out moves of world bounds )
* - WorldBounds is safer than BoundSentinel.
* - WorldBounds adds more overhead than BoundSentinel.
* - Using none of these two forces Missile to switch from SetUnitX/Y to the SetUnitPosition native.
* */ optional WorldBounds /* githubusercontent.com/nestharus/JASS/master/jass/Systems/WorldBounds/script.j
* */ optional BoundSentinel /* wc3c.net/showthread.php?t=102576
*
* c) Handle recycling: ( Performace gain, memory management )
* - uses MissileRecylcer > Dummy > xedummy.
* */ optional MissileRecycler /* hiveworkshop.com/forums/jass-resources-412/system-missilerecycler-206086/
* */ optional Dummy /* github.com/nestharus/JASS/blob/master/jass/Systems/Dummy/Dummy.w3x
* */ optional xedummy /* wc3c.net/showthread.php?t=101150
*
* d) Unit indexing: ( Avoid an onIndex event )
* - not required for Missile. Only if you use one already.
* */ optional UnitIndexer /* github.com/nestharus/JASS/tree/master/jass/Systems/Unit%20Indexer
*
************************************************************************************
*
* 1. Import instruction
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* • Copy Missile into to your map.
* • You need a dummy unit, using Vexorians "dummy.mdx".
* This unit must use the locust and crow form ability. ( Aloc & Amrf )
* ¯¯¯¯
*
* 2. Global configuration
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* Seven constants to setup!
*/
globals
/**
* Missiles are moved periodically. An interval of 1./32. is recommended.
* • Too short timeout intervals may cause performance issues.
* • Too large timeout intervals may look fishy.
*/
public constant real TIMER_TIMEOUT = 1./32.
/**
* Owner of all Missile dummies. Should be a neutral player in your map.
*/
public constant player NEUTRAL_PASSIVE = Player(15)
/**
* Raw code of the dummy unit. Object Editor ( F6 )
* • Must be correct, otherwise missile dummies can neither be recycled nor destroyed.
* • Units of other type ids will not be thrown into the recycler bin.
*/
public constant integer DUMMY_UNIT_ID = 'dumi'
/**
* The maximum collision size used in your map. If unsure use 197. ( Town hall collision )
* • Applies for all types of widgets.
* • A precise value can improve Missile's performance,
* since smaller values enumerate less widgtes per loop per missile.
*/
public constant real MAXIMUM_COLLISION_SIZE = 197.
/**
* Collision types for missiles. ( Documentation only )
* Missile decides internally each loop which type of collision is required.
* • Uses circular collision dectection for speed < collision. ( Good accuracy, best performance )
* • Uses rectangle collision for speed >= collision. ( Best accuracy, normal performance )
*/
public constant integer COLLISION_TYPE_CIRCLE = 0
public constant integer COLLISION_TYPE_RECTANGLE = 1
/**
* Determine when rectangle collision is required to detect nearby widgets.
* • The smaller the factor the earlier rectangle collision is used. ( by default 1. )
* • Formula: speed >= collision*Missile_COLLISION_ACCURACY_FACTOR
*/
public constant real COLLISION_ACCURACY_FACTOR = 1.
// Optional toogles:
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
// Set booleans listed below to "true" and the compiler will write
// the feature into your map. Otherwise this code is completly ignored.
// • Yay, I want that --> "true"
// • Naah that's useless for me --> "false"
/**
* USE_COLLISION_Z_FILTER enables z axis checks for widget collision. ( Adds minimal overhead )
* Use it when you need:
* • Missiles flying over or under widgets.
* • Determine between flying and walking units.
*/
public constant boolean USE_COLLISION_Z_FILTER = true
/**
* WRITE_DELAYED_MISSILE_RECYCLING enables a delayed dummy recycling system. ( Very recommended )
* Use it if:
* • You use a dummy recycling library like MissileRecycler, Dummy or xedummy.
* • You want to properly display death animations of effects added to missiles.
*/
public constant boolean WRITE_DELAYED_MISSILE_RECYCLING = true
/**
* DELAYED_MISSILE_DEATH_ANIMATION_TIME is the delay in seconds
* Missile holds back a dummy, before recycling it.
* • The time value does not have to be precise.
* • Requires WRITE_DELAYED_MISSILE_RECYCLING = true
*/
private constant real DELAYED_MISSILE_DEATH_ANIMATION_TIME = 2.
/**
* USE_DESTRUCTABLE_FILTER and USE_ITEM_FILTER are redundant constants from previous Missile versions.
* They do nothing, but remain for backwards compatibilty.
* From Missile version 1.5 on all widget collisions are always enabled.
*/
public constant boolean USE_DESTRUCTABLE_FILTER = true
public constant boolean USE_ITEM_FILTER = true
endglobals
/*
* 3. Function configuration
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* Four functions to setup!
*/
/**
* GetUnitBodySize(unit) returns a fictional value for z - axis collision.
* You have two options:
* • One constant value shared over all units.
* • Dynamic values based on handle id, type id, unit user data, scaling or other parameters.
*/
function GetUnitBodySize takes unit whichUnit returns real
return 100.// Other example: return LoadReal(hash, GetHandleId(whichUnit), KEY_UNIT_BODY_SIZE)
endfunction
/**
* Same as GetUnitBodySize, but for destructables.
* Using occluder height is an idea of mine. Of course you can use your own values.
*/
function GetDestructableHeight takes destructable d returns real
return GetDestructableOccluderHeight(d)// Other example: return 100.
endfunction
/**
* Same as GetUnitBodySize, but for items.
* Again it's up to you to figure out a fictional item height.
*/
function GetItemHeight takes item i returns real
return 16.
endfunction
/**
* Unit indexers and missiles ( Only if you don't use a dummy recycling library )
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* It is most likely intended that projectiles don't run through a unit indexing process.
* ToogleUnitIndexer runs:
* • Directly before a dummy is created.
* • Directly after dummy unit creation.
*
* Please return the previous setup of your indexing tool ( enabled, disabled ),
* so Missile can properly reset it to the original state.
*/
private function ToogleUnitIndexer takes boolean enable returns boolean
local boolean prev = true//UnitIndexer.enabled
// set UnitIndexer.enabled = enable
return prev
endfunction
/**
* 4. API
* ¯¯¯¯¯¯
*/
//! novjass ( Disables the compiler until the next endnovjass )
// Custom type Missile for your projectile needs.
struct Missile extends array
// Constants:
// ==========
//
readonly static constant string ORIGIN = "origin"
// • Attach point name for fxs on dummies.
readonly static constant real HIT_BOX = (2./3.)
// • Fictional hit box for homing missiles.
// while 0 would be the toe and 1 the head of a unit.
// Available creators:
// ===================
//
static method createEx takes unit missileDummy, real impactX, real impactY, real impactZ returns Missile
// • Core creator method.
// • May launches any unit.
// • Units of type Missile_DUMMY_UNIT_ID get recycled in the end.
static method create takes real x, real y, real z, real angleInRadians, real distanceToTravel, real endZ returns Missile
// • Converts arguments to fit into createEx, then calls createEx.
static method createXYZ takes real x, real y, real z, real impactX, real impactY, real impactZ returns Missile
// • Converts arguments to fit into createEx, then calls createEx.
// Available destructors:
// ======================
//
return true
// • Core destructor.
// • Returning true in any of the interface methods of the MissileStruct module
// will destroy that instance instantly.
method destroy takes nothing returns nothing
// • Destroys the missile during the next timer callback.
method terminate takes nothing returns nothing
// • Destroys the missile instantly.
// Fields you can set and read directly:
// =====================================
//
unit source
unit target // For homing missiles.
real distance // Distance traveled.
player owner // Pseudo owner for faster onCollide evaluation. The proper dummy owner remains PLAYER_NEUTRAL_PASSIVE.
real speed // Vector lenght for missile movement in plane x / y. ( DOES NOT TAKE THE TIMER TIMEOUT IN ACCOUNT )
real acceleration
real damage
real turn // Set a turn rate for missiles.
integer data // For data transfer set and read data.
boolean recycle // Is automatically set to true, when a Missile reaches it's destination.
boolean wantDestroy // Set wantDestroy to true, to destroy a missile during the next timer callback.
// Neither collision nor collisionZ accept values below zero.
real collision // Collision size in plane x / y.
real collisionZ // Collision size in z - axis. ( deprecated )
// Fields you can only read:
// =========================
//
readonly boolean allocated
readonly unit dummy// The dummy unit of this missile.
// Position members for you needs.
readonly MissilePosition origin// Grants access to readonly members of MissilePosition,
readonly MissilePosition impact// which are "x", "y", "z", "angle", "distance", "slope" and the pitch angle "alpha".
// Furthermore method origin.move(x, y, z) and impact.move(x, y, z).
readonly real terrainZ
readonly real x
readonly real y
readonly real z
readonly real angle// Current angle in radians.
// Method operators for set and read:
// ==================================
//
method operator model= takes string modelFile returns nothing
method operator model takes nothing returns string
// • Adds an effect handle on a missile dummy to it's "origin".
// • You can read the file path.
// • For multiple effects access "this.dummy" in your struct.
method operator scale= takes real value returns nothing
method operator scale takes nothing returns real
// • Set and read the scaling of the dummy unit.
method operator curve= takes real value returns nothing
method operator curve takes nothing returns real
// • Enables curved movement for your missile. ( Pass in radians, NOT degrees )
// • Do not pass in PI/2.
method operator arc= takes real value returns nothing
method operator arc takes nothing returns real
// • Enables arcing movement for your missile. ( Pass in radians, NOT degrees )
// • Do not pass in PI/2.
// Methods for missile movement:
// =============================
//
method bounce takes nothing returns nothing
// • Moves the MissilePosition "origin" to the current missile coordinates.
// • Resets the distance traveled to 0.
method deflect takes real tx, real ty returns nothing
// • Deflect the missile towards tx, ty. Then calls bounce.
method deflectEx takes real tx, real ty, real tz returns nothing
// • Deflect the missile towards tx, ty, tz. Then calls bounce.
method flightTime2Speed takes real duration returns nothing
// • Converts a fly time to a vector lenght for member "speed".
// • Does not take acceleration into account. ( Disclaimer )
method setMovementSpeed takes real value returns nothing
// • Converts Warcraft III movement speed to a vector lenght for member "speed".
// Methods for missile collision: ( all are hashtable entries )
// ==============================
// By default a widget can only be hit once per missile.
//
method hitWidget takes widget w returns nothing
// • Saves a widget in the memory as hit by this instance.
method hasHitWidget takes widget w returns boolean
// • Returns true, if "w" has been hit by this instance.
method removeHitWidget takes widget w returns nothing
// • Removes a widget from this missile's memory for widget collision. ( Can hit "w" again )
method flushHitWidgets takes nothing returns nothing
// • Flushes a missile's memory for widget collision. ( All widgets can be hit again )
method enableHitAfter takes widget w, real seconds returns nothing
// • Automatically calls removeHitWidget(w) after "seconds" time. ( Can hit "w" again after given time )
// Module MissileStruct:
// =====================
//
module MissileLaunch // ( optional )
module MissileStruct
// • Enables the entire missile interface for that struct.
// Interface in structs: ( Must implement module MissileStruct )
// =====================
//
// • Write one, many or all of the static method below into your struct.
// • Missiles launched in this struct will run those methods, when their events fire.
//
// • All of those static methods must return a boolean.
// a) return true --> destroys the missile instance instantly.
// b) return false --> keep on flying.
// Available static method:
// ========================
//
static method onCollide takes Missile missile, unit hit returns boolean
// • Runs for units in collision range of a missile.
static method onDestructable takes Missile missile, destructable hit returns boolean
// • Runs for destructables in collision range of a missile.
static method onItem takes Missile missile, item hit returns boolean
// • Runs for items in collision range of a missile.
static method onTerrain takes Missile missile returns boolean
// • Runs when a missile collides with the terrain. ( Ground and cliffs )
static method onFinish takes Missile missile returns boolean
// • Runs only when a missile reaches it's destination.
// • However does not run, if a Missile is destroyed in another method.
static method onPeriod takes Missile missile returns boolean
// • Runs every Missile_TIMER_TIMEOUT seconds.
static method onRemove takes Missile missile returns boolean
// • Runs when a missile is destroyed.
// • Unlike onFinish, onRemove will runs ALWAYS when a missile is destroyed!!!
//
// For onRemove the returned boolean has a unique meaning:
// • Return true will recycle this missile delayed. ( Only if WRITE_DELAYED_MISSILE_RECYCLING = true )
// • Return false will recycle this missile right away.
static method launch takes Missile toLaunch returns nothing
// • Well ... Launches this Missile.
// • Missile "toLaunch" will behave as declared in the struct. ( static methods from above )
// Misc: ( From the global setup )
// =====
//
// Constants:
// ==========
//
public constant real TIMER_TIMEOUT
public constant player NEUTRAL_PASSIVE
public constant integer DUMMY_UNIT_ID
public constant real MAXIMUM_COLLISION_SIZE
public constant boolean USE_COLLISION_Z_FILTER
public constant boolean WRITE_DELAYED_MISSILE_RECYCLING
public constant boolean USE_DESTRUCTABLE_FILTER
public constant boolean USE_ITEM_FILTER
readonly static constant string ORIGIN
readonly static constant real HIT_BOX
// Functions:
// ==========
//
public function GetLocZ takes real x, real y returns real
function GetUnitBodySize takes unit whichUnit returns real
function GetDestructableHeight takes destructable d returns real
function GetItemHeight takes item i returns real
//========================================================================
// Missile system. Make changes carefully.
//========================================================================
//! endnovjass ( Enables the compiler )
// Hello and welcome to Missile.
// I wrote a guideline for every piece of code inside Missile, so you
// can easily understand how the it gets compiled and evaluated.
//
// Let's go!
globals
// Core constant handle variables of Missile.
private constant trigger CORE = CreateTrigger()
private constant trigger MOVE = CreateTrigger()
private constant timer TMR = CreateTimer()
private constant location LOC = Location(0., 0.)
private constant rect RECT = Rect(0., 0., 0., 0.)
private constant group GROUP = CreateGroup()
private constant hashtable HASH = InitHashtable()
// For starting and stopping the timer.
private integer active = 0
// Arrays for data structure.
private integer array instances
private Missile array missileList
private boolexpr array expression
private triggercondition array condition
private integer array remove
private boolean array destroying
private boolean array recycling
private integer array nextNode
private integer array prevNode
// Internal widget filter functions.
private boolexpr destFilter
private boolexpr itemFilter
private boolexpr unitFilter
endglobals
public function GetLocZ takes real x, real y returns real
call MoveLocation(LOC, x, y)
return GetLocationZ(LOC)
endfunction
// For WRITE_DELAYED_MISSILE_RECYCLING = true Missile will hold back
// dummies for DELAYED_MISSILE_DEATH_ANIMATION_TIME before they are recylced. ( Code placed in a static if )
//
//! runtextmacro optional WRITE_MISSILE_RECYCLE_BIN("WRITE_DELAYED_MISSILE_RECYCLING", "DELAYED_MISSILE_DEATH_ANIMATION_TIME")
// The code of WRITE_MISSILE_POSITION_CODE boxes a missiles position and does the required trigonometry.
//
//! runtextmacro WRITE_MISSILE_POSITION_CODE()
// Missiles structure works like a linked list with the folling methods:
// allocateCollection(), allocateNode(), insertFront(node) and remove()
//
private keyword MissileStructure
struct Missile extends array
implement MissileStructure
// Constants:
// ==========
//
// Attach point name for effects created by model=.
readonly static constant string ORIGIN = "origin"
// Set a ficitional hit box in range of 0 to 1,
// while 0 is the toe and 1 the head of a unit.
readonly static constant real HIT_BOX = (2./3.)
// DEBUG_MODE only members:
// ========================
//
// Checks for double launching. Throws an error message.
debug boolean launched
// Private members:
// ================
//
// The position of a missile using curve= does not
// match the position used by Missile's trigonometry.
// Therefore we need this member two times.
// Readable x / y / z for your needs and posX / posY for cool mathematics.
private real posX
private real posY
private real dist// distance
// Readonly members:
// =================
//
// Prevents a double free case.
readonly boolean allocated
// The dummy unit.
readonly unit dummy
// Position members for your needs.
readonly MissilePosition origin// Grants access to readonly members of MissilePosition,
readonly MissilePosition impact// which are "x", "y", "z", "angle", "distance", "slope" and "alpha".
readonly real terrainZ
readonly real x
readonly real y
readonly real z
readonly real angle// Current angle
readonly real prevX
readonly real prevY
readonly real prevZ
// Collision detection type. ( Evaluated new in each loop )
readonly integer collisionType// Current collision type ( circular or rectangle )
// Members you can set:
// ====================
//
unit source
unit target // For homing missiles.
real distance // Distance traveled.
player owner // Pseudo owner for faster onCollide evaluation. The proper dummy owner is PLAYER_NEUTRAL_PASSIVE.
real speed // Vector lenght for missile movement in plane x / y.
real acceleration
real damage
integer data // For data transfer set and read data.
boolean recycle // Is set to true, when a Missile reaches it's destination.
real turn // Sets a turn rate for a missile.
real collision // Collision size in plane x / y.
// Setting collision z is deprecated since Missile v2.5.
method operator collisionZ= takes real value returns nothing
endmethod
method operator collisionZ takes nothing returns real
return collision
endmethod
// Operator overloading:
// =====================
//
// Special effect on the missile dummy. For multiple effect attaching, access unit "dummy" directly.
private effect sfx
private string path
method operator model= takes string file returns nothing
if sfx != null then
call DestroyEffect(sfx)
set sfx = null
endif
// null and ""
if StringLength(file) > 0 then
set sfx = AddSpecialEffectTarget(file, dummy, ORIGIN)
set path = file
else
set path = null
endif
endmethod
method operator model takes nothing returns string
return path
endmethod
real open
// Enables curved movement for your missile.
// Remember that the result of Tan(PI/2) is infinity.
method operator curve= takes real value returns nothing
set open = Tan(value)*origin.distance
endmethod
method operator curve takes nothing returns real
return Atan(open/origin.distance)
endmethod
real height
// Enables arcing movement for your missile.
method operator arc= takes real value returns nothing
set height = Tan(value)*origin.distance/4
endmethod
method operator arc takes nothing returns real
return Atan(4*height/origin.distance)
endmethod
private real scaling
method operator scale= takes real value returns nothing
call SetUnitScale(dummy, value, 0., 0.)
set scaling = value
endmethod
method operator scale takes nothing returns real
return scaling
endmethod
// Methods:
// ========
//
method bounce takes nothing returns nothing
call origin.move(x, y, z)
set dist = 0.
endmethod
method deflect takes real tx, real ty returns nothing
local real a = 2.*Atan2(ty - y, tx - x) + bj_PI - angle
call impact.move(x + (origin.distance - dist)*Cos(a), y + (origin.distance - dist)*Sin(a), impact.z)
call bounce()
endmethod
method deflectEx takes real tx, real ty, real tz returns nothing
call impact.move(impact.x, impact.y, tz)
call deflect(tx, ty)
endmethod
method flightTime2Speed takes real duration returns nothing
set speed = RMaxBJ(0.00000001, (origin.distance - dist)*Missile_TIMER_TIMEOUT/RMaxBJ(0.00000001, duration))
endmethod
method setMovementSpeed takes real value returns nothing
set speed = value*Missile_TIMER_TIMEOUT
endmethod
boolean wantDestroy// For "true" a missile is destroyed on the next timer callback. 100% safe.
method destroy takes nothing returns nothing
set wantDestroy = true
endmethod
// Instantly destroys a missile.
method terminate takes nothing returns nothing
if allocated then
call remove()
call impact.destroy()
call origin.destroy()
call DestroyEffect(sfx)
call FlushChildHashtable(HASH, this)
if GetUnitTypeId(dummy) == Missile_DUMMY_UNIT_ID then
// MissileRecycler > Dummy > xe.
static if LIBRARY_MissileRecycler then
call RecycleMissile(dummy)
elseif LIBRARY_Dummy and Dummy.create.exists then
call Dummy[dummy].destroy()
elseif LIBRARY_xedummy and xedummy.release.exists then
call xedummy.release(dummy)
else
call RemoveUnit(dummy)
endif
endif
set sfx = null
set source = null
set target = null
set dummy = null
set owner = null
endif
endmethod
// Runs in createEx.
//! textmacro MISSILE_RESET_ALL_MEMBERS
set path = null
set speed = 0.
set acceleration = 0.
set distance = 0.
set dist = 0
set dist = 0.
set height = 0.
set turn = 0.
set open = 0.
set collision = 0.
set collisionType = 0
set stackSize = 0
set scaling = 1.
set wantDestroy = false
set recycle = false
//! endtextmacro
// Launches a dummy of your choice.
static method createEx takes unit missileDummy, real impactX, real impactY, real impactZ returns thistype
local thistype this = thistype.allocateNode()
local real originX = GetUnitX(missileDummy)
local real originY = GetUnitY(missileDummy)
local real originZ = GetUnitFlyHeight(missileDummy)
//
//! runtextmacro MISSILE_RESET_ALL_MEMBERS()
//
set origin = MissilePosition.create(originX, originY, originZ)
set impact = MissilePosition.create(impactX, impactY, impactZ)
call MissilePosition.link(origin, impact)
set posX = originX
set posY = originY
set x = originX
set y = originY
set z = originZ
set angle = origin.angle
set dummy = missileDummy
call SetUnitFlyHeight(missileDummy, originZ, 0.)
call SaveUnitHandle(HASH, this, GetHandleId(missileDummy), missileDummy)
//
static if LIBRARY_ErrorMessage then
debug call ThrowWarning(GetUnitTypeId(missileDummy) == 0, "Missile", "createEx", "missileDummy", this, "Invalid missile dummy unit ( null )!")
endif
debug set launched = false
return this
endmethod
// Freaky static if ensures these libraries really don't exist.
static if not LIBRARY_MissileRecycler and not LIBRARY_Dummy and not Dummy.create.exists and not LIBRARY_xe_dummy and not xe_dummy.new.exists then
private static method newMissileUnit takes real x, real y, real z, real face returns unit
local boolean prev = ToogleUnitIndexer(false)
set bj_lastCreatedUnit = CreateUnit(Missile_NEUTRAL_PASSIVE, Missile_DUMMY_UNIT_ID , x, y, face)
call ToogleUnitIndexer(prev)
call SetUnitX(bj_lastCreatedUnit, x)
call SetUnitY(bj_lastCreatedUnit, y)
call UnitAddAbility(bj_lastCreatedUnit, 'Amrf')
call SetUnitFlyHeight(bj_lastCreatedUnit, z, 0.)
call PauseUnit(bj_lastCreatedUnit, true)
return bj_lastCreatedUnit
endmethod
endif
// MissileRecylcer > Dummy > xe > Missile.
//! textmacro MISSILE_GET_DUMMY_FROM_LIBRARY
static if LIBRARY_MissileRecycler then
return createEx(GetRecycledMissile(x, y, z, angle*bj_RADTODEG), impactX, impactY, impactZ)
elseif LIBRARY_Dummy and Dummy.create.exists then
local Dummy dummy = Dummy.create(x, y, angle*bj_RADTODEG)
call SetUnitFlyHeight(dummy.unit, z, 0.)
return createEx(dummy.unit, impactX, impactY, impactZ)
elseif LIBRARY_xedummy and xedummy.new.exists then
set bj_lastCreatedUnit = xedummy.new(Missile_NEUTRAL_PASSIVE, x, y, angle*bj_RADTODEG)
call SetUnitFlyHeight(bj_lastCreatedUnit, z, 0.)
return createEx(bj_lastCreatedUnit, impactX, impactY, impactZ)
else
return createEx(Missile.newMissileUnit(x, y, z, angle*bj_RADTODEG), impactX, impactY, impactZ)
endif
//! endtextmacro
// Wrapper to createEx.
static method create takes real x, real y, real z, real angle, real distance, real impactZ returns thistype
local real impactX = x + distance*Cos(angle)
local real impactY = y + distance*Sin(angle)
// Get the dummy unit.
//! runtextmacro MISSILE_GET_DUMMY_FROM_LIBRARY()
endmethod
// Wrapper to createEx.
static method createXYZ takes real x, real y, real z, real impactX, real impactY, real impactZ returns thistype
local real angle = Atan2(impactY - y, impactX - x)
// Get the dummy unit.
//! runtextmacro MISSILE_GET_DUMMY_FROM_LIBRARY()
endmethod
// Missile motion takes place every Missile_TIMER_TIMEOUT
// before accessing each active struct.
static Missile temp = 0
static method move takes nothing returns boolean
local integer loops = 0 // Current iteration.
local integer limit = 150 // Set iteration border per trigger evaluation to avoid hitting the operation limit.
local thistype this = thistype.temp
local MissilePosition p
local real a
local real d
local unit u
local real newX
local real newY
local real vel
local real point
local real pitch
loop
exitwhen 0 == this or loops == limit
set p = origin
// Save previous, respectively current missile position.
set prevX = x
set prevY = y
set prevZ = z
// Evaluate the collision type.
set vel = speed
set speed = vel + acceleration
if vel < collision*Missile_COLLISION_ACCURACY_FACTOR then
set collisionType = Missile_COLLISION_TYPE_CIRCLE
else
set collisionType = Missile_COLLISION_TYPE_RECTANGLE
endif
// Update missile guidance to its intended target.
set u = target
if u != null then
if 0 == GetUnitTypeId(u) then
set target = null
else
call origin.move(x, y, z)
call impact.move(GetUnitX(u), GetUnitY(u), GetUnitFlyHeight(u) + GetUnitBodySize(u)*Missile.HIT_BOX)
set dist = 0
set height = 0
set curve = 0
endif
endif
set a = p.angle
// Update the missile facing angle depending on the turn ratio.
if 0. != turn and Cos(angle - a) < Cos(turn) then
if 0. > Sin(a - angle) then
set angle = angle - turn
else
set angle = angle + turn
endif
else
set angle = a
endif
// Update the missile position on the parabola.
set d = p.distance// origin - impact distance.
set recycle = dist + vel >= d
if recycle then// Maximum distance reached.
set point = d
set distance = distance + d - dist
else
set distance = distance + vel
set point = dist + vel
endif
set dist = point
set newX = posX + vel*Cos(angle)
set newY = posY + vel*Sin(angle)
set posX = newX
set posY = newY
// Update point(x/y) if a curving trajectory is defined.
set u = dummy
if 0. != open and target == null then
set vel = 4*open*point*(d - point)/p.square
set a = angle + bj_PI/2
set newX = newX + vel*Cos(a)
set newY = newY + vel*Sin(a)
set a = angle + Atan(-((4*open)*(2*point - d))/p.square)
else
set a = angle
endif
set x = newX
set y = newY
// Update pos z if an arc or height is set.
call MoveLocation(LOC, newX, newY)
set terrainZ = GetLocationZ(LOC)
set pitch = p.alpha
if 0. == height and 0. == pitch then
set z = p.z - terrainZ
else
set z = p.z - terrainZ + p.slope*point
if 0. != height and target == null then
set z = z + (4*height*point*(d - point)/p.square)
set pitch = pitch - Atan(((4*height)*(2*point - d))/p.square)*bj_RADTODEG
endif
endif
// Update the pitch angle of the dummy unit.
if GetUnitTypeId(u) == Missile_DUMMY_UNIT_ID then
call SetUnitAnimationByIndex(u, R2I(pitch + 90.5))
endif
// Move the missile dummy via native.
call SetUnitFlyHeight(u, z, 0.)
call SetUnitFacing(u, a*bj_RADTODEG)
// WorldBounds > BoundSentinel.
static if not LIBRARY_BoundSentinel and not LIBRARY_WorldBounds then
if RectContainsCoords(bj_mapInitialPlayableArea, newX, newY) then
call SetUnitX(u, newX)
call SetUnitY(u, newY)
endif
elseif LIBRARY_WorldBounds then
if newX < WorldBounds.maxX and newX > WorldBounds.minX and newY < WorldBounds.maxY and newY > WorldBounds.minY then
call SetUnitX(u, newX)
call SetUnitY(u, newY)
endif
else
call SetUnitX(u, newX)
call SetUnitY(u, newY)
endif
set loops = loops + 1
set this = nextNode[this]
endloop
set u = null
set thistype.temp = this
return this == 0
endmethod
// Widget collision API:
// =====================
//
// Runs automatically on widget collision.
method hitWidget takes widget w returns nothing
if w != null then
call SaveWidgetHandle(HASH, this, GetHandleId(w), w)
endif
endmethod
// All widget which have been hit return true.
method hasHitWidget takes widget w returns boolean
return HaveSavedHandle(HASH, this, GetHandleId(w))
endmethod
// Removes a widget from the missile's memory of hit widgets. ( This widget can be hit again )
method removeHitWidget takes widget w returns nothing
if w != null then
call RemoveSavedHandle(HASH, this, GetHandleId(w))
endif
endmethod
// Flushes a missile's memory for collision. ( All widgets can be hit again )
method flushHitWidgets takes nothing returns nothing
call FlushChildHashtable(HASH, this)
call hitWidget(dummy)
endmethod
// Tells missile to call removeHitWidget(w) after "seconds" time.
// Does not apply to widgets, which are already hit by this missile.
readonly integer stackSize
method enableHitAfter takes widget w, real seconds returns nothing
local integer id = GetHandleId(w)
if w != null then
if not HaveSavedInteger(HASH, this, id) then
call SaveInteger(HASH, this, id, stackSize)
call SaveInteger(HASH, this, stackSize, id)
set stackSize = stackSize + 1
endif
call SaveReal(HASH, this, id, seconds)// Set time.
endif
endmethod
method updateStack takes nothing returns nothing
local integer dex = 0
local integer id
local real time
loop
exitwhen dex == stackSize
set id = LoadInteger(HASH, this, dex)
set time = LoadReal(HASH, this, id) - Missile_TIMER_TIMEOUT
if time <= 0. or not HaveSavedHandle(HASH, this, id) then
set stackSize = stackSize - 1
set id = LoadInteger(HASH, this, stackSize)
call SaveInteger(HASH, this, dex, id)
call SaveInteger(HASH, this, id, dex)
// Enables hit.
call RemoveSavedHandle(HASH, this, id)
// Remove data from stack.
call RemoveSavedReal(HASH, this, id)
call RemoveSavedInteger(HASH, this, id)
call RemoveSavedInteger(HASH, this, stackSize)
else
call SaveReal(HASH, this, id, time)
set dex = dex + 1
endif
endloop
endmethod
// Widget collision code:
// ======================
//
private static boolean circle = true
//
// 1.) Rectangle collision for fast moving missiles with small collision radius.
//
// Runs for destructables and items in a rectangle.
// Checks if widget w is in collision range of a missile.
// Is not precise in z - axis collision.
private method isWidgetInRectangle takes widget w, real wz, real distance returns boolean
local real wx = GetWidgetX(w)
local real wy = GetWidgetY(w)
local real dz = Missile_GetLocZ(wx, wy) - terrainZ
local real dx = x - prevX
local real dy = y - prevY
local real s = (dx*(wx - prevX) + dy*(wy - prevY))/(dx*dx + dy*dy)
if s < 0. then
set s = 0.
elseif s > 1 then
set s = 1.
endif
set dx = (prevX + s*dx) - wx
set dy = (prevY + s*dy) - wy
return dx*dx + dy*dy <= distance*distance and dz + wz >= z - distance and dz <= z + distance
endmethod
//
// 2.) Circular collision detection for all other missiles.
//
// Returns true for widgets in a xyz collision range.
private method isWidgetInRange takes widget w, real wz, real distance returns boolean
local real wx = GetWidgetX(w)
local real wy = GetWidgetY(w)
local real dz = Missile_GetLocZ(wx, wy) - terrainZ
// collision in plane x and y, collision in z axis.
return IsUnitInRangeXY(dummy, wx, wy, distance) and dz + wz >= z - distance and dz <= z + distance
endmethod
//
// 3.) Action functions inside the widget enumeration thread.
//
// Runs for every enumerated destructable.
// • Directly filters out already hit destructables.
// • Distance formula based on the Pythagorean theorem.
//
private static method filterDests takes nothing returns boolean
local destructable d = GetFilterDestructable()
local boolean b = false
if not HaveSavedHandle(HASH, temp, GetHandleId(d)) then
if circle then
set b = temp.isWidgetInRange(d, GetDestructableHeight(d), temp.collision)
else
set b = temp.isWidgetInRectangle(d, GetDestructableHeight(d), temp.collision)
endif
endif
set d = null
return b
endmethod
//
// Runs for every enumerated item.
// • Directly filters out already hit items.
// • Distance formula based on the Pythagorean theorem.
// • Items have a fix collision size of 16.
//
private static method filterItems takes nothing returns boolean
local item i = GetFilterItem()
local boolean b = false
if not HaveSavedHandle(HASH, temp, GetHandleId(i)) then
if circle then // Item in missile collision size or item pathing in missile range.
set b = temp.isWidgetInRange(i, GetItemHeight(i), RMaxBJ(temp.collision, 16.))
else
set b = temp.isWidgetInRectangle(i, GetItemHeight(i), RMaxBJ(temp.collision, 16.))
endif
endif
set i = null
return b
endmethod
//
// 4.) Filter function for rectangle unit collision.
//
// Runs for every enumerated units.
// • Filters out units which are not in collision range in plane x / y.
// • Inlined and therefore a bit faster than item and destructable collision.
//
private static method filterUnits takes nothing returns boolean
local thistype this = thistype.temp
local unit u = GetFilterUnit()
local real dx
local real dy
local real s
local boolean is = false
if not HaveSavedHandle(HASH, this, GetHandleId(u)) then
set dx = x - prevX
set dy = y - prevY
set s = (dx*(GetUnitX(u) - prevX) + dy*(GetUnitY(u)- prevY))/(dx*dx + dy*dy)
if s < 0. then
set s = 0.
elseif s > 1. then
set s = 1.
endif
set is = IsUnitInRangeXY(u, prevX + s*dx, prevY + s*dy, collision)
endif
set u = null
return is
endmethod
//
// 5.) Proper rect preparation.
//
// For rectangle.
private method prepareRectRectangle takes nothing returns nothing
local real x1 = prevX
local real y1 = prevY
local real x2 = x
local real y2 = y
local real d = collision + Missile_MAXIMUM_COLLISION_SIZE
// What is min, what is max ...
if x1 < x2 then
if y1 < y2 then
call SetRect(RECT, x1 - d, y1 - d, x2 + d, y2 + d)
else
call SetRect(RECT, x1 - d, y2 - d, x2 + d, y1 + d)
endif
elseif y1 < y2 then
call SetRect(RECT, x2 - d, y1 - d, x1 + d, y2 + d)
else
call SetRect(RECT, x2 - d, y2 - d, x1 + d, y1 + d)
endif
endmethod
//
// For circular.
private method prepareRectCircle takes nothing returns nothing
local real d = collision + Missile_MAXIMUM_COLLISION_SIZE
call SetRect(RECT, x - d, y - d, x + d, y + d)
endmethod
//
// 5.) API for the MissileStruct iteration.
//
method groupEnumUnitsRectangle takes nothing returns nothing
call prepareRectRectangle()
set thistype.temp = this
call GroupEnumUnitsInRect(GROUP, RECT, unitFilter)
endmethod
//
// Prepares destructable enumeration, then runs enumDests.
method checkDestCollision takes code func returns nothing
set circle = collisionType == Missile_COLLISION_TYPE_CIRCLE
if circle then
call prepareRectCircle()
else
call prepareRectRectangle()
endif
//
set thistype.temp = this
call EnumDestructablesInRect(RECT, destFilter, func)
endmethod
//
// Prepares item enumeration, then runs enumItems.
method checkItemCollision takes code func returns nothing
set circle = collisionType == Missile_COLLISION_TYPE_CIRCLE
if circle then
call prepareRectCircle()
else
call prepareRectRectangle()
endif
//
set thistype.temp = this
call EnumItemsInRect(RECT, itemFilter, func)
endmethod
static if Missile_WRITE_DELAYED_MISSILE_RECYCLING then
method nullBefore takes nothing returns nothing
set dummy = null
endmethod
endif
// Does not check for 'Aloc' and 'Amrf' as they could be customized.
private static method onInit takes nothing returns nothing
static if LIBRARY_ErrorMessage then
debug local boolean prev = ToogleUnitIndexer(false)
debug local unit dummy = CreateUnit(Missile_NEUTRAL_PASSIVE, Missile_DUMMY_UNIT_ID, 0., 0., 0.)
debug call ToogleUnitIndexer(prev)
//
debug call ThrowError((GetUnitTypeId(dummy) != Missile_DUMMY_UNIT_ID), "Missile", "DEBUG_MISSILE", "type id", 0, "Global setup for public integer DUMMY_UNIT_ID is incorrect! This map currently can't use Missile!")
debug call ThrowError((Missile_MAXIMUM_COLLISION_SIZE < 0), "Missile", "DEBUG_MISSILE", "collision", 0, "Global setup for public real MAXIMUM_COLLISION_SIZE is incorrect, below zero! This map currently can't use Missile!")
debug call RemoveUnit(dummy)
debug set dummy = null
endif
//
set unitFilter = Filter(function thistype.filterUnits)
set destFilter = Filter(function thistype.filterDests)
set itemFilter = Filter(function thistype.filterItems)
call TriggerAddCondition(MOVE, Condition(function thistype.move))
endmethod
endstruct
// You made it to the end of Missile, but we are not finished.
// Do you remember about the data structure, the delayed recycler
// and of course our interface module "MissileStruct"
//
// This comes now!
// Debug code taken from List ( full credits to Nestharus )
private module MissileStructure
private static thistype collectionCount = 0
private static thistype nodeCount = 0
static if LIBRARY_ErrorMessage then
debug private boolean isNode
debug private boolean isCollection
endif
private thistype _list
method operator list takes nothing returns thistype
static if LIBRARY_ErrorMessage then
debug call ThrowError(this == 0, "MissileStructure", "list", "thistype", this, "Attempted To Read Null Node.")
debug call ThrowError(not isNode, "MissileStructure", "list", "thistype", this, "Attempted To Read Invalid Node.")
endif
return _list
endmethod
private thistype _next
method operator next takes nothing returns thistype
static if LIBRARY_ErrorMessage then
debug call ThrowError(this == 0, "MissileStructure", "next", "thistype", this, "Attempted To Go Out Of Bounds.")
debug call ThrowError(not isNode, "MissileStructure", "next", "thistype", this, "Attempted To Read Invalid Node.")
endif
return _next
endmethod
private thistype _prev
method operator prev takes nothing returns thistype
static if LIBRARY_ErrorMessage then
debug call ThrowError(this == 0, "MissileStructure", "prev", "thistype", this, "Attempted To Go Out Of Bounds.")
debug call ThrowError(not isNode, "MissileStructure", "prev", "thistype", this, "Attempted To Read Invalid Node.")
endif
return _prev
endmethod
private thistype _first
method operator first takes nothing returns thistype
static if LIBRARY_ErrorMessage then
debug call ThrowError(this == 0, "MissileStructure", "first", "thistype", this, "Attempted To Read Null List.")
debug call ThrowError(not isCollection, "MissileStructure", "first", "thistype", this, "Attempted To Read Invalid List.")
endif
return _first
endmethod
private thistype _last
method operator last takes nothing returns thistype
static if LIBRARY_ErrorMessage then
debug call ThrowError(this == 0, "MissileStructure", "last", "thistype", this, "Attempted To Read Null List.")
debug call ThrowError(not isCollection, "MissileStructure", "last", "thistype", this, "Attempted To Read Invalid List.")
endif
return _last
endmethod
static method allocateCollection takes nothing returns thistype
local thistype this = thistype(0)._first
if (0 == this) then
static if LIBRARY_ErrorMessage then
debug call ThrowError(collectionCount == 8191, "MissileStructure", "allocateCollection", "thistype", 0, "Overflow.")
endif
set this = collectionCount + 1
set collectionCount = this
else
set thistype(0)._first = _first
endif
static if LIBRARY_ErrorMessage then
debug set isCollection = true
endif
set _first = 0
return this
endmethod
static method allocateNode takes nothing returns thistype
local thistype this = thistype(0)._next
if (0 == this) then
static if LIBRARY_ErrorMessage then
debug call ThrowError(nodeCount == 8191, "MissileStructure", "allocateNode", "thistype", 0, "Overflow.")
endif
set this = nodeCount + 1
set nodeCount = this
else
set thistype(0)._next = _next
endif
set allocated = true
return this
endmethod
method insertFront takes thistype node returns thistype
// Extra static unique list for missile motion.
set nextNode[node] = 0
set prevNode[node] = prevNode[0]
set nextNode[prevNode[0]] = node
set prevNode[0] = node
static if LIBRARY_ErrorMessage then
debug call ThrowError(this == 0, "List", "push", "thistype", this, "Attempted To Push On To Null List.")
debug call ThrowError(not isCollection, "List", "push", "thistype", this, "Attempted To Push On To Invalid List.")
debug set node.isNode = true
endif
set node._list = this
if (_first == 0) then
set _first = node
set _last = node
set node._next = 0
else
set _first._prev = node
set node._next = _first
set _first = node
endif
set node._prev = 0
return node
endmethod
method remove takes nothing returns nothing
local thistype node = this
set this = node._list
static if LIBRARY_ErrorMessage then
debug call ThrowError(node == 0, "MissileStructure", "remove", "thistype", this, "Attempted To Remove Null Node.")
debug call ThrowError(not node.isNode, "MissileStructure", "remove", "thistype", this, "Attempted To Remove Invalid Node (" + I2S(node) + ").")
debug set node.isNode = false
endif
set node._list = 0
if (0 == node._prev) then
set _first = node._next
else
set node._prev._next = node._next
endif
if (0 == node._next) then
set _last = node._prev
else
set node._next._prev = node._prev
endif
set node._next = thistype(0)._next
set thistype(0)._next = node
set node.allocated = false
// Static unique list for missile motion.
set nextNode[prevNode[node]] = nextNode[node]
set prevNode[nextNode[node]] = prevNode[node]
endmethod
endmodule
// Boolean expressions per struct:
// ===============================
//
// Condition function for the core trigger evaluation. ( Runs for all struct using module MissileStruct )
private function MissileCreateExpression takes integer structId, code func returns nothing
set expression[structId] = Condition(func)
endfunction
// Creates a collection for a struct. ( Runs for all struct using module MissileStruct )
private function MissileCreateCollection takes integer structId returns nothing
set missileList[structId] = Missile.allocateCollection()
endfunction
// Core:
// =====
//
// Fires every Missile_TIMER_TIMEOUT.
private function Fire takes nothing returns nothing
local integer i = remove[0]
set remove[0] = 0
loop
exitwhen 0 == i
if recycling[i] then
call TriggerRemoveCondition(CORE, condition[i])
set condition[i] = null
set active = active - 1
endif
set destroying[i] = false
set recycling[i] = false
set i = remove[i]
endloop
if 0 == active then
call PauseTimer(TMR)
else
// Move all launched missiles.
set Missile.temp = nextNode[0]
set i = 0
loop
exitwhen TriggerEvaluate(MOVE)
exitwhen i == 60// Moved over 8910 missiles, something buggy happened.
set i = i + 1 // This case is impossible, hence never happens. But if I'm wrong, which also never happens
endloop // then the map 100% crashes. i == 66 will prevent the game crash and Missile will start to
// to debug itself over the next iterations.
// Access all structs using module MissileStruct.
static if DEBUG_MODE and LIBRARY_ErrorMesssage then
if not TriggerEvaluate(CORE) then
call ThrowWarning(true, "Missile", "Fire", "op limit", 0, /*
*/"You just ran into a op limit!
The op limit protection of Missile is insufficient for your map.
The code of the missile module can't run in one thread and must be splitted.
If unsure make a post in the official 'The Hive Workshop' forum thread of Missile!")
endif
else
call TriggerEvaluate(CORE)
endif
endif
endfunction
// Conditionally starts the timer.
private function StartPeriodic takes integer structId returns nothing
if 0 == instances[structId] or destroying[structId] then
if destroying[structId] then
set recycling[structId] = false
else
if 0 == active then
call TimerStart(TMR, Missile_TIMER_TIMEOUT, true, function Fire)
endif
set active = active + 1
set condition[structId] = TriggerAddCondition(CORE, expression[structId])
endif
endif
set instances[structId] = instances[structId] + 1
endfunction
// Conditionally stops the timer in the next callback.
private function StopPeriodic takes integer structId returns nothing
set instances[structId] = instances[structId] - 1
if 0 == instances[structId] and condition[structId] != null then
if not destroying[structId] and not recycling[structId] then
set destroying[structId] = true
set recycling[structId] = true
set remove[structId] = remove[0]
set remove[0] = structId
endif
endif
endfunction
// Modules:
// ========
//
// You want to place module MissileLaunch at the very top of your struct.
module MissileLaunch
static method launch takes Missile missile returns nothing
static if LIBRARY_ErrorMessage then
debug call ThrowError(missile.launched, "thistype", "launch", "missile.launched", missile, "This missile was already launched before!")
endif
debug set missile.launched = true
//
call missileList[thistype.typeid].insertFront(missile)
call StartPeriodic(thistype.typeid)
endmethod
endmodule
module MissileTerminate
// Called from missileIterate. "P" to avoid code collision.
static method missileTerminateP takes Missile node returns nothing
if node.allocated then
static if thistype.onRemove.exists then
static if Missile_WRITE_DELAYED_MISSILE_RECYCLING and RecycleBin.recycle.exists then
if thistype.onRemove(node) and GetUnitTypeId(node.dummy) == Missile_DUMMY_UNIT_ID then
call RecycleBin.recycle(node.dummy)
call node.nullBefore()
endif
else
call thistype.onRemove(node)
endif
endif
call node.terminate()
call StopPeriodic(thistype.typeid)
endif
endmethod
endmodule
// Allows you to inject missile in certain stages of the motion process.
module MissileAction
static if DEBUG_MODE then
// Runs check during compile time.
static if thistype.onMissile.exists then
Error Message from library Missile in struct thistype !
thistype.onMissile is a reserved name for Missile, once you implemented MissileStruct.
thistype.onMissile is currently not supported by library Missile.
Please delete or re-name that method.
endif
endif
static if thistype.onItem.exists then
private static method missileActionItem takes nothing returns nothing
local item i = GetEnumItem()
local Missile this = Missile.temp
if this.allocated then
call SaveItemHandle(HASH, this, GetHandleId(i), i)
if thistype.onItem(this, i) then
call missileTerminateP(this)
endif
endif
set i = null
endmethod
endif
static if thistype.onDestructable.exists then
private static method missileActionDest takes nothing returns nothing
local destructable d = GetEnumDestructable()
local Missile this = Missile.temp
if this.allocated then
call SaveDestructableHandle(HASH, this, GetHandleId(d), d)
if thistype.onDestructable(this, d) then
call missileTerminateP(this)
endif
endif
set d = null
endmethod
endif
// Runs every Missile_TIMER_TIMEOUT for this struct.
static method missileIterateP takes nothing returns boolean
local Missile this = missileList[thistype.typeid].first
local Missile node
local real collideZ
local boolean b
local unit u
loop
exitwhen 0 == this
set node = this.next// The linked list should not lose the next node.
if this.wantDestroy then
call thistype.missileTerminateP(this)
else
if this.stackSize > 0 then
call this.updateStack()
endif
// Runs unit collision.
static if thistype.onCollide.exists then
if this.allocated and 0. < this.collision then
set b = this.collisionType == Missile_COLLISION_TYPE_RECTANGLE
if b then
call this.groupEnumUnitsRectangle()
else
call GroupEnumUnitsInRange(GROUP, this.x, this.y, this.collision + Missile_MAXIMUM_COLLISION_SIZE, null)
endif
loop
set u = FirstOfGroup(GROUP)
exitwhen u == null
call GroupRemoveUnit(GROUP, u)
if not HaveSavedHandle(HASH, this, GetHandleId(u)) then
if IsUnitInRange(u, this.dummy, this.collision) or b then
// Eventually run z collision checks.
static if Missile_USE_COLLISION_Z_FILTER then
set collideZ = Missile_GetLocZ(GetUnitX(u), GetUnitY(u)) + GetUnitFlyHeight(u) - this.terrainZ
if (collideZ + GetUnitBodySize(u) >= this.z - this.collisionZ) and (collideZ <= this.z + this.collisionZ) then
// Mark as hit.
call SaveUnitHandle(HASH, this, GetHandleId(u), u)
if thistype.onCollide(this, u) then
call thistype.missileTerminateP(this)
exitwhen true
endif
endif
else
// Runs unit collision without z collision checks.
call SaveUnitHandle(HASH, this, GetHandleId(u), u)
if thistype.onCollide(this, u) then
call thistype.missileTerminateP(this)
exitwhen true
endif
endif
endif
endif
endloop
endif
endif
// Runs destructable collision.
static if thistype.onDestructable.exists then
// Check if the missile is not terminated.
if this.allocated and 0. < this.collision then
call this.checkDestCollision(function thistype.missileActionDest)
endif
endif
// Runs item collision.
static if thistype.onItem.exists then
// Check if the missile is not terminated.
if this.allocated and 0. < this.collision then
call this.checkItemCollision(function thistype.missileActionItem)
endif
endif
// Runs when the destination is reached.
if this.recycle and this.allocated then
static if thistype.onFinish.exists then
if thistype.onFinish(this) then
call thistype.missileTerminateP(this)
endif
else
call thistype.missileTerminateP(this)
endif
endif
// Runs on terrian collision.
static if thistype.onTerrain.exists then
if this.allocated and 0. > this.z and thistype.onTerrain(this) then
call missileTerminateP(this)
endif
endif
// Runs every Missile_TIMER_TIMEOUT.
static if thistype.onPeriod.exists then
if this.allocated and thistype.onPeriod(this) then
call missileTerminateP(this)
endif
endif
endif
set this = node
endloop
set u = null
static if DEBUG_MODE and LIBRARY_ErrorMessage then
return true
else
return false
endif
endmethod
endmodule
module MissileStruct
implement MissileLaunch
implement MissileTerminate
implement MissileAction
private static method onInit takes nothing returns nothing
call MissileCreateCollection(thistype.typeid)
call MissileCreateExpression(thistype.typeid, function thistype.missileIterateP)
endmethod
endmodule
// Missile position:
// =================
//
// Simple trigonometry.
//! textmacro WRITE_MISSILE_POSITION_CODE
struct MissilePosition extends array
private static integer array recycler
private static integer alloc = 0
// Readonly members you can access.
readonly real x
readonly real y
readonly real z
readonly real angle
readonly real distance
readonly real square
readonly real slope
readonly real alpha
// Creates an origin - impact link.
private thistype ref
private static method math takes thistype a, thistype b returns nothing
local real dx
local real dy
loop
set dx = b.x - a.x
set dy = b.y - a.y
set dx = dx*dx + dy*dy
set dy = SquareRoot(dx)
exitwhen dx != 0. and dy != 0.
set b.x = b.x + .01
set b.z = b.z - Missile_GetLocZ(b.x -.01, b.y) + Missile_GetLocZ(b.x, b.y)
endloop
set a.square = dx
set a.distance = dy
set a.angle = Atan2(b.y - a.y, b.x - a.x)
set a.slope = (b.z - a.z)/dy
set a.alpha = Atan(a.slope)*bj_RADTODEG
// Set b.
if b.ref == a then
set b.angle = a.angle + bj_PI
set b.distance = dy
set b.slope = -a.slope
set b.alpha = -a.alpha
set b.square = dx
endif
endmethod
static method link takes thistype a, thistype b returns nothing
set a.ref = b
set b.ref = a
call math(a, b)
endmethod
method move takes real toX, real toY, real toZ returns nothing
set x = toX
set y = toY
set z = toZ + Missile_GetLocZ(toX, toY)
if ref != this then
call math(this, ref)
endif
endmethod
method destroy takes nothing returns nothing
set recycler[this] = recycler[0]
set recycler[0] = this
endmethod
static method create takes real x, real y, real z returns MissilePosition
local thistype this = recycler[0]
if 0 == this then
set alloc = alloc + 1
set this = alloc
else
set recycler[0] = recycler[this]
endif
set ref = this
call move(x, y, z)
return this
endmethod
endstruct
//! endtextmacro
// Delayed dummy recycling:
// ========================
//
// Ensures proper fx death animations.
//! textmacro WRITE_MISSILE_RECYCLE_BIN takes DO_THIS, AFTER_TIME
static if $DO_THIS$ then
private struct RecycleBin extends array
private static constant timer t = CreateTimer()
private static integer max = 0
private static unit array dummy
private static real array time
private static method onPeriodic takes nothing returns nothing
local integer dex = 0
loop
exitwhen dex == thistype.max
set thistype.time[dex] = thistype.time[dex] - 1
if 0 >= thistype.time[dex] then
static if LIBRARY_MissileRecycler then
call RecycleMissile(thistype.dummy[dex])
elseif Dummy.create.exists and LIBRARY_Dummy then
call Dummy[thistype.dummy[dex]].destroy()
elseif LIBRARY_xedummy and xedummy.release.exists then
call xedummy.release(thistype.dummy[dex])
else
call RemoveUnit(thistype.dummy[dex])
endif
set thistype.dummy[dex] = null
set thistype.max = thistype.max - 1
set thistype.dummy[dex] = thistype.dummy[thistype.max]
set thistype.time[dex] = thistype.time[thistype.max]
set thistype.dummy[thistype.max] = null
set dex = dex - 1
if 0 == thistype.max then
call PauseTimer(thistype.t)
endif
endif
set dex = dex + 1
endloop
endmethod
static method recycle takes unit toRecycle returns nothing
if 0 == thistype.max then
call TimerStart(thistype.t, 1., true, function thistype.onPeriodic)
endif
set thistype.dummy[max] = toRecycle
set thistype.time[max] = $AFTER_TIME$ + TimerGetRemaining(thistype.t)
set thistype.max = thistype.max + 1
endmethod
endstruct
endif
//! endtextmacro
// The end!
endlibrary
//TESH.scrollpos=0
//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=0
//TESH.alwaysfold=0
library UnitDex uses optional WorldBounds, optional GroupUtils
/***************************************************************
*
* v1.2.1, by TriggerHappy
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* UnitDex assigns every unit an unique integer. It attempts to make that number within the
* maximum array limit so you can associate it with one.
* _________________________________________________________________________
* 1. Installation
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* Copy the script to your map, save it, then restart the editor and comment out the line below.
*/
///! external ObjectMerger w3a Adef uDex anam "Detect Leave" ansf "(UnitDex)" aart "" acat "" arac 0
/* ________________________________________________________________________
* 2. Configuration
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*/
private module UnitDexConfig
// The raw code of the leave detection ability.
static constant integer DETECT_LEAVE_ABILITY = 'uDex'
// Allow debug messages (debug mode must also be on)
static constant boolean ALLOW_DEBUGGING = true
// Uncomment the lines below to define a filter for units being indexed
/*static method onFilter takes unit u returns boolean
return true
endmethod*/
endmodule
/* _________________________________________________________________________
* 3. Function API
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* Every function inlines except for UnitDexRemove
*
* function GetUnitId takes unit whichUnit returns integer
* function GetUnitById takes integer index returns unit
*
* function UnitDexEnable takes boolean flag returns nothing
* function UnitDexRemove takes unit u, boolean runEvents returns boolean
*
* function IsUnitIndexed takes unit u returns boolean
* function IsIndexingEnabled takes nothing returns boolean
*
* function GetIndexedUnit takes nothing returns unit
* function GetIndexedUnitId takes nothing returns integer
*
* function RegisterUnitIndexEvent takes boolexpr func, integer eventtype returns indexevent
* function RemoveUnitIndexEvent takes triggercondition c, integer eventtype returns nothing
* function TriggerRegisterUnitIndexEvent takes trigger t, integer eventtype returns nothing
*
* function OnUnitIndex takes code func returns triggercondition
* function OnUnitDeidex takes code func returns triggercondition
* _________________________________________________________________________
* 4. Struct API
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* UnitDex.Enabled = false // toggle the indexer
* UnitDex.Initialized // returns true if the preload timer has finished
* UnitDex.Count // returns the amount of units indexed
* UnitDex.Unit[0] // access the UnitDex array directly
* UnitDex.Group // a unit group containing every indexed unit (for enumeration)
* UnitDex.LastIndex // returns the last indexed unit's id
* _________________________________________________________________________
* 5. Public Variables
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* These are to be used with the "eventtype" argument of the event API:
*
* constant integer EVENT_UNIT_INDEX = 0
* constant integer EVENT_UNIT_DEINDEX = 1
* _________________________________________________________________________
* 6. Examples
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* 1. Associate a unit with a variable
*
* set UnitFlag[GetUnitId(yourUnit)] = true
*
* 2. Allocate a struct instance using a units assigned ID
*
* local somestruct data = GetUnitId(yourUnit)
*
* 3. Detect when a unit leaves the map
*
* function Exit takes nothing returns nothing
* call BJDebugMsg("The unit " + GetUnitName(GetIndexedUnit()) + " has left the map.")
* endfunction
*
* call OnUnitDeindex(function Exit)
* // or
* call RegisterUnitIndexEvent(Filter(function Exit), EVENT_UNIT_DEINDEX)
*
*
* _________________________________________________________________________
* 7. How it works
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* 1. Enumerate all preplaced units
* 2. TriggerRegisterEnterRegion native to detect when a unit enters the map
* 3. Assign each unit that enters the map a unique integer
* 4. Give every unit an ability based off of defend. There is no native to accurately
* detect when a unit leaves the map but when a unit dies or is removed from the game
* they are issued the "undefend" order
* 5. Catch the "undefend" order and remove unit from the queue
* _________________________________________________________________________
* 8. Notes
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* - This system is compatable with GUI because it utilizes UnitUserData (custom values for units).
* - The object merger line should be commented out after saving and restarting.
* - All public functions are inlined except UnitDexRemove.
*
* -http://www.hiveworkshop.com/forums/submissions-414/unitdex-lightweight-unit-indexer-248209/
*
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*/
globals
// Event types
constant integer EVENT_UNIT_INDEX = 0
constant integer EVENT_UNIT_DEINDEX = 1
// System variables
private trigger array IndexTrig
private integer Index = 0
private real E=-1
private boolexpr FilterEnter
endglobals
function GetUnitId takes unit whichUnit returns integer
return GetUnitUserData(whichUnit)
endfunction
function GetUnitById takes integer index returns unit
return UnitDex.Unit[index]
endfunction
function GetIndexedUnit takes nothing returns unit
return UnitDex.Unit[UnitDex.LastIndex]
endfunction
function GetIndexedUnitId takes nothing returns integer
return UnitDex.LastIndex
endfunction
function IsUnitIndexed takes unit u returns boolean
return (GetUnitById(GetUnitId(u)) != null)
endfunction
function UnitDexEnable takes boolean flag returns nothing
set UnitDex.Enabled = flag
endfunction
function IsIndexingEnabled takes nothing returns boolean
return UnitDex.Enabled
endfunction
function RegisterUnitIndexEvent takes boolexpr func, integer eventtype returns triggercondition
return TriggerAddCondition(IndexTrig[eventtype], func)
endfunction
function RemoveUnitIndexEvent takes triggercondition c, integer eventtype returns nothing
call TriggerRemoveCondition(IndexTrig[eventtype], c)
endfunction
function TriggerRegisterUnitIndexEvent takes trigger t, integer eventtype returns nothing
call TriggerRegisterVariableEvent(t, SCOPE_PRIVATE + "E", EQUAL, eventtype)
endfunction
function OnUnitIndex takes code func returns triggercondition
return TriggerAddCondition(IndexTrig[EVENT_UNIT_INDEX], Filter(func))
endfunction
function OnUnitDeindex takes code func returns triggercondition
return TriggerAddCondition(IndexTrig[EVENT_UNIT_DEINDEX], Filter(func))
endfunction
function UnitDexRemove takes unit u, boolean runEvents returns boolean
return UnitDex.Remove(u, runEvents)
endfunction
/****************************************************************/
private keyword UnitDexCore
struct UnitDex extends array
static boolean Enabled = true
readonly static integer LastIndex
readonly static boolean Initialized=false
readonly static group Group=CreateGroup()
readonly static unit array Unit
readonly static integer Count = 0
readonly static integer array List
implement UnitDexConfig
private static integer Counter = 0
implement UnitDexCore
endstruct
/****************************************************************/
private module UnitDexCore
static method Remove takes unit u, boolean runEvents returns boolean
local integer i
if (IsUnitIndexed(u)) then
set i = GetUnitId(u)
set UnitDex.List[i] = Index
set Index = i
call GroupRemoveUnit(UnitDex.Group, u)
call SetUnitUserData(u, 0)
if (runEvents) then
set UnitDex.LastIndex = i
set E = EVENT_UNIT_DEINDEX
call TriggerEvaluate(IndexTrig[EVENT_UNIT_DEINDEX])
set E = -1
endif
set UnitDex.Unit[i] = null
set UnitDex.Count = UnitDex.Count - 1
return true
endif
return false
endmethod
private static method onGameStart takes nothing returns nothing
local integer i = 0
static if (not LIBRARY_GroupUtils) then // Check if GroupUtils exists so we can use it's enumeration group.
local group ENUM_GROUP = CreateGroup() // If not, create the group.
endif
// Index preplaced units
loop
call GroupEnumUnitsOfPlayer(ENUM_GROUP, Player(i), FilterEnter)
set i = i + 1
exitwhen i == bj_MAX_PLAYER_SLOTS
endloop
static if (not LIBRARY_GroupUtils) then
call DestroyGroup(ENUM_GROUP)
set ENUM_GROUP = null
endif
// run init triggers
set i = 1
loop
exitwhen i > Counter
set LastIndex = i
call TriggerEvaluate(IndexTrig[EVENT_UNIT_INDEX])
set E = EVENT_UNIT_INDEX
set E = -1
set i = i + 1
endloop
set LastIndex = Counter
set Initialized = true
set FilterEnter = null
call DestroyTimer(GetExpiredTimer())
endmethod
private static method onEnter takes nothing returns boolean
local unit u = GetFilterUnit()
local integer i = GetUnitId(u)
local integer t = Index
if (i == 0 and thistype.Enabled) then
// If a filter was defined pass the unit through it.
static if (thistype.onFilter.exists) then
if (not thistype.onFilter(u)) then
set u = null
return false // check failed
endif
endif
// Handle debugging
static if (thistype.DEBUG_MODE and thistype.ALLOW_DEBUGGING) then
if (t == 0 and Counter+1 >= JASS_MAX_ARRAY_SIZE) then
call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "UnitDex: Maximum number of units reached!")
set u = null
return false
endif
endif
// Add to group of indexed units
call GroupAddUnit(thistype.Group, u)
// Give unit the leave detection ability
call UnitAddAbility(u, thistype.DETECT_LEAVE_ABILITY)
call UnitMakeAbilityPermanent(u, true, thistype.DETECT_LEAVE_ABILITY)
// Allocate index
if (Index != 0) then
set Index = List[t]
else
set Counter = Counter + 1
set t = Counter
endif
set List[t] = -1
set LastIndex = t
set thistype.Unit[t] = u
set Count = Count + 1
// Store the index
call SetUnitUserData(u, t)
if (thistype.Initialized) then
// Execute custom events registered with RegisterUnitIndexEvent
call TriggerEvaluate(IndexTrig[EVENT_UNIT_INDEX])
// Handle TriggerRegisterUnitIndexEvent
set E = EVENT_UNIT_INDEX
// Reset so the event can occur again
set E = -1
endif
endif
set u = null
return false
endmethod
private static method onLeave takes nothing returns boolean
local unit u
local integer i
// Check if order is undefend.
if (thistype.Enabled and GetIssuedOrderId() == 852056) then
set u = GetTriggerUnit()
// If unit was killed (not removed) then don't continue
if (GetUnitAbilityLevel(u, thistype.DETECT_LEAVE_ABILITY) != 0) then
set u = null
return false
endif
set i = GetUnitId(u)
// If unit has been indexed then deindex it
if (i > 0 and i <= Counter and u == GetUnitById(i)) then
// Recycle the index
set List[i] = Index
set Index = i
set LastIndex = i
// Remove to group of indexed units
call GroupRemoveUnit(thistype.Group, u)
// Execute custom events without any associated triggers
call TriggerEvaluate(IndexTrig[EVENT_UNIT_DEINDEX])
// Handle TriggerRegisterUnitIndexEvent
set E = EVENT_UNIT_DEINDEX
// Remove entry
call SetUnitUserData(u, 0)
set thistype.Unit[i] = null
// Decrement unit count
set Count = Count - 1
// Reset so the event can occur again
set E = -1
endif
set u = null
endif
return false
endmethod
private static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
local player p
local unit u
static if (not LIBRARY_WorldBounds) then // Check if WorldBounts exists, if not then define the necessary vars
local region reg = CreateRegion() // If WorldBounds wasn't found, create the region manually
local rect world = GetWorldBounds()
endif
set FilterEnter = Filter(function thistype.onEnter)
// Begin to index units when they enter the map
static if (LIBRARY_WorldBounds) then
call TriggerRegisterEnterRegion(CreateTrigger(), WorldBounds.worldRegion, FilterEnter)
else
call RegionAddRect(reg, world)
call TriggerRegisterEnterRegion(CreateTrigger(), reg, FilterEnter)
call RemoveRect(world)
set world = null
endif
call TriggerAddCondition(t, Filter(function thistype.onLeave))
set IndexTrig[EVENT_UNIT_INDEX] = CreateTrigger()
set IndexTrig[EVENT_UNIT_DEINDEX] = CreateTrigger()
loop
set p = Player(i)
// Detect "undefend"
call TriggerRegisterPlayerUnitEvent(t, p, EVENT_PLAYER_UNIT_ISSUED_ORDER, null)
// Hide the detect ability from players
call SetPlayerAbilityAvailable(p, thistype.DETECT_LEAVE_ABILITY, false)
set i = i + 1
exitwhen i == bj_MAX_PLAYER_SLOTS
endloop
call TimerStart(CreateTimer(), 0, false, function thistype.onGameStart)
endmethod
endmodule
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
library Stack /* v1.0.0.7
************************************************************************************
*
* */uses/*
*
* */ ErrorMessage /* hiveworkshop.com/forums/submissions-414/snippet-error-message-239210/
*
************************************************************************************
*
* module Stack
*
* Description
* -------------------------
*
* NA
*
* Fields
* -------------------------
*
* readonly static integer sentinel
*
* readonly thistype first
* readonly thistype next
*
* Methods
* -------------------------
*
* static method create takes nothing returns thistype
* method destroy takes nothing returns nothing
* - May only destroy stacks
*
* method push takes nothing returns thistype
* method pop takes nothing returns nothing
*
* method clear takes nothing returns nothing
*
* debug static method calculateMemoryUsage takes nothing returns integer
* debug static method getAllocatedMemoryAsString takes nothing returns string
*
************************************************************************************/
module Stack
private static thistype collectionCount = 0
private static thistype nodeCount = 0
debug private boolean isNode
debug private boolean isCollection
private thistype _next
method operator next takes nothing returns thistype
debug call ThrowError(this == 0, "Stack", "next", "thistype", this, "Attempted To Go Out Of Bounds.")
debug call ThrowError(not isNode, "Stack", "next", "thistype", this, "Attempted To Read Invalid Node.")
return _next
endmethod
private thistype _first
method operator first takes nothing returns thistype
debug call ThrowError(this == 0, "Stack", "first", "thistype", this, "Attempted To Read Null Stack.")
debug call ThrowError(not isCollection, "Stack", "first", "thistype", this, "Attempted To Read Invalid Stack.")
return _first
endmethod
static method operator sentinel takes nothing returns integer
return 0
endmethod
private static method allocateCollection takes nothing returns thistype
local thistype this = thistype(0)._first
if (0 == this) then
debug call ThrowError(collectionCount == 8191, "Stack", "allocateCollection", "thistype", 0, "Overflow.")
set this = collectionCount + 1
set collectionCount = this
else
set thistype(0)._first = _first
endif
return this
endmethod
private static method allocateNode takes nothing returns thistype
local thistype this = thistype(0)._next
if (0 == this) then
debug call ThrowError(nodeCount == 8191, "Stack", "allocateNode", "thistype", 0, "Overflow.")
set this = nodeCount + 1
set nodeCount = this
else
set thistype(0)._next = _next
endif
return this
endmethod
static method create takes nothing returns thistype
local thistype this = allocateCollection()
debug set isCollection = true
set _first = 0
return this
endmethod
method push takes nothing returns thistype
local thistype node = allocateNode()
debug call ThrowError(this == 0, "Stack", "push", "thistype", this, "Attempted To Push On To Null Stack.")
debug call ThrowError(not isCollection, "Stack", "push", "thistype", this, "Attempted To Push On To Invalid Stack.")
debug set node.isNode = true
set node._next = _first
set _first = node
return node
endmethod
method pop takes nothing returns nothing
local thistype node = _first
debug call ThrowError(this == 0, "Stack", "pop", "thistype", this, "Attempted To Pop Null Stack.")
debug call ThrowError(not isCollection, "Stack", "pop", "thistype", this, "Attempted To Pop Invalid Stack.")
debug call ThrowError(node == 0, "Stack", "pop", "thistype", this, "Attempted To Pop Empty Stack.")
debug set node.isNode = false
set _first = node._next
set node._next = thistype(0)._next
set thistype(0)._next = node
endmethod
private method getBottom takes nothing returns thistype
set this = _first
loop
exitwhen _next == 0
set this = _next
endloop
return this
endmethod
method clear takes nothing returns nothing
debug local thistype node = _first
debug call ThrowError(this == 0, "Stack", "clear", "thistype", this, "Attempted To Clear Null Stack.")
debug call ThrowError(not isCollection, "Stack", "clear", "thistype", this, "Attempted To Clear Invalid Stack.")
static if DEBUG_MODE then
loop
exitwhen node == 0
set node.isNode = false
set node = node._next
endloop
endif
if (_first == 0) then
return
endif
set getBottom()._next = thistype(0)._next
set thistype(0)._next = _first
set _first = 0
endmethod
method destroy takes nothing returns nothing
debug call ThrowError(this == 0, "Stack", "destroy", "thistype", this, "Attempted To Destroy Null Stack.")
debug call ThrowError(not isCollection, "Stack", "destroy", "thistype", this, "Attempted To Destroy Invalid Stack.")
static if DEBUG_MODE then
debug call clear()
debug set isCollection = false
else
if (_first != 0) then
set getBottom()._next = thistype(0)._next
set thistype(0)._next = _first
endif
endif
set _first = thistype(0)._first
set thistype(0)._first = this
endmethod
static if DEBUG_MODE then
static method calculateMemoryUsage takes nothing returns integer
local thistype start = 1
local thistype end = 8191
local integer count = 0
loop
exitwhen integer(start) > integer(end)
if (integer(start) + 500 > integer(end)) then
return count + checkRegion(start, end)
else
set count = count + checkRegion(start, start + 500)
set start = start + 501
endif
endloop
return count
endmethod
private static method checkRegion takes thistype start, thistype end returns integer
local integer count = 0
loop
exitwhen integer(start) > integer(end)
if (start.isNode) then
set count = count + 1
endif
if (start.isCollection) then
set count = count + 1
endif
set start = start + 1
endloop
return count
endmethod
static method getAllocatedMemoryAsString takes nothing returns string
local thistype start = 1
local thistype end = 8191
local string memory = null
loop
exitwhen integer(start) > integer(end)
if (integer(start) + 500 > integer(end)) then
if (memory != null) then
set memory = memory + ", "
endif
set memory = memory + checkRegion2(start, end)
set start = end + 1
else
if (memory != null) then
set memory = memory + ", "
endif
set memory = memory + checkRegion2(start, start + 500)
set start = start + 501
endif
endloop
return memory
endmethod
private static method checkRegion2 takes thistype start, thistype end returns string
local string memory = null
loop
exitwhen integer(start) > integer(end)
if (start.isNode) then
if (memory == null) then
set memory = I2S(start)
else
set memory = memory + ", " + I2S(start) + "N"
endif
endif
if (start.isCollection) then
if (memory == null) then
set memory = I2S(start)
else
set memory = memory + ", " + I2S(start) + "C"
endif
endif
set start = start + 1
endloop
return memory
endmethod
endif
endmodule
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
library ErrorMessage /* v1.0.2.0
*************************************************************************************
*
* Issue Compliant Error Messages
*
************************************************************************************
*
* function ThrowError takes boolean expression, string libraryName, string functionName, string objectName, integer objectInstance, string description returns nothing
* - In the event of an error the game will be permanently paused
*
* function ThrowWarning takes boolean expression, string libraryName, string functionName, string objectName, integer objectInstance, string description returns nothing
*
************************************************************************************/
private struct Fields extends array
static constant string COLOR_RED = "|cffff0000"
static constant string COLOR_YELLOW = "|cffffff00"
static string lastError = null
endstruct
private function Pause takes nothing returns nothing
call PauseGame(true)
endfunction
private function ThrowMessage takes string libraryName, string functionName, string objectName, integer objectInstance, string description, string errorType, string color returns nothing
local string str
local string color_braces = "|cff66FF99"
local string orange = "|cffff6600"
set str = "->\n-> " + color_braces + "{|r " + "Library" + color_braces + "(" + orange + libraryName + color_braces + ")"
if (objectName != null) then
if (objectInstance != 0) then
set str = str + "|r.Object" + color_braces + "(" + orange + objectName + color_braces + " (|rinstance = " + orange + I2S(objectInstance) + color_braces + ") )" + "|r." + "Method" + color_braces + "(" + orange + functionName + color_braces + ")"
else
set str = str + "|r.Object" + color_braces + "(" + orange + objectName + color_braces + ")|r." + "Method" + color_braces + "(" + orange + functionName + color_braces + ")"
endif
else
set str = str + "|r." + "Function" + color_braces + "(" + orange + functionName + color_braces + ")"
endif
set str = str + color_braces + " }|r " + "has thrown an exception of type " + color_braces + "(" + color + errorType + color_braces + ")|r."
set Fields.lastError = str + "\n->\n" + "-> " + color + description + "|r\n->"
endfunction
function ThrowError takes boolean expression, string libraryName, string functionName, string objectName, integer objectInstance, string description returns nothing
if (Fields.lastError != null) then
set objectInstance = 1/0
endif
if (expression) then
call ThrowMessage(libraryName, functionName, objectName, objectInstance, description, "Error", Fields.COLOR_RED)
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60000,Fields.lastError)
call TimerStart(CreateTimer(), 0, true, function Pause)
set objectInstance = 1/0
endif
endfunction
function ThrowWarning takes boolean expression, string libraryName, string functionName, string objectName, integer objectInstance, string description returns nothing
if (Fields.lastError != null) then
set objectInstance = 1/0
endif
if (expression) then
call ThrowMessage(libraryName, functionName, objectName, objectInstance, description, "Warning", Fields.COLOR_YELLOW)
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60000,Fields.lastError)
set Fields.lastError = null
endif
endfunction
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
/**
* IsTerrainWalkable snippet for estimating the walkability status of a co-ordinate pair, credits
* to Anitarf and Vexorian.
*
* API:
* boolean IsTerrainWalkable(real x, real y) - returns the walkability of (x,y)
*/
library IsTerrainWalkable initializer init
globals
// this value is how far from a point the item may end up for the point to be considered pathable
private constant real MAX_RANGE=10.
// the following two variables are set to the position of the item after each pathing check
// that way, if a point isn't pathable, these will be the coordinates of the nearest point that is
public real X=0.
public real Y=0.
private rect r
private item check
private item array hidden
private integer hiddenMax=0
endglobals
private function init takes nothing returns nothing
set check=CreateItem('ciri',0.,0.)
call SetItemVisible(check,false)
set r=Rect(0.0,0.0,128.0,128.0)
endfunction
private function hideBothersomeItem takes nothing returns nothing
if IsItemVisible(GetEnumItem()) then
set hidden[hiddenMax]=GetEnumItem()
call SetItemVisible(hidden[hiddenMax],false)
set hiddenMax=hiddenMax+1
endif
endfunction
function IsTerrainWalkable takes real x, real y returns boolean
// first, hide any items in the area so they don't get in the way of our item
call MoveRectTo(r,x,y)
call EnumItemsInRect(r,null,function hideBothersomeItem)
// try to move the check item and get its coordinates
// this unhides the item...
call SetItemPosition(check,x,y)
set X=GetItemX(check)
set Y=GetItemY(check)
//...so we must hide it again
call SetItemVisible(check,false)
// before returning, unhide any items that got hidden at the start
loop
exitwhen hiddenMax==0
set hiddenMax=hiddenMax-1
call SetItemVisible(hidden[hiddenMax],true)
endloop
// return pathability status
return (x-X)*(x-X)+(y-Y)*(y-Y)<MAX_RANGE*MAX_RANGE
endfunction
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope SnareTrap
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Snare Trap v1.0
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//
// I. Description
// Tosses a snaring net to the target location. Enemy units
// within 200 range away will be snared by the net and have
// it's movement speed reduced by 75% and has 303% chance to
// miss it's attacks.
//
// Level 1 - Nets last for 6 seconds.
// Level 2 - Nets last for 8 seconds.
// Level 3 - Nets last for 10 seconds.
//
// Tosses an invisible trap instead if there is no enemy
// units around on cast. The trap will spring a snaring
// net 1 second after any enemy unit walks over it.
//
// II. Requirements
// • Missile by BPower | hiveworkshop.com/threads/missile.265370/
// • TimerUtils by Vexorian | wc3c.net/showthread.php?t=101322
//
// III. How to import
// • Import dummy.mdx from import manager to your map. Other files are optional
// • Import the following object data to your map:
// (Unit)
// • Snare Trap (Net)
// • Snare Trap (Trap)
// • Dummy Caster
// • Missile Dummy
// (Ability)
// • Snare Trap (Miss)
// • Snare Trap (Slow)
// • Snare Trap (Main)
// (Buff)
// • Snare Trap (Buff)
// • Make sure "Snare Trap (Miss)" & "(Slow)" abilities both has "Snare Trap (Buff)" as buff
// • Import and configure required libraries properly
// • Import Snare Trap trigger and configure it properly
//
// IV. Credits
// • HappyCockroach : spell concept
// • BPower : Missile library
// • Vexorian : TimerUtils library, dummy.mdx
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Configurations
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
// A. Static configurations
globals
// 1. Snare Trap main ability raw code
private constant integer MAIN_SPELL_ID = 'A000'
//
// 2. Snare Trap (Miss) ability raw code
private constant integer MISS_SPELL_ID = 'A001'
//
// 3. Snare Trap (Slow) ability raw code
private constant integer SLOW_SPELL_ID = 'A002'
//
// 4. Snare Trap (Buff) buff raw code
private constant integer NET_BUFF_ID = 'B000'
//
// 5. Dummy Caster unit raw code
private constant integer DUMMY_CASTER_ID = 'h000'
//
// 6. Snare Trap (Net) unit raw code
private constant integer NET_DUMMY_ID = 'h001'
//
// 7. Snare Trap (Trap) unit raw code
private constant integer TRAP_DUMMY_ID = 'h002'
//
// 8. Snare Trap (Miss) ability order id
private constant integer MISS_ORDER_ID = 852190 // "curse"
//
// 9. Snare Trap (Slow) ability order id
private constant integer SLOW_ORDER_ID = 852075 // "slow"
//
// 10. Net missile model filepath
private constant string NET_MISSILE_MODEL = "Abilities\\Spells\\Orc\\Ensnare\\EnsnareMissile.mdl"
//
// 11. Trap missile model filepath
private constant string TRAP_MISSILE_MODEL = "war3mapImported\\Swashbuckler_SnareTrap_Projectile.MDX"
//
// 12. Springing trap special effect filepath
private constant string SPRUNG_EFFECT_MODEL = "Objects\\Spawnmodels\\Undead\\ImpaleTargetDust\\ImpaleTargetDust.mdl"
//
// 13. If true, apply expiration timer bar to trap unit
private constant boolean APPLY_EXP_TIMER = true
//
// 14. Missile scale/size
private constant real NET_MISSILE_SCALE = 1.5
//
// 15. Missile scale/size
private constant real TRAP_MISSILE_SCALE = 1.0
//
// 16. Missile launching vertical (height) offset
private constant real MISSILE_LAUNCH_VOFFSET = 100.0
//
// 17. Missile launching horizontal (xy) offset
private constant real MISSILE_LAUNCH_HOFFSET = 65.0
//
// 18. Missile move speed
private constant real MISSILE_SPEED = 15.0
//
// 19. Missile trajectory arc
private constant real MISSILE_ARC = 0.15
//
// Better not to modify this
private constant real INTERVAL = 0.03125000
endglobals
//
// B. Dynamic configurations
//
// 20. Net maximum snaring range (area of effect)
private constant function NetAoE takes integer level returns real
return 200.0
endfunction
//
// 21. How long the net lasts before expired (in second)
private constant function NetLifespan takes integer level returns real
return 4.0 + 2.0*level
endfunction
//
// 22. How long the trap lasts before expired (in second)
private constant function TrapLifespan takes integer level returns real
return 180.0
endfunction
//
// 23. How long the trap needs to spring (in second)
private constant function TrapSpringDelay takes integer level returns real
return 1.0
endfunction
//
// 24. Target classifications that can be affected by the net
// • target = target
// • caster = owner of the net/trap (player)
// Current targets: enemy, non-structure, ground, amphibious
private constant function SpellTargets takes unit target, player caster returns boolean
return IsUnitEnemy(target, caster) and not IsUnitType(target, UNIT_TYPE_STRUCTURE) and (IsUnitType(target, UNIT_TYPE_GROUND) or not IsUnitType(target, UNIT_TYPE_FLYING)) // Looks weird but this is how to detect both ground and amphibious units
endfunction
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
native UnitAlive takes unit id returns boolean
globals
private constant integer STOP_ORDER_ID = 851972 // "stop"
endglobals
private struct SnareTrap
unit net
group captured
boolean capture
boolean sprung // true means it's not a trap anymore
integer level
player owner
real targetX
real targetY
real lifespan // Lifespan of both net and trap
real delay // Delay for the trap to sprung
real aoe
static unit DummyCaster
static timer CheckerTimer = CreateTimer()
static group TargetGroup = CreateGroup() // Group containing units affected by the spells buff
static group AffectedGroup = CreateGroup() // Group containing any units iterated by the spell
static group RecycleGroup = CreateGroup() // For recycling purpose
static group TempGroup = CreateGroup()
static group TempGroup2
static method check takes nothing returns nothing
local unit fog
loop
set fog = FirstOfGroup(TargetGroup)
exitwhen (fog == null)
call GroupRemoveUnit(TargetGroup, fog)
// Remove buff if a unit is still in target group (has the spell buff)
// but not iterated by the spell at all. Means the unit is nowhere around
// any net
if (not UnitAlive(fog) or not IsUnitInGroup(fog, AffectedGroup)) then
// Need to remove the buff twice because the two abilties
// are using the same buff
call UnitRemoveAbility(fog, NET_BUFF_ID)
call UnitRemoveAbility(fog, NET_BUFF_ID)
else
// Return the unit to the new target group if still snared
call GroupAddUnit(RecycleGroup, fog)
endif
endloop
// Recycle group
set TempGroup2 = TargetGroup
set TargetGroup = RecycleGroup
set RecycleGroup = TempGroup2
call GroupClear(AffectedGroup)
// Reset and pause the checker timer if there's no affected unit's left
if (FirstOfGroup(TargetGroup) == null) then
call PauseTimer(CheckerTimer)
endif
endmethod
static method onPeriodic takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
local unit fog
local real a
local real x
local real y
if (.lifespan > INTERVAL) then
if (.sprung) then
if (.delay == 0) then
set .lifespan = .lifespan - INTERVAL
call GroupEnumUnitsInRange(TempGroup, .targetX, .targetY, .aoe, null)
loop
set fog = FirstOfGroup(TempGroup)
exitwhen (fog == null)
call GroupRemoveUnit(TempGroup, fog)
// If target is classified
if (UnitAlive(fog) and SpellTargets(fog, .owner)) then
if (not IsUnitInGroup(fog, TargetGroup)) then
// Apply slow and miss chance buff
call IssueTargetOrderById(DummyCaster, SLOW_ORDER_ID, fog)
call IssueTargetOrderById(DummyCaster, MISS_ORDER_ID, fog)
if (.capture) then
call GroupAddUnit(.captured, fog)
endif
// Make sure the target has the buffs
if (GetUnitAbilityLevel(fog, NET_BUFF_ID) > 0) then
if (FirstOfGroup(TargetGroup) == null) then
// Start the timer to check that affected units are still snared (within AoE)
call TimerStart(CheckerTimer, INTERVAL, true, function thistype.check)
endif
call GroupAddUnit(TargetGroup, fog)
endif
endif
call GroupAddUnit(AffectedGroup, fog)
endif
endloop
set .capture = false
if (FirstOfGroup(.captured) != null) then
loop
set fog = FirstOfGroup(.captured)
exitwhen (fog == null)
call GroupRemoveUnit(.captured, fog)
// Prevent unit to be removed when outside the rope area
call GroupAddUnit(AffectedGroup, fog)
call GroupAddUnit(RecycleGroup, fog)
set x = GetUnitX(fog)
set y = GetUnitY(fog)
// Prevent captured unit from leaving
if ((.targetX-x)*(.targetX-x)+(.targetY-y)*(.targetY-y) > .aoe*.aoe) then
call IssueImmediateOrderById(fog, STOP_ORDER_ID)
set a = Atan2(y-.targetY, x-.targetX)
call SetUnitX(fog, .targetX+.aoe*Cos(a))
call SetUnitY(fog, .targetY+.aoe*Sin(a))
endif
endloop
// Recycle group
set TempGroup2 = .captured
set .captured = RecycleGroup
set RecycleGroup = TempGroup2
endif
else
// Springing trap
set .delay = .delay - INTERVAL
if (.delay <= 0) then
call RemoveUnit(.net)
call DestroyEffect(AddSpecialEffect(SPRUNG_EFFECT_MODEL, .targetX, .targetY))
set .net = CreateUnit(.owner, NET_DUMMY_ID, .targetX, .targetY, 0)
call SetUnitAnimation(.net, "birth")
call QueueUnitAnimation(.net, "stand")
set .lifespan = NetLifespan(.level)
set .delay = 0
endif
endif
else
set .lifespan = .lifespan - INTERVAL
// Check if there is any target unit passing by the trap
call GroupEnumUnitsInRange(TempGroup, .targetX, .targetY, .aoe, null)
loop
set fog = FirstOfGroup(TempGroup)
exitwhen (fog == null)
call GroupRemoveUnit(TempGroup, fog)
if (UnitAlive(fog) and SpellTargets(fog, .owner)) then
set .sprung = true
exitwhen true
endif
endloop
if (fog != null) then
call GroupClear(TempGroup)
set fog = null
endif
endif
else
// Dispose the spell instance
call DestroyGroup(.captured)
call KillUnit(.net)
call ReleaseTimer(t)
call deallocate()
set .captured = null
set .net = null
endif
set t = null
endmethod
static method onRemove takes Missile missile returns boolean
local thistype this = missile.data
// Hoping the missile library handles world bounds correctly yeah I'm lazy so what?
set .targetX = missile.x
set .targetY = missile.y
// Check whether the missile should be a trap or a net
if (.sprung) then
set .net = CreateUnit(.owner, NET_DUMMY_ID, .targetX, .targetY, 0)
call SetUnitAnimation(.net, "birth")
call QueueUnitAnimation(.net, "stand")
else
set .net = CreateUnit(.owner, TRAP_DUMMY_ID, .targetX, .targetY, 0)
static if APPLY_EXP_TIMER then
call UnitApplyTimedLife(.net, 'BTLF', .lifespan)
endif
endif
call TimerStart(NewTimerEx(this), INTERVAL, true, function thistype.onPeriodic)
return true
endmethod
implement MissileStruct
static method onCast takes nothing returns boolean
local Missile missile
local thistype this
local unit fog
local unit caster
local real angle
local real x
local real y
if (GetSpellAbilityId() == MAIN_SPELL_ID) then
set this = allocate()
set caster = GetTriggerUnit()
set .owner = GetTriggerPlayer()
set .targetX = GetSpellTargetX()
set .targetY = GetSpellTargetY()
set .captured = CreateGroup()
set .capture = true
set .sprung = false
// Initialize the missile
set x = GetUnitX(caster)
set y = GetUnitY(caster)
set angle = Atan2(.targetY-y, .targetX-x)
set missile = Missile.createXYZ(x+MISSILE_LAUNCH_HOFFSET*Cos(angle), y+MISSILE_LAUNCH_HOFFSET*Sin(angle), MISSILE_LAUNCH_VOFFSET+GetUnitFlyHeight(caster), .targetX, .targetY, 0)
set missile.speed = MISSILE_SPEED
set missile.arc = MISSILE_ARC
set missile.data = this
set .level = GetUnitAbilityLevel(caster, MAIN_SPELL_ID)
set .aoe = NetAoE(.level)
// Check if there's target units around the target area
call GroupEnumUnitsInRange(TempGroup, .targetX, .targetY, .aoe, null)
loop
set fog = FirstOfGroup(TempGroup)
exitwhen (fog == null)
call GroupRemoveUnit(TempGroup, fog)
if (UnitAlive(fog) and SpellTargets(fog, .owner)) then
set .sprung = true
exitwhen true
endif
endloop
if (fog != null) then
call GroupClear(TempGroup)
set fog = null
endif
// If a target is detected, throw a net, else, a trap
if (.sprung) then
set missile.model = NET_MISSILE_MODEL
set missile.scale = NET_MISSILE_SCALE
set .lifespan = NetLifespan(.level)
set .delay = 0
else
set missile.model = TRAP_MISSILE_MODEL
set missile.scale = TRAP_MISSILE_SCALE
set .lifespan = TrapLifespan(.level)
set .delay = TrapSpringDelay(.level)
endif
call launch(missile)
set caster = null
endif
return false
endmethod
static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition(t, Condition(function thistype.onCast))
// Create dummy caster to apply buffs
set DummyCaster = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), DUMMY_CASTER_ID, 0, 0, 0)
call UnitAddAbility(DummyCaster, SLOW_SPELL_ID)
call UnitAddAbility(DummyCaster, MISS_SPELL_ID)
endmethod
endstruct
endscope
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope Persecute
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Persecute v1.0
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//
// I. Description
// Throws a paintball at the target. Marking it as a wanted
// target. Marked unit will leave traces as it travels.
// Ally units who's targeting it will have their attack
// speed and move speed increased. The mark lasts for 15
// seconds.
//
// Level 1 - 15% attack speed bonus. 10% move speed bonus.
// Level 2 - 25% attack speed bonus. 15% move speed bonus.
// Level 3 - 35% attack speed bonus. 20% move speed bonus.
//
// II. Requirements
// • Missile by BPower | hiveworkshop.com/threads/missile.265370/
// • UnitIndexer by TriggerHappy | hiveworkshop.com/threads/system-unitdex-unit-indexer.248209/
// • TimerUtils by Vexorian | wc3c.net/showthread.php?t=101322
//
// III. How to import
// • Import dummy.mdx from import manager to your map. Other files are optional
// • Import the following object data to your map:
// (Unit)
// • Dummy Caster
// • Missile Dummy
// (Ability)
// • Persecute (Buff)
// • Persecute (Mark)
// • Persecute (Main)
// (Buff)
// • Persecute (Buff)
// • Persecute (Mark)
// • Make sure "Persecute (Buff)" ability has "Persecute (Buff)" as buff
// • Make sure "Persecute (Mark)" ability has "Persecute (Mark)" as buff
// • Import and configure required libraries properly
// • Import Persecute trigger and configure it properly
//
// IV. Credits
// • HappyCockroach : spell concept
// • TriggerHappy : UnitIndexer library
// • Vexorian : TimerUtils library
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Configurations
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
// A. Static configurations
globals
// 1. Persecute main ability raw code
private constant integer MAIN_SPELL_ID = 'A00B'
//
// 2. Persecute (Mark) ability raw code
private constant integer MARK_SPELL_ID = 'A00C'
//
// 3. Persecute (Buff) ability raw code
private constant integer BONUS_SPELL_ID = 'A00D'
//
// 4. Persecute (Buff) buff raw code
private constant integer MARK_BUFF_ID = 'B003'
//
// 5. Persecute (Mark) buff raw code
private constant integer BONUS_BUFF_ID = 'B004'
//
// 6. Dummy Caster unit raw code
private constant integer DUMMY_CASTER_ID = 'h000'
//
// 7. Persecute (Mark) ability order id
private constant integer MARK_ORDER_ID = 852570 // "wandofshadowsight"
//
// 8. Persecute (Buff) ability order id
private constant integer BONUS_ORDER_ID = 852101 // "bloodlust"
//
// 9. Launched missile model file path
private constant string MISSILE_MODEL_PATH = "war3mapImported\\Swashbuckler_Persecute_Projectile.MDX"
//
// 10. Missile move speed
private constant real MISSILE_SPEED = 25.0
//
// 11. Missile trajectory arc
private constant real MISSILE_ARC = 0.15
//
// 12. Missile launching vertical (height) offset
private constant real MISSILE_LAUNCH_VOFFSET = 125.0
//
// 13. Missile launching horizontal (xy) offset
private constant real MISSILE_LAUNCH_HOFFSET = 65.0
//
// Interval at which the detection trigger will reset
// (Just don't modify if you don't know what it means) Congrats! If you are reading this, it means you are certified nerd
private constant real TRIGGER_RESET_INTERVAL = 30.0
//
// Better not to modify this
private constant real INTERVAL = 0.1
endglobals
//
// B. Dynamic configurations
//
// 14. Mark duration on normal units
private constant function MarkDurationNormal takes integer level returns real
return 20.0
endfunction
//
// 15. Mark duration on hero units
private constant function MarkDurationHero takes integer level returns real
return 10.0
endfunction
//
// 16. Classification of unit that can receive bonus stats when
// chasing a target with Persecute (Buff - Target) buff
// • chaser = unit that's targeting the marked unit
// • caster = owner of the caster (who gives the mark)
// • target = marked unit
// Current targets: enemy of target, ally of caster, non-mechanical, non-structure
private constant function AllowedChaser takes unit chaser, player caster, player target returns boolean
return IsUnitEnemy(chaser, target) and IsUnitAlly(chaser, caster) and not IsUnitType(chaser, UNIT_TYPE_MECHANICAL) and not IsUnitType(chaser, UNIT_TYPE_STRUCTURE)
endfunction
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
native UnitAlive takes unit id returns boolean
globals
private constant integer ATTACK_ORDER_ID = 851983 // "attack"
private constant integer SMART_ORDER_ID = 851971 // "smart"
private constant player PASSIVE = Player(PLAYER_NEUTRAL_PASSIVE)
private unit DummyCaster
endglobals
private struct PersecuteMark
unit target
real duration
static thistype array Index
static method onPeriodic takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
// If need the buff to be removed
if (.duration <= INTERVAL or not UnitAlive(.target) or GetUnitAbilityLevel(.target, MARK_BUFF_ID) == 0) then
set Index[GetUnitUserData(.target)] = 0
call UnitRemoveAbility(.target, MARK_BUFF_ID)
call ReleaseTimer(t)
call deallocate()
set .target = null
else
set .duration = .duration - INTERVAL
endif
set t = null
endmethod
static method apply takes Missile missile returns nothing
local thistype this
local integer data = GetUnitUserData(missile.target)
// If already registered
if (Index[data] != 0) then
set this = Index[data]
if (IsUnitType(missile.target, UNIT_TYPE_HERO)) then
set .duration = MarkDurationHero(missile.data)
else
set .duration = MarkDurationNormal(missile.data)
endif
else
set this = allocate()
set Index[data] = this
set .target = missile.target
// If target is a hero
if (IsUnitType(missile.target, UNIT_TYPE_HERO)) then
set .duration = MarkDurationHero(missile.data)
else
set .duration = MarkDurationNormal(missile.data)
endif
// If not have the buff yet
if (GetUnitAbilityLevel(missile.target, MARK_BUFF_ID) == 0) then
call SetUnitOwner(DummyCaster, missile.owner, false)
call IssueTargetOrderById(DummyCaster, MARK_ORDER_ID, missile.target)
call SetUnitOwner(DummyCaster, PASSIVE, false)
endif
call TimerStart(NewTimerEx(this), INTERVAL, true, function thistype.onPeriodic)
endif
endmethod
endstruct
private struct Persecute extends array
static group TempGroup
static group BonusGroup = CreateGroup() // Group containing units with bonus buff
static group DetectGroup = CreateGroup() // Group containing units registered in detection trigger
static group RecycleGroup = CreateGroup() // For recycling purpose
static timer ResetTimer = CreateTimer()
static timer CheckTimer = CreateTimer()
static trigger DetectTrigger = CreateTrigger()
static trigger LearnTrigger = CreateTrigger()
static integer array AbilityLevel
static player array CasterOwner
static unit array TargetUnit
static method removeBonus takes unit u returns nothing
if (IsUnitInGroup(u, BonusGroup)) then
call GroupRemoveUnit(BonusGroup, u)
call UnitRemoveAbility(u, BONUS_BUFF_ID)
// If there is no unit with the bonus buff anymore
if (FirstOfGroup(BonusGroup) == null) then
call PauseTimer(CheckTimer)
endif
endif
endmethod
static method check takes nothing returns nothing
local integer data
local unit fog
loop
set fog = FirstOfGroup(DetectGroup)
exitwhen (fog == null)
call GroupRemoveUnit(DetectGroup, fog)
call GroupAddUnit(RecycleGroup, fog)
if (IsUnitInGroup(fog, BonusGroup)) then
set data = GetUnitUserData(fog)
// Check if unit's target is still alive and still marked
if (not UnitAlive(TargetUnit[data]) or not IsUnitVisible(TargetUnit[data], GetOwningPlayer(fog)) or GetUnitAbilityLevel(TargetUnit[data], MARK_BUFF_ID) == 0) then
call removeBonus(fog)
endif
// Add compatibility with spell stealing abilities (no permanent buff)
elseif (GetUnitAbilityLevel(fog, BONUS_BUFF_ID) > 0) then
call UnitRemoveAbility(fog, BONUS_BUFF_ID)
endif
endloop
set TempGroup = DetectGroup
set DetectGroup = RecycleGroup
set RecycleGroup = TempGroup
endmethod
static method addBonus takes unit u, unit t returns nothing
if (not IsUnitInGroup(u, BonusGroup) and AllowedChaser(u, CasterOwner[GetUnitUserData(t)], GetOwningPlayer(t))) then
// If the unit is the first one to have bonus buff
if (FirstOfGroup(BonusGroup) == null) then
call TimerStart(CheckTimer, INTERVAL, true, function thistype.check)
endif
call GroupAddUnit(BonusGroup, u)
// Apply move and attack speed bonus based on ability level
call SetUnitAbilityLevel( DummyCaster, BONUS_SPELL_ID, AbilityLevel[GetUnitUserData(t)])
call IssueTargetOrderById(DummyCaster, BONUS_ORDER_ID, u)
endif
endmethod
static method onDetect takes nothing returns boolean
local unit t = GetEventTargetUnit()
local unit u = GetTriggerUnit()
set TargetUnit[GetUnitUserData(u)] = t
// If the target is marked
if (GetUnitAbilityLevel(t, MARK_BUFF_ID) > 0) then
call addBonus(u, t)
else
call removeBonus(u)
endif
set t = null
set u = null
return false
endmethod
static method onOrder takes nothing returns boolean
local integer order
local unit t = GetOrderTargetUnit()
local unit u = GetTriggerUnit()
if (t != null) then
set order = GetIssuedOrderId()
// Make sure the order is "attack"
if (order == ATTACK_ORDER_ID or (order == SMART_ORDER_ID and IsUnitEnemy(u, GetOwningPlayer(t)))) then
// Save the unit's current target
set TargetUnit[GetUnitUserData(u)] = t
// If the target is marked
if (GetUnitAbilityLevel(t, MARK_BUFF_ID) > 0) then
call addBonus(u, t)
else
call removeBonus(u)
endif
else
call removeBonus(u)
endif
else
call removeBonus(u)
endif
set t = null
set u = null
return false
endmethod
static method onRemove takes Missile missile returns boolean
local integer data
local unit fog
if (UnitAlive(missile.target)) then
set data = GetUnitUserData(missile.target)
set AbilityLevel[data] = GetUnitAbilityLevel(missile.source, MAIN_SPELL_ID)
set CasterOwner[data] = missile.owner
// Every unit can only have one mark
if (GetUnitAbilityLevel(missile.target, MARK_BUFF_ID) > 0) then
call UnitRemoveAbility(missile.target, MARK_BUFF_ID)
loop
set fog = FirstOfGroup(BonusGroup)
exitwhen (fog == null)
call GroupRemoveUnit(BonusGroup, fog)
if (TargetUnit[GetUnitUserData(fog)] == missile.target) then
call removeBonus(fog)
else
call GroupAddUnit(RecycleGroup, fog)
endif
endloop
set TempGroup = BonusGroup
set BonusGroup = RecycleGroup
set RecycleGroup = TempGroup
endif
// Re-apply to reset mark duration
call PersecuteMark.apply(missile)
call IssueTargetOrderById(DummyCaster, MARK_ORDER_ID, missile.target)
loop
set fog = FirstOfGroup(DetectGroup)
exitwhen (fog == null)
call GroupRemoveUnit(DetectGroup, fog)
call GroupAddUnit(RecycleGroup, fog)
// If unit's target is the marked unit
if (TargetUnit[GetUnitUserData(fog)] == missile.target) then
call addBonus(fog, missile.target)
endif
endloop
set TempGroup = DetectGroup
set DetectGroup = RecycleGroup
set RecycleGroup = TempGroup
endif
return true
endmethod
implement MissileStruct
static method onCast takes nothing returns boolean
local Missile missile
local unit caster
local unit target
local real xt
local real yt
local real x
local real y
local real a
if (GetSpellAbilityId() == MAIN_SPELL_ID) then
set caster = GetTriggerUnit()
set target = GetSpellTargetUnit()
set x = GetUnitX(caster)
set y = GetUnitY(caster)
set xt = GetUnitX(target)
set yt = GetUnitY(target)
set a = Atan2(yt-y, xt-x)
// Launch the paintball
set missile = Missile.createXYZ(x+MISSILE_LAUNCH_HOFFSET*Cos(a), y+MISSILE_LAUNCH_HOFFSET*Sin(a), MISSILE_LAUNCH_VOFFSET+GetUnitFlyHeight(caster), xt, yt, MISSILE_LAUNCH_VOFFSET+GetUnitFlyHeight(target))
set missile.owner = GetTriggerPlayer()
set missile.source = caster
set missile.target = target
set missile.arc = MISSILE_ARC
set missile.speed = MISSILE_SPEED
set missile.model = MISSILE_MODEL_PATH
set missile.data = GetUnitAbilityLevel(caster, MAIN_SPELL_ID)
call launch(missile)
set target = null
set caster = null
endif
return false
endmethod
static method resetTrigger takes nothing returns nothing
local unit fog
// Reset detection trigger
call DestroyTrigger(DetectTrigger)
set DetectTrigger = CreateTrigger()
call TriggerAddCondition(DetectTrigger, Condition(function thistype.onDetect))
loop
set fog = FirstOfGroup(DetectGroup)
exitwhen (fog == null)
call GroupRemoveUnit(DetectGroup, fog)
call GroupAddUnit(RecycleGroup, fog)
call TriggerRegisterUnitEvent(DetectTrigger, fog, EVENT_UNIT_ACQUIRED_TARGET)
endloop
// Recycle group
set TempGroup = DetectGroup
set DetectGroup = RecycleGroup
set RecycleGroup = TempGroup
endmethod
static method onDeindex takes nothing returns boolean
local unit u = GetIndexedUnit()
set TargetUnit[GetIndexedUnitId()] = null
call GroupRemoveUnit(DetectGroup, u)
if (IsUnitInGroup(u, BonusGroup)) then
call GroupRemoveUnit(BonusGroup, u)
// If there is no unit with the bonus buff anymore
if (FirstOfGroup(BonusGroup) == null) then
call PauseTimer(CheckTimer)
endif
endif
set u = null
return false
endmethod
static method initializeSpell takes nothing returns nothing
local unit fog
local group g = CreateGroup()
local trigger t1 = CreateTrigger()
local trigger t2 = CreateTrigger()
local integer i = 0
local player p
// Prepare triggers
loop
exitwhen (i == bj_MAX_PLAYER_SLOTS)
set p = Player(i)
call TriggerRegisterPlayerUnitEvent(t1, p, EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
call TriggerRegisterPlayerUnitEvent(t2, p, EVENT_PLAYER_UNIT_ISSUED_ORDER, null)
call TriggerRegisterPlayerUnitEvent(t2, p, EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER, null)
call TriggerRegisterPlayerUnitEvent(t2, p, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER, null)
set i = i + 1
endloop
call EnableTrigger(DetectTrigger)
call DestroyTrigger(LearnTrigger)
call TriggerAddCondition(t1, Condition(function thistype.onCast))
call TriggerAddCondition(t2, Condition(function thistype.onOrder))
call TriggerAddCondition(DetectTrigger, Condition(function thistype.onDetect))
call RegisterUnitIndexEvent(Condition(function thistype.onDeindex), EVENT_UNIT_DEINDEX)
// Periodically reset detection trigger to prevent event leaks
call TimerStart(ResetTimer, TRIGGER_RESET_INTERVAL, true, function thistype.resetTrigger)
// Create dummy caster to apply buffs
set DummyCaster = CreateUnit(PASSIVE, DUMMY_CASTER_ID, 0, 0, 0)
call UnitAddAbility(DummyCaster, BONUS_SPELL_ID)
call UnitAddAbility(DummyCaster, MARK_SPELL_ID)
call GroupEnumUnitsInRect(g, GetWorldBounds(), null)
loop
set fog = FirstOfGroup(g)
exitwhen (fog == null)
call GroupRemoveUnit(g, fog)
if (IsUnitIndexed(fog)) then
call TriggerRegisterUnitEvent(DetectTrigger, fog, EVENT_UNIT_ACQUIRED_TARGET)
call GroupAddUnit(DetectGroup, fog)
endif
endloop
call DestroyGroup(g)
set LearnTrigger = null
set g = null
endmethod
static method onLearn takes nothing returns boolean
if (GetLearnedSkill() == MAIN_SPELL_ID) then
// The first time the spell appears in game, initialize the spell trigger
call initializeSpell()
endif
return false
endmethod
static method onIndex takes nothing returns boolean
local unit u
if (IsTriggerEnabled(DetectTrigger)) then
set u = GetIndexedUnit()
// Add indexed unit to the detection trigger
call TriggerRegisterUnitEvent(DetectTrigger, u, EVENT_UNIT_ACQUIRED_TARGET)
call GroupAddUnit(DetectGroup, u)
set u = null
elseif (GetUnitAbilityLevel(GetIndexedUnit(), MAIN_SPELL_ID) > 0) then
// The first time the spell appears in game, initialize the spell trigger
call initializeSpell()
endif
return false
endmethod
static method onInit takes nothing returns nothing
call DisableTrigger(DetectTrigger)
call TriggerRegisterAnyUnitEventBJ(LearnTrigger, EVENT_PLAYER_HERO_SKILL)
call TriggerAddCondition(LearnTrigger, Condition(function thistype.onLearn))
call RegisterUnitIndexEvent(Condition(function thistype.onIndex), EVENT_UNIT_INDEX)
endmethod
endstruct
endscope
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope Riposte
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Riposte v1.0
// ¯¯¯¯¯¯¯¯¯¯¯¯
//
// I. Description
// Gives a chance to counter an incoming melee attack.
// Dealing damage and causing 1 second stun while
// ignoring the incoming damage.
//
// Level 1 - 20% chance. Deals 80 damage.
// Level 2 - 30% chance. Deals 90 damage.
// Level 3 - 40% chance. Deals 120 damage.
//
// II. Requirements
// • RapidSound by Quilnez | hiveworkshop.com/threads/snippet-rapidsound.258991/
// • UnitIndexer by TriggerHappy | hiveworkshop.com/threads/system-unitdex-unit-indexer.248209/
// • TimerUtils by Vexorian | wc3c.net/showthread.php?t=101322
//
// III. How to import
// • Import dummy.mdx from import manager to your map. Other files are optional
// • Import the following object data to your map:
// (Unit)
// • Dummy Caster
// (Ability)
// • Riposte (Pause)
// • Riposte (Main)
// (Buff)
// • Riposte (Buff)
// • Make sure "Persecute (Pause)" ability has "Persecute (Buff)" as buff
// • Import and configure required libraries properly
// • Import Persecute trigger and configure it properly
//
// IV. Credits
// • TriggerHappy : UnitIndexer library
// • Vexorian : TimerUtils library, dummy.mdx
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Configurations
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
// A. Static configurations
globals
// 1. Riposte main ability raw code
private constant integer MAIN_SPELL_ID = 'A009'
//
// 2. Riposte (Pause) ability raw code
private constant integer PAUSE_SPELL_ID = 'A00A'
//
// 3. Riposte (Buff) buff raw code
private constant integer PAUSE_BUFF_ID = 'B002'
//
// 4. Dummy Caster unit raw code
private constant integer DUMMY_CASTER_ID = 'h000'
//
// 5. Riposte (Pause) ability order id
private constant integer PAUSE_ORDER_ID = 852127 // "stomp"
//
// 6. Played animation when the spell is triggering
private constant string DEFLECT_ANIMATION = "defend"
//
// 7. Played animation when countering the attack
private constant string COUNTER_ANIMATION = "slam"
//
// 8. Special effect when target's attack is deflected
private constant string STUN_SFX_MODEL = "Abilities\\Spells\\Human\\Thunderclap\\ThunderclapTarget.mdl"
private constant string STUN_SFX_MODEL_PT = "overhead"
//
// 9. Dealt damage configurations
private constant attacktype ATTACK_TYPE = ATTACK_TYPE_HERO
private constant damagetype DAMAGE_TYPE = DAMAGE_TYPE_NORMAL
private constant weapontype WEAPON_TYPE = WEAPON_TYPE_METAL_MEDIUM_SLICE
//
// Interval at which the detection trigger will reset
// (Just don't modify if you don't know what it means)
private constant real TRIGGER_RESET_INTERVAL = 30.0
//
// Better not to modify this
private constant real INTERVAL = 0.1
endglobals
//
// B. Dynamic configurations
//
// 10. Chance of the counter move to be performed
private constant function TriggerChance takes integer level returns real
return 10.0 + 10.0*level
endfunction
//
// 11. Dealt damage when attack is countered
private constant function DealtDamage takes integer level returns real
return 30.0 + 30.0*level
endfunction
//
// 12. Stun duration after attack is countered
private constant function StunDuration takes integer level returns real
return 1.0
endfunction
//
// 13. Delay before the next counter move can be performed again
private constant function CooldownTime takes integer level returns real
return 2.0
endfunction
//
// 14. Delay before the next counter move can be performed again
private constant function CounterDelay takes integer level returns real
return 0.4
endfunction
//
// 15. Delay before the damage is dealt after deflecting move is performed
private constant function DamageDelay takes integer sourceType returns real
return 0.25
endfunction
//
// 16. Classification of unit that its attacks can be countered
// • target = attacker
// • caster = owner of attacked unit
// Current targets: enemy, melee, non-structure, ground, amphibious
private constant function SpellTargets takes unit target, player caster returns boolean
return IsUnitEnemy(target, caster) and IsUnitType(target, UNIT_TYPE_MELEE_ATTACKER) and not IsUnitType(target, UNIT_TYPE_STRUCTURE) and (IsUnitType(target, UNIT_TYPE_GROUND) or not IsUnitType(target, UNIT_TYPE_FLYING))
endfunction
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
native UnitAlive takes unit id returns boolean
globals
private constant integer ATTACK_ORDER_ID = 851983 // "attack"
private constant integer SMART_ORDER_ID = 851971 // "smart"
private unit DummyCaster
endglobals
private struct Stun
effect sfx
unit target
real duration
static thistype array Index
static method onPeriodic takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
// If need the buff to be removed
if (.duration <= INTERVAL or not UnitAlive(.target) or GetUnitAbilityLevel(.target, PAUSE_BUFF_ID) == 0) then
set Index[GetUnitUserData(.target)] = 0
call UnitRemoveAbility(.target, PAUSE_BUFF_ID)
if (.sfx != null) then
call DestroyEffect(.sfx)
set .sfx = null
endif
call ReleaseTimer(t)
call deallocate()
set .target = null
else
set .duration = .duration - INTERVAL
endif
set t = null
endmethod
static method apply takes unit t, real d, boolean b returns nothing
local thistype this
local integer data = GetUnitUserData(t)
// If already registered
if (Index[data] != 0) then
set this = Index[data]
set .duration = d
else
set this = allocate()
set Index[data] = this
set .target = t
set .duration = d
if (b) then
set .sfx = AddSpecialEffectTarget(STUN_SFX_MODEL, t, STUN_SFX_MODEL_PT)
endif
// If don't have the buff yet
if (GetUnitAbilityLevel(t, PAUSE_BUFF_ID) == 0) then
call SetUnitX(DummyCaster, GetUnitX(t))
call SetUnitY(DummyCaster, GetUnitY(t))
call IssueImmediateOrderById(DummyCaster, PAUSE_ORDER_ID)
endif
call TimerStart(NewTimerEx(this), INTERVAL, true, function thistype.onPeriodic)
endif
endmethod
endstruct
private struct Riposte extends array
static group TempGroup
static group DetectGroup = CreateGroup() // Group containing units with Riposte ability
static group RecycleGroup = CreateGroup() // For recycling purpose
static timer ResetTimer = CreateTimer()
static trigger DetectTrigger = CreateTrigger()
static trigger OrderTrigger
static boolean array IsTriggering
static unit array CurrentTarget
static unit array DamageTarget
static real array DamageAmount
static real array DamageDelay
static real array StunDuration
static timer array CooldownTimer
static timer array TriggerTimer
static method delayedDamage takes nothing returns nothing
local timer t = GetExpiredTimer()
local integer i = GetTimerData(t)
local unit u = GetUnitById(i)
if (UnitAlive(u) and UnitAlive(DamageTarget[i])) then
call UnitDamageTarget(u, DamageTarget[i], DamageAmount[i], true, false, ATTACK_TYPE, DAMAGE_TYPE, WEAPON_TYPE)
endif
// Re-order caster to attack target so it won't change target afterwards
call DisableTrigger(OrderTrigger)
call IssueTargetOrderById(u, ATTACK_ORDER_ID, DamageTarget[i])
call EnableTrigger(OrderTrigger)
set IsTriggering[i] = false
set DamageTarget[i] = null
set t = null
set u = null
endmethod
static method counter takes nothing returns nothing
local timer t = GetExpiredTimer()
local integer i = GetTimerData(t)
local unit u = GetUnitById(i)
if (UnitAlive(u)) then
if (UnitAlive(DamageTarget[i])) then
// Re-apply stun on target but now with sfx
call Stun.apply(DamageTarget[i], StunDuration[i], true)
call SetUnitAnimation(DamageTarget[i], "stand")
call TimerStart(t, DamageDelay[i], false, function thistype.delayedDamage)
else
set DamageTarget[i] = null
set IsTriggering[i] = false
endif
call SetUnitAnimation(u, COUNTER_ANIMATION)
call QueueUnitAnimation(u, "ready")
else
set DamageTarget[i] = null
set IsTriggering[i] = false
endif
set t = null
set u = null
endmethod
static method onAttack takes nothing returns boolean
local unit a = GetAttacker()
local unit t = GetTriggerUnit()
local integer tdata = GetUnitUserData(t)
local integer level
local integer order
local real delay
if (not IsTriggering[tdata] and CurrentTarget[tdata] == a) then
if (IsUnitInGroup(t, DetectGroup) and SpellTargets(a, GetOwningPlayer(t))) then
set order = GetUnitCurrentOrder(t)
// If caster is on attack mode
if (order == ATTACK_ORDER_ID or (order == 0 and UnitAlive(CurrentTarget[tdata]))) then
if (TimerGetRemaining(CooldownTimer[tdata]) == 0) then
set level = GetUnitAbilityLevel(t, MAIN_SPELL_ID)
if (GetRandomReal(0, 100) <= TriggerChance(level)) then
set IsTriggering[tdata] = true
set DamageTarget[tdata] = a
set DamageDelay[tdata] = DamageDelay(GetUnitTypeId(t))
set DamageAmount[tdata] = DealtDamage(level)
set StunDuration[tdata] = StunDuration(level)
set delay = CounterDelay(level)
// Pause both units so their normal attacks won't interrupt
call Stun.apply(a, delay, false)
call Stun.apply(t, delay+DamageDelay[tdata], false)
call SetUnitAnimation(a, "attack")
call SetUnitAnimation(t, DEFLECT_ANIMATION)
call TimerStart(TriggerTimer[tdata], delay, false, function thistype.counter)
call TimerStart(CooldownTimer[tdata], CooldownTime(level), false, null)
endif
endif
endif
endif
endif
set a = null
set t = null
return false
endmethod
static method onDetect takes nothing returns boolean
local unit u = GetTriggerUnit()
if (IsUnitInGroup(u, DetectGroup)) then
set CurrentTarget[GetUnitUserData(u)] = GetEventTargetUnit()
endif
set u = null
return false
endmethod
static method onOrder takes nothing returns boolean
local unit u = GetTriggerUnit()
local unit t
if (IsUnitInGroup(u, DetectGroup)) then
set t = GetOrderTargetUnit()
// Convert "smart" order to "attack" order
if (t != null and GetIssuedOrderId() == SMART_ORDER_ID and IsUnitEnemy(t, GetOwningPlayer(u))) then
set CurrentTarget[GetUnitUserData(u)] = t
call DisableTrigger(OrderTrigger)
call IssueTargetOrderById(u, ATTACK_ORDER_ID, t)
call EnableTrigger(OrderTrigger)
endif
set t = null
endif
set u = null
return false
endmethod
static method onDeindex takes nothing returns boolean
local integer data
local unit u = GetIndexedUnit()
if (IsUnitInGroup(u, DetectGroup)) then
// Remove all data related to the unit
set data = GetIndexedUnitId()
call GroupRemoveUnit(DetectGroup, u)
call ReleaseTimer(CooldownTimer[data])
call ReleaseTimer(TriggerTimer[data])
set CurrentTarget[data] = null
set CooldownTimer[data] = null
set TriggerTimer[data] = null
endif
set u = null
return false
endmethod
static method resetTrigger takes nothing returns nothing
local unit fog
// Reset detection trigger
call DestroyTrigger(DetectTrigger)
set DetectTrigger = CreateTrigger()
call TriggerAddCondition(DetectTrigger, Condition(function thistype.onDetect))
loop
set fog = FirstOfGroup(DetectGroup)
exitwhen (fog == null)
call GroupRemoveUnit(DetectGroup, fog)
call GroupAddUnit(RecycleGroup, fog)
call TriggerRegisterUnitEvent(DetectTrigger, fog, EVENT_UNIT_ACQUIRED_TARGET)
endloop
// Recycle group
set TempGroup = DetectGroup
set DetectGroup = RecycleGroup
set RecycleGroup = TempGroup
endmethod
static method initializeSpell takes nothing returns nothing
local unit fog
local group g = CreateGroup()
local trigger t = CreateTrigger()
local integer i = 0
local integer data
local player p
// Prepare triggers
set OrderTrigger = CreateTrigger()
loop
exitwhen (i == bj_MAX_PLAYER_SLOTS)
set p = Player(i)
call TriggerRegisterPlayerUnitEvent(t, p, EVENT_PLAYER_UNIT_ATTACKED, null)
call TriggerRegisterPlayerUnitEvent(OrderTrigger, p, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER, null)
set i = i + 1
endloop
call EnableTrigger(DetectTrigger)
call TriggerAddCondition(t, Condition(function thistype.onAttack))
call TriggerAddCondition(OrderTrigger, Condition(function thistype.onOrder))
call TriggerAddCondition(DetectTrigger, Condition(function thistype.onDetect))
call RegisterUnitIndexEvent(Condition(function thistype.onDeindex), EVENT_UNIT_DEINDEX)
// Periodically reset detection trigger to prevent event leaks
call TimerStart(ResetTimer, TRIGGER_RESET_INTERVAL, true, function thistype.resetTrigger)
set DummyCaster = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), DUMMY_CASTER_ID, 0, 0, 0)
call UnitAddAbility(DummyCaster, PAUSE_SPELL_ID)
call GroupEnumUnitsInRect(g, GetWorldBounds(), null)
loop
set fog = FirstOfGroup(g)
exitwhen (fog == null)
call GroupRemoveUnit(g, fog)
if (IsUnitIndexed(fog) and GetUnitAbilityLevel(fog, MAIN_SPELL_ID) > 0) then
set data = GetUnitUserData(fog)
// Add to detection trigger
set CooldownTimer[data] = NewTimer()
set TriggerTimer[data] = NewTimerEx(data)
// Preserve timers that will be used frequently
call TriggerRegisterUnitEvent(DetectTrigger, fog, EVENT_UNIT_ACQUIRED_TARGET)
call GroupAddUnit(DetectGroup, fog)
endif
endloop
call DestroyGroup(g)
set g = null
endmethod
static method onLearn takes nothing returns boolean
local integer data
local unit u
if (GetLearnedSkill() == MAIN_SPELL_ID) then
if (IsTriggerEnabled(DetectTrigger)) then
set u = GetTriggerUnit()
if (GetUnitAbilityLevel(u, MAIN_SPELL_ID) == 1) then
set data = GetUnitUserData(u)
// Add to detection trigger
set CooldownTimer[data] = NewTimer()
set TriggerTimer[data] = NewTimerEx(data)
// Preserve timers that will be used frequently
call TriggerRegisterUnitEvent(DetectTrigger, u, EVENT_UNIT_ACQUIRED_TARGET)
call GroupAddUnit(DetectGroup, u)
endif
set u = null
else
// The first time the spell appears in game, initialize the spell trigger
call initializeSpell()
endif
endif
return false
endmethod
static method onIndex takes nothing returns boolean
local integer data
local unit u
if (IsTriggerEnabled(DetectTrigger)) then
set u = GetIndexedUnit()
if (GetUnitAbilityLevel(u, MAIN_SPELL_ID) > 0) then
set data = GetIndexedUnitId()
// Preserve timers that will be used frequently
set CooldownTimer[data] = NewTimer()
set TriggerTimer[data] = NewTimerEx(data)
// Add to detection trigger
call TriggerRegisterUnitEvent(DetectTrigger, u, EVENT_UNIT_ACQUIRED_TARGET)
call GroupAddUnit(DetectGroup, u)
endif
set u = null
else
// The first time the spell appears in game, initialize the spell trigger
call initializeSpell()
endif
return false
endmethod
static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call DisableTrigger(DetectTrigger)
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_HERO_SKILL)
call TriggerAddCondition(t, Condition(function thistype.onLearn))
call RegisterUnitIndexEvent(Condition(function thistype.onIndex), EVENT_UNIT_INDEX)
endmethod
endstruct
endscope
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope Turmoil
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Turmoil v1.0
// ¯¯¯¯¯¯¯¯¯¯¯¯
//
// I. Description
// Creates a cloud of chaos with 400 radius around the hero.
// All units within the cloud area will be forced to join the
// turmoil. Involved units are silenced, attack twice faster,
// have their armor reduced by 5, and become uncontrollable
// by player. They will keep fighting random enemy unit until
// the brawl is over. Swashbuckler will gain 50% evasion
// inside the cloud area. Lasts for 10 seconds.
//
// II. Requirements
// • TimerUtils by Vexorian | wc3c.net/showthread.php?t=101322
// • Missile by BPower | hiveworkshop.com/threads/missile.265370/
// • UnitIndexer by TriggerHappy | hiveworkshop.com/threads/system-unitdex-unit-indexer.248209/
// • IsTerrainWalkable by Anitarf | hiveworkshop.com/threads/snippet-isterrainwalkable.251774/
// • Stack by Nestharus | github.com/nestharus/JASS/tree/master/jass/Data%20Structures/Stack
//
// III. How to import
// • Import dummy.mdx from import manager to your map. Other files are optional
// • Import the following object data to your map:
// (Unit)
// • Dummy Caster
// • Turmoil (Cloud)
// (Ability)
// • Turmoil (Armor Penalty)
// • Turmoil (Evasion Hider)
// • Turmoil (Evasion)
// • Turmoil (Silence)
// • Turmoil (Main)
// (Buff)
// • Turmoil (Buff)
// • Make sure "Turmoil (Silence)" ability has "Turmoil (Buff)" as buff
// • Make sure "Turmoil (Evasion Hider)" ability has "Turmoil (Evasion)" ablity in its "Spell List"
// • Import and configure required libraries properly
// • Import Persecute trigger and configure it properly
//
// IV. Credits
// • HappyCockroach : spell concept, SFX_Turmoil.mdx
// • Nestharus : Stack library
// • BPower : Missile library
// • TriggerHappy : UnitIndexer library
// • Anitarf : IsTerrainWalkable library
// • Vexorian : TimerUtils library, dummy.mdx
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Configurations
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
globals
// 1. Turmoil main ability raw code
private constant integer MAIN_SPELL_ID = 'A004'
//
// 2. Turmoil (Silence) ability raw code
private constant integer SILENCE_SPELL_ID = 'A005'
//
// 3. Turmoil (Armor Penalty) ability raw code
private constant integer ARMOR_PENALTY_SPELL_ID = 'A006'
//
// 4. Turmoil (Evasion Hider) ability raw code
private constant integer EVASION_HIDER_SPELL_ID = 'A008'
//
// 5. Turmoil (Buff) ability raw code
private constant integer SPELL_BUFF_ID = 'B001'
//
// 6. Dummy Caster unit raw code
private constant integer DUMMY_CASTER_ID = 'h000'
//
// 7. Turmoil (Cloud) unit raw code
private constant integer CLOUD_DUMMY_ID = 'h004'
//
// 8. Turmoil (Silence) ability order id
private constant integer SILENCE_ORDER_ID = 852668 // "soulburn"
//
// 9. If true, evasion will be given to allies as well
private constant boolean EVASION_BONUS_TO_ALLY = true
//
// 10. If true, all units will attack totally random target, no regards to allies
private constant boolean ALLOW_ATTACK_ALLY = false
//
// 11. Cloud model size (radius)
private constant real CLOUD_RADIUS = 101.0
//
// 12. Cloud flying height
private constant real CLOUD_HEIGHT = 75.0
//
// 13. Unit distribution zone reduction, preventing them to leave cloud area while
// looking for random spot
private constant real DISTRIBUTION_RANGE_REDUCTION = 50.0
//
// 14. Maximum target looking retry count for every unit, preventing lag or even thread crash
private constant integer DISTRIBUTION_MAX_RETRY_COUNT = 5
//
// 15. Maximum attack unit attack range inside the cloud, no exception for ranged units
private constant real MAXIMUM_ATTACK_RANGE = 128.0
//
// 16. Minimum flying height of unit to be considered as flying unit
private constant real MINIMUM_FLYING_UNIT_HEIGHT = 5.0
//
// 17. After this period, every unit entering the cloud will be a free fighter
// Means it doesn't need to be paired
private constant real DUEL_PAIRING_PERIOD = 1.0
//
// 18. After this period, every unpaired affected unit will become a free fighter
private constant real UNPAIRED_PERIOD_TOLERANCE = 3.0
//
// 19. Accuracy on looking for walkable spot, preventing units from stucking
// The lower the more precise, but slower
private constant real TERRAIN_DETECTION_ACC = 15.0*bj_DEGTORAD
//
// 20. Minimum units jumping trajectory arc
private constant real MIN_JUMP_ARC = 45.0*bj_DEGTORAD
//
// 21. Maximum units jumping trajectory arc
private constant real MAX_JUMP_ARC = 70.0*bj_DEGTORAD
//
// 22. Units jumping speed
private constant real JUMP_SPEED = 15.0
//
// Better not to modify this
private constant real INTERVAL = 0.1
endglobals
//
// B. Dynamic configurations
//
// 23. Radius of the spell
private constant function CloudAoE takes integer level returns real
return 400.0
endfunction
//
// 24. Duration of the spell
private constant function CloudDuration takes integer level returns real
return 10.0
endfunction
//
// 25. Classification of unit that can be affected by the cloud
// • target = target
// • caster = owner of cloud caster
// Current targets: non-structure, non-mechanical, ground, amphibious
private constant function SpellTargets takes unit target, player caster returns boolean
return not IsUnitType(target, UNIT_TYPE_STRUCTURE) and not IsUnitType(target, UNIT_TYPE_MECHANICAL) and (IsUnitType(target, UNIT_TYPE_GROUND) or not IsUnitType(target, UNIT_TYPE_FLYING))
endfunction
private function AllowJump takes unit whichUnit returns boolean
// If not snared and not stunned
return GetUnitAbilityLevel(whichUnit, 'B000') == 0 and GetUnitAbilityLevel(whichUnit, 'BPSE') == 0
endfunction
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
native UnitAlive takes unit id returns boolean
globals
private constant integer ATTACK_ORDER_ID = 851983 // "attack"
private constant integer MOVE_ORDER_ID = 851986 // "move"
private constant integer STOP_ORDER_ID = 851972 // "stop"
endglobals
private keyword Turmoil
private struct TurmoilAI
unit unit
unit target
real duration
player owner
integer dex
boolean free // Free unit has freedom to attack whoever it wants
boolean passive // Passive unit waits for its challenger to come
boolean affected
boolean jumping
boolean evasion
boolean isPeon
Turmoil source
static unit DummyCaster
static trigger OrderTrigger = CreateTrigger()
static integer InstanceCount = 0
static thistype array Instance // For faster unit randomization
static thistype array UnitIndex
// Returns random target from affected units without any thread crash possibility
method getTarget takes nothing returns unit
local integer i = GetRandomInt(1, InstanceCount)
local integer j = i
loop
// Check if target is classified
exitwhen ((.free or (not Instance[i].passive and not UnitAlive(Instance[i].target)))/*
*/ and Instance[i].unit != .unit and UnitAlive(Instance[i].unit) and/*
*/ (ALLOW_ATTACK_ALLY or not IsUnitAlly(.unit, Instance[i].owner)) and/*
*/ Instance[i].source == .source)
set i = i + 1
if (i > InstanceCount) then
set i = 1
endif
if (i == j) then
return null
endif
endloop
return Instance[i].unit
endmethod
static method onRemove takes Missile missile returns boolean
local thistype this = missile.data
// Prevent unit from returning back to it's prev location
call DisableTrigger(OrderTrigger)
call IssueImmediateOrderById(.unit, STOP_ORDER_ID)
call EnableTrigger(OrderTrigger)
set .jumping = false
return true
endmethod
implement MissileStruct
static method onPeriodic takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
local Missile missile
local integer data
local real angle
local real ax
local real ay
local real tx
local real ty
local real x
local real y
local real a
// If unit is still alive and still affected
if (UnitAlive(.unit) and .affected) then
// If don't have any target
if (not UnitAlive(.target)) then
if (.free) then
set .target = getTarget()
elseif (.target != null) then
set .free = true
elseif (.passive) then
set .target = getTarget()
set data = GetUnitUserData(.target)
set UnitIndex[data].target = .unit
set UnitIndex[data].passive = false
endif
if (.duration > INTERVAL) then
set .duration = .duration - INTERVAL
elseif (not .free) then
set .free = true
endif
// If both unit and target is not jumping
elseif (not .jumping and not UnitIndex[GetUnitUserData(.target)].jumping) then
set ax = GetUnitX(.unit)
set ay = GetUnitY(.unit)
set tx = GetUnitX(.target)
set ty = GetUnitY(.target)
// Attack target must within MAXIMUM_ATTACK_RANGE no exception for ranged units
if ((tx-ax)*(tx-ax)+(ty-ay)*(ty-ay) <= MAXIMUM_ATTACK_RANGE*MAXIMUM_ATTACK_RANGE) then
call DisableTrigger(OrderTrigger)
call IssueTargetOrderById(.unit, ATTACK_ORDER_ID, .target)
call EnableTrigger(OrderTrigger)
elseif (.free or not .passive) then
set angle = Atan2(ty-ay, tx-ax)
call SetUnitFacing(.unit, angle*bj_RADTODEG)
// All units with flying height higher than the following value will be
// considered as flying unit
if (GetUnitFlyHeight(.unit) > MINIMUM_FLYING_UNIT_HEIGHT or not AllowJump(.unit)) then
call DisableTrigger(OrderTrigger)
call IssueTargetOrderById(.unit, MOVE_ORDER_ID, .target)
call EnableTrigger(OrderTrigger)
else
set a = 0
loop
set x = tx-MAXIMUM_ATTACK_RANGE*Cos(angle+a)
set y = ty-MAXIMUM_ATTACK_RANGE*Sin(angle+a)
if (IsTerrainWalkable(x, y)) then
set .jumping = true
set missile = Missile.createEx(.unit, x, y, 0)
set missile.arc = GetRandomReal(MIN_JUMP_ARC, MAX_JUMP_ARC)
set missile.speed = JUMP_SPEED
set missile.data = this
call launch(missile)
exitwhen true
endif
set a = a+TERRAIN_DETECTION_ACC
exitwhen (a >= bj_PI*2)
endloop
endif
endif
endif
set .affected = false
else
if (not .isPeon) then
call UnitRemoveType(.unit, UNIT_TYPE_PEON)
endif
// Reset affected unit
call SetUnitPathing(.unit, true)
call UnitRemoveAbility(.unit, SPELL_BUFF_ID)
call UnitRemoveAbility(.unit, ARMOR_PENALTY_SPELL_ID)
call DisableTrigger(OrderTrigger)
call IssueImmediateOrderById(.unit, STOP_ORDER_ID)
call EnableTrigger(OrderTrigger)
if (.evasion) then
call UnitRemoveAbility(.unit, EVASION_HIDER_SPELL_ID)
endif
// Dispose this instance
set UnitIndex[GetUnitUserData(.unit)] = 0
set Instance[.dex] = Instance[InstanceCount]
set InstanceCount = InstanceCount - 1
call ReleaseTimer(t)
call deallocate()
set .target = null
set .unit = null
endif
set t = null
endmethod
// Method to handle orders given by player upon affected units
static method onOrder takes nothing returns boolean
local thistype this = UnitIndex[GetUnitUserData(GetTriggerUnit())]
if (this != 0) then
call DisableTrigger(OrderTrigger)
if (UnitAlive(.target)) then
if (.jumping) then
call IssueImmediateOrderById(.unit, STOP_ORDER_ID)
elseif (GetUnitFlyHeight(.unit) > MINIMUM_FLYING_UNIT_HEIGHT) then
call IssueTargetOrderById(.unit, MOVE_ORDER_ID, .target)
else
call IssueTargetOrderById(.unit, ATTACK_ORDER_ID, .target)
endif
else
// I use this instead because "stop" order does not work here damnit, blizz
call IssueTargetOrderById(.unit, MOVE_ORDER_ID, .unit)
endif
call EnableTrigger(OrderTrigger)
endif
return false
endmethod
// Add a unit into the chaos!
static method add takes unit whichUnit, Turmoil source returns nothing
local thistype this
local Missile missile
local integer data = GetUnitUserData(whichUnit)
local integer c
local real d
local real a
local real x
local real y
// If not affected yet
if (UnitIndex[data] == 0) then
set this = allocate()
set InstanceCount = InstanceCount + 1
set Instance[InstanceCount] = this
set UnitIndex[data] = this
set .dex = InstanceCount
set .unit = whichUnit
set .source = source
set .owner = GetOwningPlayer(whichUnit)
set .duration = UNPAIRED_PERIOD_TOLERANCE
set .free = (source.pairingDur <= INTERVAL)
set .passive = (InstanceCount-(InstanceCount/2)*2 == 0)
set .affected = true
static if EVASION_BONUS_TO_ALLY then
set .evasion = IsUnitAlly(whichUnit, source.owner)
else
set .evasion = (whichUnit == source.evasion)
endif
// Prepare the affected unit for the chaos!
call SetUnitPathing(whichUnit, false)
if (.evasion) then
call UnitAddAbility(whichUnit, EVASION_HIDER_SPELL_ID)
endif
call UnitAddAbility(whichUnit, ARMOR_PENALTY_SPELL_ID)
call IssueImmediateOrderById(whichUnit, STOP_ORDER_ID)
call IssueTargetOrderById(DummyCaster, SILENCE_ORDER_ID, whichUnit)
// If a passive or free fighter, distribute to random spot for the duel
if (.passive and not .free) then
set c = 0
loop
set c = c + 1
set a = GetRandomReal(-bj_PI, bj_PI)
set d = GetRandomReal(0, source.aoe-DISTRIBUTION_RANGE_REDUCTION)
set x = source.targetX-d*Cos(a)
set y = source.targetY-d*Sin(a)
if (IsTerrainWalkable(x, y)) then
if (GetUnitFlyHeight(.unit) > MINIMUM_FLYING_UNIT_HEIGHT or not AllowJump(.unit)) then
call DisableTrigger(OrderTrigger)
call IssuePointOrderById(.unit, MOVE_ORDER_ID, x, y)
call EnableTrigger(OrderTrigger)
else
set .jumping = true
set missile = Missile.createEx(.unit, x, y, 0)
set missile.arc = GetRandomReal(MIN_JUMP_ARC, MAX_JUMP_ARC)
set missile.speed = JUMP_SPEED
set missile.data = this
call launch(missile)
endif
exitwhen true
endif
exitwhen (c == DISTRIBUTION_MAX_RETRY_COUNT)
endloop
else
set .jumping = false
endif
// Disable auto-attacking
set .isPeon = IsUnitType(whichUnit, UNIT_TYPE_PEON)
if (not .isPeon) then
call UnitAddType(whichUnit, UNIT_TYPE_PEON)
endif
static if not (LIBRARY_AutoFly) then
if (UnitAddAbility(whichUnit, 'Amrf') and UnitRemoveAbility(whichUnit, 'Amrf')) then
endif
endif
call TimerStart(NewTimerEx(this), INTERVAL, true, function thistype.onPeriodic)
else
set UnitIndex[data].affected = true
endif
endmethod
static method onInit takes nothing returns nothing
local integer i = 0
local player p
loop
exitwhen (i == bj_MAX_PLAYER_SLOTS)
set p = Player(i)
call TriggerRegisterPlayerUnitEvent(OrderTrigger, p, EVENT_PLAYER_UNIT_ISSUED_ORDER, null)
call TriggerRegisterPlayerUnitEvent(OrderTrigger, p, EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER, null)
call TriggerRegisterPlayerUnitEvent(OrderTrigger, p, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER, null)
call SetPlayerAbilityAvailable(p, EVASION_HIDER_SPELL_ID, false)
set i = i + 1
endloop
call TriggerAddCondition(OrderTrigger, Condition(function thistype.onOrder))
// Create dummy caster to apply buffs
set DummyCaster = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), DUMMY_CASTER_ID, 0, 0, 0)
call UnitAddAbility(DummyCaster, SILENCE_SPELL_ID)
endmethod
endstruct
private struct Clouds extends array
unit dummy
implement Stack
endstruct
private struct TurmoilCloud
Clouds c
static real array WorldBounds
static constant real TAU = bj_PI*2
method destroy takes nothing returns nothing
local Clouds node = .c.first
loop
exitwhen node == 0
call KillUnit(node.dummy)
set node.dummy = null
set node = node.next
endloop
call deallocate()
endmethod
static method create takes Turmoil source returns thistype
local Clouds node
local thistype this = allocate()
local real d = CLOUD_RADIUS
local real a
local real r
local real x
local real y
set .c = Clouds.create()
// Create the middle cloud
set node = .c.push()
set node.dummy = CreateUnit(source.owner, CLOUD_DUMMY_ID, source.targetX, source.targetY, GetRandomInt(0, 3)*90)
static if not LIBRARY_AutoFly then
if (UnitAddAbility(node.dummy, 'Amrf') and UnitRemoveAbility(node.dummy, 'Amrf')) then
endif
endif
call SetUnitFlyHeight(node.dummy, CLOUD_HEIGHT, 0)
call SetUnitAnimation(node.dummy, "birth")
call QueueUnitAnimation(node.dummy, "stand")
loop
exitwhen d > source.aoe
set a = 0
// Calculate radial spacing between clouds
set r = Asin(CLOUD_RADIUS/d)
loop
exitwhen a >= TAU
set x = source.targetX+d*Cos(a)
set y = source.targetY+d*Sin(a)
if (x < WorldBounds[0] and x > WorldBounds[1] and y < WorldBounds[2] and y > WorldBounds[3]) then
set node = .c.push()
set node.dummy = CreateUnit(source.owner, CLOUD_DUMMY_ID, x, y, GetRandomInt(0, 3)*90)
static if (not LIBRARY_AutoFly) then
if (UnitAddAbility(node.dummy, 'Amrf') and UnitRemoveAbility(node.dummy, 'Amrf')) then
endif
endif
call SetUnitFlyHeight(node.dummy, CLOUD_HEIGHT, 0)
call SetUnitAnimation(node.dummy, "birth")
call QueueUnitAnimation(node.dummy, "stand")
endif
set a = a + r
endloop
// Go to next layer
set d = d + CLOUD_RADIUS
endloop
return this
endmethod
static method onInit takes nothing returns nothing
local rect r = GetWorldBounds()
set WorldBounds[0] = GetRectMaxX(r)
set WorldBounds[1] = GetRectMinX(r)
set WorldBounds[2] = GetRectMaxY(r)
set WorldBounds[3] = GetRectMinY(r)
set r = null
endmethod
endstruct
private struct Turmoil
TurmoilCloud cloud
player owner
unit caster
real aoe
real targetX
real targetY
real duration
real pairingDur
static group TempGroup = CreateGroup()
static method onPeriodic takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
local integer data
local unit fog
if (.duration > INTERVAL) then
set .duration = .duration - INTERVAL
if (.pairingDur > INTERVAL) then
set .pairingDur = .pairingDur - INTERVAL
endif
// Iterate through all units withing the cloud area
call GroupEnumUnitsInRange(TempGroup, .targetX, .targetY, .aoe, null)
loop
set fog = FirstOfGroup(TempGroup)
exitwhen (fog == null)
call GroupRemoveUnit(TempGroup, fog)
if (UnitAlive(fog) and SpellTargets(fog, .owner)) then
call TurmoilAI.add(fog, this)
endif
endloop
else
// Dispose this instance
call UnitRemoveAbility(.caster, EVASION_HIDER_SPELL_ID)
call .cloud.destroy()
call ReleaseTimer(t)
call deallocate()
set .caster = null
endif
set t = null
endmethod
static method onCast takes nothing returns boolean
local thistype this
local integer level
if (GetSpellAbilityId() == MAIN_SPELL_ID) then
set this = allocate()
set .caster = GetTriggerUnit()
set .owner = GetTriggerPlayer()
set .targetX = GetUnitX(.caster)
set .targetY = GetUnitY(.caster)
set .pairingDur = DUEL_PAIRING_PERIOD
set level = GetUnitAbilityLevel(.caster, MAIN_SPELL_ID)
set .aoe = CloudAoE(level)
set .duration = CloudDuration(level)
set .cloud = TurmoilCloud.create(this)
call TimerStart(NewTimerEx(this), INTERVAL, true, function thistype.onPeriodic)
endif
return false
endmethod
static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition(t, Condition(function thistype.onCast))
endmethod
endstruct
endscope