UNOBTANIUM

FurnaceStationTurtle

May 28th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.38 KB | None | 0 0
  1. local version = "Furnace Station Beta 0.1.0"
  2. local turtleID = os.getComputerID()
  3. local computerID = 6
  4. local furnace = {}
  5. local coord = {}
  6. local needsFuel = true
  7. local durationFuel = 8
  8. local amountFurnace = 10
  9. local slotFinishedItems = {}
  10. local slotToBurnItems = {}
  11. w,h = term.getSize()
  12. rednet.open("right")
  13.  
  14. --
  15. for i=1,14 do
  16.  slotFinishedItems[i] = false
  17. end
  18.  
  19. for i=1,12 do
  20.  slotToBurnItems[i] = 0
  21. end
  22.  
  23. -- coord[x|y|z|direction|extra]
  24. for i=1,5 do
  25.  coord[i] = 0
  26. end
  27.  
  28. -- furnace[number][aFuel|aItems]
  29. for i=1,amountFurnace do
  30.  furnace[i]= {}
  31.  for j=1,2 do
  32.   furnace[i][j] = 0
  33.  end
  34. end
  35.  
  36. function printCentered(str, ypos)
  37.  term.setCursorPos(w/2 - #str/2, ypos)
  38.  term.write(str)
  39. end
  40.  
  41. function printCopyright()
  42.  local str = "by UNOBTANIUM"
  43.  term.setCursorPos(w-#str, h)
  44.  term.write(str)
  45. end
  46.  
  47. function printHeader(title, line)
  48.  printCentered(title, line)
  49.  printCentered(string.rep("-", w), line+1)
  50. end
  51.  
  52. function printReplace(str,line)
  53.  printCentered(string.rep(" ", w), line)
  54.  printCentered(str, line)
  55. end
  56.  
  57. function clearScreen()
  58.  term.clear()
  59.  term.setCursorPos(1,1)
  60.  term.clear()
  61. end
  62.  
  63. function countArray(array)
  64.  local x = 0
  65.  for k,v in pairs(array) do
  66.   x = x + 1
  67.  end
  68.  return x
  69. end
  70.  
  71. function sendMessage(sendingmessage)
  72.  print("send: " .. sendingmessage)
  73.  sendingmessage = tostring(sendingmessage)
  74.  while true do
  75.   repeat
  76.    rednet.send(computerID,sendingmessage)
  77.    os.startTimer(2)
  78.    event, senderChannel, msg = os.pullEvent()
  79.   until event == 'rednet_message'
  80.   if senderChannel == computerID then
  81.    return msg
  82.   end
  83.  end
  84. end
  85.  
  86. function receiveMessage()
  87.  while true do
  88.   local senderChannel, msg = rednet.receive()
  89.   if senderChannel == computerID then
  90.    sleep(0.2)
  91.    rednet.send(computerID,"successful")
  92.    return msg
  93.   end
  94.  end
  95. end
  96.  
  97. function loadVariables()
  98.  if not fs.exists("fsVariables") then
  99.   return false
  100.  end
  101.  local file = fs.open("fsVariables","r")
  102.  computerID = tonumber(file.readLine())
  103.  amountFurnace = tonumber(file.readLine())
  104.  for i=1,14 do
  105.   if file.readLine() == "true" then
  106.   slotFinishedItems[i] = true else slotFinishedItems[i] = false end
  107.  end
  108.  for i=1,12 do
  109.   slotToBurnItems[i] = tonumber(file.readLine())
  110.  end
  111.  for i=1,5 do
  112.   coord[i] = tonumber(file.readLine())
  113.  end
  114.  for i=1,amountFurnace do
  115.   for j=1,2 do
  116.    furnace[i][j] = tonumber(file.readLine())
  117.   end
  118.  end
  119.  file.close()
  120.  return true
  121. end
  122.  
  123.  
  124. function saveVariables()
  125.  local file = fs.open("fsVariables", "w")
  126.  file.writeLine(computerID)
  127.  file.writeLine(amountFurnace)
  128.  for i=1,14 do
  129.   file.writeLine(slotFinishedItems[i])
  130.  end
  131.  for i=1,12 do
  132.   file.writeLine(slotToBurnItems[i])
  133.  end
  134.  for i=1,5 do
  135.   file.writeLine(coord[i])
  136.  end
  137.  for i=1,amountFurnace do
  138.   for j=1,2 do
  139.    file.writeLine(furnace[i][j])
  140.   end
  141.  end
  142.  file.close()
  143. end
  144.  
  145.  
  146. function changeCoord(a,b,c,d,e)
  147.  coord[1] = a
  148.  coord[2] = b
  149.  coord[3] = c
  150.  coord[4] = d
  151.  coord[5] = e
  152.  saveVariables()
  153. end
  154.  
  155. -- changeCoord(coord[1],coord[2],coord[3],coord[4],coord[5])
  156.  
  157. -- x horizontal
  158. -- y behind or directly above
  159. -- z height
  160. -- direction: 1 forward | 2 right | 3 back
  161.  
  162.  
  163. function moveTo(x,y,z,side)
  164.  print("- move -")
  165.  print("current: " .. coord[1] .. " " .. coord[2] .. " " .. coord[3] .. " " .. coord[4])
  166.  print("go to: " .. x .. " " .. y " " .. z .. " " .. side)
  167.  x = x-coord[1]
  168.  z = z-coord[3]
  169.  
  170.  print("relative: " .. x .. " y  " .. z)
  171.  
  172.  if z ~= 0 then
  173.   print("prepare for changing height")
  174.   if 0-coord[2] == -1 then
  175.    turn(2)
  176.    changeCoord(coord[1],0,coord[3],coord[4],coord[5])
  177.    turtle.back()
  178.   end
  179.  end
  180.  
  181.  if z > 0 then
  182.   print("change height")
  183.   for i=1,z do
  184.    changeCoord(coord[1],coord[2],coord[3]+1,coord[4],coord[5])
  185.    turtle.up()
  186.   end
  187.  elseif z < 0 then
  188.   z=-z
  189.   for i=1,z do
  190.    changeCoord(coord[1],coord[2],coord[3]-1,coord[4],coord[5])
  191.    turtle.down()
  192.   end
  193.  end
  194.  
  195.  if not x == 0 then
  196.   print("move x")
  197.   if x > 0 then
  198.    turn(1)
  199.    for i=1,x do
  200.     changeCoord(coord[1]+1,coord[2],coord[3],coord[4],coord[5])
  201.     turtle.forward()
  202.    end
  203.   elseif x < 0 then
  204.    turn(3)
  205.    x=-x
  206.    for i=1,x do
  207.     changeCoord(coord[1]-1,coord[2],coord[3],coord[4],coord[5])
  208.     turtle.forward()
  209.    end
  210.   end
  211.  end
  212.  
  213.  y = y-coord[2]
  214.  print("y: " .. y)
  215.  if y == -1 then
  216.   turn(2)
  217.   changeCoord(coord[1],coord[2]+y,coord[3],coord[4],coord[5])
  218.   turtle.back()
  219.  elseif y == 1 then
  220.   turn(2)
  221.   changeCoord(coord[1],coord[2]+y,coord[3],coord[4],coord[5])
  222.   turtle.forward()
  223.  end
  224.  
  225.  turn(side)
  226.  print("- move end- ")
  227.  read()
  228. end
  229.  
  230. function turn(side)
  231.  print("- turn -")
  232.  print("turn to side: " .. side)
  233.  print("current side: " .. coord[4])
  234.  while coord[4] ~= side do
  235.   print("have to turn")
  236.   if side == 1 then
  237.    changeCoord(coord[1],coord[2],coord[3],coord[4]-1,coord[5])
  238.    turtle.turnLeft()
  239.    print("turn left")
  240.   elseif side == 2 then
  241.    if coord[4] == 1 then
  242.     changeCoord(coord[1],coord[2],coord[3],2,coord[5])  
  243.     turtle.turnRight()
  244.     print("turn right")
  245.    elseif coord[4] == 3 then
  246.     changeCoord(coord[1],coord[2],coord[3],2,coord[5])  
  247.     turtle.turnLeft()
  248.     print("turn left")
  249.    end
  250.   elseif side == 3 then
  251.    changeCoord(coord[1],coord[2],coord[3],coord[4]+1,coord[5])
  252.    turtle.turnRight()
  253.    print("turn right")
  254.   end
  255.  end
  256.  print("- turn end -")
  257.  read()
  258. end
  259.  
  260. function dropFinishedItems()
  261.  print("drop cooked items")
  262.  moveTo(0,0,0,3)
  263.  for i=1,14 do
  264.   if slotFinishedItems[i] then
  265.    turtle.select(i)
  266.    while not turtle.drop() do sleep(5) end
  267.    slotFinishedItems[i] = false
  268.    saveVariables()
  269.   end
  270.  end
  271. end
  272.  
  273. function suckToBurnItems()
  274.  print("suck items")
  275.  moveTo(0,1,1,3)
  276.  turtle.select(1)
  277.  for i=1,12 do
  278.   while turtle.getItemCount(i) == 0 do
  279.    if not turtle.suck() then break end
  280.   end
  281.  end
  282.  for i=1,12 do
  283.   if turtle.getItemCount(i) > 0 then
  284.    slotToBurnItems[i] = turtle.getItemCount[i]
  285.   end
  286.  end
  287.  saveVariables()
  288. end
  289.  
  290. function getFuel()
  291.  print("grabbing fuel")
  292.  moveTo(0,-1,1,3)
  293.  for i=15,16 do
  294.   if turtle.getItemCount(i) > 0 then
  295.    turtle.select(i)
  296.    turtle.drop()
  297.   end
  298.  end
  299.  for i=15,16 do
  300.   turtle.select(15)
  301.   while turtle.getItemCount(i) == 0 do
  302.    turtle.suck()
  303.    sleep(0.5)
  304.   end
  305.  end
  306.  while turtle.getFuelLevel() <= 500 do
  307.   turtle.select(16)
  308.   turtle.refuel(1)
  309.  end
  310. end
  311.  
  312. function takeFinishedItemsOut(x)
  313.  print("taking items out of the furnace")
  314.  moveTo(x-1,0,0,2)
  315.  local emptySlot = 0
  316.  for i=1,14 do
  317.   if turtle.getItemCount(i) == 0 then
  318.    emptySlot = i
  319.    break
  320.   end
  321.  end
  322.  if emptySlot == 0 then
  323.   print("no empty slot left")
  324.   dropFinishedItems()
  325.   takeFinishedItemsOut(x)
  326.   return
  327.  end
  328.  print("taking items out")
  329.  turtle.select(emptySlot)
  330.  slotFinishedItems[emptySlot] = true
  331.  local leftInFurnace = furnace[x][2]
  332.  while furnace[x][2] > 0 do
  333.   if turtle.suck() then
  334.    furnace[x][2] = leftInFurnace - turtle.getItemCount(emptySlot)
  335.    saveVariables()
  336.   else
  337.    sleep(1)
  338.   end
  339.  end
  340. end
  341.  
  342. function checkInventory()
  343.  print("checking inventory")
  344.  local amountInUseSlots = 0
  345.  local amountFinishedSlots = 0
  346.  for i=1,14 do
  347.   if slotFinishedItems[i] then
  348.    amountFinishedSlots = amountFinishedSlots + 1
  349.   end
  350.   if i<= 12 and slotToBurnItems[i] > 0 then
  351.    amountInUseSlots = amountInUseSlots + 1
  352.   end
  353.  end
  354.  if amountFinishedSlots > amountInUseSlots then
  355.   dropFinishedItems()
  356.   suckToBurnItems()
  357.  end
  358.  if turtle.getFuelLevel() < 200 or turtle.getItemCount(15)+turtle.getItemCount(16) < 16 then
  359.   getFuel()
  360.  end
  361. end
  362.  
  363. function dropCoalInFurnace(amount)
  364.  print("droppping coal in the furnace")
  365.   if turtle.getItemCount(15) >= amount then
  366.   turtle.select(15)
  367.   turtle.dropUp(amount)
  368.  elseif turtle.getItemCount(16) >= amount then
  369.   turtle.select(16)
  370.   turtle.dropUp(amount)
  371.  else
  372.   turtle.select(15)
  373.   turtle.transferTo(16, 64-turtle.getItemCount(16))
  374.   turtle.select(16)
  375.   turtle.dropUp(amount)
  376.  end
  377. end
  378.  
  379. function burn()
  380.  print("burning items")
  381.  local nextEmptyFurnace = 0
  382.  for i=1,amountFurnace do
  383.   if furnace[i][2] == 0 then
  384.    nextEmptyFurnace = i
  385.    break
  386.   end
  387.  end
  388.  if nextEmptyFurnace == 0 then
  389.   sleep(2)
  390.   return
  391.  end
  392.  for i=1,12 do
  393.   if slotToBurnItems[i] > 0 then
  394.    if needsFuel and slotToBurnItems[i] < durationFuel then
  395.    elseif needsFuel and slotToBurnItems[i] >= durationFuel then
  396.     if math.floor(turtle.getItemCount(i)/durationFuel) > furnace[nextEmptyFurnace][1] then
  397.      moveTo(nextEmptyFurnace-1,-1,-1,2)
  398.      dropCoalInFurnace((math.floor(turtle.getItemCount(i)/durationFuel))-furnace[nextEmptyFurnace][1])
  399.      furnace[nextEmptyFurnace][1] = (math.floor(turtle.getItemCount(i)/durationFuel))
  400.      saveVariables()
  401.     end
  402.     moveTo(nextEmptyFurnace-1,1,1,2)
  403.     turtle.select(i)
  404.     turtle.dropDown(math.floor(slotToBurnItems[i]/durationFuel))
  405.     furnace[nextEmptyFurnace][2] = math.floor(slotToBurnItems[i]/durationFuel)*durationFuel
  406.     saveVariables()
  407.     sendMessage(textutils.serialize({"new",nextEmptyFurnace, math.floor(slotToBurnItems[i]/durationFuel)*10}))
  408.     return true
  409.    elseif not needsFuel then
  410.     moveTo(nextEmptyFurnace-1,1,1,2)
  411.     turtle.select(i)
  412.     turtle.dropDown(slotToBurnItems[i])
  413.     furnace[nextEmptyFurnace][2] = slotToBurnItems[i]
  414.     saveVariables()
  415.     sendMessage(textutils.serialize({"new",nextEmptyFurnace, slotToBurnItems[i]*10}))
  416.     return true
  417.    end
  418.   end
  419.  end
  420.  return false
  421. end
  422.  
  423. function run()
  424.  while true do
  425.   checkInventory()
  426.   sendMessage(textutils.serialize({"check",0,0}))
  427.   local message = textutils.unserialize(receiveMessage())
  428.   if message[1] == "furnaces are empty" then
  429.    dropFinishedItems()
  430.    suckToBurnItems()
  431.    getFuel()
  432.    burn()
  433.   elseif message[1] == "finished" then
  434.    takeFinishedItemsOut(message[2])  
  435.   elseif message[1] == "allmost done" then
  436.    if message[3] > 90 then
  437.     if not burn() then
  438.      sleep(30)
  439.     end
  440.    elseif message[3] > 30 then
  441.     for i=1,14 do
  442.      if slotFinishedItems[i] then
  443.       dropFinishedItems()
  444.       break
  445.      end
  446.     end
  447.     for i=1,12 do
  448.      if turtle.getItemCount(i) == 0 then
  449.       suckToBurnItems()
  450.       break
  451.      end
  452.     end
  453.     if needsFuel and turtle.getItemCount(15) + turtle.getItemCount(16) < 16 then
  454.      getFuel()
  455.     elseif turtle.getFuelLevel() < 200 then
  456.      getFuel()
  457.     end
  458.     burn()
  459.    elseif message[3] > 10 then
  460.     burn()
  461.    else
  462.     takeFinishedItemsOut(message[2])
  463.    end
  464.   end
  465.  end
  466. end
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489. coord[1] = 0
  490. coord[2] = 1
  491. coord[3] = -1
  492. coord[4] = 3
  493. loadVariables()
  494. run()
Advertisement
Add Comment
Please, Sign In to add comment