Advertisement
Guest User

startup

a guest
Sep 2nd, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.95 KB | None | 0 0
  1. -- Program:
  2. --   aspects
  3. -- Original program by Direwolf20
  4. -- Modified by Kzorith
  5. -- Description:
  6. --   Manages Thaumcraft 4 aspects stored in
  7. --   warded or void jars
  8. -- Hardware requirements:
  9. --    1 Advanced Computer
  10. --   15 Advanced Monitors (5x3)
  11. --    1 Advanced Wireless Turtle
  12. --    1 Wireless Modem
  13. --   52 Wired Modem
  14. --   52 Peripheral Proxy
  15. --      Network Cable
  16. --    1 Golem Connector
  17. --    1 Aspectalyzer
  18. -- Software requirements:
  19. --   Advanced Computer
  20. --     this program
  21. --     button API - pastebin get z0FBM6wd button
  22. --   Advanced Wireless Turtle
  23. --     catalogue - pastebin get dEWU3SwC catalogue
  24.  
  25. -- Initialization
  26. --  set this to whatever side the wireless modem is on
  27. rednet.open("top")
  28.  
  29. --  load the api to control the monitor
  30. os.loadAPI("button")
  31.  
  32. --  set this to the turtle's id (command 'id' on the turtle)
  33. local turtleID = 47
  34.  
  35. --  initialize a table for the essentia type and quantity
  36. local essentia = {}
  37.  
  38. --  build a table of all peripherals attached to the computer
  39. local jars = peripheral.getNames()
  40.  
  41. --  set this to whatever side the monitor is on
  42. --  NOTE: this must also be set the same in the button API
  43. local m = peripheral.wrap("right")
  44.  
  45. -- set this to whatever side the golem connector is on
  46. --local g = peripheral.wrap("left")
  47. -- get the current location of the golem,
  48. -- it should be standing around, waiting for essence
  49. --local gX, gY, gZ = g.getPosition()
  50. --print ("Golem home at: "..gX..","..gY..","..gZ)
  51.  
  52. --  initialize a table for rows on the monitor
  53. local monCoord = {}
  54.  
  55. --  stores the essentia that was picked on the monitor
  56. local currEssentia
  57.  
  58. --  stores the amount of essentia to be added to the jar
  59. local fillAmt = 0
  60.  
  61. --  determines if the list of aspects is on the screen
  62. local rowsActive = true
  63.  
  64. -- Functions
  65. --  sortEss - sorts a supplied table alphabetically
  66. --  Input: table t
  67. --  Output: a pointer to the function?
  68. function sortEss(t)
  69.    local keys = {}
  70.    for k in pairs(t) do keys[#keys+1] = k end
  71.    table.sort(keys)
  72.    
  73.    local i = 0
  74.    return function()
  75.       i = i+1
  76.       if keys[i] then
  77.          return keys[i], t[keys[i]]
  78.       end
  79.    end
  80. end
  81.  
  82. --  scanEssentia - Reads the jars of essentia via the peripheral proxy
  83. function scanEssentia()
  84.   -- read in all the peripherals attached to the computer
  85.   jars = peripheral.getNames()
  86.  
  87.   -- set the quantity of each item in essentia to 0
  88.   for i,j in pairs(essentia) do
  89.     essentia[i] = 0
  90.   end
  91.  
  92.   -- go through each peripheral attached to the computer
  93.   for i,j in ipairs(jars) do
  94.  
  95.     -- if the peripheral is a warded jar
  96.     if peripheral.getType(j) == "tt_aspectContainer" then
  97.        -- get the type of essentia in it
  98.        asp = peripheral.call(j, "getAspects")
  99.        -- get the quantity of essentia in it
  100.        countasp = peripheral.call(j, "getAspectCount", asp)
  101.        -- round the quantity down to the nearest whole number if it's more than 0
  102.        -- also ensure that the essentia name is all lower case
  103.        if countasp > 0 then
  104.           essentia[string.lower(asp)] = math.floor(countasp)
  105.        end
  106.      end
  107.      -- if the peripheral is a void jar
  108.      if peripheral.getType(j) == "tilejarvoid" then
  109.        -- get a table with the aspect name and quantity in it
  110.        tmp = peripheral.call(j, "getAspects")
  111.        if tmp[1] ~= nil then
  112.        -- get the quantity of essentia in it
  113. --       for i,v in ipairs(tmp[1]) do print( i..", "..v) end
  114.        countasp = tmp[1]["quantity"]
  115.        -- get the type of essentia in it
  116.        asp = tmp[1]["name"]
  117.        -- round the quantity down to the nearest whole number
  118.        -- also ensure that the essentia name is all lower case
  119.        if countasp > 0 then
  120.           essentia[string.lower(asp)] = math.floor(countasp)
  121.        end
  122.        end
  123.      end
  124.   end
  125. end
  126.  
  127. --  printEssentia - prints the essentia names and quantities on the monitor
  128. function printEssentia()
  129.   --set the default text color for the monitor
  130.   m.setTextColor(colors.white)
  131.   -- set the starting coords to write on the screen
  132.   local x = 1
  133.   local y = 1
  134.   -- initialize a table to keep track of where an essentia is on the screen in the first column
  135.   monCoord[x] = {}
  136.   -- go through each essentia in alphabetical order
  137.   for i,j in sortEss(essentia) do
  138.      -- set the text color based on how much essentia there is
  139.      if j==0 then m.setTextColor(colors.purple) end
  140.      if j<20 and j>0 then m.setTextColor(colors.red) end
  141.      if j<40 and j>20 then m.setTextColor(colors.yellow) end
  142.      if j>=40 then m.setTextColor(colors.green) end
  143.      
  144.      -- set the cursor to a location on the screen
  145.      m.setCursorPos(x,y)
  146.      -- write the essentia name to the screen
  147.      m.write(i)
  148.      -- move the cursor for the quantity based on if it's 1 or 2 digits
  149.      -- this will align the numbers
  150.      if j<10 then
  151.        m.setCursorPos(x+15,y)
  152.      else
  153.        m.setCursorPos(x+14,y)
  154.      end
  155.      -- write the essentia quantity to the screen
  156.      m.write(tostring(j))
  157.      -- make note what essentia is at this location on the screen
  158.      monCoord[x][y] = i
  159.      -- move the cursor down one row if we haven't placed 17 essentia on the screen
  160.      if y < 19 then
  161.         y = y+1
  162.      -- otherwise move the cursor over to the next column and start at the top
  163.      -- initialize a new table to keep track of essentia in this column
  164.      else
  165.         y = 1
  166.         x = x+17
  167.         monCoord[x] = {}  
  168.      end
  169.   end
  170.  -- set the default text color to white
  171.  m.setTextColor(colors.white)
  172. end
  173.  
  174. --  getClick - waits for the player to right click on the screen
  175. function getClick()
  176.    -- wait for something to happen to the computer
  177.    local event,side,x,y = os.pullEvent()
  178.    -- if the monitor was touched..
  179.    if event=="monitor_touch" then
  180.      -- check to see if it was one of the defined buttons
  181.      if button.checkxy(x,y) then
  182.         -- print a message on the computer
  183.         print("button")
  184.      else
  185.         -- if it was elsewhere on the screen and we are displaying essentia
  186.         if rowsActive then
  187.           -- initialize the fill amount
  188.           fillAmt = 0
  189.           -- print the column and essentia that was clicked
  190.           print(x..":"..x-(x%17)+1)
  191.           print(monCoord[x-(x%17)+1][y])
  192.           -- store the essentia name
  193.           currEssentia = monCoord[x-(x%17)+1][y]
  194.           -- if the stored name isn't blank
  195.           if currEssentia ~= nil then
  196.           -- if the quantity is less than 64
  197.           if essentia[currEssentia] < 64 then
  198.              -- display the quantity control screen
  199.              fillTable2()
  200.           else
  201.              -- clear the screen
  202.              m.clear()
  203.              -- display a message that the jar is full
  204.              button.label(1,10, currEssentia.." is already full.  Please choose another.")
  205.              -- wait 3 seconds
  206.              sleep(3)
  207.              -- refresh the essentia list and display it
  208.              refresh()
  209.           end
  210.           end
  211.         end
  212.      end
  213.    end
  214. end
  215.  
  216. --  refresh - Scans the jars and displays the essentia list
  217. function refresh()
  218.    -- show that you clicked the button
  219.    button.flash("Refresh")
  220.    -- clear the monitor
  221.    m.clear()
  222.    -- scan the jars
  223.    scanEssentia()
  224.    -- display the essentia list
  225.    printEssentia()
  226.    -- print that the screen was refreshed to the computer
  227.    print("Refreshed")
  228.    -- display the buttons on the monitor
  229.    button.screen()
  230. end
  231.  
  232. --  fillTable - builds the list of available buttons on the essentia screen
  233. function fillTable()
  234.    -- set that we are using rows on this screen
  235.    rowsActive = true
  236.    -- clear the list of buttons
  237.    button.clearTable()
  238.    -- build the Refresh button.
  239.    --   button name, function to call, any parameters to the function,
  240.    --   button location left column, top row, right column, bottom row
  241.    button.setTable("Refresh", refresh, "", 8, 25, 21, 25)
  242.    -- build the Fill All button
  243.    button.setTable("Fill All", fillAll, "", 27, 41, 21, 25)
  244.    --display the buttons on the monitor
  245.    button.screen()
  246. end
  247.  
  248. -- addEss - adds a number to the current fill amount
  249. -- Input: interger num
  250. function addEss(num)
  251.    -- add the number (which could be negative) to the current fill amount
  252.    fillAmt = fillAmt + num
  253.    -- if it's less than zero, make it zero
  254.    if fillAmt < 0 then fillAmt = 0 end
  255.    -- if the total is more than 64, make it the amount to bring the quantity to 64
  256.    if fillAmt > 64-essentia[currEssentia] then fillAmt = 64-essentia[currEssentia] end
  257.    -- clear the monitor
  258.    m.clear()
  259.    -- display the quantity control screen
  260.    fillTable2()
  261. end
  262.  
  263. -- meltEss - tell the turtle how many mana beans of what type to put in the alembic
  264. function meltEss()
  265.    -- initialize a table to hold the essentia information
  266.    local essData = {}
  267.    -- store the essentia name
  268.    essData[1] = currEssentia
  269.    -- store the quantity requested to be added
  270.    essData[2] = fillAmt
  271.    -- initialize a string to hold a data stream
  272.    local sendData = ""
  273.    -- put the information into a data stream
  274.    sendData = textutils.serialize(essData)
  275.    -- send the stream to the waiting turtle
  276.    rednet.send(turtleID, sendData)
  277.    -- clear the monitor
  278.    m.clear()
  279.    -- put a message on the monitor saying what's going on
  280.    button.label(7, 10, "Waiting for "..currEssentia.." to finish cooking....")
  281.    -- wait for the turtle to send a message back
  282.    rednet.receive()
  283. end
  284.  
  285. --  waitOnGolem - checks the golems position against it's home position to make sure it's not moving around
  286. function waitOnGolem()
  287.    -- clear the monitor
  288.    m.clear()
  289.    --put a message on the monitor saying what's going on
  290.    button.label(7, 10, "Waiting for the golum to finish moving...")
  291.    -- if the golem is moving, wait 5 seconds and check again
  292.    while not locateGolem() do
  293.       sleep(5)
  294.    end
  295. end
  296.  
  297. -- fillEss - performs the steps to add essential to jars
  298. function fillEss()
  299.    -- tell turtle what to put into the alembic
  300.    meltEss()
  301.    -- wait for golem to move the essentia
  302.    waitOnGolem()
  303.    -- clear the screen
  304.    m.clear()
  305.    -- put the buttons on the essentia screen
  306.    fillTable()
  307.    -- scan the jars and display the essentia
  308.    refresh()
  309. end
  310.  
  311. --  fillAll - checks each jar and fills it up to 64 essentia if required
  312. function fillAll()
  313.    -- show that you clicked the button
  314.    button.flash("Fill All")
  315.    -- get the current quantities in the jars
  316.    scanEssentia()
  317.    -- for each jar in the alphabetized list
  318.    for i,j in sortEss(essentia) do
  319.       -- if there is less than 64 essentia
  320.       if j < 64 then
  321.          -- figure out how much more is needed
  322.          fillAmt = 64-j
  323.          -- store what essentia it is
  324.          currEssentia = i
  325.          -- tell the turtle what to put in the alembic
  326.          meltEss()
  327.       end
  328.    end
  329.    -- wait for the golem to move the essentia
  330.    waitOnGolem()
  331.    -- clear the monitor
  332.    m.clear()
  333.    -- put the buttons on the essentia screen
  334.    fillTable()
  335.    -- scan the jars and display the essentia
  336.    refresh()
  337. end
  338.  
  339. -- cancel - abort adding any essentia to a jar
  340. function cancel()
  341.    -- clear the monitor
  342.    m.clear()
  343.    -- put the buttons on the essentia screen
  344.    fillTable()
  345.    -- scan the jars and dispaly the essentia
  346.    refresh()
  347. end  
  348.  
  349. --  fillTable2 - Builds a list of available buttons on the quantity control screen
  350. function fillTable2()
  351.    -- set that we are NOT using rows on this screen
  352.    rowsActive = false
  353.    -- clear the list of buttons
  354.    button.clearTable()
  355.    -- clear the monitor
  356.    m.clear()
  357.    -- put a message on the monitor saying what the essentia we are working on and the quantity
  358.    button.label(7, 1, "Essentia: "..currEssentia.." contains "..essentia[currEssentia])
  359.    -- set up the buttons to add or remove essentia
  360.    button.setTable("+1", addEss, 1, 8, 18, 6,6)
  361.    button.setTable("+5", addEss, 5, 20, 30, 6, 6)
  362.    button.setTable("+10", addEss, 10, 32, 42, 6, 6)
  363.    button.setTable("-1", addEss, -1, 8, 18, 8, 8)
  364.    button.setTable("-5", addEss, -5, 20, 30, 8, 8)
  365.    button.setTable("-10", addEss, -10, 32, 42, 8 ,8)
  366.    -- set up the button to refill the jar completely
  367.    button.setTable("Refill Jar", addEss, 64-essentia[currEssentia], 8, 42, 10, 10)
  368.    -- set up the button to begin filling the jar
  369.    button.setTable("Execute Fill Request", fillEss, "", 8, 42, 16, 18)
  370.    -- set up the button to abort the fill request
  371.    button.setTable("Cancel", cancel, "", 20, 30, 12, 14)
  372.    -- put a message on the monitor saying how much we currently want to add
  373.    button.label(7, 4, "Currently Adding "..fillAmt.." "..currEssentia.." essentia.")
  374.    -- display the buttons on the quantity control screen
  375.    button.screen()
  376. end
  377.  
  378. --  locateGolem - determines the current location of the golem
  379. --  Output: a boolean indicating if the golem was moving
  380. function locateGolem()
  381.    -- return that the golem isn't moving
  382.    return true
  383. end
  384.  
  385. -- put buttons on the essentia screen
  386. fillTable()
  387. -- scan the jars and display the essentia
  388. refresh()
  389. -- loop forever waiting for a click on the monitor
  390. while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement