mrWhiskasss

pre craft from volosatik titan

Oct 31st, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local shell = require("shell")
  4. local event = require("event")
  5. local unicode = require("unicode")
  6. local term = require("term")
  7. local g = component.gpu
  8. local me = component.me_interface
  9.  
  10. if not fs.exists("/lib/oc.lua") then
  11. shell.execute("wget https://www.dropbox.com/s/cgfytt8g174a6vs/libPrecraft.lua?dl=1 /lib/oc.lua")
  12. end
  13. local oc = require("oc")
  14.  
  15. if not fs.exists("/home/BD.txt") then
  16. oc.savef("BD.txt", {})
  17. end
  18.  
  19. --------------------Настройки--------------------
  20. local CBUTTON, CDRAW, CTEXT = 0x00ffff, 0x004575, 0xD38FFE -- текст кнопок, рамки, осн текст
  21. local debug = true -- вкл дебаг (меню с недостающими крафтами и нехватной ресурсов)
  22. local ADM = {['KReaTlVNuY']=true, ['4ertik_']=true} -- доступы к нажатиям
  23. local scroll = 1 -- начало скролла
  24. local precraft = true -- флаг осн цикла
  25. local go = true -- включить идет ли проверка вещей
  26. local WIDTH, HEIGHT = 80, 50
  27. --------------------Настройки--------------------
  28. local inputFind = ""
  29. local guiPath, debugLog1, debugLog2 = {}, {}, {}
  30. if debug then WIDTH, HEIGHT = 160, 50 end
  31. g.setResolution(WIDTH,HEIGHT)
  32. g.setForeground(CTEXT)
  33.  
  34. local function Clear() g.setBackground(0x000000) g.setForeground(0xD38FFE) g.fill(1,4,WIDTH,HEIGHT-4," ") end
  35.  
  36. local function DrawScrollContent()
  37. if guiPath[#guiPath]=="start" or guiPath[#guiPath] == "stop" or guiPath[#guiPath] == "go" then
  38. for i = scroll, maxscroll do
  39. if content[i] then
  40. local k = i - scroll
  41. if i == posSelect then
  42. g.setBackground(0x1e1e1e)
  43. g.fill(11, 7 k, 58, 1, " ")
  44. g.set(13,7 k, tostring(i.. ". "..content[i].name))
  45. g.set(45, 7 k, tostring(content[i].count))
  46. g.set(60, 7 k, tostring(content[i].craftSize))
  47. g.setBackground(0x000000)
  48. else
  49. g.setBackground(0x000000)
  50. g.fill(11, 7 k, 58, 1, " ")
  51. g.set(13,7 k, tostring(i.. ". "..content[i].name))
  52. g.set(45, 7 k, tostring(content[i].count))
  53. g.set(60, 7 k, tostring(content[i].craftSize))
  54. end
  55. end
  56. end
  57. end
  58. end
  59.  
  60. local function ScrollContent()
  61. local cont = {}
  62. if inputFind ~= nil and inputFind ~= "" then
  63. for key, data in pairs(content) do
  64. if unicode.lower(data.name):find(unicode.lower(inputFind)) then
  65. table.insert(cont, content[key])
  66. end
  67. end
  68. return cont
  69. else
  70. return content
  71. end
  72. end
  73.  
  74. local function eScroll(_,_,_,_,zs,nick)
  75. if ADM[nick] then
  76. for key in pairs(scrolls) do
  77. for i = 1, #scrolls[key].ScrollIn do
  78. if scrolls[key].ScrollIn[i] == guiPath[#guiPath] then
  79. if not zs then
  80. scroll = 1
  81. elseif zs == 1 and scroll > 1 then
  82. scroll = scroll - 1
  83. elseif zs == -1 and maxscroll < #content then
  84. scroll = scroll 1
  85. end
  86. maxscroll = scroll scrolls[key].h - 1
  87. scrolls[key].action()
  88. content = ScrollContent()
  89. oc.drawscroll(scrolls[key].x, scrolls[key].y, scrolls[key].w, scrolls[key].h, scroll, #content, 0x1e1e1e, 0x004575)
  90. g.fill(scrolls[key].xGui 1,scrolls[key].yGui 1,scrolls[key].wGui-4,scrolls[key].hGui-2, " ")
  91. DrawScrollContent()
  92. end
  93. end
  94. end
  95. end
  96. end
  97.  
  98. local function Main()
  99. if guiPath[#guiPath] == "start" or guiPath[#guiPath] == "stop" or guiPath[#guiPath] == "go" then
  100. g.set(13, 5, "Название предмета:")
  101. g.set(45, 5, "Кол-во:")
  102. g.set(60, 5, "Крафт:")
  103. g.set(20,43, "Для поиска введите название..")
  104. oc.right(71,50, "Всего "..#dataItems.. " крафтов")
  105. g.fill(85,1, 75, 51, " ")
  106. for i = 1, #debugLog1 do
  107. g.set(85, 4 i, tostring(debugLog1[i]))
  108. end
  109. for i = 1, #debugLog2 do
  110. g.set(85, 26 i, tostring(debugLog2[i]))
  111. end
  112. elseif guiPath[#guiPath] == "change" then
  113. g.set(13, 10, "Название предмета: "..dataItems[posSelect].name)
  114. g.set(13, 12, "id предмета: "..tostring(dataItems[posSelect].id))
  115. g.set(13, 14, "dmg предмета: "..tostring(dataItems[posSelect].dmg))
  116. g.set(13, 16, "Количество поддержания: "..tostring(dataItems[posSelect].count))
  117. g.set(13, 18, "Размер крафта: "..tostring(dataItems[posSelect].craftSize))
  118. end
  119. end
  120.  
  121. local function DrawScrolls()
  122. for key in pairs(scrolls) do
  123. for i = 1, #scrolls[key].ScrollIn do
  124. if scrolls[key].ScrollIn[i] == guiPath[#guiPath] then
  125. scrolls[key].action()
  126. eScroll(_,_,_,_,nil,"KReaTlVNuY")
  127. oc.drawscroll(scrolls[key].x, scrolls[key].y, scrolls[key].w, scrolls[key].h, scroll, #content, 0x1e1e1e, 0x004575)
  128. oc.drawbutton(scrolls[key].xGui,scrolls[key].yGui,scrolls[key].wGui,scrolls[key].hGui,CBUTTON,CDRAW,"")
  129. end
  130. end
  131. end
  132. end
  133.  
  134. local function DrawButtons()
  135. for button in pairs(buttons) do
  136. for i = 1, #buttons[button].ButtonIn do
  137. if buttons[button].ButtonIn[i] == guiPath[#guiPath] and buttons[button].visible then
  138. oc.drawbutton(buttons[button].x,buttons[button].y,buttons[button].w, buttons[button].h, buttons[button].ctext, buttons[button].cbutton, buttons[button].text)
  139. end
  140. end
  141. end
  142. end
  143.  
  144. local function toGui(gui, clear)
  145. if clear then
  146. guiPath[#guiPath] = nil
  147. end
  148. if guiPath[#guiPath] ~= gui then
  149. guiPath[#guiPath 1] = gui
  150. end
  151. dataq, dataItems = nil, nil
  152. dataItems = oc.loadf("BD.txt")
  153. Clear()
  154. Main()
  155. DrawButtons()
  156. DrawScrolls()
  157. end
  158.  
  159. local function Back(to)
  160. if to then
  161. if to <= #guiPath then for i = 1, to do table.remove(guiPath, #guiPath) end end
  162. else
  163. table.remove(guiPath, #guiPath)
  164. end
  165. posSelect = nil
  166. Clear()
  167. Main()
  168. DrawButtons()
  169. DrawScrolls()
  170. end
  171.  
  172. local function AddLogs(nameTable, log)
  173. if nameTable == "ore" then
  174. if #debugLog1 > 19 then table.remove(debugLog1, 1) end
  175. table.insert(debugLog1, log)
  176. else
  177. if #debugLog2 > 19 then table.remove(debugLog2, 1) end
  178. table.insert(debugLog2, log)
  179. end
  180. Main()
  181. end
  182.  
  183. local function Check()
  184. for i = 1, #dataq do
  185. if not go then break end
  186. if not dataq[i].craft or dataq[i].craft.isDone() or dataq[i].craft.isCanceled() then
  187. dataq[i].craft = nil
  188. local itemsMe = me.getItemDetail({id = dataq[i].id, dmg = dataq[i].dmg})
  189. if itemsMe then
  190. local delta = dataq[i].count - itemsMe.basic().qty
  191. if delta > dataq[i].craftSize then delta = dataq[i].craftSize end
  192. if delta > 0 then
  193. local cpus = me.getCpus()
  194. for j = 1, #cpus do
  195. if not cpus[j].busy and cpus[j].storage == dataq[i].cpu then
  196. local craftables = me.getCraftables({name = dataq[i].id, damage = dataq[i].dmg})
  197. if craftables.n >= 1 then
  198. dataq[i].craft = craftables[1].request(delta, false, tostring(dataq[i].cpu))
  199. --AddLogs("ore", delta.. " "..dataq[i].count)
  200. if debug then
  201. local cancel, cancelmsg = dataq[i].craft.isCanceled()
  202. if not cancel and cancelmsg then
  203. if cancelmsg == "computing" then
  204. AddLogs("ore", dataq[i].name.. " не хватает ресурсов " .. cancelmsg)
  205. end
  206. elseif cancelmsg == "request failed (missing resources?)" then
  207. AddLogs("ore", dataq[i].name.. " не хватает ресурсов " .. cancelmsg)
  208. elseif cancelmsg then
  209. AddLogs("ore", dataq[i].name .. " ".. cancelmsg)
  210. end
  211. os.sleep(0.5)
  212. break
  213. end
  214. elseif debug then
  215. AddLogs("recipe", dataq[i].name.. " не имеет рецепта")
  216. end
  217. break
  218. end
  219. end
  220. end
  221. end
  222. end
  223. end
  224. os.sleep(1)
  225. AddLogs("ore", " ")
  226. AddLogs("recipe", " ")
  227. end
  228.  
  229. local function AddItem()
  230. changeitem = true
  231. if me.getStackInSlot(1) then
  232. g.fill(1,43, WIDTH, 1, " ")
  233. g.set(14,43,"Введите название предмета:") term.setCursor(40,43)
  234. local name = tostring(io.read()) g.fill(1,43,80,1, " ")
  235. g.set(14,43,"Введите кол-во поддержания:") term.setCursor(41,43)
  236. local count = tonumber(io.read()) g.fill(1,43,80,1, " ")
  237. g.set(14,43,"Введите макс. объём крафта:") term.setCursor(41,43)
  238. local craftSize = tonumber(io.read()) g.fill(1,43,80,1, " ")
  239. table.insert(dataItems, {name=name, id = me.getStackInSlot(1).id,dmg = me.getStackInSlot(1).dmg, count = count, craftSize = craftSize, cpu = 65536})
  240. oc.savef("BD.txt", dataItems)
  241. else
  242. g.set(14, 43, "Предмет не установлен в 1-й слот интерфейса")
  243. os.sleep(2)
  244. g.fill(1, 43, WIDTH, 1, " ")
  245. end
  246. Main()
  247. changeitem = false
  248. end
  249.  
  250. local function RemoveItem()
  251. if posSelect and dataItems then
  252. for i = 1, #dataItems do
  253. if dataItems[i].name == content[posSelect].name then
  254. table.remove(dataItems, i)
  255. oc.savef("BD.txt", dataItems)
  256. eScroll(_,_,_,_,nil,"KReaTlVNuY")
  257. posSelect = nil
  258. return true
  259. end
  260. end
  261. end
  262. end
  263.  
  264. local function EditItem()
  265. if posSelect and dataItems then
  266. for i = 1, #dataItems do
  267. if dataItems[i].name == content[posSelect].name then
  268. posSelect = i
  269. toGui("change")
  270. return true
  271. end
  272. end
  273. end
  274. end
  275.  
  276. local function ChangeItem(action)
  277. if posSelect and dataItems then
  278. g.fill(1,45, WIDTH, 1, " ")
  279. if action == "changeName" then
  280. g.set(14,45,"Введите название предмета:") term.setCursor(40,45)
  281. dataItems[posSelect].name = tostring(io.read())
  282. elseif action == "changeCount" then
  283. g.set(14,45,"Введите кол-во поддержания:") term.setCursor(41,45)
  284. dataItems[posSelect].count = tonumber(io.read())
  285. elseif action == "changeCraftSize" then
  286. g.set(14,45,"Введите макс. объём крафта:") term.setCursor(41,45)
  287. dataItems[posSelect].craftSize = tonumber(io.read())
  288. end
  289. oc.savef("BD.txt", dataItems)
  290. g.fill(1, 45, WIDTH, 1, " ")
  291. Main()
  292. end
  293. end
  294.  
  295. local function eButton(_,_,x,y,_, nick)
  296. if ADM[nick] then
  297. for button in pairs(buttons) do
  298. if x >= buttons[button].x and x <= buttons[button].endX and y >= buttons[button].y and y <= buttons[button].endY then
  299. for i = 1, #buttons[button].ButtonIn do
  300. if buttons[button].ButtonIn[i] == guiPath[#guiPath] then
  301. yTouch = y
  302. buttons[button].action()
  303. return true
  304. end
  305. end
  306.  
  307. end
  308. end
  309. end
  310. end
  311.  
  312. local function InputWrite(_,_,key1, key2,nick)
  313. if ADM[nick] and not changeitem then
  314. for key in pairs(scrolls) do
  315. for i = 1, #scrolls[key].ScrollIn do
  316. if scrolls[key].ScrollIn[i] == guiPath[#guiPath] then
  317. local y = scrolls[key].iwY
  318. g.fill(20,y, 30, 1, " ")
  319. if key1 == 8 then -- backspace
  320. inputFind = unicode.sub(inputFind, 1, -2)
  321. elseif key1 == 0 and key2 == 211 then -- delete
  322. inputFind = ""
  323. elseif key1 ~= 0 then -- keyboard
  324. if inputFind == nil or inputFind == "" then
  325. inputFind = unicode.char(key1)
  326. elseif unicode.len(inputFind) < 49 then
  327. inputFind = inputFind .. unicode.char(key1)
  328. end
  329. end
  330. if unicode.len(inputFind) < 30 and unicode.len(inputFind) > 0 then
  331. g.set(20,y, tostring(inputFind))
  332. elseif unicode.len(inputFind) >= 30 then
  333. g.set(20,y, tostring(inputFind:sub(unicode.len(inputFind)-29, unicode.len(inputFind))))-- отображение последних 20 символов
  334. else
  335. g.set(20,y, "Для поиска введите название..")
  336. end
  337. eScroll(_,_,_,_,nil,"KReaTlVNuY")
  338. end
  339. end
  340. end
  341. end
  342. end
  343.  
  344. local function initButtons()
  345. for button in pairs(buttons) do
  346. buttons[button].endX = buttons[button].x buttons[button].w - 1
  347. buttons[button].endY = buttons[button].y buttons[button].h - 1
  348. end
  349. end
  350.  
  351. local function LoadSystem()
  352. g.fill(1,1,WIDTH, HEIGHT, " ")
  353. dataItems = oc.loadf("BD.txt")
  354. initButtons()
  355. etouch = event.listen("touch", eButton)
  356. escroll = event.listen("scroll", eScroll)
  357. ekeydown = event.listen("key_down", InputWrite)
  358. toGui("start")
  359. dataq = dataItems
  360.  
  361. return true
  362. end
  363.  
  364. buttons = {
  365. LOGO = {ButtonIn = {"start", "go", "stop", "change"}, visible = true, x = 10, y = 1, w = 62, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "PreCraft", action = function() end},
  366. Exit = {ButtonIn = {"start", "go","stop", "change"}, visible = false, x = 1, y = 1, w = 1, h = 1, cbutton = 0x004575, ctext = 0x00ffff, text = "", action = function() g.setResolution(160,50) event.cancel(etouch) event.cancel(ekeydown) event.cancel(escroll) precraft = false end},
  367.  
  368. Select = {ButtonIn = {"stop"}, visible = false, x = 11, y = 7, w = 60, h = 35, text = "", action = function() posSelect = yTouch scroll - 7 DrawScrollContent() end},
  369. Go = {ButtonIn = {"start","go"}, visible = true, x = 14, y = 47, w = 24, h = 3, cbutton = 0x28C730, ctext = 0x28C730, text = "Go", action = function() end},
  370. GoTrue = {ButtonIn = {"stop"}, visible = true, x = 14, y = 47, w = 24, h = 3, cbutton = 0x00ffff, ctext = 0x00ffff, text = "Go", action = function() toGui("go") dataq = nil dataq = dataItems go = true end},
  371.  
  372. Stop = {ButtonIn = {"stop"}, visible = true, x = 43, y = 47, w = 24, h = 3, cbutton = 0xff0000, ctext = 0xff0000, text = "Stop", action = function() end},
  373. StopTrue = {ButtonIn = {"start", "go"}, visible = true, x = 43, y = 47, w = 24, h = 3, cbutton = 0x00ffff, ctext = 0x00ffff,text = "Stop", action = function() toGui("stop") go = false end},
  374.  
  375. Add = {ButtonIn = {"stop"}, visible = true, x = 20, y = 45, w = 10, h = 1, cbutton = nil, ctext = 0x00ffff, text = "[Добавить]", action = function() AddItem() end},
  376. Change = {ButtonIn = {"stop"}, visible = true, x = 36, y = 45, w = 10, h = 1, cbutton = nil, ctext = 0x00ffff, text = "[Изменить]", action = function() EditItem() end},
  377. Remove = {ButtonIn = {"stop"}, visible = true, x = 54, y = 45, w = 9, h = 1, cbutton = nil, ctext = 0x00ffff, text = "[Удалить]", action = function() RemoveItem() end},
  378. -- сhange
  379. changeName = {ButtonIn = {"change"}, visible = true, x = 14, y = 35, w = 53, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "Изменить название", action = function() ChangeItem("changeName") end},
  380. changeCount = {ButtonIn = {"change"}, visible = true, x = 14, y = 38, w = 53, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "Изменить количество поддержания", action = function() ChangeItem("changeCount") end},
  381. changeCraftSize = {ButtonIn = {"change"}, visible = true, x = 14, y = 41, w = 53, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "Изменит размер крафта", action = function() ChangeItem("changeCraftSize") end},
  382. changeBack = {ButtonIn = {"change"}, visible = true, x = 14, y = 47, w = 53, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "Назад", action = function() Back() end},
  383. }
  384.  
  385. scrolls = {
  386. Buy = {ScrollIn = {"start", "go", "stop"},iwY = 43, xGui = 10 , yGui = 6, wGui = 62, hGui = 37, x = 70, y = 7, w = 1, h = 35, action = function() content = dataItems end},
  387. }
  388.  
  389. function loop()
  390. if LoadSystem() then
  391. while precraft do
  392. if go then
  393. g.set(1,1,"check")
  394. Check()
  395. else
  396. g.set(1,1,"sleep")
  397. end
  398. os.sleep(1)
  399. end
  400. end
  401. end
  402.  
  403. ok, err = pcall(loop)
  404.  
  405.  
  406. if not ok then
  407. event.cancel(etouch)
  408. event.cancel(escroll)
  409. event.cancel(ekeydown)
  410. computer.shutdown(true)
  411. end
Advertisement
Add Comment
Please, Sign In to add comment