MatthewGB

MineChemTurtle

May 24th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. st = 0.2
  2.  
  3. som = "Unchecked" -- State of machine
  4. sot = "Unkown"  -- State of turtle
  5. ssot = "Unkown"  -- SubState of turtle
  6. rsot = "OK"  -- RunState of turtle
  7. oi = "None" -- Other info
  8.  
  9. leftp = "?"
  10. rightp = "?"
  11.  
  12. n = "?"
  13. s = "?"
  14. e = "?"
  15. w = "?"
  16. function r(func) -- run
  17.  parallel.waitForAll(disp, func)
  18. end
  19. function sv(var, par) -- setvariable, to work with r() correctly
  20.  var = par
  21. end
  22. function disp()
  23.  term.clear()
  24.  term.setCursorPos(1,1)
  25.  write("Turte status: ")
  26.  write(sot)
  27.  term.setCursorPos(1,2)
  28.  write("Turte substatus: ")
  29.  write(ssot)
  30.  term.setCursorPos(1,3)
  31.  write("Turte runstatus: ")
  32.  write(rsot)
  33.  term.setCursorPos(1,4)
  34.  write("Machine status: ")
  35.  write(som)
  36.  term.setCursorPos(1,5)
  37.  write("Error Message: ")
  38.  write(oi)
  39.  term.setCursorPos(1,6)
  40.  write("T = turtle ||")
  41.  write("C = chest ||")
  42.  write("M = machine ||")
  43.  write("? = Unkown")
  44.  term.setCursorPos(1,9)
  45.  print(" "..n.." ")
  46.  print(w.."T"..e)
  47.  print(" "..s.." ")
  48. end
  49. function unJam()
  50.  sot = "Unjamming output"
  51.  turtle.select(6)
  52.  ssot = "Getting output"
  53.  disp()
  54.  while cp.takeOutput() do end
  55.  i = 6
  56.  while i<16 do
  57.   i = i + 1
  58.   turtle.select(i)
  59.   am = cp.getAtomicMass()
  60.   nm = cp.getChemicalName()
  61.   if am == 6 then
  62.    if nm == "Carbon" then
  63.     -- nothing
  64.    else
  65.     turtle.dropDown()
  66.    end
  67.   end
  68.  end
  69.  i = 6
  70.  ssot = "Turning right 1/4"
  71.  disp()
  72.  turtle.turnRight()
  73.  ssot = "Turning right 2/4"
  74.  disp()
  75.  turtle.turnRight()
  76.  while i<16 do
  77.   ssot = "Emptying slot "..i.."/16"
  78.   disp()
  79.   i = i + 1
  80.   turtle.select(i)
  81.   turtle.drop()
  82.  end
  83.  ssot = "Turning right 3/4"
  84.  disp()
  85.  turtle.turnRight()
  86.  ssot = "Turning right 4/4"
  87.  disp()
  88.  turtle.turnRight()
  89. end
  90. function getMoreVials()
  91.  som = "Need more vials"
  92.  sot = "Retrieving vials"
  93.  disp()
  94.  a = false
  95.  while a == false do
  96.   sleep(st)
  97.   turtle.select(1)
  98.   i = 1
  99.   while i<5 do
  100.    ssot = "Sucking vials from top chest ("..i.."/4)"
  101.    disp()
  102.    a = turtle.suckUp()
  103.    i = i + 1
  104.   end
  105.   turtle.select(1)
  106.   oi = "Vials loaded: "..turtle.getItemCount(1)+turtle.getItemCount(2)+turtle.getItemCount(3)+turtle.getItemCount(4).."/256"
  107.   if turtle.getItemCount(1)+turtle.getItemCount(2)+turtle.getItemCount(3)+turtle.getItemCount(4) == 0 then
  108.    oi = "Not enough has been loaded. Trying again!"
  109.    a = false
  110.   else
  111.    oi = "Enough loaded"
  112.    a = true
  113.   end
  114.  end
  115.  ssot = "Putting vials into machine: "
  116.  disp()
  117.  sleep(st)
  118.  turtle.select(1)
  119.  cp.putEmptyTestTube()
  120.  turtle.select(2)
  121.  cp.putEmptyTestTube()
  122.  turtle.select(3)
  123.  cp.putEmptyTestTube()
  124.  turtle.select(4)
  125.  cp.putEmptyTestTube()
  126.  turtle.select(1)
  127.  ssot = "Idle"
  128. end
  129. sot = "Starting up"
  130. disp()
  131. ssot = "Checking slots"
  132. disp()
  133. oi = "Empty slots: "
  134. s = 1
  135. for s = 1,16 do
  136.  if turtle.getItemCount(s) == 0 then
  137.   oi = oi..", "..s
  138.   disp()
  139.  else
  140.   print()
  141.   oi = "Slot "..s.." is not empty! Please empty all slots before starting."
  142.   rsot = "ERROR"
  143.   disp()
  144.   sleep(2)
  145.   os.reboot()
  146.  end
  147. end
  148. print()
  149. ssot = "Initialising chem peripheral"
  150. disp()
  151. sleep(st)
  152. if peripheral.getMethods("left") == nil then
  153.  if peripheral.getMethods("right") == nil then
  154.   rsot = "ERROR"
  155.   oi = "No peripherals attached!"
  156.   sleep(2)
  157.   os.reboot()
  158.  elseif peripheral.getMethods("right")[1] == "getChemicalName" then
  159.   oi = "Found chem on right"
  160.   cp = peripheral.wrap("right")
  161.   rightp = "P"
  162.  else
  163.   rsot = "ERROR"
  164.   oi = "No valid peripherals attached!"
  165.   sleep(2)
  166.   os.reboot()
  167.  end
  168. elseif peripheral.getMethods("left")[1] == "getChemicalName" then
  169.  oi = "Found chem on left"
  170.  cp = peripheral.wrap("left")
  171.  leftp = "P"
  172. else
  173.  rsot = "ERROR"
  174.  oi = "No valid peripherals attached!"
  175.  disp()
  176.  sleep(2)
  177.  os.reboot()
  178. end
  179. disp()
  180. ssot = "Checking if block infront"
  181. disp()
  182. if turtle.detect() == false then
  183.  rsot = "ERROR"
  184.  oi = "No block in front! Restart the program when there is a machine infront"
  185.  disp()
  186.  sleep(2)
  187.  os.reboot()
  188. end
  189. ssot = "Checking if block infront is a machine: "
  190. disp()
  191. state = cp.getMachineState()
  192. sleep(st)
  193. while 1<2 do
  194.  disp()
  195.  state = cp.getMachineState()
  196.  if state == nil then
  197.   state = "???"
  198.  end
  199.  print("State of machine: "..state)
  200.  som = state
  201.  disp()
  202.  if state == "needtesttubes" then
  203.   getMoreVials()
  204.  elseif state == "powered" then
  205.   unJam()
  206.   sot = "Filling machine with input"
  207.   turtle.select(5)
  208.   ssot = "Sucking items from chest underneath"
  209.   disp()
  210.   a = false
  211.   while a == false do
  212.    a = turtle.suckDown()
  213.    if a then
  214.     -- nothng
  215.    else
  216.     rsot = "WARNING"
  217.     oi = "Out of input!"
  218.     disp()
  219.    end
  220.   end
  221.   rsot = "OK"
  222.   ssot = "Inserting input items into machine"
  223.   cp.putInput()
  224.   ssot = "Idle"
  225.  elseif state == "decomposing" then
  226.   sot = "Idle"
  227.   ssot = "Idle"
  228.   oi = "None"
  229.   rsot = "OK"
  230.  elseif state == "outputjammed" then
  231.  unJam()
  232.  end -- state
  233.  sleep(1)
  234. end -- main loop
  235. write("Getting decomposable item: ")
  236. a = false
  237. while a == false do
  238.  sleep(st)
  239.  a = turtle.suckDown()
  240.  if a then
  241.   --nothing
  242.  else
  243.   print("FAIL")
  244.   print("Trying again!")
  245.  end
  246.  turtle.select(2)
  247.  turtle.transferTo(16)
  248.  print("Items loaded: "..turtle.getItemCount(16))
  249.  if turtle.getItemCount(16) == 0 then
  250.   print("No items were loaded, trying again!")
  251.  else
  252.   print("ok")
  253.  end
  254. end
Advertisement
Add Comment
Please, Sign In to add comment