Advertisement
Guest User

reactor lua

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