//TESH.scrollpos=0
//TESH.alwaysfold=0
Name | Type | is_array | initial_value |
hero | unit | No |
//TESH.scrollpos=0
//TESH.alwaysfold=0
[COLOR="Yellow"][SIZE="5"][B]AutoCastSystem v1.5[/B][/SIZE][/COLOR]
[box=Introduction]
This system allows your AI or even your own units to automatically cast random spells at hand.
It has a simple mechanic to evaluate a situation or function that you created on when, what, where
to cast a certain spell.
[/box]
[tabs]
[tab=Code]
[jass]
/*********************************************************************
AutoCastSystem v1.5
by mckill2009
**********************************************************************
INSTALLATION:
Copy ALL the required libraries and the AutoCastSystem trigger to your trigger editor
OR
Copy ALL the triggers from the AutoCastSystem folder
**********************************************************************
Struct API: See the tutorial/demo on how this works
static method register takes unit caster returns thistype
- this MUST be called first
- local ACS test >>> set test = test.register(YOUR_UNIT)
method spellTypeUnitTarget takes code func, integer spellOrderID returns nothing
method spellTypePointTarget takes code func, integer spellOrderID returns nothing
method spellTypeNoTarget takes code func, integer spellOrderID returns nothing
- call test.spellTypeUnitTarget(function CODE_THAT_RETURNS_BOOLEAN, ORDER_ID)
- the boolean must return true for the caster to cast spell automatically
- increase the chance of casting a particular spell by calling this 2 or more
- CONS, if you created a lot of this, say 6, you need to remove 6 also if you want
the spell not to be casted at all
method removeSpellOrderUnit takes integer spellOrderID returns nothing
method removeSpellOrderPoint takes integer spellOrderID returns nothing
method removeSpellOrderNone takes integer spellOrderID returns nothing
- call test.removeSpellOrderUnit(ORDER_ID)
- if you dont want the spell to be casted anymore simply remove it ONE BY ONE
- this only DECREASES the chance for a particular spell to be casted NOT REMOVING IT ALL
- if you want to remove all, do this calling as many times you added a particular spellOrderID
method launch takes real interval returns nothing
- call test.launch(1.0)
- recommended is 1 but you may set it as low as 0.03125 for a very fast trigger evaluation
thus FAST casting a spell
method removeWhenCasterDies takes boolean b returns nothing
- default is false but you may change it to true
- call test.removeWhenCasterDies(true)
method removeAutocast takes nothing returns nothing
- this removes the caster from the system even if it's still alive
- call test.removeAutocast()
Struct globals: you may use these for code func you create that returns a boolean
CS.caster (readonly)
CS.target (can modify)
CS.SpellTargetX (can modify)
CS.SpellTargetY (can modify)
**********************************************************************
CREDITS:
TimerUtils by Vexorian http://www.wc3c.net/showthread.php?t=101322
Table by Bribe http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
IsUnitChanneling by Magtheridon96 http://www.hiveworkshop.com/forums/jass-resources-412/snippet-isunitchanneling-211254/
**********************************************************************/
library AutoCastSystem uses TimerUtils, Table, IsUnitChanneling
globals
/***********************************************
* NON-CONFIGURABLES
************************************************/
private TableArray trig
private TableArray oID
private Table chk
private unit TempCaster = null
endglobals
private function UnitAlive takes unit u returns boolean
return not (IsUnitType(u, UNIT_TYPE_DEAD) or u==null)
endfunction
private module startCSinit
private static method onInit takes nothing returns nothing
set trig = TableArray[8192]
set oID = TableArray[8192]
set chk = Table.create()
endmethod
endmodule
struct CS extends array
readonly static unit caster
static unit target
static real SpellTargetX
static real SpellTargetY
static method returnCaster takes nothing returns nothing
set CS.caster = TempCaster
endmethod
implement startCSinit
endstruct
struct ACS
private unit caster
private integer indexUnitMax
private integer indexPointMax
private integer indexNoneMax
private boolean remove
private boolean removeAuto
private boolean checkTimer
private static constant integer pointIndex = 10000
private static constant integer noneIndex = 20000
private method removeEx takes integer index, integer maxIndex returns nothing
local integer i = index
loop
set i = i+1
exitwhen i==maxIndex
set oID[this][i] = 0
call DestroyTrigger(trig[this].trigger[i])
set trig[this].trigger[i] = null
endloop
endmethod
private method destroy takes nothing returns nothing
call .removeEx(0, .indexUnitMax)
call .removeEx(pointIndex, .indexPointMax)
call .removeEx(noneIndex, .indexNoneMax)
call chk.remove(GetHandleId(.caster))
set .caster = null
call .deallocate()
endmethod
private static method period takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
local integer randomizer
local integer i
if .remove then
call ReleaseTimer(GetExpiredTimer())
call .destroy()
elseif UnitAlive(.caster) then
set TempCaster = .caster
call CS.returnCaster()
set .caster = TempCaster
if not IsUnitChanneling(.caster) then
set randomizer = GetRandomInt(1, 3)
if randomizer==1 then //Unit target
set i = GetRandomInt(1, .indexUnitMax)
if oID[this][i] > 0 then
if TriggerEvaluate(trig[this].trigger[i]) then
call IssueTargetOrderById(.caster, oID[this][i], CS.target)
endif
endif
elseif randomizer==2 then //Point target
set i = GetRandomInt(pointIndex, .indexPointMax)
if oID[this][i] > 0 then
if TriggerEvaluate(trig[this].trigger[i]) then
call IssuePointOrderById(.caster, oID[this][i], CS.SpellTargetX, CS.SpellTargetY)
endif
endif
elseif randomizer==3 then //No target
set i = GetRandomInt(noneIndex, .indexNoneMax)
if oID[this][i] > 0 then
if TriggerEvaluate(trig[this].trigger[i]) then
call IssueImmediateOrderById(.caster, oID[this][i])
endif
endif
endif
endif
elseif .removeAuto then
set .remove = true
endif
set t = null
endmethod
/***********************************************
* SYSTEM API's
************************************************/
static method register takes unit caster returns thistype
local thistype this
if chk.has(GetHandleId(caster)) then
debug if IsUnitType(caster, UNIT_TYPE_HERO) then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "thistype.register ERROR: "+GetHeroProperName(caster)+" is already registered!")
else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "thistype.register ERROR: "+GetUnitName(caster)+" is already registered!")
endif
return 0
else
set this = allocate()
set .caster = caster
set .remove = false
set .indexUnitMax = 0
set .indexPointMax = pointIndex
set .indexNoneMax = noneIndex
set .removeAuto = false
set .checkTimer = true
set chk[GetHandleId(caster)] = this
endif
return this
endmethod
method spellTypeUnitTarget takes code func, integer spellOrderID returns nothing
set .indexUnitMax = .indexUnitMax + 1
set oID[this][.indexUnitMax] = spellOrderID
set trig[this].trigger[.indexUnitMax] = CreateTrigger()
call TriggerAddCondition(trig[this].trigger[.indexUnitMax], Condition(func))
endmethod
method spellTypePointTarget takes code func, integer spellOrderID returns nothing
set .indexPointMax = .indexPointMax + 1
set oID[this][.indexPointMax] = spellOrderID
set trig[this].trigger[.indexPointMax] = CreateTrigger()
call TriggerAddCondition(trig[this].trigger[.indexPointMax], Condition(func))
endmethod
method spellTypeNoTarget takes code func, integer spellOrderID returns nothing
set .indexNoneMax = .indexNoneMax + 1
set oID[this][.indexNoneMax] = spellOrderID
set trig[this].trigger[.indexNoneMax] = CreateTrigger()
call TriggerAddCondition(trig[this].trigger[.indexNoneMax], Condition(func))
endmethod
method removeSpellOrderUnit takes integer spellOrderID returns nothing
local integer i = 0
loop
set i = i+1
if oID[this][i]==spellOrderID then
set oID[this][i] = oID[this][.indexUnitMax]
set oID[this][.indexUnitMax] = 0
call DestroyTrigger(trig[this].trigger[.indexUnitMax])
set trig[this].trigger[.indexUnitMax] = null
set .indexUnitMax = .indexUnitMax - 1
exitwhen true
endif
exitwhen i==.indexUnitMax
endloop
endmethod
method removeSpellOrderPoint takes integer spellOrderID returns nothing
local integer i = pointIndex
loop
set i = i+1
if oID[this][i]==spellOrderID then
set oID[this][i] = oID[this][.indexPointMax]
set oID[this][.indexPointMax] = 0
call DestroyTrigger(trig[this].trigger[.indexPointMax])
set trig[this].trigger[.indexPointMax] = null
set .indexPointMax = .indexPointMax - 1
exitwhen true
endif
exitwhen i==.indexPointMax
endloop
endmethod
method removeSpellOrderNone takes integer spellOrderID returns nothing
local integer i = noneIndex
loop
set i = i+1
if oID[this][i]==spellOrderID then
set oID[this][i] = oID[this][.indexNoneMax]
set oID[this][.indexNoneMax] = 0
call DestroyTrigger(trig[this].trigger[.indexNoneMax])
set trig[this].trigger[.indexNoneMax] = null
set .indexNoneMax = .indexNoneMax - 1
exitwhen true
endif
exitwhen i==.indexNoneMax
endloop
endmethod
method launch takes real interval returns nothing
if chk.has(GetHandleId(.caster)) then
if .checkTimer then
set .checkTimer = false
call TimerStart(NewTimerEx(this), interval, true, function thistype.period)
else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "thistype.launch ERROR: Timer is already active!")
endif
else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "thistype.launch ERROR: attempt to start a null caster.")
endif
endmethod
method removeWhenCasterDies takes boolean b returns nothing
set .removeAuto = b
endmethod
method removeAutocast takes nothing returns nothing
set .remove = true
endmethod
endstruct
endlibrary
[/jass]
[/tab]
[tab=Tutorial (vJass)]
This is only a vJass demo, you MUST start reading at function CastAITestBloodmage
[jass]
library TEST uses AutoCastSystem, RandomUnit
globals
ACS bloodmage
ACS warden
endglobals
private function UnitAlive takes unit u returns boolean
return not IsUnitType(u, UNIT_TYPE_DEAD) and GetUnitTypeId(u)!=0
endfunction
private function FilterAllies takes unit flteredUnit returns boolean
return UnitAlive(flteredUnit) and not IsUnitEnemy(CS.caster, GetOwningPlayer(flteredUnit))
endfunction
private function FilterEnemies takes unit flteredUnit returns boolean
return UnitAlive(flteredUnit) and IsUnitEnemy(CS.caster, GetOwningPlayer(flteredUnit))
endfunction
/*******************************************************
* CAST CONDITIONS AND UNIT FILTERING
********************************************************/
private function CheckAlly takes nothing returns boolean
return FilterAllies(GetFilterUnit())
endfunction
private function CheckEnemies takes nothing returns boolean
return FilterEnemies(GetFilterUnit())
endfunction
private function TargetEnemyNonStructure takes nothing returns boolean
return FilterEnemies(GetFilterUnit()) and not IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE)
endfunction
private function TargetEnemyStructure takes nothing returns boolean
return FilterEnemies(GetFilterUnit()) and IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE)
endfunction
private function TargetEnemyFlyers takes nothing returns boolean
return FilterEnemies(GetFilterUnit()) and IsUnitType(GetFilterUnit(), UNIT_TYPE_FLYING)
endfunction
/*******************************************************
* NO-TARGET
********************************************************/
private function CastChanceNoTarget takes nothing returns boolean
local boolean b = false
if GetRandomInt(0, 100) < 80 then
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 500, Filter(function CheckEnemies))
if CS.target!=null then
set b = true
endif
endif
return b
endfunction
/*******************************************************
* TARGET UNIT
********************************************************/
private function CastHealAlly takes nothing returns boolean
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 900, Filter(function CheckAlly))
if CS.target!=null and GetWidgetLife(CS.target) < GetUnitState(CS.target, UNIT_STATE_MAX_LIFE) and GetUnitAbilityLevel(CS.target, 'Brej')==0 then
return true
endif
return false
endfunction
private function CastTargetEnemy takes nothing returns boolean
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 900, Filter(function TargetEnemyNonStructure))
return CS.target!=null
endfunction
private function CastChanceTargetEnemy takes nothing returns boolean
local boolean b = false
if GetRandomInt(0, 100) < 70 then
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 900, Filter(function TargetEnemyFlyers))
if CS.target!=null and GetUnitAbilityLevel(CS.target, 'Bena')==0 then
set b = true
endif
endif
return b
endfunction
/*******************************************************
* TARGET POINT
********************************************************/
//This is how to manipulate the spell's execution, this is a condition for the spell to take place
//if it returns true the spell is casted
private function CastPointEnemy takes nothing returns boolean
/********************************************
Struct globals you may use, these are self explanatory
CS.caster (readonly)
CS.target (can modify)
CS.SpellTargetX (can modify)
CS.SpellTargetY (can modify)
*********************************************/
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 900, Filter(function TargetEnemyNonStructure))
if CS.target!=null then
set CS.SpellTargetX = GetUnitX(CS.target)
set CS.SpellTargetY = GetUnitY(CS.target)
return true
endif
//if you do not have a target but just want to cast a spell in the XY of the map
//you may set the CS.SpellTargetX and CS.SpellTargetY to a coordinate you like
//and just return true, but remember that the XY MUST be in range of spell
return false
endfunction
/*******************************************************
* START READING HERE
********************************************************/
private function RemoveBloodmageFromSystem takes nothing returns nothing
//removes the unit from the system
call bloodmage.removeAutocast()
call BJDebugMsg("******************************************")
call BJDebugMsg("Bloodmage removed from casting ALL spells")
endfunction
private function RemoveSpell takes nothing returns nothing
call bloodmage.removeSpellOrderUnit(852218) //carrion swarm
call BJDebugMsg("******************************************")
call BJDebugMsg("After a while again, Blood mage will no longer cast any spells.")
call TimerStart(CreateTimer(), 10.0, false, function RemoveBloodmageFromSystem)
endfunction
function CastAITestBloodmage takes unit u returns nothing
//create a global or local ACS based on struct name
//in this scenario, global is created for it to be used outside this function
//a GLOBAL instance is highly recommended
//units cannot be registered twice
set bloodmage = bloodmage.register(u)
/***************************************************************************
* SPELL REGISTRATION, use OrderID's
****************************************************************************/
//the unit will cast 6 different kinds of spell based below
//you do not need to create a new function, you may add different spell to the same function but
//depends on your condition though
//you may create a condition such as CHANCE for it to be true
//for the purpose of this demo, the cooldown is set to 2 seconds for each of the spells
//all spells also is at range from the standing hero and target unit or point
//the function is the execution condtion while the number is the orderID of the spell
//the function must return TRUE for the spell to take place
//you may add the spell orderID twice or many times to increase the chance for the unit to cast a certain spell
call bloodmage.spellTypeUnitTarget(function CastHealAlly, 852160) //rejuvenation
call bloodmage.spellTypeUnitTarget(function CastTargetEnemy, 852189) //cripple
call bloodmage.spellTypeUnitTarget(function CastTargetEnemy, 852487) //life drain (channel)
call bloodmage.spellTypePointTarget(function CastPointEnemy, 852488) //flame strike
call bloodmage.spellTypePointTarget(function CastPointEnemy, 852218) //carrion swarm
call bloodmage.spellTypeNoTarget(function CastChanceNoTarget, 852588) //howl of terror
/***************************************************************************
* STARTING THE CASTING
****************************************************************************/
//the last thing you need to do, set the interval on how many seconds your caster casts the spell
call bloodmage.launch(1.0) //recommended is 1 second, but it's all up to you
//call BJDebugMsg("******************************************")
//call BJDebugMsg("Carrion Swarm will be removed from casting after a while.")
//removing a specific spell from the system
//in this example, the global bloodmage instance is used
//call TimerStart(CreateTimer(), 10.0, false, function RemoveSpell)
endfunction
function CastAITestWarden takes unit u returns nothing
set warden = warden.register(u)
call warden.spellTypePointTarget(function CastPointEnemy, 852238) //rain of fire (channel)
call warden.spellTypePointTarget(function CastPointEnemy, 852525) //blink
call warden.spellTypeUnitTarget(function CastChanceTargetEnemy, 852106) //ensnare
call warden.spellTypeUnitTarget(function CastChanceTargetEnemy, 852527) //shadow strike
call warden.spellTypeNoTarget(function CastChanceNoTarget, 852526) //fan of knives
call warden.launch(1.0)
endfunction
endlibrary
[/jass]
[/tab]
[tab=Pros/Cons]
Pros
- Cast a spell even if your unit is NOT an AI
- Works on channeled spells
- Casts random spells, makes the AI can use ALL of the spells
- Has time interval on when to cast
Cons
- Manually make a trigger/funcion that evaluates a certain situation in order for AI to cast a spell, meaning the evaluation MUST return TRUE
- Manually removes your spellOrderID, if you created 6, you need to remove 6
[/tab]
[/tabs]
[hidden=Changelogs]
v1.5
- Added a global variable TempCaster to avoid modifying the CS.caster which is now readonly
- Added module initializer
v1.4
- Most of the suggestion of MyPad is considered
- Some useless instance variable deleted
v1.3
- ADDED timer check whether it has been launched already or not
- Most of Quilnezhas been made
v1.2
- Reorganized and renamed API's
- Triggers now destroyed and nulled when unit is removed from system
v1.1
- Added an automatic removal from the system if unit is dead
[/hidden]
[hidden=Credits]
TimerUtils by Vexorian
Table by Bribe
IsUnitChanneling by Magtheridon96
For Suggestions:
Special thanks to Quilnez and MyPad their review :D
[/hidden]
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope abc initializer init
globals
private ACS a
endglobals
private function init takes nothing returns nothing
set a = a.register(null)
call a.spellTypeUnitTarget(null, 000001)
call a.spellTypeUnitTarget(null, 000002)
call a.spellTypeUnitTarget(null, 000003)
call a.spellTypeUnitTarget(null, 000004)
call a.spellTypeUnitTarget(null, 000005)
call a.spellTypeUnitTarget(null, 000006)
call a.removeSpellOrderUnit(000004)
endfunction
endscope
//TESH.scrollpos=0
//TESH.alwaysfold=0
library RandomUnit
globals
private unit TempU = null
private group TempG = CreateGroup()
private integer count = 0
endglobals
private function EnumUnits takes nothing returns nothing
set count = count+1
endfunction
function GetRandomUnitInArea takes real x, real y, real aoe, boolexpr b returns unit
local integer ran
call GroupEnumUnitsInRange(TempG, x, y, aoe, b)
set count = 0
call ForGroup(TempG, function EnumUnits)
set ran = GetRandomInt(1, count)
set count = 0
loop
set TempU = FirstOfGroup(TempG)
set count = count + 1
exitwhen TempU==null or count==ran
call GroupRemoveUnit(TempG, TempU)
endloop
call GroupClear(TempG)
return TempU
endfunction
endlibrary
//TESH.scrollpos=130
//TESH.alwaysfold=0
library TEST uses AutoCastSystem, RandomUnit
globals
ACS bloodmage
ACS warden
endglobals
private function UnitAlive takes unit u returns boolean
return not IsUnitType(u, UNIT_TYPE_DEAD) and GetUnitTypeId(u)!=0
endfunction
private function FilterAllies takes unit flteredUnit returns boolean
return UnitAlive(flteredUnit) and not IsUnitEnemy(CS.caster, GetOwningPlayer(flteredUnit))
endfunction
private function FilterEnemies takes unit flteredUnit returns boolean
return UnitAlive(flteredUnit) and IsUnitEnemy(CS.caster, GetOwningPlayer(flteredUnit))
endfunction
/*******************************************************
* CAST CONDITIONS AND UNIT FILTERING
********************************************************/
private function CheckAlly takes nothing returns boolean
return FilterAllies(GetFilterUnit())
endfunction
private function CheckEnemies takes nothing returns boolean
return FilterEnemies(GetFilterUnit())
endfunction
private function TargetEnemyNonStructure takes nothing returns boolean
return FilterEnemies(GetFilterUnit()) and not IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE)
endfunction
private function TargetEnemyStructure takes nothing returns boolean
return FilterEnemies(GetFilterUnit()) and IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE)
endfunction
private function TargetEnemyFlyers takes nothing returns boolean
return FilterEnemies(GetFilterUnit()) and IsUnitType(GetFilterUnit(), UNIT_TYPE_FLYING)
endfunction
/*******************************************************
* NO-TARGET
********************************************************/
private function CastChanceNoTarget takes nothing returns boolean
local boolean b = false
if GetRandomInt(0, 100) < 80 then
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 500, Filter(function CheckEnemies))
if CS.target!=null then
set b = true
endif
endif
return b
endfunction
/*******************************************************
* TARGET UNIT
********************************************************/
private function CastHealAlly takes nothing returns boolean
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 900, Filter(function CheckAlly))
if CS.target!=null and GetWidgetLife(CS.target) < GetUnitState(CS.target, UNIT_STATE_MAX_LIFE) and GetUnitAbilityLevel(CS.target, 'Brej')==0 then
return true
endif
return false
endfunction
private function CastTargetEnemy takes nothing returns boolean
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 900, Filter(function TargetEnemyNonStructure))
return CS.target!=null
endfunction
private function CastChanceTargetEnemy takes nothing returns boolean
local boolean b = false
if GetRandomInt(0, 100) < 70 then
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 900, Filter(function TargetEnemyFlyers))
if CS.target!=null and GetUnitAbilityLevel(CS.target, 'Bena')==0 then
set b = true
endif
endif
return b
endfunction
/*******************************************************
* TARGET POINT
********************************************************/
//This is how to manipulate the spell's execution, this is a condition for the spell to take place
//if it returns true the spell is casted
private function CastPointEnemy takes nothing returns boolean
/********************************************
Struct globals you may use, these are self explanatory
CS.caster (readonly)
CS.target (can modify)
CS.SpellTargetX (can modify)
CS.SpellTargetY (can modify)
*********************************************/
set CS.target = GetRandomUnitInArea(GetUnitX(CS.caster), GetUnitY(CS.caster), 900, Filter(function TargetEnemyNonStructure))
if CS.target!=null then
set CS.SpellTargetX = GetUnitX(CS.target)
set CS.SpellTargetY = GetUnitY(CS.target)
return true
endif
//if you do not have a target but just want to cast a spell in the XY of the map
//you may set the CS.SpellTargetX and CS.SpellTargetY to a coordinate you like
//and just return true, but remember that the XY MUST be in range of spell
return false
endfunction
/*******************************************************
* START READING HERE
********************************************************/
private function RemoveBloodmageFromSystem takes nothing returns nothing
//removes the unit from the system
call bloodmage.removeAutocast()
call BJDebugMsg("******************************************")
call BJDebugMsg("Bloodmage removed from casting ALL spells")
endfunction
private function RemoveSpell takes nothing returns nothing
call bloodmage.removeSpellOrderUnit(852218) //carrion swarm
call BJDebugMsg("******************************************")
call BJDebugMsg("After a while again, Blood mage will no longer cast any spells.")
call TimerStart(CreateTimer(), 10.0, false, function RemoveBloodmageFromSystem)
endfunction
function CastAITestBloodmage takes unit u returns nothing
//create a global or local ACS based on struct name
//in this scenario, global is created for it to be used outside this function
//a GLOBAL instance is highly recommended
//units cannot be registered twice
set bloodmage = bloodmage.register(u)
/***************************************************************************
* SPELL REGISTRATION, use OrderID's
****************************************************************************/
//the unit will cast 6 different kinds of spell based below
//you do not need to create a new function, you may add different spell to the same function but
//depends on your condition though
//you may create a condition such as CHANCE for it to be true
//for the purpose of this demo, the cooldown is set to 2 seconds for each of the spells
//all spells also is at range from the standing hero and target unit or point
//the function is the execution condtion while the number is the orderID of the spell
//the function must return TRUE for the spell to take place
//you may add the spell orderID twice or many times to increase the chance for the unit to cast a certain spell
call bloodmage.spellTypeUnitTarget(function CastHealAlly, 852160) //rejuvenation
call bloodmage.spellTypeUnitTarget(function CastTargetEnemy, 852189) //cripple
call bloodmage.spellTypeUnitTarget(function CastTargetEnemy, 852487) //life drain (channel)
call bloodmage.spellTypePointTarget(function CastPointEnemy, 852488) //flame strike
call bloodmage.spellTypePointTarget(function CastPointEnemy, 852218) //carrion swarm
call bloodmage.spellTypeNoTarget(function CastChanceNoTarget, 852588) //howl of terror
/***************************************************************************
* STARTING THE CASTING
****************************************************************************/
//the last thing you need to do, set the interval on how many seconds your caster casts the spell
call bloodmage.launch(1.0) //recommended is 1 second, but it's all up to you
//call BJDebugMsg("******************************************")
//call BJDebugMsg("Carrion Swarm will be removed from casting after a while.")
//removing a specific spell from the system
//in this example, the global bloodmage instance is used
//call TimerStart(CreateTimer(), 10.0, false, function RemoveSpell)
endfunction
function CastAITestWarden takes unit u returns nothing
set warden = warden.register(u)
call warden.spellTypePointTarget(function CastPointEnemy, 852238) //rain of fire (channel)
call warden.spellTypePointTarget(function CastPointEnemy, 852525) //blink
call warden.spellTypeUnitTarget(function CastChanceTargetEnemy, 852106) //ensnare
call warden.spellTypeUnitTarget(function CastChanceTargetEnemy, 852527) //shadow strike
call warden.spellTypeNoTarget(function CastChanceNoTarget, 852526) //fan of knives
call warden.launch(1.0)
endfunction
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 Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 3.1.1.0
One map, one hashtable. Welcome to NewTable 3.1
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
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
/**************************************
*
* IsUnitChanneling
* v2.1.0.0
* By Magtheridon96
*
* - Tells whether a unit is channeling or not.
*
* Requirements:
* -------------
*
* - RegisterPlayerUnitEvent by Magtheridon96
* - hiveworkshop.com/forums/jass-resources-412/snippet-registerplayerunitevent-203338/
*
* Optional:
* ---------
*
* - UnitIndexer by Nestharus
* - hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/
* - Table by Bribe
* - hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
* API:
* ----
*
* - function IsUnitChanneling takes unit whichUnit returns boolean
* - Tells whether a unit is channeling or not.
* (This function is only available if you have UnitIndexer)
*
* - function IsUnitChannelingById takes integer unitIndex returns boolean
* - Tells whether a unti is channeling or not given the unit index.
*
**************************************/
library IsUnitChanneling requires optional UnitIndexer, optional Table, RegisterPlayerUnitEvent
private struct OnChannel extends array
static if LIBRARY_UnitIndexer then
static boolean array channeling
else
static if LIBRARY_Table then
static key k
static Table channeling = k
else
static hashtable hash = InitHashtable()
endif
endif
private static method onEvent takes nothing returns nothing
static if LIBRARY_UnitIndexer then
local integer id = GetUnitUserData(GetTriggerUnit())
set channeling[id] = not channeling[id]
else
static if LIBRARY_Table then
local integer id = GetHandleId(GetTriggerUnit())
set channeling.boolean[id] = not channeling.boolean[id]
else
local integer id = GetHandleId(GetTriggerUnit())
call SaveBoolean(hash, 0, id, not LoadBoolean(hash, 0, id))
endif
endif
endmethod
private static method onInit takes nothing returns nothing
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_CHANNEL, function thistype.onEvent)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_ENDCAST, function thistype.onEvent)
endmethod
endstruct
static if LIBRARY_UnitIndexer then
function IsUnitChannelingById takes integer id returns boolean
return OnChannel.channeling[id]
endfunction
endif
function IsUnitChanneling takes unit u returns boolean
static if LIBRARY_UnitIndexer then
return OnChannel.channeling[GetUnitUserData(u)]
else
static if LIBRARY_Table then
return OnChannel.channeling.boolean[GetHandleId(u)]
else
return LoadBoolean(OnChannel.hash, 0, GetHandleId(u))
endif
endif
endfunction
endlibrary
//TESH.scrollpos=29
//TESH.alwaysfold=0
/**************************************************************
*
* RegisterPlayerUnitEvent
* v5.1.0.1
* By Magtheridon96
*
* I would like to give a special thanks to Bribe, azlier
* and BBQ for improving this library. For modularity, it only
* supports player unit events.
*
* Functions passed to RegisterPlayerUnitEvent must either
* return a boolean (false) or nothing. (Which is a Pro)
*
* Warning:
* --------
*
* - Don't use TriggerSleepAction inside registered code.
* - Don't destroy a trigger unless you really know what you're doing.
*
* API:
* ----
*
* - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
* - Registers code that will execute when an event fires.
* - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
* - Registers code that will execute when an event fires for a certain player.
* - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
* - Returns the trigger corresponding to ALL functions of a playerunitevent.
*
**************************************************************/
library RegisterPlayerUnitEvent // Special Thanks to Bribe and azlier
globals
private trigger array t
endglobals
function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
local integer i = GetHandleId(p)
local integer k = 15
if t[i] == null then
set t[i] = CreateTrigger()
loop
call TriggerRegisterPlayerUnitEvent(t[i], Player(k), p, null)
exitwhen k == 0
set k = k - 1
endloop
endif
call TriggerAddCondition(t[i], Filter(c))
endfunction
function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
if t[i] == null then
set t[i] = CreateTrigger()
call TriggerRegisterPlayerUnitEvent(t[i], pl, p, null)
endif
call TriggerAddCondition(t[i], Filter(c))
endfunction
function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
return t[GetHandleId(p)]
endfunction
endlibrary
//TESH.scrollpos=263
//TESH.alwaysfold=0
/*********************************************************************
AutoCastSystem v1.5
by mckill2009
**********************************************************************
INSTALLATION:
Copy ALL the required libraries and the AutoCastSystem trigger to your trigger editor
OR
Copy ALL the triggers from the AutoCastSystem folder
**********************************************************************
Struct API: See the tutorial/demo on how this works
static method register takes unit caster returns thistype
- this MUST be called first
- local ACS test >>> set test = test.register(YOUR_UNIT)
method spellTypeUnitTarget takes code func, integer spellOrderID returns nothing
method spellTypePointTarget takes code func, integer spellOrderID returns nothing
method spellTypeNoTarget takes code func, integer spellOrderID returns nothing
- call test.spellTypeUnitTarget(function CODE_THAT_RETURNS_BOOLEAN, ORDER_ID)
- the boolean must return true for the caster to cast spell automatically
- increase the chance of casting a particular spell by calling this 2 or more
- CONS, if you created a lot of this, say 6, you need to remove 6 also if you want
the spell not to be casted at all
method removeSpellOrderUnit takes integer spellOrderID returns nothing
method removeSpellOrderPoint takes integer spellOrderID returns nothing
method removeSpellOrderNone takes integer spellOrderID returns nothing
- call test.removeSpellOrderUnit(ORDER_ID)
- if you dont want the spell to be casted anymore simply remove it ONE BY ONE
- this only DECREASES the chance for a particular spell to be casted NOT REMOVING IT ALL
- if you want to remove all, do this calling as many times you added a particular spellOrderID
method launch takes real interval returns nothing
- call test.launch(1.0)
- recommended is 1 but you may set it as low as 0.03125 for a very fast trigger evaluation
thus FAST casting a spell
method removeWhenCasterDies takes boolean b returns nothing
- default is false but you may change it to true
- call test.removeWhenCasterDies(true)
method removeAutocast takes nothing returns nothing
- this removes the caster from the system even if it's still alive
- call test.removeAutocast()
Struct globals: you may use these for code func you create that returns a boolean
CS.caster (readonly)
CS.target (can modify)
CS.SpellTargetX (can modify)
CS.SpellTargetY (can modify)
**********************************************************************
CREDITS:
TimerUtils by Vexorian http://www.wc3c.net/showthread.php?t=101322
Table by Bribe http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
IsUnitChanneling by Magtheridon96 http://www.hiveworkshop.com/forums/jass-resources-412/snippet-isunitchanneling-211254/
**********************************************************************/
library AutoCastSystem uses TimerUtils, Table, IsUnitChanneling
globals
/***********************************************
* NON-CONFIGURABLES
************************************************/
private TableArray trig
private TableArray oID
private Table chk
private unit TempCaster = null
endglobals
private function UnitAlive takes unit u returns boolean
return not (IsUnitType(u, UNIT_TYPE_DEAD) or u==null)
endfunction
private module startCSinit
private static method onInit takes nothing returns nothing
set trig = TableArray[8192]
set oID = TableArray[8192]
set chk = Table.create()
endmethod
endmodule
struct CS extends array
readonly static unit caster
static unit target
static real SpellTargetX
static real SpellTargetY
static method returnCaster takes nothing returns nothing
set CS.caster = TempCaster
endmethod
implement startCSinit
endstruct
struct ACS
private unit caster
private integer indexUnitMax
private integer indexPointMax
private integer indexNoneMax
private boolean remove
private boolean removeAuto
private boolean checkTimer
private static constant integer pointIndex = 10000
private static constant integer noneIndex = 20000
private method removeEx takes integer index, integer maxIndex returns nothing
local integer i = index
loop
set i = i+1
exitwhen i==maxIndex
set oID[this][i] = 0
call DestroyTrigger(trig[this].trigger[i])
set trig[this].trigger[i] = null
endloop
endmethod
private method destroy takes nothing returns nothing
call .removeEx(0, .indexUnitMax)
call .removeEx(pointIndex, .indexPointMax)
call .removeEx(noneIndex, .indexNoneMax)
call chk.remove(GetHandleId(.caster))
set .caster = null
call .deallocate()
endmethod
private static method period takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
local integer randomizer
local integer i
if .remove then
call ReleaseTimer(GetExpiredTimer())
call .destroy()
elseif UnitAlive(.caster) then
set TempCaster = .caster
call CS.returnCaster()
set .caster = TempCaster
if not IsUnitChanneling(.caster) then
set randomizer = GetRandomInt(1, 3)
if randomizer==1 then //Unit target
set i = GetRandomInt(1, .indexUnitMax)
if oID[this][i] > 0 then
if TriggerEvaluate(trig[this].trigger[i]) then
call IssueTargetOrderById(.caster, oID[this][i], CS.target)
endif
endif
elseif randomizer==2 then //Point target
set i = GetRandomInt(pointIndex, .indexPointMax)
if oID[this][i] > 0 then
if TriggerEvaluate(trig[this].trigger[i]) then
call IssuePointOrderById(.caster, oID[this][i], CS.SpellTargetX, CS.SpellTargetY)
endif
endif
elseif randomizer==3 then //No target
set i = GetRandomInt(noneIndex, .indexNoneMax)
if oID[this][i] > 0 then
if TriggerEvaluate(trig[this].trigger[i]) then
call IssueImmediateOrderById(.caster, oID[this][i])
endif
endif
endif
endif
elseif .removeAuto then
set .remove = true
endif
set t = null
endmethod
/***********************************************
* SYSTEM API's
************************************************/
static method register takes unit caster returns thistype
local thistype this
if chk.has(GetHandleId(caster)) then
debug if IsUnitType(caster, UNIT_TYPE_HERO) then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "thistype.register ERROR: "+GetHeroProperName(caster)+" is already registered!")
else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "thistype.register ERROR: "+GetUnitName(caster)+" is already registered!")
endif
return 0
else
set this = allocate()
set .caster = caster
set .remove = false
set .indexUnitMax = 0
set .indexPointMax = pointIndex
set .indexNoneMax = noneIndex
set .removeAuto = false
set .checkTimer = true
set chk[GetHandleId(caster)] = this
endif
return this
endmethod
method spellTypeUnitTarget takes code func, integer spellOrderID returns nothing
set .indexUnitMax = .indexUnitMax + 1
set oID[this][.indexUnitMax] = spellOrderID
set trig[this].trigger[.indexUnitMax] = CreateTrigger()
call TriggerAddCondition(trig[this].trigger[.indexUnitMax], Condition(func))
endmethod
method spellTypePointTarget takes code func, integer spellOrderID returns nothing
set .indexPointMax = .indexPointMax + 1
set oID[this][.indexPointMax] = spellOrderID
set trig[this].trigger[.indexPointMax] = CreateTrigger()
call TriggerAddCondition(trig[this].trigger[.indexPointMax], Condition(func))
endmethod
method spellTypeNoTarget takes code func, integer spellOrderID returns nothing
set .indexNoneMax = .indexNoneMax + 1
set oID[this][.indexNoneMax] = spellOrderID
set trig[this].trigger[.indexNoneMax] = CreateTrigger()
call TriggerAddCondition(trig[this].trigger[.indexNoneMax], Condition(func))
endmethod
method removeSpellOrderUnit takes integer spellOrderID returns nothing
local integer i = 0
loop
set i = i+1
if oID[this][i]==spellOrderID then
set oID[this][i] = oID[this][.indexUnitMax]
set oID[this][.indexUnitMax] = 0
call DestroyTrigger(trig[this].trigger[.indexUnitMax])
set trig[this].trigger[.indexUnitMax] = null
set .indexUnitMax = .indexUnitMax - 1
exitwhen true
endif
exitwhen i==.indexUnitMax
endloop
endmethod
method removeSpellOrderPoint takes integer spellOrderID returns nothing
local integer i = pointIndex
loop
set i = i+1
if oID[this][i]==spellOrderID then
set oID[this][i] = oID[this][.indexPointMax]
set oID[this][.indexPointMax] = 0
call DestroyTrigger(trig[this].trigger[.indexPointMax])
set trig[this].trigger[.indexPointMax] = null
set .indexPointMax = .indexPointMax - 1
exitwhen true
endif
exitwhen i==.indexPointMax
endloop
endmethod
method removeSpellOrderNone takes integer spellOrderID returns nothing
local integer i = noneIndex
loop
set i = i+1
if oID[this][i]==spellOrderID then
set oID[this][i] = oID[this][.indexNoneMax]
set oID[this][.indexNoneMax] = 0
call DestroyTrigger(trig[this].trigger[.indexNoneMax])
set trig[this].trigger[.indexNoneMax] = null
set .indexNoneMax = .indexNoneMax - 1
exitwhen true
endif
exitwhen i==.indexNoneMax
endloop
endmethod
method launch takes real interval returns nothing
if chk.has(GetHandleId(.caster)) then
if .checkTimer then
set .checkTimer = false
call TimerStart(NewTimerEx(this), interval, true, function thistype.period)
else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "thistype.launch ERROR: Timer is already active!")
endif
else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "thistype.launch ERROR: attempt to start a null caster.")
endif
endmethod
method removeWhenCasterDies takes boolean b returns nothing
set .removeAuto = b
endmethod
method removeAutocast takes nothing returns nothing
set .remove = true
endmethod
endstruct
endlibrary