PonyKuu

Moving API for CC. [UNTESTED]

Nov 1st, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.08 KB | None | 0 0
  1.     --[[
  2.         location is a global table containing all the coordinates and facing direction of the turtle
  3.         And here is the function to set the position of the turtle and another one to get it.
  4.       ]]--
  5.     location = {x = 0, y = 0, z = 0, f = 0}
  6.      
  7.     function setPosition (x, y, z, f)
  8.         location.x = x
  9.         location.y = y
  10.         location.z = z
  11.         location.f = f
  12.     end
  13.      
  14.     function getPosition ()
  15.         return { x = location.x,
  16.                  y = location.y,
  17.                  z = location.z,
  18.                  f = location.f, } -- return a copy of the location table.
  19.     end
  20.      
  21.     --[[
  22.         And a small function to print the position. For debug purposes
  23.       ]]--
  24.      
  25.     function printPosition ()
  26.         print ("Currrent turtle position:")
  27.         print ("    x = "..location.x)
  28.         print ("    y = "..location.y)
  29.         print ("    z = "..location.z)
  30.         print ("    f = "..location.f)
  31.     end
  32.      
  33.     --[[
  34.         A turning function
  35.       ]]--
  36.     function turn (direction)
  37.         if direction == "left" then
  38.             turtle.turnLeft()
  39.             location.f = location.f - 1
  40.         elseif direction == "right" then
  41.             turtle.turnRight()
  42.             location.f = location.f + 1
  43.         elseif direction == "around" then
  44.             turtle.turnLeft()
  45.             turtle.turnLeft()
  46.             location.f = location.f - 2
  47.         else
  48.             print ("No such direction: "..direction)
  49.             return false
  50.         end
  51.         location.f = (location.f + 4) % 4 -- This is used to handle the f < 0 and f > 3 situations
  52.         return true
  53.     end
  54.      
  55.     --[[
  56.         This one is used to turn the turtle to specified f direction.
  57.         A little bit of pony magic is used to calculate how should it turn
  58.       ]]--
  59.     function turnTo (face)
  60.         local diff = face - location.f
  61.         if math.abs(diff) == 2 then --this is true if the difference between f and face is 2 or -2, so it should turn around
  62.             return turn ("around")
  63.         elseif math.fmod(diff + 4, 4) ==  1 then    --this is true if the difference between f and face is 1
  64.             return turn ("right")                   --f = 0 and face = 3 is also satisfies the condition (-3 + 4 ==  1)
  65.         elseif math.fmod(diff - 4, 4) == -1 then    --this is true if the difference between f and face is -1
  66.             return turn ("left")                    --f = 3 and face = 1 is also satisfies the condition ( 3 - 4 == -1)
  67.         end
  68.         return false    --returned if turtle is already faced the specified direction
  69.     end
  70.      
  71.     --[[
  72.         This function is used to return actual x and z shifts
  73.         These shifts are the numbers which you should add to the coordinates when turtle moves forward.
  74.         I use a table to decode it since it is more compact and easier to read than lots of conditions
  75.       ]]--
  76.     local tShifts = {
  77.         [0] = { 1,  0},
  78.         [1] = { 0,  1},
  79.         [2] = {-1,  0},
  80.         [3] = { 0, -1},
  81.     }
  82.     function fDirection ()
  83.         return unpack (tShifts[location.f])
  84.     end
  85.      
  86.     --[[
  87.         This function is used to update the turtle location when it has moved
  88.       ]]--
  89.     function updateLocation (direction)
  90.         if direction == "up" then
  91.             location.y = location.y + 1
  92.         elseif direction == "down" then
  93.             location.y = location.y - 1        
  94.         elseif direction == "forward" then
  95.             local xShift, zShift = fDirection ()
  96.             location.x = location.x + xShift        
  97.             location.z = location.z + zShift
  98.         elseif direction == "back" then
  99.             local xShift, yShift = fDirection ()
  100.             location.x = location.x - xShift
  101.             location.z = location.z - zShift
  102.         end
  103.     end
  104.      
  105.     --This function tries to refuel the turtle to make it have an <amount> of fuel--
  106.     function refuel (amount)
  107.         if turtle.getFuelLevel () > amount then
  108.             return true
  109.         end
  110.         for i=1,16 do
  111.             if turtle.getItemCount (i) > 0 then
  112.                 turtle.select (i)
  113.                 while turtle.refuel (1) do
  114.                     if turtle.getFuelLevel () >= amount then
  115.                         turtle.select (1)
  116.                         return true
  117.                     end
  118.                 end
  119.             end
  120.         end
  121.         turtle.select (1)
  122.         return false
  123.     end
  124.      
  125.     --[[
  126.       This function checks if there are required amount of fuel,
  127.       and if there's not, it tries to refuel it,
  128.       if it fails, it hangs until refueling sucseeds.
  129.       ]]--
  130.     function checkFuel (amount)
  131.         if turtle.getFuelLevel () > amount then
  132.             return true
  133.         else
  134.             if not refuel (amount) then
  135.                 print "No fuel found! Please, add more fuel!"
  136.                 while not refuel (amount) do
  137.                     sleep (2)
  138.                 end
  139.             end
  140.             return true
  141.         end
  142.     end
  143.      
  144.     --[[
  145.         Advanced movement function. You can pass a function to it which is executed each time turtle collects something.
  146.         Again, I use a set of tables with all the movements and some other "directional" turtle actions
  147.         These tables are global, so programs using this API can access them or even modify them to set, for example, they own forward function
  148.         Warning! Moving functions should actually move the turtle and return true, or false if it can't move the turtle
  149.       ]]--
  150.     tMove = {
  151.         forward = turtle.forward,
  152.         up = turtle.up,
  153.         down = turtle.down
  154.     }
  155.     tDetect = {
  156.         forward = turtle.detect,
  157.         up = turtle.detectUp,
  158.         down = turtle.detectDown
  159.     }
  160.     tDig = {
  161.         forward = turtle.dig,
  162.         up = turtle.digUp,
  163.         down = turtle.digDown
  164.     }
  165.     tAttack = {
  166.         forward = turtle.attack,
  167.         up = turtle.attackUp,
  168.         down = turtle.attackDown
  169.     }
  170.     function move (direction, collectFunction)
  171.         collectFunction = collectFunction or function () end --if collect function is not set, replace it with an empty function
  172.         if not checkFuel (1) then
  173.             return false
  174.         end
  175.         while not tMove[direction]() do
  176.             if tDetect[direction]() then
  177.                 if tDig[direction]() then
  178.                     collectFunction ()
  179.                 else
  180.                     print "Can't pass the obstruction!"
  181.                     return false
  182.                 end
  183.             elseif tAttack[direction]() then
  184.                 print "Aggressive liveforms detected! Combat mode engaged!"
  185.                 collectFunction ()
  186.             end
  187.         end
  188.         updateLocation (direction)
  189.         return true
  190.     end
  191.      
  192.     --[[
  193.         This one is used to move turtle to specified f direction OR up/down
  194.       ]]--
  195.     function moveEx (direction)
  196.         if type(direction) == "number" then
  197.             turnTo (direction)
  198.             return move ("forward")
  199.         else
  200.             return move (direction)
  201.         end
  202.     end
  203.      
  204.     --[[
  205.         Here is even more pony magic. You should pass to this function a table, which contains a bunch of small tables
  206.         Those tables should contain two fields: <direction/function> and <count>
  207.         For each of that small table turtle performs that function or moves to that direction <count> times
  208.         It uses moveEx to move so you should specify the f direction if you want to move it
  209.       ]]--
  210.     function sequence (seq)
  211.         for aNum, aTable in ipairs (seq) do
  212.             action = aTable[1]
  213.             count  = aTable[2]
  214.             if type(action) == "function" then
  215.                 for i = 1,count do
  216.                     action ()
  217.                 end
  218.             else
  219.                 for i = 1,count do
  220.                     moveEx (action)
  221.                 end
  222.             end
  223.         end
  224.     end
  225.      
  226.     --[[
  227.         Small goto/turning function. It moves turtle in one axis.
  228.         It uses the sequence function with only one element in sequence
  229.         Conditions... Don't know how to make it better.
  230.         Example: move.gotoOne ("x", 10) moves the turtle to x = 10
  231.       ]]--
  232.     function gotoOne (coordinate, value)
  233.         if coordinate == "y" then
  234.             local diff = value - location.y
  235.             if diff > 0 then
  236.                 sequence { {"up", diff} }
  237.             elseif diff < 0 then
  238.                 sequence { {"down", -diff} }
  239.             end
  240.         elseif coordinate == "x" then
  241.             local diff = value - location.x
  242.             if diff > 0 then
  243.                 sequence { {0, diff} }
  244.             elseif diff < 0 then
  245.                 sequence { {2, -diff} }
  246.             end
  247.         elseif coordinate == "z" then
  248.             local diff = value - location.z
  249.             if diff > 0 then
  250.                 sequence { {1, diff} }
  251.             elseif diff < 0 then
  252.                 sequence { {3, -diff} }
  253.             end
  254.         elseif coordinate == "f" then
  255.             turnTo (value)
  256.         end
  257.     end
  258.      
  259.     --[[
  260.         And finally the goto function! This is the REAL pony magic!
  261.         It is kinda strange - it uses tables as arguments. For example,
  262.         If you want to go to x, y, z = 10, 10, 10 call it this way:
  263.         move.goto ({x = 10, y = 10, z = 10})
  264.         But if you want turtle to go, for example, to y = 10 first,
  265.         you can call it this way:
  266.         move.goto ( {y = 10}, {x = 10, z = 10} )
  267.         You can even pass a function as one of its arguments - and the turtle will execute it!
  268.       ]]--
  269.      
  270.     function goto (...)
  271.         local moveSequence = {}
  272.         for number, argument in ipairs (arg) do            --for each of the arguments passed to this function
  273.             if type (argument) == "function" then          --if it is a function
  274.                 table.insert (moveSequence, {argument, 1}) --add it to the sequence
  275.             elseif type (argument) == "table" then         --and if it is a table,
  276.                 local finalF = location.f                  --make a variable for the final f direction
  277.                 for key, value in pairs (argument) do      --for each pair "key-value" in that table
  278.                     if key ~= "f" then                     --if it is not an f movement
  279.                         table.insert (moveSequence,        --add to the sequence a table with a movement function
  280.                         { function()                       --and count = 1
  281.                             gotoOne (key, value)
  282.                         end, 1 })
  283.                     else    
  284.                         finalF = value                     --if there is an f movement command
  285.                     end                                    --store that f value in the variable
  286.                 end
  287.                 table.insert (moveSequence,                --insert a turning command into the sequence
  288.                 { function()                    
  289.                     turnTo (finalF)            
  290.                 end, 1 })
  291.             end
  292.         end
  293.         sequence (moveSequence)                 --execute that sequence
  294.     end
Advertisement
Add Comment
Please, Sign In to add comment