Zefram
2014-07-03 636b0f20df4fef3eef821c23ccc2f606828f5c6f
technic/machines/other/constructor.lua
@@ -1,6 +1,77 @@
local S = technic.getter
local function deploy_node(inv, slot_name, pos1, node1, node)
   if node1.name == "air" then
      if not inv:is_empty(slot_name) then
         stack1=inv:get_list(slot_name)
         local def = stack1[1]:get_definition()
         if def.type == "node" then
            node_to_be_placed={name=stack1[1]:get_name(), param1=0, param2=node.param2}
            minetest.set_node(pos1,node_to_be_placed)
            stack1[1]:take_item()
            inv:set_stack(slot_name, 1, stack1[1])
         elseif def.type == "craft" then
            if def.on_place then
               -- print("deploy_node: item has on_place. trying...")
               local ok, stk = pcall(def.on_place, stack1[1], nil, {
                  -- Fake pointed_thing
                  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
      return
   end
   if node1.name == "ignore" or
      node1.name == "default:lava_source" or
      node1.name == "default:lava_flowing" or
      node1.name == "default:water_source" or
      node1.name == "default:water_flowing"
      then return end
   if inv:room_for_item(slot_name,node1) then
      local def = minetest.registered_nodes[node1.name]
      if not def then return end
      local drop = def.drop or node1.name
      if type(drop) == "table" then
         local pr = PseudoRandom(math.random())
         local c = 0
         local loop = 0 -- Prevent infinite loop
         while (c < (drop.max_items or 1)) and (loop < 1000) do
            local i = math.floor(pr:next(1, #drop.items))
            if pr:next(1, drop.items[i].rarity or 1) == 1 then
               for _,item in ipairs(drop.items[i].items) do
                  inv:add_item(slot_name,item)
               end
               c = c + 1
            end
            loop = loop + 1
         end
         minetest.remove_node(pos1)
      elseif type(drop) == "string" then
         inv:add_item(slot_name,drop)
         minetest.remove_node(pos1)
      end
   end
end
minetest.register_craft({
   type = "shapeless",
   output = 'technic:constructor_mk1_off 1',
@@ -63,7 +134,7 @@
   meta:set_string("formspec",
            "invsize[8,9;]"..
            "label[0,0;"..S("Constructor Mk%d"):format(1).."]"..
            "label[5,0;Slot 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))
@@ -82,10 +153,11 @@
})
minetest.register_node("technic:constructor_mk1_on", {
   description = S("Constructor Mk%d"):format(3),
   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(),
@@ -144,9 +216,9 @@
   meta:set_string("formspec",
            "invsize[8,9;]"..
            "label[0,0;"..S("Constructor Mk%d"):format(2).."]"..
            "label[5,0;Slot 1]"..
            "label[5,0;"..S("Slot %d"):format(1).."]"..
            "list[current_name;slot1;6,0;1,1;]"..
            "label[5,1;Slot 2]"..
            "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))
@@ -171,6 +243,7 @@
         "technic_constructor_back.png","technic_constructor_front_on.png"},
   is_ground_content = true,
   paramtype2 = "facedir",
   drop = "technic:constructor_mk2_off",
   groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon = 2, not_in_creative_inventory=1},
   mesecons = {effector={action_off=mk2_off}},
   sounds = default.node_sound_stone_defaults(),
@@ -245,13 +318,13 @@
   meta:set_string("formspec",
            "invsize[8,9;]"..
            "label[0,0;"..S("Constructor Mk%d"):format(3).."]"..
            "label[5,0;Slot 1]"..
            "label[5,0;"..S("Slot %d"):format(1).."]"..
            "list[current_name;slot1;6,0;1,1;]"..
            "label[5,1;Slot 2]"..
            "label[5,1;"..S("Slot %d"):format(2).."]"..
            "list[current_name;slot2;6,1;1,1;]"..
            "label[5,2;Slot 3]"..
            "label[5,2;"..S("Slot %d"):format(3).."]"..
            "list[current_name;slot3;6,2;1,1;]"..
            "label[5,3;Slot 4]"..
            "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))
@@ -280,6 +353,7 @@
         "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(),
@@ -288,74 +362,3 @@
   allow_metadata_inventory_move = technic.machine_inventory_move,
})
local function deploy_node(inv, slot_name, pos1, node1, node)
   if node1.name == "air" then
      if not inv:is_empty(slot_name) then
         stack1=inv:get_list(slot_name)
         local def = stack1[1]:get_definition()
         if def.type == "node" then
            node_to_be_placed={name=stack1[1]:get_name(), param1=0, param2=node.param2}
            minetest.set_node(pos1,node_to_be_placed)
            stack1[1]:take_item()
            inv:set_stack(slot_name, 1, stack1[1])
         elseif def.type == "craft" then
            if def.on_place then
               -- print("deploy_node: item has on_place. trying...")
               local ok, stk = pcall(def.on_place, stack1[1], nil, {
                  -- Fake pointed_thing
                  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
      return
   end
   if node1.name == "ignore" or
      node1.name == "default:lava_source" or
      node1.name == "default:lava_flowing" or
      node1.name == "default:water_source" or
      node1.name == "default:water_flowing"
      then return end
   if inv:room_for_item(slot_name,node1) then
      local def = minetest.registered_nodes[node1.name]
      if not def then return end
      local drop = def.drop or node1.name
      if type(drop) == "table" then
         local pr = PseudoRandom(math.random())
         local c = 0
         local loop = 0 -- Prevent infinite loop
         while (c < (drop.max_items or 1)) and (loop < 1000) do
            local i = math.floor(pr:next(1, #drop.items))
            if pr:next(1, drop.items[i].rarity or 1) == 1 then
               for _,item in ipairs(drop.items[i].items) do
                  inv:add_item(slot_name,item)
               end
               c = c + 1
            end
            loop = loop + 1
         end
         minetest.remove_node(pos1)
      elseif type(drop) == "string" then
         inv:add_item(slot_name,drop)
         minetest.remove_node(pos1)
      end
   end
end