• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Count Units in Transport Ship?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
How?

Found this:
  • Unit - A unit Is loaded into a transport
but not its counterpart.

Hmm, actually loaded units have the same position as when they were loaded even if the ship moves. That means one could detect when their position is changed.

Any other suggestions?

____________________________

Okey, I've created this library, feedback on it? How would you have done it?

Currently this would break if a unit carrying loaded units would die and then a new transport would take its place.

JASS:
library LoadLib initializer Init

    globals
        real                    eventUnload 
        private real array      x
        private real array      y
        private integer         tot = 0
        private integer array   inTransport
        private integer array   loadedCount
        private group array     loadedUnits
        private trigger         trgCheck = CreateTrigger()
    endglobals
    
    function GetLoadedUnits takes unit u returns group 
        return loadedUnits[GetUnitUserData(u)]
    endfunction
    
    function GetLoadedCount takes unit u returns integer 
        return loadedCount[GetUnitUserData(u)]
    endfunction
    
    
    private function Enum takes nothing returns nothing
        local unit u = GetEnumUnit()
        local integer i = GetUnitUserData(u)
        if x[i] != GetUnitX(u) or y[i] != GetUnitY(u) then
            call GroupRemoveUnit(loadedUnits[0], u)
            call GroupRemoveUnit(loadedUnits[inTransport[i]], u)
            set loadedCount[inTransport[i]] = loadedCount[inTransport[i]] - 1
            if loadedCount[inTransport[i]] == 0 then                            // Should be moved to deindex event
                call DestroyGroup(loadedUnits[inTransport[i]])
                set loadedUnits[inTransport[i]] = null
            endif
            set tot = tot - 1
            if tot == 0 then
                call DisableTrigger(trgCheck)
            endif
            set eventUnload = 0.
            set eventUnload = 1.
        endif
    endfunction
    
    private function CheckForUnload takes nothing returns boolean
        call ForGroup(loadedUnits[0], function Enum)
        return false 
    endfunction

    private function OnUnitLoad takes nothing returns boolean
        local unit loaded = GetTriggerUnit()
        local unit transport = GetTransportUnit()
        local integer l = GetUnitUserData(loaded)
        local integer t = GetUnitUserData(transport)
        set x[l] = GetUnitX(loaded)
        set y[l] = GetUnitY(loaded)
        set inTransport[l] = t 
        call GroupAddUnit(loadedUnits[0], loaded)
        if loadedUnits[t] == null then              // Should be moved to index event
            call BJDebugMsg("null")
            set loadedUnits[t] = CreateGroup()
        endif
        call GroupAddUnit(loadedUnits[t], loaded)
        set loadedCount[t] = loadedCount[t] + 1
        set tot = tot + 1
        if tot == 1 then
            call EnableTrigger(trgCheck)
        endif
        return false
    endfunction

    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
        set loadedUnits[0] = CreateGroup()
        loop
            call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_LOADED, null)
            set i = i + 1
            exitwhen i == bj_MAX_PLAYER_SLOTS
        endloop
        call TriggerRegisterTimerEvent(trgCheck, 0.1, true)
        call TriggerAddCondition(trgCheck, Condition(function CheckForUnload))
        call DisableTrigger(trgCheck)
        call TriggerAddCondition(t, Condition(function OnUnitLoad))
    endfunction
endlibrary
 
Last edited:
Status
Not open for further replies.
Top