Advertisement
Nividica

ThaumicEnergistics + ComputerCraft Example

Jul 4th, 2015
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.50 KB | None | 0 0
  1. -- Aspect Monitor
  2. -- Description: Example ComputerCraft script that monitors certain aspect levels on an AE network via a connected Essentia Provider,
  3. --  and sets a redstone signal based on the amounts. In effect, this operates just like the Essentia Level Emitter, but with more flexibility.
  4. -- This script demonstrates using the watcher event system as an alternative to polling the provider for amounts.
  5. -- Author: Nividica
  6.  
  7. -- Declare methods, because Lua
  8. local main = nil
  9. local processEvents = nil
  10. local processEssentiaEvent = nil
  11. local getIndexOf = nil
  12. local getAmountOf = nil
  13. local registerForEvents = nil
  14. local showDisplay = nil
  15. local onAspectAmountChanged = nil
  16. local resetToSafeState = nil
  17.  
  18. -- Declare essentia event types:
  19. --- Sent when a watched essentia level changes
  20. local Ess_EssentiaChange = 1
  21.  
  22. --- Sent when the provider goes online or offline
  23. local Ess_PowerChange = 2
  24.  
  25. --- Sent when the computer detaches from the provider. (Most likely the network link was broken)
  26. local Ess_Detach = 3
  27.  
  28. -- Declare variables
  29. local essentiaToWatch = { "Aqua", "Meto" }
  30. local essentiaAmount = {}
  31. local userWantsToExit = false
  32. local isRegistered = false
  33. local isOnline = false
  34. local provider = nil
  35. local status = ""
  36.  
  37. -- This is where you would do something useful with the information
  38. onAspectAmountChanged = function()
  39.   -- Make sure the provider is online, and that it is powered. Otherwise the amounts could be out of sync
  40.   if( ( provider == nil ) or ( not isOnline ) ) then
  41.     -- Stale
  42.     return
  43.   end
  44.  
  45.   -- If Aqua is less than 500, turn on redstone, else turn it off
  46.   aquaAmount = getAmountOf( "Aqua" )
  47.   if( aquaAmount ~= -1 ) then
  48.     rs.setOutput( "back", ( aquaAmount < 500 ) );
  49.   end
  50.  
  51.   -- If Meto is less than 200, turn on redstone, else turn it off
  52.   metoAmount = getAmountOf( "Meto" )
  53.   if( metoAmount ~= -1 ) then
  54.     rs.setOutput( "top", ( metoAmount < 200 ) );
  55.   end
  56.  
  57. end
  58.  
  59. -- Called automatically when the provider is no longer accessible due to a power loss, or removal.
  60. -- If you have a default or safe state, this is a good time to transition into it.
  61. -- For example, if you have it setup such that when you output a redstone signal an
  62. -- export bus does work, you should make sure that signal is turned off here.
  63. resetToSafeState = function()  
  64.   -- Turn redstone off
  65.   rs.setOutput( "back", false )
  66.   rs.setOutput( "top", false )
  67. end
  68.  
  69. -- Main loop
  70. main = function()
  71.   -- Init timeout
  72.   recheckTimeout = nil
  73.  
  74.   -- Reset state
  75.   resetToSafeState()
  76.  
  77.   -- Loop until the user elects to exit
  78.   while( not userWantsToExit ) do
  79.  
  80.     -- Reset the timeout
  81.     recheckTimeout = nil
  82.  
  83.     -- Is the provider nil?
  84.     if( provider == nil ) then      
  85.       -- Try to locate the essentia provider
  86.       result = {pcall( peripheral.find, "EssentiaProvider" )}
  87.      
  88.       -- Was a provider found?
  89.       if( result[1] and ( result[2] ~= nil ) ) then
  90.         -- Set the provider
  91.         provider = result[2]
  92.        
  93.       else
  94.         -- Set status to searching
  95.         status = "Searching for provider..."
  96.        
  97.         -- Check again in 5 seconds
  98.         recheckTimeout = 5
  99.        
  100.       end
  101.     end
  102.    
  103.     -- Should the computer register for essentia events?
  104.     if( ( provider ~= nil ) and ( not isRegistered ) )then
  105.    
  106.       -- Get the providers power state
  107.       isOnline = provider.isOnline()
  108.        
  109.       -- Can the computer register for essentia events?
  110.       if( isOnline ) then
  111.         registerForEvents()
  112.       else
  113.       -- Set status
  114.       status = "Waiting for provider to be ready..."
  115.      
  116.       -- Check again in 5 seconds
  117.         recheckTimeout = 5
  118.       end
  119.     end
  120.  
  121.     -- Display the status screen
  122.     showDisplay()
  123.  
  124.     -- Wait for an event
  125.     processEvents( recheckTimeout )
  126.   end
  127.  
  128.   -- Make sure to un-register
  129.   if( ( provider ~= nil ) and isRegistered )then
  130.     provider.unregisterAsWatcher()
  131.   end
  132.  
  133.   -- Return to the safe state before exiting
  134.   resetToSafeState()
  135.  
  136.   -- Done
  137.   print( "Exiting" )  
  138. end
  139.  
  140. -- Event processor, optional timeout value
  141. processEvents = function( timeout )
  142.   -- Is there a timeout?
  143.   if( timeout ~= nil ) then
  144.     -- Start a timer
  145.     os.startTimer(timeout)
  146.   end
  147.  
  148.   -- Pull an event
  149.   event = { os.pullEvent() }
  150.  
  151.   -- Was the event a character key press?
  152.   if( event[1] == "char" ) then
  153.     -- Was the key a q?
  154.     if( event[2] == "q" or event[2] == "Q" ) then
  155.       -- User wants to exit
  156.       userWantsToExit = true
  157.     end
  158.    
  159.   -- Was the event from the essentia provider?
  160.   elseif( event[1] == "essentia" ) then
  161.     processEssentiaEvent( event )
  162.   end
  163.  
  164. end
  165.  
  166. -- Processes an essentia event
  167. processEssentiaEvent = function( event )
  168.   -- Has the providers power changed?
  169.   if( event[2] == Ess_PowerChange ) then
  170.     -- Get the new power state
  171.     isOnline = event[3]
  172.    
  173.     -- Is the power going off?
  174.     if( not isOnline ) then
  175.       -- No longer registered
  176.       isRegistered = false
  177.      
  178.       -- Update status
  179.       status = "Provider offline, amounts shown may not be correct."
  180.      
  181.       -- Reset state
  182.       resetToSafeState()
  183.     end
  184.    
  185.   -- Essentia amount update?
  186.   elseif( event[2] == Ess_EssentiaChange ) then
  187.     -- Get the index of the essentia changed
  188.     index = getIndexOf( essentiaToWatch, event[3] )
  189.     if( index ~= -1 ) then
  190.       -- Update the amount
  191.       essentiaAmount[index] = event[4] + ( essentiaAmount[index] or 0 )
  192.      
  193.       -- Call the changed event handler
  194.       onAspectAmountChanged()
  195.     end
  196.    
  197.   -- Detached?
  198.   elseif( event[2] == Ess_Detach ) then
  199.     -- Reset everything
  200.     provider = nil
  201.     isOnline = false
  202.     isRegistered = false
  203.     resetToSafeState()
  204.  
  205.   end  
  206.  
  207. end
  208.  
  209. -- Returns the index of the item in the table
  210. -- If the item is not in the table, returns -1
  211. getIndexOf = function( t, item )
  212.   -- Search the table
  213.   for index,v in ipairs(t) do
  214.     -- Do the items match?
  215.     if v == item then
  216.       -- Return the index
  217.       return index
  218.     end
  219.   end
  220.  
  221.   -- Item not found
  222.   return -1
  223. end
  224.  
  225. -- Helper function that returns how much of the aspect is stored
  226. -- Returns -1 if the aspect is not being tracked
  227. getAmountOf = function( aspect )
  228.   -- Get the index
  229.   index = getIndexOf( essentiaToWatch, aspect )
  230.  
  231.   -- Is the essentia not being tracked
  232.   if( index == -1 ) then
  233.     return -1
  234.   end
  235.  
  236.   -- Return the amount
  237.   return essentiaAmount[index] or 0
  238. end
  239.  
  240. -- Registers the computer to receive essentia updates from the provider
  241. registerForEvents = function()  
  242.   -- Start by getting the essentia amounts
  243.   for index,aspect in ipairs( essentiaToWatch ) do
  244.     essentiaAmount[index] = provider.getAmount( aspect )
  245.   end
  246.  
  247.   -- Register for essentia events
  248.   isRegistered = provider.registerAsWatcher( unpack( essentiaToWatch ) )
  249.  
  250.   if( isRegistered ) then
  251.     -- Set status
  252.     status = "Watching Network For Changes."
  253.    
  254.     -- Call the changed event handler
  255.     onAspectAmountChanged()
  256.   end
  257.  
  258. end
  259.  
  260. -- Displays what the program is doing
  261. showDisplay = function()
  262.   -- Reset the terminal
  263.   term.clear()
  264.   term.setCursorPos( 1, 1 )
  265.  
  266.   -- Display each essentia and amount
  267.   for index,aspect in ipairs( essentiaToWatch ) do
  268.     print( aspect , ": " , essentiaAmount[index] )
  269.   end
  270.  
  271.   -- Divider line
  272.   print( "----------" )
  273.  
  274.   -- Display the status
  275.   print( "Status: ".. status )
  276.  
  277.   -- Display how to quit
  278.   print( "Press Q to exit" )
  279. end
  280.  
  281. -- Call main
  282. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement