Advertisement
Guest User

Untitled

a guest
Jun 29th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.59 KB | None | 0 0
  1. uranium.reactor_inactive_formspec =
  2.     "invsize[8,9;]"..
  3.     "image[2,2;1,1;default_furnace_fire_bg.png]"..
  4.     "list[current_name;fuel;2,3;1,1;]"..
  5.     "list[current_name;src;2,1;1,1;]"..
  6.     "list[current_name;dst;5,1;2,2;]"..
  7.     "list[current_player;main;0,5;8,4;]"
  8.  
  9. minetest.register_node("uranium:reactor", {
  10.     description = "Reactor",
  11.     tiles = {"default_furnace_top.png", "default_furnace_bottom.png", "default_furnace_side.png",
  12.         "default_furnace_side.png", "default_furnace_side.png", "default_furnace_front.png"},
  13.     paramtype2 = "facedir",
  14.     groups = {cracky=2},
  15.     sounds = default.node_sound_stone_defaults(),
  16.     on_construct = function(pos)
  17.         local meta = minetest.env:get_meta(pos)
  18.         meta:set_string("formspec", uranium.reactor_inactive_formspec)
  19.         meta:set_string("infotext", "Reactor")
  20.         local inv = meta:get_inventory()
  21.         inv:set_size("fuel", 1)
  22.         inv:set_size("src", 1)
  23.         inv:set_size("dst", 4)
  24.     end,
  25.     can_dig = function(pos,player)
  26.         local meta = minetest.env:get_meta(pos);
  27.         local inv = meta:get_inventory()
  28.         if not inv:is_empty("fuel") then
  29.             return false
  30.         elseif not inv:is_empty("dst") then
  31.             return false
  32.         elseif not inv:is_empty("src") then
  33.             return false
  34.         end
  35.         return true
  36.     end,
  37. })
  38.  
  39. minetest.register_node("uranium:reactor_active", {
  40.     description = "Reactor",
  41.     tiles = {"default_furnace_top.png", "default_furnace_bottom.png", "default_furnace_side.png",
  42.         "default_furnace_side.png", "default_furnace_side.png", "default_furnace_front_active.png"},
  43.     paramtype2 = "facedir",
  44.     light_source = 8,
  45.     drop = "uranium:reactor",
  46.     groups = {cracky=2},
  47.     sounds = default.node_sound_stone_defaults(),
  48.     on_construct = function(pos)
  49.         local meta = minetest.env:get_meta(pos)
  50.         meta:set_string("formspec", uranium.reactor_inactive_formspec)
  51.         meta:set_string("infotext", "Furnace");
  52.         local inv = meta:get_inventory()
  53.         inv:set_size("fuel", 1)
  54.         inv:set_size("src", 1)
  55.         inv:set_size("dst", 4)
  56.     end,
  57.     can_dig = function(pos,player)
  58.         local meta = minetest.env:get_meta(pos);
  59.         local inv = meta:get_inventory()
  60.         if not inv:is_empty("fuel") then
  61.             return false
  62.         elseif not inv:is_empty("dst") then
  63.             return false
  64.         elseif not inv:is_empty("src") then
  65.             return false
  66.         end
  67.         return true
  68.     end,
  69. })
  70.  
  71. function hacky_swap_node(pos,name)
  72.     local node = minetest.env:get_node(pos)
  73.     local meta = minetest.env:get_meta(pos)
  74.     local meta0 = meta:to_table()
  75.     if node.name == name then
  76.         return
  77.     end
  78.     node.name = name
  79.     local meta0 = meta:to_table()
  80.     minetest.env:set_node(pos,node)
  81.     meta = minetest.env:get_meta(pos)
  82.     meta:from_table(meta0)
  83. end
  84.  
  85. minetest.register_abm({
  86.     nodenames = {"uranium:reactor","uranium:reactor_active"},
  87.     interval = 1.0,
  88.     chance = 1,
  89.     action = function(pos, node, active_object_count, active_object_count_wider)
  90.         local meta = minetest.env:get_meta(pos)
  91.         for i, name in ipairs({
  92.                 "fuel_totaltime",
  93.                 "fuel_time",
  94.                 "src_totaltime",
  95.                 "src_time"
  96.         }) do
  97.             if meta:get_string(name) == "" then
  98.                 meta:set_float(name, 0.0)
  99.             end
  100.         end
  101.  
  102.         local inv = meta:get_inventory()
  103.  
  104.         local srclist = inv:get_list("src")
  105.         local cooked = nil
  106.        
  107.         if srclist then
  108.             cooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  109.         end
  110.        
  111.         local was_active = false
  112.        
  113.         if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
  114.             was_active = true
  115.             meta:set_float("fuel_time", meta:get_float("fuel_time") + 1)
  116.             meta:set_float("src_time", meta:get_float("src_time") + 1)
  117.             if cooked and cooked.item and meta:get_float("src_time") >= cooked.time then
  118.                 -- check if there's room for output in "dst" list
  119.                 if inv:room_for_item("dst",cooked.item) then
  120.                     -- Put result in "dst" list
  121.                     inv:add_item("dst", cooked.item)
  122.                     -- take stuff from "src" list
  123.                     srcstack = inv:get_stack("src", 1)
  124.                     srcstack:take_item()
  125.                     inv:set_stack("src", 1, srcstack)
  126.                 else
  127.                     print("Could not insert '"..cooked.item.."'")
  128.                 end
  129.                 meta:set_string("src_time", 0)
  130.             end
  131.         end
  132.        
  133.         if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
  134.             local percent = math.floor(meta:get_float("fuel_time") /
  135.                     meta:get_float("fuel_totaltime") * 100)
  136.             meta:set_string("infotext","Reactor active: "..percent.."%")
  137.             hacky_swap_node(pos,"uranium:reactor_active")
  138.             meta:set_string("formspec",
  139.                 "invsize[8,9;]"..
  140.                 "image[2,2;1,1;default_furnace_fire_bg.png^[lowpart:"..
  141.                         (100-percent)..":default_furnace_fire_fg.png]"..
  142.                 "list[current_name;fuel;2,3;1,1;]"..
  143.                 "list[current_name;src;2,1;1,1;]"..
  144.                 "list[current_name;dst;5,1;2,2;]"..
  145.                 "list[current_player;main;0,5;8,4;]")
  146.             return
  147.         end
  148.  
  149.         local fuel = nil
  150.         local cooked = nil
  151.         local fuellist = inv:get_list("fuel")
  152.         local srclist = inv:get_list("src")
  153.        
  154.         if srclist then
  155.             cooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  156.         end
  157.         if fuellist then
  158.             fuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  159.         end
  160.  
  161.         if fuel.time <= 0 then
  162.             meta:set_string("infotext","Reactor out of fuel")
  163.             hacky_swap_node(pos,"uranium:reactor")
  164.             meta:set_string("formspec", uranium.reactor_inactive_formspec)
  165.             return
  166.         end
  167.  
  168.         if cooked.item:is_empty() then
  169.             if was_active then
  170.                 meta:set_string("infotext","Reactor is empty")
  171.                 hacky_swap_node(pos,"uranium:reactor")
  172.                 meta:set_string("formspec", uranium.reactor_inactive_formspec)
  173.             end
  174.             return
  175.         end
  176.  
  177.         meta:set_string("fuel_totaltime", fuel.time)
  178.         meta:set_string("fuel_time", 0)
  179.        
  180.         local stack = inv:get_stack("fuel", 1)
  181.         stack:take_item()
  182.         inv:set_stack("fuel", 1, stack)
  183.     end,
  184. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement