Advertisement
GnoX

exc

Aug 22nd, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.27 KB | None | 0 0
  1. os.unloadAPI("gxm")
  2. os.loadAPI  ("gxm")
  3.  
  4. local tArgs = { ... }
  5. local data = { start = {}, from = {} }
  6.  
  7. if not gxm.load() then gxm.save() end
  8. gxm.positionTrack(true)
  9.  
  10. loadPosition = function()
  11.     if not fs.exists("excavateConfig") then
  12.         data.start = gxm.getData().position
  13.         data.start.f = gxm.correctDir(data.start.f + 2)
  14.         file = fs.open("excavateConfig", "w")
  15.         if file then
  16.             file.write(textutils.serialize(data))
  17.             file.close()
  18.         end        
  19.     else
  20.         file = fs.open("excavateConfig", "r")
  21.         if file then
  22.             data = textutils.unserialize(file.readAll())
  23.             file.close()
  24.         end
  25.     end
  26. end
  27.  
  28. if tArgs[1] == "-b" then
  29.     loadPosition()
  30.     gxm.goto(data.start.x, data.start.y, data.start.z, data.start.f - 2)
  31.     return
  32. elseif tArgs[1] == "-r" then
  33.     gxm.regExecPattern("")
  34.     return
  35. end
  36.  
  37. zSize = tonumber(tArgs[1])
  38. xSize = tonumber(tArgs[2]) or zSize
  39. ySize = tonumber(tArgs[3]) or gxm.getData().position.y or 64
  40.  
  41.  
  42. savePosition = function()
  43.     if fs.exists("excavateConfig") then
  44.         file = fs.open("excavateConfig", "w")
  45.         if file then
  46.             data.from = gxm.getData().position
  47.             file.write(textutils.serialize(data))
  48.             file.close()
  49.         end
  50.     end
  51. end    
  52.  
  53. excavatePattern = function()
  54.     local excString = ""
  55.     local alter     = true
  56.     local excMove   = "UDfr"
  57.     local lastMove  = "UD3d"
  58.  
  59.     local sideX      = xSize % 2 == 0
  60.     local sideZ      = zSize % 2 == 0
  61.     local side       = not sideX and not sideZ
  62.  
  63.     for i = 1, math.ceil(ySize/3) do
  64.         if i == math.ceil(ySize/3) then
  65.             excMove   = gxm.correctY(ySize) == 2 and "U0fr"  or (gxm.correctY(ySize) == 1 and "00fr" or excMove)
  66.         end
  67.         if i == math.ceil(ySize/3) - 1 then lastMove = gxm.correctY(ySize) == 1 and "UD2d" or lastMove end
  68.  
  69.         for j = 1, xSize do
  70.             local cmds      = ""
  71.  
  72.             cmds = excMove:sub(1, -2):rep(zSize - 2) .. excMove:rep(2)
  73.  
  74.             alter = not alter
  75.             if j ~= xSize then
  76.                 cmds = alter and cmds:gsub("r", "l") or cmds
  77.             else
  78.                 if not sideX and not sideZ then
  79.                     side = not side
  80.                 elseif sideX and not sideZ then
  81.                     if i == 1 then
  82.                         side = true
  83.                     elseif i % 2 == 0 then
  84.                         side = not side
  85.                     end
  86.                 elseif sideZ and not sideX then
  87.                     if i == 1 then
  88.                         side = false
  89.                     elseif (i+1) % 2 == 0 then
  90.                         side = not side
  91.                     end
  92.                 else
  93.                     side = true
  94.                 end
  95.  
  96.                 cmds = side and cmds:sub(1, -6) .. "r" .. lastMove or cmds:sub(1, -6) .. "l" .. lastMove
  97.             end
  98.  
  99.             excString = excString .. cmds
  100.         end
  101.         xSize, zSize = zSize, xSize
  102.     end
  103.     return excString:sub(1, -4):gsub("0", "")
  104. end
  105.  
  106. checkInventory = function()
  107.     for i = 1, 16 do
  108.         if turtle.getItemCount(i) == 0 then
  109.             return true
  110.         end
  111.     end
  112.     return false
  113. end
  114.  
  115. unload = function()
  116.     if not turtle.detect() then
  117.         print("Nothing in front of me! Need a chest to drop my stuff! Waiting for a chest or any block as a matter of fact :).")
  118.         while not turtle.detect() do
  119.             sleep(2)
  120.         end
  121.     end
  122.  
  123.     for i = 1, 16 do    
  124.         if turtle.getItemCount(i) > 0 then
  125.             turtle.select(i)
  126.             while turtle.getItemCount(i) ~= 0 do
  127.                 if not turtle.drop() then
  128.                     print("Chest is probably full! Waiting for more space!")                
  129.                     while not turtle.drop() do
  130.                         sleep(2)
  131.                     end
  132.                 end
  133.             end
  134.         end
  135.     end
  136.     print("Unloading complete!")
  137. end
  138.  
  139. checkFuel = function( limit )
  140.     return gxm.getFuelAbove(limit)
  141. end
  142.  
  143. parseAction = function()
  144.     loadPosition()
  145.     if gxm.getAction() == "gotoHome" then
  146.         gxm.goto(data.start.x, data.start.y, data.start.z, data.start.f)
  147.         loadPosition()
  148.         unload()
  149.         gxm.goto(data.from.x, data.from.y, data.from.z, data.from.f)
  150.     elseif gxm.getAction() == "unloading" then
  151.         unload()
  152.         gxm.goto(data.from.x, data.from.y, data.from.z, data.from.f)
  153.     elseif gxm.getAction() == "gotoBack" then
  154.         gxm.goto(data.from.x, data.from.y, data.from.z, data.from.f)
  155.     end
  156. end
  157.  
  158. function main()
  159.     if gxm.getData().pattern == "" or not gxm.getData().pattern then gxm.regExecPattern(excavatePattern()) end
  160.     loadPosition()
  161.     parseAction()
  162.     parallel.waitForAny(
  163.         function()
  164.             local r = coroutine.create(gxm.exec)
  165.             local filter
  166.             local eventData = {}
  167.             while true do
  168.                 if filter == nil or filter == eventData[1] or eventData[1] == "terminate" then
  169.                     _, filter = coroutine.resume(r, unpack(eventData))
  170.                     if coroutine.status(r) == "dead" then return end
  171.                 end
  172.                 if eventData[1] == "moved" then
  173.                     checkFuel(1000)
  174.                     if not checkInventory() then
  175.                         savePosition()
  176.                         gxm.setAction("gotoHome")
  177.                         gxm.goto(data.start.x, data.start.y, data.start.z, data.start.f)
  178.                         gxm.setAction("unloading")
  179.                         unload()
  180.                         gxm.setAction("goBack")
  181.                         loadPosition()
  182.                         gxm.goto(data.from.x, data.from.y, data.from.z, data.from.f)
  183.                         gxm.setAction("")
  184.                         r = coroutine.create(gxm.exec)
  185.                         _, filter = coroutine.resume(r)
  186.                     end
  187.                 end
  188.                 eventData = { os.pullEventRaw() }
  189.             end
  190.             gxm.goto(data.start.x, data.start.y, data.start.z, data.start.f-2)
  191.             return
  192.         end)
  193. end
  194. if tArgs[2] == "-t" or tArgs[3] == "-t" or tArgs[4] == "-t" then
  195.     gxm.regExecPattern(excavatePattern())
  196.     print(gxm.getData().pattern)
  197. else
  198.     main()
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement