mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-02-03 13:12:11 +02:00
weighted table function addition
This commit is contained in:
parent
2f8137daea
commit
ee9bd72115
@ -143,7 +143,7 @@ end
|
||||
|
||||
--- Chooses a random entry from a weighted table
|
||||
-- because this uses math.random, it cannot be used outside of events
|
||||
-- @param weight_table <table> of tables with items and their weights
|
||||
-- @param weighted_table <table> of tables with items and their weights
|
||||
-- @param item_index <number> of the index of items, defaults to 1
|
||||
-- @param weight_index <number> of the index of the weights, defaults to 2
|
||||
-- @return <any> table element
|
||||
@ -167,6 +167,24 @@ function table.get_random_weighted(weighted_table, item_index, weight_index)
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns a table with % chance values for each item of a weighted_table
|
||||
-- @param weighted_table <table> of tables with items and their weights
|
||||
-- @param item_index <number> of the index of items, defaults to 1
|
||||
-- @param weight_index <number> of the index of the weights, defaults to 2
|
||||
function table.get_random_weighted_chances(weighted_table, item_index, weight_index)
|
||||
local total_weight = 0
|
||||
item_index = item_index or 1
|
||||
weight_index = weight_index or 2
|
||||
for _, v in pairs(weighted_table) do
|
||||
total_weight = total_weight + v[weight_index]
|
||||
end
|
||||
local chance_table = {}
|
||||
for k, v in pairs(weighted_table) do
|
||||
chance_table[k] = v[weight_index] / total_weight
|
||||
end
|
||||
return chance_table
|
||||
end
|
||||
|
||||
--- Creates a fisher-yates shuffle of a sequential number-indexed table
|
||||
-- because this uses math.random, it cannot be used outside of events if no rng is supplied
|
||||
-- from: http://www.sdknews.com/cross-platform/corona/tutorial-how-to-shuffle-table-items
|
||||
|
Loading…
x
Reference in New Issue
Block a user