Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- term.setCursorPos(1,1)
- local colorOther, colorReceive, colorMe
- colorOther = colors.orange
- colorReceive = colors.brown
- colorMe = colors.lightBlue
- local debugMode = false
- print("Run in debug mode? (y/n)")
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.y then
- debugMode = true
- break
- elseif key == keys.n then
- debugMode = false
- break
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.blue)
- print("Starting program.\n")
- local driveSide
- -- check for modem
- for _, side in ipairs(rs.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- driveSide = side
- break
- end
- end
- local modem
- -- wrap and open modem or send error message
- if driveSide then
- print("Modem found, wrapping and opening.")
- modem = peripheral.wrap(driveSide)
- rednet.open(driveSide)
- term.setTextColor(colors.yellow)
- print("\nWelcome to Blockchat!")
- print("Your id is: " .. os.computerID())
- else
- error("No modem found, please attach modem and reboot program.")
- end
- -- message confirmation variables
- local sentConfirmation = 0
- local justSentMessage = 0
- -- deal with all incoming messages
- local function receive()
- local id,message,protocol = rednet.receive()
- -- happens when receives message confirmation
- if (justSentMessage == 1 and message == 1) then
- print("Message confirmation received.")
- justSentMessage = 0
- return
- end
- -- resets variable
- sentConfirmation = 0
- term.setTextColor(colorReceive)
- print("Message received.")
- print("Sender Id: " .. id)
- if (message ~= null) then
- term.setTextColor(colorOther)
- print("Msg: " .. message)
- end
- if (debugMode) then
- term.setTextColor(colorReceive)
- if (protocol ~= null) then
- print("Protocol: " .. protocol)
- else
- print("Protocol: null")
- end
- end
- -- send confirmation response
- if (sentConfirmation == 0) then
- rednet.send(id,1)
- sentConfirmation = 1
- end
- -- resets variable
- justSentMessage = 0
- end
- -- send message
- local function send()
- term.setTextColor(colorMe)
- --if (justSentMessage == 0) then
- print("\nType target ID: ")
- --end
- os.pullEvent("char")
- local targetId = tonumber(read())
- print("Type message:")
- term.setTextColor(colorMe)
- rednet.send(targetId,read())
- print("Sending message...")
- justSentMessage = 1
- end
- -- wait for Events
- while true do
- parallel.waitForAny(receive, send)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement