Taruu

Untitled

Jun 13th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local colors = require("colors")
  4. local text = require("text")
  5. local gpu = component.gpu
  6. local w, h = gpu.getResolution()
  7.  
  8. --список ячеек
  9. list_all_cell = {
  10. {"d84e5c05-5fba-4fea-b523-df103932002e","aea80191-5961-486a-a175-5b3b1e70895f","38c29be3-9d12-48d2-9147-09eac04bd3ad"},
  11. {"e8938c89-fd36-4d4c-b13a-414043937d2d","cdc87287-8ebc-4a5b-85ed-fb6630cd540c","c591cd76-0558-48cb-b7ea-2aeab05f3498"},
  12. {"b6ff451b-5eb8-4fb8-bf60-e1ff8d2f2b6d","85e3e65f-0a14-4e34-983f-facb613dc823","6fe772a2-d5b2-45af-ab70-34a68786135d"},
  13. {"c8f421d6-a31f-4c08-9bdd-286f6505080d","a43d2e3d-ba3a-4eb9-a588-5f02e7d96fe3","ac6969c0-9cd1-4356-ab2e-6ca6cf3c1c21"}
  14. }
  15. --Контроль генераторов
  16. list_all_disel_generator = {"984e503d-983d-45d2-bc18-1b33ff169166","d0fd57a5-dae2-4b8d-ad17-8a2b84218806","d447e55e-763c-4696-b5f9-a04bbe2eb851"}
  17.  
  18. --Список всех входов
  19. list_all_tanks = {"aa1429dc-8a92-4c00-8dc0-499f69c1abeb","4c513a7b-369a-4b02-88a3-3347769f3897","09e11c94-5e1e-4d8b-ac58-807d565e46d2","5f6ea351-d2f0-4634-a55c-317bda930345"}
  20.  
  21. --Case для танкеров
  22. case_tank = {
  23. [0] = "0-6%",
  24. [1]="7%-13%",
  25. [2]="14%-18%",
  26. [3]="19%-24%",
  27. [4]="25%-31%",
  28. [5]="32%-37%",
  29. [6]="38%-44%",
  30. [7]="45%-51%",
  31. [8]="52%-55%",
  32. [9]="56%-64%",
  33. [10]="65%-71%",
  34. [11]="72%-77%",
  35. [12]="78%-83%",
  36. [13]="84%-91%",
  37. [14]="92%-99%",
  38. [15]="100%"}
  39.  
  40. --макс энергии в ячейке
  41. max_energy_in_cell = 50000000
  42. --значение для запуска движка
  43. min_average = 20000000
  44. --Автоматический режим?
  45. AutoMode = false
  46. --Сторона от редстоун контроллера
  47. local side = 3
  48.  
  49. --Pass это pass
  50. function pass()
  51. end
  52.  
  53. --Копипаст.... Ну кто не без греха
  54. function format_int(number)
  55.  
  56. local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
  57.  
  58. -- reverse the int-string and append a comma to all blocks of 3 digits
  59. int = int:reverse():gsub("(%d%d%d)", "%1,")
  60.  
  61. -- reverse the int-string back remove an optional comma and put the
  62. -- optional minus and fractional part back
  63. return minus .. int:reverse():gsub("^,", "") .. fraction
  64. end
  65.  
  66. --Округление
  67. function floor_to_step(what,step)
  68. return math.floor(what/step) * step
  69. end
  70.  
  71. --Состояние резервуара
  72. function str_tank(in_count)
  73. return case_tank[math.floor(in_count)]
  74. end
  75.  
  76. --Сумма
  77. function sum(list)
  78. result = 0
  79. for item in ipairs(list) do
  80. result = result + list[item].getEnergyStored()
  81. end
  82. return result
  83. end
  84.  
  85. --Вырубаем все!
  86. function stop_all_gen()
  87. for id in ipairs(list_all_disel_generator) do
  88. list_all_disel_generator[id].setEnabled(false)
  89. end
  90. end
  91. --Стартуем
  92. function start_all_gen()
  93. for id in ipairs(list_all_disel_generator) do
  94. list_all_disel_generator[id].setEnabled(true)
  95. end
  96. end
  97.  
  98. --Контроль генераторов
  99. function on_off_generator()
  100. local all_energy = 0
  101. for id in ipairs(list_all_cell) do -- Обход всех ячеек
  102. local temp_sum = sum(list_all_cell[id])
  103. all_energy = all_energy + temp_sum
  104. local average_cell = temp_sum/#list_all_cell[id] -- Странно что ее мало кто упоминает....
  105. local generator = list_all_disel_generator[id]
  106. if generator ~= nil then
  107. local generator_status = generator.isActive()
  108. if average_cell < min_average and AutoMode then
  109. if generator_status then
  110. pass()
  111. else
  112. generator.setEnabled(true)
  113. end
  114. elseif average_cell >= max_energy_in_cell-120 and AutoMode then
  115. if generator_status == false then
  116. pass()
  117. else
  118. generator.setEnabled(false)
  119. end
  120. else
  121. pass()
  122. end
  123. clear
  124. end
  125. gpu.set(3,id+3,"Ячейка номер "..id.." RF:"..format_int(floor_to_step(average_cell,0.01)))
  126. gpu.set(63,id+3,"Танкер "..id..":"..str_tank(list_all_tanks[id].getInput(side)).." ")
  127. end
  128. gpu.set(3,1,"Всего RF:"..format_int(floor_to_step(all_energy,0.01)))
  129.  
  130. end
  131.  
  132. function clearScreen() -- очистка экранна
  133. gpu.setBackground(0x2D2D2D)
  134. gpu.fill(1,1,w,h," ")
  135. end
  136.  
  137.  
  138.  
  139. function drawbuttons() -- отрисовка кнопок
  140. gpu.setBackground(0x0066cc)
  141. gpu.set(70,24," Exit ")
  142. gpu.set(62,24,"StopAll")
  143. if AutoMode then
  144. gpu.setBackground(0x006600)
  145. gpu.set(53,24,"AutoMode")
  146. gpu.setBackground(0x0066cc)
  147. else
  148. gpu.set(53,24,"AutoMode")
  149. end
  150. gpu.set(44,24,"StartAll")
  151.  
  152. gpu.setBackground(0x2D2D2D)
  153. end
  154.  
  155. function button_handler(x,y) --Обработка кнопок
  156. gpu.fill(1,24,17,25," ")
  157. if y == 24 and 43 < x and x < 52 then
  158. gpu.set(2,25,"Start All")
  159. start_all_gen()
  160. AutoMode = true
  161. elseif y == 24 and 52 < x and x < 61 then
  162. gpu.set(2,25,"AutoMode")
  163. if AutoMode ~= true then
  164. AutoMode = true
  165. gpu.set(2,25,"AutoMode on")
  166. else
  167. AutoMode = false
  168. gpu.set(2,25,"AutoMode off")
  169. end
  170. elseif y == 24 and 61 < x and x < 69 then
  171. AutoMode = false
  172. stop_all_gen()
  173. gpu.set(2,25,"StopAll")
  174. elseif y == 24 and 69 < x and x < 78 then
  175. gpu.setBackground(0x000000)
  176. gpu.fill(1,1,w,h," ")
  177. os.exit()
  178. gpu.set(2,25,"Exit")
  179. end
  180. drawbuttons()
  181. end
  182. --Готовим экран
  183. clearScreen()
  184. drawbuttons()
  185.  
  186. --Прогон всех компонентов
  187. gpu.set(2,25,"config cells")
  188.  
  189. for col_three in ipairs(list_all_cell) do
  190. for row_item in ipairs(list_all_cell[col_three]) do
  191. temp_str_id = list_all_cell[col_three][row_item]
  192. list_all_cell[col_three][row_item] = component.proxy(temp_str_id)
  193. end
  194. end
  195. gpu.set(2,25,"config generator")
  196.  
  197. for col_three in ipairs(list_all_disel_generator) do
  198. temp_str_id = list_all_disel_generator[col_three]
  199. list_all_disel_generator[col_three] = component.proxy(temp_str_id)
  200. list_all_disel_generator[col_three].enableComputerControl(true)
  201. list_all_disel_generator[col_three].setEnabled(false)
  202. end
  203.  
  204. gpu.set(2,25,"config tanks")
  205.  
  206. for id in ipairs(list_all_tanks) do
  207. temp_str_id = list_all_tanks[id]
  208. list_all_tanks[id] = component.proxy(component.get(temp_str_id))
  209. end
  210.  
  211. -- Первый запуск для отрисовки
  212. on_off_generator()
  213.  
  214.  
  215. while true do
  216. local id, _, x, y = event.pull(5,"touch") --это не очень решение но мне хватило...
  217. gpu.fill(1,24,17,25," ")
  218. if id == "touch" then
  219. --gpu.set(2,25,x.." "..y)
  220. button_handler(x,y)
  221. else
  222. if AutoMode then
  223. gpu.set(2,25,"update")
  224. on_off_generator()
  225. else
  226. on_off_generator()
  227. gpu.set(2,25,"All stop!")
  228. end
  229. end
  230. end
Add Comment
Please, Sign In to add comment