Locust-like function

Gotham

New Member
Reaction score
7
Just a function that I worked hard on. When you call it, it creates a locust-like unit that follows its master and attacks only when the master attacks something. The locust unit is invulnerable and has a duration.
It's not a spell on its own, but can be used to make interesting spells. I'm using it on a passive spell that creates spiders when the hero kills a unit. It can also be used to make a Locust Swarm spell, with locusts only attacking the target of the attacks of their master.

Every locust unit is 100% independant of other locust units, but not independant of it's master. They die when the master dies.
A locust unit moves to some random place between 50 and 250 range of it's master, every 1.5 seconds. It stops attacking after 2 seconds of the last attack of it's master.
It don't steals any life, and when the duration ends, it just dies.

Pre-requisites:
KaTTaNa's Local Handle Variables
A unit, duplicated from the original locust unit, with 0 acquisition range.
Actually you can make any modifications to the unit, but it MUST have the locust ability and 0 acquisition range. Workarounds are possible (like not giving it the locust ability, but then somehow you must prevent order giving). They are not in this function. A similar function is Vexorian's Indie Summons.

Usage:
JASS:
call Locust(owner,'UNITID',x,y,duration)

owner (unit): the master of the unit
'UNITID' (integer): the id (like 'u001') of the unit. The locust unit will be this unit
x (real): the x coordinate where the locust will be created
y (real): the y coordinate where the locust will be created
duration (real): the duration of the unit


If you have suggestions or tips on how to improve this function they will be welcomed.


You can use this function modified in any way, without giving credit (but credit is always welcomed), as long as you don't claim it as your own.


JASS:
function LocMove takes nothing returns nothing
local timer t=GetExpiredTimer()
local unit o=GetHandleUnit(t,"powner")
local unit p=GetHandleUnit(t,"punit")
local location l=PolarProjectionBJ(GetUnitLoc(o),GetRandomReal(50,250),GetRandomReal(1,360)) //edit the first 'GetRandomReal' to change the locations around the master where the locust will wander
local real x=GetLocationX(l)
local real y=GetLocationY(l)

 if GetUnitState(o,UNIT_STATE_LIFE)<0.405 then
   call KillUnit(p)
 else
   call IssuePointOrder(p,"move",x,y)
   call TimerStart(t,1.5,false,function LocMove) //edit the number to change how often (in seconds) the locust will refresh it's position.
 endif

set t=null
set o=null
set p=null
call RemoveLocation(l)
set l=null
endfunction

function LocAttack takes nothing returns nothing
local unit p=GetHandleUnit(GetTriggeringTrigger(),"punit")
local timer t=GetHandleTimer(p,"ptimer")
local unit g=GetAttacker()
local unit r=GetTriggerUnit()
local unit o=GetHandleUnit(GetTriggeringTrigger(),"powner")

 if g==o then
   call PauseTimer(t)
   call TimerStart(t,2,false,function LocMove) //edit the number to change how many seconds after the last master's attack the locusts will keep attacking 
   call IssueTargetOrder(p,"attack",r)
 endif

set p=null
set t=null
set g=null
set r=null
set o=null
endfunction

function LocDeath takes nothing returns nothing
local unit u=GetTriggerUnit()
local trigger s=GetTriggeringTrigger()
local trigger a=GetHandleTrigger(s,"ptratt")
local timer t=GetHandleTimer(s,"ptrtim")

call SetHandleHandle(t,"powner",null)
call SetHandleHandle(t,"punit",null)
call SetHandleHandle(a,"punit",null)
call SetHandleHandle(u,"ptimer",null)
call SetHandleHandle(a,"powner",null)

call DestroyTrigger(a)
call PauseTimer(t)
call DestroyTimer(t)
call DestroyTrigger(s)

set u=null
set s=null
set a=null
set t=null
endfunction

function Locust takes unit o, integer unitid, real x, real y, real d returns unit
local player p=GetOwningPlayer(o)
local unit u
local trigger t
local timer ti
local trigger tr

set u=CreateUnit(p,unitid,x,y,0)
call UnitApplyTimedLife(u,'BTLF',d) //you can remove this line if you want the locusts to be permanent

set ti=CreateTimer()
call TimerStart(ti,0,false,function LocMove)
call SetHandleHandle(u,"ptimer",ti)
call SetHandleHandle(ti,"punit",u)
call SetHandleHandle(ti,"powner",o)

set t=CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_ATTACKED)
call TriggerAddAction(t,function LocAttack)
call SetHandleHandle(t,"punit",u)
call SetHandleHandle(t,"powner",o)

set tr=CreateTrigger()
call TriggerAddAction(tr,function LocDeath)
call TriggerRegisterUnitEvent(tr,u,EVENT_UNIT_DEATH)
call SetHandleHandle(tr,"ptratt",t)
call SetHandleHandle(tr,"ptrtim",ti)

return u
endfunction


Changelog:
· Added spaces and breaks to make a more easy to understand code. Comments are also added in the commonly editable parts so it's even easier.
· Optimized a few things (thanks to SFilip).
· Made it so that it returns a unit, to make tracking a single locust unit outside the function possible.
 

SFilip

Gone but not forgotten
Reaction score
634
> how to improve
Well first of all how about indenting?

Also, as a quick note, you destroy trigger a in LocDeath twice. Shouldn't you destroy s instead of triggering trigger there?
 

Gotham

New Member
Reaction score
7
> how to improve
Well first of all how about indenting?

Also, as a quick note, you destroy trigger a in LocDeath twice. Shouldn't you destroy s instead of triggering trigger there?

Yeah I can make that.

Mmmh, I disable the trigger first, then I destroy it. I don't destroy it twice.

But if I destroy the trigger before setting it's variable to null wouldn't it cause a leak?
 

SFilip

Gone but not forgotten
Reaction score
634
> I disable the trigger first, then I destroy it.
Damn, didn't read it right.
Still there is no need to disable a trigger if you're going to destroy it right away (unrelated: but Timers are a different story, better pause them before destroying).

> if I destroy the trigger before setting it's variable to null wouldn't it cause a leak?
Nope.
The variable (along with setting it to null part) is not related with the state of trigger it stored.
 

Gotham

New Member
Reaction score
7
Nope.
The variable (along with setting it to null part) is not related with the state of trigger it stored.
Yeah I know that, but if I destroy the trigger what happens with the remaining calls? they are executed normally?

edit: meaning if I do
JASS:
local trigger s=GetTriggeringTrigger()
call DestroyTrigger(s)
set s=null

The lines below DestroyTrigger() are executed?
 

SFilip

Gone but not forgotten
Reaction score
634
> The lines below DestroyTrigger() are executed?
Yeah, destroying a trigger is the same as disabling it when it comes to that - it will still execute whatever is left to do, but it won't run again if the event triggers.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top