Lupus590

[CC] lavaElevator (rewritten)

Dec 7th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. --
  3. -- There are config variables below.
  4. --
  5. -- Modified to use a file to
  6. -- 'remember' what action it was doing
  7. -- when the world stopped existing.
  8. --
  9. -- Fair warning, I have not tested
  10. -- this code.
  11. --
  12. -- Also, you should really make things
  13. -- local, it keeps the computer global
  14. -- memory cleaner.
  15. --
  16. -- 39 characters are not enough width.
  17. -- http://computercraft.info/wiki/Resolution#Turtle_resolution
  18. --
  19. -- I've practically rewritten this
  20. -- entirely, feel free to not use it
  21. -- because of this.
  22. --
  23. -- If you need me to explain the code
  24. -- then feel free to ask me to do so.
  25. --
  26. -- Lupus590
  27. --
  28. -- P.S. I'm part way through this
  29. -- rewrite and I'm realising that I
  30. -- may be doing a lot of tricks which
  31. -- you may not know about, I've tried
  32. -- my best to comment regularly.
  33. --
  34. -- P.P.S. You have some odd logic.
  35. --
  36. -- ]]
  37. --[[ multi line comment
  38. I've done it oddly above as I believe
  39. ComputerCraft reacts funny.
  40. It might only be the syntax highlighter
  41. that goes funny, which this would
  42. demonstrate if on an advanced system. ]]
  43.  
  44.  
  45.  
  46. ------------
  47. -- Config --
  48. ------------
  49.  
  50. local targetFuelLevel = 1000
  51.   -- turtle will try to keep at this fuel level by using lava
  52.   -- increase if turtle runs out of fuel and has no lava
  53.   -- decrease if it keeps drinking it all
  54.  
  55. local stateFileName = "lavaElelvatorState"
  56.  
  57. ----------
  58. -- Code --
  59. ----------
  60.  
  61. local currentState
  62. local state = {}
  63. state.descend = "descend"
  64. state.goToLava = "goToLava"
  65. state.getLava = "getLava"
  66. state.goToShaftBottom = "goToShaftBottom"
  67. state.ascend = "ascend"
  68. state.giveLava = "giveLava"
  69. local action = {} -- corresponds to the states above
  70. action.descend
  71. action.goToLava
  72. action.getLava
  73. action.goToShaftBottom
  74. action.ascend
  75. action.giveLava
  76.  
  77.  
  78. local function findResumeActions() -- figures out what the turtle was doing before chunk unload so that the turtle can continue efficiently
  79.   -- print("Running findResumeActions...")
  80.  
  81.   -- resume sequence { action.descend, action.goToLava, action.getLava, action.goToShaftBottom, action.ascend,  action.giveLava}
  82.  
  83.   if fs.exists(stateFileName) then
  84.     local stateFile = fs.open(stateFileName, "r")
  85.     currentState = stateFile.readLine()
  86.     stateFile.close()
  87.     if currentState == state.descend then
  88.       return "continue" -- default actions are fine
  89.      
  90.     elseif currentState == state.goToLava then
  91.       return {action.goToLava, action.getLava, action.goToShaftBottom, action.ascend, action.giveLava}
  92.      
  93.     elseif currentState == state.getLava then
  94.       return {action.getLava, action.goToShaftBottom, action.ascend, action.giveLava}
  95.    
  96.     elseif currentState == state.goToShaftBottom then
  97.       return {action.goToShaftBottom, action.ascend, action.giveLava}
  98.      
  99.     elseif currentState == state.ascend then
  100.       return {action.ascend, action.giveLava}
  101.      
  102.     elseif currentState == state.givLava then
  103.       return {action.giveLava}
  104.      
  105.     else
  106.       error("Unexpected value in file: "..stateFileName)
  107.     end
  108.   end
  109.  
  110.  
  111.   print("Could not find file: "..stateFileName)
  112.   print("Assistance required to start")
  113.   print("Please place turtle to the shaft bottom and then restart this program")
  114.   print("If the turtle is already there then type in 'Y', any other input will quit this program")
  115.  
  116.   if string.lower(read()) == "y" then
  117.     return "continue"
  118.   else
  119.     return "quit"
  120.   end
  121. end
  122.  
  123. local function saveState(state)
  124.   local stateFile = fs.open(stateFileName, "w")
  125.   stateFile.writeLine(state) -- could error here if for some reason the file can't be opened
  126.   stateFile.close()
  127.   --print("state saved")
  128. end
  129.  
  130. local function selectEmptySlot()
  131.   for i = 1, 16 do
  132.     turtle.select(i)
  133.     if turtle.getItemCount() == 0 then
  134.       return true
  135.      end
  136.   end
  137.   return false -- could not find an empty slot
  138. end
  139.  
  140. local function checkFuel() -- true = lava used
  141.   local currentSlot = 1
  142.   local drank = false
  143.   while turtle.getFuelLevel < targetFuelLevel do
  144.     turtle.select(currentSlot)
  145.     if not turtle.refuel(1) then -- if we can't refuel from it
  146.       if currentSlot ~= 16 then  
  147.         currentSlot = currentSlot + 1 -- select the next slot
  148.       else
  149.         break -- exit the while loop as we have ran out of slots to try to refuel from
  150.       end
  151.     else
  152.       drank = true -- we refuelled from something
  153.     end
  154.   end    
  155.   return drank
  156. end
  157.  
  158. function action.descend()
  159.   --print("Running action.descend...")
  160.   saveState(state.descending)
  161.   while turtle.down() do -- if we can go down then we do, otherwise we must be at the bottom
  162.     -- would check fuel but no lava on board so we are stuck anyway
  163.   end
  164. end
  165.  
  166.  
  167. function action.goToLava()
  168.   --print("Running action.goToLava...")
  169.   saveState(state.goToLava)
  170.   while turtle.forward() do
  171.     -- would check fuel but no lava on board so we are stuck anyway
  172.   end
  173. end
  174.  
  175.  
  176. function action.getLava()
  177.   --print("Running action.getLava...")
  178.   saveState(state.getLava)
  179.   -- drop off empty buckets
  180.   for i = 1,16 do
  181.     turtle.select(i)
  182.     local _, item = turtle.getItemDetail()
  183.     if item.name = "minecraft:bucket" then
  184.       turtle.drop()
  185.     end
  186.   end
  187.  
  188.   -- pick up full buckets
  189.   while selectEmptySlot() do
  190.     turtle.suck()
  191.    
  192.     -- check that the item we got is not an empty bucket
  193.     local _, item = turtle.getItemDetail()
  194.     if item.name = "minecraft:bucket" then
  195.       turtle.drop() -- if it is an empty bucket, put it back
  196.       sleep(10) -- sleep a bit so the other turtle has time to fill the bucket
  197.     end
  198.    
  199.     -- let's check our fuel while we have lava near us
  200.     if checkFuel() then
  201.       -- we just refuelled, may as well replace the empty bucket/s while we are here
  202.       return action.getLava() -- trick: by returning we don't use another 'slot' on the function stack
  203.     end
  204.   end
  205. end
  206.  
  207.  
  208. function action.goToShaftBottom()
  209.   --print("Running action.goToShaftBottom...")
  210.   saveState(state.goToShaftBottom)
  211.   checkFuel()
  212.   while turtle.back() do
  213.     checkFuel() -- have lava, don't want to get stuck
  214.   end
  215. end
  216.  
  217.  
  218. function action.ascend()
  219.   --print("Running action.ascend...")
  220.   saveState(state.ascend)
  221.  
  222.   local _, blockInFront = turtle.inspect()
  223.   local foundChest = blockInFront == "minecraft:chest" -- this should work
  224.   while not foundChest do
  225.     checkFuel()
  226.     turtle.up()
  227.     _, blockInFront = turtle.inspect() -- reusing the old variables
  228.     foundChest = blockInFront == "minecraft:chest"
  229.   end
  230. end
  231.    
  232. function action.giveLava()
  233.  --print("Running action.giveLava...")
  234.   saveState(state.giveLava)
  235.  
  236.   checkFuel() -- just before we do, lets be a selfish turtle
  237.  
  238.   -- drop off full buckets
  239.   for i = 1,16 do
  240.     turtle.select(i)
  241.     local _, item = turtle.getItemDetail()
  242.     if item.name = "minecraft:lava_bucket" then
  243.       turtle.drop()
  244.     end
  245.   end
  246.  
  247.   -- pick up empty buckets
  248.   while selectEmptySlot() do
  249.     turtle.suck()
  250.     -- check that the item we got is not a full bucket
  251.     local _, item = turtle.getItemDetail()
  252.     if item.name = "minecraft:lava_bucket" then
  253.       turtle.drop() -- if it is a full bucket, put it back
  254.       sleep(10) -- sleep a bit so the quarry has time to drink
  255.     end
  256.   end
  257. end
  258.  
  259.  
  260. ------------------
  261. -- Main Program --
  262. ------------------
  263.  
  264.  
  265. local resumeActions = findResumeActions() -- returns a table (usually)
  266. if type(resumeActions) == "table" and #resumeActions > 0 then -- is it a table and does it have stuff in
  267.   for _,v in ipairs(resumeActions) do -- go through each key in the table and put the value in v
  268.     v() -- the value should be a function, run it. If it ends up not being a function the script will error here
  269.   end
  270. elseif type(resumeActions) == "string"
  271.   if resumeActions == "quit" then
  272.     return
  273.   elseif resumeActions == "continue" then
  274.     -- continue to while true loop
  275.   else
  276.     error("Unexpected string from findResumeActions")
  277.   end
  278. else
  279.   error("Unexpected return value from findResumeActions")
  280. end
  281.  
  282. while true do
  283.   action.descend()
  284.   action.goToLava()
  285.   action.getLava()
  286.   action.goToShaftBottom()
  287.   action.ascend()
  288.   action.goToQuarry()
  289.   action.giveLava()
  290.   action.goToShaftTop()
  291. end
Add Comment
Please, Sign In to add comment