Advertisement
Jakey4543

servertest2

Jan 16th, 2022 (edited)
4,065
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ports = {
  2.     ["serverport"] = 8080,
  3.     ["phoneport"] = 8081,
  4.     ["deployerPort"] = 5050,
  5.     ["turtleports"] = {}
  6. }
  7.  
  8. local connectedTurtles = {}
  9.  
  10. local serverKey = math.random(10000,90000)
  11. local modem = peripheral.wrap("right")
  12. modem.open(ports.serverport)
  13.  
  14. local segmentSize = 1
  15.  
  16. function split(inputstr, sep)
  17.     if sep == nil then
  18.             sep = "%s"
  19.     end
  20.     local t={}
  21.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  22.             table.insert(t, str)
  23.     end
  24.     return t
  25. end
  26.  
  27. function parseParams(data)
  28.     coords = {}
  29.     params = split(data, " ")
  30.  
  31.     coords[1] = vector.new(params[1], params[2], params[3])
  32.     coords[2] = vector.new(params[4], params[5], params[6])
  33.  
  34.     return (coords)
  35. end
  36.  
  37. function vectorToString(vector)
  38.     local str = tostring(vector.x).." "..tostring(vector.y).." "..tostring(vector.z)
  39.  
  40.     return str
  41. end
  42.  
  43. function getPositioningTable(x, z, segmaentationSize)
  44.     local xRemainder = x % segmaentationSize
  45.     local zRemainder = z % segmaentationSize
  46.  
  47.     local xMain = x - xRemainder
  48.     local zMain = z - zRemainder
  49.  
  50.     xRemainder = (xRemainder == 0 and segmaentationSize or xRemainder)
  51.     zRemainder = (zRemainder == 0 and segmaentationSize or zRemainder)
  52.  
  53.     local positions = {}
  54.  
  55.     for zi = 0, z - 1 , segmaentationSize do
  56.         for xi = 0, x - 1, segmaentationSize do
  57.  
  58.             local dims = {xi, zi, segmaentationSize, segmaentationSize}
  59.             if(xi >= x - segmaentationSize and xi <= x - 1 ) then
  60.                 dims = {xi, zi, xRemainder, segmaentationSize}
  61.             end
  62.  
  63.             if(zi >= z - segmaentationSize and zi <= z - 1 ) then
  64.                 dims = {xi, zi, segmaentationSize, zRemainder}
  65.             end
  66.  
  67.             table.insert(positions, dims)
  68.         end
  69.     end
  70.  
  71.     return table.pack(positions, xRemainder, zRemainder)
  72. end
  73.  
  74. function segmentArea(endPos)
  75.     local x = endPos.x
  76.     local z = endPos.z
  77.     -- we dont segment height as theres litteraly no point lmao
  78.  
  79.     -- return getPositioningTable(x,z,segmentSize)
  80.  
  81.     return endPos
  82. end
  83.  
  84. local parsed1,parsed2 = nil,nil
  85.  
  86. repeat
  87.     os.sleep()
  88.     event, side, senderChannel, replyChannel, phoneargs, distance = os.pullEvent("modem_message")
  89.     parsed1 = parseParams(phoneargs)[1]
  90.     parsed2 = parseParams(phoneargs)[2]
  91. until replyChannel == ports.phoneport
  92.  
  93. print(string.format("Recieved quarry start at %d %d %d", parsed1.x,parsed1.y,parsed1.z))
  94. print(string.format("Recieved quarry size of %d %d %d", parsed2.x,parsed2.y,parsed2.z))
  95.  
  96. local turtlesToDeploy = 1
  97.  
  98. for i = 1,turtlesToDeploy do
  99.     print("Deploying turtle "..i)
  100.     modem.transmit(ports.deployerPort,ports.serverport,"DEPLOY_TURTLE")
  101.  
  102.     local event, side, senderChannel, replyChannel, msg, distance = nil,nil,nil,nil,nil,nil
  103.     repeat
  104.         event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  105.     until replyChannel ~= ports.phoneport
  106.     print("Recieved ping")
  107.     print("Reply channel: "..replyChannel)
  108.     if msg == "CLIENT_READY" then
  109.         table.insert(ports.turtleports,replyChannel)
  110.         table.insert(connectedTurtles,{["id"] = i, ["port"] = replyChannel}) -- saves turtles id and port
  111.  
  112.         local segment = segmentArea(parsed2)
  113.         print(segment)
  114.         os.sleep(0.5)
  115.         modem.transmit(replyChannel,ports.serverport,serverKey)
  116.         os.sleep(1)
  117.         modem.transmit(replyChannel,ports.serverport,vectorToString(parsed1).." "..vectorToString(segment))
  118.     end
  119. end
  120.  
  121. local str = nil
  122. for _,port in ipairs(ports.turtleports) do
  123.     str = str..port..", "
  124. end
  125.  
  126. str = string.sub(str,1,#str)
  127. modem.transmit(ports.phoneport,0,"Deployed "..turtlesToDeploy.." turtle(s) Ports: "..str)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement