Advertisement
denvys5

Ex

Nov 1st, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.10 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. local direction = {
  25.         ["forw"]  = 1,
  26.         ["right"] = 2,
  27.         ["back"]  = 3,
  28.         ["left"]  = 4,
  29. }
  30. local dir = direction.forw
  31. local pos = {
  32.         ["x"]   = 0,
  33.         ["y"]   = 0,
  34.         ["z"]   = 0,
  35. }
  36.  
  37. local name = "quarry"
  38. local done = false
  39. globalState = 0
  40. aState = 0 --состояние автомата
  41. tmpState = 0
  42. local lenght,width,height = 0,0,0
  43. local xt,yt,zt,dt = 0,0,0,direction.forw
  44. mode = 1
  45.  
  46. local function saveState()
  47.         name = shell.getRunningProgram()
  48.     local file = fs.open(name..".save","w")
  49.         file.writeLine(mode)
  50.         file.writeLine(globalState)
  51.     file.writeLine(pos.x)
  52.     file.writeLine(pos.y)
  53.     file.writeLine(pos.z)
  54.     file.writeLine(dir)
  55.     file.writeLine(tmpState)
  56.     file.writeLine(aState)
  57.     file.writeLine(xt)
  58.     file.writeLine(yt)
  59.     file.writeLine(zt)
  60.     file.writeLine(dt)
  61.         file.writeLine(lenght)
  62.     file.writeLine(width)
  63.     file.writeLine(height)
  64.         file.writeLine(done)
  65.     file.close()
  66. end
  67.  
  68. local function readState()
  69.         name = shell.getRunningProgram()
  70.         if not fs.exists(name..".save") then
  71.                 return false
  72.         end
  73.         local file = fs.open(name..".save","r")
  74.         mode =          tonumber(file.readLine())
  75.     globalState = tonumber(file.readLine())
  76.     pos.x =     tonumber(file.readLine())
  77.         pos.y =         tonumber(file.readLine())
  78.         pos.z =         tonumber(file.readLine())
  79.         dir =           tonumber(file.readLine())
  80.         tmpState =      tonumber(file.readLine())
  81.         aState =        tonumber(file.readLine())
  82.         xt =            tonumber(file.readLine())
  83.         yt =            tonumber(file.readLine())
  84.         zt =            tonumber(file.readLine())
  85.         dt =            tonumber(file.readLine())      
  86.         lenght =        tonumber(file.readLine())
  87.     width =     tonumber(file.readLine())
  88.     height =    tonumber(file.readLine())
  89.         done =          file.readLine()
  90.         if done == "true" then
  91.                 done = true
  92.         else
  93.                 done = false
  94.         end
  95.     file.close()
  96.         return true
  97. end
  98.  
  99. local function setStartup()
  100.         if fs.exists("startup") then
  101. --              if fs.exists("startup.old") then
  102.                         fs.delete("startup")
  103. --              else
  104. --                      shell.run("move","startup","startup.old")
  105. --              end
  106.         end
  107.         name = shell.getRunningProgram()
  108.         local file = fs.open("startup","w")
  109.         file.writeLine("shell.run(\""..name.."\")")
  110.         file.close()
  111. end
  112.  
  113. local function delStartup()
  114.         if fs.exists("startup") then
  115.                 fs.delete("startup")
  116.         end
  117. --      if fs.exists("startup.old") then
  118. --              shell.run("move","startup.old","startup")
  119. --      end
  120.         name = shell.getRunningProgram()
  121.         if fs.exists(name..".save") then
  122.                 fs.delete(name..".save")
  123.         end
  124. end
  125.  
  126. function designate(n, type)
  127.   print("Designate "..n.." as "..type)
  128.   slot[n].designation = type
  129.   table.insert(slot[type],n)
  130. end
  131.  
  132. function plan(block)
  133.     local ind = (block.x or 0)*100000000 + (block.y or 0)*10000 + (block.z or 0)
  134.     planMatrix[ind] = planMatrix[ind] or {}
  135.     return planMatrix[ind]
  136. end
  137.  
  138.  
  139. --[[
  140. if #tArgs ~= 1 then
  141.         print( "Usage= excavate <diameter>" )
  142.         return
  143. end
  144. ]]
  145.        
  146. up={name="up", dig=turtle.digUp, move=turtle.up, detect=turtle.detectUp, place=turtle.placeUp, attack=turtle.attackUp, dy}
  147. forward={name="forward", dig=turtle.dig, move=turtle.forward, detect=turtle.detect, place=turtle.place, attack=turtle.attack}
  148. down={name="down",dig=turtle.digDown, move=turtle.down, detect=turtle.detectDown, place=turtle.placeDown, attack=turtle.attackDown}
  149.  
  150. fwd = worward
  151.  
  152. local at = {x=0,y=0,z=0}
  153. local d = {x=0,z=1}
  154.  
  155. forward.at = function()  
  156.   return {x=at.x+d.x, y=at.y, z=at.z+d.z}
  157. end
  158.  
  159. up.at = function()
  160.   return {x=at.x, y=at.y+1, z=at.z}
  161. end
  162.  
  163. down.at = function()
  164.   return {x=at.x, y=at.y-1, z=at.z}
  165. end
  166.  
  167. function shouldDig(block)
  168.   if not min or not max then
  169.     return true
  170.   end
  171.   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
  172. end
  173.  
  174. function left()
  175.         turtle.turnLeft()
  176.         d.x, d.z = -d.z, d.x
  177. end
  178.  
  179. function right()
  180.         turtle.turnRight()
  181.   d.x, d.z = d.z, -d.x
  182. end
  183.  
  184. function turn(to)
  185.   x,z = sign(to.x), sign(to.z)
  186.   if x == d.x and z == d.z then
  187.     return
  188.   elseif x == -d.x and z == -d.z then
  189.     left()
  190.     left()
  191.   elseif z ~= 0 then
  192.     if z == -d.x then right() else left() end
  193.   elseif x ~= 0 then
  194.     if x == d.z then right() else left() end
  195.   end  
  196. end
  197.  
  198. function refuel()
  199.   for i,n in ipairs(slot.fuel) do
  200.     turtle.select(n)
  201.     if turtle.refuel(1) then
  202.       turtle.select(1)
  203.       return true
  204.     end
  205.   end
  206.   turtle.select(1)
  207.   return false
  208. end
  209.  
  210. function distanceToBase()
  211.   return math.abs(at.x) + math.abs(at.y) + math.abs(at.z)
  212. end
  213.  
  214. function checkFuel()
  215.   if turtle.getFuelLevel()<10 then
  216.     while not refuel() do
  217.       print("Need fuel...")
  218.       if not slot[1].designation then
  219.         designate(1,"fuel")
  220.       end
  221.       sleep(3)
  222.     end
  223.   end
  224. end
  225.  
  226. function move(dir)
  227.   checkFuel()
  228.   if dir.move() then
  229.     at = dir.at()
  230.     return true
  231.   else
  232.     return false
  233.   end
  234. end
  235.  
  236. function disposeJunk()
  237.   for i,n in ipairs(slot.junk) do
  238.     if turtle.getItemCount(n) >60 then
  239.       turtle.select(n)
  240.       turtle.drop(turtle.getItemCount(n)-1)
  241.     end
  242.   end
  243. end
  244.  
  245. function dig(dir)
  246.   local result = true
  247.   if shouldDig(dir.at()) then
  248.     turtle.select(5)
  249.     result = dir.dig()
  250.     disposeJunk()
  251.     checkOverflow()
  252.     return result
  253.   end  
  254. end
  255.  
  256. function advance(dir, steps)
  257.   if steps == nil then
  258.     steps = 1
  259.   end  
  260.   for step = 1,steps do
  261.     while not move(dir) do
  262.       if dir.detect() then
  263.         if not dig(dir) then
  264.           print("oops")
  265.           return false
  266.         end
  267.       elseif not dir.attack() then
  268.         sleep(0.5)
  269.       end
  270.     end        
  271.   end
  272. end
  273.  
  274. function goto(to)
  275.   to = {x=to.x or 0,y=to.y or 0,z=to.z or 0}
  276.   if to.y>at.y then
  277.     advance(up, to.y-at.y)
  278.   elseif to.y<at.y then
  279.     advance(down, at.y-to.y)
  280.   end
  281.  
  282.   if to.x ~= at.x then
  283.     turn({x=to.x-at.x})
  284.     advance(forward, math.abs(to.x-at.x))
  285.   end  
  286.   if to.z ~= at.z then
  287.     turn({z=to.z-at.z})
  288.     advance(forward, math.abs(to.z-at.z))
  289.   end
  290. end
  291.  
  292. function moveDir(to)
  293.   if to.y then
  294.     advance(to.y>0 and up or down, math.abs(to.y))
  295.   else
  296.     turn(to)
  297.     advance(forward, math.abs(to.x or to.z))
  298.   end
  299. end
  300.  
  301. function dropLoot()
  302.   local stoppedAt = {x=at.x, y=at.y, z=at.z}
  303.   goto({})
  304.   turn({z=-1})
  305.   for n=1,16 do
  306.     turtle.select(n)
  307.     if slot[n].designation then
  308.       if slot[n].designation ~= "fuel" then
  309.         turtle.dropUp(turtle.getItemCount(n)-1)
  310.       end
  311.     else
  312.       turtle.dropUp()
  313.     end
  314.   end
  315.   turtle.select(1)
  316.   goto(stoppedAt)
  317.   return true
  318. end
  319.  
  320. function checkOverflow()
  321.   if not excavating then
  322.     return
  323.   end
  324.   if turtle.getItemCount(16)==0 then
  325.     return false
  326.   end  
  327.   excavating = false
  328.   dropLoot()
  329.   excavating = true
  330.   return true
  331. end
  332.  
  333. function makePlan()  
  334.   planMatrix = {}
  335.   local layer1 = max.y - (math.fmod(size.y,3)==0 and 1 or 0)
  336.   local evenLayer = true
  337.   local y = layer1
  338.   local widthIsEven = math.fmod(size.x,2)==0
  339.   if(layer1<max.y) then
  340.     plan({y=max.y}).dir={y=-1}
  341.   end
  342.   while y>=min.y do
  343.     for x=min.x,max.x do
  344.       local zDir = (evenLayer == (math.fmod(x-min.x, 2)==0)) and 1 or -1
  345.       for z=min.z,max.z do
  346.         plan({x=x,y=y,z=z}).dir = {z=zDir}
  347.       end      
  348.       plan({x=x,y=y,z=(zDir==1 and max.z or min.z)}).dir = {x=evenLayer and 1 or -1}
  349.     end
  350.     local finishAt = {}
  351.     finishAt.x = evenLayer and max.x or min.x
  352.     finishAt.z = widthIsEven and min.z or (evenLayer and max.z or min.z)    
  353.     finishAt.y = y
  354.    
  355.     if(y-2>=min.y) then
  356.       for descent=0,2 do
  357.         --printArray({x=finishAt.x,y=finishAt.y-descent, z=finishAt.z})
  358.         plan({x=finishAt.x,y=finishAt.y-descent, z=finishAt.z}).dir = {y=-1}
  359.       end
  360.     else
  361.       plan(finishAt).dir = nil
  362.     end
  363.     y = y - 3
  364.     evenLayer = not evenLayer
  365.   end
  366. end
  367.  
  368. function digAround()
  369.   dig(up)
  370.   dig(down)
  371. end
  372.  
  373. function main()
  374.   arg = {}
  375.  
  376.   local command = tArgs[1] or ""
  377.  
  378.   for argbit in string.gmatch(command, '%a[%d-]*') do
  379.     argletter = string.sub(argbit,1,1)
  380.     argnum = string.sub(argbit,2)
  381.     arg[argletter] = tonumber(argnum)
  382.   end
  383.  
  384.   local commands = {
  385.     back = function() turn({z=-1}) end,
  386.     right = function() turn({x=1}) end,
  387.     left = function() turn({x=-1}) end,
  388.     fuel = function() print("Fuel: " .. turtle.getFuelLevel()) end,
  389.   }
  390.  
  391.   for n=1,16 do
  392.     slot[n] = {}
  393.     if turtle.getItemCount(n) > 0 then
  394.       turtle.select(n)
  395.       if n<=4 then      
  396.         if turtle.getItemCount(n)>1 then
  397.           designate(n,"fuel")
  398.         end
  399.       elseif n<=8 then
  400.         designate(n,"junk")
  401.       --elseif n<=12 then
  402.       --  designate(n,"skip")
  403.       end
  404.     end
  405.   end  
  406.  
  407.   if commands[command] then
  408.     commands[command]()
  409.   elseif arg.x or arg.y or arg.z then
  410.     goto(arg)
  411.   else
  412.     print("Fuel: " .. turtle.getFuelLevel())
  413.  
  414.     max = {x=(arg.r or 1) - 1, y=(arg.u or 1) - 1, z=(arg.f or 1) - 1}
  415.     min = {x=-(arg.l or 1) + 1, y=-(arg.d or 1) + 1, z=-(arg.b or 1) + 1}
  416.     size = {x=max.x-min.x+1, y=max.y-min.y+1, z=max.z-min.z+1}
  417.  
  418.     excavating = true
  419.     makePlan()  
  420.     goto({x=min.x, z=min.z, y=max.y})
  421.     digAround()
  422.     while(plan(at).dir) do
  423.       moveDir(plan(at).dir)
  424.       digAround()
  425.     end
  426.     print("End plan")
  427.     goto({})
  428.     dropLoot()
  429.   end
  430. end
  431.  
  432. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement