MineClone2/mods/CORE/voxelibre/init.lua

187 lines
5.3 KiB
Lua

voxelibre = {}
voxelibre.colors = {
"black", "blue", "brown", "cyan", "gray", "green", "light_blue", "light_gray",
"lime", "magenta", "orange", "pink", "purple", "red", "white", "yellow"
}
function table.merge(tbl, ...)
local new_tbl = table.copy(tbl)
for _, tbl2 in ipairs({...}) do
for key, value in pairs(tbl2) do
if type(value) == "table" and type(new_tbl[key]) == "table" then
new_tbl[key] = table.merge(new_tbl[key], value)
else
new_tbl[key] = value
end
end
end
return new_tbl
end
_G.table.copy = table.copy
_G.table.merge = table.merge
---comment
---@param string string
---@param position integer
---@param substring string
---@return string
function string.insert(string, position, substring)
if position < 1 or position > #string + 1 then
return string
end
local part1 = string.sub(string, 1, position - 1)
local part2 = string.sub(string, position, #string)
local new_string = part1..substring..part2
return new_string
end
_G.string.insert = string.insert
function voxelibre.load_mod_files()
local mod_path = minetest.get_modpath(minetest.get_current_modname())
for _, file in pairs(minetest.get_dir_list(mod_path, false)) do
if file:sub(-4) == ".lua" and file ~= "init.lua" then
dofile(mod_path.."/"..file)
end
end
end
function voxelibre.description_from_identifier(identifier)
local description = ""
local words = {}
for word in string.gmatch(identifier, "[^_]+") do
table.insert(words, word)
end
for i, word in ipairs(words) do
if i > 1 then
description = description .. " "
end
if word == "of" or word == "the" then
description = description .. word
else
description = description .. word:gsub("^%l", string.upper)
end
end
return description
end
local function set_description(mod_name, identifier, definitions)
if not definitions.description then
definitions.description = voxelibre.description_from_identifier(identifier)
end
end
local function set_groups(definitions)
if definitions.walkable == false then
definitions.groups.solidity = 0
else
definitions.groups.solidity = 1
end
if not definitions.drawtype and not definitions.groups.opacity then
definitions.groups.opacity = 1
else
definitions.groups.opacity = 0
end
end
local function set_images(mod_name, identifier, definitions, is_item)
local base_image_name = mod_name.."_"..identifier..".png"
if (definitions.drawtype and definitions.drawtype:find("plantlike")) or is_item then
if not definitions.inventory_image then
definitions.inventory_image = base_image_name
end
if not definitions.wield_image then
definitions.wield_image = base_image_name
end
end
end
local function set_tiles(mod_name, identifier, definitions)
local base_image_name = mod_name.."_"..identifier..".png"
if not definitions.tiles then
definitions.tiles = {base_image_name}
else
if type(definitions.tiles) == "table" then
if definitions.tiles.type then
if definitions.tiles.type == "loglike" then
local top_base_image_name = base_image_name:gsub(".png", "_top.png")
definitions.tiles = {
top_base_image_name, top_base_image_name, base_image_name
}
elseif definitions.tiles.type == "modified" then
definitions.tiles = {
base_image_name..(definitions.tiles.modifiers.top or ""),
base_image_name..(definitions.tiles.modifiers.bottom or ""),
base_image_name..(definitions.tiles.modifiers.right or ""),
base_image_name..(definitions.tiles.modifiers.left or ""),
base_image_name..(definitions.tiles.modifiers.back or ""),
base_image_name..(definitions.tiles.modifiers.front or "")
}
end
end
else
return
end
end
end
function voxelibre.register_block(identifier, definitions)
local mod_name = minetest.get_current_modname()
if not mod_name then
return
end
if not definitions._blast_resistance then
definitions._blast_resistance = 0
end
if not definitions._hardness then
definitions._hardness = 0
end
if not definitions.stack_max then
definitions.stack_max = 64
end
set_description(mod_name, identifier, definitions)
set_groups(definitions)
set_images(mod_name, identifier, definitions, false)
set_tiles(mod_name, identifier, definitions)
minetest.register_node(":blocks:"..identifier, definitions)
end
function voxelibre.register_item(identifier, definitions)
local mod_name = minetest.get_current_modname()
if not mod_name then
return
end
if not definitions.stack_max then
definitions.stack_max = 64
end
set_description(mod_name, identifier, definitions)
set_images(mod_name, identifier, definitions, true)
minetest.register_craftitem(":items:"..identifier, definitions)
end