Advertisement
Fire_Ball_20001

table_arena

Jul 12th, 2020 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.67 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local modem = component.modem
  4. local event = require("event")
  5. local thread = require("thread")
  6. local term = require("term")
  7. local computer = require("computer")
  8. local fs = require("filesystem")
  9.  
  10. -- Переменные
  11. local size = 4
  12. local x, y
  13. local control_port = 1245
  14. local threads = {}
  15. local foreground = 0xffffff
  16. local background = 0xaaaa00
  17. local table_monitors = {}
  18. local main_monitor
  19. local otvet_port = 2356
  20. local table_scan
  21. local cool_mobs
  22. local options
  23. local table_record
  24. local file_monitors
  25.  
  26. --Переменные радаров
  27. local r_com_port = 456
  28. local r_otv_port = 123
  29. local radars = {{},{},{},{},{},{},{},{}}
  30.  
  31. --переменные таблицы
  32. local ygol_0_0 = "┌"
  33. local ygol_0_1 = "┐"
  34. local ygol_1_0 = "└"
  35. local ygol_1_1 = "┘"
  36. local horiz_line = "─"
  37. local vert_line = "│"
  38. local default_perec = "┼"
  39. local up_perec = "┴"
  40. local down_perec = "┬"
  41. local right_perec = "├"
  42. local left_perec = "┤"
  43.  
  44.  
  45.  
  46.  
  47.  
  48. -- Функции
  49.  
  50. function error_out()
  51.   size = 1
  52.   for address in component.list("screen") do
  53.     switch_monitor(address)
  54.     term.clear()
  55.     x, y = gpu.getResolution()
  56.     j=1
  57.     for i = math.floor(y/4), math.floor(3*y/4) do
  58.       for z = (i-math.floor(y/4))%10+1,x,10 do
  59.         if(j == 1) then
  60.           j = 2
  61.           gpu.setBackground(0xff0000)
  62.         elseif(j==2) then
  63.           j = 1
  64.           gpu.setBackground(0xffff00)
  65.         end
  66.         gpu.fill(z,i,10,1," ")
  67.       end
  68.     end
  69.   end
  70.  
  71.  
  72.  
  73. end
  74.  
  75.  
  76.  
  77.  
  78. function decode(message)
  79. otvet = message
  80. return message
  81. end
  82.  
  83. function code(message)
  84. otvet = message
  85. return otvet
  86. end
  87.  
  88. function filtr(table)
  89.  
  90. end
  91.  
  92. function pre_initial()
  93. if(not fs.exists("/files")) then
  94.   fs.makeDirectory("/files")
  95. end
  96. if(not fs.exists("/files/options")) then
  97.   file = io.open("/files/options","w")
  98.   file:write("size: 4\n")
  99.   file:write("foreground: 0xffffff\n")
  100.   file:write("background: 0xaaaa00\n")
  101.   file:write("control_port: 1245\n")
  102.   file:write("otvet_port: 2356\n")
  103.   file:flush()
  104.   file:close()
  105. end
  106. if(not fs.exists("/files/table_players")) then
  107.   file = io.open("/files/table_players","w")
  108.   file:flush()
  109.   file:close()
  110. end
  111. if( not fs.exists("/files/table_monitors")) then
  112.   file = io.open("/files/table_monitors","w")
  113.   for address in component.list("screen") do
  114.     if(component.screen.address ~= address) then
  115.       file:write(address.."\n")
  116.     end
  117.   end
  118.   file:close()
  119. end
  120.  
  121. table_records = io.open("/files/table_players","a")
  122.  
  123.  
  124.  
  125.  
  126. file = io.open("/files/options","r")
  127. while(true) do
  128.   data = nil
  129.   data = file:read("*l")
  130.   if(data == nil) then
  131.     break
  132.   end
  133.   temp = data:find(":")
  134.   option = data:sub(1,temp-1)
  135.   if(option == "size") then
  136.     size = tonumber(data:sub(temp+2))
  137.   elseif(option=="foreground") then
  138.     foreground = tonumber(data:sub(temp+2))
  139.   elseif(option=="background") then
  140.     background = tonumber(data:sub(temp+2))
  141.   elseif(option=="control_port") then
  142.     control_port = tonumber(data:sub(temp+2))
  143.   elseif(option == "otvet_port") then
  144.     otvet_port = tonumber(data:sub(temp+2))
  145.   end
  146. end
  147. file:close()
  148. i = 1
  149. for line in io.lines("/files/table_monitors") do
  150.   table_monitors[i] = line
  151.   i = i+1
  152. end
  153. for address in component.list("screen") do
  154.  
  155.   for i = 1, #table_monitors do
  156.     if(address ~= table_monitors[i]) then
  157.       component.setPrimary("screen",address)
  158.       main_monitor = address
  159.     end
  160.   end
  161.  
  162. end
  163. end
  164.  
  165. function initial()
  166. modem.broadcast(otvet_port,code("Ok"))
  167. i = 1
  168. for address in component.list("screen")
  169. do
  170. if(main_monitor ~= address) then
  171. table_monitors[i] = address
  172. i=i+1  
  173. end
  174. component.invoke(address,"turnOn")
  175. end
  176.  
  177. term.clear()
  178. switch_monitor(table_monitors[1])
  179. term.clear()
  180. if(not(modem.isOpen(control_port))) then
  181. modem.open(control_port)
  182. end
  183. end
  184.  
  185. function scan(address,isPlyers)
  186. if(isPlayers) then
  187. return component.invoke(address,"getPlayers")
  188. else
  189.   return component.invoke(address,"getMobs")
  190. end
  191. end
  192. function output_table()
  193. print("Не готово")
  194. end
  195.  
  196. function edit_size(new_size)
  197.   size = new_size
  198.   file = io.open("/files/options","r")
  199.   os.sleep(0.5)
  200.   proverca =false
  201.   while(true) do
  202.     temp = file:read("*l")
  203.     if(temp == nil) then
  204.       proverca =true
  205.       break
  206.     end
  207.     temp_nb = temp:find(":")
  208.     temp_label = temp:sub(1,temp_nb-1)
  209.     print(temp_label)
  210.     if(temp_label == "size") then
  211.       break
  212.     end
  213.   end
  214.   file:close()
  215.   file = io.open("/files/options","a")
  216.   if(proverca) then
  217.     file:seek("end")
  218.     file:write("\nsize: "..tostring(size))
  219.  
  220.   else
  221.     file:seek("set",temp_nb+1)
  222.     file:write(size.."  \n")
  223.   end
  224. file:close()
  225.  
  226. for i = 1, #table_monitors do
  227. switch_monitor(table_monitors[i])
  228. term.clear()
  229. term.write("Готово\n")
  230. end
  231. os.sleep(2)
  232. end
  233.  
  234. function switch_monitor(new_monitor)
  235. if(new_monitor == nil)
  236. then
  237.   gpu.bind(main_monitor)
  238.   gpu.setForeground(0xffffff)
  239.   gpu.setBackground(0x000000)
  240.   temp_x, temp_y = gpu.maxResolution()
  241.   gpu.setResolution(temp_x, temp_y)
  242.   component.setPrimary("screen",main_monitor)
  243. else
  244.   gpu.bind(new_monitor)
  245.   gpu.setForeground(foreground)
  246.   gpu.setBackground(background)
  247.   set_scale_monitor(new_monitor)
  248.   component.setPrimary("screen",new_monitor)
  249. end
  250. end
  251.  
  252.  
  253. function set_scale_monitor(address)
  254.   temp_x,temp_y = component.invoke(address,"getAspectRatio")
  255.   proportion = (temp_x*2 - 0.5)/(temp_y-0.25)
  256.   x,y = gpu.maxResolution()
  257.   if(x/y>proportion) then
  258.     x = math.floor(proportion*y)
  259.   else
  260.     y = math.floor(x/proportion)
  261.   end
  262.   x = math.floor(x/size)
  263.   y = math.floor(y/size)
  264.   gpu.setResolution(x,y)
  265. end
  266.  
  267.  
  268.  
  269. --Сам код
  270. pre_initial()
  271. initial()
  272.  
  273. viem_energ = thread.create(function() while(true) do x, y = gpu.getResolution()  gpu.set(1,y, tostring(computer.energy())) os.sleep(0.5) end end)
  274.  
  275. while(true) do
  276.   term.write("Введите команду\n")
  277.   _,_,_,_,_,command = event.pull("modem_message")
  278.   command = decode(command)
  279.   if(command == "exit") then
  280.     modem.broadcast(otvet_port,code("Ok"))
  281.     foreground = 0xffffff
  282.     background = 0x000000
  283.     for i = 1 , #table_monitors do
  284.       switch_monitor(table_monitors[i])
  285.       term.clear()
  286.       component.invoke(table_monitors[i],"turnOff")
  287.     end
  288.     viem_energ:kill()
  289.     switch_monitor()
  290.     term.clear()
  291.     modem.close(control_port)
  292.     os.exit()
  293.   elseif(command == "test_start") then
  294.     modem.broadcast(otvet_port,code("ok"))
  295.     term.clear()
  296.   elseif(command=="full_exit") then
  297.     modem.broadcast(otvet_port,code("ok"))
  298.     foreground = 0xffffff
  299.     background = 0x000000
  300.     for i = 1, #table_monitors do
  301.       switch_monitor(table_monitors[i])
  302.       term.clear()
  303.       component.invoke(table_monitors[i],"turnOff")
  304.     end
  305.     switch_monitor()
  306.     viem_energ:kill()
  307.     computer.shutdown()
  308.   elseif(command == "Изменить размер") then
  309.     term.clear()
  310.     term.write("Изменение размера\n")
  311.     modem.broadcast(otvet_port,code("размер"))
  312.     switch_monitor(table_monitors[1])
  313.     _,_,_,_,_, otvet = event.pull(30,"modem_message")
  314.     otvet = decode(otvet)
  315.     if(otvet ==nil) then
  316.       term.write("Ошибка!!!\n")
  317.       os.sleep(2)
  318.       term.clear()
  319.     else
  320.       edit_size(tonumber(otvet))
  321.       switch_monitor(table_monitors[1])
  322.       term.clear()
  323.     end
  324.   elseif(command == "Скан 1") then --До и после сканирование
  325.     for address in component.list("radar") do
  326.       table_scan[address] = scan
  327.     end
  328.   elseif(command == "test") then
  329.     error_out()
  330.     os.sleep(10)
  331.   else
  332.     term.write("Неверная команда\n")
  333.   end
  334.  
  335.  
  336. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement