Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --// Server
- local SERVER_PORT = 3230
- local PHONE_PORT = 2212
- local CLIENT_PORT = 2020
- local SLOT_COUNT = 16
- local modem = peripheral.wrap("left")
- modem.open(SERVER_PORT)
- local botsDeployed = 0
- function split (inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- function getItemIndex(itemName)
- for slot = 1, SLOT_COUNT, 1 do
- local item = turtle.getItemDetail(slot)
- if(item ~= nil) then
- if(item["name"] == itemName) then
- return slot
- end
- end
- end
- end
- function parseParams(data)
- parsed = {}
- params = split(data, " ")
- parsed[1] = vector.new(params[1], params[2], params[3])
- parsed[2] = params[4]
- parsed[3] = params[5]
- parsed[4] = params[6]
- return (parsed)
- end
- function format(a,b,c,d,e,f,g)
- local formated = ""
- formated = formated..tostring(a).." "
- formated = formated..tostring(b).." "
- formated = formated..tostring(c).." "
- formated = formated..tostring(d).." "
- formated = formated..tostring(e).." "
- formated = formated..tostring(f).." "
- formated = formated..tostring(g).." "
- return formated
- end
- function deployBot(startPos,toPos,tunnelLen,index)
- --// deployBot(p,targetPosition,length,i)
- turtle.select(getItemIndex("computercraft:turtle_expanded"))
- while (turtle.detect()) do
- os.sleep(0.3)
- end
- turtle.place()
- peripheral.call("front","turnOn")
- local newVec = vector.new(toPos.x, toPos.y, toPos.z)
- if index ~= 0 then
- if axis == "x" then
- newVec.x = newVec.x + (index*2)+1
- elseif axis == "z" then
- newVec.z = newVec.z + (index*2)+1
- elseif axis == "-x" then
- newVec.x = newVec.x - (index*2)+1
- elseif axis == "-z" then
- newVec.z = newVec.z - (index*2)+1
- end
- end
- repeat
- os.sleep(0.1)
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- until msg == "BotOnline"
- print("Sending payload")
- local payloadMessage = format(startPos.x, startPos.y, startPos.z, newVec.x, newVec.y, newVec.z, tunnelLen)
- modem.transmit(CLIENT_PORT,SERVER_PORT,payloadMessage)
- print("Bot deployed to ", newVec.x, newVec.y, newVec.z)
- repeat
- os.sleep(0.1)
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- until msg == "BotMoving"
- end
- function calculateBots(Size)
- local botsNeeded = Size/2
- return botsNeeded
- end
- function isNumberEven(n)
- if (n % 2) == 0 then
- return true
- else
- return false
- end
- end
- function returnMessageToPhone(message)
- modem.transmit(PHONE_PORT,SERVER_PORT,message)
- end
- function locateVector()
- local v = vector.new(gps.locate())
- v.x = v.x
- v.y = v.y
- return v
- end
- function calculateHeading()
- local x,y,z = 0,0,0
- local start = locateVector()
- turtle.forward()
- local current = locateVector()
- turtle.back()
- if start.x > current.x then
- heading = heading + vector.new(1,0,0)
- elseif current.x < start.x then
- heading = heading - vector.new(1,0,0)
- end
- if start.z > current.z then
- heading = heading + vector.new(0,0,1)
- elseif current.z < start.z then
- heading = heading - vector.new(0,0,1)
- end
- return heading
- end
- function recalculateVector(vec)
- return vector.new(vec.x, vec.y, vec.z)
- end
- local function ToInt(n)
- return math.floor(tonumber(n))
- end
- while (true) do
- print("Waiting for target signal...")
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- local args = split(msg," ")
- local data = parseParams(msg)
- local axis = ""
- local size = data[2]
- local length = data[4]
- if (isNumberEven(size) == false) then
- print("Size number is un-even cant send bots.")
- returnMessageToPhone("Size number is un-even cant send bots.")
- return
- end
- local targetPosition = data[1]
- targetPosition = recalculateVector(targetPosition)
- local botsNeed = calculateBots(size)
- print("Tunnel requested at:", targetPosition.x, targetPosition.y, targetPosition.z)
- print("Bots to send: "..botsNeed)
- if data[3] == 1 then
- axis = "x"
- elseif data[3] == 2 then
- axis = "z"
- elseif data[3] == -1 then
- axis = "-x"
- elseif data[3] == -2 then
- axis = "-z"
- end
- -- length = toint(length)
- for i = 0,botsNeed-1,1 do
- local p = locateVector()
- p.y = p.y + 1
- deployBot(p,targetPosition,length,i)
- os.sleep(0.5)
- end
- returnMessageToPhone(botsNeed, "bot(s) deployed to: ", targetPosition.x, targetPosition.y, targetPosition.z)
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- if message == "lastTurtle" then
- turtle.digUp()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement