HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Track Unit Creation and Kills for each Player

01-24-2011, 06:55 PM#1
PsycoMarauder
Ok, I've tried doing this using a few different methods yet none worked the way I wanted it to. Essentially what I'm trying to do is record # of each type of unit trained by a player, as well as the # of each type of unit killed by said player, and IF possible record the # of each type of unit killed by the killing player for specific unit types. Hopefully that wasn't too confusing. I can't come up with an efficient way to track this that doesn't cause a ton of lag. I run probably one of the best organized wc3 leagues at risknextgendotcom and this data would be interesting to display for a players stats. The info is sent through hostbots via mmd. If anyone can help me write this trigger or if anyone is up to the task of writing it it would be extremely appreciated.
01-24-2011, 07:16 PM#2
Anitarf
Quote:
record the # of each type of unit killed by the killing player of specific unit types.
You lost me here, could you explain what you mean by this in more detail? The rest is easy.
01-24-2011, 07:38 PM#3
PsycoMarauder
Alright, so for example if one of player 1's knights killed player 2's priest. For Player 1's "Knight Kill Data," the Priest Value would increase by 1.
01-24-2011, 08:54 PM#4
Anitarf
Ah, I understand now. Yes, that is doable.

I would use Table to store all the data.

Collapse JASS:
library UnitTypeStatistics requires Table

    private struct PlayerData
        player p

        Table trained // This stores the number of units the player trained for each unit type.
        Table killed // This stores the number of units the player killed for each unit type.
        Table killTables // This stores a kills table for each unit type.

        static PlayerData array data // This links PlayerData structs to player ids.

        static method create takes player p returns PlayerData
            // This gets automatically called from the onInit method for every player playing.
            local PlayerData this = PlayerData.allocate()
            set data[GetPlayerId(p)] = this
            set .trained = Table.create()
            set .killed = Table.create()
            set .killTables = Table.create()
            set .p = p
            return this
        endmethod

        static method onTrain takes nothing returns nothing
            local unit u = GetTriggerUnit()
            local integer uid = GetUnitTypeId(u)
            local PlayerData this = .data[GetPlayerId(GetOwningPlayer(u))]

            set this.trained[uid] = this.trained[uid] + 1

            set u = null
        endmethod

        static method onDeath takes nothing returns nothing
            local unit k = GetKillingUnit()
            local unit u = GetTriggerUnit()
            local integer kid = GetUnitTypeId(k)
            local integer uid = GetUnitTypeId(u)
            local PlayerData this = .data[GetPlayerId(GetOwningPlayer(k))]
            local PlayerData that = .data[GetPlayerId(GetOwningPlayer(u))]
            local Table tab =  Table( this.killTables[kid] )

            set this.killed[uid] = this.killed[uid] + 1

            if tab == 0 then
                set tab = Table.create()
                set this.killTables[kid] = integer( tab )
            endif
            set tab[uid] = tab[uid] + 1

            set u = null
            set k = null
        endmethod

        static method onInit takes nothing returns nothing
            local trigger t
            local integer i = 0
            loop
                call PlayerData.create(Player(i)) // You should probably first check if the player is in the game before calling this line.
                set i = i + 1
                exitwhen i > 11
            endloop

            set t=CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_TRAIN_FINISH )
            call TriggerAddAction( t, function PlayerData.onTrain)

            set t=CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_DEATH )
            call TriggerAddAction( t, function PlayerData.onDeath)
        endmethod

    endstruct



    function GetUnitsTrained takes player p, integer unitID returns integer
        return PlayerData.data[GetPlayerId(p)].trained[unitID]
    endfunction

    function GetUnitsKilled takes player p, integer unitID returns integer
        return PlayerData.data[GetPlayerId(p)].killed[unitID]
    endfunction

    function GetUnitsKilledByUnitType takes player p, integer unitID, integer killerID returns integer
        local Table tab =  Table( PlayerData.data[GetPlayerId(p)].killTables[killerID] )
        if tab == 0 then
            return 0
        endif
        return tab[unitID]
    endfunction

endlibrary