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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      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