System UnitLock

Cohadar

master of fugue
Reaction score
209
JASS:

//==============================================================================
//                    UnitLock --  by Cohadar -- v1.0
//==============================================================================
//
//  PURPOUSE:
//       * Preventing multiple spells from moving the unit at the same time,
//         thus preventing spell interference bugs.
//       
//       * Orders spells by priority which has 2 effects:
//           * Stronger spells can overtake the weaker ones.
//           * Weaker spells will not work on units affected by stronger spell.
//
//  FUNCTIONS:
//       * UnitLock_Set(whichUnit, priority, overtakeIfSame) -> lockId
//       * UnitLock_Get(whichUnit) -> lockId
//       * UnitLock_Release(whichUnit, lockId)
//
//  HOW TO USE:
//       * The Set function creates a lock on the unit and returns the lockId
//         lockId can be any number different from zero
//
//       * priority field determines what to do if there already exists a lock on the unit
//         Some spells can have higher priority than others so they can overtake the unit
//
//       * There are 3 lock types: weak, normal, strong
//           * [weak] (priority <= 0.0)
//                 Weak locks will always be overtaken even if new lock is of lower priority
//                 All weak locks are equal
//           * [normal] (0.0 < priority <= 1.0)
//                 Normal locks can be overtaken only by a lock of higher priority 
//                 or if overtakeIfSame is true for a lock of same priority
//                 If overtaking fails Set function returns zero
//           * [strong] (1.0 < priority)
//                 Strong locks cannot be overtaken
//                 All strong locks are equal
//
//       * Get function returns the lockId, or zero if no lock is set.
//         You should use this function inside a spell loop to check if your spell
//         stil has lock on the unit.
//
//       * Release function must provide a valid lockId.
//         Spell instance can unlock only it's own locks.
//
//  DETAILS:
//       * The release method will restore the unit's default fly height.
//
//       * Units that get close to map borders will have their lock broken.
//         This will prevent the spell from taking the unit beyond border.
//
//       * There is no GetPriority function, this is on purpose.
//
//  REQUIREMENTS: 
//       * PUI
//
//  HOW TO IMPORT:
//       * Just create a trigger named UnitLock
//       * convert it to text and replace the whole trigger text with this one
//
//==============================================================================


library UnitLock initializer Init uses PUI
//! runtextmacro PUI_PROPERTY("private", "integer", "LockId", "0")
//! runtextmacro PUI_PROPERTY("private", "real", "LockPriority", "0.0")

globals
    public  constant real HEIGHT_RESTORATION_SPEED = 400.
    private integer lockCounter = 0
endglobals

//===========================================================================
//  Returns zero if lock could not be set
//===========================================================================
public function Set takes unit whichUnit, real priority, boolean overtakeIfSame returns integer
    local boolean overtake = false
    
    if (LockId[whichUnit] == 0) or (LockPriority[whichUnit] <= 0.0) then
        set overtake = true
    else
        if (LockPriority[whichUnit] <= 1.0) then
            if LockPriority[whichUnit] < priority then
                set overtake = true
            elseif LockPriority[whichUnit] == priority then
                set overtake = overtakeIfSame
            endif
        endif
    endif

    if overtake then
        set lockCounter = lockCounter + 1
        set LockId[whichUnit] = lockCounter
        set LockPriority[whichUnit] = priority
        return lockCounter
    else
        return 0
    endif
endfunction

//===========================================================================
//  Use this inside a spell loop to check if your spell stil has lock on the unit.
//===========================================================================
public function Get takes unit whichUnit returns integer
    return LockId[whichUnit]
endfunction

//===========================================================================
//  Spell instance can only release it's own lock.
//===========================================================================
public function Release takes unit whichUnit, integer lockId returns nothing
    if LockId[whichUnit] == lockId then
        set LockId[whichUnit] = 0
        set LockPriority[whichUnit] = 0.0
        call SetUnitFlyHeight(whichUnit, GetUnitDefaultFlyHeight(whichUnit), HEIGHT_RESTORATION_SPEED)
    endif
endfunction

//===========================================================================
private function BorderStop takes nothing returns nothing
    local unit whichUnit = GetTriggerUnit()
    set LockId[whichUnit] = 0
    set LockPriority[whichUnit] = 0.0
    call SetUnitFlyHeight(whichUnit, GetUnitDefaultFlyHeight(whichUnit), HEIGHT_RESTORATION_SPEED)
    set whichUnit = null
endfunction

//===========================================================================
private function Init takes nothing returns nothing
    local trigger trig = CreateTrigger()
    call TriggerRegisterLeaveRectSimple( trig, GetPlayableMapRect() )
    call TriggerAddAction( trig, function BorderStop )
endfunction

endlibrary


It might not look that way at first but this little script is actually a physics system. The biggest lol about this is that it is actually the most powerfull wc3 physics system ever created.

Try out the spells from demo map and be amazed :)
 

Attachments

  • UnitLock v1.0.w3x
    36.7 KB · Views: 279

Azlier

Old World Ghost
Reaction score
461
It's a vJass script. All it needs is a vJass preprocessor and PUI. Nothing else special.

Now THAT'S a good idea for a script!
 

Hatebreeder

So many apples
Reaction score
381
It would be cool, if the spells of same priority would nullify each other ^.^
Other than that, this seems VERY usefull to me.
+REP =)
EDIT: Damn, mus spread rep around before i can give you again =/
 

Prometheus

Everything is mutable; nothing is sacred
Reaction score
589
Pretty cool, but the levitate spell is kinda jumpy. Then again, this isn't a spell pack. :D

Edit: Typo?

JASS:
//           * [normal] (0.0 < priority <= 1.0)


Should be...

JASS:
//           * [normal] (0.0 > priority <= 1.0)
 

Cohadar

master of fugue
Reaction score
209
What happens if I overtake units 2^31-1 times? Will LockCounter overflow then?

LoL, no it won't.
Negative id's are also valid, the only invalid one is zero so if by any chance you manage to waste all 4294967295 available id's one of your spells will fail and than it will continue normally.

Of course by the time you finish that epic game our galaxy will collapse into a black hole, so in order to use this system properly you need to be on a spaceship located beyond the time-horizon and flying the hell out of what used to be Milky Way.

Fixed:
JASS:

library UnitLock initializer Init uses PUI, FTLDrive
 

emjlr3

Change can be a good thing
Reaction score
395
why do you reset units flyheight?
 

Cohadar

master of fugue
Reaction score
209
why do you reset units flyheight?

Well ground units would be hovering in air for no reason if I didn't and air units would be flying on bad height.

You can thing of it as "gravitation" for ground units and for air units it is simply default fly height.
 

emjlr3

Change can be a good thing
Reaction score
395
there is no documentation anywhere that suggests this would be used for fly height manipulating spells (which there are not many of in the first place) - I figured it would be more for buffs/debuffs, knockbacks, etc.
 

SerraAvenger

Cuz I can
Reaction score
234
Demo Map > Documentation

Only if there's a docu : )
Perhaps I want to see what It does but don't want to test out the demo map? Perhaps I can't?
Also you might want to catch the readers interest before he actually WANTS to see the demo map ; )
 

BRUTAL

I'm working
Reaction score
118
this will work with like, regular warcraft spells also correct? like if you make the trigger for when it is used
also, if i want to like, stop all spells from being casted while a hero is already in effect of a spell (lets say a slide spell) would that mean id have to use the event for all my spells? and doesnt that event serve to be glitchy ><
 
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