refactoring

This commit is contained in:
BuckarooBanzay 2022-03-26 15:21:22 +01:00
parent bec1cdb0be
commit f0f206226d
8 changed files with 119 additions and 117 deletions

116
api.lua
View File

@ -1,116 +0,0 @@
local Context = {}
local Context_mt = { __index = Context }
function Context:translate(x, y, z)
-- offset position
local opos = {
x = x or 0,
y = y or 0,
z = z or 0
}
-- transformed position
local tpos = mtscad.transform_pos(self.pos, vector.add(opos, self.pos), self.rotation)
local ctx = self:clone()
ctx.pos = tpos
return ctx
end
function Context:with(node)
self.node = node
return self
end
function Context:set()
minetest.set_node(self.pos, self.node)
return self
end
function Context:execute(fn)
fn(self)
return self
end
local slope_param2 = {
["1,1,0"] = 3,
["1,-1,0"] = 21,
["0,1,1"] = 2,
["0,-1,1"] = 22,
["-1,1,0"] = 1,
["-1,-1,0"] = 23,
["0,1,-1"] = 0,
["0,-1,-1"] = 20
}
local function format_pos(p)
return p.x .. "," .. p.y .. "," .. p.z
end
local function get_stairsplus_nodename(name, stairtype)
--[[
"moreblocks:slope_stone"
"moreblocks:stair_stone"
"default:stone"
--]]
end
function Context:slope(dir)
local ctx = self:clone()
ctx.node.param2 = slope_param2[format_pos(dir)]
return ctx
end
function Context:set_node()
print("set_node", dump(self))
local tnode = mtscad.transform_node(self.node, self.rotation)
minetest.set_node(self.pos, tnode)
return self
end
function Context:cube(x, y, z)
local pos2 = vector.add(self.pos, {x=x-1, y=y-1, z=z-1})
for xi=self.pos.x,pos2.x do
for yi=self.pos.y,pos2.y do
for zi=self.pos.z,pos2.z do
local ipos = { x=xi, y=yi, z=zi }
local tpos = mtscad.transform_pos(self.pos, ipos, self.rotation)
local tnode = mtscad.transform_node(self.node, self.rotation)
minetest.set_node(tpos, tnode)
end
end
end
return self
end
local function add_rotation(a, b)
-- TODO: only 90°-increments allowed
a = a or 0
b = b or 0
local sum = a + b
while sum >= 360 do
sum = sum - 360
end
return sum
end
function Context:rotate(x, y, z)
local ctx = self:clone()
ctx.rotation.x = add_rotation(ctx.rotation.x, x)
ctx.rotation.y = add_rotation(ctx.rotation.y, y)
ctx.rotation.z = add_rotation(ctx.rotation.z, z)
return ctx
end
-- copy the current context
function Context:clone()
return mtscad.create_context(self.pos, self.rotation, self.node)
end
-- create a new context with given (optional) params
function mtscad.create_context(pos, rotation, node)
local self = {
pos = pos and vector.copy(pos) or vector.zero(),
rotation = rotation and vector.copy(rotation) or vector.zero(),
node = node or { name="air" }
}
return setmetatable(self, Context_mt)
end

17
context/basics.lua Normal file
View File

@ -0,0 +1,17 @@
function mtscad.Context:with(node)
self.node = node
return self
end
function mtscad.Context:execute(fn)
fn(self)
return self
end
function mtscad.Context:set_node()
local tnode = mtscad.transform_node(self.node, self.rotation)
minetest.set_node(self.pos, tnode)
return self
end

15
context/cube.lua Normal file
View File

@ -0,0 +1,15 @@
function mtscad.Context:cube(x, y, z)
local pos2 = vector.add(self.pos, {x=x-1, y=y-1, z=z-1})
for xi=self.pos.x,pos2.x do
for yi=self.pos.y,pos2.y do
for zi=self.pos.z,pos2.z do
local ipos = { x=xi, y=yi, z=zi }
local tpos = mtscad.transform_pos(self.pos, ipos, self.rotation)
local tnode = mtscad.transform_node(self.node, self.rotation)
minetest.set_node(tpos, tnode)
end
end
end
return self
end

17
context/new.lua Normal file
View File

@ -0,0 +1,17 @@
mtscad.Context = {}
local Context_mt = { __index = mtscad.Context }
-- copy the current context
function mtscad.Context:clone()
return mtscad.create_context(self.pos, self.rotation, self.node)
end
-- create a new context with given (optional) params
function mtscad.create_context(pos, rotation, node)
local self = {
pos = pos and vector.copy(pos) or vector.zero(),
rotation = rotation and vector.copy(rotation) or vector.zero(),
node = node or { name="air" }
}
return setmetatable(self, Context_mt)
end

19
context/rotate.lua Normal file
View File

@ -0,0 +1,19 @@
local function add_rotation(a, b)
-- TODO: only 90°-increments allowed
a = a or 0
b = b or 0
local sum = a + b
while sum >= 360 do
sum = sum - 360
end
return sum
end
function mtscad.Context:rotate(x, y, z)
local ctx = self:clone()
ctx.rotation.x = add_rotation(ctx.rotation.x, x)
ctx.rotation.y = add_rotation(ctx.rotation.y, y)
ctx.rotation.z = add_rotation(ctx.rotation.z, z)
return ctx
end

31
context/slope.lua Normal file
View File

@ -0,0 +1,31 @@
local slope_param2 = {
["1,1,0"] = 3,
["1,-1,0"] = 21,
["0,1,1"] = 2,
["0,-1,1"] = 22,
["-1,1,0"] = 1,
["-1,-1,0"] = 23,
["0,1,-1"] = 0,
["0,-1,-1"] = 20
}
local function format_pos(p)
return p.x .. "," .. p.y .. "," .. p.z
end
local function get_stairsplus_nodename(name, stairtype)
--[[
"moreblocks:slope_stone"
"moreblocks:stair_stone"
"default:stone"
--]]
end
function mtscad.Context:slope(dir)
local ctx = self:clone()
ctx.node.param2 = slope_param2[format_pos(dir)]
return ctx
end

14
context/translate.lua Normal file
View File

@ -0,0 +1,14 @@
function mtscad.Context:translate(x, y, z)
-- offset position
local opos = {
x = x or 0,
y = y or 0,
z = z or 0
}
-- transformed position
local tpos = mtscad.transform_pos(self.pos, vector.add(opos, self.pos), self.rotation)
local ctx = self:clone()
ctx.pos = tpos
return ctx
end

View File

@ -2,6 +2,11 @@
mtscad = {}
local MP = minetest.get_modpath("mtscad")
dofile(MP .. "/api.lua")
dofile(MP .. "/context/new.lua")
dofile(MP .. "/context/basics.lua")
dofile(MP .. "/context/translate.lua")
dofile(MP .. "/context/rotate.lua")
dofile(MP .. "/context/cube.lua")
dofile(MP .. "/context/slope.lua")
dofile(MP .. "/util.lua")
dofile(MP .. "/test.lua")