Assist System

AoW_Hun7312

I'm a magic man, I've got magic hands.
Reaction score
76
I've currently been working on an assist system for my map, but I'm fairly certain it can be improved. Does anyone see anything that would help its performance?

JASS:
// Created by AoW_Hun7312
library ASYST initializer Init requires AIDS, Damage, TimerUtils

    globals
        // Configurable Settings
        // The gold gained from a kill
        private constant integer Bounty = 250
        // The gold to split among assisters
        private constant integer AssistBounty = 50
        // The time before assists are no longer valid.
        private constant real DecayTime = 10.
        // The duration of the messages
        private constant real MessageDuration  = 15.
        // Messages displayed when killed
        private constant string KillMessage       = " killed "
        private constant string AssistMessage     = "With help from: "
        private constant string BountyMessage     = " gold!"
        private constant string SplitMessage      = " gold is split!"
        
        // Don't touch below here!
        private force array AssistGroup
        private timer array AssistTimer
        private group RegisteredGroup = CreateGroup()
        private player killedp = null
        private player killerp = null
        private trigger UnitDamaged = CreateTrigger()
        private trigger UnitKilled = CreateTrigger()
        private unit damaged = null
        private unit damager = null
        private unit killed = null
        private unit killer = null
    endglobals
    
    function ASYST_Register takes unit whichUnit returns nothing
        set AssistGroup[GetUnitId(whichUnit)] = CreateForce()
        call GroupAddUnit(RegisteredGroup, whichUnit)
    endfunction
    
    private function AssistDecay takes nothing returns nothing
        local integer index = GetTimerData(GetExpiredTimer())
        call ForceClear(AssistGroup[index])
        call ReleaseTimer(AssistTimer[index])
        set AssistTimer[index] = null
    endfunction
    
    private function Damaged takes nothing returns boolean
        local integer index = 0
        set damaged = GetTriggerUnit()
        if (IsUnitInGroup(damaged, RegisteredGroup)) then
            set damager = GetEventDamageSource()
            set index = GetUnitId(damaged)
            if (AssistTimer[index] == null) then
                set AssistTimer[index] = NewTimer()
                call ForceAddPlayer(AssistGroup[index], GetOwningPlayer(damager))
                call SetTimerData(AssistTimer[index], index)
                call TimerStart(AssistTimer[index], DecayTime, false, function AssistDecay)
            else
                call ForceAddPlayer(AssistGroup[index], GetOwningPlayer(damager))
                call PauseTimer(AssistTimer[index])
                call TimerStart(AssistTimer[index], DecayTime, false, function AssistDecay) 
            endif
        endif
        return false
    endfunction
    
    private function Killed takes nothing returns boolean
        local integer counter = 0
        local integer index = 0
        local integer gold = AssistBounty
        local string assist = ""
        set killed = GetTriggerUnit()
        if (IsUnitInGroup(killed, RegisteredGroup)) then
            set killer = GetKillingUnit()
            if not (killed == killer) then
                set index = GetUnitId(killed)
                set gold = gold / CountPlayersInForceBJ(AssistGroup[index])
                set killedp = GetOwningPlayer(killed)
                set killerp = GetOwningPlayer(killer)
                loop
                    exitwhen counter > 11
                    if (IsPlayerInForce(Player(counter), AssistGroup[index]) and (Player(counter) != killerp)) then
                        call SetPlayerState(Player(counter), ConvertPlayerState(1), GetPlayerState(Player(counter), ConvertPlayerState(1)) + gold)
                        set assist = assist + GetPlayerName(Player(counter)) + ", "
                    endif
                    set counter = counter + 1
                endloop
            endif
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, MessageDuration, GetPlayerName(killerp) + KillMessage + GetPlayerName(killedp) + " for " + I2S(Bounty) + BountyMessage)
            if (assist != "") then
                call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, MessageDuration, AssistMessage + assist + I2S(gold) + SplitMessage)
            endif
            call ForceClear(AssistGroup[index])
        endif
        return false
    endfunction
    
    private function Init takes nothing returns nothing
        call Damage_RegisterEvent(UnitDamaged)
        call TriggerRegisterAnyUnitEventBJ(UnitKilled, EVENT_PLAYER_UNIT_DEATH)
        call TriggerAddCondition(UnitDamaged, function Damaged)
        call TriggerAddCondition(UnitKilled, function Killed)
    endfunction
    
endlibrary
 

Laiev

Hey Listen!!
Reaction score
188
JASS:
function ASYST_Register takes unit whichUnit returns nothing


can be

JASS:
public function Register takes unit whichUnit returns nothing


to call it will stay the same...

JASS:
ASYST_Register (whichUnit)


also i think you could use struct
 

AoW_Hun7312

I'm a magic man, I've got magic hands.
Reaction score
76
JASS:
function ASYST_Register takes unit whichUnit returns nothing


can be

JASS:
public function Register takes unit whichUnit returns nothing


to call it will stay the same...

JASS:
ASYST_Register (whichUnit)


also i think you could use struct

Doing this would make it error when you call it with GUI (at least in my experience.)
 

Laiev

Hey Listen!!
Reaction score
188
@AoW

...?

and why you want to use it with GUI if you're making it with vJass?
 

Hatebreeder

So many apples
Reaction score
381
I thought I saw local units somewhere o_O

I shouldn't comment when I'm up late =_=
My bad.
 

Bribe

vJass errors are legion
Reaction score
67
JASS:
call TriggerRegisterUnitEvent(UnitKilled, whichUnit, EVENT_UNIT_DEATH)


What advantage does this have over [ljass]call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)[/ljass] and a very, very simple boolean to check if the unit is a valid unit? Infinite fewer handles == undeniable.
 

Bribe

vJass errors are legion
Reaction score
67
[lJASS]ConvertPlayerState(1)[/lJASS] is slower than the built-in variable set to it;



If you prefer something of a shorter name, you can do:

JASS:
private constant playerstate GOLD_COUNT = PLAYER_STATE_RESOURCE_GOLD


The following block, you don't need to pause the timer before starting it again. When you start it again, everything the timer had going for it before it canceled. Sort of how every time you use GroupEnumUnitsIn..., the group gets cleared before adding those units.

JASS:
//
            if (AssistTimer[index] == null) then
                set AssistTimer[index] = NewTimer()
                call ForceAddPlayer(AssistGroup[index], GetOwningPlayer(damager))
                call SetTimerData(AssistTimer[index], index)
                call TimerStart(AssistTimer[index], DecayTime, false, function AssistDecay)
            else
                call ForceAddPlayer(AssistGroup[index], GetOwningPlayer(damager))
                call PauseTimer(AssistTimer[index]) // useless
                call TimerStart(AssistTimer[index], DecayTime, false, function AssistDecay) 
            endif


The following block does the exact same thing, but with less text. Whenever you have the opportunity to recycle text, I recommend you take it. Smaller map file size = smaller download size. For that matter, you should also get Vexorian's Map Optimizer, as it strips out all of the comments from the war3map.j file (don't worry, they will be there again next time you open world editor).

JASS:
//
    if (AssistTimer[index] == null) then
        set AssistTimer[index] = NewTimer()
        call SetTimerData(AssistTimer[index], index)
    endif
    call ForceAddPlayer(AssistGroup[index], GetOwningPlayer(damager))
    call TimerStart(AssistTimer[index], DecayTime, false, function AssistDecay)
 

AoW_Hun7312

I'm a magic man, I've got magic hands.
Reaction score
76
Changed as well. Thanks for that tip, I had to change a few other triggers I set up that way.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      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