/ bjvxlx_computers / computer.lua
computer.lua
  1  local mod = bjvxlx.computers
  2  local S = mod.translate
  3  
  4  minetest.register_node('bjvxlx_computers:computer_off', {
  5      description = S("Computer"),
  6      _doc_items_longdesc = S(
  7          "A computer is a programmable data processing machine."
  8      ),
  9      drawtype = 'mesh',
 10      mesh = "bjvxlx_computers_computer.obj",
 11      tiles = {"bjvxlx_computers_computer_off.png"},
 12      paramtype2 = '4dir',
 13      sounds = mcl_sounds.node_sound_metal_defaults(),
 14      stack_max = 1,
 15      is_ground_content = false,
 16      groups = {pickaxey = 2, mesecon_effector_off = 1, mesecon = 2},
 17      mesecons = {effector = {
 18          rules = mesecon.rules.alldirs,
 19          action_on = function (pos)
 20              minetest.swap_node(pos, {
 21                  name = 'bjvxlx_computers:computer_on',
 22                  param2 = minetest.get_node(pos).param2
 23              })
 24          end
 25      }},
 26      on_construct = function (pos)
 27          local meta = minetest.get_meta(pos)
 28          local inv = meta:get_inventory()
 29          inv:set_size('bootdisk', 1)
 30          inv:set_width('bootdisk', 1)
 31          inv:set_size('datadisks', 2)
 32          inv:set_width('datadisks', 2)
 33          meta:set_string('formspec', table.concat({
 34              "formspec_version[7]",
 35              "size[11,8]",
 36              "style_type[list;size=1,1;spacing=0,0]",
 37              "style_type[label;font=mono,bold]",
 38              "label[1,0.5;BOOT]",
 39              "label[3,0.5;DATA]",
 40              "image[1,1;1,1;mcl_formspec_itemslot.png;]",
 41              "image[3,1;1,1;mcl_formspec_itemslot.png;]",
 42              "image[4,1;1,1;mcl_formspec_itemslot.png;]",
 43              "list[context;bootdisk;1,1;1,1;]",
 44              "list[context;datadisks;3,1;2,1;]",
 45              "list[current_player;main;1,3;9,4;]"
 46          }, ''))
 47      end,
 48      on_destruct = function (pos)
 49          mod.pc_eject(pos)
 50      end
 51  })
 52  
 53  minetest.register_node('bjvxlx_computers:computer_on', {
 54      description = S("Computer"),
 55      _doc_items_longdesc = S(
 56          "A computer is a programmable data processing machine."
 57      ),
 58      drawtype = 'mesh',
 59      mesh = "bjvxlx_computers_computer.obj",
 60      tiles = {"bjvxlx_computers_computer_on.png"},
 61      paramtype2 = '4dir',
 62      sounds = mcl_sounds.node_sound_metal_defaults(),
 63      stack_max = 1,
 64      is_ground_content = false,
 65      groups = {pickaxey = 2, not_in_creative_inventory = 1,
 66          mesecon_effector_on = 1, mesecon = 2},
 67      mesecons = {effector = {
 68          rules = mesecon.rules.alldirs,
 69          action_off = function (pos)
 70              mod.pc_shutdown(pos)
 71              minetest.swap_node(pos, {
 72                  name = 'bjvxlx_computers:computer_off',
 73                  param2 = minetest.get_node(pos).param2
 74              })
 75          end
 76      }},
 77      on_destruct = function (pos)
 78          mod.pc_shutdown(pos)
 79          mod.pc_eject(pos)
 80      end,
 81      on_rightclick = function (pos, node, clicker)
 82          if clicker:is_player() then
 83              mod.pc_wakeup(pos, clicker)
 84          end
 85      end
 86  })
 87  
 88  bjvxlx.engineering.register_blueprint({
 89      output = 'bjvxlx_computers:computer_off',
 90      recipe = {
 91          {'bjvxlx_computers:monitor',
 92              'bjvxlx_computers:bronze_block',
 93              'bjvxlx_bronze:bronze_block'},
 94          {'bjvxlx_computers:keyboard',
 95              'bjvxlx_computers:ram',
 96              'bjvxlx_bronze:bronze_block'},
 97          {'bjvxlx_bronze:bronze_block',
 98              'bjvxlx_computers:cpu',
 99              'bjvxlx_bronze:bronze_block'}
100      },
101      tool = 'screwdriver:screwdriver'
102  })
103  
104  function mod.pc_wakeup(pos, player)
105      bjvxlx.text_shell.show_session(
106          player:get_player_name(),
107          'bjvxlx_computers:mineos',
108          {pos = vector.new(pos.x, pos.y, pos.z)}
109      )
110  end
111  
112  function mod.pc_shutdown(pos)
113      for player_name in pairs(bjvxlx.text_shell.active_sessions) do
114          bjvxlx.text_shell.stop_session(
115              player_name,
116              'bjvxlx_computers:mineos',
117              {pos = vector.new(pos.x, pos.y, pos.z)}
118          )
119      end
120  end
121  
122  function mod.pc_eject(pos)
123      local stacks = {}
124      local inv = minetest.get_meta(pos):get_inventory()
125      table.insert(stacks, inv:get_stack('bootdisk', 1))
126      inv:set_stack('bootdisk', 1, ItemStack())
127      for n = 1, inv:get_size('datadisks') do
128          table.insert(stacks, inv:get_stack('datadisks', n))
129          inv:set_stack('datadisks', n, ItemStack())
130      end
131      for _, stack in ipairs(stacks) do
132          if not stack:is_empty() then
133              minetest.add_item(pos, stack)
134          end
135      end
136  end