Advertisement
danya201272

[OPENCOMPUTERS] Nanits

Oct 31st, 2022 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.63 KB | Source Code | 0 0
  1. -- GPU Tier 2, Interner card, Wifi card, Tablet Tier 2
  2. local component = require("component")
  3. local computer = require("computer")
  4. local event = require("event")
  5. local colors = require("colors")
  6. local unicode = require("unicode")
  7. local term = require("term")
  8. local sides = require("sides")
  9. local gpu = component.gpu
  10. local modem = component.modem
  11. local lastResponse = ""
  12. a = 0
  13. term.clear()
  14. function fixedLengthRandom(digits)
  15.   digits = 10 ^ (digits - 1)
  16.   return math.random(digits, digits * 10 - 1)
  17. end
  18. ports = math.floor(fixedLengthRandom(4))
  19. modem.open(ports)
  20. modem.broadcast(ports, "nanomachines", "setResponsePort", ports)
  21. function printResponse()
  22.   w, h = component.gpu.getResolution()
  23.   gpu.fill(1, h, w, h, " ")
  24.   gpu.set(1, h, lastResponse)
  25. end
  26. function handleModemMessage(_, _, _, _, _, header, command, ...)
  27.   if header ~= "nanomachines" then return end
  28.   lastResponse = "последний ответ: " .. command
  29.   for _, v in ipairs({...}) do
  30.     lastResponse = lastResponse .. ", " .. tostring(v)
  31.   end
  32.   printResponse()
  33. end
  34. function funcButton()
  35. for i = 1, 18 do
  36. modem.broadcast(ports,"nanomachines","setInput",i,false)
  37. os.sleep(1)
  38. end
  39. end
  40. function funcButton1()
  41. if buttons.button1.buttonPressed == true then
  42. modem.broadcast(ports,"nanomachines","setInput",1,true)
  43. else
  44. modem.broadcast(ports,"nanomachines","setInput",1,false)
  45. end
  46. end
  47. function funcButton2()
  48. if buttons.button2.buttonPressed == true then
  49. modem.broadcast(ports,"nanomachines","setInput",2,true)
  50. else
  51. modem.broadcast(ports,"nanomachines","setInput",2,false)
  52. end
  53. end
  54. function funcButton3()
  55. if buttons.button3.buttonPressed == true then
  56. modem.broadcast(ports,"nanomachines","setInput",3,true)
  57. else
  58. modem.broadcast(ports,"nanomachines","setInput",3,false)
  59. end
  60. end
  61. function funcButton4()
  62. if buttons.button4.buttonPressed == true then
  63. modem.broadcast(ports,"nanomachines","setInput",4,true)
  64. else
  65. modem.broadcast(ports,"nanomachines","setInput",4,false)
  66. end
  67. end
  68. function funcButton5()
  69. if buttons.button5.buttonPressed == true then
  70. modem.broadcast(ports,"nanomachines","setInput",5,true)
  71. else
  72. modem.broadcast(ports,"nanomachines","setInput",5,false)
  73. end
  74. end
  75. function funcButton6()
  76. if buttons.button6.buttonPressed == true then
  77. modem.broadcast(ports,"nanomachines","setInput",6,true)
  78. else
  79. modem.broadcast(ports,"nanomachines","setInput",6,false)
  80. end
  81. end
  82. function funcButton7()
  83. if buttons.button7.buttonPressed == true then
  84. modem.broadcast(ports,"nanomachines","setInput",7,true)
  85. else
  86. modem.broadcast(ports,"nanomachines","setInput",7,false)
  87. end
  88. end
  89. function funcButton8()
  90. if buttons.button8.buttonPressed == true then
  91. modem.broadcast(ports,"nanomachines","setInput",8,true)
  92. else
  93. modem.broadcast(ports,"nanomachines","setInput",8,false)
  94. end
  95. end
  96. function funcButton9()
  97. if buttons.button9.buttonPressed == true then
  98. modem.broadcast(ports,"nanomachines","setInput",9,true)
  99. else
  100. modem.broadcast(ports,"nanomachines","setInput",9,false)
  101. end
  102. end
  103. function funcButton10()
  104. if buttons.button10.buttonPressed == true then
  105. modem.broadcast(ports,"nanomachines","setInput",10,true)
  106. else
  107. modem.broadcast(ports,"nanomachines","setInput",10,false)
  108. end
  109. end
  110. function funcButton11()
  111. if buttons.button11.buttonPressed == true then
  112. modem.broadcast(ports,"nanomachines","setInput",11,true)
  113. else
  114. modem.broadcast(ports,"nanomachines","setInput",11,false)
  115. end
  116. end
  117. function funcButton12()
  118. if buttons.button12.buttonPressed == true then
  119. modem.broadcast(ports,"nanomachines","setInput",12,true)
  120. else
  121. modem.broadcast(ports,"nanomachines","setInput",12,false)
  122. end
  123. end
  124. function funcButton13()
  125. if buttons.button13.buttonPressed == true then
  126. modem.broadcast(ports,"nanomachines","setInput",13,true)
  127. else
  128. modem.broadcast(ports,"nanomachines","setInput",13,false)
  129. end
  130. end
  131. function funcButton14()
  132. if buttons.button14.buttonPressed == true then
  133. modem.broadcast(ports,"nanomachines","setInput",14,true)
  134. else
  135. modem.broadcast(ports,"nanomachines","setInput",14,false)
  136. end
  137. end
  138. function funcButton15()
  139. if buttons.button15.buttonPressed == true then
  140. modem.broadcast(ports,"nanomachines","setInput",15,true)
  141. else
  142. modem.broadcast(ports,"nanomachines","setInput",15,false)
  143. end
  144. end
  145. function funcButton16()
  146. if buttons.button16.buttonPressed == true then
  147. modem.broadcast(ports,"nanomachines","setInput",16,true)
  148. else
  149. modem.broadcast(ports,"nanomachines","setInput",16,false)
  150. end
  151. end
  152. function funcButton17()
  153. if buttons.button17.buttonPressed == true then
  154. modem.broadcast(ports,"nanomachines","setInput",17,true)
  155. else
  156. modem.broadcast(ports,"nanomachines","setInput",17,false)
  157. end
  158. end
  159. function funcButton18()
  160. if buttons.button18.buttonPressed ==  true then
  161. modem.broadcast(ports,"nanomachines","setInput",18,true)
  162. else
  163. modem.broadcast(ports,"nanomachines","setInput",18,false)
  164. end
  165. end
  166. function funcButton19()
  167. a = 1
  168. end
  169. function funcButton20()
  170. modem.broadcast(ports, "nanomachines","getActiveEffects")
  171. end
  172. function funcButton21()
  173. term.clear()
  174. drawButtons()
  175. end
  176. function funcButton22()
  177. for v = 1, 18 do
  178. modem.broadcast(ports,"nanomachines","setInput",v,true)
  179. os.sleep(1)
  180. end
  181. end
  182. buttons = {button = {x=15, y=20, text="OFF", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton, height=3, cFore = 0xFFFFFF, cBack = 0x0000FF},
  183.            button1 = {x=18, y=4, text=".1", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton1, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  184.            button2 = {x=26, y=4, text=".2", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton2, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  185.            button3 = {x=34, y=4, text=".3", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton3, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  186.            button4 = {x=42, y=4, text=".4", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton4, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  187.            button5 = {x=50, y=4, text=".5", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton5, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  188.            button6 = {x=58, y=4, text=".6", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton6, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  189.            button7 = {x=10, y=10, text=".7", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton7, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  190.            button8 = {x=18, y=10, text=".8", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton8, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  191.            button9 = {x=26, y=10, text=".9", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton9, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  192.            button10 = {x=34, y=10, text="10", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton10, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  193.            button11 = {x=42, y=10, text="11", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton11, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  194.            button12 = {x=50, y=10, text="12", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton12, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  195.            button13 = {x=58, y=10, text="13", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton13, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  196.            button14 = {x=66, y=10, text="14", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton14, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  197.            button15 = {x=26, y=16, text="15", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton15, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  198.            button16 = {x=34, y=16, text="16", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton16, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  199.            button17 = {x=42, y=16, text="17", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton17, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  200.            button18 = {x=50, y=16, text="18", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton18, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
  201.            button19 = {x=24, y=20, text="EXIT", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton19, height=3, cFore = 0xFFFFFF, cBack = 0x0000FF},
  202.            button20 = {x=34, y=20, text="GRAPHI", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton20, height=3, cFore = 0xFFFFFF, cBack = 0x0000FF},
  203.            button21 = {x=46, y=20, text="UPDATE", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton21, height=3, cFore = 0xFFFFFF, cBack = 0x0000FF},
  204.            button22 = {x=58, y=20, text="ON", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton22, height=3, cFore = 0xFFFFFF, cBack = 0x0000FF}}
  205. function initButtons()
  206. for k,v in pairs(buttons) do
  207. v.width = unicode.wlen(v.text) + 2
  208. end
  209. end
  210. initButtons()
  211. function drawButtons()
  212.  
  213. for k,v in pairs(buttons) do
  214. if v.active then
  215. if not v.buttonPressed then -- если кнопка не нажата
  216. gpu.setForeground(v.cFore)
  217. gpu.setBackground(v.cBack)
  218. else                      -- в ином случае
  219. gpu.setForeground(v.cFore1)
  220. gpu.setBackground(v.cBack1)
  221. end
  222. gpu.fill(v.x, v.y, v.width, v.height, " ") -- фон для кнопки
  223. if v.height == 1 then         -- если высота кнопки равна 1
  224. gpu.set(v.x+1, v.y, v.text)
  225. elseif v.height%2 == 0 then   -- если высота кнопки равна четному числу
  226. gpu.set(v.x+1, v.y + (v.height/2 - 1), v.text)
  227. elseif v.height%2 == 1 then   -- если высота кнопки равна нечетному числу
  228. gpu.set(v.x+1, v.y + (math.ceil(v.height/2) - 1), v.text)
  229. end
  230. if v.autoSwitch == true and v.buttonPressed == true then
  231. v.buttonPressed = false
  232. os.sleep(2)
  233. drawButtons()
  234. end
  235. end
  236. end
  237. gpu.setForeground(0xFFFFFF)
  238. gpu.setBackground(0x000000)
  239. end
  240.  
  241. function searchButton()
  242.  
  243. while true do
  244. event.listen("modem_message", handleModemMessage)
  245. if a == 1 then
  246. event.ignore("modem_message", handleModemMessage)
  247. term.clear()
  248. break
  249. end
  250. local _,_,x,y = event.pull("touch")
  251. for k,v in pairs(buttons) do
  252. if x >= v.x and x < v.x + v.width+2 and y >= v.y and y < v.y + v.height and v.active then
  253. if v.switchedButton == true then
  254. if not v.autoSwitch then
  255. if v.buttonPressed == false then
  256. v.buttonPressed = true
  257. else
  258. v.buttonPressed = false
  259. end
  260. else
  261. v.buttonPressed = true
  262. end
  263. term.clear()
  264. drawButtons()
  265. end
  266. v.func()
  267. end
  268. end
  269. end
  270. end
  271. drawButtons()
  272. searchButton()
Tags: GTCE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement