• 🏆 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!

[Solved] Need help with some math (distributing gold among players)

Status
Not open for further replies.

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,534
I need a system that takes a set amount of gold and distributes it among players. However, I want the gold to be distributed based on each Player's total kills.

Players with less kills should receive more gold, the idea being that the "poor" receive more than the "rich".

Example:
Player 1 uses the "Helping Hand" ability, spending 1000 gold to do so
This 1000 gold is then divided among his 3 allies (P2, P3, and P4)
Each of these players receives a portion of the 1000 gold based on their kills
Say P2 has 5 kills, P3 has 10 kills, and P4 has 15 kills
P2 receives the most gold, P3 receives the 2nd most gold, and P4 receives the least gold

I have some ideas myself but they seem like over complicated solutions, and I imagine there's a much easier way of going about this.

Any ideas?
 
Last edited:
First find the highest killcount of all players and store that to a variable.
Then divide the killcount of each player by the stored highest killcount.
This is the handicap value.
Now reverse the logic by subtracting the handicap value from 1, so that someone who only achieved 5% of the kills of the best player gets 95% of the payout whereas someone who killed half as much as the leading player gets 50% payout.
Sum up all payout percentages then divide 1000gold through the total of payout percentage.
Then apply that gold amount.


Heres the algorithm in pseudocode:

Loop through Killcount[player] and find the highest

store highest killcount to Killcount_of_leader

(1 - killcount[player]/killcount_of_leader) = handicap[player]

Total_handicap = handicap[player 1] + handicap[player 2] + ...

1000/total_handicap * handicap[player] = payout



Note that this algorithm awards the leading player no payout at all.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,534
First find the highest killcount of all players and store that to a variable.
Then divide the killcount of each player by the stored highest killcount.
This is the handicap value.
Now reverse the logic by subtracting the handicap value from 1, so that someone who only achieved 5% of the kills of the best player gets 95% of the payout whereas someone who killed half as much as the leading player gets 50% payout.
Sum up all payout percentages then divide 1000gold through the total of payout percentage.
Then apply that gold amount.


Heres the algorithm in pseudocode:

Loop through Killcount[player] and find the highest

store highest killcount to Killcount_of_leader

(1 - killcount[player]/killcount_of_leader) = handicap[player]

Total_handicap = handicap[player 1] + handicap[player 2] + ...

1000/total_handicap * handicap[player] = payout



Note that this algorithm awards the leading player no payout at all.
Awarding the leading player no payout is not ideal, but if all else fails i'll definitely use this. Thanks for the help.
 
PlayerGold = MAX_GOLD / (TOTAL_WEIGHT / PlayerWeight )
PlayerWeight = MAX_PLAYER_KILLS / PlayerKills

===

So in the example:

MAX_PLAYER_KILLS = 15
MAX_GOLD = 1000

Player 2 weight: 15 / 5 = 3
Player 3 weight: 15 / 10 = 1.5
Player 4 weight: 15 / 15 = 1

TOTAL_WEIGHT: 5.5

(the idea is the player with most kills has weight "1" and other players get a multiple of it)

Player 2 gold: 1000 / (5.5/3) = 545.5
Player 3 gold: 1000 / (5.5/1.5) = 272.7
Player 4 gold: 1000 / (5.5/1) = 181.8

so Player2 has 1/3 of Player4's kills, and gets triple of his gold
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,534
PlayerGold = MAX_GOLD / (TOTAL_WEIGHT / PlayerWeight )
PlayerWeight = MAX_PLAYER_KILLS / PlayerKills

===

So in the example:

MAX_PLAYER_KILLS = 15
MAX_GOLD = 1000

Player 2 weight: 15 / 5 = 3
Player 3 weight: 15 / 10 = 1.5
Player 4 weight: 15 / 15 = 1

TOTAL_WEIGHT: 5.5

(the idea is the player with most kills has weight "1" and other players get a multiple of it)

Player 2 gold: 1000 / (5.5/3) = 545.5
Player 3 gold: 1000 / (5.5/1.5) = 272.7
Player 4 gold: 1000 / (5.5/1) = 181.8

so Player2 has 1/3 of Player4's kills, and gets triple of his gold
Awesome, that works really well. The only issue I ran into was that it would fail if Players had 0 kills. But to combat this I put in a minimum value, so players with < 10 kills are treated as though they have 10 kills.
  • Helping Hand
    • Events
      • Unit - A unit Finishes research
    • Conditions
    • Actions
      • Set VariableSet hh_MAX_GOLD = 2500
      • -------- --------
      • -------- Add Players To Force --------
      • Set VariableSet hh_TempForce = (All players matching ((((Matching player) controller) Equal to User) and (((Matching player) slot status) Equal to Is playing)).)
      • Player Group - Remove (Owner of (Triggering unit)) from hh_TempForce.
      • -------- --------
      • -------- Get Max Player Kills --------
      • Set VariableSet hh_MAX_PLAYER_KILLS = 0
      • Player Group - Pick every player in hh_TempForce and do (Actions)
        • Loop - Actions
          • Set VariableSet PN = (Player number of (Picked player))
          • -------- --------
          • Set VariableSet hh_Kills[PN] = Kills[PN]
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • hh_Kills[PN] Less than 10
            • Then - Actions
              • -------- This guarantees that a player will be treated as though they have AT LEAST 10 kills (Needed for the formula to work) --------
              • Set VariableSet hh_Kills[PN] = 10
            • Else - Actions
          • -------- --------
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • hh_Kills[PN] Greater than hh_MAX_PLAYER_KILLS
            • Then - Actions
              • Set VariableSet hh_MAX_PLAYER_KILLS = hh_Kills[PN]
            • Else - Actions
      • -------- --------
      • -------- Get Player Weight / Total Weight --------
      • Set VariableSet hh_TOTAL_WEIGHT = 0.00
      • Player Group - Pick every player in hh_TempForce and do (Actions)
        • Loop - Actions
          • Set VariableSet PN = (Player number of (Picked player))
          • Set VariableSet hh_PlayerWeight[PN] = ((Real(hh_MAX_PLAYER_KILLS)) / (Real(hh_Kills[PN])))
          • Set VariableSet hh_TOTAL_WEIGHT = (hh_TOTAL_WEIGHT + hh_PlayerWeight[PN])
      • -------- --------
      • -------- Add Gold --------
      • Player Group - Pick every player in hh_TempForce and do (Actions)
        • Loop - Actions
          • Set VariableSet PN = (Player number of (Picked player))
          • Set VariableSet TempReal = ((Real(hh_MAX_GOLD)) / (hh_TOTAL_WEIGHT / hh_PlayerWeight[PN]))
          • Player - Add (Integer(TempReal)) to (Picked player).Current gold
          • -------- --------
          • -------- Message --------
          • Set VariableSet TempPG = (Player group((Picked player)))
          • Game - Display to TempPG for 10.00 seconds the text: ((Name of (Owner of (Triggering unit))) + ( just lent a helping hand with + ((|cffffff00 + ((String((Integer(TempReal)))) + |r)) + gold!)))
          • Custom script: call DestroyForce (udg_TempPG)
      • -------- --------
      • Custom script: call DestroyForce (udg_hh_TempForce)
 
Status
Not open for further replies.
Top