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.
 
> 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?
 
> 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?
 
> 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.
 
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?
 
> 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 The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top