Attachment System Challenge - KillCounter

Cohadar

master of fugue
Reaction score
209
Task:
Make a KillCounter library that will attach a Counter variable to all units on the map and will keep track of how many units some unit killed.

Conditions:
1. All your code must be in one library. (except the system you are using)
2. Monopolizing UnitUserData is not allowed.
3. No using of Object editor is allowed, you must use some attaching system to do the job.
4. The only event you are allowed to use is EVENT_PLAYER_UNIT_DEATH

Reward:
Rep and some <3 from me. (the better solution the more love)

PS: Once a first successful solution is found for one attachment system no further solutions will be considered - fastest one wins.

Winners: (in chronological order)
Flare <3 - Ugly solution with group and a timer, but at least it works
TheDamien <3 - Solid solution with a timer but still not usable for serious mapping.
TheDamien-CSData <3<3<3 - first usable solution that uses one standard system, CSData + group trick.
(Group trick can be used with almost all attaching systems.)
Grim001 <3 - an inlined 0x100000 with a group trick.(nice testmap)
Vexorian <3 - for his time and ideas, no map since it basically comes down to other group tricks.
Out of competition:
Cohadar-PUI - PUI + Pivot array.
 

Attachments

  • UATest-Flare.w3x
    11.5 KB · Views: 240
  • UATest-TheDamien.w3x
    11.1 KB · Views: 236
  • UATest-CSData.w3x
    15.4 KB · Views: 263
  • UATest-grim001.w3m
    17.3 KB · Views: 245
  • UATest-PUI.w3x
    16.7 KB · Views: 225

Cohadar

master of fugue
Reaction score
209
Everyone can try their luck with this, after all it sounds like a simple task does it not?

Unit's kill counter does not reset when unit dies.

The purpose of this contest is simple: to test what system is best to use for unit attaching.
 

Cohadar

master of fugue
Reaction score
209
It doesn't.
Will explain in detail when you post the solution, no need to talk for nothing now.
 
Reaction score
456
JASS:
library KillCounter initializer Init

    globals
        private unit array units
        private integer array counters
        private boolean array initialized
    endglobals
    
    public function H2I takes handle h returns integer
        return h
        return 0
    endfunction

    private function IncreaseUnitKills takes nothing returns boolean
        local unit u = GetKillingUnit()
        local integer id = H2I(u) - 0x100000
        
        if u == null then
            return false
        endif
        
        if initialized[id] == false or units[id] != u  then
            set units[id] = u
            set counters[id] = 0
            set initialized[id] = true
        endif
        
        set counters[id] = counters[id] + 1
        
        debug call BJDebugMsg(GetUnitName(u) + &quot; has killed &quot; + I2S(counters[id]) + &quot; units.&quot;)
        
        set u = null
        return false
    endfunction

    private function Init takes nothing returns nothing
        local trigger trig = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(trig, EVENT_PLAYER_UNIT_DEATH)
        call TriggerAddCondition(trig, Condition(function IncreaseUnitKills))
    endfunction
    
endlibrary

Ah okay.. not then.. that worked well in my tests. But I guess it doesn't work then o_O.

Edit:// Oh this might not need the initialized variable at all. And maybe even better if I give more index space for the units variable?
JASS:
library KillCounter initializer Init

    globals
        private unit array units
        private integer array counters
    endglobals
    
    public function H2I takes handle h returns integer
        return h
        return 0
    endfunction

    private function IncreaseUnitKills takes nothing returns boolean
        local unit u = GetKillingUnit()
        local integer id = H2I(u) - 0x100000
        
        if u == null then
            return false
        endif
        
        if units[id] != u then
            set units[id] = u
            set counters[id] = 0
        endif
        
        set counters[id] = counters[id] + 1
        
        debug call BJDebugMsg(GetUnitName(u) + &quot; has killed &quot; + I2S(counters[id]) + &quot; units.&quot;)
        
        set u = null
        return false
    endfunction

    private function Init takes nothing returns nothing
        local trigger trig = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(trig, EVENT_PLAYER_UNIT_DEATH)
        call TriggerAddCondition(trig, Condition(function IncreaseUnitKills))
    endfunction
    
endlibrary
 

Flare

Stops copies me!
Reaction score
662
JASS:
library KC initializer InitFunc

globals
    private constant real CheckTime = 5
    private constant boolean SHOW_DEBUG_MSG = true
    private unit array Units
    private integer array Kills
    private integer C = 0
    private group G = CreateGroup ()
    private timer T = CreateTimer ()
endglobals


function GetKills takes unit whichUnit returns integer
    local integer i = C
    loop
    exitwhen i &lt;= 0
        if Units<i> == whichUnit then
            if SHOW_DEBUG_MSG then
                call BJDebugMsg (&quot;This unit has &quot; + I2S (Kills<i>) + &quot; kills&quot;)
            endif
            return Kills<i>
        endif
        set i = i - 1
    endloop
    if SHOW_DEBUG_MSG then
        call BJDebugMsg (&quot;This unit hasn&#039;t killed any units yet&quot;)
    endif
    return 0
endfunction



private function CheckerFunc takes nothing returns nothing
    local integer i = C
    local boolean exit = false
    loop
    exitwhen i &lt;= 0 or exit
        if IsUnitInGroup (Units<i>, G) == false then
            set Units<i> = Units[C]
            set Kills<i> = Kills[C]
            set C = C - 1
        endif
        set i = i - 1
    endloop
endfunction


private function DeathFunc takes nothing returns nothing
    local integer i = C
    local boolean exit = false
    local unit k = GetKillingUnit ()
    loop
    exitwhen i &lt;= 0 or exit
        if k == Units<i> then
            set Kills<i>  = Kills<i> + 1
            set exit = true
        endif
        set i = i - 1
    endloop
    
    if exit == false then
        set C = C + 1
        set Units[C] = k
        set Kills[C] = 1
        call GroupAddUnit (G, k)
    endif
    
    set k = null
endfunction



private function SafeFilter takes nothing returns boolean
    return true
endfunction

private function InitFunc takes nothing returns nothing
    local trigger t = CreateTrigger ()
    local integer i = 0
    loop
    exitwhen i &gt; 15
        call TriggerRegisterPlayerUnitEvent (t, Player (i), EVENT_PLAYER_UNIT_DEATH, Condition (function SafeFilter))
        set i = i + 1
    endloop
    call TriggerAddAction (t, function DeathFunc)
    call TimerStart (T, CheckTime, true, function CheckerFunc)
endfunction

endlibrary</i></i></i></i></i></i></i></i></i>


And my lazy-ass GUI test triggers :)
Code:
Selection
    Events
        Player - Player 1 (Red) Selects a unit
    Conditions
    Actions
        Set Selected = (Triggering unit)


-------------------------------

Get Kills
    Events
        Player - Player 1 (Red) skips a cinematic sequence
    Conditions
    Actions
        Custom script:   set udg_Kills = GetKills (udg_Selected)


-------------------------------

Up
    Events
        Player - Player 1 (Red) Presses the Up Arrow key
    Conditions
    Actions
        Unit - Remove Selected from the game

Seems to work for me. I'll upload a test map shortly if you want, since it's currently in my contest spell map :p

EDIT: The in existence check seems to work too
you must use some attaching system to do the job.
:( Is it an absolute must that an attachment system is used? Or does array looping count as a system :rolloeyes:

The only event you are allowed to use is EVENT_PLAYER_UNIT_DEATH

Am I still allowed to use the timer to check if a unit no longer exists, so I can recycle unused arrays?
 
Reaction score
333
Here is mine:

JASS:
library KillCount initializer Init
    globals
        private constant real PERIOD = 5.
        private unit array Units
        private integer array Data
        
        private integer N = -1
        private integer array S
    endglobals
    
    private function H2I takes handle h returns integer
        return h
        return 0
    endfunction
    
    private function SetUnitKills takes unit u, integer i returns nothing
        set Units[H2I(u)-0x100000] = u
        set Data[H2I(u)-0x100000] = i
    endfunction
    
    private function GetUnitKills takes unit u returns integer
        return Data[H2I(u)-0x100000]
    endfunction
    
    private function Callback takes nothing returns nothing
        local integer i = N
        loop
            exitwhen i &lt; 0
            if (GetUnitTypeId(Units[S<i>]) == 0) then
                set Units[S<i>] = null
                set Data[S<i>] = 0
                set S<i> = S[N]
                set N = N-1
            endif
            set i = i-1
        endloop
    endfunction
    
    private function UnitDies takes nothing returns boolean
        local unit u = GetKillingUnit()
        local unit v = GetTriggerUnit()
        
        if (u != null and u != v) then
            call SetUnitKills(u, GetUnitKills(u)+1)
        endif
        
        if (GetUnitKills(v) &gt; 0) then
            set N = N+1
            set S[N] = H2I(v)-0x100000
        endif
        
        set u = null
        set v = null
        return false
    endfunction
    
    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
        call TriggerAddCondition(t, Condition(function UnitDies))
        
        call TimerStart(CreateTimer(), PERIOD, true, function Callback)
    endfunction
endlibrary</i></i></i></i>


The attachment system in this case is bundled with the library. Testing has been brief.
 

Cohadar

master of fugue
Reaction score
209
Flare was the first to submit correct solution - congrats.
(although I tested it wrong at first - sorry)
Uberplayer failed because he has perma leak in his code.
TheDamien was the second to submit correct solution - congrats.
Quraji - failed - permanently

Correct solutions will be attached to the first post, incorrect here.
 

Attachments

  • UATest-Uberplayer.w3x
    10.7 KB · Views: 255
  • UATest-Quraji.w3x
    11.3 KB · Views: 215

Flare

Stops copies me!
Reaction score
662
Uberplayer failed because he has perma leak in his code.
Test map attached below.

// checking others...

What permanent leak? I don't see it :p And you never actually pointed it out

UNLESS this
JASS:
//===========================================================================
//  How this works?
//  Well basically when test unit kills a hero it kills the test unit.
//  after that it revives hero and creates a NEW test unit on the same place.
//  Since unit handles are recycled (when there are no leaks) sooner or later
//  some attahing is going to happen to 2 different units with same handle.
//
//  If you see something like: 145 has killed 2 unit - you fail
//   * If second number is greater than one - you fail
//   * if first number constantly increases - you fail


Explains where the leak is
 

Cohadar

master of fugue
Reaction score
209
Turn THAT off and see if there is no leak.
(Accusing me of leaking I mean really...)

I don't know where YOUR leak is and I don't care, find your own errors.

I do have a working solution for this challenge. (just thought I mentioned it)

NOTE:
Leak was in Uberplayer's code not Flare's.
 
Reaction score
456
Don't tell us what it is before someone has done it correctly. A question, can we try again?
 

Cohadar

master of fugue
Reaction score
209
You can try again.
TheDamien has found a correct solution, but it is not the end.
Anyone else who finds a correct solution using some other system gets the rep.

I must notice that I find a lack of standard attaching systems here,
where are all those HSAS and HAIL and gamecache people when you need them...

I will attach all correct solutions to the first post.
 
Reaction score
456
Gosh I edit this post later.

No need to test. It worked fine with your recycle thing, because kills is always increased by 1. The condition isn't needed to give the same result. So it was a phail :p
 

Cohadar

master of fugue
Reaction score
209
Your methods are different - you don't use timer.

Flare's solution was OK (in the mathematical sense, not in technical) so I apologize.
 

Trollvottel

never aging title
Reaction score
262
:( i would have made some similar system like thedamien.... but newgen didnt work. and now it works and its too late...


my question is:
if some unit dies and is not a hero, dont have it's kills to be resetted? (not concerning thedamien's system but uberplayer's)
and to flare:
JASS:
exitwhen i &lt;= 0 or exit

exit is always false!?:nuts:
 

Cohadar

master of fugue
Reaction score
209
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top