Advertisement
Guest User

myAPI.lua

a guest
Apr 5th, 2020
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. function forcemove()
  2.     for i=1,16 do
  3.         if turtle.forward() then
  4.             if facing==0 then pos.x = pos.x+1
  5.             elseif facing==2 then pos.x = pos.x-1
  6.             elseif facing==1 then pos.z = pos.z+1
  7.             elseif facing==3 then pos.z = pos.z-1
  8.             end
  9.             return true
  10.         end
  11.         turtle.dig()
  12.         if i>2 then sleep(0.25) end
  13.     end
  14.     return false
  15. end
  16.  
  17. function forcedown()
  18.     for i=1,16 do
  19.         if turtle.down() then
  20.             pos.y = pos.y-1
  21.             return true
  22.         end
  23.         turtle.digDown()
  24.         if i>2 then sleep(0.25) end
  25.     end
  26.     return false
  27. end
  28.  
  29. function trymove()
  30.     repeat
  31.     until turtle.forward()
  32.     if facing==0 then pos.x = pos.x+1
  33.     elseif facing==2 then pos.x = pos.x-1
  34.     elseif facing==1 then pos.z = pos.z+1
  35.     elseif facing==3 then pos.z = pos.z-1
  36.     end
  37. end
  38.  
  39. function tryup()
  40.     repeat
  41.     until turtle.up()
  42.     pos.y = pos.y+1
  43. end
  44.  
  45. function turnTo(face)
  46.     if (facing-face)%4==1 then
  47.         turtle.turnLeft()
  48.         facing = (facing-1)%4
  49.     end
  50.     while facing ~= face%4 do
  51.         turtle.turnRight()
  52.         facing = (facing+1)%4
  53.     end
  54. end
  55.  
  56. function trydown()
  57.     repeat
  58.     until turtle.down()
  59.     pos.y = pos.y-1
  60. end
  61.  
  62. function moveOver()
  63.     while not turtle.forward() do
  64.         tryup()
  65.     end
  66.     if facing==0 then pos.x = pos.x+1
  67.     elseif facing==2 then pos.x = pos.x-1
  68.     elseif facing==1 then pos.z = pos.z+1
  69.     elseif facing==3 then pos.z = pos.z-1
  70.     end
  71.     while not turtle.detectDown() do
  72.         trydown()
  73.     end
  74. end
  75.  
  76. function fuelToGo(x,y,z, hover)
  77.     return math.abs(x-pos.x)+math.abs(z-pos.z)+math.abs(hover-pos.y)+math.abs(hover-y)
  78. end
  79.  
  80. function dropIf(item)
  81.     local data = turtle.getItemDetail()
  82.     if data then
  83.         if data.name == item then
  84.             turtle.drop()
  85.         end
  86.     end
  87. end
  88.  
  89. function dropFiltered()
  90.     local data = turtle.getItemDetail()
  91.     if data and filterTable then
  92.         for i=1,#filterTable do
  93.             if data.name == filterTable[i] then
  94.                 turtle.drop()
  95.             end
  96.         end
  97.     end
  98. end
  99.  
  100. function addFilter(item)
  101.     if item=='c' then item='minecraft:cobblestone'
  102.     elseif item=='d' then item='minecraft:dirt' end
  103.     if filterTable then
  104.         table.insert(filterTable,item)
  105.     else
  106.         filterTable = {item}
  107.     end
  108. end
  109.  
  110. function deleteFilters()
  111.     filterTable = nil
  112. end
  113.  
  114. function printFilter()
  115.     if filterTable then
  116.         for i=1,#filterTable do
  117.             print( filterTable[i] )
  118.         end
  119.     else print( 'No item filter' ) end
  120. end
  121.  
  122. function setBase(xx,yy,zz)
  123.     basepos = {x = 0, y = 0, z = 0}
  124.     basepos.x = xx
  125.     basepos.y = yy
  126.     basepos.z = zz
  127. end
  128.  
  129. function setPos(xx,yy,zz,face)
  130.     pos = {x = 0, y = 0, z = 0}
  131.     pos.x = xx
  132.     pos.y = yy
  133.     pos.z = zz
  134.     facing = face
  135. end
  136. --facing: 0=+x 1=+z 2=-x 3=-z
  137. function goPlace(xx,yy,zz,hover)
  138.     turnTo(0)
  139.     local relx = pos.x-xx
  140.     local rely = pos.y-yy
  141.     local relz = pos.z-zz
  142.     while hover>pos.y do
  143.         tryup()
  144.     end
  145.     while hover<pos.y do
  146.         trydown()
  147.     end
  148.     if relz>0 then
  149.         turnTo(3)
  150.         repeat
  151.             trymove()
  152.         until pos.z==zz
  153.     elseif relz<0 then
  154.         turnTo(1)
  155.         repeat
  156.             trymove()
  157.         until pos.z==zz
  158.     end
  159.     turnTo(0)
  160.     if relx>0 then
  161.         turnTo(2)
  162.         repeat
  163.             trymove()
  164.         until pos.x==xx
  165.     elseif relx<0 then
  166.         turnTo(0)
  167.         repeat
  168.             trymove()
  169.         until pos.x==xx
  170.     end
  171.     turnTo(0)
  172.     while 0>pos.y-yy do
  173.         tryup()
  174.     end
  175.     while 0<pos.y-yy do
  176.         trydown()
  177.     end
  178. end
  179.  
  180. function returnPlace(xx,yy,zz,hover)
  181.     goPlace(xx,hover,pos.z,hover)
  182.     goPlace(xx,yy,zz,hover)
  183. end
  184.  
  185. function setBasePos(xx,yy,zz,face)
  186.     setBase(xx,yy,zz)
  187.     setPos(xx,yy,zz,face)
  188. end
  189.  
  190. function returnBase(hover)
  191.     returnPlace(basepos.x,basepos.y,basepos.z,hover)
  192. end
  193.  
  194. function posx()
  195.     return pos.x
  196. end
  197. function posy()
  198.     return pos.y
  199. end
  200. function posz()
  201.     return pos.x
  202. end
  203.  
  204. function invFull(from)
  205.     local isfull = true
  206.     for i = from, 16 do
  207.         if turtle.getItemCount(i)==0 then
  208.             isfull = false
  209.         end
  210.     end
  211.     return isfull
  212. end
  213.  
  214. function invSelectAny(from)
  215.     local succ = false
  216.     for i = from,16 do
  217.         if turtle.getItemCount(i)>0 then
  218.             turtle.select(i)
  219.             succ = true
  220.             break
  221.         end
  222.     end
  223.     return succ
  224. end
  225.  
  226. function dropAll(from)
  227.     for i = from,16 do
  228.         turtle.select(i)
  229.         dropFiltered()
  230.         turtle.dropDown()
  231.     end
  232.     return not invSelectAny(from)
  233. end
  234.  
  235. function refuelUntil(slot,fuelneed)
  236.     if slot==1 then print("Request ", math.ceil(fuelneed/80)," coal") end
  237.     turtle.select(slot)
  238.     while turtle.getFuelLevel()<fuelneed do
  239.         turtle.refuel(1)
  240.     end
  241. end
  242.  
  243. function digShaftDown(x,y,z, depth)
  244.     refuelUntil(1,fuelToGo(x,y,z,y)*2+depth*2+2)
  245.     trymove()
  246.     goPlace(x,y,z,y)
  247.     for i=1,depth do
  248.         if not forcedown() then break end
  249.     end
  250.     returnPlace(x,y+1,z,y+1)
  251.     returnBase(y+1)
  252.     repeat
  253.     until dropAll(2)
  254. end
  255.  
  256. function digShaftDown2(x,y,z, depth)
  257.     refuelUntil(1,fuelToGo(x,y,z,y)*2+depth*2+2)
  258.     trymove()
  259.     goPlace(x,y,z,y)
  260.     for i=1,depth do
  261.         if not forcedown() then break end
  262.         turtle.dig()
  263.     end
  264.     returnPlace(x,y+1,z,y+1)
  265.     returnBase(y+1)
  266.     repeat
  267.     until dropAll(2)
  268. end
  269.  
  270. function digShaftDown3(x,y,z, depth)
  271.     refuelUntil(1,fuelToGo(x,y,z,y)*2+depth*2+2)
  272.     trymove()
  273.     goPlace(x,y,z,y)
  274.     for i=1,depth do
  275.         if not forcedown() then break end
  276.         turtle.dig()
  277.     end
  278.     turnTo(2)
  279.     for i=1,y-pos.y do
  280.         turtle.dig()
  281.         tryup()
  282.     end
  283.     returnPlace(x,y+1,z,y+1)
  284.     returnBase(y+1)
  285.     repeat
  286.     until dropAll(2)
  287. end
  288. --digShafts(1,10,0,1,10, 60)
  289. function digShafts(x1,x2,y,z1,z2, depth)
  290.     local xm3 = (x2-x1+1)%3
  291.     for z=z1,z2 do
  292.         for x=x1+1,x2-xm3-1,3 do
  293.             digShaftDown3(x,y,z, depth)
  294.         end
  295.         if xm3==2 then
  296.             digShaftDown2(x2-1,y,z, depth)
  297.         elseif xm3==1 then
  298.             digShaftDown(x2,y,z, depth)
  299.         end
  300.     end
  301. end
  302.  
  303. function cover(x1,x2,z1,z2, gapx, gapz)
  304.     refuelUntil(1,fuelToGo(x1,pos.y,z1,pos.y)*2+(x2-x1)*(z2-z1)*3/gapz)
  305.     local maxy = pos.y
  306.     local odd = true
  307.     trymove()
  308.     goPlace(x1,pos.y,z1,pos.y)
  309.     while not turtle.detectDown() do
  310.         trydown()
  311.     end
  312.     for z=z1+gapz,z2,gapz do
  313.         if odd then
  314.             for x=x1,x2,gapx do
  315.                 while pos.x<x do
  316.                     moveOver()
  317.                     maxy = math.max(maxy,pos.y)
  318.                 end
  319.                 tryup()
  320.                 if not invSelectAny(2) then
  321.                     returnBase(maxy+1)
  322.                     return false
  323.                 end
  324.                 turtle.placeDown()
  325.             end
  326.         else
  327.             for x=x2,x1,-gapx do
  328.                 while pos.x>x do
  329.                     moveOver()
  330.                     maxy = math.max(maxy, pos.y)
  331.                 end
  332.                 tryup()
  333.                 if not invSelectAny(2) then
  334.                     returnBase(maxy+1)
  335.                     return false
  336.                 end
  337.                 turtle.placeDown()
  338.             end
  339.         end
  340.         turnTo(1)
  341.         while pos.z<z do
  342.             moveOver()
  343.             maxy = math.max(maxy, pos.y)
  344.         end
  345.         if odd then turnTo(2)
  346.         else turnTo(0) end
  347.         odd = not odd
  348.     end
  349.     returnBase(maxy+1)
  350. end
  351.  
  352. function digTunnel(x,y,z,dir ,depth)
  353.     refuelUntil(1,fuelToGo(x,y,z,y)*2+depth*2+2)
  354.     trymove()
  355.     goPlace(x,y,z,pos.y)
  356.     turnTo(dir)
  357.     for i=1,depth do
  358.         if not forcemove() then
  359.             refuelUntil(1,fuelToGo(base.x,base.y,base.z,pos.y)+depth*2+2)
  360.             break
  361.         end
  362.         turtle.digUp()
  363.     end
  364.     turnTo(dir+2)
  365.     for i=1,depth do
  366.         turtle.digDown()
  367.         forcemove()
  368.     end
  369.     returnPlace(x,y,z,y)
  370.     returnBase(basepos.y+1)
  371.     repeat
  372.     until dropAll(2)
  373. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement