Advertisement
Guest User

phoneserver

a guest
Nov 26th, 2020
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.08 KB | None | 0 0
  1. --Credit: Michael Reeves--
  2. --https://www.twitch.tv/michaelreeves--
  3. --PHONE  SERVER--
  4.  
  5. local SERVER_PORT = 420
  6. local CLIENT_PORT = 0
  7. local SLOT_COUNT = 16
  8.  
  9.  
  10. local segmentation = 5
  11. if (#arg == 1) then
  12.     segmentation = tonumber(arg[1])
  13. elseif (#arg == 0) then
  14.     print(string.format("No segmentation size selected, defaulting to %d", segmentation))
  15. else
  16.     print('Too many args given...')
  17.     exit(1)
  18. end
  19.  
  20.  
  21. local modem = peripheral.wrap("left")
  22. modem.open(SERVER_PORT)
  23.  
  24. local target = vector.new()
  25. local size = vector.new()
  26. local finish = vector.new()
  27.  
  28. -- I STOLE --
  29. function split (inputstr, sep)
  30.     if sep == nil then
  31.             sep = "%s"
  32.     end
  33.     local t={}
  34.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  35.             table.insert(t, str)
  36.     end
  37.     return t
  38. end
  39.  
  40. function parseParams(data)
  41.     coords = {}
  42.     params = split(data, " ")
  43.    
  44.     coords[1] = vector.new(params[1], params[2], params[3])
  45.     coords[2] = vector.new(params[4], params[5], params[6])
  46.  
  47.     return (coords)
  48. end
  49.  
  50. function getItemIndex(itemName)
  51.     for slot = 1, SLOT_COUNT, 1 do
  52.         local item = turtle.getItemDetail(slot)
  53.         if(item ~= nil) then
  54.             if(item["name"] == itemName) then
  55.                 return slot
  56.             end
  57.         end
  58.     end
  59. end
  60.  
  61. function checkFuel()
  62.     turtle.select(1)
  63.    
  64.     if(turtle.getFuelLevel() < 50) then
  65.         print("Attempting Refuel...")
  66.         for slot = 1, SLOT_COUNT, 1 do
  67.             turtle.select(slot)
  68.             if(turtle.refuel(1)) then
  69.                 return true
  70.             end
  71.         end
  72.         return false
  73.     else
  74.         return true
  75.     end
  76. end
  77.  
  78. function deployFuelChest()
  79.     if (not checkFuel()) then
  80.         print("SERVER NEEDS FUEL...")
  81.         exit(1)
  82.     end
  83.     turtle.select(getItemIndex("enderchests:ender_chest"))
  84.     turtle.up()
  85.     turtle.place()
  86.     turtle.down()
  87. end
  88.  
  89.  
  90. function deploy(startCoords, quarySize, endCoords, options)
  91.     --Place turtle from inventory
  92.     turtle.select(getItemIndex("computercraft:turtle_normal"))
  93.     while(turtle.detect()) do
  94.         os.sleep(0.3)
  95.     end
  96.  
  97.     --Place and turn on turtle
  98.     turtle.place()
  99.     peripheral.call("front", "turnOn")
  100.    
  101.    
  102.     --Wait for client to send ping
  103.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  104.     if(msg ~= "CLIENT_DEPLOYED") then
  105.         print("No client deploy message, exitting...")
  106.         os.exit()
  107.     end
  108.  
  109.    
  110.     if(options["withStorage"]) then
  111.         --Set up ender chest
  112.         if (not checkFuel()) then
  113.             print("SERVER NEEDS FUEL...")
  114.             exit(1)
  115.         end
  116.         turtle.select(getItemIndex("enderchests:ender_chest"))
  117.         turtle.up()
  118.         turtle.place()
  119.         turtle.down()
  120.     end
  121.    
  122.     deployFuelChest()
  123.     local storageBit = options["withStorage"] and 1 or 0
  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 %d",
  129.         startCoords.x, startCoords.y, startCoords.z,
  130.         quarySize.x, quarySize.y, quarySize.z,
  131.         endCoords.x, endCoords.y, endCoords.z,
  132.         storageBit
  133.     ))
  134. end
  135.  
  136.  
  137.  
  138. -- Return array of arbitrary size for each bot placement
  139. function getPositioningTable(x, z, segmaentationSize)
  140.     local xRemainder = x % segmaentationSize
  141.     local zRemainder = z % segmaentationSize
  142.  
  143.     local xMain = x - xRemainder
  144.     local zMain = z - zRemainder
  145.  
  146.     xRemainder = (xRemainder == 0 and segmaentationSize or xRemainder)
  147.     zRemainder = (zRemainder == 0 and segmaentationSize or zRemainder)
  148.  
  149.     local positions = {}
  150.  
  151.     for zi = 0, z - 1 , segmaentationSize do
  152.         for xi = 0, x - 1, segmaentationSize do
  153.            
  154.             local dims = {xi, zi, segmaentationSize, segmaentationSize}
  155.             if(xi >= x - segmaentationSize and xi <= x - 1 ) then
  156.                 dims = {xi, zi, xRemainder, segmaentationSize}
  157.             end
  158.            
  159.             if(zi >= z - segmaentationSize and zi <= z - 1 ) then
  160.                 dims = {xi, zi, segmaentationSize, zRemainder}
  161.             end
  162.            
  163.             table.insert(positions, dims)
  164.         end
  165.     end
  166.    
  167.     return table.pack(positions, xRemainder, zRemainder)
  168. end
  169.  
  170. while (true) do
  171.     -- Wait for phone
  172.     print("Waiting for target signal...")
  173.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  174.  
  175.     -- Parse out coordinates and options
  176.     local args = split(msg, " ")
  177.     local withStorage = args[#args]
  178.     withStorage = withStorage == "1" and true or false
  179.     data = parseParams(msg)
  180.     options = {}
  181.     options["withStorage"] = True
  182.  
  183.     target = data[1]
  184.     size = data[2]
  185.  
  186.     finish = vector.new(gps.locate())
  187.     finish.y = finish.y + 1
  188.     print(string.format( "RECEIVED QUARY REQUEST AT: %d %d %d", target.x, target.y, target.z))
  189.  
  190.     tab, xDf, zDf = table.unpack(getPositioningTable(size.x, size.z, segmentation))
  191.  
  192.     print(string.format("Deploying %d bots...", #tab))
  193.     for i = 1, #tab, 1 do
  194.         xOffset, zOffset, width, height = table.unpack(tab[i])
  195.         local offsetTarget = vector.new(target.x + xOffset, target.y, target.z + zOffset)
  196.         local sclaedSize = vector.new(width, size.y, height)
  197.  
  198.         deploy(offsetTarget, sclaedSize, finish, options)
  199.         os.sleep(1)
  200.         print(string.format( "Deploying to;  %d %d %d    %d %d",  target.x + xOffset, target.y, target.z + zOffset, sclaedSize.x, sclaedSize.z))
  201.     end
  202.  
  203.     -- All bots deployed, wait for last bot finished signal
  204.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  205.     turtle.digUp()
  206.     turtle.up()
  207.     turtle.dig()
  208.     turtle.down()
  209.     turtle.turnLeft()
  210.     turtle.forward(1)
  211.     turtle.turnRight()
  212.     turtle.select(getItemIndex("enderchests:ender_chest"))
  213.     endercount = (turtle.getItemCount() - 2)
  214.     if (endercount ~= 0) then
  215.         print(string.format("Depositing %d Ender Chests.", endercount))
  216.         turtle.drop(endercount)
  217.     end
  218.    
  219.     turtle.turnRight()
  220.     turtle.forward(1)
  221.     turtle.turnLeft()
  222.    
  223.  
  224. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement