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

Problem Dire's Code

Started by LoR3nT_83, 11 March 2015 - 12:24 PM
LoR3nT_83 #1
Posted 11 March 2015 - 01:24 PM
Hi I downloaded dire's code to control reactors and turbines, the problem is i got 6 turbines and 2 reactors whereas he has 1 of each. I have to adapt the code so.
Codes are here : Button : http://pastebin.com/zDB8PmEJ
and reactor : http://pastebin.com/A7v2sKyW

I have my 2 reactors (BigReactors-Reactor_4, BigReactors-Reactor_6) and my 6 turbines (BigReactors-Turbine_1, BigReactors-Turbine_2, …, BigReactors-Turbine_6)

But when i replace the original code:
  • p = peripheral.find("tile_blockcapacitorbank_name")
  • m = peripheral.find("monitor")
  • r = peripheral.find("BigReactors-Reactor")
  • t = peripheral.find("BigReactors-Turbine")



By :
  • p = peripheral.find("tile_blockcapacitorbank_name")
  • m = peripheral.find("monitor")
  • r = peripheral.find("BigReactors-Reactor_4, BigReactors-Reactor_6")
  • t = peripheral.find("BigReactors-Turbine_1, BigReactors-Turbine_2, BigReactors-Turbine_3, BigReactors-Turbine_4, BigReactors-Turbine_5, BigReactors-Turbine_6")

I have the error : "reactor:8:attempt to index ? ( a nil value)"
So it was not too easy, can someone help me please ? :)/>
Lupus590 #2
Posted 11 March 2015 - 02:44 PM
this is similar to a problem already (nearly) solved
http://www.computerc...eactor-program/
please check that your question has not already been asked and answered before posting
Edited on 11 March 2015 - 07:44 PM
LoR3nT_83 #3
Posted 11 March 2015 - 03:01 PM
Hi, lupus, i modified for my 6 turbines and 2 reactors
You said "to make it loop through the table".
How i can Do this ? I have to modify everytime r or t is used ?
[indent=1] r = table.pack(peripheral.find("BigReactors-Reactor_4, BigReactors-Reactor_6"))[/indent]
[indent=1] t = table.pack(peripheral.find("BigReactors-Turbine_1, BigReactors-Turbine_2, BigReactors-Turbine_3, BigReactors-Turbine_4, BigReactors-Turbine_5, BigReactors-Turbine_6"))[/indent]
Lupus590 #4
Posted 11 March 2015 - 06:47 PM

for i =0, length(reactorTable)
    reactor[i].getinfo
end

I'm a bit low on time at the moment but this should be enough that someone else will know what I meant to help you
Lyqyd #5
Posted 11 March 2015 - 06:52 PM
this is similar to a problem already (nearly) solved
http://www.computerc...eactor-program/
please check that your question has not already been asked and answered before posting

Similar, but not identical. We prefer one question-asker per topic, as it makes it easier to prevent confusion. Linking to topics with similar questions can be helpful as a reference, but do not tell people not to make their own topic. What we don't want is one person making multiple topics for questions about the same piece of code, and even in that case it's the moderators' jobs to take care of it.
Lupus590 #6
Posted 11 March 2015 - 07:07 PM
-snip-

sorry
LoR3nT_83 #7
Posted 11 March 2015 - 07:39 PM
thanks lupus, where i have to insert it ?
Bomb Bloke #8
Posted 11 March 2015 - 11:22 PM
You don't - that's pseudo-code, not something you can actually execute.

By :
  • p = peripheral.find("tile_blockcapacitorbank_name")
  • m = peripheral.find("monitor")
  • r = peripheral.find("BigReactors-Reactor_4, BigReactors-Reactor_6")
  • t = peripheral.find("BigReactors-Turbine_1, BigReactors-Turbine_2, BigReactors-Turbine_3, BigReactors-Turbine_4, BigReactors-Turbine_5, BigReactors-Turbine_6")

That's not how you use peripheral.find(). The whole idea of the function is to get devices of a given type without you having to know their names - you just provide their types.

Let's say you wanted "r" to point to a table holding wrapped versions of both reactors on your network. You'd just do:

r = { peripheral.find("BigReactors-Reactor") }

Likewise, if you want "t" to hold all four of your turbines:

t = { peripheral.find("BigReactors-Turbine") }

"find" returns them all. Dire's original code only keeps hold of the first value returned, but by gathering up all the results into a table, we can take the lot.

So now, instead of having one reactor referred to via "r", we instead have two - r[1] and r[2]. Likewise, instead of using "t" to refer to one turbine, we instead have t[1], t[2], t[3] and t[4].

For example, on line 498 of the reactor script, we have:

tempMB = r.getEnergyProducedLastTick()

We can no longer refer to plain old "r", and we would want "tempMB" to hold the sum of r[1].getEnergyProducedLastTick() and r[2].getEnergyProducedLastTick(). So we could replace that line with:

tempMB = r[1].getEnergyProducedLastTick() + r[2].getEnergyProducedLastTick()

Or, we could write something that'll work no matter how many reactors we have, taking advantage of for loops:

tempMB = 0
for i = 1, #r do  -- "#r" returns the number of entries in "r".
  tempMB = tempMB + r[i].getEnergyProducedLastTick()
end

When dealing with t.getActive(), you would need to take a different tactic - you can't "add together" boolean states. With that function, you might just pay attention to t[1].getActive(), and ignore the state of the other turbines (assuming they're the same). Of course, you'd need to make sure you do the same to t[2]/t[3]/t[4] as you do to t[1] when calling setActive(), in order to make sure that's the case.

Then in terms of t.getRotorSpeed(), you might take an average - calculate the sum, then divide by the number of turbines. You might not bother dividing, and just pretend you've got one super turbine with a top speed equal to the maximum of all four combined. Whatever.

Long story short, you'll need to go through and replace every reference to "t" or "r" with something that makes sense when dealing with multiples of those objects.
Rand_Al_Thorn #9
Posted 20 April 2015 - 04:21 AM
omg I have spent the last 3 days trying to solve or find the answer to this problem. Has anyone successfully rewritten this code to be able to handle 4 turbines? If so please forward on your work or pastebin it :)/>