Mathilde411

champ.lua

Feb 16th, 2023 (edited)
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. local BASE = {
  2.     x = -75,
  3.     y = 64,
  4.     z = -252
  5. }
  6.  
  7. local GROUND_HEIGHT = 64
  8.  
  9. --posZ = 0, posX = 1, negZ = 2, negX = 3
  10. local CURRENT_ANGLE = nil
  11.  
  12. local LAST_TURN_DIRECTION = 1
  13.  
  14. local NODES = {
  15.     {
  16.         z = -253,
  17.         xStart = -71,
  18.         xEnd = -85
  19.     },
  20.     {
  21.         z = -256,
  22.         xStart = -85,
  23.         xEnd = -71
  24.     },
  25.     {
  26.         z = -259,
  27.         xStart = -71,
  28.         xEnd = -85
  29.     },
  30.     {
  31.         z = -262,
  32.         xStart = -85,
  33.         xEnd = -71
  34.     },
  35. }
  36.  
  37. function turnOnce(direction)
  38.     local success = false
  39.     if(direction == 1) then
  40.         success = turtle.turnLeft()
  41.     elseif(direction == -1) then
  42.         success = turtle.turnRight()
  43.     end
  44.  
  45.     if(success) then
  46.         CURRENT_ANGLE = (CURRENT_ANGLE + direction) % 4
  47.     end
  48.  
  49.     return success
  50. end
  51.  
  52. function turn(angle)
  53.     local success = true
  54.     local absoluteAngle = math.abs(angle)
  55.     local direction = angle / absoluteAngle
  56.     for _ = 1, absoluteAngle do
  57.         success = success and turnOnce(direction)
  58.     end
  59.  
  60.     if(success) then
  61.         LAST_TURN_DIRECTION = direction
  62.     end
  63.     return success
  64. end
  65.  
  66. function turnTo(angle)
  67.     diffAngle = (angle - CURRENT_ANGLE) % 4
  68.     diffAngle = diffAngle > 2 and diffAngle - 4 or diffAngle
  69.     diffAngle = diffAngle == 2 and -1 * LAST_TURN_DIRECTION * diffAngle or diffAngle
  70.     return turn(diffAngle)
  71. end
  72.  
  73. function goTo(x, y, z)
  74.     local currX, currY, currZ = gps.locate()
  75.     local stepsX = x - currX
  76.     local stepsY = y - currY
  77.     local stepsZ = z - currZ
  78.  
  79.     if(stepsY ~= 0) then
  80.         local units = math.abs(stepsY)
  81.         local direction = stepsY / units
  82.         if(direction > 0) then
  83.             for _ = 1, units do
  84.                 if(not turtle.up()) then
  85.                     return false
  86.                 end
  87.             end
  88.         else
  89.             for _ = 1, units do
  90.                 if(not turtle.down()) then
  91.                     return false
  92.                 end
  93.             end
  94.         end
  95.     end
  96.  
  97.     if(stepsX ~= 0) then
  98.         local units = math.abs(stepsX)
  99.         local direction = stepsX / units
  100.  
  101.         if(direction > 0) then
  102.             if(not turnTo(1)) then
  103.                 return false
  104.             end
  105.         else
  106.             if(not turnTo(3)) then
  107.                 return false
  108.             end
  109.         end
  110.  
  111.         for _ = 1, units do
  112.             if(not turtle.forward()) then
  113.                 return false
  114.             end
  115.         end
  116.     end
  117.  
  118.     if(stepsZ ~= 0) then
  119.         local units = math.abs(stepsZ)
  120.         local direction = stepsZ / units
  121.  
  122.         if(direction > 0) then
  123.             if(not turnTo(0)) then
  124.                 return false
  125.             end
  126.         else
  127.             if(not turnTo(2)) then
  128.                 return false
  129.             end
  130.         end
  131.  
  132.         for _ = 1, units do
  133.             if(not turtle.forward()) then
  134.                 return false
  135.             end
  136.         end
  137.     end
  138. end
  139.  
  140. function findFacing()
  141.     local currX, currY, currZ = gps.locate()
  142.     local moved = false
  143.     local blocked = false
  144.     local up = 0
  145.  
  146.     repeat
  147.         local turnCount = 0
  148.         while((not moved) and (turnCount < 4)) do
  149.             if(turtle.forward()) then
  150.                 moved = true
  151.             else
  152.                 turtle.turnLeft()
  153.                 turnCount = turnCount + 1
  154.             end
  155.         end
  156.  
  157.         if(not moved) then
  158.             blocked = blocked or (not turtle.up())
  159.             if(not blocked) then
  160.                 up = up + 1
  161.             end
  162.         end
  163.     until(moved or blocked)
  164.  
  165.     local newX, newY, newZ = gps.locate()
  166.  
  167.     turtle.turnLeft()
  168.     turtle.turnLeft()
  169.     turtle.forward()
  170.     turtle.turnLeft()
  171.     turtle.turnLeft()
  172.  
  173.     for _ = 1,up do
  174.         turtle.down()
  175.     end
  176.  
  177.     if(not moved) then
  178.         return false
  179.     end
  180.  
  181.     local diffX = newX - currX
  182.     local diffZ = newZ - currZ
  183.  
  184.     if(diffZ > 0) then
  185.         CURRENT_ANGLE = 0
  186.     elseif(diffX > 0) then
  187.         CURRENT_ANGLE = 1
  188.     elseif(diffZ < 0) then
  189.         CURRENT_ANGLE = 2
  190.     else
  191.         CURRENT_ANGLE = 3
  192.     end
  193.  
  194.     return true
  195. end
  196.  
  197. function hoverTo(x, y, z)
  198.     local currX, currY, currZ = gps.locate()
  199.     goTo(currX, GROUND_HEIGHT + 1, currZ)
  200.     goTo(x, GROUND_HEIGHT + 1, z)
  201.     goTo(x, y, z)
  202. end
  203.  
  204. function getToBase()
  205.     hoverTo(BASE.x, BASE.y, BASE.z)
  206. end
  207.  
  208. function refuel()
  209.     goTo(BASE.x, BASE.y + 3, BASE.z)
  210.     turnTo(0)
  211.     while(turtle.getFuelLevel() < 500) do
  212.         turtle.suck(1)
  213.         turtle.refuel()
  214.     end
  215. end
  216.  
  217. function refurbish()
  218.     goTo(BASE.x, BASE.y + 2, BASE.z)
  219.     turnTo(0)
  220.  
  221.     turtle.select(1)
  222.     turtle.suck(64)
  223. end
  224.  
  225. function harvestSeed()
  226.     inspect, result = turtle.inspect()
  227.    
  228.     if(inspect and result.name == "minecraft:wheat" and result.state.age >= 7) then
  229.         turtle.dig()
  230.         turtle.place()
  231.     end
  232. end
  233.  
  234. function harvest()
  235.     turtle.select(1)
  236.     hoverTo(NODES[1].xStart, GROUND_HEIGHT, NODES[1].z)
  237.     for i = 1,4 do
  238.         local step = nil
  239.         if(NODES[i].xStart < NODES[i].xEnd) then
  240.             step = 1
  241.         else
  242.             step = -1
  243.         end
  244.  
  245.         for x = NODES[i].xStart, NODES[i].xEnd, step  do
  246.             goTo(x, GROUND_HEIGHT, NODES[i].z)
  247.             if(x ~= NODES[i].xStart and x ~= NODES[i].xEnd) then
  248.                 turnTo(0)
  249.                 harvestSeed()
  250.                 turnTo(2)
  251.                 harvestSeed()
  252.             end
  253.         end
  254.  
  255.         if(i < 4) then
  256.             goTo(NODES[i+1].xStart, GROUND_HEIGHT, NODES[i+1].z)
  257.         end
  258.     end
  259.     getToBase()
  260. end
  261.  
  262. function deposit()
  263.     goTo(BASE.x, BASE.y + 2, BASE.z)
  264.     turnTo(0)
  265.  
  266.     for i=1,16 do
  267.         turtle.select(i)
  268.         local details = turtle.getItemDetail()
  269.         if(details and details.name == "minecraft:wheat_seeds") then
  270.             turtle.drop()
  271.         end
  272.     end
  273.  
  274.     goTo(BASE.x, BASE.y + 1, BASE.z)
  275.     turnTo(0)
  276.  
  277.     for i=1,16 do
  278.         turtle.select(i)
  279.         local details = turtle.getItemDetail()
  280.         if(details and details.name == "minecraft:wheat") then
  281.             turtle.drop()
  282.         end
  283.     end
  284.  
  285.     goTo(BASE.x, BASE.y, BASE.z)
  286.     turnTo(0)
  287.  
  288.     for i=1,16 do
  289.         turtle.select(i)
  290.         local details = turtle.getItemDetail()
  291.         if(details and details.name == "minecraft:bread") then
  292.             turtle.drop()
  293.         end
  294.     end
  295.  
  296.     goTo(BASE.x - 1, BASE.y, BASE.z)
  297.     turnTo(0)
  298.  
  299.     for i=1,16 do
  300.         turtle.select(i)
  301.         local details = turtle.getItemDetail()
  302.         if(details and details.name == "minecraft:diamond_hoe") then
  303.             turtle.drop()
  304.         end
  305.     end
  306.  
  307.     goTo(BASE.x - 1, BASE.y + 1, BASE.z)
  308.     turnTo(0)
  309.  
  310.     for i=1,16 do
  311.         turtle.select(i)
  312.         local details = turtle.getItemDetail()
  313.         if(details and details.name == "minecraft:crafting_table") then
  314.             turtle.drop()
  315.         end
  316.     end
  317.  
  318.     goTo(BASE.x - 1, BASE.y, BASE.z)
  319.     for i=1,16 do
  320.         turtle.select(i)
  321.         turtle.dropDown()
  322.     end
  323. end
  324.  
  325. function unequip()
  326.     turtle.select(1)
  327.     turtle.equipLeft()
  328.  
  329.     local details = turtle.getItemDetail()
  330.     if(details and details.name == "minecraft:diamond_hoe") then
  331.         goTo(BASE.x - 1, BASE.y, BASE.z)
  332.     elseif(details and details.name == "minecraft:crafting_table") then
  333.         goTo(BASE.x - 1, BASE.y + 1, BASE.z)
  334.     end
  335.  
  336.     turnTo(0)
  337.     turtle.drop()
  338. end
  339.  
  340. function equip(type)
  341.     unequip()
  342.     if(type == "hoe") then
  343.         goTo(BASE.x - 1, BASE.y, BASE.z)
  344.     elseif(type == "craft") then
  345.         goTo(BASE.x - 1, BASE.y + 1, BASE.z)
  346.     end
  347.  
  348.     turnTo(0)
  349.     turtle.suck(1)
  350.     turtle.equipLeft()
  351. end
  352.  
  353. function craft()
  354.     turtle.select(16)
  355.  
  356.     local crafting = true
  357.     while(crafting) do
  358.         goTo(BASE.x, BASE.y + 1, BASE.z)
  359.         turnTo(0)
  360.  
  361.         turtle.suck()
  362.  
  363.         local items = turtle.getItemCount()
  364.         if(items >= 3) then
  365.             local toCraft = math.floor(items/3)
  366.             turtle.transferTo(5, toCraft)
  367.             turtle.transferTo(6, toCraft)
  368.             turtle.transferTo(7, toCraft)
  369.             turtle.drop()
  370.             turtle.craft()
  371.             goTo(BASE.x, BASE.y, BASE.z)
  372.             turnTo(0)
  373.             turtle.drop()
  374.         else
  375.             turtle.drop()
  376.             crafting = false
  377.         end
  378.     end
  379. end
  380.  
  381.  
  382. findFacing()
  383. getToBase()
  384. deposit()
  385.  
  386. while(true) do
  387.     equip("hoe")
  388.     refuel()
  389.     refurbish()
  390.     harvest()
  391.     deposit()
  392.     equip("craft")
  393.     craft()
  394.     deposit()
  395.     goTo(BASE.x, BASE.y, BASE.z)
  396.     turnTo(2)
  397.     sleep(600)
  398. end
Advertisement
Add Comment
Please, Sign In to add comment