Name | Type | is_array | initial_value |
g | group | No | |
Initialized | boolean | No | |
l | location | No |
library MissileEngine /* v1.0.0
****************************************************************************************************
* _____________________
* * MissileEngine *
* * By: BloodForBlood *
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* */requires/*
*
* */ Table /*
* - https://www.hiveworkshop.com/threads/snippet-new-table.188084/
* */ TimerUtils /*
* - http://www.wc3c.net/showthread.php?t=101322
*
* What is Missile Engine:
* It generates custom missiles and this has
* a modified "BoundSentinel" inside this
* library for safety.
*
* Requirements:x
* 1. vJASS compiler
* 2. Table version 4.0 or above(NewTable)
* 3. TimerUtils 2.0(Chromatic TimerUtils)
* 4. Vexorian's dummy.mdx or any dummy model with "origin" attachment
*
* Features:
* 1. SingleTarget or MultiTarget
* - SingleTarget means after hitting a unit, the missile will be destroyed
* MultiTarget means after hitting a unit, the missile will not be destroyed
* 2. Custom Missile Model
* - Custom missile model on create, like:
* "Abilities\\Weapons\\BallistaMissile\\BallistaMissile.mdl"
* 3. Configurable Missile
* - You can configure the missile's owner, AoE or collision, speed, max distance
* angle, starting X and Y point
* 4. Safe
* - Any missile that leaves the map's boundary will be destroyed
* to avoid game crash
*
* How to Implement:
* 1. Import or copy the dummy model("war3mapImported/dummy.mdx") to your map and
* use the "ObjectMerger" macro script above "globals" to create the dummy unit.
* You can also copy the unit(Base Model) manually from the Object Editor to
* your map.
* 2. Implement or copy "Table" and "TimerUtils" to your map.
* 3. Copy this library/trigger to your map.
*
* API:
* struct Missile
* static method getData takes nothing returns Missile
* - Returns the Missile(struct data) on registered
* codes(functions) by using onHit, onPeriodic
* or onDest
*
* method getUnit takes nothing returns unit
* - Returns the Missile's unit
*
* method getLastHittedUnit takes nothing returns unit
* - Returns the last hitted unit by the missile
*
* method getMissileOwner takes nothing returns unit
* - Returns the unit where the missile came from
*
* method getSpeed takes nothing returns real
* - Returns the missile's speed
*
* method getDistTraveled takes nothing returns real
* - Returns the distance traveled of the missile
*
* method setSpeed takes real r returns nothing
* - Sets the missile's speed
*
* method setAoE takes real r returns nothing
* - Sets the missile's collision or AoE
*
* method setAngle takes real r returns nothing
* - Sets the missile's angle
*
* method onHit takes code c returns nothing
* - Registers the onHit event to the function
*
* method onPeriodic takes code c returns nothing
* - Registers the onPeriodic event to the function
*
* method onDest takes code c returns nothing
* - Registers the onDestory event to the function
*
* static method create takes unit owner, string mdl, real AoE, real spd, real dist, real angle,
* real x, real y, real height, boolean single, boolean enemy,
* boolean alive, boolean bldg returns Missile
* - Creates a missile, see Demo for help
*
* method destroy takes nothing returns nothing
* - Destroys the missile properly
*
* Credits:
* Bribe - for NewTable
* Vexorian - for TimerUtils, BoundSentinel and dummy.mdx
*
***************************************************************************************************/
// external ObjectMerger w3u hpea uMis ufoo 0 uabi Aloc uico "ReplaceableTextures\WorldEditUI\DoodadPlaceholder.blp" umdl "war3mapImported\dummy.mdl" ushu " " unam "Base Model" udtm "1" umvt "fly" ucol 0 uhom 1 usid 0 usin 0 utyp " " umvr 0.10 uaen 0
globals
private constant integer BASE_MODEL_ID = 'uMis'
private constant real PERIODIC = 0.03125
private constant real EXTRA = -100 // The extra offset of BoundSentinel for missiles
private HashTable data
private real maxx
private real maxy
private real minx
private real miny
endglobals
struct Missile
private unit missile
private real angle
private real collision
private real destRange
private real distTraveled
private real speed
private boolean single
private timer time
private unit owner
private boolean enemyOnly
private effect sfx
private HashTable ht
private trigger array eventTrig[3]
private unit hitted
private boolean alive
private boolean bldg
static method getData takes nothing returns Missile
return data[GetHandleId(GetTriggeringTrigger())][StringHash("ME_STRUCTDATA")]
endmethod
method getUnit takes nothing returns unit
return .missile
endmethod
method getLastHittedUnit takes nothing returns unit
return .hitted
endmethod
method getMissileOwner takes nothing returns unit
return .owner
endmethod
method getSpeed takes nothing returns real
return .speed
endmethod
method getDistTraveled takes nothing returns real
return .distTraveled
endmethod
method setSpeed takes real r returns nothing
set .speed = r
endmethod
method setAoE takes real r returns nothing
set .collision = r
endmethod
method setAngle takes real r returns nothing
set .angle = r
call SetUnitFacing(.missile, r)
endmethod
method onHit takes code c returns nothing
call TriggerAddCondition(.eventTrig[0], Filter(c))
endmethod
method onPeriodic takes code c returns nothing
call TriggerAddCondition(.eventTrig[1], Filter(c))
endmethod
method onDest takes code c returns nothing
call TriggerAddCondition(.eventTrig[2], Filter(c))
endmethod
private static method filter takes unit u, unit FoG returns boolean
local Missile dt = data[0][GetHandleId(u)]
if (u == null or FoG == null) then
return false
endif
if (FoG == dt.owner) then
return false
endif
if (FoG == u) then
return false
endif
if (dt.ht[GetHandleId(FoG)].boolean[StringHash("ME_HITTED")]) then
return false
endif
if (dt.enemyOnly) then
if (IsUnitAlly(FoG, GetOwningPlayer(u))) then
return false
endif
endif
if (dt.alive) then
if (GetWidgetLife(FoG) <= 0.405) then
return false
endif
endif
if (not dt.bldg) then
if (IsUnitType(FoG, UNIT_TYPE_STRUCTURE)) then
return false
endif
endif
return true
endmethod
private static method periodic takes nothing returns nothing
local timer t = GetExpiredTimer()
local Missile dt = GetTimerData(t)
local real speed = dt.speed * PERIODIC
local real x = GetUnitX(dt.missile) + speed * Cos(dt.angle * bj_DEGTORAD)
local real y = GetUnitY(dt.missile) + speed * Sin(dt.angle * bj_DEGTORAD)
local group g = CreateGroup()
local unit u
call TriggerEvaluate(dt.eventTrig[1])
if (GetWidgetLife(dt.missile) > 0.405 or dt.missile != null) then
set dt.distTraveled = dt.distTraveled + speed
call SetUnitX(dt.missile, x)
call SetUnitY(dt.missile, y)
if (dt.distTraveled >= dt.destRange) then
call dt.destroy()
endif
call GroupEnumUnitsInRange(g, x, y, dt.collision, null)
loop
set u = FirstOfGroup(g)
exitwhen u == null
call GroupRemoveUnit(g, u)
if (thistype.filter(dt.missile, u)) then
set dt.ht[GetHandleId(u)].boolean[StringHash("ME_HITTED")] = true
set dt.hitted = u
call TriggerEvaluate(dt.eventTrig[0])
if (dt.single) then
call dt.destroy()
endif
endif
endloop
else
call dt.destroy()
endif
call DestroyGroup(g)
set t = null
set g = null
set u = null
endmethod
static method create takes unit owner, string mdl, real AoE, real spd, real dist, real angle,/*
*/ real x, real y, real height, boolean single, boolean enemy,/*
*/ boolean alive, boolean bldg returns Missile
local Missile this = .allocate()
set .missile = CreateUnit(GetOwningPlayer(owner), BASE_MODEL_ID, x, y, angle)
set .owner = owner
set .collision = AoE
set .speed = spd
set .destRange = dist
set .angle = angle
set .single = single
set .enemyOnly = enemy
set .alive = alive
set .bldg = bldg
set .time = NewTimerEx(this)
set data[0][GetHandleId(.missile)] = this
set .sfx = AddSpecialEffectTarget(mdl, .missile, "origin")
set .ht = HashTable.create()
set .ht[GetHandleId(.missile)].boolean[StringHash("ME_HITTED")] = true
set .ht[GetHandleId(.owner)].boolean[StringHash("ME_HITTED")] = true
set .eventTrig[0] = CreateTrigger()
set data[GetHandleId(.eventTrig[0])][StringHash("ME_STRUCTDATA")] = this
set .eventTrig[1] = CreateTrigger()
set data[GetHandleId(.eventTrig[1])][StringHash("ME_STRUCTDATA")] = this
set .eventTrig[2] = CreateTrigger()
set data[GetHandleId(.eventTrig[2])][StringHash("ME_STRUCTDATA")] = this
set data[GetHandleId(.missile)][StringHash("ME_STRUCTDATA")] = this
call SetUnitFlyHeight(.missile, height, 9999)
call TimerStart(.time, PERIODIC, true, function thistype.periodic)
return this
endmethod
method destroy takes nothing returns nothing
if (.eventTrig[2] != null) then
call TriggerEvaluate(.eventTrig[2])
endif
if (.time != null) then
call ReleaseTimer(.time)
endif
if (.sfx != null) then
call DestroyEffect(.sfx)
endif
if (.missile != null) then
call KillUnit(.missile)
endif
if (.ht != null) then
call .ht.destroy()
endif
if (.eventTrig[0] != null) then
set data[GetHandleId(.eventTrig[0])][StringHash("ME_STRUCTDATA")] = 0
call DestroyTrigger(.eventTrig[0])
endif
if (.eventTrig[1] != null) then
set data[GetHandleId(.eventTrig[1])][StringHash("ME_STRUCTDATA")] = 0
call DestroyTrigger(.eventTrig[1])
endif
if (.eventTrig[2] != null) then
set data[GetHandleId(.eventTrig[2])][StringHash("ME_STRUCTDATA")] = 0
call DestroyTrigger(.eventTrig[2])
endif
set .missile = null
set .collision = 0
set .speed = 0
set .destRange = 0
set .distTraveled = 0
set .angle = 0
set .single = false
set .enemyOnly = false
set .time = null
set .sfx = null
set .ht = 0
set .eventTrig[0] = null
set .eventTrig[1] = null
set .eventTrig[2] = null
set .hitted = null
call .deallocate()
endmethod
endstruct
// Special thanks to Vexorian
private function dis takes nothing returns nothing
local unit u=GetTriggerUnit()
local real x=GetUnitX(u)
local real y=GetUnitY(u)
local Missile dt
if(x>maxx) then
set x=maxx
elseif(x<minx) then
set x=minx
endif
if(y>maxy) then
set y=maxy
elseif(y<miny) then
set y=miny
endif
if (GetUnitTypeId(u) == BASE_MODEL_ID) then
set dt = data[GetHandleId(u)][StringHash("ME_STRUCTDATA")]
call dt.destroy()
else
call SetUnitX(u,x)
call SetUnitY(u,y)
endif
set u=null
endfunction
private module m
private static method onInit takes nothing returns nothing
local trigger t=CreateTrigger()
local region r=CreateRegion()
local rect rc=GetWorldBounds()
set data = HashTable.create()
set minx=GetRectMinX(rc) - -100
set miny=GetRectMinY(rc) - -100
set maxx=GetRectMaxX(rc) + -100
set maxy=GetRectMaxY(rc) + -100
set rc=Rect(minx,miny,maxx,maxy)
call RegionAddRect(r, rc)
call RemoveRect(rc)
call TriggerRegisterLeaveRegion(t,r, null)
call TriggerAddAction(t, function dis)
//this is not necessary but I'll do it anyway:
set t=null
set r=null
set rc=null
endmethod
endmodule
private struct s
implement m
endstruct
endlibrary
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 4.1.0.1.
One map, one hashtable. Welcome to NewTable 4.1.0.1
This newest iteration of Table introduces the new HashTable struct.
You can now instantiate HashTables which enables the use of large
parent and large child keys, just like a standard hashtable. Previously,
the user would have to instantiate a Table to do this on their own which -
while doable - is something the user should not have to do if I can add it
to this resource myself (especially if they are inexperienced).
This library was originally called NewTable so it didn't conflict with
the API of Table by Vexorian. However, the damage is done and it's too
late to change the library name now. To help with damage control, I
have provided an extension library called TableBC, which bridges all
the functionality of Vexorian's Table except for 2-D string arrays &
the ".flush(integer)" method. I use ".flush()" to flush a child hash-
table, because I wanted the API in NewTable to reflect the API of real
hashtables (I thought this would be more intuitive).
API
------------
struct Table
| static method create takes nothing returns Table
| create a new Table
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush all stored values inside of it
|
| method remove takes integer key returns nothing
| remove the value at index "key"
|
| method operator []= takes integer key, $TYPE$ value returns nothing
| assign "value" to index "key"
|
| method operator [] takes integer key returns $TYPE$
| load the value at index "key"
|
| method has takes integer key returns boolean
| whether or not the key was assigned
|
----------------
struct TableArray
| static method operator [] takes integer array_size returns TableArray
| create a new array of Tables of size "array_size"
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush and destroy it
|
| method operator size takes nothing returns integer
| returns the size of the TableArray
|
| method operator [] takes integer key returns Table
| returns a Table accessible exclusively to index "key"
*/
globals
private integer less = 0 //Index generation for TableArrays (below 0).
private integer more = 8190 //Index generation for Tables.
//Configure it if you use more than 8190 "key" variables in your map (this will never happen though).
private hashtable ht = InitHashtable()
private key sizeK
private key listK
endglobals
private struct dex extends array
static method operator size takes nothing returns Table
return sizeK
endmethod
static method operator list takes nothing returns Table
return listK
endmethod
endstruct
private struct handles extends array
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private struct agents extends array
method operator []= takes integer key, agent value returns nothing
call SaveAgentHandle(ht, this, key, value)
endmethod
endstruct
//! textmacro NEW_ARRAY_BASIC takes SUPER, FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSaved$SUPER$(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSaved$SUPER$(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//! textmacro NEW_ARRAY takes FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$Handle(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$Handle(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//Run these textmacros to include the entire hashtable API as wrappers.
//Don't be intimidated by the number of macros - Vexorian's map optimizer is
//supposed to kill functions which inline (all of these functions inline).
//! runtextmacro NEW_ARRAY_BASIC("Real", "Real", "real")
//! runtextmacro NEW_ARRAY_BASIC("Boolean", "Boolean", "boolean")
//! runtextmacro NEW_ARRAY_BASIC("String", "Str", "string")
//New textmacro to allow table.integer[] syntax for compatibility with textmacros that might desire it.
//! runtextmacro NEW_ARRAY_BASIC("Integer", "Integer", "integer")
//! runtextmacro NEW_ARRAY("Player", "player")
//! runtextmacro NEW_ARRAY("Widget", "widget")
//! runtextmacro NEW_ARRAY("Destructable", "destructable")
//! runtextmacro NEW_ARRAY("Item", "item")
//! runtextmacro NEW_ARRAY("Unit", "unit")
//! runtextmacro NEW_ARRAY("Ability", "ability")
//! runtextmacro NEW_ARRAY("Timer", "timer")
//! runtextmacro NEW_ARRAY("Trigger", "trigger")
//! runtextmacro NEW_ARRAY("TriggerCondition", "triggercondition")
//! runtextmacro NEW_ARRAY("TriggerAction", "triggeraction")
//! runtextmacro NEW_ARRAY("TriggerEvent", "event")
//! runtextmacro NEW_ARRAY("Force", "force")
//! runtextmacro NEW_ARRAY("Group", "group")
//! runtextmacro NEW_ARRAY("Location", "location")
//! runtextmacro NEW_ARRAY("Rect", "rect")
//! runtextmacro NEW_ARRAY("BooleanExpr", "boolexpr")
//! runtextmacro NEW_ARRAY("Sound", "sound")
//! runtextmacro NEW_ARRAY("Effect", "effect")
//! runtextmacro NEW_ARRAY("UnitPool", "unitpool")
//! runtextmacro NEW_ARRAY("ItemPool", "itempool")
//! runtextmacro NEW_ARRAY("Quest", "quest")
//! runtextmacro NEW_ARRAY("QuestItem", "questitem")
//! runtextmacro NEW_ARRAY("DefeatCondition", "defeatcondition")
//! runtextmacro NEW_ARRAY("TimerDialog", "timerdialog")
//! runtextmacro NEW_ARRAY("Leaderboard", "leaderboard")
//! runtextmacro NEW_ARRAY("Multiboard", "multiboard")
//! runtextmacro NEW_ARRAY("MultiboardItem", "multiboarditem")
//! runtextmacro NEW_ARRAY("Trackable", "trackable")
//! runtextmacro NEW_ARRAY("Dialog", "dialog")
//! runtextmacro NEW_ARRAY("Button", "button")
//! runtextmacro NEW_ARRAY("TextTag", "texttag")
//! runtextmacro NEW_ARRAY("Lightning", "lightning")
//! runtextmacro NEW_ARRAY("Image", "image")
//! runtextmacro NEW_ARRAY("Ubersplat", "ubersplat")
//! runtextmacro NEW_ARRAY("Region", "region")
//! runtextmacro NEW_ARRAY("FogState", "fogstate")
//! runtextmacro NEW_ARRAY("FogModifier", "fogmodifier")
//! runtextmacro NEW_ARRAY("Hashtable", "hashtable")
struct Table extends array
// Implement modules for intuitive syntax (tb.handle; tb.unit; etc.)
implement realm
implement integerm
implement booleanm
implement stringm
implement playerm
implement widgetm
implement destructablem
implement itemm
implement unitm
implement abilitym
implement timerm
implement triggerm
implement triggerconditionm
implement triggeractionm
implement eventm
implement forcem
implement groupm
implement locationm
implement rectm
implement boolexprm
implement soundm
implement effectm
implement unitpoolm
implement itempoolm
implement questm
implement questitemm
implement defeatconditionm
implement timerdialogm
implement leaderboardm
implement multiboardm
implement multiboarditemm
implement trackablem
implement dialogm
implement buttonm
implement texttagm
implement lightningm
implement imagem
implement ubersplatm
implement regionm
implement fogstatem
implement fogmodifierm
implement hashtablem
method operator handle takes nothing returns handles
return this
endmethod
method operator agent takes nothing returns agents
return this
endmethod
//set this = tb[GetSpellAbilityId()]
method operator [] takes integer key returns Table
return LoadInteger(ht, this, key) //return this.integer[key]
endmethod
//set tb[389034] = 8192
method operator []= takes integer key, Table tb returns nothing
call SaveInteger(ht, this, key, tb) //set this.integer[key] = tb
endmethod
//set b = tb.has(2493223)
method has takes integer key returns boolean
return HaveSavedInteger(ht, this, key) //return this.integer.has(key)
endmethod
//call tb.remove(294080)
method remove takes integer key returns nothing
call RemoveSavedInteger(ht, this, key) //call this.integer.remove(key)
endmethod
//Remove all data from a Table instance
method flush takes nothing returns nothing
call FlushChildHashtable(ht, this)
endmethod
//local Table tb = Table.create()
static method create takes nothing returns Table
local Table this = dex.list[0]
if this == 0 then
set this = more + 1
set more = this
else
set dex.list[0] = dex.list[this]
call dex.list.remove(this) //Clear hashed memory
endif
debug set dex.list[this] = -1
return this
endmethod
// Removes all data from a Table instance and recycles its index.
//
// call tb.destroy()
//
method destroy takes nothing returns nothing
debug if dex.list[this] != -1 then
debug call BJDebugMsg("Table Error: Tried to double-free instance: " + I2S(this))
debug return
debug endif
call this.flush()
set dex.list[this] = dex.list[0]
set dex.list[0] = this
endmethod
//! runtextmacro optional TABLE_BC_METHODS()
endstruct
//! runtextmacro optional TABLE_BC_STRUCTS()
struct TableArray extends array
//Returns a new TableArray to do your bidding. Simply use:
//
// local TableArray ta = TableArray[array_size]
//
static method operator [] takes integer array_size returns TableArray
local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
local TableArray this = tb[0] //The last-destroyed TableArray that had this array size
debug if array_size <= 0 then
debug call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
debug return 0
debug endif
if this == 0 then
set this = less - array_size
set less = this
else
set tb[0] = tb[this] //Set the last destroyed to the last-last destroyed
call tb.remove(this) //Clear hashed memory
endif
set dex.size[this] = array_size //This remembers the array size
return this
endmethod
//Returns the size of the TableArray
method operator size takes nothing returns integer
return dex.size[this]
endmethod
//This magic method enables two-dimensional[array][syntax] for Tables,
//similar to the two-dimensional utility provided by hashtables them-
//selves.
//
//ta[integer a].unit[integer b] = unit u
//ta[integer a][integer c] = integer d
//
//Inline-friendly when not running in debug mode
//
method operator [] takes integer key returns Table
static if DEBUG_MODE then
local integer i = this.size
if i == 0 then
call BJDebugMsg("IndexError: Tried to get key from invalid TableArray instance: " + I2S(this))
return 0
elseif key < 0 or key >= i then
call BJDebugMsg("IndexError: Tried to get key [" + I2S(key) + "] from outside TableArray bounds: " + I2S(i))
return 0
endif
endif
return this + key
endmethod
//Destroys a TableArray without flushing it; I assume you call .flush()
//if you want it flushed too. This is a public method so that you don't
//have to loop through all TableArray indices to flush them if you don't
//need to (ie. if you were flushing all child-keys as you used them).
//
method destroy takes nothing returns nothing
local Table tb = dex.size[this.size]
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to destroy an invalid TableArray: " + I2S(this))
debug return
debug endif
if tb == 0 then
//Create a Table to index recycled instances with their array size
set tb = Table.create()
set dex.size[this.size] = tb
endif
call dex.size.remove(this) //Clear the array size from hash memory
set tb[this] = tb[0]
set tb[0] = this
endmethod
private static Table tempTable
private static integer tempEnd
//Avoids hitting the op limit
private static method clean takes nothing returns nothing
local Table tb = .tempTable
local integer end = tb + 0x1000
if end < .tempEnd then
set .tempTable = end
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
else
set end = .tempEnd
endif
loop
call tb.flush()
set tb = tb + 1
exitwhen tb == end
endloop
endmethod
//Flushes the TableArray and also destroys it. Doesn't get any more
//similar to the FlushParentHashtable native than this.
//
method flush takes nothing returns nothing
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to flush an invalid TableArray instance: " + I2S(this))
debug return
debug endif
set .tempTable = this
set .tempEnd = this + this.size
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
call this.destroy()
endmethod
endstruct
//NEW: Added in Table 4.0. A fairly simple struct but allows you to do more
//than that which was previously possible.
struct HashTable extends array
//Enables myHash[parentKey][childKey] syntax.
//Basically, it creates a Table in the place of the parent key if
//it didn't already get created earlier.
method operator [] takes integer index returns Table
local Table t = Table(this)[index]
if t == 0 then
set t = Table.create()
set Table(this)[index] = t //whoops! Forgot that line. I'm out of practice!
endif
return t
endmethod
//You need to call this on each parent key that you used if you
//intend to destroy the HashTable or simply no longer need that key.
method remove takes integer index returns nothing
local Table t = Table(this)[index]
if t != 0 then
call t.destroy()
call Table(this).remove(index)
endif
endmethod
//Added in version 4.1
method has takes integer index returns boolean
return Table(this).has(index)
endmethod
//HashTables are just fancy Table indices.
method destroy takes nothing returns nothing
call Table(this).destroy()
endmethod
//Like I said above...
static method create takes nothing returns thistype
return Table.create()
endmethod
endstruct
endlibrary
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 boolean DEBUG_MODE = 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
if(USE_HASH_TABLE) then
// new blue
call SaveInteger(ht,0,GetHandleId(t), value)
elseif (USE_FLEXIBLE_OFFSET) then
// orange
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
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
if(USE_HASH_TABLE) then
// new blue
return LoadInteger(ht,0,GetHandleId(t) )
elseif (USE_FLEXIBLE_OFFSET) then
// orange
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
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()
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")
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
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])
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
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
scope MultiTargetArrow initializer onInit
globals
private real array damage
endglobals
private function condition takes nothing returns boolean
if (GetSpellAbilityId() != 'A001') then
return false
endif
return true
endfunction
private function unitHit takes nothing returns nothing
local Missile arrow = Missile.getData()
local unit u = arrow.getLastHittedUnit()
call UnitDamageTarget(arrow.getMissileOwner(), u, damage[arrow], false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS)
call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\HydraliskImpact\\HydraliskImpact.mdl", u, "chest"))
set damage[arrow] = damage[arrow] - 10
set u = null
endfunction
private function death takes nothing returns nothing
local Missile arrow = Missile.getData()
set damage[arrow] = 0
endfunction
private function onCast takes nothing returns nothing
local unit u = GetTriggerUnit()
local Missile arrow
local location loc = GetUnitLoc(u)
local location sLoc = GetSpellTargetLoc()
local real AoE = 100
local real speed = 2600
local real maxRange = 2600
local real angle = AngleBetweenPoints(loc, sLoc)
local real x = GetUnitX(u)
local real y = GetUnitY(u)
local real height = 50
local boolean singleTarget = false
local boolean enemyOnly = true
local boolean aliveOnly = true
local boolean includeBuilding = false
set arrow = Missile.create(u, MODEL, AoE, speed, maxRange, angle, x, y, height, singleTarget, enemyOnly, aliveOnly, includeBuilding)
set damage[arrow] = 100
call arrow.onHit(function unitHit)
call arrow.onDest(function death)
call RemoveLocation(loc)
call RemoveLocation(sLoc)
set loc = null
set sLoc = null
endfunction
private function onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterPlayerUnitEvent(t, Player(0), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
call TriggerAddCondition(t, Condition(function condition))
call TriggerAddAction(t, function onCast)
set t = null
endfunction
endscope
scope SingleTargetArrow initializer onInit
globals
private constant real damage = 100
endglobals
private function condition takes nothing returns boolean
if (GetSpellAbilityId() != 'A000') then
return false
endif
return true
endfunction
private function unitHit takes nothing returns nothing
local Missile arrow = Missile.getData()
local unit u = arrow.getLastHittedUnit()
call UnitDamageTarget(arrow.getMissileOwner(), u, damage, false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS)
if (IsUnitType(u, UNIT_TYPE_STRUCTURE)) then
call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\AncientProtectorMissile\\AncientProtectorMissile.mdl", u, "origin"))
else
call DestroyEffect(AddSpecialEffectTarget("Abilities\\Weapons\\HydraliskImpact\\HydraliskImpact.mdl", u, "chest"))
endif
set u = null
endfunction
private function onCast takes nothing returns nothing
local unit u = GetTriggerUnit()
local Missile arrow
local location loc = GetUnitLoc(u)
local location sLoc = GetSpellTargetLoc()
local real AoE = 100
local real speed = 650
local real maxRange = 3000
local real angle = AngleBetweenPoints(loc, sLoc)
local real x = GetUnitX(u)
local real y = GetUnitY(u)
local real height = 50
local boolean singleTarget = true
local boolean enemyOnly = true
local boolean aliveOnly = true
local boolean includeBuilding = true
set arrow = Missile.create(u, MODEL, AoE, speed, maxRange, angle, x, y, height, singleTarget, enemyOnly, aliveOnly, includeBuilding)
call arrow.onHit(function unitHit)
call RemoveLocation(loc)
call RemoveLocation(sLoc)
set loc = null
set sLoc = null
endfunction
private function onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterPlayerUnitEvent(t, Player(0), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
call TriggerAddCondition(t, Condition(function condition))
call TriggerAddAction(t, function onCast)
set t = null
endfunction
endscope
scope Preload
private module m
private static method onInit takes nothing returns nothing
call Preload("Abilities\\Weapons\\HydraliskImpact\\HydraliskImpact.mdl")
call Preload("Abilities\\Weapons\\AncientProtectorMissile\\AncientProtectorMissile.mdl")
call Preload(MODEL)
endmethod
endmodule
private struct s
implement m
endstruct
endscope
scope Demo initializer onInit
globals
constant string MODEL = "Abilities\\Weapons\\BallistaMissile\\BallistaMissile.mdl"
private unit tester
endglobals
private function unitHitted takes nothing returns nothing
local Missile arrow = Missile.getData()
local unit u = arrow.getLastHittedUnit()
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 1, GetUnitName(u) + " is hitted by a missile")
set u = null
// On unit hit functions
endfunction
private function periodic takes nothing returns nothing
// Periodic functions
endfunction
private function death takes nothing returns nothing
// On missile destroy functions
endfunction
private function onCast takes nothing returns nothing
local Missile arrow
local real AoE = 100
local real speed = 522
local real maxRange = 1200
local real angle = GetUnitFacing(tester)
local real x = GetUnitX(tester)
local real y = GetUnitY(tester)
local real height = 50
local boolean singleTarget = false
local boolean enemyOnly = true
local boolean aliveOnly = true
local boolean includeBuilding = true
set arrow = Missile.create(tester, MODEL, AoE, speed, maxRange, angle, x, y, height, singleTarget, enemyOnly, aliveOnly, includeBuilding)
call arrow.onHit(function unitHitted)
call arrow.onPeriodic(function periodic)
call arrow.onDest(function death)
endfunction
private function onInit takes nothing returns nothing
local trigger t = CreateTrigger()
set tester = CreateUnit(Player(0), 'Hvwd', GetStartLocationX(0), GetStartLocationY(0), bj_UNIT_FACING)
call TriggerRegisterPlayerEvent(t, Player(0), EVENT_PLAYER_END_CINEMATIC)
call TriggerAddAction(t, function onCast)
set t = null
call BJDebugMsg("Press ESC to generate harmless missiles")
endfunction
endscope