Advertisement
jille_Jr

CC: Treefarm - gps.lua

May 29th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.86 KB | None | 0 0
  1.  
  2. -- DIRECTIONS
  3. north = "n"
  4. south = "s"
  5. east = "e"
  6. west = "w"
  7. up = "u"
  8. down = "d"
  9.  
  10. -- TURTLE POSITION
  11. absPos = {} --{x=0,y=0,z=0,dir=north}
  12. homePos = {}
  13.  
  14. -- gets all global variables
  15. function init( global )
  16.     setfenv(1,global)
  17.     -- gps functions included in CC
  18.     gpsModule = _G["gps".._G.oldAPISyntax]
  19. end
  20.  
  21. function isDir( dir )
  22.     if dir == north
  23.     or dir == south
  24.     or dir == east
  25.     or dir == west
  26.     or dir == up
  27.     or dir == down
  28.     then
  29.         return true
  30.     else
  31.         return false
  32.     end
  33. end
  34.  
  35. function getDirectionOffset( dir )
  36.     local x,y,z = 0,0,0
  37.  
  38.     if dir == up then y = y + 1 end
  39.     if dir == down then y = y - 1 end
  40.  
  41.     if dir == south then z = z + 1 end
  42.     if dir == north then z = z - 1 end
  43.  
  44.     if dir == east then x = x + 1 end
  45.     if dir == west then x = x - 1 end
  46.  
  47.     return x,y,z
  48. end
  49.  
  50. function getPosition( offset,dir )
  51.     local ox,oy,oz = 0,0,0
  52.     local x,y,z = absPos.x,absPos.y,absPos.z
  53.  
  54.     if type(offset) ~= "number" then
  55.         offset = 0
  56.     end
  57.  
  58.     if type(dir) == "string" then
  59.         ox,oy,oz = getDirectionOffset(dir)
  60.     end
  61.  
  62.     x = x + ox*offset
  63.     y = y + oy*offset
  64.     z = z + oz*offset
  65.  
  66.     return x,y,z,absPos.dir
  67. end
  68.  
  69. function setPosition( x,y,z,dir )
  70.     absPos.x = x or absPos.x
  71.     absPos.y = y or absPos.y
  72.     absPos.z = z or absPos.z
  73.     absPos.dir = dir or absPos.dir
  74. end
  75.  
  76. function addPosition( x,y,z )
  77.     absPos.x = absPos.x + (x or 0)
  78.     absPos.y = absPos.y + (y or 0)
  79.     absPos.z = absPos.z + (z or 0)
  80. end
  81.  
  82. function getHome( offset,dir )
  83.     local ox,oy,oz = 0,0,0
  84.     local x,y,z = homePos.x,homePos.y,homePos.z
  85.  
  86.     if type(offset) ~= "number" then
  87.         offset = 0
  88.     end
  89.  
  90.     if type(dir) == "string" then
  91.         ox,oy,oz = getDirectionOffset(dir)
  92.     end
  93.  
  94.     x = x + ox*offset
  95.     y = y + oy*offset
  96.     z = z + oz*offset
  97.  
  98.     return x,y,z,homePos.dir
  99. end
  100.  
  101. function setHome( x,y,z,dir )
  102.     homePos.x = x or homePos.x
  103.     homePos.y = y or homePos.y
  104.     homePos.z = z or homePos.z
  105.     homePos.dir = dir or homePos.dir
  106. end
  107.  
  108. function dirToValue( dir )
  109.     if dir == south then return 0 end
  110.     if dir == west then return 1 end
  111.     if dir == north then return 2 end
  112.     if dir == east then return 3 end
  113.     if dir == up then return math.huge end
  114.     if dir == down then return -math.huge end
  115. end
  116.  
  117. function valueToDir( value )
  118.     if value == 0 then return south end
  119.     if value == 1 then return west end
  120.     if value == 2 then return north end
  121.     if value == 3 then return east end
  122.     if value == math.huge then return up end
  123.     if value == -math.huge then return down end
  124. end
  125.  
  126. function turnRight( times )
  127.     if type(times)~="number" then times = 1 end
  128.     if times<1 then times = 1 end
  129.     if times%1~=0 then times = math.floor(times) end
  130.  
  131.     for count = 1,times do
  132.         -- turn the dir var
  133.         absPos.dir = valueToDir(((dirToValue(absPos.dir) or 0)+1)%4)
  134.  
  135.         turtle.turnRight()
  136.     end
  137. end
  138.  
  139. function turnLeft( times )
  140.     if type(times)~="number" then times = 1 end
  141.     if times<1 then times = 1 end
  142.     if times%1~=0 then times = math.floor(times) end
  143.  
  144.     for count = 1,times do
  145.         -- turn the dir var
  146.         absPos.dir = valueToDir(((dirToValue(absPos.dir) or 0)+3)%4)
  147.  
  148.         turtle.turnLeft()
  149.     end
  150. end
  151.  
  152. -- locate where the turtle is in the real world by using the built in gps system
  153. -- requires the turtle to have a wireless modem on the right side
  154. function autoSetPosition()
  155.     print("gps start")
  156.     if peripheral.getType("right")~="modem" then return false end
  157.     rednet.open("right")
  158.  
  159.     print("gps step 1")
  160.  
  161.     local x1,y1,z1 = gpsModule.locate()print("gps step 2")
  162.     if type(x1)~="number" or type(y1)~="number" or type(z1)~="number" then return false end
  163.  
  164.     local dir = "unknown"
  165.  
  166.     local try = 0
  167.     while not turtle.forward() do
  168.         try = try + 1
  169.         turtle.turnRight()
  170.         if try == 4 then return false end
  171.     end
  172.     print("gps step 3")
  173.  
  174.     local x2,y2,z2 = gpsModule.locate()print("gps step 4")
  175.     if type(x2)~="number" or type(y2)~="number" or type(z2)~="number" then return false end
  176.  
  177.     if x2 == x1 + 1 then dir = east end
  178.     if x2 == x1 - 1 then dir = west end
  179.     if z2 == z1 + 1 then dir = south end
  180.     if z2 == z1 - 1 then dir = north end
  181.  
  182.     if dir == "unknown" then
  183.         return false
  184.     end
  185.     print("gps step 5")
  186.     setPosition(x2,y2,z2,dir)
  187.     print("gps step final")
  188.     return true
  189. end
  190.  
  191. function getFacing()
  192.     return absPos.dir
  193. end
  194.  
  195. function face( dir )
  196.     -- stop if dir is not a valid direction
  197.     if not isDir(dir) then return end
  198.     if dir == up then return end
  199.     if dir == down then return end
  200.  
  201.     --
  202.     if type(absPos.dir) ~= "string" then
  203.         locate()
  204.     end
  205.  
  206.     local wantedValue = dirToValue(dir)
  207.     local currentValue = dirToValue(absPos.dir)
  208.  
  209.     -- continue looping until you face the desired direction
  210.     while currentValue ~= wantedValue do
  211.         if (currentValue+1)%4 == wantedValue then
  212.             -- disered direction is to the right
  213.             turnRight()
  214.         elseif (currentValue+3)%4 == wantedValue then
  215.             -- disered direction is to the left
  216.             turnLeft()
  217.         else
  218.             -- desired direction is behind us, so turn either right or left
  219.             if math.random(0,1) == 1 then
  220.                 turnRight()
  221.             else
  222.                 turnLeft()
  223.             end
  224.         end
  225.  
  226.         -- update currentValue variable
  227.         currentValue = dirToValue(absPos.dir)
  228.     end
  229. end
  230.  
  231. function moveUp( times,killMobs,destroyBlocks )
  232.     if type(times)~="number" then times = 1 end
  233.     --if times<1 then times = 1 end
  234.     if times%1~=0 then times = math.floor(times) end
  235.  
  236.     if type(absPos.dir) ~= "string" then
  237.         autoSetPosition()
  238.     end
  239.  
  240.     for count = 1,times do
  241.         while not turtle.up() do
  242.             if not destroyBlocks then return false end
  243.             dig.up(killMobs,destroyBlocks)
  244.         end
  245.         addPosition(_,1)
  246.     end
  247.     return true
  248. end
  249.  
  250. function moveDown( times,killMobs,destroyBlocks )
  251.     if type(times)~="number" then times = 1 end
  252.     --if times<1 then times = 1 end
  253.     if times%1~=0 then times = math.floor(times) end
  254.  
  255.     if type(absPos.dir) ~= "string" then
  256.         autoSetPosition()
  257.     end
  258.  
  259.     for count = 1,times do
  260.         while not turtle.down() do
  261.             if not destroyBlocks then return false end
  262.             dig.down(killMobs,destroyBlocks)
  263.         end
  264.         addPosition(_,-1)
  265.     end
  266.     return true
  267. end
  268.  
  269. function moveForward( times,killMobs,destroyBlocks )
  270.     if type(times)~="number" then times = 1 end
  271.     --if times<1 then times = 1 end
  272.     if times%1~=0 then times = math.floor(times) end
  273.  
  274.     if type(absPos.dir) ~= "string" then
  275.         autoSetPosition()
  276.     end
  277.  
  278.     for count = 1,times do
  279.         while not turtle.forward() do
  280.             if destroyBlocks then
  281.                 dig.forward(killMobs,destroyBlocks)
  282.             else
  283.                 return false
  284.             end
  285.         end
  286.         addPosition(getDirectionOffset(absPos.dir))
  287.     end
  288.     return true
  289. end
  290.  
  291. function goto( x,y,z,dir,killMobs,destroyBlocks )
  292.     --print("goto function: x="..tostring(x).." y="..tostring(y).." z="..tostring(z).." killMobs="..tostring(killMobs).." destroyBlocks="..tostring(destroyBlocks))
  293.     if type(x)~="number" then x=absPos.x end
  294.     if type(y)~="number" then y=absPos.y end
  295.     if type(z)~="number" then z=absPos.z end
  296.     x=math.floor(x) y=math.floor(y) z=math.floor(z)
  297.     if type(killMobs)~="boolean" then killMobs=false end
  298.     if type(destroyBlocks)~="boolean" then destroyBlocks=false end
  299.  
  300.     if type(absPos.dir) ~= "string" then
  301.         autoSetPosition()
  302.     end
  303.  
  304.     while absPos.x~=x or absPos.y~=y or absPos.z~=z do
  305.         -- current location
  306.         local cx,cy,cz = getPosition()
  307.  
  308.         -- y
  309.         if y~=cy then
  310.             for count = math.min(cy,y),math.max(cy,y)-1 do
  311.                 if y>cy then
  312.                     if not moveUp(_,killMobs,destroyBlocks) then break end
  313.                 elseif y<cy then
  314.                     if not moveDown(_,killMobs,destroyBlocks) then break end
  315.                 end
  316.             end
  317.         end
  318.  
  319.         -- x
  320.         if x~=cx then
  321.             for count = math.min(cx,x),math.max(cx,x)-1 do
  322.                 if x>cx then -- x increasing
  323.                     face(east)
  324.                 elseif x<cx then -- x decreasing
  325.                     face(west)
  326.                 end
  327.                 if not moveForward(_,killMobs,destroyBlocks) then break end
  328.             end
  329.         end
  330.  
  331.         -- z
  332.         if z~=cz then
  333.             for count = math.min(cz,z),math.max(cz,z)-1 do
  334.                 if z>cz then -- z increasing
  335.                     face(south)
  336.                 elseif z<cz then -- z decreasing
  337.                     face(north)
  338.                 end
  339.                 if not moveForward(_,killMobs,destroyBlocks) then break end
  340.             end
  341.         end
  342.     end
  343.  
  344.     if isDir(dir) then
  345.         face(dir)
  346.     end
  347. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement