Advertisement
serafim7

управление нанитами [OpenComputers]

Aug 19th, 2016
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. --[[   opencomputers nano на русском
  2. Программа для включения эффектов нанитов.
  3.  
  4. требования: планшет или компьютер с wi-fi картой
  5. примечание: могут одновременно активны только 2 эффекта
  6.  
  7. 4 назначить вход
  8. номер входа 1-18
  9. состояние вкл(1) выкл(2)
  10.  ]]--
  11.  
  12. local event = require("event")
  13. local component = require("component")
  14. local term = require("term")
  15.  
  16. local lastResponse = ""
  17. local modem = component.modem
  18. modem.open(1)
  19. modem.broadcast(1, "nanomachines", "setResponsePort", 1)
  20.  
  21. local function printResponse()
  22.   local w, h = component.gpu.getResolution()
  23.   component.gpu.fill(1, h, w, h, " ")
  24.   component.gpu.set(1, h, lastResponse)
  25. end
  26.  
  27. local function handleModemMessage(_, _, _, _, _, header, command, ...)
  28.   if header ~= "nanomachines" then return end
  29.   lastResponse = "последний ответ: " .. command
  30.   for _, v in ipairs({...}) do
  31.     lastResponse = lastResponse .. ", " .. tostring(v)
  32.   end
  33.   printResponse()
  34. end
  35.  
  36. event.listen("modem_message", handleModemMessage)
  37.  
  38. local function send(command, ...)
  39.   component.modem.broadcast(1, "nanomachines", command, ...)
  40. end
  41.  
  42. local function readNumber(name, validator)
  43.   local index
  44.   while not index do
  45.     io.write(name..": ")
  46.     index = tonumber(io.read())
  47.     if not index or validator and not validator(index) then
  48.       index = nil
  49.       io.write("такого входа не существует\n")
  50.     end
  51.   end
  52.   return index
  53. end
  54.  
  55. local running = true
  56. local commands = {
  57.   { "питание нанитов",
  58.     function()
  59.       send("getPowerState")
  60.     end
  61.   },
  62.   { "активные эфекты",
  63.     function()
  64.       send("getActiveEffects")
  65.     end
  66.   },
  67.   { "состояние входа",
  68.     function()
  69.       local index = readNumber("номер входа")
  70.       send("getInput", index)
  71.     end
  72.   },
  73.   { "установить состояние входа",
  74.     function()
  75.       local index = readNumber("номер входа")
  76.       io.write("1. влючить                                       \n")
  77.       io.write("2. выключить\n")
  78.       local value = readNumber("состояние", function(x) return x == 1 or x == 2 end)
  79.       send("setInput", index, value == 1)
  80.     end
  81.   },
  82.   { "количество входов",
  83.     function()
  84.       send("getTotalInputCount")
  85.     end
  86.   },
  87.   { "количество безопасных входов",
  88.     function()
  89.       send("getSafeActiveInputs")
  90.     end
  91.   },
  92.   { "сколько всего активных входов",
  93.     function()
  94.       send("getMaxActiveInputs")
  95.     end
  96.   },
  97.   { "зодоровье игрока",
  98.     function()
  99.       send("getHealth")
  100.     end
  101.   },
  102.   { "голод игрока",
  103.     function()
  104.       send("getHunger")
  105.     end
  106.   },
  107.   { "возраст игрока",
  108.     function()
  109.       send("getAge")
  110.     end
  111.   },
  112.   { "имя игрока",
  113.     function()
  114.       send("getName")
  115.     end
  116.   },
  117.   { "опыт игрока",
  118.     function()
  119.       send("getExperience")
  120.     end
  121.   },
  122.   { "выход",
  123.     function()
  124.       running = false
  125.     end
  126.   }
  127. }
  128.  
  129. function main()
  130.   while running do
  131.     term.clear()
  132.     for i = 1, #commands do
  133.       local command = commands[i]
  134.       io.write(i,"  ",command[1],"\n")
  135.     end
  136.     printResponse()
  137.  
  138.     local command = readNumber("введите команду", function(x) return x > 0 and x <= #commands end)
  139.     commands[command][2]()
  140.   end
  141. end
  142.  
  143. local result, reason = pcall(main)
  144. if not result then
  145.   io.stderr:write(reason, "\n")
  146. end
  147.  
  148. event.ignore("modem_message", handleModemMessage)
  149.  
  150. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement