Clean up constructor

This commit is contained in:
ShadowNinja 2014-07-21 01:52:13 -04:00
parent 7d5edcf23e
commit 14b30bfd0d

View File

@ -1,75 +1,67 @@
local S = technic.getter local S = technic.getter
local function deploy_node(inv, slot_name, pos1, node1, node) local function deploy_node(inv, slot_name, pos, node, machine_node)
if node1.name == "air" then if node.name ~= "air" then
if not inv:is_empty(slot_name) then if node.name == "ignore" or
stack1=inv:get_list(slot_name) node.name == "default:lava_source" or
local def = stack1[1]:get_definition() node.name == "default:lava_flowing" or
if def.type == "node" then node.name == "default:water_source" or
node_to_be_placed={name=stack1[1]:get_name(), param1=0, param2=node.param2} node.name == "default:water_flowing" then
minetest.set_node(pos1,node_to_be_placed) return
stack1[1]:take_item() end
inv:set_stack(slot_name, 1, stack1[1]) local drops = minetest.get_node_drops(node.name, "")
elseif def.type == "craft" then local remove_to = false
if def.on_place then for i, item in ipairs(drops) do
-- print("deploy_node: item has on_place. trying...") if not inv:room_for_item(slot_name, item) then
local ok, stk = pcall(def.on_place, stack1[1], nil, { remove_to = i - 1
-- Fake pointed_thing break
type = "node",
above = pos1,
under = { x=pos1.x, y=pos1.y-1, z=pos1.z },
})
if ok then
-- print("deploy_node: on_place succeeded!")
inv:set_stack(slot_name, 1, stk or stack1[1])
return
-- else
-- print("deploy_node: WARNING: error while running on_place: "..tostring(stk))
end
end
minetest.item_place_object(stack1[1], nil, {
-- Fake pointed_thing
type = "node",
above = pos1,
under = pos1,
})
inv:set_stack(slot_name, 1, nil)
end end
inv:add_item(slot_name, item)
end
if remove_to then
for i = 1, remove_to do
inv:remove_item(drops[i])
end
else
minetest.remove_node(pos)
end end
return return
end end
if node1.name == "ignore" or if not inv:is_empty(slot_name) then
node1.name == "default:lava_source" or local stack = inv:get_list(slot_name)[1]
node1.name == "default:lava_flowing" or local def = stack:get_definition()
node1.name == "default:water_source" or if def.type == "node" then
node1.name == "default:water_flowing" minetest.set_node(pos, {
then return end name = stack:get_name(),
if inv:room_for_item(slot_name,node1) then param2 = machine_node.param2
local def = minetest.registered_nodes[node1.name] })
if not def then return end stack:take_item()
local drop = def.drop or node1.name inv:set_stack(slot_name, 1, stack)
if type(drop) == "table" then elseif def.type == "craft" then
local pr = PseudoRandom(math.random()) if def.on_place then
local c = 0 -- Use pcall to avoid nil placer errors.
local loop = 0 -- Prevent infinite loop -- TODO: Do without pcall.
while (c < (drop.max_items or 1)) and (loop < 1000) do local ok, stk = pcall(def.on_place, stack, nil, {
local i = math.floor(pr:next(1, #drop.items)) -- Fake pointed_thing
if pr:next(1, drop.items[i].rarity or 1) == 1 then type = "node",
for _,item in ipairs(drop.items[i].items) do above = pos,
inv:add_item(slot_name,item) under = {x=pos.x, y=pos.y-1, z=pos.z},
end })
c = c + 1 if ok then
inv:set_stack(slot_name, 1, stk or stack)
return
end end
loop = loop + 1
end end
minetest.remove_node(pos1) minetest.item_place_object(stack, nil, {
elseif type(drop) == "string" then -- Fake pointed_thing
inv:add_item(slot_name,drop) type = "node",
minetest.remove_node(pos1) above = pos,
under = pos,
})
inv:set_stack(slot_name, 1, nil)
end end
end end
end end
minetest.register_craft({ minetest.register_craft({
@ -92,273 +84,106 @@ minetest.register_craft({
}) })
local function mk1_on(pos, node) local function make_on(mark, length)
local meta = minetest.get_meta(pos) return function(pos, node)
local inv = meta:get_inventory()
local pos1={}
pos1.x=pos.x
pos1.y=pos.y
pos1.z=pos.z
if node.param2==3 then pos1.x=pos1.x+1 end
if node.param2==2 then pos1.z=pos1.z+1 end
if node.param2==1 then pos1.x=pos1.x-1 end
if node.param2==0 then pos1.z=pos1.z-1 end
if node.name == "technic:constructor_mk1_off" then
technic.swap_node(pos,"technic:constructor_mk1_on")
nodeupdate(pos)
local node1=minetest.get_node(pos1)
deploy_node (inv,"slot1",pos1,node1,node)
end
end
local function mk1_off(pos, node)
if node.name == "technic:constructor_mk1_on" then
technic.swap_node(pos,"technic:constructor_mk1_off")
nodeupdate(pos)
end
end
minetest.register_node("technic:constructor_mk1_off", {
description = S("Constructor Mk%d"):format(1),
tile_images = {"technic_constructor_mk1_top_off.png","technic_constructor_mk1_bottom_off.png","technic_constructor_mk1_side2_off.png","technic_constructor_mk1_side1_off.png",
"technic_constructor_back.png","technic_constructor_front_off.png"},
is_ground_content = true,
paramtype2 = "facedir",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon_receptor_off = 1, mesecon_effector_off = 1, mesecon = 2},
mesecons= {effector={action_on=mk1_on}},
sounds = default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec",
"invsize[8,9;]"..
"label[0,0;"..S("Constructor Mk%d"):format(1).."]"..
"label[5,0;"..S("Slot %d"):format(1).."]"..
"list[current_name;slot1;6,0;1,1;]"..
"list[current_player;main;0,5;8,4;]")
meta:set_string("infotext", S("Constructor Mk%d"):format(1))
local inv = meta:get_inventory()
inv:set_size("slot1", 1)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
return inv:is_empty("slot1") local dir = vector.new()
end, if node.param2 == 3 then dir.x = 1 end
allow_metadata_inventory_put = technic.machine_inventory_put, if node.param2 == 2 then dir.z = 1 end
allow_metadata_inventory_take = technic.machine_inventory_take, if node.param2 == 1 then dir.x = -1 end
allow_metadata_inventory_move = technic.machine_inventory_move, if node.param2 == 0 then dir.z = -1 end
})
minetest.register_node("technic:constructor_mk1_on", { local place_pos = vector.new(pos)
description = S("Constructor Mk%d"):format(1),
tile_images = {"technic_constructor_mk1_top_on.png","technic_constructor_mk1_bottom_on.png","technic_constructor_mk1_side2_on.png","technic_constructor_mk1_side1_on.png",
"technic_constructor_back.png","technic_constructor_front_on.png"},
paramtype2 = "facedir",
drop = "technic:constructor_mk1_off",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,mesecon = 2,not_in_creative_inventory=1},
mesecons= {effector={action_off=mk1_off}},
sounds = default.node_sound_stone_defaults(),
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
})
if node.name == "technic:constructor_mk"..mark.."_off" then
--Constructor Mk2 technic.swap_node(pos, "technic:constructor_mk"..mark.."_on")
nodeupdate(pos)
local function mk2_on(pos, node) for i = 1, length do
local meta = minetest.get_meta(pos) place_pos = vector.add(place_pos, dir)
local inv = meta:get_inventory() local place_node = minetest.get_node(place_pos)
local pos1={} deploy_node(inv, "slot"..i, place_pos, place_node, node)
local pos2={} end
pos1.x=pos.x end
pos1.y=pos.y
pos1.z=pos.z
pos2.x=pos.x
pos2.y=pos.y
pos2.z=pos.z
if node.param2==3 then pos1.x=pos1.x+1 pos2.x=pos2.x+2 end
if node.param2==2 then pos1.z=pos1.z+1 pos2.z=pos2.z+2 end
if node.param2==1 then pos1.x=pos1.x-1 pos2.x=pos2.x-2 end
if node.param2==0 then pos1.z=pos1.z-1 pos2.z=pos2.z-2 end
if node.name == "technic:constructor_mk2_off" then
technic.swap_node(pos,"technic:constructor_mk2_on")
nodeupdate(pos)
local node1=minetest.get_node(pos1)
deploy_node (inv,"slot1",pos1,node1,node)
local node1=minetest.get_node(pos2)
deploy_node (inv,"slot2",pos2,node1,node)
end end
end end
local function mk2_off(pos, node) local function make_off(mark)
if node.name == "technic:constructor_mk2_on" then return function(pos, node)
technic.swap_node(pos,"technic:constructor_mk2_off") if node.name == "technic:constructor_mk"..mark.."_on" then
nodeupdate(pos) technic.swap_node(pos,"technic:constructor_mk"..mark.."_off")
nodeupdate(pos)
end
end end
end end
minetest.register_node("technic:constructor_mk2_off", {
description = S("Constructor Mk%d"):format(2),
tile_images = {"technic_constructor_mk2_top_off.png","technic_constructor_mk2_bottom_off.png","technic_constructor_mk2_side2_off.png","technic_constructor_mk2_side1_off.png",
"technic_constructor_back.png","technic_constructor_front_off.png"},
is_ground_content = true,
paramtype2 = "facedir",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon = 2},
mesecons= {effector={action_on=mk2_on}},
sounds = default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec",
"invsize[8,9;]"..
"label[0,0;"..S("Constructor Mk%d"):format(2).."]"..
"label[5,0;"..S("Slot %d"):format(1).."]"..
"list[current_name;slot1;6,0;1,1;]"..
"label[5,1;"..S("Slot %d"):format(2).."]"..
"list[current_name;slot2;6,1;1,1;]"..
"list[current_player;main;0,5;8,4;]")
meta:set_string("infotext", S("Constructor Mk%d"):format(2))
local inv = meta:get_inventory()
inv:set_size("slot1", 1)
inv:set_size("slot2", 1)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:is_empty("slot1")==false or inv:is_empty("slot2")==false then return false end
return true
end,
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
})
minetest.register_node("technic:constructor_mk2_on", { local function make_constructor(mark, length)
description = S("Constructor Mk%d"):format(2), minetest.register_node("technic:constructor_mk"..mark.."_off", {
tile_images = {"technic_constructor_mk2_top_on.png","technic_constructor_mk2_bottom_on.png","technic_constructor_mk2_side2_on.png","technic_constructor_mk2_side1_on.png", description = S("Constructor Mk%d"):format(mark),
"technic_constructor_back.png","technic_constructor_front_on.png"}, tiles = {"technic_constructor_mk"..mark.."_top_off.png",
is_ground_content = true, "technic_constructor_mk"..mark.."_bottom_off.png",
paramtype2 = "facedir", "technic_constructor_mk"..mark.."_side2_off.png",
drop = "technic:constructor_mk2_off", "technic_constructor_mk"..mark.."_side1_off.png",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon = 2, not_in_creative_inventory=1}, "technic_constructor_back.png",
mesecons = {effector={action_off=mk2_off}}, "technic_constructor_front_off.png"},
sounds = default.node_sound_stone_defaults(), paramtype2 = "facedir",
allow_metadata_inventory_put = technic.machine_inventory_put, groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, mesecon = 2},
allow_metadata_inventory_take = technic.machine_inventory_take, mesecons = {effector = {action_on = make_on(mark, length)}},
allow_metadata_inventory_move = technic.machine_inventory_move, sounds = default.node_sound_stone_defaults(),
}) on_construct = function(pos)
local meta = minetest.get_meta(pos)
local formspec = "size[8,9;]"..
"label[0,0;"..S("Constructor Mk%d"):format(mark).."]"..
"list[current_player;main;0,5;8,4;]"
for i = 1, length do
formspec = formspec
.."label[5,"..(i - 1)..";"..S("Slot %d"):format(i).."]"
.."list[current_name;slot"..i
..";6,"..(i - 1)..";1,1;]"
end
meta:set_string("formspec", formspec)
meta:set_string("infotext", S("Constructor Mk%d"):format(mark))
local inv = meta:get_inventory()
for i = 1, length do
inv:set_size("slot"..i, 1)
end
end,
can_dig = function(pos, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
for i = 1, length do
if not inv:is_empty("slot"..i) then
return false
end
end
return true
end,
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
})
minetest.register_node("technic:constructor_mk"..mark.."_on", {
-- Constructor Mk3 tiles = {"technic_constructor_mk"..mark.."_top_on.png",
local function mk3_on(pos, node) "technic_constructor_mk"..mark.."_bottom_on.png",
local meta = minetest.get_meta(pos) "technic_constructor_mk"..mark.."_side2_on.png",
local inv = meta:get_inventory() "technic_constructor_mk"..mark.."_side1_on.png",
"technic_constructor_back.png",
local pos1={} "technic_constructor_front_on.png"},
local pos2={} paramtype2 = "facedir",
local pos3={} drop = "technic:constructor_mk"..mark.."_off",
local pos4={} groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
mesecon=2, not_in_creative_inventory=1},
pos1.x=pos.x mesecons= {effector = {action_off = make_off(mark)}},
pos1.y=pos.y sounds = default.node_sound_stone_defaults(),
pos1.z=pos.z allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
pos2.x=pos.x allow_metadata_inventory_move = technic.machine_inventory_move,
pos2.y=pos.y })
pos2.z=pos.z
pos3.x=pos.x
pos3.y=pos.y
pos3.z=pos.z
pos4.x=pos.x
pos4.y=pos.y
pos4.z=pos.z
if node.param2==3 then pos1.x=pos1.x+1 pos2.x=pos2.x+2 pos3.x=pos3.x+3 pos4.x=pos4.x+4 end
if node.param2==2 then pos1.z=pos1.z+1 pos2.z=pos2.z+2 pos3.z=pos3.z+3 pos4.z=pos4.z+4 end
if node.param2==1 then pos1.x=pos1.x-1 pos2.x=pos2.x-2 pos3.x=pos3.x-3 pos4.x=pos4.x-4 end
if node.param2==0 then pos1.z=pos1.z-1 pos2.z=pos2.z-2 pos3.z=pos3.z-3 pos4.z=pos4.z-4 end
if node.name == "technic:constructor_mk3_off" then
technic.swap_node(pos,"technic:constructor_mk3_on")
nodeupdate(pos)
local node1=minetest.get_node(pos1)
deploy_node (inv,"slot1",pos1,node1,node)
local node1=minetest.get_node(pos2)
deploy_node (inv,"slot2",pos2,node1,node)
local node1=minetest.get_node(pos3)
deploy_node (inv,"slot3",pos3,node1,node)
local node1=minetest.get_node(pos4)
deploy_node (inv,"slot4",pos4,node1,node)
end
end end
local function mk3_off(pos, node) make_constructor(1, 1)
if node.name == "technic:constructor_mk3_on" then make_constructor(2, 2)
technic.swap_node(pos,"technic:constructor_mk3_off") make_constructor(3, 4)
nodeupdate(pos)
end
end
minetest.register_node("technic:constructor_mk3_off", {
description = S("Constructor Mk%d"):format(3),
tile_images = {"technic_constructor_mk3_top_off.png","technic_constructor_mk3_bottom_off.png","technic_constructor_mk3_side2_off.png","technic_constructor_mk3_side1_off.png",
"technic_constructor_back.png","technic_constructor_front_off.png"},
paramtype2 = "facedir",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon = 2},
mesecons = {effector={action_on=mk3_on}},
sounds = default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec",
"invsize[8,9;]"..
"label[0,0;"..S("Constructor Mk%d"):format(3).."]"..
"label[5,0;"..S("Slot %d"):format(1).."]"..
"list[current_name;slot1;6,0;1,1;]"..
"label[5,1;"..S("Slot %d"):format(2).."]"..
"list[current_name;slot2;6,1;1,1;]"..
"label[5,2;"..S("Slot %d"):format(3).."]"..
"list[current_name;slot3;6,2;1,1;]"..
"label[5,3;"..S("Slot %d"):format(4).."]"..
"list[current_name;slot4;6,3;1,1;]"..
"list[current_player;main;0,5;8,4;]")
meta:set_string("infotext", S("Constructor Mk%d"):format(3))
local inv = meta:get_inventory()
inv:set_size("slot1", 1)
inv:set_size("slot2", 1)
inv:set_size("slot3", 1)
inv:set_size("slot4", 1)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:is_empty("slot1")==false or inv:is_empty("slot2")==false or inv:is_empty("slot3")==false or inv:is_empty("slot4")==false then return false end
return true
end,
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
})
minetest.register_node("technic:constructor_mk3_on", {
description = S("Constructor Mk%d"):format(3),
tile_images = {"technic_constructor_mk3_top_on.png","technic_constructor_mk3_bottom_on.png","technic_constructor_mk3_side2_on.png","technic_constructor_mk3_side1_on.png",
"technic_constructor_back.png","technic_constructor_front_on.png"},
is_ground_content = true,
paramtype2 = "facedir",
drop = "technic:constructor_mk3_off",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon = 2,not_in_creative_inventory=1},
mesecons = {effector={action_off=mk3_off}},
sounds = default.node_sound_stone_defaults(),
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
})