BillBodkin

Mine with advanced peripherals

Sep 13th, 2021 (edited)
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.04 KB | None | 0 0
  1. -- pastebin get 4SKNxjPs
  2.  
  3. local pickSlot  = 13
  4. local modemSlot = 14
  5. local geoSlot   = 15
  6. local chestSlot = 16
  7.  
  8. local pickName  = "minecraft:diamond_pickaxe"
  9. local modemName = "computercraft:wireless_modem_advanced"
  10. local geoName   = "advancedperipherals:geo_scanner"
  11.  
  12. local minFuel = turtle.getFuelLimit() * 0.25
  13. local maxFuel = turtle.getFuelLimit() * 0.75
  14.  
  15. local distanceWeight = -2
  16. local blockScoreWeight = 1
  17.  
  18. local controllerID = 6
  19.  
  20. local maxY = 9
  21. local minY = 8
  22.  
  23. local blockScores = {
  24.     ["minecraft:diamond_ore"] = 10,
  25.     ["minecraft:gold_ore"] = 8,
  26.     ["minecraft:iron_ore"] = 5,
  27.     ["minecraft:coal_ore"] = 7,
  28.     ["minecraft:redstone_ore"] = 2,
  29.     ["minecraft:lapis_ore"] = 1,
  30.     ["minecraft:emerald_ore"] = 11,
  31.     ["minecraft:oak_log"] = 9,
  32.     ["minecraft:dark_oak_log"] = 9,
  33.     ["minecraft:birch_log"] = 9,
  34.     ["minecraft:jungle_log"] = 9,
  35.     ["minecraft:acaia_log"] = 9,
  36.     ["minecraft:sand"] = 3
  37. }
  38.  
  39. local validFuels = {
  40.     ["minecraft:coal_block"] = true,
  41.     ["minecraft:coal"] = true,
  42.     ["minecraft:charcoal"] = true,
  43.     ["minecraft:blaze_rod"] = true
  44. }
  45.  
  46. sleep(1)--might need to increase to prevent server crash
  47.  
  48. function Hold(s)
  49.     if turtle.getItemCount(s) == 1 then
  50.         turtle.select(s)
  51.         turtle.equipLeft()
  52.         local i = turtle.getItemDetail()
  53.         if i ~= nil then
  54.             if i.name == pickName then
  55.                 turtle.transferTo(pickSlot)
  56.             elseif i.name == geoName then
  57.                 turtle.transferTo(geoSlot)
  58.             elseif i.name == modemName then
  59.                 turtle.transferTo(modemSlot)
  60.             else
  61.                 turtle.transferTo(1)
  62.             end
  63.         end
  64.         turtle.select(1)
  65.     end
  66. end
  67.  
  68. function Network()
  69.     Hold(modemSlot)
  70.     local x, y, z = gps.locate()
  71.     while x == nil do
  72.         print("waiting for GPS")
  73.         sleep(3)
  74.         x, y, z = gps.locate()
  75.     end
  76.     rednet.open("left")
  77.    
  78.     function SendUpdate()
  79.         rednet.send(controllerID, {
  80.             ["timestamp"] = os.time(),
  81.             ["id"] = os.getComputerID(),
  82.             ["x"] = x,
  83.             ["y"] = y,
  84.             ["z"] = z,
  85.             ["fuel"] = turtle.getFuelLevel()
  86.         })
  87.     end
  88.    
  89.     SendUpdate()
  90.    
  91.     local id, message = nil
  92.     while true do
  93.         print("Waiting for message from controller")
  94.         id, message = rednet.receive(nil, 10)
  95.         if id ~= controllerID then--if nothing send another update
  96.             SendUpdate()
  97.         else
  98.             break
  99.         end
  100.         if message ~= nil and type(message) == "table" then
  101.             --could be a broadcast for new controller
  102.             if message["controllerID"] ~= nil and message["newControllerPassword"] == "dododo" then
  103.                 controllerID = message["controllerID"]
  104.             end
  105.         end
  106.     end
  107.    
  108.     print("Message from controller received")
  109.    
  110.     if message["distanceWeight"] ~= nil then
  111.         distanceWeight = message["distanceWeight"]
  112.     end
  113.    
  114.     if message["blockScoreWeight"] ~= nil then
  115.         blockScoreWeight = message["blockScoreWeight"]
  116.     end
  117.    
  118.     if message["controllerID"] ~= nil then
  119.         controllerID = message["controllerID"]
  120.     end
  121.    
  122.     if message["blockScores"] ~= nil then
  123.         blockScores = message["blockScores"]
  124.     end
  125.    
  126.     if message["maxY"] ~= nil then
  127.         maxY = message["maxY"]
  128.     end
  129.    
  130.     if message["minY"] ~= nil then
  131.         minY = message["minY"]
  132.     end
  133.    
  134.     rednet.close("left")
  135.    
  136.     Hold(pickSlot)
  137.    
  138.     if y > maxY then
  139.         for i = y-1, maxY, -1 do
  140.             Down()
  141.         end
  142.     end
  143.    
  144.     if y < minY then
  145.         for i = y+1, minY do
  146.             Up()
  147.         end
  148.     end
  149. end
  150.  
  151. function Scan()
  152.     Hold(geoSlot)
  153.     local geo = peripheral.find("geoScanner")
  154.     local s, r = nil
  155.     while s == nil do
  156.         s, r = geo.scan(8)
  157.         if r ~= nil then
  158.             print(r)
  159.         end
  160.         sleep(2)
  161.     end
  162.     --print(textutils.serialize(s))
  163.     return s
  164. end
  165.  
  166. function ScoreScan(s, d)
  167.     local hsi, hsv, hsx, hsy, hsz = nil
  168.     for k, v in pairs(s) do
  169.         if blockScores[v.name] ~= nil then
  170.             local score = ((math.abs(v.x) + math.abs(v.y) + math.abs(v.z)) * distanceWeight) + (blockScores[v.name] * blockScoreWeight)
  171.            
  172.             if v.x == 0 and v.z == 0 then
  173.                 score = score * 0.7
  174.             end
  175.            
  176.             --prefer routes with no turning
  177.             if d == "north" and v.x == 0 and v.z <= 0 then
  178.                 score = score * 0.6
  179.             end
  180.            
  181.             if d == "south" and v.x == 0 and v.z >= 0 then
  182.                 score = score * 0.6
  183.             end
  184.            
  185.             if d == "east" and v.x >= 0 and v.z == 0 then
  186.                 score = score * 0.6
  187.             end
  188.            
  189.             if d == "west" and v.x <= 0 and v.z == 0 then
  190.                 score = score * 0.6
  191.             end
  192.            
  193.             if hsi == nil or score > hsv then
  194.                 hsi = k
  195.                 hsv = score
  196.                 hsx = v.x
  197.                 hsy = v.y
  198.                 hsz = v.z
  199.             end
  200.         end
  201.     end
  202.     return hsi, hsv, hsx, hsy, hsz
  203. end
  204.  
  205. function Forward()
  206.     while turtle.forward() == false do
  207.         turtle.dig()
  208.     end
  209. end
  210.  
  211. function Up()
  212.     while turtle.up() == false do
  213.         turtle.digUp()
  214.     end
  215. end
  216.  
  217. function Down()
  218.     while turtle.down() == false do
  219.         turtle.digDown()
  220.     end
  221. end
  222.  
  223. function Dig()
  224.     while turtle.detect() do
  225.         turtle.dig()
  226.     end
  227. end
  228.  
  229. function DigUp()
  230.     while turtle.detectUp() do
  231.         turtle.digUp()
  232.     end
  233. end
  234.  
  235. function DigDown()
  236.     while turtle.detectDown() do
  237.         turtle.digDown()
  238.     end
  239. end
  240.  
  241. local dirs = {
  242.     [1] = "north",
  243.     [2] = "east",
  244.     [3] = "south",
  245.     [4] = "west"
  246. }
  247.  
  248. function GetIndex(v, t)
  249.     for tk, tv in pairs(t) do
  250.         if tv == v then
  251.             return tk
  252.         end
  253.     end
  254. end
  255.  
  256. function Left(d)
  257.     turtle.turnLeft()
  258.    
  259.     local cd = GetIndex(d, dirs)
  260.    
  261.     nd = cd - 1
  262.     if nd == 0 then
  263.         nd = 4
  264.     end
  265.    
  266.     print("Facing " .. dirs[nd])
  267.     return dirs[nd]
  268. end
  269.  
  270. function Right(d)
  271.     turtle.turnRight()
  272.    
  273.     local cd = GetIndex(d, dirs)
  274.    
  275.     nd = cd + 1
  276.     if nd == 5 then
  277.         nd = 1
  278.     end
  279.    
  280.     print("Facing " .. dirs[nd])
  281.     return dirs[nd]
  282. end
  283.  
  284. function Face(d, nd)
  285.     local cd = GetIndex(d, dirs)
  286.     local td = GetIndex(nd, dirs)
  287.    
  288.     if cd == td then
  289.         print("Already facing " .. nd)
  290.         return nd
  291.     end
  292.    
  293.     if cd == 1 and td == 4 then
  294.         turtle.turnLeft()
  295.         print("Facing " .. nd)
  296.         return nd
  297.     end
  298.    
  299.     if cd == 4 and td == 1 then
  300.         turtle.turnRight()
  301.         print("Facing " .. nd)
  302.         return nd
  303.     end
  304.    
  305.     if cd < td then
  306.         for i = cd, td - 1 do
  307.             turtle.turnRight()
  308.         end
  309.     end
  310.    
  311.     if cd > td then
  312.         for i = cd, td + 1, -1 do
  313.             turtle.turnLeft()
  314.         end
  315.     end
  316.    
  317.     print("Facing " .. nd)
  318.     return nd
  319. end
  320.  
  321. function Inv()
  322.     Hold(pickSlot)
  323.     DigUp()
  324.     turtle.select(16)
  325.     while turtle.placeUp() == false do
  326.         DigUp()
  327.         turtle.attackUp()
  328.     end
  329.     local c = peripheral.wrap("top")
  330.    
  331.     while turtle.getItemCount(6) > 0 or turtle.getItemCount(1) > 0 do
  332.         for i = 1, 12 do
  333.             turtle.select(i)
  334.             turtle.dropUp(64)
  335.         end
  336.     end
  337.    
  338.     if turtle.getFuelLevel() < minFuel then
  339.         local fuelAttempts = 0
  340.         turtle.select(1)
  341.         while turtle.getFuelLevel() < maxFuel do
  342.             local cl = c.list()
  343.             print(cl[1].name)
  344.             if cl[1] ~= nil and validFuels[cl[1].name] then
  345.                 turtle.suckUp(cl[1].count - 1)
  346.                 turtle.refuel(64)
  347.             else
  348.                 print("Waiting for fuel")
  349.             end
  350.             sleep(1)
  351.             fuelAttempts = fuelAttempts + 1
  352.             if fuelAttempts > 10 and turtle.getFuelLevel() > minFuel then
  353.                 print("Will try get more fule later")
  354.                 break
  355.             end
  356.         end
  357.     end
  358.    
  359.     turtle.select(16)
  360.     Hold(pickSlot)
  361.     turtle.digUp()
  362.     turtle.select(1)
  363. end
  364.  
  365. function GetDir()
  366.     Hold(modemSlot)
  367.     local sx, sy, sz = gps.locate()
  368.     while sx == nil do
  369.         print("Coudn't get GPS")
  370.         sleep(3)
  371.         sx, sy, sz = gps.locate()
  372.     end
  373.     Hold(pickSlot)
  374.     Forward()
  375.     Hold(modemSlot)
  376.     local nx, ny, nz = gps.locate()
  377.     while nx == nil do
  378.         print("Coudn't get GPS")
  379.         sleep(3)
  380.         nx, ny, nz = gps.locate()
  381.     end
  382.    
  383.     turtle.turnLeft()
  384.     turtle.turnLeft()
  385.    
  386.     Hold(pickSlot)
  387.     Forward()
  388.    
  389.     turtle.turnLeft()
  390.     turtle.turnLeft()
  391.    
  392.     if nz > sz then
  393.         return "south"
  394.     end
  395.     if nz < sz then
  396.         return "north"
  397.     end
  398.     if nx > sx then
  399.         return "east"
  400.     end
  401.     if nx < sx then
  402.         return "west"
  403.     end
  404. end
  405.  
  406. ---
  407.  
  408. if turtle.getItemCount(16) == 0 then
  409.     Hold(pickSlot)
  410.     turtle.select(16)
  411.     turtle.digUp()
  412. end
  413.  
  414. Inv()
  415.  
  416. local dir = GetDir()
  417. print("facing " .. dir)
  418.  
  419. function GotoRel(rx, ry, rz)
  420.     if ry >= 0 then
  421.         for i = 1, ry do
  422.             Up()
  423.         end
  424.     else
  425.         for i = -1, ry, -1 do
  426.             Down()
  427.         end
  428.     end
  429.    
  430.     if rx > 0 then
  431.         dir = Face(dir, "east")
  432.         for i = 1, rx do
  433.             Forward()
  434.         end
  435.     elseif rx < 0 then
  436.         dir = Face(dir, "west")
  437.         for i = -1, rx, -1 do
  438.             Forward()
  439.         end
  440.     end
  441.    
  442.     if rz > 0 then
  443.         dir = Face(dir, "south")
  444.         for i = 1, rz do
  445.             Forward()
  446.         end
  447.     elseif rz < 0 then
  448.         dir = Face(dir, "north")
  449.         for i = -1, rz, -1 do
  450.             Forward()
  451.         end
  452.     end
  453. end
  454.  
  455. function Cycle()
  456.     local scanned = Scan(8)
  457.     Hold(pickSlot)
  458.     for noScanTimes = 1, 20 do
  459.         local hsi, hsv, hsx, hsy, hsz = ScoreScan(scanned)
  460.        
  461.         if turtle.getItemCount(6) > 0 or turtle.getFuelLevel() < minFuel then
  462.             Inv()
  463.         end
  464.        
  465.         turtle.select(1)
  466.        
  467.         -- if nothing found, move forward
  468.         if hsi == nil then
  469.             print("Nothing found")
  470.             for i = 1, 8 do
  471.                 Forward()
  472.             end
  473.             return
  474.         else
  475.             print(hsi, hsv, hsx, hsy, hsz)
  476.             GotoRel(hsx, hsy, hsz)
  477.             --offset scanned by moved
  478.             for k, v in pairs(scanned) do
  479.                 scanned[k].x = v.x - hsx
  480.                 scanned[k].y = v.y - hsy
  481.                 scanned[k].z = v.z - hsz
  482.                 if scanned[k].x == 0 and scanned[k].y == 0 and scanned[k].z == 0 then
  483.                     scanned[k] = nil
  484.                 end
  485.             end
  486.         end
  487.     end
  488. end
  489.  
  490. while true do
  491.     Network()
  492.     for c = 1, 2 do
  493.         print("c: " .. tostring(c))
  494.         Cycle()
  495.         sleep(0)
  496.     end
  497. end
Add Comment
Please, Sign In to add comment