Maengorn

phone server (front load disk)

Dec 23rd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 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("right")
  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. end
  82.  
  83.  
  84. function deploy(startCoords, quarySize, endCoords, options)
  85. --Place turtle from inventory
  86. turtle.select(getItemIndex("computercraft:turtle_advanced"))
  87. while(turtle.detect()) do
  88. os.sleep(0.3)
  89. end
  90.  
  91. --Place and turn on turtle
  92. turtle.place()
  93. peripheral.call("front", "turnOn")
  94.  
  95.  
  96. --Wait for client to send ping
  97. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  98. if(msg ~= "CLIENT_DEPLOYED") then
  99. print("No client deploy message, exitting...")
  100. os.exit()
  101. end
  102.  
  103.  
  104. if(options["withStorage"]) then
  105. --Set up ender chest
  106. if (not checkFuel()) then
  107. print("SERVER NEEDS FUEL...")
  108. exit(1)
  109. end
  110. end
  111.  
  112. deployFuelChest()
  113. local storageBit = options["withStorage"] and 1 or 0
  114.  
  115. -- Client is deployed
  116. modem.transmit(CLIENT_PORT,
  117. SERVER_PORT,
  118. string.format("%d %d %d %d %d %d %d %d %d %d",
  119. startCoords.x, startCoords.y, startCoords.z,
  120. quarySize.x, quarySize.y, quarySize.z,
  121. endCoords.x, endCoords.y, endCoords.z,
  122. storageBit
  123. ))
  124. end
  125.  
  126.  
  127.  
  128. -- Return array of arbitrary size for each bot placement
  129. function getPositioningTable(x, z, segmaentationSize)
  130. local xRemainder = x % segmaentationSize
  131. local zRemainder = z % segmaentationSize
  132.  
  133. local xMain = x - xRemainder
  134. local zMain = z - zRemainder
  135.  
  136. xRemainder = (xRemainder == 0 and segmaentationSize or xRemainder)
  137. zRemainder = (zRemainder == 0 and segmaentationSize or zRemainder)
  138.  
  139. local positions = {}
  140.  
  141. for zi = 0, z - 1 , segmaentationSize do
  142. for xi = 0, x - 1, segmaentationSize do
  143.  
  144. local dims = {xi, zi, segmaentationSize, segmaentationSize}
  145. if(xi >= x - segmaentationSize and xi <= x - 1 ) then
  146. dims = {xi, zi, xRemainder, segmaentationSize}
  147. end
  148.  
  149. if(zi >= z - segmaentationSize and zi <= z - 1 ) then
  150. dims = {xi, zi, segmaentationSize, zRemainder}
  151. end
  152.  
  153. table.insert(positions, dims)
  154. end
  155. end
  156.  
  157. return table.pack(positions, xRemainder, zRemainder)
  158. end
  159.  
  160. while (true) do
  161. -- Wait for phone
  162. print("Waiting for target signal...")
  163. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  164.  
  165. -- Parse out coordinates and options
  166. local args = split(msg, " ")
  167. local withStorage = args[#args]
  168. withStorage = withStorage == "1" and true or false
  169. data = parseParams(msg)
  170. options = {}
  171. options["withStorage"] = True
  172.  
  173. target = data[1]
  174. size = data[2]
  175.  
  176. finish = vector.new(gps.locate())
  177. finish.y = finish.y + 1
  178. print(string.format( "RECEIVED QUARY REQUEST AT: %d %d %d", target.x, target.y, target.z))
  179.  
  180. tab, xDf, zDf = table.unpack(getPositioningTable(size.x, size.z, segmentation))
  181.  
  182. print(string.format("Deploying %d bots...", #tab))
  183. for i = 1, #tab, 1 do
  184. xOffset, zOffset, width, height = table.unpack(tab[i])
  185. local offsetTarget = vector.new(target.x + xOffset, target.y, target.z + zOffset)
  186. local sclaedSize = vector.new(width, size.y, height)
  187.  
  188. deploy(offsetTarget, sclaedSize, finish, options)
  189. os.sleep(1)
  190. print(string.format( "Deploying to; %d %d %d %d %d", target.x + xOffset, target.y, target.z + zOffset, sclaedSize.x, sclaedSize.z))
  191. end
  192.  
  193. -- All bots deployed, wait for last bot finished signal
  194. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  195. turtle.digUp()
  196. turtle.down()
  197. turtle.down()
  198. turtle.select(getItemIndex("enderstorage:ender_storage"))
  199. endercount = (turtle.getItemCount())
  200. if (endercount ~= 0) then
  201. print(string.format("Depositing %d Ender Chests.", endercount))
  202. turtle.drop(endercount)
  203. end
  204.  
  205. turtle.up()
  206. turtle.up()
  207.  
  208.  
  209. end
  210.  
  211.  
Advertisement
Add Comment
Please, Sign In to add comment