Uncle
Warcraft Moderator
- Joined
- Aug 10, 2018
- Messages
- 7,609
I'm trying to come up with a function that will take a table (array) filled with units and split that into X parts where each part is as close to equal in unit-power as possible.
In other words, I have 4 footman, 3 rifleman, and 2 knights. Knights > Rifleman > Footman. I want to split those among X players as equally as possible.
The end results of dividing those units among 4 players would look something along the lines of this:
player 1 gets: 2 footman, 1 rifleman
player 2 gets: 1 knight
player 3 gets: 2 footman, 2 rifleman
player 4 gets: 1 knight
The order doesn't matter as long as things are close to equal.
Here's some function I found online that I modified to work how I wanted:
It returns two tables, set1 and set2, which each contain an "equal" power level of units which I can then distribute to the players.
UnitData[id].power is a stat I've given each Unit-Type in the game that determines their worth. A Knight would have much higher power than a Footman for example.
Unfortunately, this method among other things I've tried produces less than desirable results where one player ends up receiving way more power worth of units than everyone else.
I was hoping for a function that could manage splitting the table to 2, 3, or 4 players at a time with a bit more accuracy. It could look something like this:
Any help would be greatly appreciated!
In other words, I have 4 footman, 3 rifleman, and 2 knights. Knights > Rifleman > Footman. I want to split those among X players as equally as possible.
The end results of dividing those units among 4 players would look something along the lines of this:
player 1 gets: 2 footman, 1 rifleman
player 2 gets: 1 knight
player 3 gets: 2 footman, 2 rifleman
player 4 gets: 1 knight
The order doesn't matter as long as things are close to equal.
Here's some function I found online that I modified to work how I wanted:
Lua:
-- this takes a table filled with units and divides those units among two new tables
-- it also maintains a balance of "power" so that both unit tables are similar in strength
function DivideTableIntoTwo(tbl)
local u -- unit
local id -- unit-type id
local set1 = {} -- first table
local set2 = {} -- second table
local sum1 = 0 -- sum of set1 power
local sum2 = 0 -- sum of set2 power
local total = #tbl
for i=1,total do
u = tbl[i]
id = GetUnitTypeId(u)
if sum1 < sum2 then
table.insert(set1, u)
sum1 = sum1 + UnitData[id].power
else
table.insert(set2, u)
sum2 = sum2 + UnitData[id].power
end
tbl[i] = nil
end
return set1, set2
end
UnitData[id].power is a stat I've given each Unit-Type in the game that determines their worth. A Knight would have much higher power than a Footman for example.
Unfortunately, this method among other things I've tried produces less than desirable results where one player ends up receiving way more power worth of units than everyone else.
I was hoping for a function that could manage splitting the table to 2, 3, or 4 players at a time with a bit more accuracy. It could look something like this:
Lua:
function DistributeUnits(unitTable, numberOfPlayers)
if numberOfPlayers == 2 then
-- do some fancy math
return tbl1, tbl2
end
if numberOfPlayers == 3 then
-- do some fancy math
return tbl1, tbl2, tbl3
end
if numberOfPlayers == 4 then
-- do some fancy math
return tbl1, tbl2, tbl3, tbl4
end
end
Last edited: