Advertisement
BigGamingGamers

phone server

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