Skip to content

Commit

Permalink
Use PascalCase instead of snake_case for math.WeightedRandom
Browse files Browse the repository at this point in the history
  • Loading branch information
nike4613 committed Aug 15, 2024
1 parent 4633803 commit 8328f68
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions gamemodes/terrortown/gamemode/server/sv_roleselection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ local function SetSubRoles(plys, availableRoles, selectableRoles, selectedForced
pick = math.random(plysAmount)
else
-- use a weighted sum to select the player
pick = math.random_weighted(plys, function(ply)
pick = math.WeightedRandom(plys, function(ply)
local weightTbl = ply:GetRoleWeightTable()
return weightTbl[subrole] or minWeight
end)
Expand Down Expand Up @@ -915,7 +915,7 @@ local function SelectBaseRolePlayers(plys, subrole, roleAmount)
pick = math.random(#plys)
else
-- use a weighted sum to select the player
pick = math.random_weighted(plys, function(ply)
pick = math.WeightedRandom(plys, function(ply)
local weightTbl = ply:GetRoleWeightTable()
return weightTbl[subrole] or minWeight
end)
Expand Down
10 changes: 5 additions & 5 deletions lua/ttt2/extensions/math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ function math.ExponentialDecay(halflife, dt)
end

---
-- Gets the index of an item in the provided table, weighted according to the weights (derived from get_weight).
-- Gets the index of an item in the provided table, weighted according to the weights (derived from getWeight).
-- @param table tbl The array of items to find a weighted item in.
-- @param function get_weight Called as get_weight(item, index). Must return number.
-- @param function getWeight Called as getWeight(item, index). Must return number.
-- @return number
-- @realm shared
function math.random_weighted(tbl, get_weight)
function math.WeightedRandom(tbl, getWeight)
-- There are several possible ways to get a weighted item. The most obvious is to simply include an item in
-- the table N times, where N is an integer proportional to the weight. This, however, requires maintaining
-- that table, which may be undesirable.
Expand All @@ -46,7 +46,7 @@ function math.random_weighted(tbl, get_weight)
-- first, compute the sum weight
local sum = 0
for k, v in pairs(tbl) do
sum = sum + get_weight(v, k)
sum = sum + getWeight(v, k)
end

-- get the random number
Expand All @@ -55,7 +55,7 @@ function math.random_weighted(tbl, get_weight)
-- now do the prefix-sum for the final value
sum = 0
for k, v in pairs(tbl) do
sum = sum + get_weight(v, k)
sum = sum + getWeight(v, k)
if sum >= rand then
return k
end
Expand Down

0 comments on commit 8328f68

Please sign in to comment.