Advertisement
Guest User

jmq

a guest
Feb 21st, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.00 KB | None | 0 0
  1. -- jmq quarry. Bit of a demonstrator of movement tracking without GPS.
  2. -- To use: Put a chest behind the turtle. If using flat bedrock, just
  3. -- run jmq [size], where size is how big you want the hole to be.
  4. -- If not using flat bedrock, run jmq x z y, where y is how deep you
  5. -- want the hole. Try feet pos - 6
  6.  
  7. local t = turtle
  8. local NORTH,   EAST,  SOUTH, WEST= 0,1,2,3 -- if turtle placed facing NORTH
  9. local FORWARD, RIGHT, BACK,  LEFT= 0,1,2,3 -- These are the actual ones used.
  10. local xoffs = { 0, 1, 0, -1}; -- Change in X moving forward for each facing.
  11. local zoffs = {-1, 0, 1,  0}; -- Change in Z moving forward for each facing.
  12.  
  13. -- current facing and position - these are relative coords.
  14. local facing=FORWARD
  15. local pos={0,0,0}
  16. local      X,Y,Z = 1,2,3 --array index
  17.  
  18.  
  19. --QUARRY SPECIFIC
  20. chestPos  = {pos[X],pos[Y],pos[Z]}
  21. chestSide = BACK --Immediately behind turtle
  22. trashSide = LEFT
  23.  
  24.  
  25.  
  26.  
  27. function left()
  28.   turtle.turnLeft()
  29.   facing = (facing + 3) % 4
  30. end
  31. function turnRight()
  32.   turtle.turnRight()
  33.   facing = (facing + 1) % 4
  34. end
  35. function getOpposite(dir)
  36.   return (dir + 2) % 4
  37. end
  38.  
  39. function forward()
  40.   if turtle.forward() then
  41.     pos[1] = pos[1] + xoffs[facing+1]
  42.     pos[3] = pos[3] + zoffs[facing+1]
  43.     return true
  44.   end
  45.   return false
  46. end
  47. function back()
  48.   if turtle.back() then
  49.     pos[1] = pos[1] - xoffs[facing+1]
  50.     pos[3] = pos[3] - zoffs[facing+1]
  51.     return true
  52.   end
  53.   return false
  54. end
  55. function up()
  56.   if turtle.up() then
  57.     pos[2] = pos[2] + 1
  58.     return true
  59.   end
  60.   return false
  61. end
  62.  
  63. function down()  
  64.   if turtle.down() then
  65.     pos[2] = pos[2] - 1
  66.     return true
  67.   end
  68.   return false
  69. end
  70.  
  71. function face(dir)
  72.   dir = (facing - dir) % 4
  73.   if dir == 0 then return
  74.   elseif dir == 1 then left()
  75.   elseif dir == 2 then left() left()
  76.   elseif dir == 3 then turnRight()
  77.   else print ("HUHHHHH??????"..dir)
  78.   end
  79. end
  80.  
  81. function go(dir)
  82.   face(dir)
  83.   return forward()
  84. end
  85.  
  86. --This likely to be used with stored coords.
  87. function travel(coords, y, z)
  88.   if y ~= nil then coords = {coords, y, z} end
  89.   while pos[1] < coords[1] do while not go(RIGHT) do sleep(1) end end
  90.   while pos[1] > coords[1] do while not go(LEFT) do sleep(1) end end
  91.   while pos[2] < coords[2] do while not up() do sleep(1) end end
  92.   while pos[2] > coords[2] do while not down() do sleep(1) end end
  93.   while pos[3] < coords[3] do while not go(BACK) do sleep(1) end end
  94.   while pos[3] > coords[3] do while not go(FORWARD) do sleep(1) end end
  95. end
  96.  
  97. function ppos()
  98.   print(pos[1]..", "..pos[2]..", "..pos[3]..", facing:"..facing)
  99. end
  100.  
  101.  
  102. function pfwd(tries)
  103.   if tries == nil then tries = 1000 end
  104.   for i=1,tries do
  105.     if forward() then
  106.       return true
  107.     else
  108.       if not t.dig() then
  109.         sleep(0.3)
  110.         print("Try "..i)
  111.       end
  112.     end
  113.   end
  114.   return false
  115. end
  116. function pdown(tries)
  117.   if tries == nil then tries = 1000 end
  118.   for i=1,tries do
  119.     if down() then
  120.       return true
  121.     else
  122.       if not t.digDown() then
  123.         sleep(0.3)
  124.         print("Try "..i)
  125.       end
  126.     end
  127.   end
  128.   return false
  129. end
  130. function pup(tries)
  131.   if tries == nil then tries = 1000 end
  132.   for i=1,tries do
  133.     if up() then
  134.       return true
  135.     else
  136.       if not t.digUp() then
  137.         sleep(0.3)
  138.       end
  139.     end
  140.   end
  141.   return false
  142. end
  143.  
  144. function pgo(dir, tries)
  145.   face(dir)
  146.   return pfwd(tries)
  147. end
  148.  
  149.  
  150. -- Best for starting new layer -
  151. -- if turtle gets stuck will be easier to find
  152. function ptravel(coords, y, z)
  153.   if y ~= nil then coords = {coords, y, z} end
  154.   while pos[1] < coords[1] do pgo(RIGHT) end
  155.   while pos[1] > coords[1] do pgo(LEFT) end
  156.   while pos[3] < coords[3] do pgo(BACK) end
  157.   while pos[3] > coords[3] do pgo(FORWARD) end
  158.   while pos[2] < coords[2] do pup() end
  159.   while pos[2] > coords[2] do pdown() end
  160. end
  161. -- as above, but go forward/back first: Best for returning from dumping stuff
  162. -- as otherwise turtle will dig through anything sitting to the right of the start point
  163. function ptravelF(coords, y, z)
  164.   if y ~= nil then coords = {coords, y, z} end
  165.   while pos[3] < coords[3] do pgo(BACK) end
  166.   while pos[3] > coords[3] do pgo(FORWARD) end
  167.   while pos[1] < coords[1] do pgo(RIGHT) end
  168.   while pos[1] > coords[1] do pgo(LEFT) end
  169.   while pos[2] < coords[2] do pup() end
  170.   while pos[2] > coords[2] do pdown() end
  171. end
  172. -- as above, but go up/down first: Best for dumping stuff
  173. -- as otherwise would likely dig through the wall/stairs.
  174. function ptravelV(coords, y, z)
  175.   if y ~= nil then coords = {coords, y, z} end
  176.   while pos[2] < coords[2] do pup() end
  177.   while pos[2] > coords[2] do pdown() end
  178.   while pos[1] < coords[1] do pgo(RIGHT) end
  179.   while pos[1] > coords[1] do pgo(LEFT) end
  180.   while pos[3] < coords[3] do pgo(BACK) end
  181.   while pos[3] > coords[3] do pgo(FORWARD) end
  182. end
  183.  
  184. function digTall()
  185.   t.digUp()
  186.   t.digDown()
  187. end
  188.  
  189.  
  190. function stairs(fwd, right,depth)
  191.   if depth == nil then depth = 500 end
  192.   digTall()
  193.   while true do
  194.     for i = 1,fwd-1 do
  195.       print("Step "..i.." of "..fwd)
  196.       if not pfwd(10) then return end
  197.       digTall()
  198.       if t.getItemCount(16) > 0 then
  199.         dumpStuff()
  200.       end
  201.       print(depth)
  202.       if not pdown(10) or depth < 1 then return end
  203.       t.digDown()
  204.       depth = depth - 1
  205.     end
  206.     turnRight()
  207.     for i = 1,right-1 do
  208.       print("Step "..i.." of "..right)
  209.       if not pfwd(10) then return end
  210.       digTall()
  211.       if t.getItemCount(16) > 0 then
  212.         dumpStuff()
  213.       end
  214.       if not pdown(10) or depth < 1 then return end
  215.       t.digDown()
  216.       depth = depth - 1
  217.     end
  218.     turnRight()
  219.   end
  220. end
  221.  
  222. function excavateColumn(length)
  223.   digTall()
  224.   for i = 1,length-1 do
  225.     if not pfwd() then return false end
  226.     digTall()
  227.     if t.getItemCount(16) > 0 then
  228.       dumpStuff()
  229.     end
  230.   end
  231.   return true
  232. end
  233.  
  234. function excavateLayer(rows, cols)
  235.   f=facing
  236.   right=(facing + 1) % 4 --relative to facing
  237.   if not excavateColumn(cols) then return false end
  238.   for i = 2,rows do
  239.     pgo(right)
  240.     f = getOpposite(f)
  241.     face(f)
  242.     if not excavateColumn(cols) then return false end
  243.   end
  244. end
  245.  
  246.  
  247. function excavateUp(rows, cols, targetY)
  248.   ppos()
  249.   print("Excavate up to "..targetY)
  250.   home = {pos[X], pos[Y], pos[Z]}
  251.   while pos[Y] < targetY do
  252.     ptravel(home)
  253.     face(FORWARD)
  254.     excavateLayer(rows, cols)
  255.     home[Y] = home[Y] + 3
  256.   end
  257. end
  258.  
  259. function excavateDown(rows, cols, targetY)
  260.   local home = {pos[X], pos[Y], pos[Z]}
  261.   excavateLayer(rows, cols)
  262.   while pos[Y] > targetY do
  263.     home[Y] = home[Y] - 3
  264.     ptravel(home)
  265.     face(FORWARD)
  266.     excavateLayer(rows, cols)
  267.   end
  268. end
  269.  
  270. function quarry(rows, cols, depth)
  271.   t.select(1)
  272.   local home = {pos[X], pos[Y], pos[Z]}
  273.   homeFacing = facing
  274.   dumpStuff()
  275.   stairs(rows,cols,depth)
  276.   turnRight()
  277.   pfwd() -- move to inner part
  278.   ptravel(home[X]+1, pos[Y], home[Z]-1)
  279.   face(FORWARD)
  280.   excavateUp(cols-2, rows-2,  home[Y])
  281.   ptravel(home)
  282.   dumpStuff()
  283.   face(homeFacing)
  284.  
  285. end
  286.  
  287. function inventoryContainsItems()
  288.   for i=1,16 do
  289.     if t.getItemCount(i) > 0 then
  290.       return true
  291.     end
  292.   end
  293.   return false
  294. end
  295.  
  296. function dumpStuff()
  297.   local home = {pos[X], pos[Y], pos[Z]}
  298.   local homeFacing = facing
  299.   ptravelV(chestPos) --up then across
  300.   face(trashSide)
  301.   local ok, meta = t.inspect();
  302.   if ok and string.match(meta.name, 'rash') then -- Probably a trash can?
  303.     print("Dump cobble")
  304.     for i=1,16 do
  305.       local block = t.getItemDetail(i)
  306.       if (block and block.name == "minecraft:cobblestone") then
  307.         t.select(i)
  308.         if not t.drop() then
  309.           print("Can't put item in trashcan.")
  310.           while not t.drop() do
  311.             sleep(1)
  312.           end
  313.         end
  314.       end
  315.     end
  316.   end
  317.  
  318.   if inventoryContainsItems() then
  319.     face(chestSide)
  320.     if not t.inspect() then
  321.       print("Please place a chest here")
  322.       print("or clean out my inventory")
  323.       while inventoryContainsItems() and not t.inspect() do
  324.         sleep(1)
  325.       end
  326.     end
  327.     for i=1,16 do
  328.       if t.getItemCount(i) > 0 then
  329.         t.select(i)
  330.         if not t.drop() then
  331.           print("Chest is full, please clear some space")
  332.           while not t.drop() do
  333.             sleep(1)
  334.           end
  335.         end
  336.       end
  337.     end
  338.   end
  339.   t.select(1)
  340.   ptravelF(home) -- across then down
  341.   face(homeFacing)
  342. end
  343.  
  344. arg = {...}
  345.  
  346. if arg[1] == nil then
  347.   print("I dig a quarry forward and right.")
  348.   print("use: jmq [fwd] [right] [deep]")
  349.   print("or:  jmq [size]")
  350.   print("or:  jmq help")
  351.   print("")
  352.   return
  353. end
  354.  
  355. if arg[1] == 'help' then
  356.   print("Put chest behind me")
  357.   print("Trash can to left (optional)")
  358.   print("")
  359.   print("Use jmq[size] only with flat bedrock")
  360.   print("Otherwise use jmq [fwd] [right] [deep]")
  361.   print("[deep] = feet pos - bedrock height")
  362.   print("")
  363.   print("Example: jmq 8 8 8")
  364.   return
  365. end
  366.  
  367.  
  368. local r, c, d = arg[1], arg[1], 200
  369. if arg[2] ~= nil then
  370.   c = tonumber(arg[2])
  371. end
  372. if arg[3] ~= nil then
  373.   d = tonumber(arg[3])
  374. end
  375. print (r)
  376. print (c)
  377. print (d)
  378. quarry(r, c, d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement