Nessiesson

CyaNox crazy turtle infinite room dig

Sep 1st, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.55 KB | None | 0 0
  1. -- init
  2.  
  3. num = 1
  4. step = 1
  5. curnum = 1
  6.  
  7. fuelenderchest = 1
  8. stuffenderchest = 2
  9. minfuel = 1024
  10.  
  11. -- A little change to standard assert function
  12. _G.assert = function(condition, errMsg, level)
  13.   if not condition then
  14.         error(errMsg, (tonumber(level) or 1) + 1)
  15.   end
  16.   return condition
  17. end
  18.  
  19. -- explode(seperator, string)
  20. function explode(d,p)
  21.   local t, ll
  22.   t={}
  23.   ll=0
  24.   if(#p == 1) then return {p} end
  25.     while true do
  26.       l=string.find(p,d,ll,true) -- find the next d in the string
  27.       if l~=nil then -- if "not not" found then..
  28.         table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
  29.         ll=l+1 -- save just after where we found it for searching next time.
  30.       else
  31.         table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
  32.         break -- Break at end, as it should be, according to the lua manual.
  33.       end
  34.     end
  35.   return t
  36. end
  37.  
  38. function exportResume(t, r)
  39.   r = r or "resume"
  40.   f = io.open(r, "w")
  41.   for k,v in pairs(t) do
  42.     f:write(k .. "=" .. tostring(v) .. "\n")
  43.   end
  44.   f:close()
  45. end
  46.  
  47. -- resume function. Needs to be called after variable init.
  48. function loadResume(r)
  49.   r = r or "resume"
  50.   t = io.open(r)
  51.   if t then
  52.     for lines in t:lines(t) do
  53.       c = explode("=", lines)
  54.       if #c == 2 then
  55.         if #c[1] > 2 then
  56.           print("setting "..tostring(c[1]).." to "..tostring(c[2]))
  57.           if c[1] == "num" then num = tonumber(c[2]) end
  58.           if c[1] == "step" then step = tonumber(c[2]) end
  59.           if c[1] == "curnum" then curnum = tonumber(c[2]) end
  60.           _G[c[1]] = tonumber(c[2])
  61.         end
  62.       end
  63.     end
  64.     t:close()
  65.   end
  66. end
  67.  
  68. local function forcePlaceUp ()
  69.     while not turtle.placeUp() do
  70.         if turtle.detectUp() then
  71.             if peripheral.isPresent ("top") and peripheral.getType ("top") == "turtle" then
  72.                 -- Other turtle detected. Waiting...
  73.                 while peripheral.isPresent ("top") do
  74.                     sleep (2)
  75.                 end
  76.             end
  77.             turtle.digUp()
  78.         elseif turtle.attackUp() then
  79.             -- Attacking obstructing entity
  80.         end
  81.     end
  82. end
  83.  
  84. function checkFuel (amount)
  85.     assert (type(amount) == "number", "Bad fuel amount. Number required, got "..type(amount), 2)
  86.     local fuel = turtle.getFuelLevel()
  87.     if fuel == "unlimited" or fuel > amount then
  88.         return true
  89.     else
  90.         turtle.select (fuelenderchest)
  91.         forcePlaceUp ()
  92.         turtle.suckUp ()
  93.         while true do
  94.             if not turtle.refuel (1) then
  95.                 if turtle.getItemCount (fuelenderchest) then
  96.                     turtle.dropUp ()
  97.                 end
  98.                 sleep (1)
  99.                 turtle.suckUp ()
  100.             end
  101.             if turtle.getFuelLevel () >= amount then
  102.                 if turtle.getItemCount (fuelenderchest) > 0 then
  103.                     turtle.dropUp ()
  104.                 end
  105.                 -- If it can't drop all the fuel into chest, just consume it all
  106.                 if turtle.getItemCount (fuelenderchest) > 0 then
  107.                     turtle.refuel (turtle.getItemCount(fuelenderchest))
  108.                 end
  109.                 turtle.digUp ()
  110.                 turtle.select (1)
  111.                 return true
  112.             end
  113.             sleep (1)
  114.         end
  115.     end
  116. end
  117.  
  118. function dumpStuff ()
  119.     if stuffenderchest ~= nil then
  120.         turtle.select (stuffenderchest)
  121.         forcePlaceUp ()
  122.         for i = 1,16 do
  123.             if i ~= fuelenderchest and i ~= stuffenderchest then
  124.                 while turtle.getItemCount (i) > 0 do
  125.                     turtle.select (i)
  126.                     turtle.dropUp ()
  127.                 end
  128.             end
  129.         end
  130.         turtle.select (stuffenderchest)
  131.         turtle.digUp ()
  132.         turtle.select (1)
  133.     end
  134. end
  135.  
  136. function checkSpace ()
  137.     if turtle.getItemCount (16) > 0 then
  138.         dumpStuff ()
  139.     end
  140. end
  141.  
  142.  
  143. -- resume
  144. loadResume()
  145.  
  146. -- mainloop
  147. while true do
  148.   -- pre check fuel
  149.   checkFuel(minfuel)
  150.   -- pre check inventory
  151.   checkSpace()
  152.  
  153.   -- dig + check inventory
  154.   turtle.digUp()
  155.   checkSpace()
  156.   while turtle.suckUp() do
  157.     checkSpace()
  158.     sleep(0.01)
  159.   end
  160.   turtle.digDown()
  161.   checkSpace()
  162.   while turtle.suckDown() do
  163.     checkSpace()
  164.     sleep(0.01)
  165.   end
  166.   turtle.dig()
  167.   checkSpace()
  168.   while turtle.suck() do
  169.     checkSpace()
  170.     sleep(0.01)
  171.   end
  172.   uptries = 0
  173.   while turtle.detectUp() do
  174.     uptries = uptries + 1
  175.     if uptries > 10 then break end
  176.     turtle.digUp()
  177.     checkSpace()
  178.     sleep(0.01)
  179.   end
  180.   -- move
  181.   while not turtle.forward() do
  182.     -- try fixing the reason why we can't move and retry
  183.     turtle.dig()
  184.     turtle.attack()
  185.     checkFuel(minfuel)
  186.     checkSpace()
  187.     sleep(0.01)
  188.   end
  189.   curnum = curnum + 1
  190.  
  191.   if curnum > num then
  192.     curnum = 1
  193.     turtle.turnLeft()
  194.    
  195.     step = step + 1
  196.     if step > 2 then
  197.       step = 1
  198.       num = num + 1
  199.     end
  200.   end
  201.   -- write resume
  202.   exportResume({num = num, step = step, curnum = curnum})
  203.   -- pause
  204.   sleep(0.01)
  205. end
Advertisement
Add Comment
Please, Sign In to add comment