Guest User

Ex 3.0

a guest
Sep 13th, 2012
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.09 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. function sign(num)  
  4.   return not num and 0 or num>0 and 1 or -1
  5. end  
  6.  
  7. function printArray(arr)
  8.   if arr then
  9.     for i,v in pairs(arr) do
  10.       write(i .. "=" .. v .. " ")
  11.     end
  12.   end
  13.   print("")
  14. end
  15.  
  16. local goto, checkOverflow, dig, turn, advance
  17.  
  18. local planMatrix
  19. local max
  20. local min
  21. local size
  22. local excavating = false
  23. local slot={junk={}, ignore={}, fuel={}}
  24.  
  25. function designate(n, type)
  26.   print("Designate "..n.." as "..type)
  27.   slot[n].designation = type
  28.   table.insert(slot[type],n)
  29. end
  30.  
  31. function plan(block)
  32.     local ind = (block.x or 0)*100000000 + (block.y or 0)*10000 + (block.z or 0)
  33.     planMatrix[ind] = planMatrix[ind] or {}
  34.     return planMatrix[ind]
  35. end
  36.  
  37.  
  38. --[[
  39. if #tArgs ~= 1 then
  40.     print( "Usage= excavate <diameter>" )
  41.     return
  42. end
  43. ]]
  44.    
  45. up={name="up", dig=turtle.digUp, move=turtle.up, detect=turtle.detectUp, place=turtle.placeUp, attack=turtle.attackUp, dy}
  46. forward={name="forward", dig=turtle.dig, move=turtle.forward, detect=turtle.detect, place=turtle.place, attack=turtle.attack}
  47. down={name="down",dig=turtle.digDown, move=turtle.down, detect=turtle.detectDown, place=turtle.placeDown, attack=turtle.attackDown}
  48.  
  49. fwd = worward
  50.  
  51. local at = {x=0,y=0,z=0}
  52. local d = {x=0,z=1}
  53.  
  54. forward.at = function()  
  55.   return {x=at.x+d.x, y=at.y, z=at.z+d.z}
  56. end
  57.  
  58. up.at = function()
  59.   return {x=at.x, y=at.y+1, z=at.z}
  60. end
  61.  
  62. down.at = function()
  63.   return {x=at.x, y=at.y-1, z=at.z}
  64. end
  65.  
  66. function shouldDig(block)
  67.   if not min or not max then
  68.     return true
  69.   end
  70.   return block.x>=min.x and block.x<=max.x and block.y>=min.y and block.y<=max.y and block.z>=min.z and block.z<=max.z
  71. end
  72.  
  73. function left()
  74.     turtle.turnLeft()
  75.     d.x, d.z = -d.z, d.x
  76. end
  77.  
  78. function right()
  79.     turtle.turnRight()
  80.   d.x, d.z = d.z, -d.x
  81. end
  82.  
  83. function turn(to)
  84.   x,z = sign(to.x), sign(to.z)
  85.   if x == d.x and z == d.z then
  86.     return
  87.   elseif x == -d.x and z == -d.z then
  88.     left()
  89.     left()
  90.   elseif z ~= 0 then
  91.     if z == -d.x then right() else left() end
  92.   elseif x ~= 0 then
  93.     if x == d.z then right() else left() end
  94.   end  
  95. end
  96.  
  97. function refuel()
  98.   for i,n in ipairs(slot.fuel) do
  99.     turtle.select(n)
  100.     if turtle.refuel(1) then
  101.       turtle.select(1)
  102.       return true
  103.     end
  104.   end
  105.   turtle.select(1)
  106.   return false
  107. end
  108.  
  109. function distanceToBase()
  110.   return math.abs(at.x) + math.abs(at.y) + math.abs(at.z)
  111. end
  112.  
  113. function checkFuel()
  114.   if turtle.getFuelLevel()<10 then
  115.     while not refuel() do
  116.       print("Need fuel...")
  117.       if not slot[1].designation then
  118.         designate(1,"fuel")
  119.       end
  120.       sleep(3)
  121.     end
  122.   end
  123. end
  124.  
  125. function move(dir)
  126.   checkFuel()
  127.   if dir.move() then
  128.     at = dir.at()
  129.     return true
  130.   else
  131.     return false
  132.   end
  133. end
  134.  
  135. function disposeJunk()
  136.   for i,n in ipairs(slot.junk) do
  137.     if turtle.getItemCount(n) >60 then
  138.       turtle.select(n)
  139.       turtle.drop(turtle.getItemCount(n)-1)
  140.     end
  141.   end
  142. end
  143.  
  144. function dig(dir)
  145.   local result = true
  146.   if shouldDig(dir.at()) then
  147.     turtle.select(5)
  148.     result = dir.dig()
  149.     disposeJunk()
  150.     checkOverflow()
  151.     return result
  152.   end  
  153. end
  154.  
  155. function advance(dir, steps)
  156.   if steps == nil then
  157.     steps = 1
  158.   end  
  159.   for step = 1,steps do
  160.     while not move(dir) do
  161.       if dir.detect() then
  162.         if not dig(dir) then
  163.           print("oops")
  164.           return false
  165.         end
  166.       elseif not dir.attack() then
  167.         sleep(0.5)
  168.       end
  169.     end
  170.   end
  171. end
  172.  
  173. function goto(to)
  174.   to = {x=to.x or 0,y=to.y or 0,z=to.z or 0}
  175.   if to.y>at.y then
  176.     advance(up, to.y-at.y)
  177.   elseif to.y<at.y then
  178.     advance(down, at.y-to.y)
  179.   end
  180.  
  181.   if to.x ~= at.x then
  182.     turn({x=to.x-at.x})
  183.     advance(forward, math.abs(to.x-at.x))
  184.   end  
  185.   if to.z ~= at.z then
  186.     turn({z=to.z-at.z})
  187.     advance(forward, math.abs(to.z-at.z))
  188.   end
  189. end
  190.  
  191. function moveDir(to)
  192.   if to.y then
  193.     advance(to.y>0 and up or down, math.abs(to.y))
  194.   else
  195.     turn(to)
  196.     advance(forward, math.abs(to.x or to.z))
  197.   end
  198. end
  199.  
  200. function dropLoot()
  201.   local stoppedAt = {x=at.x, y=at.y, z=at.z}
  202.   goto({})
  203.   turn({z=-1})
  204.   for n=1,16 do
  205.     turtle.select(n)
  206.     if slot[n].designation then
  207.       if slot[n].designation ~= "fuel" then
  208.         turtle.drop(turtle.getItemCount(n)-1)
  209.       end
  210.     else
  211.       turtle.drop()
  212.     end
  213.   end
  214.   turtle.select(1)
  215.   goto(stoppedAt)
  216.   return true
  217. end
  218.  
  219. function checkOverflow()
  220.   if not excavating then
  221.     return
  222.   end
  223.   if turtle.getItemCount(16)==0 then
  224.     return false
  225.   end  
  226.   excavating = false
  227.   dropLoot()
  228.   excavating = true
  229.   return true
  230. end
  231.  
  232. function makePlan()  
  233.   planMatrix = {}
  234.   local layer1 = max.y - (math.fmod(size.y,3)==0 and 1 or 0)
  235.   local evenLayer = true
  236.   local y = layer1
  237.   local widthIsEven = math.fmod(size.x,2)==0
  238.   if(layer1<max.y) then
  239.     plan({y=max.y}).dir={y=-1}
  240.   end
  241.   while y>=min.y do
  242.     for x=min.x,max.x do
  243.       local zDir = (evenLayer == (math.fmod(x-min.x, 2)==0)) and 1 or -1
  244.       for z=min.z,max.z do
  245.         plan({x=x,y=y,z=z}).dir = {z=zDir}
  246.       end      
  247.       plan({x=x,y=y,z=(zDir==1 and max.z or min.z)}).dir = {x=evenLayer and 1 or -1}
  248.     end
  249.     local finishAt = {}
  250.     finishAt.x = evenLayer and max.x or min.x
  251.     finishAt.z = widthIsEven and min.z or (evenLayer and max.z or min.z)    
  252.     finishAt.y = y
  253.    
  254.     if(y-2>=min.y) then
  255.       for descent=0,2 do
  256.         --printArray({x=finishAt.x,y=finishAt.y-descent, z=finishAt.z})
  257.         plan({x=finishAt.x,y=finishAt.y-descent, z=finishAt.z}).dir = {y=-1}
  258.       end
  259.     else
  260.       plan(finishAt).dir = nil
  261.     end
  262.     y = y - 3
  263.     evenLayer = not evenLayer
  264.   end
  265. end
  266.  
  267. function digAround()
  268.   dig(up)
  269.   dig(down)
  270. end
  271.  
  272. function main()
  273.   arg = {}
  274.  
  275.   local command = tArgs[1] or ""
  276.  
  277.   for argbit in string.gmatch(command, '%a[%d-]*') do
  278.     argletter = string.sub(argbit,1,1)
  279.     argnum = string.sub(argbit,2)
  280.     arg[argletter] = tonumber(argnum)
  281.   end
  282.  
  283.   local commands = {
  284.     back = function() turn({z=-1}) end,
  285.     right = function() turn({x=1}) end,
  286.     left = function() turn({x=-1}) end,
  287.     fuel = function() print("Fuel: " .. turtle.getFuelLevel()) end,
  288.   }
  289.  
  290.   for n=1,16 do
  291.     slot[n] = {}
  292.     if turtle.getItemCount(n) > 0 then
  293.       turtle.select(n)
  294.       if n<=4 then      
  295.         if turtle.getItemCount(n)>1 then
  296.           designate(n,"fuel")
  297.         end
  298.       elseif n<=8 then
  299.         designate(n,"junk")
  300.       --elseif n<=12 then
  301.       --  designate(n,"skip")
  302.       end
  303.     end
  304.   end  
  305.  
  306.   if commands[command] then
  307.     commands[command]()
  308.   elseif arg.x or arg.y or arg.z then
  309.     goto(arg)
  310.   else
  311.     print("Fuel: " .. turtle.getFuelLevel())
  312.  
  313.     max = {x=(arg.r or 1) - 1, y=(arg.u or 1) - 1, z=(arg.f or 1) - 1}
  314.     min = {x=-(arg.l or 1) + 1, y=-(arg.d or 1) + 1, z=-(arg.b or 1) + 1}
  315.     size = {x=max.x-min.x+1, y=max.y-min.y+1, z=max.z-min.z+1}
  316.  
  317.     excavating = true
  318.     makePlan()  
  319.     goto({x=min.x, z=min.z, y=max.y})
  320.     digAround()
  321.     while(plan(at).dir) do
  322.       moveDir(plan(at).dir)
  323.       digAround()
  324.     end
  325.     print("End plan")
  326.     goto({})
  327.     dropLoot()
  328.   end
  329. end
  330.  
  331. main()
Advertisement
Add Comment
Please, Sign In to add comment