Advertisement
Jakey4543

Server

Dec 24th, 2020 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. --// Server
  2.  
  3. local SERVER_PORT = 3230
  4. local PHONE_PORT = 2212
  5. local CLIENT_PORT = 2020
  6. local SLOT_COUNT = 16
  7.  
  8. local modem = peripheral.wrap("left")
  9. modem.open(SERVER_PORT)
  10.  
  11. local botsDeployed = 0
  12.  
  13. function split (inputstr, sep)
  14.     if sep == nil then
  15.             sep = "%s"
  16.     end
  17.     local t={}
  18.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  19.             table.insert(t, str)
  20.     end
  21.     return t
  22. end
  23.  
  24. function getItemIndex(itemName)
  25.     for slot = 1, SLOT_COUNT, 1 do
  26.         local item = turtle.getItemDetail(slot)
  27.         if(item ~= nil) then
  28.             if(item["name"] == itemName) then
  29.                 return slot
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. function parseParams(data)
  36.     parsed = {}
  37.     params = split(data, " ")
  38.  
  39.     parsed[1] = vector.new(params[1], params[2], params[3])
  40.     parsed[2] = params[4]
  41.     parsed[3] = params[5]
  42.     parsed[4] = params[6]
  43.  
  44.     return (parsed)
  45. end
  46.  
  47. function format(a,b,c,d,e,f,g)
  48.     local formated = ""
  49.     formated = formated..tostring(a).." "
  50.     formated = formated..tostring(b).." "
  51.     formated = formated..tostring(c).." "
  52.     formated = formated..tostring(d).." "
  53.     formated = formated..tostring(e).." "
  54.     formated = formated..tostring(f).." "
  55.     formated = formated..tostring(g).." "
  56.  
  57.     return formated
  58. end
  59.  
  60. function deployBot(startPos,toPos,tunnelLen,index)
  61.     --// deployBot(p,targetPosition,length,i)
  62.     turtle.select(getItemIndex("computercraft:turtle_expanded"))
  63.     while (turtle.detect()) do
  64.         os.sleep(0.3)
  65.     end
  66.  
  67.     turtle.place()
  68.     peripheral.call("front","turnOn")
  69.  
  70.     local newVec = vector.new(toPos.x, toPos.y, toPos.z)
  71.  
  72.     if index ~= 0 then
  73.         if axis == "x" then
  74.             newVec.x = newVec.x + (index*2)+1
  75.         elseif axis == "z" then
  76.             newVec.z = newVec.z + (index*2)+1
  77.         elseif axis == "-x" then
  78.             newVec.x = newVec.x - (index*2)+1
  79.         elseif axis == "-z" then
  80.             newVec.z = newVec.z - (index*2)+1
  81.         end
  82.     end
  83.  
  84.     repeat
  85.         os.sleep(0.1)
  86.         event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  87.     until msg == "BotOnline"
  88.  
  89.     print("Sending payload")
  90.  
  91.  
  92.     local payloadMessage = format(startPos.x, startPos.y, startPos.z, newVec.x, newVec.y, newVec.z, tunnelLen)
  93.  
  94.     modem.transmit(CLIENT_PORT,SERVER_PORT,payloadMessage)
  95.     print("Bot deployed to ", newVec.x, newVec.y, newVec.z)
  96.  
  97.     repeat
  98.         os.sleep(0.1)
  99.         event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  100.     until msg == "BotMoving"
  101. end
  102.  
  103. function calculateBots(Size)
  104.     local botsNeeded = Size/2
  105.     return botsNeeded
  106. end
  107.  
  108. function isNumberEven(n)
  109.    if (n % 2) == 0 then
  110.         return true
  111.    else
  112.         return false
  113.    end
  114. end
  115.  
  116. function returnMessageToPhone(message)
  117.     modem.transmit(PHONE_PORT,SERVER_PORT,message)
  118. end
  119.  
  120. function locateVector()
  121.     local v = vector.new(gps.locate())
  122.     v.x = v.x
  123.     v.y = v.y
  124.  
  125.     return v
  126. end
  127.  
  128. function calculateHeading()
  129.     local x,y,z = 0,0,0
  130.  
  131.     local start = locateVector()
  132.     turtle.forward()
  133.     local current = locateVector()
  134.  
  135.     turtle.back()
  136.  
  137.     if start.x > current.x then
  138.         heading = heading + vector.new(1,0,0)
  139.     elseif current.x < start.x then
  140.         heading = heading - vector.new(1,0,0)
  141.     end
  142.  
  143.     if start.z > current.z then
  144.         heading = heading + vector.new(0,0,1)
  145.     elseif current.z < start.z then
  146.         heading = heading - vector.new(0,0,1)
  147.     end
  148.     return heading
  149. end
  150.  
  151. function recalculateVector(vec)
  152.     return vector.new(vec.x, vec.y, vec.z)
  153. end
  154.  
  155. local function ToInt(n)
  156.     return math.floor(tonumber(n))
  157. end
  158.  
  159.  
  160. while (true) do
  161.     print("Waiting for target signal...")
  162.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  163.  
  164.     local args = split(msg," ")
  165.     local data = parseParams(msg)
  166.  
  167.     local axis = ""
  168.     local size = data[2]
  169.     local length = data[4]
  170.  
  171.     if (isNumberEven(size) == false) then
  172.         print("Size number is un-even cant send bots.")
  173.         returnMessageToPhone("Size number is un-even cant send bots.")
  174.         return
  175.     end
  176.    
  177.     local targetPosition = data[1]
  178.  
  179.     targetPosition = recalculateVector(targetPosition)
  180.  
  181.     local botsNeed = calculateBots(size)
  182.  
  183.     print("Tunnel requested at:", targetPosition.x, targetPosition.y, targetPosition.z)
  184.     print("Bots to send: "..botsNeed)
  185.  
  186.     if data[3] == 1 then
  187.         axis = "x"
  188.     elseif data[3] == 2 then
  189.         axis = "z"
  190.     elseif data[3] == -1 then
  191.         axis = "-x"
  192.     elseif data[3] == -2 then
  193.         axis = "-z"
  194.     end
  195.  
  196. --  length = toint(length)
  197.  
  198.     for i = 0,botsNeed-1,1 do
  199.         local p = locateVector()
  200.         p.y = p.y + 1
  201.         deployBot(p,targetPosition,length,i)
  202.  
  203.         os.sleep(0.5)
  204.     end
  205.  
  206.     returnMessageToPhone(botsNeed, "bot(s) deployed to: ", targetPosition.x, targetPosition.y, targetPosition.z)
  207.  
  208.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  209.     if message == "lastTurtle" then
  210.         turtle.digUp()
  211.     end
  212. end
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement