diff --git a/mods/lord/Core/helpers/src/lua_ext/table.lua b/mods/lord/Core/helpers/src/lua_ext/table.lua index faa820746..75e2c71ff 100644 --- a/mods/lord/Core/helpers/src/lua_ext/table.lua +++ b/mods/lord/Core/helpers/src/lua_ext/table.lua @@ -22,16 +22,26 @@ function table.has_key(table, find_key) return false end +--- @overload fun(table1:table, table2:table):table --- @param table1 table --- @param table2 table +--- @param overwrite boolean whether to overwrite the `table1` (default: false) --- @return table -function table.merge(table1, table2) - local merged_table = table_copy(table1) +function table.merge(table1, table2, overwrite) + overwrite = overwrite or false + local merged_table = overwrite and table1 or table_copy(table1) for key, value in pairs(table2) do merged_table[key] = value end return merged_table end +local table_merge = table.merge + +--- @param table1 table +--- @param table2 table +function table.overwrite(table1, table2) + return table_merge(table1, table2, true) +end --- @param table table --- @return boolean diff --git a/mods/lord/_experimental/lord_bows/init.lua b/mods/lord/_experimental/lord_bows/init.lua index 3e9c8a4c0..8870e11fe 100644 --- a/mods/lord/_experimental/lord_bows/init.lua +++ b/mods/lord/_experimental/lord_bows/init.lua @@ -4,19 +4,12 @@ GRAVITY = minetest.settings:get("movement_gravity") or 9.81 dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/entities_projectiles.lua") dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/mechanics_throwing.lua") -local function table_merge(t1, t2) - for k, v in pairs(t2) do - t1[k] = v - end - return t1 -end - local stage_groups = {not_in_creative_inventory = 1} local function register_bow(name, def) local wield_scale = {x = 2, y = 2, z = 0.75} - local all_stage_groups = table_merge(stage_groups, def.groups) + local all_stage_groups = table.merge(stage_groups, def.groups) minetest.register_tool(name, { range = 1, diff --git a/mods/lord/lord_classes/init.lua b/mods/lord/lord_classes/init.lua index 2f131b89e..2471b098d 100644 --- a/mods/lord/lord_classes/init.lua +++ b/mods/lord/lord_classes/init.lua @@ -1,3 +1,6 @@ +local table_has_key + = table.has_key + local SL = minetest.get_translator("lord_classes") --[[ @@ -143,22 +146,13 @@ function races.save() return true end -local function table_contains(t, v) - for k, _ in pairs(t) do - if k == v then - return true - end - end - return false -end - -- Validates {race, gender} tables -- Returns true if the table is valid, false otherwise function races.validate(race_and_gender) local race = race_and_gender[1] local gender = race_and_gender[2] - if table_contains(races.list, race) then + if table_has_key(races.list, race) then if gender == "male" or gender == "female" then return true end @@ -494,7 +488,7 @@ end) minetest.register_on_joinplayer(function(player) local name = player:get_player_name() - if table_contains(cache.players, name) then -- Player is registered already + if table_has_key(cache.players, name) then -- Player is registered already local r = races.get_race_and_gender(name) if races.list[r[1]].cannot_be_selected then races.show_change_form(name) diff --git a/mods/lord/lord_replacer/init.lua b/mods/lord/lord_replacer/init.lua index cb21b0a04..b00c385ce 100644 --- a/mods/lord/lord_replacer/init.lua +++ b/mods/lord/lord_replacer/init.lua @@ -1,3 +1,6 @@ +local table_has_value + = table.has_value + local S = minetest.get_translator("lord_replacer") local DEFAULT_SELECTED_NODE = "default:dirt" @@ -15,19 +18,6 @@ local REPLACER_IRREPLACEABLE = { } ---- Checks if a table has value. ----@param table table ----@param value any ----@return boolean @does table has given value -local function table_has_value(table, value) - for _, v in ipairs(table) do - if v == value then - return true - end - end - return false -end - --- Sets replacer's selection using its metadata. ---@param itemstack ItemStack ---@param selected_node NodeTable @`minetest.get_node` format diff --git a/mods/lord/lottinventory/cooking.lua b/mods/lord/lottinventory/cooking.lua index efa5a72b0..75694b2e5 100644 --- a/mods/lord/lottinventory/cooking.lua +++ b/mods/lord/lottinventory/cooking.lua @@ -26,14 +26,6 @@ zcc.items_in_group = function(group) return items end -local table_copy = function(table) - local out = {} - for k, v in pairs(table) do - out[k] = v - end - return out -end - zcc.add_craft = function(input, output, groups) if minetest.get_item_group(output, "forbidden") > 0 then return @@ -63,7 +55,7 @@ zcc.add_craft = function(input, output, groups) zcc.add_craft({ width = c.width, type = c.type, - items = table_copy(c.items) + items = table.copy(c.items) }, output, g2) -- it is needed to copy the table, else groups won't work right end return diff --git a/mods/lord/lottinventory/forbidden.lua b/mods/lord/lottinventory/forbidden.lua index 651b5c416..1f015aedd 100644 --- a/mods/lord/lottinventory/forbidden.lua +++ b/mods/lord/lottinventory/forbidden.lua @@ -26,14 +26,6 @@ zfc.items_in_group = function(group) return items end -local table_copy = function(table) - local out = {} - for k,v in pairs(table) do - out[k] = v - end - return out -end - zfc.add_craft = function(input, output, groups) if minetest.get_item_group(output, "forbidden") > 0 then if not groups then groups = {} end @@ -54,7 +46,7 @@ zfc.add_craft = function(input, output, groups) zfc.add_craft({ width = c.width, type = c.type, - items = table_copy(c.items) + items = table.copy(c.items) }, output, g2) -- it is needed to copy the table, else groups won't work right end return diff --git a/mods/lord/lottinventory/master.lua b/mods/lord/lottinventory/master.lua index 814c90455..268f6e668 100644 --- a/mods/lord/lottinventory/master.lua +++ b/mods/lord/lottinventory/master.lua @@ -30,14 +30,6 @@ zmc.items_in_group = function(group) return items end -local table_copy = function(table) - local out = {} - for k,v in pairs(table) do - out[k] = v - end - return out -end - zmc.add_craft = function(input, output, groups) if not groups then groups = {} end local c = {} @@ -57,7 +49,7 @@ zmc.add_craft = function(input, output, groups) zmc.add_craft({ width = c.width, type = c.type, - items = table_copy(c.items) + items = table.copy(c.items) }, output, g2) -- it is needed to copy the table, else groups won't work right end return diff --git a/mods/lord/lottinventory/protection.lua b/mods/lord/lottinventory/protection.lua index 1c82b5359..b3ca5f158 100644 --- a/mods/lord/lottinventory/protection.lua +++ b/mods/lord/lottinventory/protection.lua @@ -26,14 +26,6 @@ zpc.items_in_group = function(group) return items end -local table_copy = function(table) - local out = {} - for k,v in pairs(table) do - out[k] = v - end - return out -end - zpc.add_craft = function(input, output, groups) if minetest.get_item_group(output, "armor_use") > 0 or minetest.get_item_group(output, "armor_crafts") > 0 then if minetest.get_item_group(output, "forbidden") > 0 then @@ -57,7 +49,7 @@ zpc.add_craft = function(input, output, groups) zpc.add_craft({ width = c.width, type = c.type, - items = table_copy(c.items) + items = table.copy(c.items) }, output, g2) -- it is needed to copy the table, else groups won't work right end return diff --git a/mods/lord/lottinventory/zcg.lua b/mods/lord/lottinventory/zcg.lua index d477b1593..09e5f5070 100644 --- a/mods/lord/lottinventory/zcg.lua +++ b/mods/lord/lottinventory/zcg.lua @@ -30,14 +30,6 @@ zcg.items_in_group = function(group) return items end -local table_copy = function(table) - local out = {} - for k,v in pairs(table) do - out[k] = v - end - return out -end - zcg.add_craft = function(input, output, groups) if minetest.get_item_group(output, "forbidden") > 0 then return @@ -69,7 +61,7 @@ zcg.add_craft = function(input, output, groups) zcg.add_craft({ width = c.width, type = c.type, - items = table_copy(c.items) + items = table.copy(c.items) }, output, g2) -- it is needed to copy the table, else groups won't work right end return diff --git a/mods/lord/mega_sl/init.lua b/mods/lord/mega_sl/init.lua index 5676c6d89..67e0058ec 100644 --- a/mods/lord/mega_sl/init.lua +++ b/mods/lord/mega_sl/init.lua @@ -1,9 +1,5 @@ ---- @param table table ----@return boolean -local function table_is_empty(table) - for _,_ in pairs(table) do return false end -- luacheck: ignore - return true -end +local table_is_empty + = table.is_empty minetest.register_chatcommand ("S", { description = "Сохранить данные в файл", diff --git a/mods/lord/player/arena_health.lua b/mods/lord/player/arena_health.lua index 137b5bf4f..57f5d8b0b 100644 --- a/mods/lord/player/arena_health.lua +++ b/mods/lord/player/arena_health.lua @@ -1,3 +1,6 @@ +local table_has_value + = table.has_value + local ARENA_AREA_IDS = {} local arena_ids = minetest.settings:get("arenas") or "" @@ -27,13 +30,7 @@ local function player_undisplay_hp(player) player:set_properties({nametag = name, nametag_color = "#FFFFFF",}) end --- TODO: move this functions into separate mod -local table_indexOf = table.indexof ---- @param list table ---- @param value any -local function table_has_value(list, value) - return table_indexOf(list, value) ~= -1 -end +-- TODO: move this function into Core/helpers --- @param list table --- @param values table local function table_keys_has_one_of_values(list, values) diff --git a/mods/lord/roads/init.lua b/mods/lord/roads/init.lua index ff7858f1b..24be903e7 100644 --- a/mods/lord/roads/init.lua +++ b/mods/lord/roads/init.lua @@ -1,13 +1,6 @@ local S = minetest.get_translator("roads") local function register_road(name, main_material, desc, fill) -- функция регистрации всех нодов дороги - - local function table_concat(s,t) -- функция объединение таблиц - for i, v in pairs(t) do - s[i] = v - end - end - local mn = "roads:"..name -- имя мода с материалом local road_name = mn.."_road" -- дорога local border_name = mn.."_border" -- бордюр @@ -18,7 +11,7 @@ local function register_road(name, main_material, desc, fill) -- функция local border_item_name = mn.."_border_item" -- бордюр(итем) local sp_groups = {not_in_creative_inventory = 1} -- специальная группа - table_concat(sp_groups, desc.groups) + table.overwrite(sp_groups, desc.groups) -- textures: border_x, border_z, border_top, step_border_x, -- step_border_z, incorn_border_top, outcorn_border_top