Advertisement
Guest User

tankmanager

a guest
Oct 20th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 KB | None | 0 0
  1. engine = peripheral.wrap("bottom") -- engine on bottom
  2. local modem = peripheral.wrap("right") -- modem on the right
  3. local tankTable = engine.getTankInfo("top")
  4. local fuelTankTable = tankTable[1]
  5. local commandchannel = 1337
  6. local inputchannel = 1334
  7. modem.open(inputchannel) -- open input channel
  8. local assignedTank = 999
  9. local bufferTankStatus = 0
  10. local myname = "Compression Block #1"
  11. local validCommands = {"start","stop","update","exit"}
  12. local fuelType = ""
  13. local fuelLevel= 0
  14.  
  15. function requestBufferTankStatus()
  16. modem.transmit(assignedTank, inputchannel, "statusRequest")
  17. end
  18.  
  19. function refreshLocalTankInfo()
  20.   requestBufferTankStatus()
  21.   tankTable = engine.getTankInfo("top")
  22.   fuelTankTable = tankTable[1]
  23.   fuelType = fuelTankTable["name"]
  24.   if fuelType == nil then
  25.   fuelType = "none"
  26.   end
  27.   fuelLevel = fuelTankTable["amount"]
  28.   if fuelLevel == nil then
  29.   fuelLevel = 0
  30.   end
  31. end
  32.  
  33. function engineStart()
  34.   redstone.setOutput("bottom", false)
  35.   redstone.setOutput("back", false)
  36.   modem.transmit(commandchannel,inputchannel,"engine started")
  37.   print("Starting engine..")
  38. end
  39.  
  40. function engineStop()
  41.   redstone.setOutput("bottom", true)
  42.   redstone.setOutput("back", true)
  43.   modem.transmit(commandchannel,inputchannel,"engine halted")
  44.   print("Halting engine..")
  45. end
  46.  
  47. function complistener()
  48.   while true do
  49.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  50.     if message == "statusRequest" and replyChannel == commandchannel then
  51.       refreshLocalTankInfo()
  52.       modem.transmit(commandchannel, inputchannel, tostring(bufferTankStatus)..","..tostring(fuelTankTable["amount"])..","..tostring(engine.getEnergy())..","..tostring(engine.getEnergyPerTick()))
  53.     elseif message == "engine halt" and replychannel == commandchannel then
  54.       engineStop()
  55.     elseif message == "engine start" and replyChannel == commandchannel then
  56.       engineStart()
  57.     elseif replyChannel == assignedTank and message == nil then
  58.       bufferTankStatus = 0
  59.     elseif replyChannel == assignedTank and message ~= nil then
  60.       bufferTankStatus = message
  61.     end
  62.   end
  63. end
  64.  
  65. function ManagerIO()
  66.   term.clear()
  67.   term.setCursorPos(1,1)
  68.   printStatus()
  69.   print("Valid inputs:")
  70.   term.setTextColor(colors.orange)
  71.   for k,v in pairs(validCommands) do
  72.     write(string.upper(v).." ")
  73.   end
  74.   term.setTextColor(colors.white)
  75.   print("")
  76.   print(myname..">>")
  77.   input = string.lower(io.read())
  78.   if input == "start" then
  79.     engineStart()
  80.     sleep(1)
  81.   elseif input == "stop" then
  82.     engineStop()
  83.   elseif input == "exit" then
  84.     terminate = true
  85.     do return end
  86.   else  
  87.     print("Invalid input.")
  88.   end
  89. end
  90. function printStatus()
  91.   refreshLocalTankInfo()
  92.   engineOnline = not redstone.getOutput("bottom")
  93.   write("Engine is: ")
  94.   if not engineOnline then
  95.     term.setTextColor(colors.purple)
  96.     print("INHIBITED")
  97.   elseif engineOnline and tostring(engine.getEnergyPerTick()) == "80" then
  98.     term.setTextColor(colors.green)
  99.     print("ONLINE")
  100.   elseif engineOnline and fuelLevel == 0 then
  101.     term.setTextColor(colors.red)
  102.     print("CHOKED")
  103.   else
  104.     term.setTextColor(colors.red)
  105.     print("MALFUNCTIONING")
  106.   end
  107.   term.setTextColor(colors.white)
  108.   print("")
  109.   print("Fuel type: "..fuelType)
  110.   print("Reserve fuel level: "..bufferTankStatus)
  111.   print("Engine fuel level: "..fuelLevel)
  112.   print("Engine energy level: "..engine.getEnergy())
  113.   print("Engine production: "..engine.getEnergyPerTick().." RF/t")
  114.   print("")
  115. end
  116. terminate = false
  117. print("Starting "..myname.." listener on "..inputchannel..".")
  118. while true do
  119.   if terminate then
  120.     term.clear()
  121.     term.setCursorPos(1,1)
  122.     do return end
  123.   else
  124.     parallel.waitForAny(complistener,ManagerIO)
  125.   end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement