Advertisement
Guest User

reactor_display

a guest
Apr 20th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. -- clear the terminal
  2. term.clear()
  3. term.setCursorPos(1, 1)
  4.  
  5. rednet.open("back")
  6.  
  7. function printTable(table, offset)
  8.   offset = offset or 0
  9.   if (type(table) ~= "table") then
  10.     print("Not a table")
  11.     return
  12.   end
  13.   print(string.rep(" ", offset) .. "{")
  14.   for key, value in pairs(table) do
  15.     if(type(value) == "table") then
  16.       write(string.rep(" ", offset + 1) .. key .. " => ")
  17.       printTable(value, offset + 1)
  18.     else
  19.       print(string.rep(" ", offset + 1) .. key .. " => " .. value .. ",")
  20.     end
  21.   end
  22.   print(string.rep(" ", offset) .. "}")
  23. end
  24.  
  25. dofile("reactor.lua")
  26.  
  27. -- load the config file
  28. config_file = io.open("config", "r")
  29. local config = textutils.unserialize(config_file:read("*a"))
  30. config_file:close()
  31.  
  32. monitor = peripheral.wrap(config["monitor_side"])
  33. monitor.clear()
  34. monitor.setTextScale(1.5)
  35. local monitor_width, monitor_height = monitor.getSize()
  36.  
  37. local offsets = {
  38.   { ["xoffset"] = 0, ["yoffset"] = 0 },
  39.   { ["xoffset"] = 25, ["yoffset"] = 0 },
  40.   { ["xoffset"] = 0, ["yoffset"] = 9 },
  41.   { ["xoffset"] = 25, ["yoffset"] = 9 },
  42. }
  43.  
  44. -- create reactors
  45. local reactors = {}
  46. for i=1,4 do
  47.   reactors[i] = Reactor(config["reactors"][i]["name"], config["reactors"][i]["id"], offsets[i]["xoffset"], offsets[i]["yoffset"])
  48. end
  49.  
  50.  
  51. -- draw the dividers
  52. function drawDividers()
  53.   -- set colors
  54.   monitor.setBackgroundColor(colors.black)
  55.   monitor.setTextColor(colors.white)
  56.  
  57.   -- draw vertical dividers
  58.   for i = 1, monitor_height do
  59.     monitor.setCursorPos(24, i)
  60.     monitor.write("||")
  61.   end
  62.  
  63.   -- draw horizontal divider
  64.   for i = 1, monitor_width do
  65.     monitor.setCursorPos(i, 9)
  66.     monitor.write("-")
  67.   end
  68.  
  69.   -- make the intersection look pretty
  70.   monitor.setCursorPos(24, 9)
  71.   monitor.write("++")
  72. end
  73.  
  74. -- Draws the display
  75. function drawDisplay()
  76.   parallel.waitForAll(reactors[1].update, reactors[2].update, reactors[3].update, reactors[4].update)
  77.   for num, reactor in pairs(reactors) do
  78.     reactor.display(monitor)
  79.   end
  80. end
  81.  
  82. -- sleep
  83. function doSleep()
  84.   os.sleep(config["sleep_time"])
  85. end
  86.  
  87. -- wait for a touch on the monitor
  88. function doTouchEvent()
  89.   local event, side, xCoord, yCoord = os.pullEvent("monitor_touch")
  90.   for num, reactor in pairs(reactors) do
  91.     reactor.handleTouch(xCoord, yCoord)
  92.   end
  93. end
  94.  
  95. -- main run loop
  96. drawDividers()
  97. while true do
  98.   drawDisplay()
  99.   parallel.waitForAny(doTouchEvent, doSleep)
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement