Advertisement
chaos511

dig.lua

Feb 18th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.55 KB | None | 0 0
  1. -- This is an API for digging and
  2. -- keeping track of motion
  3. --Orignal code from user flexico on the computercraft forums http://www.computercraft.info/forums2/index.php?/topic/29859-excavate-except-it-recovers-after-reboot/
  4. -- Flexico64@gmail.com
  5.  
  6. -- x is right, y is up, z is forward
  7. -- r is clockwise rotation in degrees
  8.  
  9. os.loadAPI("flex")
  10.  
  11. dist_x = 0
  12. xmin = 0
  13. xmax = 0
  14.  
  15. dist_y = 0
  16. ymin = 0
  17. ymax = 0
  18.  
  19. dist_z = 0
  20. zmin = 0
  21. zmax = 0
  22.  
  23. dist_r = 0
  24. lastMove = "?"
  25.  
  26. function getx() return dist_x end
  27. function gety() return dist_y end
  28. function getz() return dist_z end
  29. function getr() return dist_r end
  30.  
  31. function setx(x) dist_x = x end
  32. function sety(y) dist_y = y end
  33. function setz(z) dist_z = z end
  34. function setr(r) dist_r = r end
  35.  
  36. function getLastMove()
  37.  return lastMove
  38. end
  39. function setLastMove(lm)
  40.  lastMove = lm
  41. end
  42.  
  43. function getXmin() return xmin end
  44. function getXmax() return xmax end
  45. function getYmin() return ymin end
  46. function getYmax() return ymax end
  47. function getZmin() return zmin end
  48. function getZmax() return zmax end
  49.  
  50. function setXmin(x) xmin = x end
  51. function setXmax(x) xmax = x end
  52. function setYmin(y) ymin = y end
  53. function setYmax(y) ymax = y end
  54. function setZmin(z) zmin = z end
  55. function setZmax(z) zmax = z end
  56.  
  57.  
  58. fuelSlot = {1,16}
  59.  
  60. function getFuelSlot()
  61.  return fuelSlot[1],fuelSlot[2]
  62. end
  63.  
  64. function setFuelSlot(a,b)
  65.  b = b or a
  66.  fuelSlot[1] = a
  67.  fuelSlot[2] = b
  68. end
  69.  
  70.  
  71. function location()
  72.  return
  73.   { dist_x, dist_y, dist_z, dist_r,
  74.     lastMove, xmin, xmax, ymin,
  75.     ymax, zmin, zmax }
  76. end
  77.  
  78. function saveCoords()
  79.  local file,loc,x
  80.  file = fs.open("dig_save.txt","w")
  81.  loc = location()
  82.  for x=1,#loc do
  83.   file.writeLine(tostring(loc[x]))
  84.  end --for
  85.  file.close()
  86. end --function
  87.  
  88. function loadCoords()
  89.  local file
  90.  file = fs.open("dig_save.txt","r")
  91.  dist_x = tonumber(file.readLine())
  92.  dist_y = tonumber(file.readLine())
  93.  dist_z = tonumber(file.readLine())
  94.  dist_r = tonumber(file.readLine())
  95.  lastMove = file.readLine()
  96.  xmin = tonumber(file.readLine())
  97.  xmax = tonumber(file.readLine())
  98.  ymin = tonumber(file.readLine())
  99.  ymax = tonumber(file.readLine())
  100.  zmin = tonumber(file.readLine())
  101.  zmax = tonumber(file.readLine())
  102.  file.close()
  103. end
  104.  
  105.  
  106. function makeStartup(command, args)
  107.  command = tostring(command)
  108.  local x
  109.  for x=1,#args do
  110.   command = command.." "..args[x]
  111.  end --for
  112.  local file = fs.open("startup","w")
  113.  file.writeLine("os.loadAPI(\"flex\")")
  114.  file.writeLine("flex.send(\"> "..command.."\")")
  115.  file.writeLine("shell.run(\""..command.."\")")
  116.  file.writeLine("os.unloadAPI(\"flex\")")
  117.  file.close()
  118. end --function
  119.  
  120.  
  121. stuck = false
  122. function isStuck()
  123.  return stuck
  124. end
  125.  
  126.  
  127. function update(n)
  128.  
  129.  if n=="fwd" then
  130.  
  131.   if dist_r == 0 then
  132.    dist_z = dist_z + 1
  133.    lastMove = "z+"
  134.    
  135.   elseif dist_r == 90 then
  136.    dist_x = dist_x + 1
  137.    lastMove = "x+"
  138.    
  139.   elseif dist_r == 180 then
  140.    dist_z = dist_z - 1
  141.    lastMove = "z-"
  142.    
  143.   elseif dist_r == 270 then
  144.    dist_x = dist_x - 1
  145.    lastMove = "x-"
  146.    
  147.   end
  148.  
  149.  elseif n=="up" then
  150.   dist_y = dist_y + 1
  151.   lastMove = "y+"
  152.  
  153.  elseif n=="down" then
  154.   dist_y = dist_y - 1
  155.   lastMove = "y-"
  156.  
  157.  elseif n=="right" then
  158.   dist_r = dist_r + 90
  159.  
  160.  elseif n=="left" then
  161.   dist_r = dist_r - 90
  162.  
  163.  end
  164.  
  165.  while dist_r >= 360 do
  166.   dist_r = dist_r - 360
  167.  end
  168.  while dist_r < 0 do
  169.   dist_r = dist_r + 360
  170.  end
  171.  
  172.  if dist_x < xmin then
  173.   xmin = dist_x
  174.  elseif dist_x > xmax then
  175.   xmax = dist_x
  176.  end
  177.  
  178.  if dist_y < ymin then
  179.   ymin = dist_y
  180.  elseif dist_y > ymax then
  181.   ymax = dist_y
  182.  end
  183.  
  184.  if dist_z < zmin then
  185.   zmin = dist_z
  186.  elseif dist_z > zmax then
  187.   zmax = dist_z
  188.  end
  189.  
  190.  saveCoords()
  191.  
  192. end
  193.  
  194.  
  195. -- MOVEMENT FUNCTIONS
  196.  
  197. function refuel()
  198.  local a,x,z,slot
  199.  slot = turtle.getSelectedSlot()
  200.  a = true
  201.  while turtle.getFuelLevel() == 0 do
  202.   for x=fuelSlot[1],fuelSlot[2] do
  203.    turtle.select(x)
  204.    if turtle.refuel(1) then break end
  205.    if x == fuelSlot[2] and a then
  206.     z = "Waiting for fuel (slot "
  207.     ..tostring(fuelSlot[1])
  208.     if fuelSlot[1] ~= fuelSlot[2] then
  209.      z = z.."-"..tostring(fuelSlot[2])
  210.     end --if
  211.     z = z..")..."
  212.     flex.send(z,colors.yellow)
  213.     a = false
  214.    end --if
  215.   end --for
  216.  end --while
  217.  if not a then
  218.   flex.send("Thanks!",colors.lime)
  219.  end --if
  220.  turtle.select(slot)
  221. end --function
  222.  
  223.  
  224. function fwd(n)
  225.  local x,a,b
  226.  for x=1, n or 1 do
  227.   refuel()
  228.   if flex.getBlock() == "minecraft:tall_grass" or
  229.      flex.getBlock() == "minecraft:grass" then
  230.    turtle.dig()
  231.   end
  232.   stuck = false
  233.   while not turtle.forward() do
  234.    a,b = turtle.dig()
  235.    if b == "Unbreakable block detected" then
  236.     flex.send(b,colors.orange)
  237.     stuck = true
  238.     return
  239.    end
  240.    --turtle.attack()
  241.   end
  242.   update("fwd")
  243.  end
  244. end
  245.  
  246. function up(n)
  247.  local x,a,b
  248.  for x=1, n or 1 do
  249.   refuel()
  250.   stuck = false
  251.   while not turtle.up() do
  252.    a,b = turtle.digUp()
  253.    if b == "Unbreakable block detected" then
  254.     flex.send(b,colors.orange)
  255.     stuck = true
  256.     return
  257.    end
  258.    --turtle.attackUp()
  259.   end
  260.   update("up")
  261.  end
  262.  if (n or 1) < 0 then
  263.   down(-n)
  264.  end
  265. end
  266.  
  267. function down(n)
  268.  local x
  269.  for x=1, n or 1 do
  270.   refuel()
  271.   stuck = false
  272.   while not turtle.down() do
  273.    a,b = turtle.digDown()
  274.    if b == "Unbreakable block detected" then
  275.     flex.send(b,colors.orange)
  276.     stuck = true
  277.     return
  278.    end
  279.    --turtle.attackDown()
  280.   end
  281.   update("down")
  282.  end
  283.  if (n or 1) < 0 then
  284.   up(-n)
  285.  end
  286. end
  287.  
  288. function left(n)
  289.  local x
  290.  for x=1, n or 1 do
  291.   turtle.turnLeft()
  292.   update("left")
  293.  end
  294.  for x=n or 1, -1 do
  295.   turtle.turnRight()
  296.   update("right")
  297.  end
  298. end
  299.  
  300. function right(n)
  301.  local x
  302.  for x=1, n or 1 do
  303.   turtle.turnRight()
  304.   update("right")
  305.  end
  306.  for x=n or 1, -1 do
  307.   turtle.turnLeft()
  308.   update("left")
  309.  end
  310. end
  311.  
  312. function dig()
  313.  while turtle.dig() do end
  314. end
  315.  
  316.  
  317. -- GOTO FUNCTIONS
  318.  
  319. function gotor(r)
  320.  local r = math.floor(r/90)*90
  321.  while r>=360 do r = r-360 end
  322.  while r<0 do r = r+360 end
  323.  
  324.  local x = r-dist_r
  325.  while x < 0 do x = x+360 end
  326.  if x == 90 then right()
  327.  elseif x == 180 then
  328.   if r == 0 then right(2)
  329.   else left(2) end
  330.  elseif x == 270 then left()
  331.  end
  332. end
  333.  
  334. function gotoy(y)
  335.  while dist_y < y do
  336.   up()
  337.  end
  338.  while dist_y > y do
  339.   down()
  340.  end
  341. end
  342.  
  343. function gotox(x)
  344.  if dist_x < x then
  345.   gotor(90)
  346.  end
  347.  if dist_x > x then
  348.   gotor(270)
  349.  end
  350.  while dist_x ~= x do
  351.   fwd()
  352.  end
  353. end
  354.  
  355. function gotoz(z)
  356.  if dist_z < z then
  357.   gotor(0)
  358.  end
  359.  if dist_z > z then
  360.   gotor(180)
  361.  end
  362.  while dist_z ~= z do
  363.   fwd()
  364.  end
  365. end
  366.  
  367. function goto(x,y,z,r,lm)
  368.  if type(x) == "table" then
  369.   if #x < 4 then
  370.    flex.send("Invalid Goto Table",colors.red)
  371.    return
  372.   end
  373.   y = x[2]
  374.   z = x[3]
  375.   r = x[4]
  376.   lm = x[5]
  377.   x = x[1]
  378.  end
  379.  gotoz(z or 0)
  380.  gotox(x or 0)
  381.  gotor(r or 0)
  382.  gotoy(y or 0)
  383.  setLastMove(lm or "?")
  384. end
  385.  
  386. -------
  387.  
  388. function place()
  389.  while not turtle.place() and (
  390.     flex.getBlock()=="minecraft:air" or
  391.     flex.getBlock()=="minecraft:water" or
  392.     flex.getBlock()=="minecraft:lava" ) do
  393.   turtle.attack()
  394.   turtle.attackUp()
  395.   turtle.attackDown()
  396.  end
  397. end
  398.  
  399. function placeUp()
  400.  while not turtle.placeUp() and (
  401.     flex.getBlock("up")=="minecraft:air" or
  402.     flex.getBlock("up")=="minecraft:water" or
  403.     flex.getBlock("up")=="minecraft:lava" ) do
  404.   turtle.attack()
  405.   turtle.attackUp()
  406.  end
  407. end
  408.  
  409. function placeDown()
  410.  while not turtle.placeDown() and (
  411.     flex.getBlock("down")=="minecraft:air" or
  412.     flex.getBlock("down")=="minecraft:water" or
  413.     flex.getBlock("down")=="minecraft:lava" ) do
  414.   turtle.attack()
  415.   turtle.attackDown()
  416.  end
  417. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement