guitarplayer616

[GoTo] schem turtle

Nov 25th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.62 KB | None | 0 0
  1. --[[Navigation function]]--
  2.  
  3. function goto(heightGoal,widthGoal,lengthGoal)
  4.     shell.run("position")
  5.     if turtle.getFuelLevel() < 200 then
  6.         refill()
  7.     end
  8.     if heightGoal > heightPos then
  9.         while heightGoal > heightPos do
  10.             up()
  11.         end
  12.     elseif heightGoal < heightPos then
  13.         while heightGoal < heightPos do
  14.             down()
  15.         end
  16.     end
  17.     if widthGoal > widthPos then
  18.         turn("east")
  19.         while widthGoal > widthPos do
  20.             forward()
  21.         end
  22.     elseif widthGoal < widthPos then
  23.         turn("west")
  24.         while widthGoal < widthPos do
  25.             forward()
  26.         end
  27.     end
  28.     if lengthGoal > lengthPos then
  29.         turn("south")
  30.         while lengthGoal > lengthPos do
  31.             forward()
  32.         end
  33.     elseif lengthGoal < lengthPos then
  34.         turn("north")
  35.         while lengthGoal < lengthPos do
  36.             forward()
  37.         end
  38.     end
  39. end
  40.  
  41. function update(dir)
  42.         if dir == "forward" then
  43.             if face == "north" then
  44.                 lengthPos = lengthPos - 1
  45.             elseif face == "south" then
  46.                 lengthPos = lengthPos + 1
  47.             elseif face == "west" then
  48.                 widthPos = widthPos - 1
  49.             elseif face == "east" then
  50.                 widthPos = widthPos + 1
  51.             end
  52.         elseif dir == "backward" then
  53.                 if face == "north" then
  54.                         lengthPos = lengthPos + 1
  55.                 elseif face == "south" then
  56.                         lengthPos = lengthPos - 1
  57.                 elseif face == "west" then
  58.                         widthPos = widthPos + 1
  59.                 elseif face == "east" then
  60.                         widthPos = widthPos - 1
  61.                 end
  62.         elseif dir == "up" then
  63.                 heightPos = heightPos + 1
  64.         elseif dir == "down" then
  65.                 heightPos = heightPos - 1
  66.         elseif dir == "right" then
  67.                 if face == "north" then
  68.                         face = "east"
  69.                 elseif face == "east" then
  70.                         face = "south"
  71.                 elseif face == "south" then
  72.                         face = "west"
  73.                 elseif face == "west" then
  74.                         face = "north"
  75.                 end
  76.         elseif dir == "left" then
  77.                 if face == "north" then
  78.                         face = "west"
  79.                 elseif face == "west" then
  80.                         face = "south"
  81.                 elseif face == "south" then
  82.                         face = "east"
  83.                 elseif face == "east" then
  84.                         face = "north"
  85.                 end
  86.         end
  87.         recordPos(heightPos,widthPos,lengthPos,face)
  88. end
  89.  
  90. --[[Movement functions]]--
  91.  
  92. function forward()
  93.   update("forward")
  94.   while not turtle.forward() do
  95.     turtle.dig()
  96.   end
  97. end
  98.  
  99. function up()
  100.   update("up")
  101.   while not turtle.up() do
  102.     turtle.digUp()
  103.   end
  104. end
  105.  
  106. function down()
  107.   update("down")
  108.   while not turtle.down() do
  109.     turtle.digDown()
  110.   end
  111. end
  112.  
  113. function right()
  114.   update("right")
  115.   turtle.turnRight()
  116. end
  117.    
  118. function left()
  119.   update("left")
  120.   turtle.turnLeft()
  121. end
  122.  
  123. function turn(dir)
  124.     if face == "north" then
  125.         if dir == "east" then
  126.             while face ~= dir do
  127.                 right()
  128.             end
  129.         else
  130.             while face ~= dir do
  131.                 left()
  132.             end
  133.         end
  134.    elseif face == "east" then
  135.         if dir == "south" then
  136.             while face ~= dir do
  137.                 right()
  138.             end
  139.         else
  140.             while face ~= dir do
  141.                 left()
  142.             end
  143.         end
  144.     elseif face == "south" then
  145.         if dir == "west" then
  146.             while face ~= dir do
  147.                 right()
  148.             end
  149.         else
  150.             while face ~= dir do
  151.                 left()
  152.             end
  153.         end
  154.     elseif face == "west" then
  155.         if dir == "north" then
  156.             while face ~= dir do
  157.                 right()
  158.             end
  159.         else
  160.             while face ~= dir do
  161.                 left()
  162.             end
  163.         end
  164.     end
  165. end
  166.  
  167. function place()
  168.   while not turtle.placeDown() do
  169.     turtle.digDown()
  170.   end
  171.   return true
  172. end
  173.  
  174. function smartPlace(wrench)
  175. --wrench turning included
  176. --check for wrench 14
  177. --check if slabs or stairs
  178.     local block = turtle.getItemDetail()
  179.     if block then
  180.         --lookup stairs or slabs
  181.         if (block.name:lower():find("stairs") or block.name:lower():find("slab")) and wrench then
  182.             --place until metadata matches
  183.             place()
  184.             while true do
  185.                 local stair,orient = turtle.inspectDown()
  186.                 if stair then
  187.                     if orient.metadata == blockData then
  188.                         break
  189.                     end
  190.                 end
  191.                 local mem = turtle.getSelectedSlot()
  192.                 turtle.select(14)
  193.                 turtle.placeDown()
  194.                 turtle.select(mem)
  195.             end
  196.         else
  197.             place()
  198.         end
  199.     else
  200.         return false
  201.     end
  202. end
  203.  
  204.  
  205. --[[record keeping functions]]--
  206.  
  207. function recordPos(heightPos,widthPos,lengthPos,face)
  208.     local h = fs.open("position","w")
  209.     h.writeLine("heightPos = "..tostring(heightPos))
  210.     h.writeLine("widthPos = "..tostring(widthPos))
  211.     h.writeLine("lengthPos = "..tostring(lengthPos))
  212.     h.writeLine("face = ".."\""..tostring(face).."\"")
  213.     h.close()
  214. end
  215.  
  216. function recordObj(x,y,z)
  217.     local h = fs.open("objective",'w')
  218.     h.writeLine("x = "..tostring(x))
  219.     h.writeLine("y = "..tostring(y))
  220.     h.writeLine("z = "..tostring(z))
  221.     h.close()
  222. end
  223.  
  224. function recordObjSlot(num)
  225.     local h = fs.open("objectiveSlot",'w')
  226.     h.writeLine("slot = "..tostring(num))
  227.     h.close()
  228. end
Add Comment
Please, Sign In to add comment