UNOBTANIUM

FurnaceStationComputer

May 28th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. local version = "Furnace Sation Beta 0.1.0"
  2. local computerID = os.getComputerID()
  3. local turtleID = 7
  4. local amountFurnace = 10
  5. local timer = {}
  6. local finished = {}
  7. w,h = term.getSize()
  8. rednet.open("back")
  9.  
  10. -- timer[furnacenumber]
  11. for i=1,amountFurnace do
  12.  timer[i] = 0
  13.  finished[i] = false
  14.  print(timer[i])
  15. end
  16.  
  17. function printCentered(str, ypos)
  18.  term.setCursorPos(w/2 - #str/2, ypos)
  19.  term.write(str)
  20. end
  21.  
  22. function printCopyright()
  23.  local str = "by UNOBTANIUM"
  24.  term.setCursorPos(w-#str, h)
  25.  term.write(str)
  26. end
  27.  
  28. function printHeader(title, line)
  29.  printCentered(title, line)
  30.  printCentered(string.rep("-", w), line+1)
  31. end
  32.  
  33. function printReplace(str,line)
  34.  printCentered(string.rep(" ", w), line)
  35.  printCentered(str, line)
  36. end
  37.  
  38. function clearScreen()
  39.  term.clear()
  40.  term.setCursorPos(1,1)
  41.  term.clear()
  42. end
  43.  
  44. function countArray(array)
  45.  local x = 0
  46.  for k,v in pairs(array) do
  47.   x = x + 1
  48.  end
  49.  return x
  50. end
  51.  
  52.  
  53. function sendMessage(sendingmessage)
  54.  sendingmessage = tostring(sendingmessage)
  55.  while true do
  56.   repeat
  57.    rednet.send(turtleID,sendingmessage)
  58.    os.startTimer(2)
  59.    event, id, msg = os.pullEvent()
  60.   until event == 'rednet_message'
  61.   if id == turtleID then
  62.    return msg
  63.   end
  64.  end
  65. end
  66.  
  67. function receiveMessage()
  68.  while true do
  69.   local senderChannel, msg = rednet.receive()
  70.   if senderChannel == turtleID then
  71.    sleep(0.2)
  72.    rednet.send(turtleID,"successful")
  73.    return msg
  74.   end
  75.  end
  76. end
  77.  
  78. function loadVariables()
  79.  if not fs.exists("fsVariables") then
  80.   return false
  81.  end
  82.  local file = fs.open("fsVariables","r")
  83.  computerID = tonumber(file.readLine())
  84.  amountFurnace = tonumber(file.readLine())
  85.  for i=1,amountFurnace do
  86.   timer[i] = tonumber(file.readLine())
  87.   local line = file.readLine()
  88.   if line == "true" then
  89.   finished[i] = true else finished[i] = false end
  90.  end
  91.  file.close()
  92.  return true
  93. end
  94.  
  95.  
  96. function saveVariables()
  97.  local file = fs.open("fsVariables", "w")
  98.  file.writeLine(computerID)
  99.  file.writeLine(amountFurnace)
  100.  for a=1,amountFurnace do
  101.   file.writeLine(timer[i])
  102.   file.writeLine(finished[i])
  103.  end
  104.  file.close()
  105. end
  106.  
  107.  
  108. function countDown()
  109.  for i=1,amountFurnace do
  110.   if timer[i] > 1 then
  111.    timer[i] = timer[i] - 1
  112.   elseif timer[i] == 1 then
  113.    timer[i] = 0
  114.    finished[i] = true
  115.   end
  116.  end
  117. end
  118.  
  119. function check()
  120.  local furnacenumber = 0
  121.  for i=1,amountFurnace do
  122.   if finished[i] then
  123.    furnacenumber = i
  124.    break
  125.   end
  126.  end
  127.  if furnacenumber > 0 then
  128.   local message = {"finished",furnacenumber,0}
  129.   sendMessage(textutils.serialize(message))
  130.   return
  131.  end
  132.  
  133.  local timeLeft = 90000
  134.  for i=1,amountFurnace do
  135.   print(timer[i])
  136.   if timer[i] > 0 and timeLeft > timer[i] then
  137.    timeLeft = timer[i]
  138.    furnacenumber = i
  139.   end
  140.  end
  141.  if furnacenumber > 0 then
  142.   local message = {"allmost done",furnacenumber, timeLeft}
  143.   sendMessage(textutils.serialize(message))
  144.   return
  145.  end
  146.  
  147.  sendMessage(textutils.serialize{"furnaces are empty",0,0})
  148. end
  149.  
  150. function run()
  151.  while true do
  152.   if countArray(timer) > 0 then
  153.    os.startTimer(1)
  154.   end
  155.   local event, id, m = os.pullEvent()
  156.  
  157.   if event == "timer" then
  158.    countDown()
  159.   elseif event == "rednet_message" then
  160.    print(m)
  161.    if m[1] == "new" then
  162.     print("new: " ..m[2] .. " " .. m[3])
  163.     timer[m[2]] = m[3]
  164.    elseif m[1] == "done" then
  165.     print("done: " .. m[2])
  166.     finished[m[2]] = false
  167.    end
  168.    print("check")
  169.    check()
  170.   end
  171.   saveVariables()
  172.  end
  173. end
  174.  
  175.  
  176. loadVariables()
  177. run()
Advertisement
Add Comment
Please, Sign In to add comment