lib_ecology/useful_code_samples.lua

94 lines
1.7 KiB
Lua

-- Modify a node to add a group
function minetest.add_group(node, groups)
local def = minetest.registered_items[node]
if not def then
return false
end
local def_groups = def.groups or {}
for group, value in pairs(groups) do
if value ~= 0 then
def_groups[group] = value
else
def_groups[group] = nil
end
end
minetest.override_item(node, {groups = def_groups})
return true
end
-- Check if the table contains an element.
function table.contains(table, element)
for key, value in pairs(table) do
if value == element then
if key then
return key
else
return true
end
end
end
return false
end
-- This isn't already in the math library? Really?
function math.round(i)
return math.floor(i + 0.5)
end
-- Push an element onto a stack (table).
function push(t, x)
t[#t+1] = x
end
function lib_ecology.clone_node(name)
local node = minetest.registered_nodes[name]
local node2 = table.copy(node)
return node2
end
--NOTES
--[[
Code below taken from Valleys_c deco.lua, and deco_trees.lua
Sections are delineated due to execution timing.
--]]
--[[
-- Create and initialize a table for a schematic.
function lib_ecology.schematic_array(width, height, depth)
-- Dimensions of data array.
local s = {size={x=width, y=height, z=depth}}
s.data = {}
for z = 0,depth-1 do
for y = 0,height-1 do
for x = 0,width-1 do
local i = z*width*height + y*width + x + 1
s.data[i] = {}
s.data[i].name = "air"
s.data[i].param1 = 000
end
end
end
s.yslice_prob = {}
return s
end
-- Clear all decorations, so I can place the new trees.
--minetest.clear_registered_decorations()
-- A list of all schematics, for re-use.
lib_ecology.schematics = {}
--]]