zzFeVeRzz123

123

Apr 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. local event = require("event")
  2. local component = require("component")
  3. local term = require("term")
  4.  
  5. local modem = component.modem
  6. modem.open(1)
  7. modem.broadcast(1, "nanomachines", "setResponsePort", 1)
  8.  
  9. local lastResponse = ""
  10. local function printResponse()
  11. local w, h = component.gpu.getResolution()
  12. component.gpu.fill(1, h, w, h, " ")
  13. component.gpu.set(1, h, lastResponse)
  14. end
  15. local function handleModemMessage(_, _, _, _, _, header, command, ...)
  16. if header ~= "nanomachines" then return end
  17. lastResponse = "Last response: " .. command
  18. for _, v in ipairs({...}) do
  19. lastResponse = lastResponse .. ", " .. tostring(v)
  20. end
  21. printResponse()
  22. end
  23.  
  24. event.listen("modem_message", handleModemMessage)
  25.  
  26. local function send(command, ...)
  27. component.modem.broadcast(1, "nanomachines", command, ...)
  28. end
  29.  
  30. local function readNumber(name, validator)
  31. local index
  32. while not index do
  33. io.write(name..": ")
  34. index = tonumber(io.read())
  35. if not index or validator and not validator(index) then
  36. index = nil
  37. io.write("invalid input\n")
  38. end
  39. end
  40. return index
  41. end
  42.  
  43. local running = true
  44. local commands = {
  45. { "Get power state",
  46. function()
  47. send("getPowerState")
  48. end
  49. },
  50.  
  51. { "Get active effects",
  52. function()
  53. send("getActiveEffects")
  54. end
  55. },
  56. { "Get input",
  57. function()
  58. local index = readNumber("index")
  59. send("getInput", index)
  60. end
  61. },
  62. { "Set input",
  63. function()
  64. local index = readNumber("index")
  65. io.write("1. On\n")
  66. io.write("2. Off\n")
  67. local value = readNumber("state", function(x) return x == 1 or x == 2 end)
  68. send("setInput", index, value == 1)
  69. end
  70. },
  71. { "Get total input count",
  72. function()
  73. send("getTotalInputCount")
  74. end
  75. },
  76. { "Get safe active input count",
  77. function()
  78. send("getSafeActiveInputs")
  79. end
  80. },
  81. { "Get max active input count",
  82. function()
  83. send("getMaxActiveInputs")
  84. end
  85. },
  86.  
  87. { "Get health",
  88. function()
  89. send("getHealth")
  90. end
  91. },
  92. { "Get hunger",
  93. function()
  94. send("getHunger")
  95. end
  96. },
  97. { "Get age",
  98. function()
  99. send("getAge")
  100. end
  101. },
  102. { "Get name",
  103. function()
  104. send("getName")
  105. end
  106. },
  107. { "Get experience",
  108. function()
  109. send("getExperience")
  110. end
  111. },
  112.  
  113. { "Exit",
  114. function()
  115. running = false
  116. end
  117. }
  118. }
  119.  
  120. function main()
  121. while running do
  122. term.clear()
  123. for i = 1, #commands do
  124. local command = commands[i]
  125. io.write(i,". ",command[1],"\n")
  126. end
  127. printResponse()
  128.  
  129. local command = readNumber("command", function(x) return x > 0 and x <= #commands end)
  130. commands[command][2]()
  131. end
  132. end
  133.  
  134. local result, reason = pcall(main)
  135. if not result then
  136. io.stderr:write(reason, "\n")
  137. end
  138.  
  139. event.ignore("modem_message", handleModemMessage)
  140.  
  141. term.clear()
Add Comment
Please, Sign In to add comment