BillBodkin

gotoWaterSourceCords

Sep 22nd, 2021 (edited)
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.15 KB | None | 0 0
  1. local pickSlot  = 13
  2. local modemSlot = 14
  3. local chestSlot = 16
  4.  
  5. local pickName  = "minecraft:diamond_pickaxe"
  6. local modemName = "computercraft:wireless_modem_advanced"
  7.  
  8. local minFuel = turtle.getFuelLimit() * 0.25
  9. local maxFuel = turtle.getFuelLimit() * 0.75
  10.  
  11. local validFuels = {
  12.     ["minecraft:coal_block"] = true,
  13.     ["minecraft:coal"] = true,
  14.     ["minecraft:charcoal"] = true,
  15.     ["minecraft:blaze_rod"] = true
  16. }
  17.  
  18. sleep(1)--might need to increase to prevent server crash
  19.  
  20. if fs.exists("DiscordHook.lua") == false then
  21.     shell.execute("wget", "https://raw.githubusercontent.com/Wendelstein7/DiscordHook-CC/master/DiscordHook.lua", "DiscordHook.lua")
  22. end
  23.  
  24. local DiscordHook = require("./DiscordHook")
  25.  
  26. local hookSuccess, hook = DiscordHook.createWebhook("https://discord.com/api/webhooks/890291858431959120/1tn7J_aOFVJfxIqSwds0r2GvfgPyrzlnq6XE8TDPL10AfO0bx9GFaeGuRw6Q_fSpOGOG")
  27. if not hookSuccess then
  28.   error("Webhook connection failed! Reason: " .. hook)
  29. end
  30.  
  31. function Hold(s)
  32.     if turtle.getItemCount(s) == 1 then
  33.         turtle.select(s)
  34.         turtle.equipLeft()
  35.         local i = turtle.getItemDetail()
  36.         if i ~= nil then
  37.             if i.name == pickName then
  38.                 turtle.transferTo(pickSlot)
  39.             elseif i.name == geoName then
  40.                 turtle.transferTo(geoSlot)
  41.             elseif i.name == modemName then
  42.                 turtle.transferTo(modemSlot)
  43.             else
  44.                 turtle.transferTo(1)
  45.             end
  46.         end
  47.         turtle.select(1)
  48.     end
  49. end
  50.  
  51. local facing = "unknown"
  52. local pos = "unknown"
  53.  
  54. function Forward()
  55.     while turtle.forward() == false do
  56.         turtle.dig()
  57.     end
  58.    
  59.     if facing == "south" then
  60.         pos["z"] = pos["z"] + 1
  61.     elseif facing == "north" then
  62.         pos["z"] = pos["z"] - 1
  63.     elseif facing == "east" then
  64.         pos["x"] = pos["x"] + 1
  65.     elseif facing == "west" then
  66.         pos["x"] = pos["x"] - 1
  67.     end
  68.    
  69.     print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
  70. end
  71.  
  72. function Up()
  73.     while turtle.up() == false do
  74.         turtle.digUp()
  75.     end
  76.    
  77.     pos["y"] = pos["y"] + 1
  78.     print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
  79. end
  80.  
  81. function Down()
  82.     while turtle.down() == false do
  83.         turtle.digDown()
  84.     end
  85.    
  86.     pos["y"] = pos["y"] - 1
  87.     print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
  88. end
  89.  
  90. function Dig()
  91.     while turtle.detect() do
  92.         turtle.dig()
  93.     end
  94. end
  95.  
  96. function DigUp()
  97.     while turtle.detectUp() do
  98.         turtle.digUp()
  99.     end
  100. end
  101.  
  102. function DigDown()
  103.     while turtle.detectDown() do
  104.         turtle.digDown()
  105.     end
  106. end
  107.  
  108. function GetIndex(v, t)
  109.     for tk, tv in pairs(t) do
  110.         if tv == v then
  111.             return tk
  112.         end
  113.     end
  114. end
  115.  
  116. local dirs = {
  117.     [1] = "north",
  118.     [2] = "east",
  119.     [3] = "south",
  120.     [4] = "west"
  121. }
  122.  
  123. function Left()
  124.     turtle.turnLeft()
  125.    
  126.     local cd = GetIndex(facing, dirs)
  127.    
  128.     nd = cd - 1
  129.     if nd == 0 then
  130.         nd = 4
  131.     end
  132.    
  133.     print("Facing " .. dirs[nd])
  134.     facing = dirs[nd]
  135. end
  136.  
  137. function Right()
  138.     turtle.turnRight()
  139.    
  140.     local cd = GetIndex(facing, dirs)
  141.    
  142.     nd = cd + 1
  143.     if nd == 5 then
  144.         nd = 1
  145.     end
  146.    
  147.     print("Facing " .. dirs[nd])
  148.     facing = dirs[nd]
  149. end
  150.  
  151. function Face(nd)
  152.     local cd = GetIndex(facing, dirs)
  153.     local td = GetIndex(nd, dirs)
  154.     print("Currently facing: " .. facing .. " " .. tostring(cd))
  155.     print("Target facing: " .. nd .. " " .. tostring(td))
  156.    
  157.     facing = nd
  158.    
  159.     if cd == td then
  160.         print("Already facing " .. nd)
  161.         return
  162.     end
  163.    
  164.     if cd == 1 and td == 4 then
  165.         turtle.turnLeft()
  166.         print("Facing " .. nd)
  167.         return
  168.     end
  169.    
  170.     if cd == 4 and td == 1 then
  171.         turtle.turnRight()
  172.         print("Facing " .. nd)
  173.         return
  174.     end
  175.    
  176.     if cd < td then
  177.         for i = cd, td - 1 do
  178.             turtle.turnRight()
  179.         end
  180.     end
  181.    
  182.     if cd > td then
  183.         for i = cd, td + 1, -1 do
  184.             turtle.turnLeft()
  185.         end
  186.     end
  187.    
  188.     print("Facing " .. nd)
  189. end
  190.  
  191. function Inv()
  192.     Hold(pickSlot)
  193.     DigUp()
  194.     turtle.select(16)
  195.     while turtle.placeUp() == false do
  196.         DigUp()
  197.         turtle.attackUp()
  198.     end
  199.     local c = peripheral.wrap("top")
  200.    
  201.     if turtle.getFuelLevel() < minFuel then
  202.         local fuelAttempts = 0
  203.         turtle.select(1)
  204.         while turtle.getFuelLevel() < maxFuel do
  205.             local cl = c.list()
  206.             print(cl[1].name)
  207.             if cl[1] ~= nil and validFuels[cl[1].name] then
  208.                 turtle.suckUp(cl[1].count - 1)
  209.                 turtle.refuel(64)
  210.             else
  211.                 print("Waiting for fuel")
  212.             end
  213.             sleep(1)
  214.             fuelAttempts = fuelAttempts + 1
  215.             if fuelAttempts > 10 and turtle.getFuelLevel() > minFuel then
  216.                 print("Will try get more fuel later")
  217.                 break
  218.             end
  219.         end
  220.     end
  221.    
  222.     turtle.select(16)
  223.     Hold(pickSlot)
  224.     turtle.digUp()
  225.     turtle.select(1)
  226. end
  227.  
  228. function GetDir()
  229.     Hold(modemSlot)
  230.     local sx, sy, sz = gps.locate()
  231.     while sx == nil do
  232.         print("Coudn't get GPS")
  233.         sleep(3)
  234.         sx, sy, sz = gps.locate()
  235.     end
  236.     Hold(pickSlot)
  237.     Forward()
  238.     Hold(modemSlot)
  239.     local nx, ny, nz = gps.locate()
  240.     while nx == nil do
  241.         print("Coudn't get GPS")
  242.         sleep(3)
  243.         nx, ny, nz = gps.locate()
  244.     end
  245.    
  246.     turtle.turnLeft()
  247.     turtle.turnLeft()
  248.    
  249.     Hold(pickSlot)
  250.     Forward()
  251.    
  252.     turtle.turnLeft()
  253.     turtle.turnLeft()
  254.    
  255.     if nz > sz then
  256.         facing = "south"
  257.     elseif nz < sz then
  258.         facing = "north"
  259.     elseif nx > sx then
  260.         facing = "east"
  261.     elseif nx < sx then
  262.         facing = "west"
  263.     end
  264.     print("Facing: " .. facing)
  265. end
  266.  
  267. function GetPos()
  268.     Hold(modemSlot)
  269.     local cx, cy, cz = gps.locate()
  270.     pos = {}
  271.     pos["x"] = cx
  272.     pos["y"] = cy
  273.     pos["z"] = cz
  274.     print("Pos: " .. tostring(cx) .. ", " .. tostring(cy) .. ", " .. tostring(cz))
  275. end
  276.  
  277. ---
  278.  
  279. if turtle.getItemCount(16) == 0 then
  280.     Hold(pickSlot)
  281.     turtle.select(16)
  282.     turtle.digUp()
  283. end
  284.  
  285. Inv()
  286.  
  287. GetPos()
  288. GetDir()
  289.  
  290. function GotoRel(rx, ry, rz)
  291.     print("Going to rel: " .. tostring(rx) .. ", " .. tostring(ry) .. ", " .. tostring(rz))
  292.     Hold(pickSlot)
  293.    
  294.     if ry >= 0 then
  295.         for i = 1, ry do
  296.             Up()
  297.         end
  298.     else
  299.         for i = -1, ry, -1 do
  300.             Down()
  301.         end
  302.     end
  303.    
  304.     if rx > 0 then
  305.         dir = Face("east")
  306.         for i = 1, rx do
  307.             Forward()
  308.         end
  309.     elseif rx < 0 then
  310.         dir = Face("west")
  311.         for i = -1, rx, -1 do
  312.             Forward()
  313.         end
  314.     end
  315.    
  316.     if rz > 0 then
  317.         dir = Face("south")
  318.         for i = 1, rz do
  319.             Forward()
  320.         end
  321.     elseif rz < 0 then
  322.         dir = Face("north")
  323.         for i = -1, rz, -1 do
  324.             Forward()
  325.         end
  326.     end
  327. end
  328.  
  329. function Goto(x, y, z)
  330.     print("Going to: " .. tostring(x) .. ", " .. tostring(y) .. ", " .. tostring(z))
  331.     GotoRel(x - pos["x"], y - pos["y"], z - pos["z"])
  332. end
  333.  
  334. function CheckSelf()
  335.     Hold(pickSlot)
  336.     if turtle.getItemCount(6) > 0 or turtle.getFuelLevel() < minFuel then
  337.         Inv()
  338.     end
  339.     turtle.select(1)
  340. end
  341.  
  342. while true do
  343.     Hold(modemSlot)
  344.     rednet.open("left")
  345.     print("Waiting for command")
  346.     hook.send("Waiting for command")
  347.     local id, msg = rednet.receive()
  348.     print("Received command: " .. tostring(msg["x"]) .. " " .. tostring(msg["y"]) .. " " .. tostring(msg["z"]))
  349.     hook.send("Received command: " .. tostring(msg["x"]) .. " " .. tostring(msg["y"]) .. " " .. tostring(msg["z"]))
  350.    
  351.     Goto(msg["x"], msg["y"], msg["z"])
  352.     turtle.select(1)
  353.     turtle.placeDown()
  354.    
  355.     print("Finished, waiting for next command")
  356.     hook.send("Finished, waiting for next command")
  357. end
  358.  
Add Comment
Please, Sign In to add comment