Guest User

aspects

a guest
Sep 22nd, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.71 KB | None | 0 0
  1. -- Aspects program, adapted from Direwolf20 Season 7 Episode 64 Let's Play
  2. --
  3. -- Hopefully a little cleaned up and easier to understand by Senseidragon.
  4.  
  5. os.loadAPI("button")
  6. local jars = peripheral.getNames()
  7. -- if monitor is not on left of computer, change this
  8. local monitor = peripheral.wrap("left")
  9.  
  10. local essenceIndex = {}
  11. local aspectLevel = {}
  12. local essenceCount = {}
  13. -- direction of the Aspectalyzer from the iron chest.
  14. local analyzerFacing = "SOUTH"
  15.  
  16. local analyzer = peripheral.find("tt_aspectanalyzer")
  17. -- coded to look for an iron chest (from Iron Chests mod)
  18. local chest = peripheral.find("iron")
  19. local rowsActive = true
  20. local monCoord = {}
  21. local currEssentia
  22. local fillAmt = 0
  23. -- Size should be 54 for an iron chest.
  24. local chestSize = chest.getInventorySize()
  25.  
  26.  
  27. -- This function will go through the iron chest to determine
  28. -- what ethereal essences are in which slot.  Only ethereal
  29. -- essences should be in this chest, no more than 1 stack each
  30. function locateEssences()
  31.   local sampleEssence = {}
  32.   local aspectName
  33.   -- iterate through all the slots in the chest
  34.   for idx = 1,chestSize do
  35.     -- If there is an item in this slot
  36.     if chest.getStackInSlot(idx) then
  37.       -- push one of it into the Aspectalyzer
  38.       chest.pushItem(analyzerFacing, idx, 1, 1)
  39.       -- Get the aspects from this item
  40.       sampleEssence = analyzer.getAspects()
  41.       -- if there are 2 essentia entries for an essence, get the non-auram one
  42.       if sampleEssence[2] then
  43.         if sampleEssence[2] == "auram" then
  44.           aspectName = sampleEssence[1]
  45.         else
  46.           aspectName = sampleEssence[2]
  47.         end
  48.       else
  49.         -- otherwise, get the name
  50.         aspectName = sampleEssence[1]
  51.       end
  52.       -- Tell the chest to suck the item back from the Aspectalyzer into its
  53.       -- original slot
  54.       chest.pullItem(analyzerFacing, 1, 1, idx)
  55.       -- Store the chest slot where the essence can be found
  56.       essenceIndex[aspectName] = idx
  57.       -- Set our initial level of this aspect (in the jars) to zero
  58.       aspectLevel[aspectName] = 0
  59.       -- Set how many Ethereal Essences we have available for this aspect
  60.       essenceCount[aspectName] = chest.getStackInSlot(idx)["qty"]
  61.     end
  62.   end
  63. end
  64.  
  65. -- This function scans and records the current aspect levels in all of your void jars
  66. function scanAspects()
  67.   local aspectName
  68.   local tempLevel
  69.  
  70.   -- iterate through everything attached to a Proxy Peripheral
  71.   for i,j in ipairs(jars) do
  72.     -- is it connected to a Void Jar?
  73.     if peripheral.getType(j) == "tt_aspectContainer" then
  74.       -- Get the name and count of the aspect in the jar
  75.       aspectName = peripheral.call(j, "getAspects")
  76.       tempLevel = peripheral.call(j, "getAspectCount", aspectName[1])
  77.       -- If the jar is not empty
  78.       if tempLevel > 0 then
  79.         -- Record the amount of this aspect
  80.         aspectLevel[aspectName[1]] = math.floor(tempLevel)
  81.       end
  82.     end
  83.   end
  84. end
  85.  
  86. -- A generic sorting function to sort aspects alphabetically
  87. function sortEssentia(t)
  88.   local keys = {}
  89.   for k in pairs(t) do keys[#keys+1] = k end
  90.   table.sort(keys)
  91.        
  92.   local i = 0
  93.   return function()
  94.     i = i+1
  95.     if keys[i] then
  96.       return keys[i], t[keys[i]]
  97.     end
  98.   end
  99. end
  100.  
  101. -- Sometimes blank screens can be unsettling
  102. function printWaitMessage()
  103.   monitor.clear()
  104.   button.label(1,10, "Calculating aspect levels. One moment...")
  105. end
  106.  
  107. -- Print out the big list of all aspects in the jars and their current levels
  108. function printEssentia()
  109.   monitor.setTextColor(colors.white)
  110.   local x = 1
  111.   local y = 1
  112.   monCoord[x] = {}
  113.  
  114.   for i,j in sortEssentia(aspectLevel) do
  115.     -- Aspects with 20 or less show in red
  116.     if j<=20 then monitor.setTextColor(colors.red) end
  117.     -- Aspects between 21-39 inclusive show in yellow
  118.     if j<40 and j>20 then monitor.setTextColor(colors.yellow) end
  119.     -- Aspects with 40 or more show in green
  120.     if j>=40 then monitor.setTextColor(colors.green) end
  121.  
  122.     monitor.setCursorPos(x,y)
  123.     monitor.write(i)
  124.     monitor.setCursorPos(x+14,y)
  125.     monitor.write(tostring(j))
  126.  
  127.     monCoord[x][y] = i
  128.     if y < 17 then
  129.       y = y+1
  130.     else
  131.       y = 1
  132.       x = x+17
  133.       monCoord[x] = {}  
  134.     end
  135.   end
  136.   monitor.setTextColor(colors.white)
  137. end
  138.  
  139. -- The main event handling routine
  140. -- Waits for the user to click something, or for a 10 second timer to elapse
  141. function getClick()
  142.   local timerCode = 0
  143.   -- Set the interval timer to 10 seconds
  144.   timerCode = os.startTimer(10)
  145.   local event,side,x,y
  146.   repeat
  147.     event,side,x,y = os.pullEvent()
  148.   -- Keep waiting until you get a monitor click or the timer expires
  149.   until side == timerCode or event=="monitor_touch"
  150.   -- If they touched the monitor, figure out where they touched it
  151.   if event=="monitor_touch" then
  152.     if button.checkxy(x,y) then
  153.       -- Buttons know how to handle themselves, so we just print "button"
  154.       -- to the console to let us know we saw a click.
  155.       print("button")
  156.     else
  157.       -- They didn't click a button, let's see what aspect they clicked on
  158.       if rowsActive then
  159.         fillAmt = 0
  160.         print(x..":"..x-(x%17)+1)
  161.         print(monCoord[x-(x%17)+1][y])
  162.         currEssentia = monCoord[x-(x%17)+1][y]
  163.         -- If they clicked on an aspect, go to the "fill it up" screen
  164.         if currEssentia ~= nil then
  165.           -- if it isn't already full, that is
  166.           if aspectLevel[currEssentia] < 64 then
  167.             print("aLvl: "..aspectLevel[currEssentia])
  168.             fillTable2()
  169.           else
  170.             -- They clicked a full aspect, tell them they derped
  171.             monitor.clear()
  172.             button.label(1,10, currEssentia.." is already full.  Please choose another.")
  173.             sleep(3)
  174.             refresh()
  175.           end
  176.         end
  177.       end
  178.     end
  179.   else
  180.     -- The 10 second timer expired, refresh the screen with updated values
  181.     refresh()
  182.   end
  183. end
  184.  
  185. -- This updates the main screen and recalculates all the values.  It is a
  186. -- rather slow function, so don't call it too frequently
  187. function refresh()
  188.   fillTable()
  189.   button.flash("Refresh")
  190.   -- Recalculate all the essences in the chest
  191.   locateEssences()
  192.   -- Recalculate all the aspect levels in the jars
  193.   scanAspects()
  194.   monitor.clear()
  195.   -- Display everything
  196.   printEssentia()
  197.   print("Refreshed")
  198.   button.screen()
  199. end
  200.  
  201. function fillTable()
  202.   -- Show the buttons on the main screen
  203.   rowsActive = true
  204.   button.clearTable()
  205.   button.setTable("Refresh", refresh, "", 30, 38, 19,19)
  206.   button.screen()
  207.   button.setTable("Refill All", refillAll, "", 40,50, 19,19)
  208. end
  209.  
  210. -- Iterate through every aspect you have already calculated
  211. -- and if they are not already full, try to fill them
  212. function refillAll()
  213.   printWaitMessage()
  214.   for essName,essAmt in pairs(aspectLevel) do
  215.     currEssentia = essName
  216.     fillAmt = (64-essAmt)
  217.     if essenceCount[currEssentia] ~= nil then
  218.       if fillAmt > (essenceCount[currEssentia]-1)*2 then fillAmt = (essenceCount[currEssentia]-1)*2 end
  219.       fillEss2()
  220.     else
  221.       print("Supply chest needs more "..currEssentia)
  222.     end
  223.   end
  224.   fillTable()
  225. end
  226.  
  227. -- Poorly named function.  Shoot Direwolf20, not me.
  228. function cancel()
  229.   monitor.clear()
  230.   fillTable()
  231.   refresh()
  232. end  
  233.  
  234. -- A function used when telling the program how much essence to burn.
  235. -- Don't let the player burn more than what would fill the jar    
  236. function addEss(num)
  237.   fillAmt = fillAmt + num
  238.   if fillAmt < 0 then fillAmt = 0 end
  239.   if fillAmt > 64-aspectLevel[currEssentia] then fillAmt = 64-aspectLevel[currEssentia] end
  240.   if fillAmt > (essenceCount[currEssentia]-1)*2 then fillAmt = (essenceCount[currEssentia]-1)*2 end
  241.   monitor.clear()
  242.   fillTable2()
  243. end
  244.  
  245. -- Send some essence to the ender chest to get burned
  246. function fillEss2()
  247.   printWaitMessage()
  248.   chest.pushItem("UP", essenceIndex[currEssentia], fillAmt/2)
  249. end
  250.  
  251. -- Display the "fill it up" screen
  252. function fillTable2()
  253.   rowsActive = false
  254.   button.clearTable()
  255.   monitor.clear()
  256.  
  257.   -- If you don't have any essence in the chest to fill this aspect
  258.   -- then set the count to zero
  259.   local count
  260.   if essenceCount[currEssentia] == nil then
  261.     count = 0
  262.     print("No essenceCount entry for "..currEssentia)
  263.   else
  264.     count = essenceCount[currEssentia] - 1
  265.     print(currEssentia..": "..count)
  266.   end
  267.  
  268.   -- We never take the last essence from the chest, thus the reason for the -1.
  269.   if count < 1 then
  270.     button.label(1,10, currEssentia.." is out of stock.  Please choose another.")
  271.     sleep(3)
  272.     cancel()
  273.   else
  274.     button.label(7, 1, "Essentia: "..currEssentia.." contains "..aspectLevel[currEssentia])
  275.     button.label(7, 2, "Available: "..currEssentia.." is: "..(essenceCount[currEssentia]-1)*2)
  276.     button.setTable("+2", addEss, 2, 8, 18, 6,6)
  277.     button.setTable("+6", addEss, 6, 20, 30, 6, 6)
  278.     button.setTable("+10", addEss, 10, 32, 42, 6, 6)
  279.     button.setTable("-2", addEss, -2, 8, 18, 8, 8)
  280.     button.setTable("-6", addEss, -6, 20, 30, 8, 8)
  281.     button.setTable("-10", addEss, -10, 32, 42, 8 ,8)
  282.     button.setTable("Refill Jar", addEss, 64-essenceIndex[currEssentia], 8, 42, 10, 10)
  283.     button.setTable("Execute Fill Request", fillEss2, "", 8, 42, 16, 18)
  284.     button.setTable("Cancel", cancel, "", 20, 30, 12, 14)
  285.     button.label(7, 4, "Currently Adding "..fillAmt.." "..currEssentia.." essentia.")
  286.     button.screen()
  287.   end
  288. end
  289.  
  290. print("This program is written for a 5x3 Advanced Monitor placed on the LEFT side of an Advanced Computer.  If you mess with the monitor blocks, you may need to stop/start the game to get the monitor to display correctly again.")
  291. print("Remember, this program is written to process ethereal essences only.")
  292.  
  293. printWaitMessage()
  294. fillTable()
  295. refresh()
  296. while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment