OreSeur-

Server Code

Jan 19th, 2021 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.61 KB | None | 0 0
  1. local SERVER_PORT = 568
  2. local CLIENT_PORT = 342
  3.  
  4. local TURTLE_SLOT = 1
  5. local INVENTORY_SLOT = 2
  6. local FUEL_SLOT = 3
  7. local TURTLE_SUPPLY = 4
  8. local INVENTORY_SUPPLY = 5
  9. local FUEL_SUPPLY = 6
  10.  
  11. --The number of slots in the turtle
  12. local SLOT_COUNT = 16
  13.  
  14. --find the size of our segmentation
  15. --use given command line arugment or defualt to 8
  16. local segmentation = 8
  17. if (#arg == 1) then
  18.     segmentation = tonumber(arg[1])
  19. elseif (#arg == 0) then
  20.     print(string.format("Using default segmentation size of %d", segmentation))
  21. else
  22.     print('Too many args given...')
  23.     exit(1)
  24. end
  25.  
  26. --connect and open the modem on the turtle
  27. local modem = peripheral.wrap("left")
  28. modem.open(SERVER_PORT)
  29.  
  30. --these are the vectors for mining dimensions
  31. local target = vector.new()
  32. local size = vector.new()
  33. local finish = vector.new()
  34.  
  35. --self-explanatory
  36. --This may need more work?
  37. --We can assume there is at leat one chest in the slot
  38. function deployInventoryChest()
  39.     --give the turtle a Inventory chest
  40.     turtle.drop(INVENTORY_SLOT)
  41.     --make sure the slot for inventory chests
  42.     --isn't empty or full
  43.     if turtle.getItemCount(INVENTORY_SLOT) == 0 then
  44.         turtle.select(INVENTORY_SUPPLY)
  45.         turtle.placeUp()
  46.         turtle.suckUp()
  47.         turtle.digUp()
  48.     elseif turtle.getItemCount(INVENTORY_SLOT) == 64 then
  49.         turtle.select(INVENTORY_SLOT)
  50.         turtle.placeUp()
  51.         turtle.dropUp(32)
  52.         turtle.digUp()
  53.     end
  54. end
  55.  
  56. --self-explanatory
  57. --This may need more work?
  58. --We can assume there is at leat one chest in the slot
  59. function deployFuelChest()
  60.     turtle.drop(FUEL_SLOT)
  61.     --make sure the slot for fuel chests
  62.     --isn't empty or full
  63.     if turtle.getItemCount(FUEL_SLOT) == 0 then
  64.         turtle.select(FUEL_SUPPLY)
  65.         turtle.placeUp()
  66.         turtle.suckUp()
  67.         turtle.digUp()
  68.     elseif turtle.getItemCount(INVENTORY_SLOT) == 64 then
  69.         turtle.select(INVENTORY_SLOT)
  70.         turtle.placeUp()
  71.         turtle.select(FUEL_SLOT)
  72.         turtle.dropUp(32)
  73.         turtle.digUp()
  74.     end
  75. end
  76.  
  77. --split a string message into a table of strings
  78. --inputstr is the sting to be separated
  79. --sep is the string that will be found between sections
  80. function split (inputstr, sep)
  81.     if sep == nil then
  82.             sep = "%s"
  83.     end
  84.     local t={}
  85.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  86.             table.insert(t, str)
  87.     end
  88.     return t
  89. end
  90.  
  91. --the data is the
  92. --put the string data into the coords array
  93. function parseParams(data)
  94.     coords = {}
  95.     params = split(data, " ")
  96.  
  97.     coords[1] = vector.new(params[1], params[2], params[3])
  98.     coords[2] = vector.new(params[4], params[5], params[6])
  99.  
  100.     return (coords)
  101. end
  102.  
  103. --deploy a turtle
  104. function deploy(startCoords, quarySize, endCoords)
  105.     --Place turtle from inventory
  106.     turtle.select(1)
  107.     while(turtle.detect()) do
  108.         os.sleep(1)
  109.     end
  110.    
  111.     --Place and turn on turtle
  112.     turtle.place()
  113.     peripheral.call("front", "turnOn")
  114.  
  115.     --Wait for client to send ping
  116.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  117.     if(msg ~= "CLIENT_DEPLOYED") then
  118.         print("No client deploy message, exitting...")
  119.         os.exit()
  120.     end
  121.    
  122.     deployInventoryChest()
  123.     deployFuelChest()
  124.    
  125.     -- Client is deployed
  126.     modem.transmit(CLIENT_PORT,
  127.         SERVER_PORT,
  128.         string.format("%d %d %d %d %d %d %d %d %d",
  129.         startCoords.x, startCoords.y, startCoords.z,
  130.         quarySize.x, quarySize.y, quarySize.z,
  131.         endCoords.x, endCoords.y, endCoords.z
  132.     ))
  133. end
  134.  
  135. while (true) do
  136.     -- Wait for phone
  137.     print("Waiting for target signal...")
  138.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  139.  
  140.     --get the target and size
  141.     data = parseParams(msg)
  142.     target = data[1]
  143.     size = data[2]
  144.  
  145.     --finish right above the server
  146.     finish = vector.new(gps.locate())
  147.     finish.y = finish.y + 1
  148.    
  149.     --print the server quarry request
  150.     print(string.format( "RECEIVED QUARY REQUEST AT: %d %d %d", target.x, target.y, target.z))
  151.    
  152.     --deploy turtles
  153.    
  154.     for i = 1, size.x, segmentation do
  155.         --x dig length is the smaller of
  156.         --segmentation and size.x - i
  157.         --This will allow variable sizes of mining areas on the edges
  158.         --This means segmentation won't have to evenly divide size.x
  159.         local xLen = segmentation
  160.         if ((size.x - i) < xLen) then
  161.             xLen = (size.x - i)
  162.         end
  163.        
  164.         for j = 1, size.z, segmentation do
  165.             --z dig length is the smaller of
  166.             --segmentation and size.z - j
  167.             --This will allow variable sizes of mining areas on the edges
  168.             --This means segmentation won't have to evenly divide size.z
  169.             local zLen = segmentation
  170.             if ((size.z - j) < zLen) then
  171.                 zLen = (size.z - j)
  172.             end
  173.            
  174.             --start cords of the dig are i, j
  175.             --size of dig is xLen by zLen
  176.             local scaledSize = vector.new(xLen, size.y, zLen)
  177.             local offsetTarget = vector.new(target.x + i, target.y, target.z + j)
  178.            
  179.             deploy(offsetTarget, scaledSize, finish)
  180.             print(string.format( "Deploying to;  %d %d %d    %d %d",  target.x + i, target.y, target.z + j, xLen, zLen))
  181.             os.sleep(2)
  182.            
  183.         end
  184.     end
  185.  
  186.     -- All bots deployed, wait for last bot finished signal
  187.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  188.     turtle.digUp()
  189. end
  190.  
Add Comment
Please, Sign In to add comment