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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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