Advertisement
GnoX

gxm

Aug 22nd, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.92 KB | None | 0 0
  1. --- API GnoX Move ---
  2.  
  3. local savePosition = true
  4. local lastSlot = 16
  5. local data  = { position = {} }
  6. local rt    = turtle.turnRight
  7. local lt    = turtle.turnLeft
  8. local fd    = turtle.forward
  9. local bk    = turtle.back
  10. local mup   = turtle.up
  11. local dn    = turtle.down
  12. local up
  13.  
  14. ----------------------------------------------------------------------
  15. --------------------- Data manipulation and gps ----------------------
  16. ----------------------------------------------------------------------
  17.  
  18. local isGps = function()
  19.     return gps.locate()
  20. end
  21.  
  22. local parseTurtleData = function()
  23.     if not data.position.x then
  24.         if isGps() then
  25.             data.position.x, data.position.y, data.position.z = gps.locate()
  26.             data.position.f = findDirection()
  27.         else
  28.             print("No GPS found. Save position manually? y/n")
  29.             if read() == "y" then
  30.                 local act = { "x", "y", "z", "f" }
  31.                 for k in ipairs(act) do
  32.                     local inp, yes = nil, nil
  33.                     while not tonumber(inp) do
  34.                         if yes then print("Not a number, try again!") end
  35.                         yes = 1
  36.                         write("Position "..act[k]..": ")
  37.                         inp = read()
  38.                     end
  39.                     data.position[act[k]] = tonumber(inp)
  40.                 end
  41.             end
  42.         end
  43.     end
  44.     return textutils.serialize(data)
  45. end
  46.  
  47. save = function()
  48.     serialized = parseTurtleData();
  49.     file = fs.open("position", "w")
  50.     if file then        
  51.         file.write(serialized)
  52.     else
  53.         print("Error in opening the file")
  54.     end
  55.     file.close()
  56.     return true
  57. end
  58.  
  59. load = function()
  60.     if fs.exists("position") then
  61.         file = fs.open("position", "r")
  62.         if file then
  63.             content = file.readAll()            
  64.             if content ~= "" then
  65.                 data = textutils.unserialize(content)
  66.                 file.close()
  67.             else
  68.                 file.close()
  69.                 fs.delete("position")
  70.             end
  71.         end              
  72.         if not data.position.x then
  73.             print("There was a problem in loading positions. Trying to save.")
  74.             save()
  75.         end            
  76.  
  77.         return true
  78.     else
  79.         print("Could not find file 'position'!")
  80.         return save()
  81.     end
  82. end
  83.  
  84. resetData = function()
  85.     data = nil
  86.     if fs.exists("position") then fs.delete("position") end
  87. end
  88.  
  89. getData = function()
  90.     return data
  91. end
  92.  
  93. setAction = function( action )
  94.     data.action = action
  95.     save()
  96. end
  97.  
  98. getAction = function()
  99.     return data.action
  100. end
  101.  
  102. positionTrack = function(state)
  103.     savePosition = state
  104. end
  105.  
  106. relative = function()
  107.     data.position.x, data.position.y, data.position.z, data.position.f = 0, 0, 0, 0
  108.     save()
  109. end
  110.  
  111. ---------------------------------------------
  112. ------------------ Main ---------------------
  113. ---------------------------------------------
  114.  
  115. local changeHorizontalPos = function( move )
  116.     if savePosition then
  117.         if data.position.f == 0 then
  118.             data.position.z = data.position.z + move
  119.         elseif data.position.f == 1 then
  120.             data.position.x = data.position.x - move
  121.         elseif data.position.f == 2 then
  122.             data.position.z = data.position.z - move
  123.         elseif data.position.f == 3 then
  124.             data.position.x = data.position.x + move
  125.         end
  126.     end
  127. end
  128.  
  129. local changeVerticalPos = function( move )
  130.     if savePosition then
  131.         data.position.y = data.position.y + move
  132.     end
  133. end
  134.  
  135. correctY = function( y )    
  136.     if y > 2 then
  137.         return correctY(y - 3)
  138.     else
  139.         return y
  140.     end
  141. end
  142.  
  143. correctDir = function( dir )
  144.     if savePosition then
  145.         if dir > 3 then
  146.             return correctDir(dir - 4)
  147.         elseif dir < 0 then
  148.             return correctDir(dir + 4)
  149.         else
  150.             return dir
  151.         end
  152.     end
  153. end
  154.  
  155. findDirection = function()
  156.     if isGps() then
  157.         lastX, _, lastZ = gps.locate()
  158.         bk()
  159.         x, _, z = gps.locate()
  160.         fd()
  161.  
  162.         if z < lastZ then
  163.             return 0
  164.         elseif x > lastX then
  165.             return 1
  166.         elseif z > lastZ then
  167.             return 2
  168.         elseif x < lastX then
  169.             return 3
  170.         end
  171.     end
  172. end
  173.  
  174. --------------------------------------------------------------------
  175. --  Moves a turtle in any way
  176. --  @returns void
  177. --  @arguments
  178. --  move     ---     type table of function
  179. --  n        ---     number of moves
  180. --  dig      ---     (optional) if turtle cannot go forward then dig
  181. --  attack   ---     (optional) same thing with attack
  182. --  suck     ---     (optional) same thing with suck
  183. --------------------------------------------------------------------
  184.  
  185. local _move = function( move, n, dig, attack, suck )
  186.     if not n then n = 1 end
  187.     for i = 1, n do
  188.         local count = 0
  189.         while not move() do
  190.             if dig and not dig() then
  191.                 if not attack() then
  192.                     count = count + 1
  193.                     if count > 10 then
  194.                         return false
  195.                     else
  196.                         suck()
  197.                     end
  198.                 end
  199.             end
  200.         end
  201.  
  202.         if savePosition then
  203.             if move == fd then
  204.                 changeHorizontalPos(1)
  205.             elseif move == bk then
  206.                 changeHorizontalPos(-1)
  207.             elseif move == mup then
  208.                 changeVerticalPos(1)
  209.             elseif move == dn then
  210.                 changeVerticalPos(-1)
  211.             end
  212.             save()
  213.         end        
  214.     end
  215.     return true
  216. end
  217.  
  218. --------------------------------------------------------------------
  219. --  Turns turtle in desired way
  220. --  @returns integer sides
  221. --  @arguments
  222. --  side     ---     hand of turn
  223. --  n        ---     number of turns
  224. --------------------------------------------------------------------
  225.  
  226. local _turn = function( side, n )
  227.     if not n then n = 1 end
  228.     for i = 1, n do
  229.         if savePosition then
  230.             if side == rt then
  231.                 data.position.f = correctDir(data.position.f + 1)
  232.             elseif side == lt then
  233.                 data.position.f = correctDir(data.position.f - 1)
  234.             end
  235.             save()
  236.         end
  237.  
  238.         side()
  239.     end
  240.     return n
  241. end
  242.  
  243. local _place = function(slot, p, dig, facing)
  244.     if type(slot) == "function" then
  245.         for i=1, 16 do
  246.             turtle.select(i)
  247.             if p() then return i end
  248.         end
  249.     elseif facing then
  250.         _turnTo(facing)
  251.     end
  252.     if slot == 1 then
  253.         if lastSlot then
  254.             slot = lastSlot
  255.         end
  256.     elseif slot == 0 then
  257.         slot = 1
  258.     elseif slot == -1 then
  259.         return true
  260.     end
  261.     if turtle.getItemCount(slot) > 1 then
  262.         lastSlot = slot
  263.         turtle.select(slot)
  264.         if not p() then
  265.             dig()
  266.             return p()
  267.         end
  268.     else
  269.         while true do
  270.             for i = 1, 16 do
  271.                 if turtle.getItemCount(i) > 0 then
  272.                     turtle.select(i)
  273.                     if turtle.compareTo(slot) then
  274.                         turtle.transferTo(slot)
  275.                         turtle.select(slot)
  276.                         if turtle.getItemCount(slot) > 1 then
  277.                             if not p() then
  278.                                 dig()
  279.                                 return p()
  280.                             else return true end
  281.                         end
  282.                     end
  283.                 end
  284.             end
  285.             sleep(5)
  286.             print("Cannot find required building block. Retrying...")
  287.         end
  288.     end
  289. end
  290.  
  291. local _turnTo = function( to )
  292.     from = data.position.f
  293.     if from == 0 then
  294.         if to == 1 then
  295.             right()
  296.         elseif to == 2 then
  297.             right(2)
  298.         elseif to == 3 then
  299.             left()
  300.         end
  301.    elseif from == 1 then
  302.         if to == 0 then
  303.             left()
  304.         elseif to == 2 then
  305.             right()
  306.         elseif to == 3 then
  307.             right(2)
  308.         end
  309.    elseif from == 2 then
  310.         if to == 0 then
  311.             right(2)
  312.         elseif to == 1 then
  313.             left()
  314.         elseif to == 3 then
  315.             right()
  316.         end
  317.    elseif from == 3 then
  318.         if to == 0 then
  319.             right()
  320.         elseif to == 1 then
  321.             right(2)
  322.         elseif to == 2 then
  323.             left()
  324.         end
  325.    end  
  326. end
  327.  
  328. left = function( n )
  329.     _turn(lt, n)
  330. end
  331.  
  332. right = function( n )
  333.     _turn(rt, n)
  334. end
  335.  
  336. --local _go_back = function()
  337. --    right(2)
  338. --    if not turtle.dig() then
  339. --        if turtle.attack() then
  340. --            turtle.suck()
  341. --        end
  342. --    end
  343. --    right(2)
  344. --    return true
  345. --end
  346.  
  347. local _go_back = function()
  348.     addToQueue("rr!D?a:orr")
  349. end
  350.  
  351. forward = function( n )
  352.     return _move( fd, n, turtle.dig, turtle.attack, turtle.suck )
  353. end
  354.  
  355. back = function( n )
  356.     _move( bk, n, _go_back )
  357. end
  358.  
  359. up = function( n )
  360.     _move( mup, n, turtle.digUp, turtle.attackUp, turtle.suckUp )
  361. end
  362.  
  363. down = function( n )
  364.     _move( dn, n, turtle.digDown, turtle.attackDown, turtle.suckDown )
  365. end
  366.  
  367. sForward = function( n )
  368.     if not n then n = 1 end
  369.     for i = 1, n do
  370.         while turtle.detect() or not _move(fd) do
  371.             up()
  372.         end        
  373.     end
  374. end
  375.  
  376. sUp = function( n )    
  377.     n = n or 1
  378.     for i = 1, n do
  379.         while turtle.detectUp() or not _move(mup) do
  380.             sleep(1)
  381.         end
  382.     end
  383. end
  384.  
  385. sDown = function( n )
  386.     if not n then n = 1 end
  387.     for i = 1, n do
  388.         while turtle.detectDown() or not _move(dn) do
  389.             sleep(1)
  390.         end
  391.     end
  392. end
  393.  
  394. place = function()
  395.     return _place(turtle.place)
  396. end
  397.  
  398. placeUp = function()
  399.     return _place(turtle.placeUp)
  400. end
  401.  
  402. placeDown = function()
  403.     return _place(turtle.placeDown)
  404. end
  405.  
  406. cPlace = function(comparationSlot)
  407.     _place(comparationSlot, turtle.place, turtle.dig)
  408. end
  409.  
  410. cPlaceDown = function(comparationSlot)
  411.     _place(comparationSlot, turtle.placeDown, turtle.digDown)
  412. end
  413.  
  414. cPlaceUp = function(comparationSlot)
  415.     _place(comparationSlot, turtle.placeUp, turtle.digUp)
  416. end
  417.  
  418. fPlace = function(slot, facing)
  419.     _place(slot, turtle.place, turtle.dig, facing)
  420. end
  421.  
  422. fPlaceUp = function(slot, facing)
  423.     _place(slot, turtle.placeUp, turtle.digUp, facing)
  424. end
  425.  
  426. fPlaceDown = function(slot, facing)
  427.     _place(slot, turtle.placeDown, turtle.digDown, facing)
  428. end
  429.  
  430. local hasFuel = function( n )
  431.     for i = 1, 16 do        
  432.         turtle.select(i)
  433.         while turtle.refuel(1) do
  434.             print(turtle.getFuelLevel())
  435.             if turtle.getFuelLevel() >= n then
  436.                 return true
  437.             end            
  438.         end        
  439.     end
  440.     return false
  441. end
  442.  
  443. getFuelAbove = function( n )
  444.     if turtle.getFuelLevel() <= n then
  445.         while not hasFuel(n) do            
  446.             sleep(5)
  447.         end
  448.     end
  449.     return turtle.getFuelLevel()
  450. end
  451.  
  452. goto = function( x, y, z, f )
  453.     if savePosition then
  454.         data.gotoBack = {}
  455.         data.gotoBack.x = data.position.x
  456.         data.gotoBack.y = data.position.y
  457.         data.gotoBack.z = data.position.z
  458.         data.gotoBack.f = data.position.f
  459.         save()
  460.     end
  461.  
  462.     if data.position.x > x then
  463.         _turnTo(1)
  464.         sForward(data.position.x - x)
  465.     elseif data.position.x < x then
  466.         _turnTo(3)
  467.         sForward(x - data.position.x)
  468.     end
  469.  
  470.     if data.position.z > z then
  471.         _turnTo(2)
  472.         sForward(data.position.z - z)
  473.     elseif data.position.z < z then
  474.         _turnTo(0)
  475.         sForward(z - data.position.z)
  476.     end
  477.    
  478.     if data.position.y > y then
  479.         sDown(data.position.y - y)
  480.     elseif data.position.y < y then
  481.         up(y - data.position.y)
  482.     end
  483.  
  484.     if f then
  485.         _turnTo(f)
  486.     end
  487. end
  488.  
  489. goBack = function()
  490.     if data.gotoBack then
  491.         goto(data.gotoBack.x, data.gotoBack.y, data.gotoBack.z, data.gotoBack.f)
  492.     end
  493.     save()
  494. end
  495.  
  496. local actions = {
  497.     ["f"] = forward,
  498.     ["b"] = back,
  499.     ["u"] = up,
  500.     ["d"] = down,
  501.     ["l"] = left,
  502.     ["r"] = right,
  503.     ["U"] = turtle.digUp,
  504.     ["D"] = turtle.digDown,
  505.     ["F"] = turtle.dig,
  506.     ["^"] = turtle.placeUp,
  507.     ["-"] = turtle.place,
  508.     ["_"] = turtle.placeDown,
  509.     ["q"] = turtle.detectUp,
  510.     ["a"] = turtle.detect,
  511.     ["y"] = turtle.detectDown,
  512.     ["Q"] = turtle.compareUp,
  513.     ["A"] = turtle.compare,
  514.     ["Y"] = turtle.compareDown,
  515.     ["c"] = turtle.compareTo,
  516.     ["o"] = turtle.attack,
  517.     [";"] = cPlace,
  518.     [","] = cPlaceDown,
  519.     ["p"] = cPlaceUp,
  520.     ["z"] = fPlaceUp,
  521.     ["h"] = fPlace,
  522.     ["n"] = fPlaceDown,
  523.     ["s"] = turtle.select
  524. }
  525.  
  526. local isValidPatternMove = function( cmd )
  527.     for k in pairs(actions) do
  528.         if cmd == k then
  529.             return true
  530.         end
  531.     end
  532.     return false
  533. end
  534.  
  535. regExecPattern = function( pattern )
  536.     data.pattern = pattern
  537.     save()
  538. end
  539.  
  540. addToExecQueue = function( pattern )
  541.     data.pattern = pattern + data.pattern
  542. end
  543.  
  544. exec = function( pattern )
  545.     if not pattern then pattern = data.pattern or "" end
  546.     while pattern ~= "" do
  547.         actual       = pattern:match ("%d*!?[%a%-_^,;]%??")
  548.         actual       = actual:find("?") and pattern:match("%d*!?[%a%-_^,;]%?%d*[%a%-_^,;]:?") or actual
  549.         actual       = actual:find(":") and pattern:match("%d*!?[%a%-_^,;]%?%d*[%a%-_^,;]:%d*[%a%-_^,;]") or actual
  550.         cond         = (actual:find(":") and actual:match("!?[%a%-_^,;]%?%d*[%a%-_^,;]:%d*[%a%-_^,;]") or actual:match("!?[%a%-_^]%?[%a%-_^]")) or false
  551.         moves        = tonumber(actual:match("%d*")) or 1
  552.         cmd          = actual:match  ("[%a%-_^,;]")
  553.         pattern      = pattern:sub   (actual:len() + 1 )
  554.         if cond then
  555.             local firstMove, secondMove
  556.             a = cond:find("!") and 2 or 1
  557.             if cond:find("c") then
  558.                 if not cond:find("!") then
  559.                     condition = actions[cond:sub(1, 1)](moves)
  560.                 else
  561.                     condition = not actions[cond:sub(2, 2)](moves)
  562.                 end
  563.             else
  564.                 condition = not cond:find("!") and actions[cond:sub(1, 1)]() or not actions[cond:sub(2, 2)]()
  565.             end
  566.  
  567.             if cond:find("%d*") then
  568.                 firstMove = cond:match("?%d*"):gsub("?", "") or 1
  569.                 secondMove = cond:match(":%d*"):gsub(":", "") or 1
  570.                 cond = cond:gsub("%?(%d*)", "?"):gsub(":(%d*)", ":")
  571.             end
  572.  
  573.             firstCmd  = isValidPatternMove(cond:sub(a+2, a+2)) and cond:sub(a+2, a+2) or false
  574.             secondCmd = isValidPatternMove(cond:sub(a+4, a+4)) and cond:sub(a+4, a+4) or false
  575.  
  576.             if savePosition then              
  577.                 data.pattern = pattern
  578.                 save()
  579.             end                        
  580.             if condition and firstCmd then
  581.                 actions[firstCmd](tonumber(firstMove))
  582.             elseif secondCmd then
  583.                 actions[secondCmd](tonumber(secondMove))
  584.             end
  585.             if savePosition then
  586.                 save()
  587.             end
  588.             os.queueEvent("moved")
  589.         else
  590.             if isValidPatternMove(cmd) then
  591.                 if cmd == "," or cmd == "s" or cmd == "p" or cmd == ";" then
  592.                     actions[cmd](moves)
  593.                 else
  594.                     for i = 1, moves do
  595.                         if savePosition then
  596.                             local move = moves - i
  597.                             local tCmd = cmd
  598.                             if move == 0 then move = "" tCmd = "" end
  599.                             data.pattern = tostring(move)..tCmd..pattern
  600.                             save()
  601.                         end
  602.                         actions[cmd]()
  603.                         if savePosition then
  604.                             save()
  605.                         end
  606.                         os.queueEvent("moved")
  607.                         sleep(.002)
  608.                     end
  609.                 end
  610.             else
  611.                 print("Not a valid move!")
  612.             end
  613.         end
  614.     end
  615. end
  616.  
  617. allItemCount = function()
  618.     local count = 0
  619.     for i = 1, 16 do
  620.         count = count + turtle.getItemCount(i)
  621.     end
  622.     return count
  623. end
  624.  
  625. canTakeMoreItems = function()
  626.     for i = 1, 16 do
  627.         if turtle.getItemCount(i) == 0 then
  628.             return true
  629.         end
  630.     end
  631.     return false
  632. end
  633.  
  634. slotHasMaterial = function(slot)
  635.     return turtle.getItemCount(slot) > 0
  636. end
  637.  
  638. findSlotWithMaterial = function(comparationSlot)
  639.     for i = 1, 16 do
  640.         if turtle.getItemCount(i) > 1 then
  641.             if i ~= comparationSlot then
  642.                 turtle.select(i)
  643.                 if turtle.compareTo(comparationSlot) then
  644.                     return true
  645.                 end
  646.             end
  647.         end
  648.         return false
  649.     end
  650. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement