This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Armitige's profile picture

Help with Reactor/Turbine control program

Started by Armitige, 21 November 2014 - 03:09 PM
Armitige #1
Posted 21 November 2014 - 04:09 PM
I recently posted a program I wrote to control a Big Reactors reactor/turbine system in the programs section of the forums which I've since removed. After doing a bunch of testing, it seems the program is easily broken.

The issue I'm having is adding turbines as peripherals. I want the program to dynamically turbines based on how many turbines are connected to the computer. I encounter a problem when the turbines don't have consecutive peripheral names ie "BigReactors-Turbine_0", "BigReactors-Turbine_1", etc.

Right now I'm using the often used wrapping function


function wrapThis(thing, x)
        local wrapped, i = nil, x
        while wrapped == nil and i <= 100 do
                wrapped = peripheral.wrap(thing.."_"..i)
                i = i + 1
        end
        if wrapped == nil then
                return nil
        else
                return wrapped
        end
end

and I'm attempting to wrap each turbine using a for loop


for i = 0, 3 do
			    t[i] = wrapThis("BigReactors-Turbine", i)
end

The problem is immediate. If the turbine labels don't start by ending with a "0", and increment by 1, then the wrapThis function will wrap the same turbine multiple times.

I'm far too inexperienced with lua to come up with a neat and tidy way to do what I want. I've looked at other programs that perform similar functions to mine (when everything is perfect), but I'm just not familiar enough with lua libraries to follow what's going on in most of them.

I'm not really looking for someone to supply me with code that will do the job, more after being pointed in the right direction.

Any tips would be greatly appreciated.
KingofGamesYami #2
Posted 21 November 2014 - 04:35 PM
There's an easy way to get peripherals:

peripheral.getNames()
Returns a table with all connected peripheral names (EG "monitor_0" or "right")

Then you can iterate through that, like this

local t = {}
for _, name in ipairs( peripheral.getNames() ) do
  if peripheral.getType( name ) == "BigReactors-Turbine" then --#I'm not sure of the peripheral type, you may want to test one in game
    t[ #t + 1 ] = peripheral.wrap( name )
  end
  --#if you want to wrap only the first three, you could insert a statement here checking the length of t, and breaking the loop if it is too high.
end
Armitige #3
Posted 21 November 2014 - 05:32 PM
Thanks so much. I've been looking at using peripheral.getNames(), but I was unsure how I could compare what was in the table.

Could you clarify what this line is doing?

t[ #t + 1 ] = peripheral.wrap( name )
What I can make out is it's storing the peripheral in the table t with an index that's 1 larger than the length of t?

You got the peripheral type right, btw. 8)

This is exactly what I needed. Had to re-write a few functions, but all seems to be working great. Will test some more.
KingofGamesYami #4
Posted 21 November 2014 - 06:47 PM

t[ #t + 1 ] = peripheral.wrap( name )

t[ number of indexes in t + 1 ] = peripheral.wrap( name )

You got it. You can use table.insert to achieve the same thing, but this is faster, especially if used several times.
Armitige #5
Posted 22 November 2014 - 04:44 AM
This was a huge help. It's actually made the program more versatile. It will still only work properly on a reactor/turbine setup the same size as the one I'm using (there's far too many variables with reactor/turbine setups to write a program that will not only monitor data, but manage control rod setting, turbine activity, and coil engagement), but it now scales properly based on the number of turbines that are connected to the reactor, from 1 to 4. Which means I can use it to control my current setup which has only 1 turbine in place so far. It also means that should I choose to set up a second reactor/turbine system on the server, the code you helped me with will allow me to just drop the program into a second control computer/monitor setup.

Heh, I wish I could give you 10 thumbs up for your help :lol:/>
Bomb Bloke #6
Posted 22 November 2014 - 04:49 AM
If you're on CC 1.6 or later, then peripheral.find() is an easier method again:

local t = {peripheral.find("BigReactors-Turbine")}
Armitige #7
Posted 22 November 2014 - 05:38 AM
If you're on CC 1.6 or later, then peripheral.find() is an easier method again:

local t = {peripheral.find("BigReactors-Turbine")}
Hmm, ok, thanks Bomb Bloke.
Will that store multiple peripherals of the same type as an array? ie t would be an array of turbines I could reference with indices?
Edited on 22 November 2014 - 04:41 AM
Bomb Bloke #8
Posted 22 November 2014 - 12:32 PM
Yes, the resulting table "t" would be exactly the same as if you built it using the code KoGY showed you above.
Armitige #9
Posted 22 November 2014 - 02:17 PM
Awesome, thanks mate! :D/>