Advertisement
Guest User

Blockchat_Indev

a guest
Mar 31st, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3.  
  4. local colorOther, colorReceive, colorMe
  5. colorOther = colors.orange
  6. colorReceive = colors.brown
  7. colorMe = colors.lightBlue
  8.  
  9. local debugMode = false
  10.  
  11. print("Run in debug mode? (y/n)")
  12. while true do
  13.   local event, key = os.pullEvent("key")
  14.  
  15.   if key == keys.y then
  16.     debugMode = true
  17.     break
  18.   elseif key == keys.n then
  19.     debugMode = false
  20.     break
  21.   end
  22. end  
  23.  
  24. term.clear()
  25. term.setCursorPos(1,1)
  26.  
  27. term.setTextColor(colors.blue)
  28.  
  29. print("Starting program.\n")
  30.  
  31. local driveSide
  32.  
  33. -- check for modem
  34. for _, side in ipairs(rs.getSides()) do
  35.   if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  36.     driveSide = side
  37.     break
  38.   end
  39. end
  40.  
  41. local modem
  42.  
  43. -- wrap and open modem or send error message
  44. if driveSide then
  45.   print("Modem found, wrapping and opening.")
  46.   modem = peripheral.wrap(driveSide)
  47.   rednet.open(driveSide)
  48.   term.setTextColor(colors.yellow)
  49.   print("\nWelcome to Blockchat!")
  50.   print("Your id is: " .. os.computerID())
  51. else
  52.   error("No modem found, please attach modem and reboot program.")
  53. end
  54.  
  55. -- message confirmation variables
  56. local sentConfirmation = 0
  57. local justSentMessage = 0
  58.  
  59. -- deal with all incoming messages
  60. local function receive()
  61.  
  62.   local id,message,protocol = rednet.receive()
  63.  
  64.   -- happens when receives message confirmation
  65.   if (justSentMessage == 1 and message == 1) then
  66.     print("Message confirmation received.")
  67.     justSentMessage = 0
  68.     return
  69.   end
  70.  
  71.   -- resets variable
  72.   sentConfirmation = 0
  73.  
  74.   term.setTextColor(colorReceive)
  75.        
  76.   print("Message received.")
  77.      
  78.   print("Sender Id: " .. id)
  79.  
  80.   if (message ~= null) then
  81.     term.setTextColor(colorOther)
  82.     print("Msg: " .. message)
  83.   end
  84.  
  85.   if (debugMode) then
  86.     term.setTextColor(colorReceive)
  87.     if (protocol ~= null) then
  88.       print("Protocol: " .. protocol)
  89.     else
  90.       print("Protocol: null")
  91.     end
  92.   end
  93.  
  94.   -- send confirmation response
  95.   if (sentConfirmation == 0) then
  96.     rednet.send(id,1)
  97.     sentConfirmation = 1
  98.   end
  99.  
  100.   -- resets variable
  101.   justSentMessage = 0
  102.  
  103. end
  104.  
  105. -- send message
  106. local function send()
  107.   term.setTextColor(colorMe)
  108.   --if (justSentMessage == 0) then
  109.     print("\nType target ID: ")
  110.   --end
  111.   os.pullEvent("char")
  112.   local targetId = tonumber(read())
  113.   print("Type message:")
  114.   term.setTextColor(colorMe)
  115.   rednet.send(targetId,read())
  116.   print("Sending message...")
  117.   justSentMessage = 1
  118. end
  119.  
  120. -- wait for Events
  121. while true do
  122.   parallel.waitForAny(receive, send)
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement