Minilex

Client

Dec 25th, 2023 (edited)
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.06 KB | None | 0 0
  1. print("Starting up client")
  2. local redSide = "left"
  3. local redProtocol = "mine"
  4. local gui = false
  5. local gx, gy, gz = gps.locate(10)
  6. gz = gz - 1
  7. local direction = "north"
  8.  
  9. --Returns true value if all requitements are met
  10. function verify()
  11.     --verifing coordinates are not empty
  12.     if gps.locate() == nil then
  13.         return false
  14.     end
  15.     return true
  16. end
  17.  
  18. --Massive functions for turning the turtle using Compass Measurements
  19. function lookNorth()
  20.     if direction ~= "north" then
  21.         if direction == "east" then
  22.             turtle.turnLeft()
  23.         elseif direction == "south" then
  24.             turtle.turnLeft()
  25.             turtle.turnLeft()
  26.         elseif direction == "west" then
  27.             turtle.turnRight()
  28.         end
  29.     end
  30.     direction = "north"
  31. end
  32.  
  33. function lookSouth()
  34.     if direction ~= "south" then
  35.         if direction == "west" then
  36.             turtle.turnLeft()
  37.         elseif direction == "north" then
  38.             turtle.turnLeft()
  39.             turtle.turnLeft()
  40.         elseif direction == "east" then
  41.             turtle.turnRight()
  42.         end
  43.     end
  44.     direction = "south"
  45. end
  46.  
  47. function lookEast()
  48.     if direction ~= "east" then
  49.         if direction == "south" then
  50.             turtle.turnLeft()
  51.         elseif direction == "west" then
  52.             turtle.turnLeft()
  53.             turtle.turnLeft()
  54.         elseif direction == "north" then
  55.             turtle.turnRight()
  56.         end
  57.     end
  58.     direction = "east"
  59. end
  60.  
  61. function lookWest()
  62.     if direction ~= "west" then
  63.         if direction == "north" then
  64.             turtle.turnLeft()
  65.         elseif direction == "east" then
  66.             turtle.turnLeft()
  67.             turtle.turnLeft()
  68.         elseif direction == "south" then
  69.             turtle.turnRight()
  70.         end
  71.     end
  72.     direction = "west"
  73. end
  74.  
  75. --Compass Functions ends
  76.  
  77. function sortInventory()
  78.     local slots = {}
  79.     for i = 1, 16, 1 do
  80.         if turtle.getItemDetail(i) ~= nil then
  81.             slots[i] = turtle.getItemDetail(i).name
  82.         end
  83.     end
  84.  
  85.     for c = 1, 16, 1 do
  86.         if slots[c] ~= nil then
  87.             for s = 1, 16, 1 do
  88.                 if slots[s] ~= nil then
  89.                     if slots[c] == slots[s] and turtle.getItemCount(c) ~= 64 and turtle.getItemCount(s) ~= 64 then
  90.                         turtle.select(s)
  91.                         turtle.transferTo(c)
  92.                         if turtle.getItemCount(s) == 0 then
  93.                             slots[s] = nil
  94.                         end
  95.                     end
  96.                 end
  97.             end
  98.         end
  99.     end
  100.  
  101.     for c = 1, 16, 1 do
  102.         if slots[c] == nil then
  103.             for s = 1, 16, 1 do
  104.                 if s > c and slots[s] ~= nil then
  105.                     turtle.select(s)
  106.                     turtle.transferTo(c)
  107.                     slots[c] = slots[s]
  108.                     slots[s] =  nil
  109.                     break
  110.                 end
  111.             end
  112.         end
  113.     end
  114. end
  115.  
  116. --Checks to see if there is any space left in the turtle and returns true if space left are false if no space is left
  117. function checkSpace()
  118.     --True is empty and false is full
  119.     local slots = {}
  120.     for i = 1, 16, 1 do
  121.         if turtle.getItemDetail(i) ~= nil then
  122.             if turtle.getItemDetail(i).name == "minecraft:cobblestone" or turtle.getItemDetail() == "minecraft:dirt" then
  123.                 turtle.select(i)
  124.                 turtle.drop()
  125.                 slots[i] = true
  126.             else
  127.                 slots[i] = false
  128.             end
  129.         else
  130.             slots[i] = true
  131.         end
  132.     end
  133.  
  134.     for _, slot in pairs(slots) do
  135.         if slot == true then
  136.             return true
  137.         end
  138.     end
  139.  
  140.     return false
  141. end
  142.  
  143. function goForward(steps)
  144.     for i = 1, steps, 1 do
  145.         while turtle.detect() == true do
  146.             turtle.dig()
  147.         end
  148.         turtle.forward()
  149.     end
  150. end
  151.  
  152. function goDown(steps)
  153.     for i = 1, steps, 1 do
  154.         if turtle.detectDown() == true then
  155.             turtle.digDown()
  156.         end
  157.         turtle.down()
  158.     end
  159. end
  160.  
  161. function goUp(steps)
  162.     for i = 1, steps, 1 do
  163.         if turtle.detectUp() == true then
  164.             turtle.digUp()
  165.         end
  166.         turtle.up()
  167.     end
  168. end
  169.  
  170. function getCoalLevel(distance)
  171.     local distanceNeeded = (distance - turtle.getFuelLevel())
  172.     if distanceNeeded > turtle.getFuelLevel() then
  173.         print(string.format("I have %f of fuel I'm requesting %f more!", turtle.getFuelLevel(), distanceNeeded))
  174.         local refuled = false
  175.         while refuled do
  176.             for i = 1, 16, 1 do
  177.                 local coalNeeded = math.ceil(distanceNeeded / (80 * turtle.getItemCount(i)))
  178.                 if turtle.getItemDetail(i) ~= nil then
  179.                     if turtle.getItemDetail(i).name == "minecraft:coal" then
  180.                         if turtle.getItemCount(i) >= coalNeeded then
  181.                             turtle.select(i)
  182.                             turtle.refuel(coalNeeded)
  183.                             refuled = true
  184.                         else
  185.                             print("There is not enough coal to continue please place the coal into the slot")
  186.                         end
  187.                     end
  188.                 end
  189.             end
  190.             print("Waiting 5 seconds")
  191.             sleep(5)
  192.         end
  193.     end
  194. end
  195.  
  196. --Directs the turtle to it's corresponding given coordinates
  197. function gotoCoord(x, y, z)
  198.     --Calculating Distance
  199.     local distance = math.abs(x - gx) + math.abs(y - gy) + math.abs(z - gz)
  200.     getCoalLevel(distance)
  201.     print("Going To (" .. x .. ", " .. y .. ", " .. z .. ")")
  202.     print("I am at (" .. gx .. ", " .. gy .. ", " .. gz .. ")")
  203.     --Turtle is always placed north
  204.  
  205.     --North is negative z
  206.     --South is positive z
  207.     --West is negative x
  208.     --East is postive x
  209.  
  210.     if gz > z then
  211.         lookNorth()
  212.     elseif gz < z then
  213.         lookSouth()
  214.     end
  215.     goForward(math.abs(z - gz))
  216.  
  217.     if gx > x then
  218.         lookWest()
  219.     else
  220.         lookEast()
  221.     end
  222.     goForward(math.abs(x - gx))
  223.  
  224.     if gy > y then
  225.         goDown(math.abs(y - gy))
  226.     else
  227.         goUp(math.abs(y - gy))
  228.     end
  229.  
  230.     lookNorth()
  231.     if checkSpace() == false then
  232.         sortInventory()
  233.         if checkSpace() == false then
  234.             print("Need to Surface!")
  235.         end
  236.     end
  237. end
  238.  
  239. function getConnection()
  240.     rednet.open("left")
  241.     if rednet.isOpen() then
  242.         while true do
  243.             rednet.broadcast(string.format("%f,%f,%f", gx, gy, gz), "mine")
  244.             local id, message = rednet.receive("mine")
  245.  
  246.             local command, index = {}, 0
  247.             for i in string.gmatch(message, "([^" .. "," .. "]+)") do
  248.                 command[index] = i
  249.                 index = index + 1
  250.             end
  251.  
  252.             --command, x, y, z
  253.             print(tonumber(command[1]), tonumber(command[2]), tonumber(command[3]))
  254.  
  255.             gotoCoord(tonumber(command[1]), tonumber(command[2]), tonumber(command[3]))
  256.             gx, gy, gz = gps.locate(100)
  257.         end
  258.     end
  259. end
  260.  
  261. getConnection()
  262.  
Add Comment
Please, Sign In to add comment