mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-30 04:30:58 +02:00
utils->core, list_utils->table, overhaul both and updated extant uses
This commit is contained in:
parent
c7201e119b
commit
ae024c4e10
@ -16,9 +16,9 @@ local name_combinations = #naming_words.adverbs * #naming_words.adjectives * #na
|
||||
-- @returns name as a string
|
||||
local function create_name(words_table)
|
||||
local adverb, adjective, noun
|
||||
adverb = table.get_random(words_table.adverbs)
|
||||
adjective = table.get_random(words_table.adjectives)
|
||||
noun = table.get_random(words_table.nouns)
|
||||
adverb = table.get_random(words_table.adverbs, true)
|
||||
adjective = table.get_random(words_table.adjectives, true)
|
||||
noun = table.get_random(words_table.nouns, true)
|
||||
return adverb .. '_' .. adjective .. '_' .. noun
|
||||
end
|
||||
|
||||
|
@ -1,16 +1,22 @@
|
||||
local Module = {}
|
||||
local Game = require 'utils.game'
|
||||
local prefix = '## - '
|
||||
local minutes_to_ticks = 60 * 60
|
||||
local hours_to_ticks = 60 * 60 * 60
|
||||
local ticks_to_minutes = 1 / minutes_to_ticks
|
||||
local ticks_to_hours = 1 / hours_to_ticks
|
||||
|
||||
--- Measures distance between pos1 and pos2
|
||||
Module.distance = function(pos1, pos2)
|
||||
local dx = pos2.x - pos1.x
|
||||
local dy = pos2.y - pos1.y
|
||||
return math.sqrt(dx * dx + dy * dy)
|
||||
end
|
||||
|
||||
--- Takes msg and prints it to all players except provided player
|
||||
Module.print_except = function(msg, player)
|
||||
for _, p in pairs(game.players) do
|
||||
if p.connected and p ~= player then
|
||||
for _, p in pairs(game.connected_players) do
|
||||
if p ~= player then
|
||||
p.print(msg)
|
||||
end
|
||||
end
|
||||
@ -93,10 +99,7 @@ Module.ternary = function(c, t, f)
|
||||
end
|
||||
end
|
||||
|
||||
local minutes_to_ticks = 60 * 60
|
||||
local hours_to_ticks = 60 * 60 * 60
|
||||
local ticks_to_minutes = 1 / minutes_to_ticks
|
||||
local ticks_to_hours = 1 / hours_to_ticks
|
||||
--- Takes a time in ticks and returns a string with the time in format "x hour(s) x minute(s)"
|
||||
Module.format_time = function(ticks)
|
||||
local result = {}
|
||||
|
||||
@ -123,15 +126,15 @@ Module.format_time = function(ticks)
|
||||
end
|
||||
|
||||
--- Prints a message letting the player know they cannot run a command
|
||||
-- @param1 name of the command
|
||||
-- @param name string name of the command
|
||||
Module.cant_run = function(name)
|
||||
Game.player_print("Can't run command (" .. name .. ') - insufficient permission.')
|
||||
end
|
||||
|
||||
--- Logs the use of a command and its user
|
||||
-- @param1 takes a string with the actor's name (usually acquired by calling get_actor)
|
||||
-- @param2 the command's name as table element
|
||||
-- @param3 the command's parameters as a table element (optional)
|
||||
-- @param actor string with the actor's name (usually acquired by calling get_actor)
|
||||
-- @param command the command's name as table element
|
||||
-- @param parameters the command's parameters as a table (optional)
|
||||
Module.log_command = function(actor, command, parameters)
|
||||
local action = table.concat {'[Admin-Command] ', actor, ' used: ', command}
|
||||
if parameters then
|
||||
@ -145,4 +148,17 @@ Module.comma_value = function(n) -- credit http://richard.warburton.it
|
||||
return left .. (num:reverse():gsub('(%d%d%d)', '%1,'):reverse()) .. right
|
||||
end
|
||||
|
||||
--- Asserts the argument is one of type arg_types
|
||||
-- @param arg the variable to check
|
||||
-- @param arg_types the type as a table of sings
|
||||
-- @return boolean
|
||||
Module.verify_mult_types = function(arg, arg_types)
|
||||
for _, arg_type in pairs(arg_types) do
|
||||
if type(arg) == arg_type then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
return Module
|
@ -1,17 +1,7 @@
|
||||
-- Functions that perform actions on tables
|
||||
|
||||
local function assert_argument_valid(a, arg_type)
|
||||
arg_type = arg_type or 'table'
|
||||
if type(a) ~= arg_type then
|
||||
error("bad argument #1 to '" .. debug.getinfo(2, 'n').name .. "' (table expected, got " .. type(a) .. ')', 3)
|
||||
end
|
||||
end
|
||||
|
||||
--- Removes a specific element from a table
|
||||
--- Searches a table to remove a specific element without an index
|
||||
-- @param t table to search
|
||||
-- @param element to search for
|
||||
table.remove_element = function(t, element)
|
||||
assert_argument_valid(t)
|
||||
for k, v in pairs(t) do
|
||||
if v == element then
|
||||
table.remove(t, k)
|
||||
@ -20,9 +10,10 @@ table.remove_element = function(t, element)
|
||||
end
|
||||
end
|
||||
|
||||
--- Adds the contents of table t2 to table t1
|
||||
-- @param t1 table to insert into
|
||||
-- @param t2 table to insert from
|
||||
table.add_all = function(t1, t2)
|
||||
assert_argument_valid(t1)
|
||||
assert_argument_valid(t2)
|
||||
for k, v in pairs(t2) do
|
||||
if tonumber(k) then
|
||||
table.insert(t1, v)
|
||||
@ -32,21 +23,11 @@ table.add_all = function(t1, t2)
|
||||
end
|
||||
end
|
||||
|
||||
table.size = function(t)
|
||||
assert_argument_valid(t)
|
||||
local size = 0
|
||||
for _ in pairs(t) do
|
||||
size = size + 1
|
||||
end
|
||||
return size
|
||||
end
|
||||
|
||||
--- 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
|
||||
table.index_of = function(t, e)
|
||||
assert_argument_valid(t)
|
||||
local i = 1
|
||||
for _, v in pairs(t) do
|
||||
if v == e then
|
||||
@ -62,13 +43,14 @@ end
|
||||
-- @param e element to search for
|
||||
-- @returns true or false
|
||||
table.contains = function(t, e)
|
||||
assert_argument_valid(t)
|
||||
return table.index_of(t, e) > -1
|
||||
end
|
||||
|
||||
--- 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
|
||||
table.set = function(t, index, element)
|
||||
assert_argument_valid(t)
|
||||
assert_argument_valid(index, 'number')
|
||||
local i = 1
|
||||
for k in pairs(t) do
|
||||
if i == index then
|
||||
@ -80,22 +62,22 @@ table.set = function(t, index, element)
|
||||
error('Index out of bounds', 2)
|
||||
end
|
||||
|
||||
table.get = function(t, index)
|
||||
assert_argument_valid(t)
|
||||
assert_argument_valid(index, 'number')
|
||||
local i = 1
|
||||
for k in pairs(t) do
|
||||
if i == index then
|
||||
return t[k]
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
error('Index out of bounds', 2)
|
||||
end
|
||||
|
||||
--- Chooses a random entry from a table
|
||||
table.get_random = function(this_table)
|
||||
return this_table[math.random(#this_table)]
|
||||
--@param t table to select an element from
|
||||
--@param sorted boolean to indicate whether the table is sorted by numerical index or not
|
||||
--@return a random element of table t
|
||||
table.get_random = function(t, sorted)
|
||||
if sorted then
|
||||
return t[math.random(#t)]
|
||||
else
|
||||
local target_index = math.random(1, table_size(t))
|
||||
local count = 1
|
||||
for _, v in pairs(t) do
|
||||
if target_index == count then
|
||||
return t[v]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Chooses a random entry from a weighted table
|
||||
@ -140,8 +122,6 @@ end
|
||||
table.binary_search =
|
||||
function(t, target)
|
||||
--For some reason bit32.bnot doesn't return negative numbers so I'm using ~x = -1 - x instead.
|
||||
assert_argument_valid(t)
|
||||
assert_argument_valid(target, 'number')
|
||||
|
||||
local lower = 1
|
||||
local upper = #t
|
Loading…
x
Reference in New Issue
Block a user