2018-12-09 17:50:16 +02:00
|
|
|
local random = math.random
|
2018-12-09 20:10:16 +02:00
|
|
|
local floor = math.floor
|
2018-12-09 17:50:16 +02:00
|
|
|
local remove = table.remove
|
2018-12-09 20:10:16 +02:00
|
|
|
local insert = table.insert
|
|
|
|
local tonumber = tonumber
|
|
|
|
local pairs = pairs
|
|
|
|
local table_size = table_size
|
2018-12-09 17:50:16 +02:00
|
|
|
|
2018-11-26 06:03:08 +02:00
|
|
|
--- Searches a table to remove a specific element without an index
|
2018-11-22 11:39:07 +02:00
|
|
|
-- @param t table to search
|
|
|
|
-- @param element to search for
|
2017-08-02 10:12:57 +02:00
|
|
|
table.remove_element = function(t, element)
|
2018-11-20 12:46:42 +02:00
|
|
|
for k, v in pairs(t) do
|
|
|
|
if v == element then
|
2018-12-09 17:50:16 +02:00
|
|
|
remove(t, k)
|
2018-11-20 12:46:42 +02:00
|
|
|
break
|
|
|
|
end
|
2017-08-02 10:12:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-26 06:03:08 +02:00
|
|
|
--- Adds the contents of table t2 to table t1
|
|
|
|
-- @param t1 table to insert into
|
|
|
|
-- @param t2 table to insert from
|
2018-11-20 12:46:42 +02:00
|
|
|
table.add_all = function(t1, t2)
|
|
|
|
for k, v in pairs(t2) do
|
|
|
|
if tonumber(k) then
|
2018-12-09 17:50:16 +02:00
|
|
|
insert(t1, v)
|
2018-11-20 12:46:42 +02:00
|
|
|
else
|
|
|
|
t1[k] = v
|
|
|
|
end
|
2017-08-02 10:12:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-22 11:39:07 +02:00
|
|
|
--- Checks if a table contains an element
|
|
|
|
-- @param t table to search
|
|
|
|
-- @param e element to search for
|
|
|
|
-- @returns the index of an element or -1
|
2017-08-02 10:12:57 +02:00
|
|
|
table.index_of = function(t, e)
|
2018-11-20 12:46:42 +02:00
|
|
|
local i = 1
|
|
|
|
for _, v in pairs(t) do
|
|
|
|
if v == e then
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
i = i + 1
|
2017-08-02 10:12:57 +02:00
|
|
|
end
|
2018-11-20 12:46:42 +02:00
|
|
|
return -1
|
2017-08-02 10:12:57 +02:00
|
|
|
end
|
|
|
|
|
2018-12-09 20:10:16 +02:00
|
|
|
local index_of = table.index_of
|
|
|
|
|
2018-11-22 11:39:07 +02:00
|
|
|
--- Checks if a table contains an element
|
|
|
|
-- @param t table to search
|
|
|
|
-- @param e element to search for
|
|
|
|
-- @returns true or false
|
2017-08-02 10:12:57 +02:00
|
|
|
table.contains = function(t, e)
|
2018-12-09 20:10:16 +02:00
|
|
|
return index_of(t, e) > -1
|
2017-08-02 10:12:57 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 06:03:08 +02:00
|
|
|
--- Adds an element into a specific index position while shuffling the rest down
|
|
|
|
-- @param t table to add into
|
|
|
|
-- @param index the position in the table to add to
|
|
|
|
-- @param element to add
|
2018-11-20 12:46:42 +02:00
|
|
|
table.set = function(t, index, element)
|
|
|
|
local i = 1
|
|
|
|
for k in pairs(t) do
|
|
|
|
if i == index then
|
|
|
|
t[k] = element
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
i = i + 1
|
2017-08-02 10:12:57 +02:00
|
|
|
end
|
2018-11-20 12:46:42 +02:00
|
|
|
error('Index out of bounds', 2)
|
2017-08-02 10:12:57 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 06:03:08 +02:00
|
|
|
--- Chooses a random entry from a table
|
2018-12-19 15:43:30 +02:00
|
|
|
-- because this uses math.random, it cannot be used outside of events
|
|
|
|
-- @param t table to select an element from
|
|
|
|
-- @param sorted boolean to indicate whether the table is sorted by numerical index or not
|
|
|
|
-- @param key boolean to indicate whether to return the key or value
|
|
|
|
-- @return a random element of table t
|
|
|
|
table.get_random = function(t, sorted, key)
|
2018-11-26 06:03:08 +02:00
|
|
|
if sorted then
|
2018-12-09 17:50:16 +02:00
|
|
|
return t[random(#t)]
|
2018-11-27 13:24:21 +02:00
|
|
|
end
|
2018-12-09 17:50:16 +02:00
|
|
|
local target_index = random(1, table_size(t))
|
2018-11-27 13:24:21 +02:00
|
|
|
local count = 1
|
2018-12-19 15:43:30 +02:00
|
|
|
for k, v in pairs(t) do
|
2018-11-27 13:24:21 +02:00
|
|
|
if target_index == count then
|
2018-12-19 15:43:30 +02:00
|
|
|
if key then
|
|
|
|
return k
|
|
|
|
else
|
|
|
|
return t[v]
|
|
|
|
end
|
2018-11-20 12:46:42 +02:00
|
|
|
end
|
2018-12-19 15:43:30 +02:00
|
|
|
count = count + 1
|
2017-08-02 10:12:57 +02:00
|
|
|
end
|
2018-11-17 13:56:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Chooses a random entry from a weighted table
|
2018-12-19 15:43:30 +02:00
|
|
|
-- because this uses math.random, it cannot be used outside of events
|
|
|
|
-- @param weight_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
|
|
|
|
-- @returns a table entry
|
|
|
|
-- @see features.chat_triggers::hodor
|
2018-11-17 13:56:03 +02:00
|
|
|
table.get_random_weighted = function(weighted_table, item_index, weight_index)
|
2018-11-20 12:46:42 +02:00
|
|
|
local total_weight = 0
|
2018-12-09 20:10:16 +02:00
|
|
|
item_index = item_index or 1
|
|
|
|
weight_index = weight_index or 2
|
2018-11-17 13:56:03 +02:00
|
|
|
|
2018-11-20 12:46:42 +02:00
|
|
|
for _, w in pairs(weighted_table) do
|
|
|
|
total_weight = total_weight + w[weight_index]
|
|
|
|
end
|
2018-11-17 13:56:03 +02:00
|
|
|
|
2018-12-09 20:10:16 +02:00
|
|
|
local index = random() * total_weight
|
2018-11-20 12:46:42 +02:00
|
|
|
local weight_sum = 0
|
|
|
|
for _, w in pairs(weighted_table) do
|
|
|
|
weight_sum = weight_sum + w[weight_index]
|
|
|
|
if weight_sum >= index then
|
|
|
|
return w[item_index]
|
|
|
|
end
|
|
|
|
end
|
2018-11-17 13:56:03 +02:00
|
|
|
end
|
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
--- Creates a fisher-yates shuffle of a sequential number-indexed table
|
2018-12-19 15:43:30 +02:00
|
|
|
-- because this uses math.random, it cannot be used outside of events if no rng is supplied
|
2018-12-09 17:50:16 +02:00
|
|
|
-- from: http://www.sdknews.com/cross-platform/corona/tutorial-how-to-shuffle-table-items
|
|
|
|
-- @param t table to shuffle
|
|
|
|
table.shuffle_table = function(t, rng)
|
|
|
|
local rand = rng or math.random
|
|
|
|
local iterations = #t
|
|
|
|
if iterations == 0 then
|
|
|
|
error('Not a sequential table')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local j
|
|
|
|
|
|
|
|
for i = iterations, 2, -1 do
|
|
|
|
j = rand(i)
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Clears all existing entries in a table
|
|
|
|
-- @param t table to clear
|
2018-12-22 01:38:47 +02:00
|
|
|
-- @param sorted boolean to indicate whether the table is sorted by numerical index or not
|
|
|
|
table.clear_table = function(t, sorted)
|
|
|
|
if sorted then
|
|
|
|
for i = 1, #t do
|
|
|
|
t[i] = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for i in pairs(t) do
|
|
|
|
t[i] = nil
|
|
|
|
end
|
2018-12-09 17:50:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-21 17:32:07 +02:00
|
|
|
--[[
|
2018-11-17 13:56:03 +02:00
|
|
|
Returns the index where t[index] == target.
|
2018-12-09 20:10:16 +02:00
|
|
|
If there is no such index, returns a negative value such that bit32.bnot(value) is
|
|
|
|
the index that the value should be inserted to keep the list ordered.
|
2018-11-17 13:56:03 +02:00
|
|
|
t must be a list in ascending order for the return value to be valid.
|
2018-01-21 17:32:07 +02:00
|
|
|
|
|
|
|
Usage example:
|
|
|
|
local t = {1,3,5,7,9}
|
|
|
|
local x = 5
|
2018-11-17 13:56:03 +02:00
|
|
|
local index = table.binary_search(t, x)
|
|
|
|
if index < 0 then
|
|
|
|
game.print("value not found, smallest index where t[index] > x is: " .. bit32.bnot(index))
|
2018-01-21 17:32:07 +02:00
|
|
|
else
|
|
|
|
game.print("value found at index: " .. index)
|
2018-11-17 13:56:03 +02:00
|
|
|
end
|
2018-01-21 17:32:07 +02:00
|
|
|
]]
|
2018-12-09 20:10:16 +02:00
|
|
|
table.binary_search = function(t, target)
|
2018-11-20 12:46:42 +02:00
|
|
|
--For some reason bit32.bnot doesn't return negative numbers so I'm using ~x = -1 - x instead.
|
2018-11-17 13:56:03 +02:00
|
|
|
|
2018-01-21 17:32:07 +02:00
|
|
|
local lower = 1
|
|
|
|
local upper = #t
|
2018-11-17 13:56:03 +02:00
|
|
|
|
2018-01-21 17:32:07 +02:00
|
|
|
if upper == 0 then
|
2018-11-20 12:46:42 +02:00
|
|
|
return -2 -- ~1
|
2018-01-21 17:32:07 +02:00
|
|
|
end
|
2018-11-17 13:56:03 +02:00
|
|
|
|
|
|
|
repeat
|
2018-12-09 20:10:16 +02:00
|
|
|
local mid = floor((lower + upper) * 0.5)
|
2018-11-20 12:46:42 +02:00
|
|
|
local value = t[mid]
|
|
|
|
if value == target then
|
|
|
|
return mid
|
|
|
|
elseif value < target then
|
|
|
|
lower = mid + 1
|
|
|
|
else
|
|
|
|
upper = mid - 1
|
|
|
|
end
|
2018-01-21 17:32:07 +02:00
|
|
|
until lower > upper
|
2018-11-17 13:56:03 +02:00
|
|
|
|
2018-01-21 17:32:07 +02:00
|
|
|
return -1 - lower -- ~lower
|
2018-11-20 12:46:42 +02:00
|
|
|
end
|
2018-12-01 07:53:54 +02:00
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
-- add table-related functions that exist in base factorio to the 'table' table
|
2018-12-22 14:49:21 +02:00
|
|
|
table.inspect = require 'utils.inspect'
|
2018-12-01 07:53:54 +02:00
|
|
|
table.size = table_size
|