Old Reactor Control Program that doesn't work anymore?

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
Hello!

I've just built a BigReactor in my current FTB Monster 1.1.2 world and I wanted something to automatically control it. I remembered that the last world I played on (which had something like 24, 7x7x7 reactors) each had a computer attached to it with a reactor control program that I'd gotten from the internet. I think it was this forum, though it could've been the CC one or just from Googling. Unfortunately, I can't find the code itself anywhere and I'm not even sure if it IS the code I used because I don't remember having any monitors plugged in.

Here's the code (THIS CODE IS NOT MINE):
Code:
-- Basic control for BigReactors-Reactor
-- BSD3 License
-- Emily Backes <[email protected]>

-- Uses the first monitor it finds, if any
-- May need 3x3 or larger for that
-- No log output or printer usage yet
-- Will work on adv comps but mouse event handling
--  would need to be added below
-- Suitable for use in /startup

-- Max energy in a reactor's internal cell
local emax=10000000

-- wrap everything in an exception handler
local ok,msg=pcall(function ()
local r
local m
local redirected=false
local p

function findDev (dType)
  local d
  for _,d in pairs(peripheral.getNames()) do
  if (peripheral.getType(d) == dType) then
  return peripheral.wrap(d)
  end
  end
  return nil, dType..": not found"
end

function setupDevs() 
  r=assert(findDev("BigReactors-Reactor"))
  if (not r.getConnected()) then
  return nil, "Computer port not connected to a valid reactor"
  end
  --if (r.getNumberOfControlRods() <1) then
  --  return nil, "Reactor seems invalid"
  --end
  r.getEnergyPercent = function ()
  return math.floor(1000 * r.getEnergyStored() / emax)/10
  end
  if r.nativeEPLT then
  r.getEnergyProducedLastTick = r.nativeEPLT
  end
  r.nativeEPLT = r.getEnergyProducedLastTick
  r.getEnergyProducedLastTick = function ()
  return math.floor(r.nativeEPLT()*1000)/1000
  end

  if redirected then
  term.restore()
  redirected = false
  end
  m=findDev("monitor")
  if m then
  m.setTextScale(1)
  term.clear()
  term.setCursorPos(1,1)
  print("Redirecting to attached monitor")
  term.redirect(m)
  redirected = true
  end

  term.setCursorBlink(false)
  p=findDev("printer")
end

function ft ()
  local d=os.day()
  local t=os.time()
  local h=math.floor(t)
  local m=math.floor((t-h)*60)
  return string.format("Day %d, %02d:%02d",d,h,m)
end

function log (msg)
  local stamp=ft()
  print (stamp..": "..msg)
end

function tableWidth(t)
  local width=0
  for _,v in pairs(t) do
  if #v>width then width=#v end
  end
  return width
end

function ljust(s,w)
  local pad=w-#s
  return s .. string.rep(" ",pad)
end

function rjust(s,w)
  local pad=w-#s
  return string.rep(" ",pad) .. s
end

function display()
  term.clear()
  term.setCursorPos(1,1)
  print("Reactor Status")
  print(ft())
  print("")
  local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","Temperature","FuelAmount","WasteAmount","FuelAmountMax","EnergyProducedLastTick"}
  local units={"","","","RF","%","C","mB","mB","mB","RF/t"}
  local values={}
  for _,v in pairs(funcs) do
  values[#values+1] = tostring(r["get"..v]())
  end
  local funcW=tableWidth(funcs)
  local valW=tableWidth(values)
  for i,v in pairs(funcs) do
  print(rjust(v,funcW)..": "..rjust(values,valW).." "..units)
  end
end

log("Starting")
setupDevs()
while true do
  local e=r.getEnergyStored()
  local p=math.floor(100*e/emax)
  local a=p<100
  local elt=r.getEnergyProducedLastTick()
  display()
  r.setAllControlRodLevels(p)
  r.setActive(a)
  os.startTimer(0.8333334)
  local event,p1,p2,p3,p4,p5 = os.pullEvent()
  if event == "key" then
  break
  elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
  setupDevs()
  elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
  error("received "..event)
  end
end

end)
term.restore()
error(msg)

I'm getting an error "attempt to call nil" on line 141 which is: "term.restore()" on the penultimate line, there.

This code was used just fine on Monster V1.1.1 many months ago (if it's what I think it is), so has something changed with any of the mods, or is there something obvious I'm doing wrong? Thanks!
 

rhn

Too Much Free Time
Nov 11, 2013
5,706
4,420
333
Hello!

I've just built a BigReactor in my current FTB Monster 1.1.2 world and I wanted something to automatically control it. I remembered that the last world I played on (which had something like 24, 7x7x7 reactors) each had a computer attached to it with a reactor control program that I'd gotten from the internet. I think it was this forum, though it could've been the CC one or just from Googling. Unfortunately, I can't find the code itself anywhere and I'm not even sure if it IS the code I used because I don't remember having any monitors plugged in.

Here's the code (THIS CODE IS NOT MINE):
Code:
-- Basic control for BigReactors-Reactor
-- BSD3 License
-- Emily Backes <[email protected]>

-- Uses the first monitor it finds, if any
-- May need 3x3 or larger for that
-- No log output or printer usage yet
-- Will work on adv comps but mouse event handling
--  would need to be added below
-- Suitable for use in /startup

-- Max energy in a reactor's internal cell
local emax=10000000

-- wrap everything in an exception handler
local ok,msg=pcall(function ()
local r
local m
local redirected=false
local p

function findDev (dType)
  local d
  for _,d in pairs(peripheral.getNames()) do
  if (peripheral.getType(d) == dType) then
  return peripheral.wrap(d)
  end
  end
  return nil, dType..": not found"
end

function setupDevs()
  r=assert(findDev("BigReactors-Reactor"))
  if (not r.getConnected()) then
  return nil, "Computer port not connected to a valid reactor"
  end
  --if (r.getNumberOfControlRods() <1) then
  --  return nil, "Reactor seems invalid"
  --end
  r.getEnergyPercent = function ()
  return math.floor(1000 * r.getEnergyStored() / emax)/10
  end
  if r.nativeEPLT then
  r.getEnergyProducedLastTick = r.nativeEPLT
  end
  r.nativeEPLT = r.getEnergyProducedLastTick
  r.getEnergyProducedLastTick = function ()
  return math.floor(r.nativeEPLT()*1000)/1000
  end

  if redirected then
  term.restore()
  redirected = false
  end
  m=findDev("monitor")
  if m then
  m.setTextScale(1)
  term.clear()
  term.setCursorPos(1,1)
  print("Redirecting to attached monitor")
  term.redirect(m)
  redirected = true
  end

  term.setCursorBlink(false)
  p=findDev("printer")
end

function ft ()
  local d=os.day()
  local t=os.time()
  local h=math.floor(t)
  local m=math.floor((t-h)*60)
  return string.format("Day %d, %02d:%02d",d,h,m)
end

function log (msg)
  local stamp=ft()
  print (stamp..": "..msg)
end

function tableWidth(t)
  local width=0
  for _,v in pairs(t) do
  if #v>width then width=#v end
  end
  return width
end

function ljust(s,w)
  local pad=w-#s
  return s .. string.rep(" ",pad)
end

function rjust(s,w)
  local pad=w-#s
  return string.rep(" ",pad) .. s
end

function display()
  term.clear()
  term.setCursorPos(1,1)
  print("Reactor Status")
  print(ft())
  print("")
  local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","Temperature","FuelAmount","WasteAmount","FuelAmountMax","EnergyProducedLastTick"}
  local units={"","","","RF","%","C","mB","mB","mB","RF/t"}
  local values={}
  for _,v in pairs(funcs) do
  values[#values+1] = tostring(r["get"..v]())
  end
  local funcW=tableWidth(funcs)
  local valW=tableWidth(values)
  for i,v in pairs(funcs) do
  print(rjust(v,funcW)..": "..rjust(values,valW).." "..units)
  end
end

log("Starting")
setupDevs()
while true do
  local e=r.getEnergyStored()
  local p=math.floor(100*e/emax)
  local a=p<100
  local elt=r.getEnergyProducedLastTick()
  display()
  r.setAllControlRodLevels(p)
  r.setActive(a)
  os.startTimer(0.8333334)
  local event,p1,p2,p3,p4,p5 = os.pullEvent()
  if event == "key" then
  break
  elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
  setupDevs()
  elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
  error("received "..event)
  end
end

end)
term.restore()
error(msg)

I'm getting an error "attempt to call nil" on line 141 which is: "term.restore()" on the penultimate line, there.

This code was used just fine on Monster V1.1.1 many months ago (if it's what I think it is), so has something changed with any of the mods, or is there something obvious I'm doing wrong? Thanks!
It is due to an API change in ComputerCraft. The "term.restore" was removed.
http://computercraft.info/wiki/index.php?title=Term.restore
I believe however that you can simply do a search and replace on the "term.restore" with "term.native" and it should work like intended again.
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
Awesome! Thanks, but unfortunately that doesn't entirely work...

Now I get the following error:

Code:
startup:142: startup:110: attempt to call nil

Line 110 is:
Code:
values[#values+1] = tostring(r["get"..v]())

Line 142 is the last line:
Code:
error(msg)
 

xyzzy75

New Member
Jul 29, 2019
86
0
0
Can't help you with LUA, but you might find it easier to just automate the reactor using MFR's rednet controller. That's what I always do, anyway.
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
Can't help you with LUA, but you might find it easier to just automate the reactor using MFR's rednet controller. That's what I always do, anyway.

Hmm. Never tried that before. I've always enjoyed programming little snippets of code to do things (and eventually giving up and finding somebody else's code) and that program was awesome. It changed the control rods depending on how much storage it had. Means that you can have no other automation on your Energy Cells or whatever else. It all just regulates itself - and you can add as many as you want, too. Have 20 reactors with a computer each and they'll decide between themselves which ones provide the steady flow of RF.

Anyways, I'll have a play around with the Rednet Controller on BRs, but if I can't figure it out, I'll come back here.

EDIT: Ok, I have no idea where to start with this...
 
Last edited: