Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sides = {"right", "left", "top", "bottom", "front", "back" }
- REGISTER_PROTOCOL = "REGISTER"
- SERVER_PROTOCOL = "SERVER"
- GENERAL_PROTOCOL = "GENERAL"
- SERVER_CHANNEL = 5
- REGISTER_CHANNEL = 3
- SERVER_NAME = "Server_hub"
- DOOR_SERVICE_PROTOCOL = "DOOR_SERVICE"
- local gModem
- local modemSide
- local SERVER_ID
- function openModem()
- for k, side in pairs(sides) do
- if peripheral.isPresent(side) then
- if peripheral.getType(side) == "modem" then
- gModem = peripheral.wrap(side)
- modemSide = side
- rednet.open(modemSide)
- end
- end
- end
- SERVER_ID = rednet.lookup(SERVER_PROTOCOL, SERVER_NAME)
- end
- function start()
- openModem()
- run()
- end
- function run()
- local ID
- local timerID
- while true do
- rednet.broadcast("...", DOOR_SERVICE_PROTOCOL)
- timerID = os.startTimer(1)
- local event, param1, param2 = os.pullEvent()
- if event == "rednet_message" then
- ID = param1
- break
- end
- end
- os.cancelTimer(timerID)
- clear()
- print("Press enter if you wish to sync with ID: " .. ID)
- timerID = os.startTimer(5)
- while true do
- local event, param1 = os.pullEvent()
- if event == "key" and param1 == 28 then
- rednet.send(ID, "...", DOOR_SERVICE_PROTOCOL)
- os.cancelTimer(timerID)
- collectBoundaries(ID)
- break
- elseif event == "timer" and param1 == timerID then
- break
- end
- end
- end
- function collectBoundaries(ID)
- local count = 0
- local coordinates = {}
- local quit = false
- repeat
- clear()
- print("Current coordinates = ")
- for k, v in ipairs(coordinates) do
- print("["..k.."]".." x= " .. v[1] .." y= " .. v[2].. " z= ".. v[3])
- end
- print()
- print("Press [enter] to send coordinate of boundary, [backspace] to quit,")
- print(" [r] to reset all, or [d] to delete one coordinate")
- --while true do
- local event, param1 = os.pullEvent("key")
- if param1 == 28 then
- local x, y, z = gps.locate()
- if x ~= nil then
- local coor = {}
- coor[1] = math.floor(x)
- coor[2] = math.floor(y - 1) --This takes into account that the device is off the ground a little
- coor[3] = math.floor(z)
- for i = 1, #coor do
- print(coor[i])
- end
- table.insert(coordinates, coor)
- count = count + 1
- else
- print("Cannot determine GPS position!")
- sleep(1.5)
- quit = true
- break
- end
- elseif param1 == 14 then
- --'backspace'
- rednet.send(ID, "quit", DOOR_SERVICE_PROTOCOL)
- quit = true
- break
- elseif param1 == 19 then
- --'r'
- count = 0
- coordinates = {}
- elseif param1 == 32 then
- --'d'
- local index
- clear()
- print("Type the number of the coordinate you'd like deleted")
- index = tonumber(io.read())
- table.remove(coordinates, index)
- end
- --end
- until count == 4
- if quit then
- return nil
- end
- for k, v in ipairs(coordinates) do
- local msg = string.format("%d %d %d", v[1], v[2] ,v[3])
- repeat
- rednet.send(ID, msg, DOOR_SERVICE_PROTOCOL)
- local fID, fMsg = rednet.receive(DOOR_SERVICE_PROTOCOL)
- until fID == ID and fMsg == "confirm"
- end
- getHeight(ID)
- print("To finish creating the boundary, please go to the main computer")
- sleep(2)
- end
- function getHeight(ID)
- print("=========================")
- print("Enter the height you want for the boundary: ")
- print()
- local height = io.read()
- rednet.send(ID, height, DOOR_SERVICE_PROTOCOL)
- end
- function round(num, idp)
- local mult = 10 ^ (idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement