Skip to content

Commit

Permalink
Use Core/helpers/table istead local functions. Closes #922
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 committed Jan 21, 2023
1 parent 0123bc2 commit 5400ad8
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 100 deletions.
14 changes: 12 additions & 2 deletions mods/lord/Core/helpers/src/lua_ext/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions mods/lord/_experimental/lord_bows/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 5 additions & 11 deletions mods/lord/lord_classes/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local table_has_key
= table.has_key

local SL = minetest.get_translator("lord_classes")

--[[
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 3 additions & 13 deletions mods/lord/lord_replacer/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local table_has_value
= table.has_value

local S = minetest.get_translator("lord_replacer")

local DEFAULT_SELECTED_NODE = "default:dirt"
Expand All @@ -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
Expand Down
10 changes: 1 addition & 9 deletions mods/lord/lottinventory/cooking.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions mods/lord/lottinventory/forbidden.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 1 addition & 9 deletions mods/lord/lottinventory/master.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down
10 changes: 1 addition & 9 deletions mods/lord/lottinventory/protection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 1 addition & 9 deletions mods/lord/lottinventory/zcg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions mods/lord/mega_sl/init.lua
Original file line number Diff line number Diff line change
@@ -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 = "Сохранить данные в файл",
Expand Down
11 changes: 4 additions & 7 deletions mods/lord/player/arena_health.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local table_has_value
= table.has_value

local ARENA_AREA_IDS = {}

local arena_ids = minetest.settings:get("arenas") or ""
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions mods/lord/roads/init.lua
Original file line number Diff line number Diff line change
@@ -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" -- бордюр
Expand All @@ -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
Expand Down

0 comments on commit 5400ad8

Please sign in to comment.