manaphoenix

Ficsit Networks [Manufacturers setting setter]

Sep 16th, 2020 (edited)
843
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[ Settings ]] --[[
  2.     The Clock Variable is the overclock value of the machine, the input is a value from 0 - 250.
  3.         Leave at -1 to not change the setting.
  4.     The Recipe Variable is the recipe you want the machine to be set to.
  5.         WARNING: This will clear the items currently in the machine (as in Delete).
  6.         Leave empty to keep the recipe as is.
  7.     The Standby Variable is whether the machine should be off or on, true is off, false is on.
  8.         Leave at -1 to not change the setting.
  9.     The Group Variable lets you only manipulate a certain set of machines based on their "nicks" as the mod calls them
  10.         Leave empty to manipulate all machines on the line.
  11.         note: including multiple nicks acts as an AND logic gate, meaning the machine must have all the given nicks.
  12.        
  13.     WARNING: At the time of writing (9/16/2020) Ficsit networks is written in a way where my check uses machines with an overclock ability.
  14.         machines without an overclock ability will not work with this script!
  15.         The first and most popular that can be mentioned that will not be caught by this script is RefinedPowers buildings.
  16. ]]--
  17.  
  18. Clock = -1
  19. Recipe = ""
  20. Standby = -1
  21. Groups = ""
  22.  
  23.  
  24.  
  25.  
  26.  
  27. -- [[ Code DO NOT EDIT ]] --
  28.  
  29. local comps = {}
  30.  
  31. local function isAMachine(object)
  32.     if object.maxPotential > 1.0 then -- this will error for computers and other network components, and for storage components and things that still internally have a potential for some reason, will be caught because they can't go higher than 1.0 (since there not actual machines)
  33.         return -- no need to do anything fancy, this is an intential error check
  34.     else
  35.         error() -- pcall works off of errors, so we just return a generic error as a false
  36.     end
  37. end
  38.  
  39. local function findMachines() -- all sections broken up into functions, each with correct returns on whether something worked.
  40.     --[[
  41.         check to see if the group given exists within the network.
  42.         proxy always returns a table, even if no components exist.
  43.         So instead we use #table which returns how many elements are in the table,
  44.             note: the # operator only works for numbered elements
  45.             so if a table contains words or skips numbers, this will return the wrong result!
  46.             but proxy is always a numbered table so no worries there.
  47.            
  48.             FYI if you do want to count a mixed/named table, you will need to do it manually via "for i,v in pairs() do"
  49.     ]]
  50.     local proxy = component.proxy(component.findComponent(Groups)) -- cached to avoid multiple lookups
  51.     if (#proxy > 0) then
  52.         for _, i in pairs(proxy) do -- loop through every component in the group
  53.             --[[
  54.                 if the object is an actual machine, add it to the list.
  55.                 pcall works by calling a function, and if it errors it catches and returns false (for failure to run) and also lets the program continue
  56.                 the function looks like:
  57.                     pcall(FuncToCall,FuncArg1,...)
  58.                
  59.                 note: pcall can be a heavy performance call, so don't use it too often.
  60.             ]]--
  61.             if (pcall(isAMachine,i)) then
  62.                 table.insert(comps, i) -- add it to the list
  63.             end
  64.         end
  65.        
  66.         if (#comps > 0) then -- if at least one Machine was found, we can contiune
  67.             print("Found: " .. #comps .. " Machines")
  68.             return true
  69.         else -- otherwise our code ends here
  70.             print("No Machines Found, Cannot continue!")
  71.             return false
  72.         end
  73.     else -- the code also ends if no group was found.
  74.         print("Group set does not exist, did you mistype?")
  75.         return false
  76.     end
  77. end
  78.  
  79. local function setClockSpeed()
  80.     if Clock > -1 then -- We check to see if the user actually wants to change the clock speed from what it is
  81.         for _, j in pairs (comps) do -- for every machine in the table
  82.             j.potential = Clock / 100 -- set the clock speed
  83.         end
  84.         print("Machines set to " .. Clock .. "%") -- nice print out so the user can see whats going on
  85.         return true -- Success! these returns aren't used, but are here for users to write in their own expansion to the code
  86.     else
  87.         return false -- No change asked for!
  88.     end
  89. end
  90.  
  91. local function setRecipe()
  92.     --[[
  93.         check if the a recipe change was asked for.
  94.         the checked was switched to a len() (length of string) because checking against empty string means a new empty string "" object has to be created every time it wants
  95.         todo this check (basically performance)
  96.             note: you can either run string operations on a string object or through the string API
  97.                 example: string.len(stringObject) or just stringObject:len()
  98.     ]]--
  99.     if Recipe:len() > 0 then
  100.         for _, n in pairs (comps) do -- for every machine
  101.             if tostring(n) == "Manufacturer" then -- if its a machine that has a recipe handler
  102.                 local myRecipe = n:getRecipes()[Recipe] -- set the recipe to a variable so we don't do multiple lookups per loop
  103.                 if (myRecipe) then -- if the recipe is valid for the machine
  104.                     n:getOutputInv():flush() -- delete output inv
  105.                     n:getInputInv():flush() -- delete input inv
  106.                     n:setRecipe(myRecipe) -- set the recipe
  107.                 end
  108.             end
  109.         end
  110.        
  111.         print("Recipe set to: " .. Recipe) -- nice print out so the user can see whats going on
  112.         return true -- Success! these returns aren't used, but are here for users to write in their own expansion to the code
  113.     else
  114.         return false -- No change asked for!
  115.     end
  116. end
  117.  
  118. local function setStandby()
  119.     if Standby ~= -1 then -- check if a standby change was asked for
  120.         for _, m in pairs (comps) do -- for every machine
  121.             m.standby = Standby -- change the machine to its new standby
  122.         end
  123.        
  124.         print("All machines set to: " .. tostring(Standby)) -- nice print out so the user can see whats going on
  125.         return true -- Success, again these returns aren't used, but are here for users to write in their own expansion to the code
  126.     else
  127.         return false -- No change
  128.     end
  129. end
  130.  
  131. --[[
  132.     We attempt to fill the Machine table, if it returns true it continues
  133.         NOTE:
  134.             if (condition) then
  135.             is the same thing as
  136.             if (condition == true) then
  137.                 an if condition only checks if something isn't nil, not its actual value unless you do a explicit compare ==
  138.                     (technically even being explicit it still checks for nil, since it checks against the logic given and returns either false or true)
  139.                 and by luas terms false and nil are the same (conditional wise), so we don't have to be explicit.
  140.     Next we attempt to set the recipe,
  141.     finally we set clockspeed and whether the machine is in standby
  142. ]]--
  143. if findMachines() then
  144.     setRecipe()
  145.     setClockSpeed()
  146.     setStandby()
  147. end
  148.  
  149. --[[
  150.     this final printout was changed to state the end of the code,
  151.     we don't want to state "success" because it could have errored somewhere
  152. ]]--
  153. print ("Program Finished")
RAW Paste Data