Advertisement
Brekkjern

Untitled

Nov 16th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.21 KB | None | 0 0
  1. -- Side the modem is attached.
  2. -- Valid: "left", "right", "front", "back", "top", "bottom"
  3. local peripheralModem = peripheral.wrap("back")
  4.  
  5. -- Side the monitor is attached
  6. local mon = peripheral.wrap("top")
  7.  
  8. -- Side the rednet cable is attached
  9. local rednetSide = "bottom"
  10.  
  11.  
  12. -- Set up classes
  13.  
  14. -- Batbox object
  15. local batbox = {}
  16.  
  17. batbox.new = function(id)
  18.  
  19.     -- Class local variables
  20.     local self = {}
  21.     self.id = id
  22.     self.name = self.id
  23.  
  24.     -- Getters
  25.     self.getMaxEnergyOutput = function()
  26.         return peripheralModem.callRemote(self.id, "getMaxEnergyOutput")
  27.     end
  28.  
  29.     self.getCapacity = function()
  30.         return peripheralModem.callRemote(self.id, "getCapacity")
  31.     end
  32.  
  33.     self.getStored = function()
  34.         return peripheralModem.callRemote(self.id, "getStored")
  35.     end
  36.  
  37.     self.getOutput = function()
  38.         return peripheralModem.callRemote(self.id, "getOutput")
  39.     end
  40.  
  41.     self.getName = function()
  42.         return self.name
  43.     end
  44.  
  45.     self.getId = function()
  46.         return self.id
  47.     end
  48.  
  49.     -- Setters
  50.     self.setName = function(newName)
  51.         self.name = newName
  52.     end
  53.  
  54.     -- Other methods
  55.     self.getPercent = function()
  56.         return ((self.getStored() / self.getCapacity()) * 100 )
  57.     end
  58.  
  59.     self.show = function()
  60.         mon.write(self.getName())
  61.         local x, y = mon.getCursorPos()
  62.         mon.setCursorPos(4, y + 1)
  63.         mon.write("EU: "..self.getStored().." / "..self.getCapacity()..". "..self.getPercent().."%")
  64.         mon.setCursorPos(1, y + 3)
  65.     end
  66.  
  67.     --#### PREVENT READ AND WRITE ACCESS TO THE RETURNED TABLE
  68.     local mt = self
  69.  
  70.     -- PREVENT WRITE ACCESS AND ABORT APPROPRIATELY
  71.     mt.__newindex = function(table, key, value)
  72.         local msg = string.format("%s %s %s %s %s",
  73.             "Attempt to illegally set Batbox object key",
  74.             tostring(key),
  75.             "to value",
  76.             tostring(value),
  77.             ", aborting...\n\n"
  78.             )
  79.         io.stderr:write(msg)
  80.         os.exit(1)
  81.     end
  82.  
  83.     -- PREVENT READ ACCESS AND ABORT APPROPRIATELY
  84.     mt.__index = function(table, key)
  85.         if type(key) ~= "function" then
  86.         io.stderr:write("Attempt to illegally read attribute " ..
  87.             tostring(key) .. " from Batbox object, aborting...\n\n")
  88.         os.exit(1)
  89.         end
  90.     end
  91.  
  92.     -- WRITE NEW __index AND __newindex TO METATABLE
  93.     setmetatable(self, mt)
  94.  
  95.     return self
  96. end
  97.  
  98. -- Nuclear reactor object
  99. local reactor = {}
  100.  
  101. reactor.new = function(id)
  102.    
  103.     -- Class local variables
  104.     local self = {}
  105.     self.id = id
  106.     self.name = id
  107.  
  108.     -- Getters
  109.     self.getInvName = function()
  110.         return peripheralModem.callRemote(self.id, "getInvName")
  111.     end
  112.  
  113.     self.getStackInSlot = function(slotNumber)
  114.         return peripheralModem.callRemote(self.id, "getStackInSlot", slotNumber)
  115.     end
  116.  
  117.     self.getSizeInventory = function()
  118.         return peripheralModem.callRemote(self.id, "getSizeInventory")
  119.     end
  120.  
  121.     self.getOutput = function()
  122.         return peripheralModem.callRemote(self.id, "getOutput")
  123.     end
  124.  
  125.     self.getHeat = function()
  126.         return peripheralModem.callRemote(self.id, "getHeat")
  127.     end
  128.  
  129.     self.getMaxHeat = function()
  130.         return peripheralModem.callRemote(self.id, "getMaxHeat")
  131.     end
  132.  
  133.     self.getName = function()
  134.         return self.name
  135.     end
  136.  
  137.     self.getId = function()
  138.         return self.id
  139.     end
  140.  
  141.     -- Setters
  142.     self.setName = function(newName)
  143.         self.name = newName
  144.     end
  145.  
  146.     -- Other methods
  147.     self.getHeatPercent = function()
  148.         return ((self.getHeat() / self.getMaxHeat()) *100 )
  149.     end
  150.  
  151.     self.show = function()
  152.         mon.write(self.getName())
  153.         local x, y = mon.getCursorPos()
  154.         mon.setCursorPos(4, y + 1)
  155.         mon.write("Heat: "..self.getHeat().." / "..self.getMaxHeat()..". "..self.getPercent().."%")
  156.         mon.setCursorPos(1, y + 3)
  157.     end
  158.  
  159.     --#### PREVENT READ AND WRITE ACCESS TO THE RETURNED TABLE
  160.     local mt = self
  161.  
  162.     -- PREVENT WRITE ACCESS AND ABORT APPROPRIATELY
  163.     mt.__newindex = function(table, key, value)
  164.         local msg = string.format("%s %s %s %s %s",
  165.             "Attempt to illegally set Reactor object key",
  166.             tostring(key),
  167.             "to value",
  168.             tostring(value),
  169.             ", aborting...\n\n"
  170.             )
  171.         io.stderr:write(msg)
  172.         os.exit(1)
  173.     end
  174.  
  175.     -- PREVENT READ ACCESS AND ABORT APPROPRIATELY
  176.     mt.__index = function(table, key)
  177.         if type(key) ~= "function" then
  178.         io.stderr:write("Attempt to illegally read attribute " ..
  179.             tostring(key) .. " from Reactor object, aborting...\n\n")
  180.         os.exit(1)
  181.         end
  182.     end
  183.  
  184.     -- WRITE NEW __index AND __newindex TO METATABLE
  185.     setmetatable(self, mt)
  186.  
  187.     return self
  188.  
  189. end
  190.  
  191.  
  192. -- Program
  193.  
  194. -- List of the connected peripherals matching the supported types.
  195. local reactors = {}
  196. local batboxes = {}
  197.  
  198. for k, v in pairs(peripheralModem.getNamesRemote()) do
  199.    
  200.     local peripheralType = peripheralModem.getTypeRemote(v)
  201.  
  202.     if peripheralType == "batbox" then
  203.         v = batbox.new(v)
  204.         table.insert(batboxes, v)
  205.     elseif peripheralType == "reactor_chamber" or peripheralType == "reactor" then
  206.         v = reactor.new(v)
  207.         table.insert(reactors, v)
  208.     end
  209. end
  210.  
  211. -- Make the text scale smaller.
  212. mon.setTextScale(0.5)
  213.  
  214.  
  215. -- Main program loop
  216. while true do
  217.  
  218.     mon.clear()
  219.     mon.setCursorPos(1,1)
  220.  
  221.     local totalStored = 0
  222.     local totalCapacity = 0
  223.  
  224.     -- Write out the batbox info
  225.     for k, v in pairs(batboxes) do
  226.  
  227.         v.show()
  228.  
  229.     end
  230.  
  231.     -- mon.write("Total storage percentage: "..((totalStored / totalCapacity) * 100).."%")
  232.  
  233.     -- Write out the reactor info
  234.     for k, v in pairs(reactors) do
  235.  
  236.         v.show()
  237.  
  238.     end
  239.  
  240.     -- Throttle the monitor updates
  241.     sleep(1)
  242. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement