Advertisement
shenniko

AutoEssentia

Aug 28th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 60.74 KB | None | 0 0
  1. os.loadAPI("button")
  2. local m = peripheral.wrap("top")
  3. button.startup("top")
  4. local run = true
  5. local disableFillRedstoneDirection = "bottom"
  6. local essentia
  7. local essentiasCount = 51
  8. local emptyJars
  9. local totalJars
  10. local fillAmount
  11. local fillEssentia
  12. local fillAvailableSpace
  13. local topOffJarsEssentiaList
  14. local fillAllTo64EssentiaList
  15. local fillAllTo128EssentiaList
  16. local chests = {}
  17. local aspectAnalyzers = {}
  18. local chestAspects
  19. local distributeBurnRequests = true
  20. local alchemicalFurnaceFromChestDirection = "auto"
  21. local redstoneWaitTicks = 3
  22.  
  23. -- Print debug output during startup: 0 = off, 1 = summary debug, 2 = everything
  24. -- Errors are still shown regardless of the debug output level.
  25. local debugOutput = 2
  26. local padding = " "
  27. local numberPadding = 3
  28.  
  29. -- If true, the more intensive portions of this script sleep between each action
  30. -- If false, the script runs at maximum speed
  31. local serverFriendly = true
  32.  
  33. --[[
  34.  
  35. --Locations and colors of different visuals displayed on the monitor.--
  36.  
  37. * Auto positioning is done per-item depending on where it normally sits in relation
  38. to the other items. For the final position of a control see the setAutoDetectVariables() function below.
  39.  
  40. ** Virtually every "auto" set location for the main display is relative to the essentiaX/Y locations. essentiaX = "auto" will
  41. center the display on the monitor or if it can't be centered due to being too small it will be left-aligned.
  42.  
  43. ** Virtually every "auto" set location for the refill essentia display is relative to the refillScreenX/Y locations.
  44. refillScreenX = "auto" will center the display on the monitor or if it can't be centered due to being too small it will be left-aligned.
  45.  
  46. ** If a location isn't default to "auto" it does not currently support auto positioning.
  47.  
  48. * Any location or color that defaults to "auto" can be manually specified if that's desired. However, no error
  49. checking is done to make sure the manual positions are valid.
  50.  
  51. * Button height is calculated as 1 characters height. A button with the height of 1 will be the height of the button text.
  52.  
  53. * The inactive colors are the colors used during normal display. Active colors are during button presses.
  54. --]]
  55.  
  56. local essentiaColumnWidth = 19
  57. local essentiaColumnHeight = 17
  58. local essentiaColumnSpacing = 2
  59. local essentiaX = "auto"
  60. local essentiaY = 1
  61.  
  62. local refreshButtonX = "auto"
  63. local refreshButtonY = "auto"
  64. local refreshButtonWidth = 19
  65. local refreshButtonHeight = 2
  66.  
  67. local topoffAllButtonX = "auto"
  68. local topoffAllButtonY = "auto"
  69. local topoffAllButtonWidth = 19
  70. local topoffAllButtonHeight = 2
  71.  
  72. local emptyJarsX = "auto"
  73. local emptyJarsY = "auto"
  74.  
  75. local totalJarsX = "auto"
  76. local totalJarsY = "auto"
  77.  
  78. local refillScreenX = "auto"
  79. local refillScreenY = 1
  80.  
  81. local fillAllTo64ButtonX = "auto"
  82. local fillAllTo64ButtonY = "auto"
  83. local fillAllTo64ButtonWidth = 18
  84. local fillAllTo64ButtonHeight = 2
  85.  
  86. local fillAllTo128ButtonX = "auto"
  87. local fillAllTo128ButtonY = "auto"
  88. local fillAllTo128ButtonWidth = 18
  89. local fillAllTo128ButtonHeight = 2
  90.  
  91. local otherOptionsButtonX = "auto"
  92. local otherOptionsButtonY = "auto"
  93. local otherOptionsButtonWidth = 18
  94. local otherOptionsButtonHeight = 2
  95.  
  96. local essentiaTopOffJarsX = "auto"
  97. local essentiaTopOffJarsY = 5
  98.  
  99. local essentiaTopOffLabelX = "auto"
  100. local essentiaTopOffLabelY = 2
  101.  
  102. local essentiaFillAllTo64X = "auto"
  103. local essentiaFillAllTo64Y = 5
  104.  
  105. local essentiaFillAllTo64LabelX = "auto"
  106. local essentiaFillAllTo64LabelY = 2
  107.  
  108. local essentiaFillAllTo128X = "auto"
  109. local essentiaFillAllTo128Y = 5
  110.  
  111. local essentiaFillAllTo128LabelX = "auto"
  112. local essentiaFillAllTo128LabelY = 2
  113.  
  114. local cancelTopOffJarsButtonX = "auto"
  115. local cancelTopOffJarsButtonY = "auto"
  116. local cancelTopOffJarsButtonWidth = 19
  117. local cancelTopOffJarsButtonHeight = 2
  118.  
  119. local beginTopOffJarsFillButtonX = "auto"
  120. local beginTopOffJarsFillButtonY = "auto"
  121. local beginTopOffJarsFillButtonWidth = 19
  122. local beginTopOffJarsFillButtonHeight = 2
  123.  
  124. local cancelFillAllTo64ButtonX = "auto"
  125. local cancelFillAllTo64ButtonY = "auto"
  126. local cancelFillAllTo64ButtonWidth = 19
  127. local cancelFillAllTo64ButtonHeight = 2
  128.  
  129. local beginFillAllTo64ButtonX = "auto"
  130. local beginFillAllTo64ButtonY = "auto"
  131. local beginFillAllTo64ButtonWidth = 19
  132. local beginFillAllTo64ButtonHeight = 2
  133.  
  134. local cancelFillAllTo128ButtonX = "auto"
  135. local cancelFillAllTo128ButtonY = "auto"
  136. local cancelFillAllTo128ButtonWidth = 19
  137. local cancelFillAllTo128ButtonHeight = 2
  138.  
  139. local beginFillAllTo128ButtonX = "auto"
  140. local beginFillAllTo128ButtonY = "auto"
  141. local beginFillAllTo128ButtonWidth = 19
  142. local beginFillAllTo128ButtonHeight = 2
  143.  
  144. local burnScreenLabelX = "auto"
  145. local burnScreenLabelY = 3
  146.  
  147. local burnScreenBeanLabelX = 6
  148. local burnScreenBeanLabelY = 10
  149.  
  150. local burnScreenStatusX = 6
  151. local burnScreenStatusY = 13
  152.  
  153. local burnScreenBeanTextLabel = "Currently bean type: "
  154. local burnScreenStatusTextLabel = "Current status: "
  155.  
  156.  
  157. local monBackColor = colors.black
  158.  
  159. local essentiaInactiveTextColorEmpty = colors.gray
  160. local essentiaInactiveTextColorLow = colors.red
  161. local essentiaInactiveTextColorMedium = colors.yellow
  162. local essentiaInactiveTextColorHigh = colors.green
  163. local essentiaInactiveBackColor = monBackColor
  164. local essentiaActiveTextColor = colors.white
  165. local essentiaActiveBackColor = colors.lime
  166.  
  167. local refreshActiveBackColor = colors.lime
  168. local refreshInactiveBackColor = colors.blue
  169. local refreshActiveTextColor = colors.white
  170. local refreshInactiveTextColor = colors.white
  171.  
  172. local topoffAllJarsActiveTextColor = colors.white
  173. local topoffAllJarsInactiveTextColor = colors.white
  174. local topoffAllJarsActiveBackColor = colors.lime
  175. local topoffAllJarsInactiveBackColor = colors.blue
  176.  
  177. local fillAllTo64InactiveTextColor = colors.white
  178. local fillAllTo64ActiveTextColor = colors.white
  179. local fillAllTo64InactiveBackColor = colors.blue
  180. local fillAllTo64ActiveBackColor = colors.lime
  181.  
  182. local fillAllTo128InactiveTextColor = colors.white
  183. local fillAllTo128ActiveTextColor = colors.white
  184. local fillAllTo128InactiveBackColor = colors.blue
  185. local fillAllTo128ActiveBackColor = colors.lime
  186.  
  187. local refillButtonsInactiveBackColor = colors.blue
  188. local refillButtonsActiveBackColor = colors.lime
  189. local refillButtonsInactiveTextColor = colors.white
  190. local refillButtonsActiveTextColor = colors.white
  191.  
  192. local otherOptionsButtonInactiveBackColor = colors.blue
  193. local otherOptionsButtonActiveBackColor = colors.lime
  194. local otherOptionsButtonInactiveTextColor = colors.white
  195. local otherOptionsButtonActiveTextColor = colors.white
  196.  
  197. local cancelTopOffJarsButtonInactiveBackColor = colors.blue
  198. local cancelTopOffJarsButtonActiveBackColor = colors.lime
  199. local cancelTopOffJarsButtonInactiveTextColor = colors.white
  200. local cancelTopOffJarsButtonActiveTextColor = colors.white
  201.  
  202. local beginTopOffJarsFillButtonInactiveBackColor = colors.blue
  203. local beginTopOffJarsFillButtonActiveBackColor = colors.lime
  204. local beginTopOffJarsFillButtonInactiveTextColor = colors.white
  205. local beginTopOffJarsFillButtonActiveTextColor = colors.white
  206.  
  207. local cancelFillAllTo64ButtonInactiveBackColor = colors.blue
  208. local cancelFillAllTo64ButtonActiveBackColor = colors.lime
  209. local cancelFillAllTo64ButtonInactiveTextColor = colors.white
  210. local cancelFillAllTo64ButtonActiveTextColor = colors.white
  211.  
  212. local beginFillAllTo64ButtonInactiveBackColor = colors.blue
  213. local beginFillAllTo64ButtonActiveBackColor = colors.lime
  214. local beginFillAllTo64ButtonInactiveTextColor = colors.white
  215. local beginFillAllTo64ButtonActiveTextColor = colors.white
  216.  
  217. local cancelFillAllTo128ButtonInactiveBackColor = colors.blue
  218. local cancelFillAllTo128ButtonActiveBackColor = colors.lime
  219. local cancelFillAllTo128ButtonInactiveTextColor = colors.white
  220. local cancelFillAllTo128ButtonActiveTextColor = colors.white
  221.  
  222. local beginFillAllTo128ButtonInactiveBackColor = colors.blue
  223. local beginFillAllTo128ButtonActiveBackColor = colors.lime
  224. local beginFillAllTo128ButtonInactiveTextColor = colors.white
  225. local beginFillAllTo128ButtonActiveTextColor = colors.white
  226.  
  227.  
  228. local emptyJarsTextColor = colors.white
  229. local emptyJarsBackColor = monBackColor
  230.  
  231. local totalJarsTextColor = colors.white
  232. local totalJarsBackColor = monBackColor
  233.  
  234. local refillLabelTextColor = colors.white
  235. local refillCurrentAmountTextColor = colors.white
  236. local refillAddingAmountTextColor = colors.green
  237.  
  238. local topOffAllJarsEssentiaBackColor = monBackColor
  239. local topOffAllJarsEssentiaTextColorZero = essentiaInactiveTextColorEmpty
  240. local topOffAllJarsEssentiaTextColorNonZero = colors.white
  241.  
  242. local topOffAllJarsEssentiaLabelBackColor = monBackColor
  243. local topOffAllJarsEssentiaLabelTextColor = colors.white
  244.  
  245. local fillAllTo64EssentiaBackColor = monBackColor
  246. local fillAllTo64EssentiaTextColorZero = essentiaInactiveTextColorEmpty
  247. local fillAllTo64EssentiaTextColorNonZero = essentiaInactiveTextColorMedium
  248.  
  249. local fillAllTo64EssentiaLabelBackColor = monBackColor
  250. local fillAllTo64EssentiaLabelTextColor = colors.white
  251.  
  252. local fillAllTo128EssentiaBackColor = monBackColor
  253. local fillAllTo128EssentiaTextColorZero = essentiaInactiveTextColorEmpty
  254. local fillAllTo128EssentiaTextColorNonZero = essentiaInactiveTextColorHigh
  255.  
  256. local fillAllTo128EssentiaLabelBackColor = monBackColor
  257. local fillAllTo128EssentiaLabelTextColor = colors.white
  258.  
  259.  
  260. local burnScreenStatusBackColor = monBackColor
  261. local burnScreenStatusTextColor = colors.white
  262. local burnScreenBeanLabelBackColor = monBackColor
  263. local burnScreenBeanLabelTextColor = colors.white
  264. local burnScreenLabelBackColor = monBackColor
  265. local burnScreenLabelTextColor = colors.white
  266. local burnScreenBorderColor = colors.blue
  267.  
  268.  
  269.  
  270.  
  271. function setAutoDetectVariables()
  272. local monWidth
  273. local monHeight
  274.  
  275. monWidth,monHeight = m.getSize()
  276.  
  277. if (alchemicalFurnaceFromChestDirection ~= "auto") then
  278. if (alchemicalFurnaceFromChestDirection ~= "north"
  279. and alchemicalFurnaceFromChestDirection ~= "south"
  280. and alchemicalFurnaceFromChestDirection ~= "east"
  281. and alchemicalFurnaceFromChestDirection ~= "west") then
  282.  
  283. print("Invalid alchemical furnace from chest direction. !!RESTART REQUIRED!!")
  284. while (true) do
  285. sleep(60)
  286. end
  287. end
  288. end
  289.  
  290. if (essentiaX == "auto") then
  291. -- Centers the essentia display on the monitor horizontally
  292. essentiaX = math.floor((monWidth - ((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing)) / 2) + 1
  293.  
  294. -- The monitor is too small
  295. if (essentiaX < 1) then
  296. print("The monitor is too small width wise to display all the needed main screen information correctly.")
  297.  
  298. essentiaX = 1
  299. end
  300. end
  301.  
  302. if (refreshButtonX == "auto") then
  303. -- Centers the refresh button on the monitor horizontally
  304. refreshButtonX = essentiaX + math.floor(((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing) / 2) - math.ceil(refreshButtonWidth / 2) + 1
  305. end
  306.  
  307. if (refreshButtonY == "auto") then
  308. -- Puts the refresh button directly below the essentia list
  309. refreshButtonY = essentiaY + essentiaColumnHeight + 1
  310. end
  311.  
  312. if (topoffAllButtonX == "auto") then
  313. -- Puts the top off all button in line with the refresh button
  314. topoffAllButtonX = essentiaX + math.floor(((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing) / 2) - math.ceil(topoffAllButtonWidth / 2) + 1
  315. end
  316.  
  317. if (topoffAllButtonY == "auto") then
  318. -- Puts the top off all button directly below the refresh button
  319. topoffAllButtonY = refreshButtonY + refreshButtonHeight + 2
  320. end
  321.  
  322. if (fillAllTo64ButtonX == "auto") then
  323. -- Puts the fill all to 64 button directly to the right of the refresh button
  324. fillAllTo64ButtonX = refreshButtonX + refreshButtonWidth + 2
  325. end
  326.  
  327. if (fillAllTo64ButtonY == "auto") then
  328. -- Puts the fill all to 64 button in line with the refresh button
  329. fillAllTo64ButtonY = refreshButtonY
  330. end
  331.  
  332. if (fillAllTo128ButtonX == "auto") then
  333. -- Puts the fill all to 64 button directly to the right of the fill all to 64 button
  334. fillAllTo128ButtonX = fillAllTo64ButtonX
  335. end
  336.  
  337. if (fillAllTo128ButtonY == "auto") then
  338. -- Puts the fill all to 64 button directly below the fill all to 64 button
  339. fillAllTo128ButtonY = fillAllTo64ButtonY + fillAllTo64ButtonHeight + 2
  340. end
  341.  
  342. if (otherOptionsButtonX == "auto") then
  343. -- Puts the other button directly to the left of the top off all jars button
  344. otherOptionsButtonX = topoffAllButtonX - otherOptionsButtonWidth - 2
  345. end
  346.  
  347. if (otherOptionsButtonY == "auto") then
  348. -- Puts the other button at the same level as the top off all button
  349. otherOptionsButtonY = topoffAllButtonY
  350. end
  351.  
  352. if (emptyJarsX == "auto") then
  353. emptyJarsX = essentiaX + 1
  354. end
  355.  
  356. if (emptyJarsY == "auto") then
  357. emptyJarsY = essentiaY + essentiaColumnHeight + 1
  358. end
  359.  
  360. if (totalJarsX == "auto") then
  361. totalJarsX = emptyJarsX
  362. end
  363.  
  364. if (totalJarsY == "auto") then
  365. totalJarsY = emptyJarsY + 1
  366. end
  367.  
  368. if (refillScreenX == "auto") then
  369. -- Centers the refill screen on the monitor - the refill screen is 41 pixels wide hard coded
  370. refillScreenX = math.floor((monWidth - 41) / 2) + 1
  371.  
  372. -- The monitor is too small
  373. if (refillScreenX < 1) then
  374. print("The monitor is too small width wise to display all the needed refill screen information correctly.")
  375.  
  376. refillScreenX = 1
  377. end
  378. end
  379.  
  380. if (essentiaTopOffJarsX == "auto") then
  381. -- Centers the top off all essentia display on the monitor horizontally
  382. essentiaTopOffJarsX = math.floor((monWidth - ((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing)) / 2) + 1
  383.  
  384. -- The monitor is too small
  385. if (essentiaTopOffJarsX < 1) then
  386. print("The monitor is too small width wise to display all the needed top off all screen information correctly.")
  387.  
  388. essentiaTopOffJarsX = 1
  389. end
  390. end
  391.  
  392. if (essentiaTopOffLabelX == "auto") then
  393. essentiaTopOffLabelX = essentiaTopOffJarsX + math.floor(((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing) / 2) - math.ceil(string.len("Top off jars fill request") / 2)
  394. end
  395.  
  396. if (cancelTopOffJarsButtonX == "auto") then
  397. cancelTopOffJarsButtonX = math.floor((monWidth / 2) - cancelTopOffJarsButtonWidth) + 1
  398. end
  399.  
  400. if (cancelTopOffJarsButtonY == "auto") then
  401. cancelTopOffJarsButtonY = essentiaTopOffJarsY + essentiaColumnHeight + 1
  402. end
  403.  
  404. if (beginTopOffJarsFillButtonX == "auto") then
  405. beginTopOffJarsFillButtonX = math.floor((monWidth / 2)) + 2
  406. end
  407.  
  408. if (beginTopOffJarsFillButtonY == "auto") then
  409. beginTopOffJarsFillButtonY = essentiaTopOffJarsY + essentiaColumnHeight + 1
  410. end
  411.  
  412.  
  413. if (essentiaFillAllTo64X == "auto") then
  414. -- Centers the top off all essentia display on the monitor horizontally
  415. essentiaFillAllTo64X = math.floor((monWidth - ((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing)) / 2) + 1
  416.  
  417. -- The monitor is too small
  418. if (essentiaFillAllTo64X < 1) then
  419. print("The monitor is too small width wise to display all the needed fill all to 64 screen information correctly.")
  420.  
  421. essentiaFillAllTo64X = 1
  422. end
  423. end
  424.  
  425. if (essentiaFillAllTo64LabelX == "auto") then
  426. essentiaFillAllTo64LabelX = essentiaFillAllTo64X + math.floor(((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing) / 2) - math.ceil(string.len("Fill all jars to 64 request") / 2) + 1
  427. end
  428.  
  429. if (cancelFillAllTo64ButtonX == "auto") then
  430. cancelFillAllTo64ButtonX = math.floor((monWidth / 2) - cancelFillAllTo64ButtonWidth) + 1
  431. end
  432.  
  433. if (cancelFillAllTo64ButtonY == "auto") then
  434. cancelFillAllTo64ButtonY = essentiaFillAllTo64Y + essentiaColumnHeight + 1
  435. end
  436.  
  437. if (beginFillAllTo64ButtonX == "auto") then
  438. beginFillAllTo64ButtonX = math.floor((monWidth / 2)) + 2
  439. end
  440.  
  441. if (beginFillAllTo64ButtonY == "auto") then
  442. beginFillAllTo64ButtonY = essentiaFillAllTo64Y + essentiaColumnHeight + 1
  443. end
  444.  
  445.  
  446. if (essentiaFillAllTo128X == "auto") then
  447. -- Centers the top off all essentia display on the monitor horizontally
  448. essentiaFillAllTo128X = math.floor((monWidth - ((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing)) / 2) + 1
  449.  
  450. -- The monitor is too small
  451. if (essentiaFillAllTo128X < 1) then
  452. print("The monitor is too small width wise to display all the needed fill all to 128 screen information correctly.")
  453.  
  454. essentiaFillAllTo128X = 1
  455. end
  456. end
  457.  
  458. if (essentiaFillAllTo128LabelX == "auto") then
  459. essentiaFillAllTo128LabelX = essentiaFillAllTo128X + math.floor(((math.ceil(essentiasCount / essentiaColumnHeight) * (essentiaColumnWidth + essentiaColumnSpacing)) - essentiaColumnSpacing) / 2) - math.ceil(string.len("Fill all jars to 128 request") / 2) + 1
  460. end
  461.  
  462. if (cancelFillAllTo128ButtonX == "auto") then
  463. cancelFillAllTo128ButtonX = math.floor((monWidth / 2) - cancelFillAllTo128ButtonWidth) + 1
  464. end
  465.  
  466. if (cancelFillAllTo128ButtonY == "auto") then
  467. cancelFillAllTo128ButtonY = essentiaFillAllTo128Y + essentiaColumnHeight + 1
  468. end
  469.  
  470. if (beginFillAllTo128ButtonX == "auto") then
  471. beginFillAllTo128ButtonX = math.floor((monWidth / 2)) + 2
  472. end
  473.  
  474. if (beginFillAllTo128ButtonY == "auto") then
  475. beginFillAllTo128ButtonY = essentiaFillAllTo128Y + essentiaColumnHeight + 1
  476. end
  477.  
  478. if (burnScreenLabelX == "auto") then
  479. burnScreenLabelX = math.floor((monWidth / 2) - (string.len("Burn request information") / 2)) + 1
  480. end
  481.  
  482. if (serverFriendly == true) then
  483. serverFriendly = .05
  484. else
  485. serverFriendly = 0
  486. end
  487. end
  488.  
  489. function sortEss(t)
  490. local keys = {}
  491. local k, i
  492.  
  493. for k in pairs(t) do
  494. keys[#keys+1] = k
  495. end
  496. table.sort(keys)
  497.  
  498. i = 0
  499. return function()
  500. i = i + 1
  501. if keys[i] then
  502. return keys[i], t[keys[i]]
  503. end
  504. end
  505. end
  506.  
  507. function scanJars()
  508. local myEmptyJars = 0
  509. local myTotalJars = 0
  510. local jars = {}
  511. local jarID = 1
  512. local aspect
  513. local aspectCount
  514. local i, j
  515.  
  516. for i,j in ipairs(peripheral.getNames()) do
  517.  
  518. if (peripheral.getType(j) == "tt_aspectContainer") or (peripheral.getType(j) == "tilejar") or (peripheral.getType(j) == "tilejarvoid") then
  519. aspect = peripheral.call(j, "getAspects")
  520.  
  521. if (aspect[1] ~= nill) then
  522. for k,v in pairs(aspect) do
  523. aspectCount = aspect[1]["quantity"]
  524. aspect = string.lower(aspect[1]["name"])
  525. end
  526. else
  527. aspectCount = 0
  528. end
  529.  
  530. if (aspectCount > 0) then
  531. jars[jarID] = {}
  532. jars[jarID]["aspect"] = aspect
  533. jars[jarID]["count"] = aspectCount
  534. jarID = jarID + 1
  535. myTotalJars = myTotalJars + 1
  536. elseif (aspectCount == 0) then
  537. myEmptyJars = myEmptyJars + 1
  538. myTotalJars = myTotalJars + 1
  539. end
  540. end
  541. end
  542.  
  543. emptyJars = myEmptyJars
  544. totalJars = myTotalJars
  545.  
  546. return jars
  547. end
  548.  
  549. function updateEssentia()
  550. local myJars
  551. local myEssentia
  552. local i, j
  553.  
  554. myJars = scanJars()
  555. myEssentia = essentiaList()
  556.  
  557. for i in pairs(myJars) do
  558. myEssentia[myJars[i]["aspect"]] = myEssentia[myJars[i]["aspect"]] + myJars[i]["count"]
  559. end
  560.  
  561. for i,j in sortEss(myEssentia) do
  562. if (essentia[i] ~= j) then
  563. essentia[i] = j
  564. end
  565. end
  566. end
  567.  
  568. function printEssentia()
  569. local inactiveTextColor
  570. local buttonName
  571. local x, y
  572. local i, j
  573.  
  574. x = essentiaX
  575. y = essentiaY
  576.  
  577. for i,j in sortEss(essentia) do
  578. inactiveTextColor = getEssentiaColor(j)
  579.  
  580. buttonName = i .. string.sub(padding, 1, essentiaColumnWidth - (string.len(i) + string.len(pad(j)))) .. pad(j)
  581. button.addButton(buttonName, essentiaButton, buttonName, x, y, essentiaColumnWidth, 0, essentiaInactiveBackColor, essentiaActiveBackColor, inactiveTextColor, essentiaActiveTextColor)
  582.  
  583. if (y < (essentiaY + essentiaColumnHeight - 1)) then
  584. y = y + 1
  585. else
  586. y = essentiaY
  587. x = x + essentiaColumnWidth + essentiaColumnSpacing
  588. end
  589. end
  590. end
  591.  
  592. function getEssentiaColor(currentEssentiaAmount)
  593. local myColor
  594.  
  595. if (currentEssentiaAmount == 0) then
  596. myColor = essentiaInactiveTextColorEmpty
  597. elseif (currentEssentiaAmount <= 20) then
  598. myColor = essentiaInactiveTextColorLow
  599. elseif (currentEssentiaAmount < 100 and currentEssentiaAmount > 20) then
  600. myColor = essentiaInactiveTextColorMedium
  601. else
  602. myColor = essentiaInactiveTextColorHigh
  603. end
  604.  
  605. return myColor
  606. end
  607.  
  608. function printEmptyJars()
  609. m.setTextColor(emptyJarsTextColor)
  610. m.setBackgroundColor(emptyJarsBackColor)
  611.  
  612. m.setCursorPos(emptyJarsX, emptyJarsY)
  613. m.write("Empty jars:" .. string.sub(padding, 1, essentiaColumnWidth - (string.len("Empty jars:") + string.len(pad(emptyJars))) - 1) .. pad(emptyJars))
  614. end
  615.  
  616. function printTotalJars()
  617. m.setTextColor(totalJarsTextColor)
  618. m.setBackgroundColor(totalJarsBackColor)
  619.  
  620. m.setCursorPos(totalJarsX, totalJarsY)
  621. m.write("Total jars:" .. string.sub(padding, 1, essentiaColumnWidth - (string.len("Total jars:") + string.len(pad(totalJars))) - 1) .. pad(totalJars))
  622. end
  623.  
  624. function pad(number, amount)
  625. local myPadding = "000000"
  626. local paddingRequired = 0
  627.  
  628. if not (amount) then
  629. amount = numberPadding
  630. end
  631.  
  632. paddingRequired = amount - string.len(tostring(number))
  633.  
  634. if (paddingRequired ~= 0 and paddingRequired > 0) then
  635. return string.sub(myPadding, 1, paddingRequired) .. tostring(number)
  636. end
  637.  
  638. return tostring(number)
  639. end
  640.  
  641. function getClick()
  642. local event
  643. local side
  644. local x,y
  645.  
  646. event,side,x,y = os.pullEvent()
  647.  
  648. if (event == "monitor_touch") then
  649. button.checkxy(x,y)
  650. end
  651. end
  652.  
  653. function refresh(buttonToFlash)
  654. -- Flash the refresh button
  655. if (buttonToFlash) then
  656. button.flash(buttonToFlash)
  657. end
  658.  
  659. -- Clear the screen and button list
  660. m.setBackgroundColor(monBackColor)
  661. m.clear()
  662. button.clear()
  663.  
  664. -- Scan and print the essentias
  665. updateEssentia()
  666. printEssentia()
  667.  
  668. -- Print the number of empty jars
  669. printEmptyJars()
  670.  
  671. -- Print the number of jars
  672. printTotalJars()
  673.  
  674. -- Create the refresh button
  675. addRefreshButton()
  676.  
  677. -- Create the top off all jars button
  678. addTopoffAllJarsButton()
  679.  
  680. -- Create the fill all to 64 button
  681. addFillAllTo64Button()
  682.  
  683. -- Create the fill all to 128 button
  684. addFillAllTo128Button()
  685.  
  686. -- Create the other button
  687. addOtherOptionsButton()
  688.  
  689. -- Draw all the buttons on the screen
  690. button.drawButtons()
  691. end
  692.  
  693. function addRefreshButton()
  694. button.addButton("Refresh", refresh, "Refresh", refreshButtonX, refreshButtonY, refreshButtonWidth, refreshButtonHeight, refreshInactiveBackColor, refreshActiveBackColor, refreshInactiveTextColor, refreshActiveTextColor)
  695. end
  696.  
  697. function addTopoffAllJarsButton()
  698. button.addButton("Top off all jars", topoffAllJars, "Top off all jars", topoffAllButtonX, topoffAllButtonY, topoffAllButtonWidth, topoffAllButtonHeight, topoffAllJarsInactiveBackColor, topoffAllJarsActiveBackColor, topoffAllJarsInactiveTextColor, topoffAllJarsActiveTextColor)
  699. end
  700.  
  701. function addFillAllTo64Button()
  702. button.addButton("Fill all to 64", fillAllTo64, "Fill all to 64", fillAllTo64ButtonX, fillAllTo64ButtonY, fillAllTo64ButtonWidth, fillAllTo64ButtonHeight, fillAllTo64InactiveBackColor, fillAllTo64ActiveBackColor, fillAllTo64InactiveTextColor, fillAllTo64ActiveTextColor)
  703. end
  704.  
  705. function addFillAllTo128Button()
  706. button.addButton("Fill all to 128", fillAllTo128, "Fill all to 128", fillAllTo128ButtonX, fillAllTo128ButtonY, fillAllTo128ButtonWidth, fillAllTo128ButtonHeight, fillAllTo128InactiveBackColor, fillAllTo128ActiveBackColor, fillAllTo128InactiveTextColor, fillAllTo128ActiveTextColor)
  707. end
  708.  
  709. function addOtherOptionsButton()
  710. button.addButton("Reboot", otherOptions, "Reboot", otherOptionsButtonX, otherOptionsButtonY, otherOptionsButtonWidth, otherOptionsButtonHeight, otherOptionsButtonInactiveBackColor, otherOptionsButtonActiveBackColor, otherOptionsButtonInactiveTextColor, otherOptionsButtonActiveTextColor)
  711. end
  712.  
  713. function otherOptions(buttonName)
  714. button.flash(buttonName)
  715. os.reboot()
  716. end
  717.  
  718. function fillAllTo64(buttonName)
  719. local aspect
  720. local count
  721. local essentiaToFill
  722. local haveEssentiaToFill
  723. local myEmptyJars
  724.  
  725. -- The button is erased if jars to top off are found else it's toggled back below.
  726. button.toggleButton(buttonName, true)
  727. sleep(.15)
  728.  
  729. essentiaToFill = essentiaList()
  730. myEmptyJars = emptyJars
  731. haveEssentiaToFill = false
  732.  
  733. -- Update the list of essentias in all jars
  734. updateEssentia()
  735.  
  736. -- Checks each jar to see if any of them aren't full.
  737. for aspect,count in pairs(essentia) do
  738. if (count < 64) then
  739. if (count == 0) then
  740. myEmptyJars = myEmptyJars - 1
  741. end
  742.  
  743. essentiaToFill[aspect] = 64 - count
  744. haveEssentiaToFill = true
  745. end
  746. end
  747.  
  748. if (haveEssentiaToFill == true) then
  749. -- Not all essentia is at 64 or more, display the found essentias and confirm filling.
  750. showFillAllTo64(essentiaToFill, myEmptyJars)
  751. else
  752. -- All essentia are above 64, rename the button and hold the text for a second so it can be read before reverting to the normal text and color.
  753. button.renameButton(buttonName, "All jars at 64", true)
  754. sleep(2.5)
  755. button.toggleButton("All jars at 64", false)
  756. button.renameButton("All jars at 64", buttonName, true)
  757. end
  758. end
  759.  
  760. function showFillAllTo64(essentiaList, emptyJarsAfterFill)
  761. local x, y
  762. local myEssentia
  763. local amount
  764. local errorText
  765.  
  766. fillAllTo64EssentiaList = essentiaList
  767.  
  768. -- Clear the screen and button list
  769. m.setBackgroundColor(monBackColor)
  770. m.clear()
  771. button.clear()
  772.  
  773. x = essentiaFillAllTo64X
  774. y = essentiaFillAllTo64Y
  775.  
  776. m.setBackgroundColor(fillAllTo64EssentiaLabelBackColor)
  777. m.setTextColor(fillAllTo64EssentiaLabelTextColor)
  778.  
  779. m.setCursorPos(essentiaFillAllTo64LabelX, essentiaFillAllTo64LabelY)
  780. m.write("Fill all jars to 64 request")
  781.  
  782. m.setBackgroundColor(fillAllTo64EssentiaBackColor)
  783.  
  784. for myEssentia,amount in sortEss(essentiaList) do
  785. if (amount == 0) then
  786. m.setTextColor(fillAllTo64EssentiaTextColorZero)
  787. else
  788. m.setTextColor(fillAllTo64EssentiaTextColorNonZero)
  789. end
  790.  
  791. m.setCursorPos(x, y)
  792. m.write(string.upper(string.sub(myEssentia, 1, 1)) .. string.sub(myEssentia, 2) .. string.sub(padding, 1, essentiaColumnWidth - (string.len(myEssentia) + string.len(pad(amount)))) .. pad(amount))
  793.  
  794. if (y < (essentiaFillAllTo64Y + essentiaColumnHeight - 1)) then
  795. y = y + 1
  796. else
  797. y = essentiaFillAllTo64Y
  798. x = x + essentiaColumnWidth + essentiaColumnSpacing
  799. end
  800. end
  801.  
  802. button.addButton("Cancel filling", cancelFillAllTo64Button, "Cancel filling", cancelFillAllTo64ButtonX, cancelFillAllTo64ButtonY, cancelFillAllTo64ButtonWidth, cancelFillAllTo64ButtonHeight, cancelFillAllTo64ButtonInactiveBackColor, cancelFillAllTo64ButtonActiveBackColor, cancelFillAllTo64ButtonInactiveTextColor, cancelFillAllTo64ButtonActiveTextColor)
  803. button.addButton("Begin filling", beginFillAllTo64Button, "Begin filling", beginFillAllTo64ButtonX, beginFillAllTo64ButtonY, beginFillAllTo64ButtonWidth, beginFillAllTo64ButtonHeight, beginFillAllTo64ButtonInactiveBackColor, beginFillAllTo64ButtonActiveBackColor, beginFillAllTo64ButtonInactiveTextColor, beginFillAllTo64ButtonActiveTextColor)
  804.  
  805. if (emptyJarsAfterFill < 0) then
  806. errorText = "NOT ENOUGH EMPTY JARS; HAVE " .. emptyJars .. " NEED " .. (emptyJars - emptyJarsAfterFill)
  807. m.setCursorPos(essentiaFillAllTo64LabelX + math.floor(string.len("Fill all jars to 64 request") / 2) - math.floor(string.len(errorText) / 2), essentiaFillAllTo64LabelY + 1)
  808. m.setTextColor(colors.red)
  809. m.write(errorText)
  810. button.disableButton("Begin filling")
  811. end
  812.  
  813. button.drawButtons()
  814. end
  815.  
  816. function fillAllTo128(buttonName)
  817. local aspect
  818. local count
  819. local essentiaToFill
  820. local haveEssentiaToFill
  821. local myEmptyJars
  822. local myJars
  823. local jar
  824. local essentiaJarCount
  825.  
  826. -- The button is erased if jars to top off are found else it's toggled back below.
  827. button.toggleButton(buttonName, true)
  828. sleep(.15)
  829.  
  830. essentiaToFill = essentiaList()
  831. essentiaJarCount = essentiaList()
  832. myJars = scanJars()
  833. myEmptyJars = emptyJars
  834. haveEssentiaToFill = false
  835.  
  836. -- Count how many jars of each aspect exist
  837. for jar in pairs(myJars) do
  838. essentiaJarCount[myJars[jar]["aspect"]] = essentiaJarCount[myJars[jar]["aspect"]] + 1
  839. end
  840.  
  841. -- Update the list of essentias in all jars
  842. updateEssentia()
  843.  
  844. -- Checks each jar to see if any of them aren't full.
  845. for aspect,count in pairs(essentia) do
  846. if (count < 128) then
  847. if (count == 0) then
  848. myEmptyJars = myEmptyJars - 2
  849. else
  850. if (essentiaJarCount[aspect] == 1) then
  851. if (count <= 64) then -- if it's above 64 the jar is probably an advanced thaumaturgy jar that holds 256
  852. myEmptyJars = myEmptyJars - 1
  853. end
  854. end
  855. end
  856.  
  857. essentiaToFill[aspect] = 128 - count
  858. haveEssentiaToFill = true
  859. end
  860. end
  861.  
  862. if (haveEssentiaToFill == true) then
  863. -- Jars have been found that aren't full, display the found jars and confirm filling.
  864. showFillAllTo128(essentiaToFill, myEmptyJars)
  865. else
  866. -- All essentia are above 128, rename the button and hold the text for a second so it can be read before reverting to the normal text and color.
  867. button.renameButton(buttonName, "All jars at 128", true)
  868. sleep(2.5)
  869. button.toggleButton("All jars at 128", false)
  870. button.renameButton("All jars at 128", buttonName, true)
  871. end
  872. end
  873.  
  874. function showFillAllTo128(essentiaList, emptyJarsAfterFill)
  875. local x, y
  876. local myEssentia
  877. local amount
  878. local errorText
  879.  
  880. fillAllTo128EssentiaList = essentiaList
  881.  
  882. -- Clear the screen and button list
  883. m.setBackgroundColor(monBackColor)
  884. m.clear()
  885. button.clear()
  886.  
  887. x = essentiaFillAllTo128X
  888. y = essentiaFillAllTo128Y
  889.  
  890. m.setBackgroundColor(fillAllTo128EssentiaLabelBackColor)
  891. m.setTextColor(fillAllTo128EssentiaLabelTextColor)
  892. m.setCursorPos(essentiaFillAllTo128LabelX, essentiaFillAllTo128LabelY)
  893. m.write("Fill all jars to 128 request")
  894.  
  895. m.setBackgroundColor(fillAllTo128EssentiaBackColor)
  896.  
  897. for myEssentia,amount in sortEss(essentiaList) do
  898. if (amount == 0) then
  899. m.setTextColor(fillAllTo128EssentiaTextColorZero)
  900. else
  901. m.setTextColor(fillAllTo128EssentiaTextColorNonZero)
  902. end
  903.  
  904. m.setCursorPos(x, y)
  905. m.write(string.upper(string.sub(myEssentia, 1, 1)) .. string.sub(myEssentia, 2) .. string.sub(padding, 1, essentiaColumnWidth - (string.len(myEssentia) + string.len(pad(amount)))) .. pad(amount))
  906.  
  907. if (y < (essentiaFillAllTo128Y + essentiaColumnHeight - 1)) then
  908. y = y + 1
  909. else
  910. y = essentiaFillAllTo128Y
  911. x = x + essentiaColumnWidth + essentiaColumnSpacing
  912. end
  913. end
  914.  
  915. button.addButton("Cancel filling", cancelFillAllTo128Button, "Cancel filling", cancelFillAllTo128ButtonX, cancelFillAllTo128ButtonY, cancelFillAllTo128ButtonWidth, cancelFillAllTo128ButtonHeight, cancelFillAllTo128ButtonInactiveBackColor, cancelFillAllTo128ButtonActiveBackColor, cancelFillAllTo128ButtonInactiveTextColor, cancelFillAllTo128ButtonActiveTextColor)
  916. button.addButton("Begin filling", beginFillAllTo128Button, "Begin filling", beginFillAllTo128ButtonX, beginFillAllTo128ButtonY, beginFillAllTo128ButtonWidth, beginFillAllTo128ButtonHeight, beginFillAllTo128ButtonInactiveBackColor, beginFillAllTo128ButtonActiveBackColor, beginFillAllTo128ButtonInactiveTextColor, beginFillAllTo128ButtonActiveTextColor)
  917.  
  918. if (emptyJarsAfterFill < 0) then
  919. errorText = "NOT ENOUGH EMPTY JARS; HAVE " .. emptyJars .. " NEED " .. (emptyJars - emptyJarsAfterFill)
  920.  
  921. m.setCursorPos(essentiaFillAllTo128LabelX + math.floor(string.len("Fill all jars to 128 request") / 2) - math.floor(string.len(errorText) / 2), essentiaFillAllTo128LabelY + 1)
  922. m.setTextColor(colors.red)
  923. m.write(errorText)
  924. button.disableButton("Begin filling")
  925. end
  926.  
  927. button.drawButtons()
  928. end
  929.  
  930. function topoffAllJars(buttonName)
  931. local myJars
  932. local jar
  933. local essentiaToFill
  934. local haveEssentiaToFill
  935.  
  936. -- The button is erased if jars to top off are found else it's toggled back below.
  937. button.toggleButton(buttonName, true)
  938. sleep(.15)
  939.  
  940. -- Get a table of all the essentia jars
  941. myJars = scanJars()
  942. essentiaToFill = essentiaList()
  943. haveEssentiaToFill = false
  944.  
  945. -- Checks each jar to see if any of them aren't full.
  946. for jar in pairs(myJars) do
  947. if (myJars[jar]["count"] < 64) then
  948. essentiaToFill[myJars[jar]["aspect"]] = essentiaToFill[myJars[jar]["aspect"]] + (64 - myJars[jar]["count"])
  949. haveEssentiaToFill = true
  950. elseif (myJars[jar]["count"] > 64) then -- Advanced Thaumaturgy jars hold 256
  951. essentiaToFill[myJars[jar]["aspect"]] = essentiaToFill[myJars[jar]["aspect"]] + (256 - myJars[jar]["count"])
  952. haveEssentiaToFill = true
  953. end
  954. end
  955.  
  956. if (haveEssentiaToFill == true) then
  957. -- Jars have been found that aren't full, display the found jars and confirm filling.
  958. showTopOffAllJars(essentiaToFill)
  959. else
  960. -- No jars are found, rename the button and hold the text for a second so it can be read before reverting to the normal text and color.
  961. button.renameButton(buttonName, "All jars full", true)
  962. sleep(2.5)
  963. button.toggleButton("All jars full", false)
  964. button.renameButton("All jars full", buttonName, true)
  965. end
  966. end
  967.  
  968. function showTopOffAllJars(essentiaList)
  969. local x, y
  970. local myEssentia
  971. local essentiaText
  972. local amount
  973.  
  974. topOffJarsEssentiaList = essentiaList
  975.  
  976. -- Clear the screen and button list
  977. m.setBackgroundColor(monBackColor)
  978. m.clear()
  979. button.clear()
  980.  
  981. x = essentiaTopOffJarsX
  982. y = essentiaTopOffJarsY
  983.  
  984. m.setBackgroundColor(topOffAllJarsEssentiaLabelBackColor)
  985. m.setTextColor(topOffAllJarsEssentiaLabelTextColor)
  986. m.setCursorPos(essentiaTopOffLabelX, essentiaTopOffLabelY)
  987. m.write("Top off jars fill request")
  988.  
  989. m.setBackgroundColor(topOffAllJarsEssentiaBackColor)
  990.  
  991. for myEssentia,amount in sortEss(essentiaList) do
  992. if (amount == 0) then
  993. m.setTextColor(topOffAllJarsEssentiaTextColorZero)
  994. else
  995. m.setTextColor(topOffAllJarsEssentiaTextColorNonZero)
  996. end
  997.  
  998. m.setCursorPos(x, y)
  999. m.write(string.upper(string.sub(myEssentia, 1, 1)) .. string.sub(myEssentia, 2) .. string.sub(padding, 1, essentiaColumnWidth - (string.len(myEssentia) + string.len(pad(amount)))) .. pad(amount))
  1000.  
  1001. if (y < (essentiaTopOffJarsY + essentiaColumnHeight - 1)) then
  1002. y = y + 1
  1003. else
  1004. y = essentiaTopOffJarsY
  1005. x = x + essentiaColumnWidth + essentiaColumnSpacing
  1006. end
  1007. end
  1008.  
  1009. button.addButton("Cancel filling", cancelTopOffJarsButton, "Cancel filling", cancelTopOffJarsButtonX, cancelTopOffJarsButtonY, cancelTopOffJarsButtonWidth, cancelTopOffJarsButtonHeight, cancelTopOffJarsButtonInactiveBackColor, cancelTopOffJarsButtonActiveBackColor, cancelTopOffJarsButtonInactiveTextColor, cancelTopOffJarsButtonActiveTextColor)
  1010. button.addButton("Begin filling", beginTopOffJarsFillButton, "Begin filling", beginTopOffJarsFillButtonX, beginTopOffJarsFillButtonY, beginTopOffJarsFillButtonWidth, beginTopOffJarsFillButtonHeight, beginTopOffJarsFillButtonInactiveBackColor, beginTopOffJarsFillButtonActiveBackColor, beginTopOffJarsFillButtonInactiveTextColor, beginTopOffJarsFillButtonActiveTextColor)
  1011.  
  1012. button.drawButtons()
  1013. end
  1014.  
  1015. function cancelTopOffJarsButton(buttonName)
  1016. button.flash(buttonName)
  1017. topOffJarsEssentiaList = nil
  1018. refresh()
  1019. end
  1020.  
  1021. function beginTopOffJarsFillButton(buttonName)
  1022. local beanType
  1023. local amountToBurn
  1024. local burnCount
  1025.  
  1026. button.flash(buttonName)
  1027.  
  1028. drawBurnMainScreen()
  1029. allBurnResults = {}
  1030. for beanType,amountToBurn in sortEss(topOffJarsEssentiaList) do
  1031. if (amountToBurn ~= 0) then
  1032. updateBurnBeanType(beanType)
  1033. burnCount = burnBeans(beanType, amountToBurn)
  1034.  
  1035.  
  1036. print("")
  1037. print(beanType)
  1038. print("Burned: " .. burnCount)
  1039.  
  1040. if (burnCount == -100) then
  1041. print(translateBurnResultToText(burnCount))
  1042. break
  1043. end
  1044. end
  1045. end
  1046.  
  1047. refresh()
  1048. end
  1049.  
  1050. function cancelFillAllTo64Button(buttonName)
  1051. button.flash(buttonName)
  1052. fillAllTo64EssentiaList = nil
  1053. refresh()
  1054. end
  1055.  
  1056. function beginFillAllTo64Button(buttonName)
  1057. local beanType
  1058. local amountToBurn
  1059. local burnCount
  1060.  
  1061. button.flash(buttonName)
  1062.  
  1063. drawBurnMainScreen()
  1064. allBurnResults = {}
  1065. for beanType,amountToBurn in sortEss(fillAllTo64EssentiaList) do
  1066. if (amountToBurn ~= 0) then
  1067. updateBurnBeanType(beanType)
  1068. burnCount = burnBeans(beanType, amountToBurn)
  1069.  
  1070.  
  1071. print("")
  1072. print(beanType)
  1073. print("Burned: " .. burnCount)
  1074.  
  1075. if (burnCount == -100) then
  1076. print(translateBurnResultToText(burnCount))
  1077. break
  1078. end
  1079. end
  1080. end
  1081.  
  1082. refresh()
  1083. end
  1084.  
  1085. function cancelFillAllTo128Button(buttonName)
  1086. button.flash(buttonName)
  1087. fillAllTo128EssentiaList = nil
  1088. refresh()
  1089. end
  1090.  
  1091. function beginFillAllTo128Button(buttonName)
  1092. local beanType
  1093. local amountToBurn
  1094. local burnCount
  1095. local bk, bv
  1096.  
  1097. button.flash(buttonName)
  1098.  
  1099. drawBurnMainScreen()
  1100. for beanType,amountToBurn in sortEss(fillAllTo128EssentiaList) do
  1101. if (amountToBurn ~= 0) then
  1102. updateBurnBeanType(beanType)
  1103. burnCount = burnBeans(beanType, amountToBurn)
  1104.  
  1105. print("")
  1106. print(beanType)
  1107. print("Burned: " .. burnCount)
  1108.  
  1109. if (burnCount == -100) then
  1110. print(translateBurnResultToText(burnCount))
  1111. break
  1112. end
  1113. end
  1114. end
  1115.  
  1116. refresh()
  1117. end
  1118.  
  1119. function essentiaButton(which)
  1120. local buttonEssentiaName
  1121. local availableEssentiaSpace = 0
  1122. local myJars
  1123. local essentiaCount = 0
  1124. local i
  1125.  
  1126. myJars = scanJars()
  1127. button.flash(which)
  1128. buttonEssentiaName = string.sub(which, 1, string.find(which, " ") - 1)
  1129.  
  1130. for i in pairs(myJars) do
  1131. if (myJars[i]["aspect"] == buttonEssentiaName) then
  1132. availableEssentiaSpace = availableEssentiaSpace + (64 - myJars[i]["count"])
  1133. essentiaCount = essentiaCount + myJars[i]["count"]
  1134. end
  1135. end
  1136.  
  1137. showRefillEssentia(buttonEssentiaName, essentiaCount, availableEssentiaSpace)
  1138. end
  1139.  
  1140. function showRefillEssentia(essentiaName, essentiaCount, availableEssentiaSpace)
  1141. local fillingText
  1142.  
  1143. fillEssentia = essentiaName
  1144. fillCurrentAmount = essentiaCount
  1145. fillAmount = 0
  1146. fillAvailableSpace = availableEssentiaSpace
  1147.  
  1148. button.clear()
  1149. m.setBackgroundColor(monBackColor)
  1150. m.clear()
  1151.  
  1152. m.setTextColor(refillLabelTextColor)
  1153. fillingText = "Refilling: " .. string.upper(string.sub(fillEssentia, 1, 1)) .. string.sub(fillEssentia, 2)
  1154. m.setCursorPos(refillScreenX + math.floor((41 - string.len(fillingText)) / 2), refillScreenY)
  1155. m.write(fillingText)
  1156.  
  1157. if (fillAvailableSpace == 0 and emptyJars == 0) then
  1158. m.setCursorPos(refillScreenX + math.floor((41 - string.len("ALL JARS USED OR FULL")) / 2), refillScreenY + 1)
  1159. m.setTextColor(colors.red)
  1160. m.write("ALL JARS USED OR FULL")
  1161. end
  1162.  
  1163.  
  1164. m.setTextColor(refillCurrentAmountTextColor)
  1165. m.setCursorPos(refillScreenX, refillScreenY + 2)
  1166. m.write("Currently contains:" .. string.sub(padding, 1, 41 - (string.len("Currently contains:") + string.len(pad(fillCurrentAmount)))) .. pad(fillCurrentAmount))
  1167.  
  1168. m.setTextColor(refillAddingAmountTextColor)
  1169. m.setCursorPos(refillScreenX, refillScreenY + 3)
  1170. m.write("Currently adding:" .. string.sub(padding, 1, 41 - (string.len("Currently adding:") + string.len(pad(fillAmount)))) .. pad(fillAmount))
  1171.  
  1172. m.setTextColor(getEssentiaColor(fillCurrentAmount))
  1173. m.setCursorPos(refillScreenX, refillScreenY + 4)
  1174. m.write("Total after filling:" .. string.sub(padding, 1, 41 - (string.len("Total after filling:") + string.len(pad(fillCurrentAmount)))) .. pad(fillCurrentAmount))
  1175.  
  1176. button.addButton("+1", addEssentia, "+1", refillScreenX, refillScreenY + 6, 10, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1177. button.addButton("+5", addEssentia, "+5", refillScreenX + 11, refillScreenY + 6, 9, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1178. button.addButton("+10", addEssentia, "+10", refillScreenX + 21, refillScreenY + 6, 9, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1179. button.addButton("+64", addEssentia, "+64", refillScreenX + 31, refillScreenY + 6, 10, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1180.  
  1181. button.addButton("-1", addEssentia, "-1", refillScreenX, refillScreenY + 10, 10, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1182. button.addButton("-5", addEssentia, "-5", refillScreenX + 11, refillScreenY + 10, 9, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1183. button.addButton("-10", addEssentia, "-10", refillScreenX + 21, refillScreenY + 10, 9, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1184. button.addButton("-64", addEssentia, "-64", refillScreenX + 31, refillScreenY + 10, 10, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1185.  
  1186. button.addButton("Top off jar(s)", addEssentia, "Top off jar(s)", refillScreenX, refillScreenY + 14, 20, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1187. button.addButton("Max fill", addEssentia, "Max fill", refillScreenX + 21, refillScreenY + 14, 20, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1188.  
  1189. button.addButton("Cancel filling", refresh, "Cancel filling", refillScreenX, refillScreenY + 18, 41, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1190.  
  1191. button.addButton("Begin filling", buttonBeginRefill, "Begin filling", refillScreenX, refillScreenY + 22, 41, 2, refillButtonsInactiveBackColor, refillButtonsActiveBackColor, refillButtonsInactiveTextColor, refillButtonsActiveTextColor)
  1192.  
  1193. button.drawButtons()
  1194. end
  1195.  
  1196. function addEssentia(amount)
  1197. local myFillAmount
  1198.  
  1199. myFillAmount = fillAmount
  1200.  
  1201. --print(fillAmount .. " before")
  1202.  
  1203. button.flash(amount)
  1204.  
  1205. if (amount == "Top off jar(s)") then
  1206. fillAmount = fillAvailableSpace
  1207. elseif (amount == "Max fill") then
  1208. fillAmount = fillAvailableSpace + (emptyJars * 64)
  1209. else
  1210. fillAmount = fillAmount + tonumber(amount)
  1211. end
  1212.  
  1213. if (fillAmount < 0) then
  1214. --print(fillAmount .. " is less than 0 - setting to 0")
  1215. fillAmount = 0
  1216. elseif (fillAmount > fillAvailableSpace) then
  1217. if (emptyJars > 0) then
  1218. if (fillAmount > (fillAvailableSpace + (emptyJars * 64))) then
  1219. fillAmount = fillAvailableSpace + (emptyJars * 64)
  1220. end
  1221. else
  1222. fillAmount = fillAvailableSpace
  1223. end
  1224. end
  1225.  
  1226. if (fillAmount ~= myFillAmount) then
  1227. m.setBackgroundColor(monBackColor)
  1228.  
  1229. m.setTextColor(refillAddingAmountTextColor)
  1230. m.setCursorPos(refillScreenX, refillScreenY + 3)
  1231. m.write("Currently adding:" .. string.sub(padding, 1, 41 - (string.len("Currently adding:") + string.len(pad(fillAmount)))) .. pad(fillAmount))
  1232.  
  1233. m.setTextColor(getEssentiaColor(fillCurrentAmount + fillAmount))
  1234. m.setCursorPos(refillScreenX, refillScreenY + 4)
  1235. m.write("Total after filling:" .. string.sub(padding, 1, 41 - (string.len("Total after filling:") + string.len(pad(fillCurrentAmount + fillAmount)))) .. pad(fillCurrentAmount + fillAmount))
  1236. end
  1237. end
  1238.  
  1239. -- Standard refill button
  1240. function buttonBeginRefill(buttonName)
  1241. local burnCount
  1242.  
  1243. button.flash(buttonName)
  1244.  
  1245. if (fillAmount ~= 0) then
  1246. drawBurnMainScreen()
  1247. updateBurnBeanType(fillEssentia)
  1248. burnCount = burnBeans(fillEssentia, fillAmount)
  1249.  
  1250. print("")
  1251. print("Burned: " .. burnCount)
  1252.  
  1253. print(translateBurnResultToText(burnCount))
  1254. end
  1255.  
  1256. refresh()
  1257. end
  1258.  
  1259. function translateBurnResultToText(burnResult)
  1260. if (burnResult == -100) then
  1261. return "Aborted"
  1262. end
  1263. end
  1264.  
  1265. maximumLength = 0
  1266.  
  1267. function updateBurnStatus(newStatus)
  1268. local monWidth, monHeight
  1269. local pos
  1270. local writeWidth
  1271. local y
  1272.  
  1273. monWidth, monHeight = m.getSize()
  1274. writeWidth = monWidth - 10
  1275. pos = 1
  1276.  
  1277. for y = 0, 3 do
  1278. m.setCursorPos(burnScreenStatusX, y + burnScreenStatusY + 2)
  1279. m.write(string.sub(padding, 1, writeWidth))
  1280. m.setCursorPos(burnScreenStatusX, y + burnScreenStatusY + 2)
  1281. m.write(string.sub(newStatus, pos, writeWidth + pos - 1))
  1282. pos = pos + writeWidth
  1283. end
  1284. end
  1285.  
  1286. function updateBurnBeanType(newBean)
  1287. m.setCursorPos(burnScreenBeanLabelX + string.len(burnScreenBeanTextLabel) + 1, burnScreenBeanLabelY)
  1288. m.write(string.upper(string.sub(newBean, 1, 1)) .. string.sub(newBean, 2) .. string.sub(padding, 1, 15 - string.len(newBean)))
  1289. end
  1290.  
  1291. function drawBurnMainScreen()
  1292. local monWidth
  1293. local monHeight
  1294. local x, y
  1295.  
  1296. -- Clears the screen before creating the information burn screen
  1297. button.clear()
  1298. m.setBackgroundColor(monBackColor)
  1299. m.clear()
  1300.  
  1301. -- Draws the border on the screen
  1302. monWidth,monHeight = m.getSize()
  1303. m.setBackgroundColor(burnScreenBorderColor)
  1304.  
  1305. m.setCursorPos(1, 1)
  1306. m.write(string.sub(padding, 1, monWidth))
  1307. m.setCursorPos(1, monHeight)
  1308. m.write(string.sub(padding, 1, monWidth))
  1309.  
  1310. for y = 0, monHeight do
  1311. m.setCursorPos(1, y)
  1312. m.write(" ")
  1313. m.setCursorPos(monWidth - 1, y)
  1314. m.write(" ")
  1315. end
  1316.  
  1317. -- Draws the label text
  1318. m.setBackgroundColor(burnScreenLabelBackColor)
  1319. m.setTextColor(burnScreenLabelTextColor)
  1320. m.setCursorPos(burnScreenLabelX, burnScreenLabelY)
  1321. m.write("Burn request information")
  1322.  
  1323. m.setBackgroundColor(burnScreenBeanLabelBackColor)
  1324. m.setTextColor(burnScreenBeanLabelTextColor)
  1325. m.setCursorPos(burnScreenBeanLabelX, burnScreenBeanLabelY)
  1326. m.write(burnScreenBeanTextLabel)
  1327.  
  1328. m.setBackgroundColor(burnScreenBeanLabelBackColor)
  1329. m.setTextColor(burnScreenBeanLabelTextColor)
  1330. m.setCursorPos(burnScreenStatusX, burnScreenStatusY)
  1331. m.write(burnScreenStatusTextLabel)
  1332. end
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396. --[[
  1397.  
  1398. The functions below are used to track mana beans in connected chests and put them in the alchemical furnaces.
  1399.  
  1400. --]]
  1401.  
  1402. function executeBurn(chest, beanType, amount, returnIfStuck)
  1403. local amountBurned
  1404. local stack
  1405. local slot
  1406. local direction
  1407. local directions
  1408. local aspect
  1409. local count
  1410. local validBeanCount
  1411. local waitingForBeans
  1412. local amountToBurn
  1413. local waitingForFurnace
  1414.  
  1415. -- No beans for this type have ever been found.
  1416. if (chests[chest][beanType] == nil) then
  1417. return 0
  1418. end
  1419.  
  1420. if (returnIfStuck ~= false and returnIfStuck ~= true) then
  1421. returnIfStuck = false
  1422. end
  1423.  
  1424. updateBurnStatus("attempting to burn beans")
  1425.  
  1426. if (alchemicalFurnaceFromChestDirection == "auto") then
  1427. directions = validFurnaceDirections()
  1428. else
  1429. directions = {[alchemicalFurnaceFromChestDirection] = true}
  1430. end
  1431.  
  1432. amountToBurn = amount
  1433. waitingForBeans = 0
  1434. waitingForFurnace = 0
  1435.  
  1436. while (amountToBurn > 0) do
  1437. amountBurned = 0
  1438. for slot in pairs(chests[chest][beanType]) do
  1439. stack = peripheral.call(chests[chest]["peripheral name"], "getStackInSlot", slot)
  1440.  
  1441. if (stack ~= nil and stack.qty > 2) then
  1442. count = amountToBurn - amountBurned
  1443.  
  1444. if (count > (stack.qty - 2)) then
  1445. count = stack.qty - 2
  1446. waitingForBeans = 0
  1447. end
  1448. else
  1449. count = 0
  1450. end
  1451.  
  1452. if (count ~= 0) then
  1453. while (count > 0) do
  1454. for direction in pairs(directions) do
  1455. if (count > 0) then
  1456. burned = peripheral.call(chests[chest]["peripheral name"], "pushItemIntoSlot", direction, slot, count, 1)
  1457. count = count - burned
  1458. amountBurned = amountBurned + burned
  1459.  
  1460. if (burned ~= 0) then
  1461. waitingForFurnace = 0
  1462. end
  1463.  
  1464. sleep(serverFriendly)
  1465. end
  1466. end
  1467.  
  1468. if (count > 0) then
  1469. -- Failed to put all the beans into the attached furnace(s)
  1470. if (waitingForFurnace == 1 and returnIfStuck == true) then
  1471. return amount - (amountToBurn - amountBurned)
  1472. elseif (waitingForFurnace == 0) then
  1473. print("Furnace(s) full, waiting for furnace(s) to have room...")
  1474. updateBurnStatus("furnace(s) full, waiting for them to have room")
  1475. end
  1476.  
  1477. waitingForFurnace = 1
  1478. sleep(1)
  1479. end
  1480. end
  1481. else
  1482. sleep(serverFriendly)
  1483. end
  1484. end
  1485.  
  1486. if (amountBurned == 0) then
  1487. -- Out of beans
  1488. if (waitingForBeans == 1 and returnIfStuck == true) then
  1489. return amount - (amountToBurn - amountBurned)
  1490. elseif (waitingForBeans == 0) then
  1491. print("Out of bean type " .. beanType .. " in chest " .. chests[chest]["peripheral name"] .. ". There needs to be more than 2 beans (in a given stack) to burn them...")
  1492. updateBurnStatus("out of " .. beanType .. " in chest " .. chests[chest]["peripheral name"] .. " - waiting for refill")
  1493. end
  1494.  
  1495. waitingForBeans = 1
  1496. --enableRefill()
  1497. sleep(1)
  1498. --disableRefill()
  1499. else
  1500. waitingForBeans = 0
  1501. end
  1502.  
  1503. amountToBurn = amountToBurn - amountBurned
  1504. end
  1505.  
  1506. -- This should always calculate to amount but just in case it doesn't, it's not set static.
  1507. return (amount - amountToBurn)
  1508. end
  1509.  
  1510. function validFurnaceDirections()
  1511. return {["north"] = true, ["south"] = true, ["east"] = true, ["west"] = true}
  1512. end
  1513.  
  1514. function burnBeans(beanType, amount)
  1515. local remainingAmount
  1516. local ci
  1517. local continue
  1518. local amountBurned
  1519. local amountToBurn
  1520. local chestsWithAspect
  1521.  
  1522. amountBurned = 0
  1523. remainingAmount = amount
  1524. chestsWithAspect = {}
  1525. chestsBurnResult = {}
  1526.  
  1527. if (chestAspects[beanType] == 0) then
  1528. -- No chests contain the bean type
  1529. return 0, nil
  1530. else
  1531. for ci in pairs(chests) do
  1532. if (chests[ci][beanType] ~= nil) then
  1533. chestsWithAspect[ci] = true
  1534. end
  1535. end
  1536.  
  1537. if (distributeBurnRequests == true) then
  1538. continue = true
  1539.  
  1540. while (continue) do
  1541. for ci in pairs(chestsWithAspect) do
  1542. if (rs.getInput("left") == true) then
  1543. return -100
  1544. end
  1545. amountToBurn = 64
  1546.  
  1547. if ((remainingAmount - amountToBurn) < 0) then
  1548. amountToBurn = remainingAmount
  1549. end
  1550.  
  1551. amountBurned = executeBurn(ci, beanType, amountToBurn, true)
  1552. remainingAmount = remainingAmount - amountBurned
  1553.  
  1554. if (remainingAmount <= 0) then
  1555. continue = false
  1556. break
  1557. end
  1558. end
  1559.  
  1560. if (continue == true) then
  1561. sleep(5)
  1562. end
  1563. end
  1564. else
  1565. for ci in pairs(chestsWithAspect) do
  1566. amountBurned = executeBurn(ci, beanType, remainingAmount)
  1567.  
  1568. remainingAmount = remainingAmount - amountBurned
  1569.  
  1570. if (remainingAmount <= 0) then
  1571. break
  1572. end
  1573. end
  1574. end
  1575. end
  1576.  
  1577. return (amount - remainingAmount)
  1578. end
  1579.  
  1580. function getValidChestNames()
  1581. local chests = {
  1582. ["container_chest"] = true,
  1583. ["iron"] = true,
  1584. ["gold"] = true,
  1585. ["diamond"] = true,
  1586. ["crystal"] = true,
  1587. ["obsidian"] = true,
  1588. ["copper"] = true,
  1589. ["silver"] = true
  1590. }
  1591.  
  1592. return chests
  1593. end
  1594.  
  1595. function findChestsAndAnalyzers()
  1596. local i, j
  1597. local peripheralType
  1598. local validChestNames
  1599. local myChestID
  1600. local myAnalyzerID
  1601.  
  1602. validChestNames = getValidChestNames()
  1603. myChestId = 1
  1604. myAnalyzerID = 1
  1605.  
  1606. for i,j in ipairs(peripheral.getNames()) do
  1607. peripheralType = peripheral.getType(j)
  1608.  
  1609. if (validChestNames[peripheralType] == true) then
  1610. if (debugOutput > 1) then
  1611. print("Valid chest found: " .. peripheralType .. " size: " .. peripheral.call(j, "getInventorySize"))
  1612. end
  1613.  
  1614. chests[myChestId] = {}
  1615. chests[myChestId]["peripheral name"] = j
  1616. myChestId = myChestId + 1
  1617. elseif (peripheralType == "tt_aspectanalyzer" or peripheralType == "ttinkerer_magnet") then
  1618. if (debugOutput > 1) then
  1619. print("Aspect analyzer found: " .. j)
  1620. end
  1621.  
  1622. aspectAnalyzers[myAnalyzerID] = {}
  1623. aspectAnalyzers[myAnalyzerID]["peripheral name"] = j
  1624. myAnalyzerID = myAnalyzerID + 1
  1625. end
  1626. end
  1627. end
  1628.  
  1629. function printChestAspects()
  1630. local ci
  1631. local e, s, i
  1632. local chestEssentia
  1633.  
  1634. for ci in pairs(chests) do
  1635. chestEssentia = ""
  1636.  
  1637. for e, i in pairs(chests[ci]) do
  1638. if not (string.find(e, " ")) then
  1639. for s in pairs(i) do
  1640. chestEssentia = chestEssentia .. e .. ":" .. s .. ", "
  1641. end
  1642. end
  1643. end
  1644.  
  1645. if (chestEssentia ~= nil) then
  1646. print("Chest " .. ci .. ": " .. string.sub(chestEssentia, 1, string.len(chestEssentia) - 2))
  1647. end
  1648.  
  1649. print("")
  1650. end
  1651.  
  1652. for ci in pairs(chests) do
  1653. print("Chest: " .. ci .. " analyze time: " .. chests[ci]["analyze time"])
  1654. end
  1655. end
  1656.  
  1657. function analyzeChestsContents()
  1658. local ci
  1659. local e
  1660. local slot
  1661. local chestContents
  1662. local aspect
  1663. local count
  1664. local startTime
  1665. local endTime
  1666. local missingAspects
  1667.  
  1668. chestAspects = essentiaList()
  1669.  
  1670. for ci in pairs(chests) do
  1671. if (debugOutput > 1) then
  1672. print("Analyzing chest contents " .. ci)
  1673. end
  1674.  
  1675. chestContents = peripheral.call(chests[ci]["peripheral name"], "getAllStacks")
  1676. startTime = os.clock()
  1677. for slot in pairs(chestContents) do
  1678. if (chestContents[slot].name == "Mana Bean") then
  1679. aspect,count = scanItemInSlot(chests[ci]["aspect analyzer"], chests[ci]["peripheral name"], slot, chestContents[slot].qty)
  1680.  
  1681. if (aspect ~= nil) then
  1682. if (chests[ci][aspect] == nil) then
  1683. chests[ci][aspect] = {}
  1684. end
  1685.  
  1686. chests[ci][aspect][slot] = true
  1687. chestAspects[aspect] = 1
  1688.  
  1689. if (debugOutput > 1) then
  1690. print("Found mana bean in chest " .. ci .. " slot " .. slot .. " of aspect type " .. aspect)
  1691. end
  1692. else
  1693. print("Mana bean in chest " .. ci .. " slot " .. slot .. " needs refilling; only " .. count .. " left.")
  1694. end
  1695. end
  1696. end
  1697.  
  1698. chests[ci]["analyze time"] = os.clock() - startTime
  1699.  
  1700. if (debugOutput > 0) then
  1701. print("Time to analyze chest " .. ci .. " " .. (os.clock() - startTime))
  1702. end
  1703. end
  1704.  
  1705. missingAspects = ""
  1706. for e in pairs(chestAspects) do
  1707. if (chestAspects[e] == 0) then
  1708. if (missingAspects ~= "") then
  1709. missingAspects = missingAspects .. ", "
  1710. end
  1711.  
  1712. missingAspects = missingAspects .. e
  1713. end
  1714. end
  1715.  
  1716. if (missingAspects ~= "") then
  1717. print("WARNING: Not every aspect was found in the connected chests. You won't be able to refill them unless they're added to the chest(s).")
  1718. print("")
  1719. print("Missing aspects: " .. missingAspects)
  1720. end
  1721. end
  1722.  
  1723. function scanItemInSlot(aspectAnalyzer, chest, slot, quantity)
  1724. local aspect
  1725. local stack
  1726.  
  1727. if (quantity == nil) then
  1728. stack = peripheral.call(chest, "getStackInSlot", slot)
  1729. if (stack ~= nil) then
  1730. quantity = stack.qty
  1731. else
  1732. quantity = 0
  1733. end
  1734. end
  1735.  
  1736. if (quantity <= 2) then
  1737. return nil, quantity
  1738. end
  1739.  
  1740. if (peripheral.call(chest, "pushItemIntoSlot", "down", slot, 1, 1) == 0) then
  1741. print("Failed to push mana bean from chest " .. chest .. " slot " .. slot .. " into aspect analyzer. Program halted. !!RESTART REQUIRED!!")
  1742.  
  1743. -- Something went wrong, wait for human intervention.
  1744. while (true) do
  1745. sleep(60)
  1746. end
  1747. end
  1748.  
  1749. aspect = peripheral.call(aspectAnalyzer, "getAspects")
  1750.  
  1751. if (peripheral.call(chest, "pullItemIntoSlot", "down", 1, 1, slot) == 0) then
  1752. print("Failed to pull mana bean from aspect analyzer " .. aspectAnalyzer .. " back into chest " .. chest .. " slot " .. slot .. ". Program halted. !!RESTART REQUIRED!!")
  1753.  
  1754. -- Something went wrong, wait for human intervention.
  1755. while (true) do
  1756. sleep(60)
  1757. end
  1758. end
  1759.  
  1760. return aspect, quantity
  1761. end
  1762.  
  1763. function matchChestsWithAnalyzer()
  1764. local ci
  1765. local ai
  1766. local hasItem
  1767. local chestContents
  1768. local slot
  1769. local foundSlot
  1770. local itemStack
  1771.  
  1772. -- Checks to make sure none of the analyzers have any items in them
  1773. for ai in pairs (aspectAnalyzers) do
  1774. print("--- " .. aspectAnalyzers[ai]["peripheral name"] .. "--")
  1775.  
  1776. -- hasItem = peripheral.call(aspectAnalyzers[ai]["peripheral name"], "hasItem")
  1777. print ("ttinkerer_magnet_1".ListMethods())
  1778. hasItem = aspectAnalyzers[ai]["peripheral name"].hasItem()
  1779. if (hasItem) then
  1780. print("One of the aspect analyzer(s) has an item in it. All aspect analyzer should be empty.")
  1781.  
  1782. while (hasItem) do
  1783. sleep(1)
  1784. -- hasItem = peripheral.call(aspectAnalyzers[ai]["peripheral name"], "hasItem")
  1785. hasItem = aspectAnalyzers[ai]["peripheral name"].hasItem()
  1786. end
  1787. end
  1788. end
  1789.  
  1790.  
  1791. for ci in pairs(chests) do
  1792. chestContents = peripheral.call(chests[ci]["peripheral name"], "getAllStacks")
  1793. foundSlot = false
  1794.  
  1795. -- See if the chest contains anything and record the slot of any item if it does.
  1796. for slot in pairs(chestContents) do
  1797. if (chestContents[slot].name == "Mana Bean") then
  1798. foundSlot = slot
  1799. break
  1800. end
  1801. end
  1802.  
  1803. -- Something went wrong, try to get it fixed.
  1804. if not (foundSlot) then
  1805. print("No beans found in chest: " .. chests[ci]["peripheral name"] .. " at least one bean type needs to be present during startup. Please put some beans in the chest.")
  1806. print("")
  1807.  
  1808. while not (foundSlot) do
  1809. sleep(5)
  1810. chestContents = peripheral.call(chests[ci]["peripheral name"], "getAllStacks")
  1811.  
  1812. for slot in pairs(chestContents) do
  1813. if (chestContents[slot].name == "Mana Bean") then
  1814. foundSlot = slot
  1815. break
  1816. end
  1817. end
  1818. end
  1819.  
  1820. print("Found mana bean in chest slot: " .. foundSlot)
  1821. print("")
  1822. end
  1823.  
  1824.  
  1825. -- Put one of the random items from the chest into the aspect analyzer above it.
  1826. if not (peripheral.call(chests[ci]["peripheral name"], "pushItemIntoSlot", "down", foundSlot, 1, 1)) then
  1827. print("Failed to put item from chest " .. chests[ci]["peripheral name"] .. " slot " .. foundSlot .. " into aspect analyzer. Make sure everything in the chest is a mana bean.")
  1828. print("")
  1829. sleep(1)
  1830.  
  1831. while not (peripheral.call(chests[ci]["peripheral name"], "pushItemIntoSlot", "down", foundSlot, 1, 1)) do
  1832. sleep(1)
  1833. end
  1834. end
  1835.  
  1836.  
  1837. -- Scan through the aspect analyzers and find which one has the item in it.
  1838. for ai in pairs (aspectAnalyzers) do
  1839. --hasItem = peripheral.call(aspectAnalyzers[ai]["peripheral name"], "hasItem")
  1840. hasItem = aspectAnalyzers[ai]["peripheral name"].hasItem()
  1841.  
  1842. if (hasItem) then
  1843. if not (peripheral.call(chests[ci]["peripheral name"], "pullItemIntoSlot", "down", 1, 1, foundSlot)) then
  1844. print("Failed to pull the item back out of the aspect analyzer. Make sure the redstone signal from the computer is disabling refilling of the chests during startup. !!RESTART REQUIRED!!")
  1845.  
  1846. -- This is too difficult to explain how to recover from. Wait for human intervention.
  1847. while (true) do
  1848. sleep(60)
  1849. end
  1850. end
  1851.  
  1852. chests[ci]["aspect analyzer"] = aspectAnalyzers[ai]["peripheral name"]
  1853.  
  1854. if (debugOutput) then
  1855. print("Matched chest: " .. chests[ci]["peripheral name"] .. " with aspect analyzer: " .. aspectAnalyzers[ai]["peripheral name"])
  1856. print("")
  1857. end
  1858.  
  1859. break
  1860. end
  1861. end
  1862.  
  1863. if not (hasItem) then
  1864. print("Failed to match chest " .. chests[ci]["peripheral name"] .. " with any connected aspect analyzer. Make sure all chests have aspect analyzers below them and all chests and aspect analyzers have peripherals/proxies connected and enabled. !!RESTART REQUIRED!!")
  1865.  
  1866. -- Some part of the peripheral configuration is wrong and needs to be fixed. Wait for human intervention.
  1867. while (true) do
  1868. sleep(60)
  1869. end
  1870. end
  1871. end
  1872. end
  1873.  
  1874. function disableRefill()
  1875. rs.setOutput(disableFillRedstoneDirection, true)
  1876. redstone.setBundledOutput(disableFillRedstoneDirection, colors.white)
  1877. sleep(redstoneWaitTicks * .05)
  1878. end
  1879.  
  1880. function enableRefill(skipSleep)
  1881. rs.setOutput(disableFillRedstoneDirection, false)
  1882. redstone.setBundledOutput(disableFillRedstoneDirection, 0)
  1883.  
  1884. if (skipSleep ~= true) then
  1885. sleep(redstoneWaitTicks * .05)
  1886. end
  1887. end
  1888.  
  1889. function essentiaList()
  1890. local essentias
  1891.  
  1892. essentias = {
  1893. ["aer"] = 0,
  1894. ["alienis"] = 0,
  1895. ["aqua"] = 0,
  1896. ["arbor"] = 0,
  1897. ["auram"] = 0,
  1898. ["bestia"] = 0,
  1899. ["cognitio"] = 0,
  1900. ["corpus"] = 0,
  1901. ["exanimis"] = 0,
  1902. ["fabrico"] = 0,
  1903. ["fames"] = 0,
  1904. ["gelum"] = 0,
  1905. ["granum"] = 0,
  1906. ["herba"] = 0,
  1907. ["humanus"] = 0,
  1908. ["ignis"] = 0,
  1909. ["instrumentum"] = 0,
  1910. ["iter"] = 0,
  1911. ["limus"] = 0,
  1912. ["lucrum"] = 0,
  1913. ["lux"] = 0,
  1914. ["machina"] = 0,
  1915. ["messis"] = 0,
  1916. ["metallum"] = 0,
  1917. ["meto"] = 0,
  1918. ["mortuus"] = 0,
  1919. ["motus"] = 0,
  1920. ["ordo"] = 0,
  1921. ["pannus"] = 0,
  1922. ["perditio"] = 0,
  1923. ["perfodio"] = 0,
  1924. ["permutatio"] = 0,
  1925. ["potentia"] = 0,
  1926. ["praecantatio"] = 0,
  1927. ["sano"] = 0,
  1928. ["saxum"] = 0,
  1929. ["sensus"] = 0,
  1930. ["spiritus"] = 0,
  1931. ["telum"] = 0,
  1932. ["tempus"] = 0,
  1933. ["tempestas"] = 0,
  1934. ["tenebrae"] = 0,
  1935. ["terra"] = 0,
  1936. ["tutamen"] = 0,
  1937. ["vacuos"] = 0,
  1938. ["venenum"] = 0,
  1939. ["victus"] = 0,
  1940. ["vinculum"] = 0,
  1941. ["vitium"] = 0,
  1942. ["vitreus"] = 0,
  1943. ["volatus"] = 0
  1944. }
  1945.  
  1946. return essentias
  1947. end
  1948.  
  1949.  
  1950. m.clear()
  1951. m.setCursorPos(1, 1)
  1952. term.redirect(m)
  1953. print("Analyzing chests, watch the computer for more information.")
  1954. print("")
  1955. print("The monitor will refresh once chest analysis is finished.")
  1956.  
  1957. if term.restore then
  1958. term.restore()
  1959. else
  1960. term.redirect(m)
  1961. end
  1962.  
  1963. essentia = essentiaList()
  1964. setAutoDetectVariables()
  1965.  
  1966. disableRefill()
  1967. findChestsAndAnalyzers()
  1968. matchChestsWithAnalyzer()
  1969. analyzeChestsContents()
  1970. enableRefill()
  1971.  
  1972. print("")
  1973. print("Chest analysis finished. Showing interface.")
  1974. sleep(1)
  1975.  
  1976.  
  1977. refresh()
  1978.  
  1979. while (run) do
  1980. -- uses os.pullevent so this isn't triggered constantly
  1981. getClick()
  1982. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement