implement cube and rect

master
rad 2013-03-06 01:20:08 +04:00
parent 37fa5e7aa7
commit 1cd35e9b1c
3 changed files with 65 additions and 50 deletions

View File

@ -30,7 +30,7 @@ do
end,
on_construct = function(pos)
local meta = minetest.env:get_meta(pos)
meta:set_string("command", "wr_utils.print_table(_G)")--"r.move(-1,0,0) r.rect(10)")
meta:set_string("command", "r:jump(0, -1, 0) r:rect(30) r:cube(10)")
meta:set_string("formspec",
"size[8,11]"..
"list[current_name;main;0,0;8,4;]"..
@ -73,13 +73,6 @@ do
}
})
function get_nodedef_field(nodename, fieldname)
if not minetest.registered_nodes[nodename] then
return nil
end
return minetest.registered_nodes[nodename][fieldname]
end
function digg(initial_pos, node, command)
local env = minetest.env
local meta = env:get_meta(initial_pos)
@ -95,36 +88,5 @@ do
setfenv(command_func, context)
command_func()
end
--[[
local step = 1
for command in commands do
print("On step="..step.." do command "..command)
local axis = string.sub(command, 1, 1)
local direction = string.sub(command, 2, 2)
local count_str = string.sub(command, 3)
local count = math.abs(tonumber(count_str))
local increment
print("dir="..direction)
if direction == "-" then
increment = -1
else
increment = 1
end
while count > 0 do
pos[axis] = pos[axis] + increment;
local processed_node = env:get_node_or_nil(pos)
if not processed_node then
print("no node at "..minetest.pos_to_string(pos))
break
end
local drawtype = get_nodedef_field(processed_node.name, "drawtype")
if drawtype == "normal" and not (pos.x == initial_pos.x and pos.y == initial_pos.y and pos.z == initial_pos.z) then
inv:add_item("main", processed_node)
env:remove_node(pos)
end
count = count - 1
end
step = step + 1
end]]--
end
end

View File

@ -10,23 +10,69 @@ this.Router = {}
local Router_mt = {__index = this.Router}
function this.Router.new(pos)
return setmetatable({pos={
x=pos.x,
y=pos.y,
z=pos.z}
}, Router_mt)
local env = minetest.env
local meta = env:get_meta(pos)
t = {
initial_pos = wr_utils.copy_table(pos),
pos = wr_utils.copy_table(pos),
env = env,
meta = meta,
inv = meta:get_inventory()
}
print("new router with pos="..minetest.pos_to_string(t.pos))
return setmetatable(t, Router_mt)
end
function this.Router.jump(pos)
print("jump")
function this.Router:processNode(pos)
local processed_node = self.env:get_node_or_nil(pos)
if not processed_node then
print("no node at "..minetest.pos_to_string(pos))
return
end
local drawtype = wr_utils.get_nodedef_field(processed_node.name, "drawtype")
if drawtype == "normal" and not (pos.x == self.initial_pos.x and pos.y == self.initial_pos.y and pos.z == self.initial_pos.z) then
self.inv:add_item("main", processed_node)
self.env:remove_node(pos)
end
end
function this.Router.move(pos)
function this.Router:jump(x, y, z)
local p = self.pos
print("jump to "..minetest.pos_to_string(p))
p.x = p.x + x
p.y = p.y + y
p.z = p.z + z
end
function this.Router:move(pos)
print("move")
end
function this.Router.rect(pos, r)
print("rect(r="..tostring(r))
function this.Router:rect(r)
local p = wr_utils.copy_table(self.pos)
local halfr = math.floor(r/2)
for i = -halfr, halfr do
for j = -halfr, halfr do
p.x = self.pos.x + i
p.z = self.pos.z + j
self:processNode(p)
end
end
end
function this.Router:cube(r)
local p = wr_utils.copy_table(self.pos)
local halfr = math.floor(r/2)
for i = -halfr, halfr do
for j = -halfr, halfr do
for k = -halfr, halfr do
p.x = self.pos.x + i
p.z = self.pos.z + j
p.y = self.pos.y + k
self:processNode(p)
end
end
end
end
return this

View File

@ -8,10 +8,17 @@ end
function this.print_table(table)
for k,v in pairs(table) do
print(k.."=",tostring(v))
print(k.."="..tostring(v))
end
end
function this.get_nodedef_field(nodename, fieldname)
if not minetest.registered_nodes[nodename] then
return nil
end
return minetest.registered_nodes[nodename][fieldname]
end
return this