minetest-mod-squaresville/schematics.lua

119 lines
2.8 KiB
Lua

-- Squaresville schematics.lua
-- Copyright Duane Robertson (duane@duanerobertson.com), 2017
-- Distributed under the LGPLv2.1 (https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html)
-- Do not distribute.
--dofile(squaresville.path..'/gpl3.lua')
-- Create and initialize a table for a schematic.
function squaresville.schematic_array(width, height, depth)
if not (width and height and depth and type(width) == 'number' and type(height) == 'number' and type(depth) == 'number') then
return
end
-- 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
local default_schematic_path = minetest.get_modpath("default").."/schematics/"
local function convert_mts(mts)
local sch = minetest.serialize_schematic(mts, 'lua', {})
sch = minetest.deserialize('return {'..sch..'}')
return sch.schematic
end
squaresville.place_schematic = function(schem, ipos, vm, center)
local force_placement
local replacements = schem.replacements
local rot = schem.rotation
local size = schem.size or {x=0, z=0}
local pos = table.copy(ipos)
if center or (schem.flags and schem.flags['place_center_x']) then
if schem.size_offset then
pos.x = pos.x - schem.size_offset.x
else
pos.x = pos.x - math.floor(size.x / 2)
end
end
if center or (schem.flags and schem.flags['place_center_z']) then
if schem.size_offset then
pos.z = pos.z - schem.size_offset.z
else
pos.z = pos.z - math.floor(size.z / 2)
end
end
if schem.filename then
schem = schem.filename
elseif schem.schematic then
schem = schem.schematic
--else
--print('non-mts schematic? ')
end
minetest.place_schematic_on_vmanip(vm, pos, schem, rot, replacements, force_placement)
end
squaresville.schematics = {}
do
for _, s in pairs(squaresville.decorations) do
if s.deco_type == 'schematic' then
local schem = convert_mts(s.schematic)
s.size = schem.size
if s.size then
s.size_offset = {x=math.floor(s.size.x / 2), y=math.floor(s.size.y / 2), z=math.floor(s.size.z / 2)}
if s.name == 'jungle_tree' then
s.size.x = 3
s.size.z = 3
end
end
end
end
end
do
local w, h, d = 1, 8, 1
local s = squaresville.schematic_array(w, h, d)
for y = 0, math.floor(h/2)-1 do
s.data[0*d*h + y*d + 0 + 1].name = 'squaresville:fur_tree'
s.data[0*d*h + y*d + 0 + 1].param1 = 255
end
for y = 0, h-1 do
if y / 2 == math.floor(y / 2) then
s.yslice_prob[#s.yslice_prob+1] = {ypos=y,prob=170}
end
end
squaresville.schematics['fur_tree'] = s
end