System AutoMover

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:

library AutoMover requires optional Table, optional ABC
/*
      ___ __  _ ______  _____ _____  _ ___    _  ____  ___
     /   | | | |__  __|/  _  \ _  _ \/ _  \  / / ___//   /
    / == | | | |  ||   | | | || || || |\| | / / /__ / * /
   /  == | |_| |  ||   | |_| || || || |_| |/ / /__ / /\ \
  /__/ |_|\___/   ||   \_____/| || ||\____/_/____//_/  \_\
  
  by kingking   ~ v1.0.0 ~
  
    What is AutoMover?
    ~ AutoMover is an automated creeps ordering system.
    ~ AutoMove is useful for TD(aka tower defense), AoS style maps.
    
    Functions :
    ~ AutoMover_Region.create(rect r) -> AutoMover_Region
    -> Return an instance.
    
    ~ set .enable = true/false
    -> Enable/Disable. (Units will be stayed in the region if it is disabled.)
    
    ~ .isEnabled() -> boolean
    -> Is enabled?
    
    ~ .addToPrev(AutoMove_Region)
    -> Add region to the previous of current instance.
    
    ~ .addToNext(AutoMove_Region)
    -> Add region to the next of current instance.
    
    ~ .removeNext()
    -> Remove all regions after next.
    
    ~ .removePrev()
    -> Remove all regions at previous.
    
    ~ .unlink()
    -> Remove all regions at previous and next.
    
    ~ .destroy()
    -> Destroy the instance.
    
    ~ AutoMover_Region.getTriggering() -> AutoMover_Region
    -> Returns triggering instance.
    
    function interface ToPrev takes unit whichUnit returns boolean
    -> Condition for units to move to previous region.
    
    function interface ToNext takes unit whichUnit returns boolean
    -> Condition for units to move to next region.
    
    function interface OnAutoMove takes unit whichUnit returns nothing
    -> Callback when a units is ordered.
    
    ~ set .toPrevFilter = func
    
    ~ set .toNextFilter = func
    
    ~ set .endCallback = func
        
        
    Concept :
    this -> this.next -> this.next.next
    
    this.prev.prev <- this.prev <- this
    
    Details :
    ~ AutoMover is basically a linked list.
    ~ AutoMover will order units automatically once you have
      setup correctly.
      
    Requires :
    ~ Table/ABC
    ~ Jasshelper 0.A.2.B or later
    
    How to import :
    ~ Copy this trigger to your map.
    ~ Copy ABC/Table to your map if you don't have.
    ~ Define the regions.
    
*/
    globals
        private constant string OnMoveOrder = "move"
        /* "move" or "attack" or "smart" */
    endglobals
    
    static if LIBRARY_Table then
        globals
            private HandleTable AutoMoverTable
        endglobals
    endif
    
    function interface ToPrev takes unit whichUnit returns boolean
    function interface ToNext takes unit whichUnit returns boolean
    function interface OnAutoMove takes unit whichUnit returns nothing
    
    public struct Region
        private static conditionfunc cf
        private static thistype array Stack
        private static integer StackLevel = 0
        private thistype prev
        private thistype next
        readonly real x
        readonly real y
        private region reg
        private trigger trig
        ToPrev toPrevFilter
        ToNext toNextFilter
        OnAutoMove endCallback
        
        static method getTriggering takes nothing returns thistype
            return .Stack[.StackLevel]
        endmethod
        
        private static method Cond takes nothing returns boolean
            local unit u = GetTriggerUnit()
            static if LIBRARY_Table then
                local thistype this = AutoMoverTable[GetTriggeringTrigger()]
            elseif LIBRARY_ABC then
                local thistype this = GetTriggerStructA(GetTriggeringTrigger())
            endif
            set thistype.StackLevel = thistype.StackLevel + 1
            set thistype.Stack[thistype.StackLevel] = this
            if .toPrevFilter != 0 and .toPrevFilter.evaluate(u) then
                call IssuePointOrder(u,OnMoveOrder,.prev.x,.prev.y)
                if .endCallback != 0 then
                    call .endCallback.execute(u)
                endif
            endif
            if .toNextFilter != 0 and .toNextFilter.evaluate(u) then
                call IssuePointOrder(u,OnMoveOrder,.next.x,.next.y)
                if .endCallback != 0 then
                    call .endCallback.execute(u)
                endif
            endif
            set thistype.StackLevel = thistype.StackLevel - 1
            set u = null
            return false
        endmethod
        
        static method create takes rect r returns thistype
            local thistype this
            if r == null then
                return 0
            endif
            set this = .allocate()
            set .prev = this
            set .next = this
            set .x = GetRectCenterX(r)
            set .y = GetRectCenterY(r)
            set .trig = CreateTrigger()
            set .reg = CreateRegion()
            call RegionAddRect(.reg,r)
            call TriggerRegisterEnterRegion(.trig,.reg,null)
            call TriggerAddCondition(.trig,thistype.cf)
            
            static if LIBRARY_Table then
                set AutoMoverTable[.trig] = this
            elseif LIBRARY_ABC then
                call SetTriggerStructA(.trig,this)
            endif
            
            return this
        endmethod
        
        method operator enable= takes boolean flag returns nothing
            if flag then
                call EnableTrigger(.trig)
            else
                call DisableTrigger(.trig)
            endif
        endmethod
        
        method operator isEnabled takes nothing returns boolean
            return IsTriggerEnabled(.trig)
        endmethod
        
        method addToPrev takes thistype this2 returns nothing
            set this.prev = this2
            set this2.next = this
        endmethod
        
        method addToNext takes thistype this2 returns nothing
            set this.next = this2
            set this2.prev = this
        endmethod
        
        method removeNext takes nothing returns nothing
            set this.next.prev = this.next
            set this.next = this
        endmethod
        
        method removePrev takes nothing returns nothing
            set this.prev.next = this.prev
            set this.prev = this
        endmethod

        method unlink takes nothing returns nothing
            if .prev == this then
                set .next.prev = .next
            else
                set .next.prev = .prev
            endif
            
            if .next == this then
                set .prev.next = .prev
            else
                set .prev.next = .next
            endif
        endmethod
        
        method onDestroy takes nothing returns nothing
            call .unlink()
            static if LIBRARY_Table then
                call AutoMoverTable.flush(.trig)
            elseif LIBRARY_ABC then
                call ClearTriggerStructA(.trig)
            endif
            call TriggerClearConditions(.trig)
            call DisableTrigger(.trig)
            call DestroyTrigger(.trig)
            call RemoveRegion(.reg)
            set .trig = null
            set .reg = null
            set .toPrevFilter = 0
            set .toNextFilter = 0
            set .endCallback = 0
        endmethod
        
        private static method onInit takes nothing returns nothing
            static if LIBRARY_Table then
                set AutoMoverTable = HandleTable.create()
            elseif not LIBRARY_ABC then
                call BJDebugMsg("AutoMover Error : Table/ABC is not found in this map!")
            endif
            set thistype.cf = Condition(function thistype.Cond)
        endmethod
        
    endstruct
    
endlibrary


Table | ABC
 

Attachments

  • AutoMover.w3x
    29.3 KB · Views: 306
First of, please make your interfaces private.
Secondly, how is this better than this?
third, since you are not able to create a whole way and add units to it so they automatically move that way, this isn't good in any way. (lol)
Also, some creeps in TDs might cast spells and such, so a reordering system (such as in the provided link) is necessary.
 
First of, please make your interfaces private.
:confused:

Secondly, how is this better than this?
This 1 is more efficient and much LIGHTER.

third, since you are not able to create a whole way and add units to it so they automatically move that way, this isn't good in any way. (lol)
Have you looked it carefully? :nuts: I used linked list to achieve it.

Also, some creeps in TDs might cast spells and such, so a reordering system (such as in the provided link) is necessary.
It is easy by porting LastOrder into it. :cool:

Btw, I need more suggestions to improve it. :thup:
 
why do people feel the documentation is not as important as the code itself...
 
This is nice =)

Any way to achieve the Order once a unit "forgets" it?
Ex. You use a stun ability on a creep and move out of sight. That creep usually doesn't start doing it's previously ordered Order. It just stands there.
 
Ex. You use a stun ability on a creep and move out of sight. That creep usually doesn't start doing it's previously ordered Order. It just stands there.
I will port LastOrder into it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • 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 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