Advertisement
Enstroe

BB_Core

Jan 24th, 2022 (edited)
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.77 KB | None | 0 0
  1. ------------------------------------------------------
  2. -- # Title : Base Butler
  3. ------------------------------------------------------
  4. -- # Description : A Modded-Minecraft Automation Assistant
  5. -- #             - built in 'Computer Craft'/'CC: Tweaked' and 'Advanced Peripherals'.
  6. ------------------------------------------------------
  7.  
  8.  
  9. ---------------------------
  10. -- # Member Variable Declarations
  11. ---------------------------
  12.  
  13. Monitor = nil --peripheral.find("monitor")
  14. ChatBox = nil --peripheral.find("chatBox")
  15.  
  16. Scale = 1
  17.  
  18. PosX = Scale
  19. PosY = Scale
  20.    
  21. CommandUser = "Broomfields"     --Change to your user name
  22. CommandPhrase = "BB"       --Change to your assistant name
  23.  
  24. CommandPhraseLength = string.len(CommandPhrase)
  25.  
  26. Programs = {};
  27. table.insert(Programs, "Information")
  28. table.insert(Programs, "Update")            -- Set And Get
  29. table.insert(Programs, "Reboot")
  30. table.insert(Programs, "Message")
  31. table.insert(Programs, "Message")
  32. table.insert(Programs, "Speak")
  33. table.insert(Programs, "CommandUser")       -- Set And Get
  34. table.insert(Programs, "CommandPhrase")     -- Set And Get
  35. table.insert(Programs, "Label")             -- Set And Get
  36. table.insert(Programs, "MonitorColour")     -- Set And Get
  37. table.insert(Programs, "MonitorTextColour") -- Set And Get
  38. table.insert(Programs, "MonitorScale")      -- Set And Get
  39. table.insert(Programs, "Mute")              -- Set And Get
  40.  
  41. -- Set And Get Power Networks (Network is defined with ID and has configurable ports)
  42. table.insert(Programs, "Power")  -- Network{On, Off}, Network{Set, Edit, Remove [Input, Output] ports}, Network{List [Input, Output, All] ports and networks}, Network{Measure (Total Difference, Active Difference, Active Rate)}, Network{Ratio}
  43.  
  44.  
  45. os.loadAPI("BaseButler/utility.lua")
  46. os.loadAPI("BaseButler/peripherals.lua")
  47. os.loadAPI("BaseButler/interaction.lua")
  48.  
  49.  
  50. ---------------------------
  51. -- # Assistant Core Functions
  52. ---------------------------
  53.  
  54.  
  55. -- Asserts that a command is present in the given text
  56. function AssertCommand(text)
  57.  
  58.     if (text ~= nil) then
  59.  
  60.         if (string.len(text) >= CommandPhraseLength) then
  61.  
  62.             local comparison = string.sub(text, 1, CommandPhraseLength)
  63.             print("### Command Assertion ###")
  64.             print("Text: '" .. text .. "'")
  65.             print("Subbed Comparison: '" .. comparison .. "'")
  66.             print("Command Phrase: '" .. CommandPhrase .. "'")
  67.             print("### End Command Assertion ###")
  68.  
  69.             if (string.upper(comparison) == string.upper(CommandPhrase)) then
  70.                 -- Command Found
  71.                 return true
  72.             end
  73.  
  74.         end
  75.  
  76.     else
  77.  
  78.         print("Error - AssertCommand(text) - text == nil")
  79.  
  80.     end
  81.  
  82.     -- Command Not Found
  83.     return false
  84. end
  85.  
  86.  
  87. -- Parses identified command line in chat and calls relevant program
  88. function ParseCommand(text)
  89.    
  90.     if (text ~= nil) then
  91.         local parts = utility.Split(text)
  92.         local commandPhraseIndex = -1
  93.         local commandProgramIndex = -1
  94.         local matchedProgram = nil
  95.        
  96.         local programsCount = utility.Count(Programs)
  97.         local partsCount = utility.Count(parts)
  98.        
  99.         print("[DEBUG] Parts Count = " .. partsCount)
  100.  
  101.         local partIndex = 0 -- lua indexes start at 1, so increment at start
  102.         while (partIndex < partsCount) do
  103.  
  104.             partIndex = partIndex + 1
  105.  
  106.             local part = parts[partIndex]
  107.  
  108.             print("[DEBUG] Parts Index = " .. partsCount .. " | Part = " .. part)
  109.  
  110.             -- Identify Command Phrase
  111.             if (commandPhraseIndex == -1) then
  112.                
  113.                 if (string.upper(part) == string.upper(CommandPhrase)) then
  114.                     commandPhraseIndex = partIndex
  115.                 end
  116.  
  117.             -- Identify Command / Program
  118.             elseif (commandProgramIndex == -1) then
  119.  
  120.                 print("[DEBUG] Program Count = " .. programsCount)
  121.  
  122.                 local programIndex = 1 -- lua indexes start at 1, so increment at start      
  123.                 while (programIndex < programsCount) do
  124.                     programIndex = programIndex + 1
  125.  
  126.                     local program = Programs[programIndex]
  127.  
  128.                     print("[DEBUG] Program Index = " .. programIndex .. " | Program = " .. program)
  129.  
  130.                     if (string.upper(part) == string.upper(program)) then
  131.                         commandProgramIndex = programIndex
  132.                         matchedProgram = Programs[programIndex]
  133.                         print("[DEBUG] Program Matched!")
  134.                         break
  135.                     end
  136.  
  137.                     os.sleep(0) -- Allow for thread to yield
  138.                 end
  139.  
  140.                 if (matchedProgram ~= nil) then
  141.                     break
  142.                 end
  143.  
  144.             end
  145.  
  146.             -- Run Identified Program
  147.             if (matchedProgram ~= nil) then -- Maybe add option for searching args later
  148.                 print("[DEBUG] Running Program: " .. Programs[programIndex])
  149.                 RunProgram(Programs[programIndex])
  150.                 return true
  151.             end
  152.  
  153.         end
  154.  
  155.         os.sleep(0) -- Allow for thread to yield
  156.     end
  157.  
  158.     return false
  159. end
  160.  
  161.  
  162. -- Runs given program
  163. function RunProgram(text)
  164.  
  165.     if (text ~= nil) then
  166.         -- ToDo
  167.         -- Find Program Location (Probably just a programs folder)
  168.         -- Run Program
  169.     end
  170.  
  171. end
  172.  
  173.  
  174.  
  175. -- Main Process Function (TODO : Turn in to State Machine)
  176. function MainProcess()
  177.  
  178.     interaction.ComputerLine("Waiting for messages...", Monitor)
  179.     interaction.NewLine(Monitor)
  180.  
  181.     local inError = false
  182.     while inError == false do
  183.  
  184.         ChatBox = peripherals.AssertChatBoxPresent(ChatBox)
  185.         Monitor = peripherals.AssertMonitorPresent(Monitor)
  186.         interaction.RefreshDisplay(Monitor)
  187.  
  188.         local eventData = {os.pullEvent("chat")}
  189.         local event = eventData[1]
  190.         local username = eventData[2]
  191.         local message = eventData[3]
  192.            
  193.         local monitorChatLine = ("<" .. username .. "> " .. message)
  194.         interaction.ChatLine(monitorChatLine, Monitor)
  195.  
  196.         if (username == CommandUser) then
  197.             if (AssertCommand(message)) then
  198.                
  199.                 interaction.ComputerLine("~ Command Identified", Monitor)
  200.                
  201.                 if (ParseCommand(message)) then
  202.                     interaction.ComputerLine("~ Command Parsed", Monitor)
  203.                 else
  204.                     interaction.ComputerLine("~ Command Not Parsed", Monitor)
  205.                 end
  206.             end
  207.         end
  208.  
  209.         os.sleep(1)
  210.  
  211.     end
  212.  
  213.     interaction.ComputerLine("~ Terminating Instance", Monitor)
  214. end
  215.  
  216.  
  217.  
  218. ---------------------------
  219. -- # Start Main Process
  220. ---------------------------
  221.  
  222. MainProcess()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement