From 6f3118036d24c2841cfbddbb5b328504b3491299 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 18 Sep 2022 17:32:13 +0100 Subject: [PATCH] Update utils --- .../utils/format/array_2d.lua | 9 ++++-- .../utils/format/human_size.lua | 4 ++- .../utils/format/human_time.lua | 5 ++- worldeditadditions_core/utils/format/init.lua | 18 +++++++---- .../utils/format/make_ascii_table.lua | 7 ++-- worldeditadditions_core/utils/format/map.lua | 5 ++- .../utils/format/node_distribution.lua | 9 ++++-- worldeditadditions_core/utils/lru.lua | 4 +-- worldeditadditions_core/utils/mesh.lua | 4 +-- .../utils/node_identification.lua | 21 ++++++------ worldeditadditions_core/utils/nodes.lua | 12 ++++--- worldeditadditions_core/utils/numbers.lua | 32 ++++++++++--------- worldeditadditions_core/utils/parse/axes.lua | 13 +++++--- .../utils/parse/axes_parser.lua | 18 ++++------- .../utils/parse/chance.lua | 5 ++- worldeditadditions_core/utils/parse/init.lua | 19 ++++++----- worldeditadditions_core/utils/parse/map.lua | 11 ++++--- worldeditadditions_core/utils/parse/seed.lua | 5 ++- .../utils/parse/weighted_nodes.lua | 7 ++-- .../utils/strings/init.lua | 12 +++---- .../utils/strings/polyfill.lua | 12 +++---- .../utils/strings/split.lua | 8 ++--- .../utils/strings/split_shell.lua | 7 ++-- .../utils/strings/tochars.lua | 4 +-- worldeditadditions_core/utils/tables/init.lua | 26 +++++++-------- .../utils/tables/table_get_last.lua | 4 +-- .../utils/terrain/apply_heightmap_changes.lua | 4 +-- .../utils/terrain/calculate_normals.lua | 4 +-- .../utils/terrain/calculate_slopes.lua | 8 ++--- .../utils/terrain/init.lua | 12 +++---- .../utils/terrain/make_heightmap.lua | 6 ++-- 31 files changed, 179 insertions(+), 136 deletions(-) diff --git a/worldeditadditions_core/utils/format/array_2d.lua b/worldeditadditions_core/utils/format/array_2d.lua index 5e940d1..2ca2362 100644 --- a/worldeditadditions_core/utils/format/array_2d.lua +++ b/worldeditadditions_core/utils/format/array_2d.lua @@ -1,8 +1,11 @@ +local wea_c = worldeditadditions_core + + --- Prints a 2d array of numbers formatted like a JS TypedArray (e.g. like a manip node list or a convolutional kernel) -- In other words, the numbers should be formatted as a single flat array. -- @param tbl number[] The ZERO-indexed list of numbers -- @param width number The width of 2D array. -function worldeditadditions.format.array_2d(tbl, width) +local function format_array_2d(tbl, width) print("==== count: "..(#tbl+1)..", width:"..width.." ====") local display_width = 1 for _i,value in pairs(tbl) do @@ -11,10 +14,12 @@ function worldeditadditions.format.array_2d(tbl, width) display_width = display_width + 2 local next = {} for i=0, #tbl do - table.insert(next, worldeditadditions.str_padstart(tostring(tbl[i]), display_width)) + table.insert(next, wea_c.str_padstart(tostring(tbl[i]), display_width)) if #next == width then print(table.concat(next, "")) next = {} end end end + +return format_array_2d \ No newline at end of file diff --git a/worldeditadditions_core/utils/format/human_size.lua b/worldeditadditions_core/utils/format/human_size.lua index 543b6d8..0214892 100644 --- a/worldeditadditions_core/utils/format/human_size.lua +++ b/worldeditadditions_core/utils/format/human_size.lua @@ -4,10 +4,12 @@ -- @param n number The number to format. -- @param decimals number The number of decimal places to show. -- @return string A formatted string that represents the given input number. -function worldeditadditions.format.human_size(n, decimals) +local function format_human_size(n, decimals) local sizes = { "", "K", "M", "G", "T", "P", "E", "Y", "Z" } local factor = math.floor((#tostring(n) - 1) / 3) local multiplier = 10^(decimals or 0) local result = math.floor(0.5 + (n / (1000 ^ factor)) * multiplier) / multiplier return result .. sizes[factor+1] end + +return format_human_size \ No newline at end of file diff --git a/worldeditadditions_core/utils/format/human_time.lua b/worldeditadditions_core/utils/format/human_time.lua index 0f9a87f..2b6930a 100644 --- a/worldeditadditions_core/utils/format/human_time.lua +++ b/worldeditadditions_core/utils/format/human_time.lua @@ -3,7 +3,7 @@ -- Ported from PHP human_time from Pepperminty Wiki: https://github.com/sbrl/Pepperminty-Wiki/blob/fa81f0d/core/05-functions.php#L82-L104 -- @param ms float The number of milliseconds to convert. -- @return string A human-readable string representing the input ms. -function worldeditadditions.format.human_time(ms) +local function format_human_time(ms) if type(ms) ~= "number" then return "unknown" end local tokens = { { 31536000 * 1000, 'year' }, @@ -26,3 +26,6 @@ function worldeditadditions.format.human_time(ms) end end end + + +return format_human_time \ No newline at end of file diff --git a/worldeditadditions_core/utils/format/init.lua b/worldeditadditions_core/utils/format/init.lua index 43ad01b..363eebd 100644 --- a/worldeditadditions_core/utils/format/init.lua +++ b/worldeditadditions_core/utils/format/init.lua @@ -1,8 +1,12 @@ -worldeditadditions.format = {} +local wea_c = worldeditadditions_core + + +wea_c.format = { + array_2d = dofile(wea_c.modpath.."/utils/format/array_2d.lua"), + human_size = dofile(wea_c.modpath.."/utils/format/human_size.lua"), + human_time = dofile(wea_c.modpath.."/utils/format/human_time.lua"), + node_distribution = dofile(wea_c.modpath.."/utils/format/node_distribution.lua"), + make_ascii_table = dofile(wea_c.modpath.."/utils/format/make_ascii_table.lua"), + map = dofile(wea_c.modpath.."/utils/format/map.lua"), +} -dofile(worldeditadditions.modpath.."/utils/format/array_2d.lua") -dofile(worldeditadditions.modpath.."/utils/format/node_distribution.lua") -dofile(worldeditadditions.modpath.."/utils/format/make_ascii_table.lua") -dofile(worldeditadditions.modpath.."/utils/format/map.lua") -dofile(worldeditadditions.modpath.."/utils/format/human_time.lua") -dofile(worldeditadditions.modpath.."/utils/format/human_size.lua") diff --git a/worldeditadditions_core/utils/format/make_ascii_table.lua b/worldeditadditions_core/utils/format/make_ascii_table.lua index dcf62db..697b148 100644 --- a/worldeditadditions_core/utils/format/make_ascii_table.lua +++ b/worldeditadditions_core/utils/format/make_ascii_table.lua @@ -1,3 +1,4 @@ +local wea_c = worldeditadditions_core --- Makes a human-readable table of data. -- Data should be a 2D array - i.e. a table of tables. The nested tables should @@ -6,7 +7,7 @@ -- useful when you want to print a node list. -- @param data table[] A table of tables. Each subtable is a single row of the tabulated output. -- @returns string The input table of tables formatted into a nice ASCII table. -function worldeditadditions.format.make_ascii_table(data) +local function format_make_ascii_table(data) local extra_padding = 2 local result = {} local max_lengths = {} @@ -22,7 +23,7 @@ function worldeditadditions.format.make_ascii_table(data) for _key, row in ipairs(data) do local row_result = {} for i = 1, #row, 1 do - row_result[#row_result + 1] = worldeditadditions.str_padend(tostring(row[i]), max_lengths[i], " ") + row_result[#row_result + 1] = wea_c.str_padend(tostring(row[i]), max_lengths[i], " ") end result[#result+1] = table.concat(row_result, "") end @@ -30,3 +31,5 @@ function worldeditadditions.format.make_ascii_table(data) -- TODO: Add multi-column support here return table.concat(result, "\n") end + +return format_make_ascii_table \ No newline at end of file diff --git a/worldeditadditions_core/utils/format/map.lua b/worldeditadditions_core/utils/format/map.lua index 62a71ef..0ee431c 100644 --- a/worldeditadditions_core/utils/format/map.lua +++ b/worldeditadditions_core/utils/format/map.lua @@ -1,10 +1,13 @@ + --- Formats a key-value table of values as a string. -- @param map table The table of key-value pairs to format. -- @returns string The given table of key-value pairs formatted as a string. -function worldeditadditions.format.map(map) +local function format_map(map) local result = {} for key, value in pairs(map) do table.insert(result, key.."\t"..tostring(value)) end return table.concat(result, "\n") end + +return format_map \ No newline at end of file diff --git a/worldeditadditions_core/utils/format/node_distribution.lua b/worldeditadditions_core/utils/format/node_distribution.lua index 45734d7..574e576 100644 --- a/worldeditadditions_core/utils/format/node_distribution.lua +++ b/worldeditadditions_core/utils/format/node_distribution.lua @@ -1,18 +1,19 @@ +local wea_c = worldeditadditions_core --- Turns an associative node_id → count table into a human-readable list. -- @param distribution table A node distribution in the format { node_name = count, .... }. -- @param nodes_total number The total number of nodes in the distribution. -- @param add_total bool Whether to add a grand total to the bottom or not. Default: no -function worldeditadditions.format.node_distribution(distribution, nodes_total, add_total) +local function format_node_distribution(distribution, nodes_total, add_total) local distribution_data = {} for node_id, count in pairs(distribution) do table.insert(distribution_data, { count, - tostring(worldeditadditions.round((count / nodes_total) * 100, 2)).."%", + tostring(wea_c.round((count / nodes_total) * 100, 2)).."%", minetest.get_name_from_content_id(node_id) }) end - local result = worldeditadditions.format.make_ascii_table(distribution_data) + local result = wea_c.format.make_ascii_table(distribution_data) if add_total == true then result = result.."\n"..string.rep("=", 6 + #tostring(nodes_total) + 6).."\n".. @@ -21,3 +22,5 @@ function worldeditadditions.format.node_distribution(distribution, nodes_total, return result end + +return format_node_distribution \ No newline at end of file diff --git a/worldeditadditions_core/utils/lru.lua b/worldeditadditions_core/utils/lru.lua index 0ec0111..139ea6f 100644 --- a/worldeditadditions_core/utils/lru.lua +++ b/worldeditadditions_core/utils/lru.lua @@ -1,6 +1,6 @@ local Queue -if worldeditadditions then - Queue = dofile(worldeditadditions.modpath.."/utils/queue.lua") +if worldeditadditions_core then + Queue = dofile(worldeditadditions_core.modpath.."/utils/queue.lua") else Queue = require("queue") end diff --git a/worldeditadditions_core/utils/mesh.lua b/worldeditadditions_core/utils/mesh.lua index 792e7a7..28271fd 100644 --- a/worldeditadditions_core/utils/mesh.lua +++ b/worldeditadditions_core/utils/mesh.lua @@ -1,4 +1,4 @@ -local wea = worldeditadditions +local wea_c = worldeditadditions_core -- ███████ █████ ██████ ███████ @@ -72,7 +72,7 @@ function Mesh.dedupe(self) for j,face_next in ipairs(self.faces) do if i ~= j -- If we're not comparing a face to itself... and face_check == face_next -- ....and the 2 faces are equal.... - and not wea.table.contains(toremove, j) then -- ...and we haven't already marked it for removal... + and not wea_c.table.contains(toremove, j) then -- ...and we haven't already marked it for removal... -- Mark it for removal table.insert(toremove, j) end diff --git a/worldeditadditions_core/utils/node_identification.lua b/worldeditadditions_core/utils/node_identification.lua index bcf855b..f93e8b9 100644 --- a/worldeditadditions_core/utils/node_identification.lua +++ b/worldeditadditions_core/utils/node_identification.lua @@ -1,3 +1,6 @@ +local wea_c = worldeditadditions_core + + local node_id_air = minetest.get_content_id("air") local node_id_ignore = minetest.get_content_id("ignore") @@ -5,7 +8,7 @@ local node_id_ignore = minetest.get_content_id("ignore") -- It is recommended that the result of this function be cached. -- @param id number The content/node id to check. -- @return bool Whether the given node/content id is an airlike node or not. -function worldeditadditions.is_airlike(id) +function wea_c.is_airlike(id) -- Do a fast check against air and ignore if id == node_id_air then return true @@ -25,7 +28,7 @@ function worldeditadditions.is_airlike(id) return true end -- Just in case - if worldeditadditions.str_starts(name, "wielded_light") then + if wea_c.str_starts(name, "wielded_light") then return true end -- Just in case @@ -36,7 +39,7 @@ end -- It is recommended that the result of this function be cached. -- @param id number The content/node id to check. -- @return bool Whether the given node/content id is a liquid-ish node or not. -function worldeditadditions.is_liquidlike(id) +function wea_c.is_liquidlike(id) -- print("[is_liquidlike]") if id == node_id_ignore then return false end @@ -56,7 +59,7 @@ end -- It is recommended that the result of this function be cached. -- @param id number The content/node id to check. -- @return bool Whether the given node/content id is a sapling or not. -function worldeditadditions.is_sapling(id) +function wea_c.is_sapling(id) local node_name = minetest.get_name_from_content_id(id) return minetest.get_item_group(node_name, "sapling") ~= 0 end @@ -81,7 +84,7 @@ local sapling_aliases = {} -- @param sapling_node_name string The canonical name of the sapling. -- @param alias string The alias name of the sapling. -- @returns bool[,string] Whether the alias registration was successful or not. If false, then an error message as a string is also returned as the second value. -function worldeditadditions.register_sapling_alias(sapling_node_name, alias) +function wea_c.register_sapling_alias(sapling_node_name, alias) if sapling_aliases[sapling_node_name] ~= nil then return false, "Error: An alias against the node name '"..sapling_node_name.."' already exists." end @@ -91,9 +94,9 @@ end --- Convenience function to register many sapling aliases at once. -- @param tbl [string, string][] A list of tables containing exactly 2 strings in the form { sapling_node_name, alias }. -- @returns bool[,string] Whether the alias registrations were successful or not. If false, then an error message as a string is also returned as the second value. -function worldeditadditions.register_sapling_alias_many(tbl) +function wea_c.register_sapling_alias_many(tbl) for i, next in ipairs(tbl) do - local success, msg = worldeditadditions.register_sapling_alias( + local success, msg = wea_c.register_sapling_alias( next[1], next[2] ) @@ -103,14 +106,14 @@ function worldeditadditions.register_sapling_alias_many(tbl) end --- Returns the current key ⇒ value table of sapling names and aliases. -- @return table -function worldeditadditions.get_all_sapling_aliases() +function wea_c.get_all_sapling_aliases() return sapling_aliases end --- Attempts to normalise a sapling name using the currently registered aliases. -- @param in_name string The sapling name to normalise -- @param return_nil_on_failure bool Whether to return nil if we fail to resolve the sapling name with an alias, or return the original node name instead (default: false). -function worldeditadditions.normalise_saplingname(in_name, return_nil_on_failure) +function wea_c.normalise_saplingname(in_name, return_nil_on_failure) if sapling_aliases[in_name] then return sapling_aliases[in_name] elseif return_nil_on_failure then return nil else return in_name end diff --git a/worldeditadditions_core/utils/nodes.lua b/worldeditadditions_core/utils/nodes.lua index 70ea40c..79fb104 100644 --- a/worldeditadditions_core/utils/nodes.lua +++ b/worldeditadditions_core/utils/nodes.lua @@ -1,6 +1,8 @@ +local wea_c = worldeditadditions_core + --- Makes an associative table of node_name => weight into a list of node ids. -- Node names with a higher weight are repeated more times. -function worldeditadditions.make_weighted(tbl) +function wea_c.make_weighted(tbl) local result = {} for node_name, weight in pairs(tbl) do local next_id = minetest.get_content_id(node_name) @@ -17,7 +19,7 @@ end -- (e.g. an entry with a weight of 2 will be repeated twice). -- @param list table[] The list to unwind. -- @return number[],number The unwound list of node ids, follows by the number of node ids in total. -function worldeditadditions.unwind_node_list(list) +function wea_c.unwind_node_list(list) local result = {} for i,item in ipairs(list) do local node_id = minetest.get_content_id(item.node) @@ -28,7 +30,7 @@ function worldeditadditions.unwind_node_list(list) return result, #result end -function worldeditadditions.registered_nodes_by_group(group) +function wea_c.registered_nodes_by_group(group) local result = {} for name, def in pairs(minetest.registered_nodes) do if def.groups[group] then @@ -39,7 +41,7 @@ function worldeditadditions.registered_nodes_by_group(group) end --- Turns a node_name → weight table into a list of { node_name, weight } tables. -function worldeditadditions.weighted_to_list(node_weights) +function wea_c.weighted_to_list(node_weights) local result = {} for node_name, weight in pairs(node_weights) do table.insert(result, { node_name, weight }) @@ -80,7 +82,7 @@ end -- @param {Vector} pos2 The second position defining the area to emerge. -- @param {function} callback The callback to call when the emerging process is complete. -- @param {any} callback_state A state object to pass to the callback as a 2nd parameter (the 1st parameter is the emerge_area progress tracking state object) -function worldeditadditions.emerge_area(pos1, pos2, callback, callback_state) +function wea_c.emerge_area(pos1, pos2, callback, callback_state) local state = { stats = { cancelled = 0, error = 0, from_memory = 0, from_disk = 0, generated = 0 }, callback = callback, diff --git a/worldeditadditions_core/utils/numbers.lua b/worldeditadditions_core/utils/numbers.lua index 1c09f9f..1de8cf5 100644 --- a/worldeditadditions_core/utils/numbers.lua +++ b/worldeditadditions_core/utils/numbers.lua @@ -1,16 +1,18 @@ +local wea_c = worldeditadditions_core + -- From http://lua-users.org/wiki/SimpleRound -function worldeditadditions.round(num, numDecimalPlaces) +function wea_c.round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end -function worldeditadditions.hypotenuse(x1, y1, x2, y2) +function wea_c.hypotenuse(x1, y1, x2, y2) local xSquare = (x1 - x2) ^ 2; local ySquare = (y1 - y2) ^ 2; return math.sqrt(xSquare + ySquare); end -function worldeditadditions.sum(list) +function wea_c.sum(list) if #list == 0 then return 0 end local sum = 0 for i,value in ipairs(list) do @@ -22,15 +24,15 @@ end --- Calculates the mean of all the numbers in the given list. -- @param list number[] The list (table) of numbers to calculate the mean for. -- @returns The mean of the numbers in the given table. -function worldeditadditions.average(list) +function wea_c.average(list) if #list == 0 then return 0 end - return worldeditadditions.sum(list) / #list + return wea_c.sum(list) / #list end --- Finds the minimum value in the given list. -- @param list number[] The list (table) of numbers to find the minimum value of. -- @returns number The minimum value in the given list. -function worldeditadditions.min(list) +function wea_c.min(list) if #list == 0 then return nil end local min = nil for i,value in pairs(list) do @@ -44,7 +46,7 @@ end --- Finds the maximum value in the given list. -- @param list number[] The list (table) of numbers to find the maximum value of. -- @returns number The maximum value in the given list. -function worldeditadditions.max(list) +function wea_c.max(list) if #list == 0 then return nil end -- We use pairs() instead of ipairs() here, because then we can support -- zero-indexed 1D heightmaps too - and we don't care that the order is @@ -61,7 +63,7 @@ end --- Returns the minetest.get_us_time() in ms -- @return float -function worldeditadditions.get_ms_time() +function wea_c.get_ms_time() return minetest.get_us_time() / 1000 end @@ -71,10 +73,10 @@ end -- @param existing_times number[] A list of times - in ms - that the most recent work units have taken. -- @param done_count number The number of work units completed so far. -- @param total_count number The total number of work units to be completed. -function worldeditadditions.eta(existing_times, done_count, total_count) +function wea_c.eta(existing_times, done_count, total_count) local max = 100 - local average = worldeditadditions.average( - worldeditadditions.table.get_last(existing_times, max) + local average = wea_c.average( + wea_c.table.get_last(existing_times, max) ) local times_left = total_count - done_count if times_left == 0 then return 0 end @@ -84,7 +86,7 @@ end --- Returns the sign (+ or -) at the beginning of a string if present. -- @param src string|int Input string. -- @return string|int Returns the signed multiplier (1|-1). -function worldeditadditions.getsign(src) +function wea_c.getsign(src) if type(src) == "number" then if src < 0 then return -1 else return 1 end elseif type(src) ~= "string" then return 1 @@ -98,12 +100,12 @@ end -- @param min number The minimum allowed value. -- @param max number The maximum allowed value. -- @returns number The clamped number. -function worldeditadditions.clamp(value, min, max) +function wea_c.clamp(value, min, max) if value < min then return min end if value > max then return max end return value end -- For Testing: --- worldeditadditions = {} --- print(worldeditadditions.getsign('-y')) +-- wea_c = {} +-- print(wea_c.getsign('-y')) diff --git a/worldeditadditions_core/utils/parse/axes.lua b/worldeditadditions_core/utils/parse/axes.lua index a297462..6339d5a 100644 --- a/worldeditadditions_core/utils/parse/axes.lua +++ b/worldeditadditions_core/utils/parse/axes.lua @@ -1,9 +1,11 @@ +---@diagnostic disable: cast-local-type +-- ^--- we're using Vector3 here, so it gets confused local Vector3 -if worldeditadditions then - local wea = worldeditadditions - Vector3 = dofile(wea.modpath.."/utils/vector3.lua") +if worldeditadditions_core then + local wea_c = worldeditadditions_core + Vector3 = dofile(wea_c.modpath.."/utils/vector3.lua") else - Vector3 = require("worldeditadditions.utils.vector3") + Vector3 = require("worldeditadditions_core.utils.vector3") end --- Parses an absolute axis name to a Vector3 instance. @@ -64,7 +66,8 @@ end --- Parses a relative or absolute axis name into a Vector3 instance. -- @param axis_name string The axis name to parse. --- @param facing_dir table The direction the player is facing. Obtain this by calling worldeditadditions. +-- @param facing_dir table The direction the player is facing. Obtain this by calling worldeditadditions_core.????? +-- TODO: Fix this comment. local function parse_axis_name(axis_name, facing_dir) local success, result = parse_relative_axis_name(axis_name, facing_dir) if not success then diff --git a/worldeditadditions_core/utils/parse/axes_parser.lua b/worldeditadditions_core/utils/parse/axes_parser.lua index 8310a83..3004e07 100644 --- a/worldeditadditions_core/utils/parse/axes_parser.lua +++ b/worldeditadditions_core/utils/parse/axes_parser.lua @@ -14,22 +14,18 @@ local function unpack(tbl) end --------------- +local wea_c = worldeditadditions_core or nil local Vector3 -if worldeditadditions then - local wea = worldeditadditions - Vector3 = dofile(wea.modpath.."/utils/vector3.lua") -else - Vector3 = require("worldeditadditions.utils.vector3") -end - local key_instance -if worldeditadditions then - local wea = worldeditadditions - key_instance = dofile(wea.modpath.."/utils/parse/key_instance.lua") +if worldeditadditions_core then + key_instance = dofile(wea_c.modpath.."/utils/parse/key_instance.lua") + Vector3 = dofile(wea_c.modpath.."/utils/vector3.lua") else + Vector3 = require("worldeditadditions_core.utils.vector3") key_instance = require("worldeditadditions.utils.parse.key_instance") end + --- Unified Axis Keywords banks local keywords = { -- Compass keywords @@ -139,7 +135,7 @@ end --- Converts Unified Axis Keyword table into Vector3 instances. -- @param: tbl: Table: Keyword table to parse --- @param: facing: Table: Output from worldeditadditions.player_dir(name) +-- @param: facing: Table: Output from worldeditadditions_core.player_dir(name) -- @param: sum: Bool | String | nil: Return a single vector by summing the 2 output vectors together -- @returns: Vector3, [Vector3]: returns min, max Vector3s or sum Vector3 (if @param: sum ~= nil) -- if error: @returns: false, String: error message diff --git a/worldeditadditions_core/utils/parse/chance.lua b/worldeditadditions_core/utils/parse/chance.lua index f8a9a7b..8e56de6 100644 --- a/worldeditadditions_core/utils/parse/chance.lua +++ b/worldeditadditions_core/utils/parse/chance.lua @@ -3,7 +3,7 @@ -- @param str string The string to parse. -- @param invert_percent string The operation mode. Valid modes: "1-in-n" (default), "weight". "1-in-n" refers to a 1-in-N chance of something happening (lower numbers mean greater likelihood). "weight", on the other hand, is instead a weighting that something will happen (higher numbers mean a greater likelihood). -- @returns number|nil The 1-in-N chance if parsing was successful, otherwise nil. -function worldeditadditions.parse.chance(str, mode) +local function parse_chance(str, mode) if not mode then mode = "1-in-n" end if tonumber(str) ~= nil then return tonumber(str) end if str:sub(#str) == "%" then @@ -14,3 +14,6 @@ function worldeditadditions.parse.chance(str, mode) end return nil end + + +return parse_chance \ No newline at end of file diff --git a/worldeditadditions_core/utils/parse/init.lua b/worldeditadditions_core/utils/parse/init.lua index 49c8f44..19dc236 100644 --- a/worldeditadditions_core/utils/parse/init.lua +++ b/worldeditadditions_core/utils/parse/init.lua @@ -1,3 +1,5 @@ +local wea_c = worldeditadditions_core + -- ██████ █████ ██████ ███████ ███████ -- ██ ██ ██ ██ ██ ██ ██ ██ -- ██████ ███████ ██████ ███████ █████ @@ -5,21 +7,22 @@ -- ██ ██ ██ ██ ██ ███████ ███████ -- Unified Axes Keyword Parser -local uak_parse = dofile(worldeditadditions.modpath.."/utils/parse/axes_parser.lua") +local uak_parse = dofile(wea_c.modpath.."/utils/parse/axes_parser.lua") -- Old axis parsing functions -local axes = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") +local axes = dofile(wea_c.modpath.."/utils/parse/axes.lua") -worldeditadditions.parse = { +wea_c.parse = { direction_keyword = uak_parse.keyword, directions = uak_parse.keytable, -- Old parse functions (marked for deprecation). -- Use parse.keytable or parse.keyword instead axes = axes.parse_axes, axis_name = axes.parse_axis_name, + + seed = dofile(wea_c.modpath.."/utils/parse/seed.lua"), + chance = dofile(wea_c.modpath.."/utils/parse/chance.lua"), + map = dofile(wea_c.modpath.."/utils/parse/map.lua"), + weighted_nodes = dofile(wea_c.modpath.."/utils/parse/weighted_nodes.lua") } -dofile(worldeditadditions.modpath.."/utils/parse/chance.lua") -dofile(worldeditadditions.modpath.."/utils/parse/map.lua") -dofile(worldeditadditions.modpath.."/utils/parse/seed.lua") -dofile(worldeditadditions.modpath.."/utils/parse/weighted_nodes.lua") -dofile(worldeditadditions.modpath.."/utils/parse/tokenise_commands.lua") +dofile(wea_c.modpath.."/utils/parse/tokenise_commands.lua") diff --git a/worldeditadditions_core/utils/parse/map.lua b/worldeditadditions_core/utils/parse/map.lua index e2b28ca..1446f64 100644 --- a/worldeditadditions_core/utils/parse/map.lua +++ b/worldeditadditions_core/utils/parse/map.lua @@ -1,4 +1,5 @@ -local wea = worldeditadditions +---@diagnostic disable: cast-local-type +local wea_c = worldeditadditions_core --- Parses a map of key-value pairs into a table. -- For example, "count 25000 speed 0.8 rate_erosion 0.006 doawesome true" would be parsed into @@ -6,10 +7,10 @@ local wea = worldeditadditions -- @param params_text string The string to parse. -- @param keywords string[]? Optional. A list of keywords. Keywords can be present on their own without a value. If found, their value will be automatically set to bool true. -- @returns table A table of key-value pairs parsed out from the given string. -function worldeditadditions.parse.map(params_text, keywords) +local function parse_map(params_text, keywords) if not keywords then keywords = {} end local result = {} - local parts = wea.split(params_text, "%s+", false) + local parts = wea_c.split(params_text, "%s+", false) local last_key = nil local mode = "KEY" @@ -26,7 +27,7 @@ function worldeditadditions.parse.map(params_text, keywords) else last_key = part -- Keyword support - if wea.table.contains(keywords, last_key) then + if wea_c.table.contains(keywords, last_key) then result[last_key] = true else mode = "VALUE" @@ -35,3 +36,5 @@ function worldeditadditions.parse.map(params_text, keywords) end return true, result end + +return parse_map \ No newline at end of file diff --git a/worldeditadditions_core/utils/parse/seed.lua b/worldeditadditions_core/utils/parse/seed.lua index 6f83bd3..3692d95 100644 --- a/worldeditadditions_core/utils/parse/seed.lua +++ b/worldeditadditions_core/utils/parse/seed.lua @@ -6,7 +6,7 @@ -- (caution: certainlly NOT crypto-secure!). -- @param {string} str The string to convert. -- @source https://stackoverflow.com/a/2624210/1460422 The idea came from here -function worldeditadditions.parse.seed(str) +local function parse_seed(str) if type(str) == "number" then return str end if tonumber(str) ~= nil then return tonumber(str) end local result = 0 @@ -15,3 +15,6 @@ function worldeditadditions.parse.seed(str) end return result end + + +return parse_seed \ No newline at end of file diff --git a/worldeditadditions_core/utils/parse/weighted_nodes.lua b/worldeditadditions_core/utils/parse/weighted_nodes.lua index b4dc0ba..2c89e21 100644 --- a/worldeditadditions_core/utils/parse/weighted_nodes.lua +++ b/worldeditadditions_core/utils/parse/weighted_nodes.lua @@ -1,11 +1,11 @@ --- Parses a list of strings as a list of weighted nodes - e.g. like in -- the //mix command. Example: "dirt 5 stone sand 2". --- @param parts string[] The list of strings to parse (try worldeditadditions.split) +-- @param parts string[] The list of strings to parse (try worldeditadditions_core.split) -- @param as_list bool If true, then table.insert() successive { node = string, weight = number } subtables when parsing instead of populating as an associative array. -- @param func_normalise callable If specified, the given function will be used to normalise node names instead of worldedit.normalize_nodename. A single argument is passed containing the un-normalised node name, and the return value is assumed to be the normalised node name. -- @returns table A table in the form node_name => weight. -function worldeditadditions.parse.weighted_nodes(parts, as_list, func_normalise) +local function parse_weighted_nodes(parts, as_list, func_normalise) if as_list == nil then as_list = false end local MODE_EITHER = 1 local MODE_NODE = 2 @@ -61,3 +61,6 @@ function worldeditadditions.parse.weighted_nodes(parts, as_list, func_normalise) return true, result end + + +return parse_weighted_nodes \ No newline at end of file diff --git a/worldeditadditions_core/utils/strings/init.lua b/worldeditadditions_core/utils/strings/init.lua index d5b76c9..ff31d18 100644 --- a/worldeditadditions_core/utils/strings/init.lua +++ b/worldeditadditions_core/utils/strings/init.lua @@ -1,7 +1,7 @@ -local wea = worldeditadditions +local wea_c = worldeditadditions_core -dofile(wea.modpath.."/utils/strings/split.lua") -dofile(wea.modpath.."/utils/strings/polyfill.lua") -dofile(wea.modpath.."/utils/strings/tochars.lua") -wea.split_shell = dofile(wea.modpath.."/utils/strings/split_shell.lua") -wea.to_boolean = dofile(wea.modpath.."/utils/strings/to_boolean.lua") +dofile(wea_c.modpath.."/utils/strings/split.lua") +dofile(wea_c.modpath.."/utils/strings/polyfill.lua") +dofile(wea_c.modpath.."/utils/strings/tochars.lua") +wea_c.split_shell = dofile(wea_c.modpath.."/utils/strings/split_shell.lua") +wea_c.to_boolean = dofile(wea_c.modpath.."/utils/strings/to_boolean.lua") diff --git a/worldeditadditions_core/utils/strings/polyfill.lua b/worldeditadditions_core/utils/strings/polyfill.lua index 1348da2..64f4b90 100644 --- a/worldeditadditions_core/utils/strings/polyfill.lua +++ b/worldeditadditions_core/utils/strings/polyfill.lua @@ -43,12 +43,12 @@ local function trim(str) end -if worldeditadditions then - worldeditadditions.str_padend = str_padend - worldeditadditions.str_padstart = str_padstart - worldeditadditions.str_starts = str_starts - worldeditadditions.str_ends = str_ends - worldeditadditions.trim = trim +if worldeditadditions_core then + worldeditadditions_core.str_padend = str_padend + worldeditadditions_core.str_padstart = str_padstart + worldeditadditions_core.str_starts = str_starts + worldeditadditions_core.str_ends = str_ends + worldeditadditions_core.trim = trim else return { str_padend = str_padend, diff --git a/worldeditadditions_core/utils/strings/split.lua b/worldeditadditions_core/utils/strings/split.lua index 541b86b..4005760 100644 --- a/worldeditadditions_core/utils/strings/split.lua +++ b/worldeditadditions_core/utils/strings/split.lua @@ -15,7 +15,7 @@ -- for substr in gsplit(text, pattern, plain) do -- doSomething(substr) -- end -function worldeditadditions.gsplit(text, pattern, plain) +function worldeditadditions_core.gsplit(text, pattern, plain) local splitStart, length = 1, #text return function () if splitStart then @@ -48,9 +48,9 @@ end -- @param plain boolean If true (or truthy), pattern is interpreted as a -- plain string, not a Lua pattern -- @returns table A sequence table containing the substrings -function worldeditadditions.dsplit(text, pattern, plain) +function worldeditadditions_core.dsplit(text, pattern, plain) local ret = {} - for match in worldeditadditions.gsplit(text, pattern, plain) do + for match in worldeditadditions_core.gsplit(text, pattern, plain) do table.insert(ret, match) end return ret @@ -62,7 +62,7 @@ end -- @param plain boolean If true (or truthy), pattern is interpreted as a -- plain string, not a Lua pattern -- @returns table A sequence table containing the substrings -function worldeditadditions.split(str,dlm,plain) +function worldeditadditions_core.split(str,dlm,plain) local pos, ret = 0, {} local ins, i = str:find(dlm,pos,plain) -- "if plain" shaves off some time in the while statement diff --git a/worldeditadditions_core/utils/strings/split_shell.lua b/worldeditadditions_core/utils/strings/split_shell.lua index d1211ce..9598321 100644 --- a/worldeditadditions_core/utils/strings/split_shell.lua +++ b/worldeditadditions_core/utils/strings/split_shell.lua @@ -1,5 +1,6 @@ --- worldeditadditions = { modpath="/home/sbrl/.minetest/worlds/Mod-Sandbox/worldmods/WorldEditAdditions/worldeditadditions/" } -local table_map = dofile(worldeditadditions.modpath.."/utils/tables/table_map.lua") +-- worldeditadditions_core = { modpath="/home/sbrl/.minetest/worlds/Mod-Sandbox/worldmods/WorldEditAdditions/worldeditadditions_core/" } +local wea_c = worldeditadditions_core +local table_map = dofile(wea_c.modpath.."/utils/tables/table_map.lua") local function is_whitespace(char) return char:match("%s") @@ -26,7 +27,7 @@ local function split_shell(text, autotrim) if mode == "NORMAL" then if is_whitespace(curchar) and #acc > 0 then - local nextval = worldeditadditions.trim(table.concat(acc, "")) + local nextval = wea_c.trim(table.concat(acc, "")) if #nextval > 0 then table.insert(result, table.concat(acc, "")) end diff --git a/worldeditadditions_core/utils/strings/tochars.lua b/worldeditadditions_core/utils/strings/tochars.lua index e8e34aa..3dca60b 100644 --- a/worldeditadditions_core/utils/strings/tochars.lua +++ b/worldeditadditions_core/utils/strings/tochars.lua @@ -3,7 +3,7 @@ -- @param sort bool Sort characters -- @param rem_dups bool Remove duplicate characters -- @returns table A sequence table containing the substrings -function worldeditadditions.tochars(text,sort,rem_dups) +function worldeditadditions_core.tochars(text,sort,rem_dups) local t, set = {}, {} if rem_dups then text:gsub(".",function(c) set[c] = true end) @@ -20,7 +20,7 @@ end --- Split into a set of characters. -- @param text string The string to iterate over -- @returns table A sequence set table containing the substrings -function worldeditadditions.tocharset(text) +function worldeditadditions_core.tocharset(text) local t = {} text:gsub(".",function(c) t[c] = true end) return t diff --git a/worldeditadditions_core/utils/tables/init.lua b/worldeditadditions_core/utils/tables/init.lua index f77cdbf..837f52b 100644 --- a/worldeditadditions_core/utils/tables/init.lua +++ b/worldeditadditions_core/utils/tables/init.lua @@ -8,18 +8,18 @@ -- Lua doesn't exactly come with batteries included, so this is quite an -- extensive collection of functions :P -local wea = worldeditadditions +local wea_c = worldeditadditions_core -wea.table = { - apply = dofile(wea.modpath.."/utils/tables/table_apply.lua"), - contains = dofile(wea.modpath.."/utils/tables/table_contains.lua"), - deepcopy = dofile(wea.modpath.."/utils/tables/deepcopy.lua"), - filter = dofile(wea.modpath.."/utils/tables/table_filter.lua"), - get_last = dofile(wea.modpath.."/utils/tables/table_get_last.lua"), - makeset = dofile(wea.modpath.."/utils/tables/makeset.lua"), - map = dofile(wea.modpath.."/utils/tables/table_map.lua"), - shallowcopy = dofile(wea.modpath.."/utils/tables/shallowcopy.lua"), - tostring = dofile(wea.modpath.."/utils/tables/table_tostring.lua"), - unique = dofile(wea.modpath.."/utils/tables/table_unique.lua"), - unpack = dofile(wea.modpath.."/utils/tables/table_unpack.lua"), +wea_c.table = { + apply = dofile(wea_c.modpath.."/utils/tables/table_apply.lua"), + contains = dofile(wea_c.modpath.."/utils/tables/table_contains.lua"), + deepcopy = dofile(wea_c.modpath.."/utils/tables/deepcopy.lua"), + filter = dofile(wea_c.modpath.."/utils/tables/table_filter.lua"), + get_last = dofile(wea_c.modpath.."/utils/tables/table_get_last.lua"), + makeset = dofile(wea_c.modpath.."/utils/tables/makeset.lua"), + map = dofile(wea_c.modpath.."/utils/tables/table_map.lua"), + shallowcopy = dofile(wea_c.modpath.."/utils/tables/shallowcopy.lua"), + tostring = dofile(wea_c.modpath.."/utils/tables/table_tostring.lua"), + unique = dofile(wea_c.modpath.."/utils/tables/table_unique.lua"), + unpack = dofile(wea_c.modpath.."/utils/tables/table_unpack.lua"), } diff --git a/worldeditadditions_core/utils/tables/table_get_last.lua b/worldeditadditions_core/utils/tables/table_get_last.lua index a6ce58f..fcf55f1 100644 --- a/worldeditadditions_core/utils/tables/table_get_last.lua +++ b/worldeditadditions_core/utils/tables/table_get_last.lua @@ -1,6 +1,6 @@ -local wea = worldeditadditions +local wea_c = worldeditadditions_core -local table_unpack = dofile(wea.modpath.."/utils/tables/table_unpack.lua") +local table_unpack = dofile(wea_c.modpath.."/utils/tables/table_unpack.lua") --- Returns only the last count items in a given numerical table-based list. diff --git a/worldeditadditions_core/utils/terrain/apply_heightmap_changes.lua b/worldeditadditions_core/utils/terrain/apply_heightmap_changes.lua index fc1e1d1..944fd93 100644 --- a/worldeditadditions_core/utils/terrain/apply_heightmap_changes.lua +++ b/worldeditadditions_core/utils/terrain/apply_heightmap_changes.lua @@ -1,5 +1,5 @@ -local wea = worldeditadditions -local Vector3 = wea.Vector3 +local wea_c = worldeditadditions_core +local Vector3 = wea_c.Vector3 --- Applies changes to a heightmap to a Voxel Manipulator data block. -- @param pos1 vector Position 1 of the defined region diff --git a/worldeditadditions_core/utils/terrain/calculate_normals.lua b/worldeditadditions_core/utils/terrain/calculate_normals.lua index fbadf11..8272a09 100644 --- a/worldeditadditions_core/utils/terrain/calculate_normals.lua +++ b/worldeditadditions_core/utils/terrain/calculate_normals.lua @@ -1,5 +1,5 @@ -local wea = worldeditadditions -local Vector3 = wea.Vector3 +local wea_c = worldeditadditions_core +local Vector3 = wea_c.Vector3 --- Calculates a normal map for the given heightmap. diff --git a/worldeditadditions_core/utils/terrain/calculate_slopes.lua b/worldeditadditions_core/utils/terrain/calculate_slopes.lua index 8da99eb..e4fd99d 100644 --- a/worldeditadditions_core/utils/terrain/calculate_slopes.lua +++ b/worldeditadditions_core/utils/terrain/calculate_slopes.lua @@ -1,5 +1,5 @@ -local wea = worldeditadditions -local Vector3 = wea.Vector3 +local wea_c = worldeditadditions_core +local Vector3 = wea_c.Vector3 --- Converts a 2d heightmap into slope values in radians. @@ -9,10 +9,10 @@ local Vector3 = wea.Vector3 -- @param heightmap_size int[] The size of the heightmap in the form [ z, x ] -- @return Vector[] The calculated slope map, in the same form as the input heightmap. Each element of the array is a (floating-point) number representing the slope in that cell in radians. local function calculate_slopes(heightmap, heightmap_size) - local normals = wea.terrain.calculate_normals(heightmap, heightmap_size) + local normals = wea_c.terrain.calculate_normals(heightmap, heightmap_size) local slopes = { } - local up = wea.Vector3.new(0, 1, 0) -- Z & Y are flipped + local up = Vector3.new(0, 1, 0) -- Z & Y are flipped for z = heightmap_size.z-1, 0, -1 do for x = heightmap_size.x-1, 0, -1 do diff --git a/worldeditadditions_core/utils/terrain/init.lua b/worldeditadditions_core/utils/terrain/init.lua index c83577e..3952f43 100644 --- a/worldeditadditions_core/utils/terrain/init.lua +++ b/worldeditadditions_core/utils/terrain/init.lua @@ -1,12 +1,10 @@ -local wea = worldeditadditions -local Vector3 = wea.Vector3 - +local wea_c = worldeditadditions_core local terrain = { - make_heightmap = dofile(wea.modpath.."/utils/terrain/make_heightmap.lua"), - calculate_normals = dofile(wea.modpath.."/utils/terrain/calculate_normals.lua"), - calculate_slopes = dofile(wea.modpath.."/utils/terrain/calculate_slopes.lua"), - apply_heightmap_changes = dofile(wea.modpath.."/utils/terrain/apply_heightmap_changes.lua") + make_heightmap = dofile(wea_c.modpath.."/utils/terrain/make_heightmap.lua"), + calculate_normals = dofile(wea_c.modpath.."/utils/terrain/calculate_normals.lua"), + calculate_slopes = dofile(wea_c.modpath.."/utils/terrain/calculate_slopes.lua"), + apply_heightmap_changes = dofile(wea_c.modpath.."/utils/terrain/apply_heightmap_changes.lua") } return terrain diff --git a/worldeditadditions_core/utils/terrain/make_heightmap.lua b/worldeditadditions_core/utils/terrain/make_heightmap.lua index c69821b..1669f27 100644 --- a/worldeditadditions_core/utils/terrain/make_heightmap.lua +++ b/worldeditadditions_core/utils/terrain/make_heightmap.lua @@ -1,5 +1,5 @@ -local wea = worldeditadditions -local Vector3 = wea.Vector3 +local wea_c = worldeditadditions_core +local Vector3 = wea_c.Vector3 --- Given a manip object and associates, generates a 2D x/z heightmap. @@ -23,7 +23,7 @@ local function make_heightmap(pos1, pos2, manip, area, data) -- Scan each column top to bottom for y = pos2.y+1, pos1.y, -1 do local i = area:index(x, y, z) - if not (wea.is_airlike(data[i]) and not wea.is_liquidlike(data[i])) then + if not (wea_c.is_airlike(data[i]) and not wea_c.is_liquidlike(data[i])) then -- It's the first non-airlike node in this column -- Start heightmap values from 1 (i.e. there's at least 1 node in the column) heightmap[hi] = (y - pos1.y) + 1