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

Reactormanager stops with error

Started by pokeball3000, 12 August 2012 - 02:45 PM
pokeball3000 #1
Posted 12 August 2012 - 04:45 PM
i wanted a program to run some nuclear reactors. (i calculated their run- and cooldowntime before i wrote the code)
sorry about some german terms but i guess you should understand the code anyways.
the problem is, that after some time (about 1100 seconds) i get this error:
bios:38: vm error: java.lang.ArrayIndexOutOfBoundsException: 256
the code works perfectly with lower timers like 8 seconds run and 5 minutes cd (for a mark V)
Spoiler

--modes
function zyklus100()
  term.clear()
  term.setCursorPos(1,1)
  print("Soll ein Vollzyklus gestartet werden?")
  print("werden? ja / nein")
  print("Dauer: 2h 45m - EU: 8.000.000")
  auswahl = read()
  if auswahl == "ja" then
	rs.setOutput("back", true)
	az = 0
	zz = dauer
	euzyk = geseu
	cdz = cd
	timerstart()
  else
	neustart()
  end
end
function zyklus50()
  term.clear()
  term.setCursorPos(1,1)
  print("Soll ein Halbzyklus gestartet werden?")
  print("werden? ja / nein")
  print("Dauer: 1h 22m - EU: 4.000.000")
  auswahl = read()
  if auswahl == "ja" then
	rs.setOutput("back", true)
	az = 0
	zz = dauer * 0.50
	euzyk = geseu * 0.50
	cdz = cd * 0.50
	timerstart()
  else
	neustart()
  end
end
function zyklus25()
  term.clear()
  term.setCursorPos(1,1)
  print("Soll ein Viertelzyklus gestartet werden?")
  print("werden? ja / nein")
  print("Dauer: 51m - EU: 2.000.000")
  auswahl = read()
  if auswahl == "ja" then
	rs.setOutput("back", true)
	az = 0
	zz = dauer * 0.25
	euzyk = geseu * 0.25
	cdz = cd * 0.25
	timerstart()
  else
	neustart()
  end
end
function zyklus12()
  term.clear()
  term.setCursorPos(1,1)
  print("Soll ein Achtelzyklus gestartet werden?")
  print("werden? ja / nein")
  print("Dauer: 26m - EU: 1.000.000")
  auswahl = read()
  if auswahl == "ja" then
	rs.setOutput("back", true)
	az = 0
	zz = dauer * 0.125
	euzyk = geseu * 0.125
	cdz = cd * 0.125
	timerstart()
  else
	neustart()
  end
end
--functions core
function reinstall()
  term.clear()
  term.setCursorPos(1,1)
  print("Setup aendert Daten...")
  print("Bitte warten.")
  sleep(3)
  shell.run("akwmanager")
end
function neustart()
  print("Abbruch oder Fehler bei der Eingabe.")
  print("Neustart...")
  sleep(2)
  shell.run("m1menu")
end
--functions modes
function timerstart()
  az = az + 5
  if az > (zz - 1) then
	caz = 0
	czz = cdz
	rs.setOutput("back", false)
	cdtimerstart()
  else
	term.clear()
	term.setCursorPos(1,1)
	print("Reaktor laeuft...")
	print("Ausstoss/Zyklus  :"..euzyk.." EU")
	print("EU/sec		   :"..eusec.." EU")
	print("Verbleibende Zeit:")
	print(" "..az.." von "..zz.." Sekunden")  
	sleep(5.01)
	timerstart()
  end
end
function cdtimerstart()
  caz = caz + 5
  if caz > (czz - 1) then
	timerstop()
  else
	term.clear()
	term.setCursorPos(1,1)
	print("Reaktor kuehlt ab...")
	print("Verbleibende Zeit:")
	print(" "..caz.." von "..czz.." Sekunden")  
	sleep(5.01)
	cdtimerstart()
  end
end
function timerstop()
	print("Zyklus abgeschlossen.")
	print("Produzierte EU in diesem Zyklus:")
	print(euzyk.." EU")
	print("Enter - Hauptmenu")
	wait = read()
	shell.run("m1menu")
end
--var
geseu = 8000000
eusec = 40 * 20
dauer = 10000
cd = 14
--core
term.clear()
term.setCursorPos(1,1)
print("	Mark I Reaktor	")
print("======================")
print()
print("Bitte Funktion waehlen")
print("1/1-Zyklus starten (1)")
print("1/2-Zyklus starten (2)")
print("1/4-Zyklus starten (3)")
print("1/8-Zyklus starten (4)")
print("Reaktortyp aendern (5)")
auswahl = read()
if auswahl == "1" then
  zyklus100()
elseif auswahl == "2" then
  zyklus50()
elseif auswahl == "3" then
  zyklus25()
elseif auswahl == "4" then
  zyklus12()
elseif auswahl == "5" then
  reinstall()
else
  neustart()
end

I don't know how to optimize the code or how to do it better, beacause i just started using CC and/or Lua.
sometimes the error occurs earlier if a longer lagg occurs. i hope you can help me.
Lyqyd #2
Posted 12 August 2012 - 05:42 PM
If this program runs itself, that's the problem. Use a while true loop to keep it running instead.
pokeball3000 #3
Posted 12 August 2012 - 06:06 PM
it's running with a handful of programs.
installakwman - copies all programs to local computer (works)
deinstallakwman - deletes all files related to "akwmanager" (works)
akwmanager - mainprogram where you decide which reactortyp you are using (works)
m1menu - runs mark I with 100/50/25/12,5 % (thats the code i postet - breaks after ~1100 sec or longer laggs)
m2ctrl - runs mark II with a ~3min cycle (works)
m3ctrl - runs mark III with a ~3min cycle (works)
m4ctrl - runs mark IV with a ~1min cylce (works)
m5ctrl - runs mark V with a 7,5 sec cycle (works)
m1menu and the m*ctrl programs run the same code, difference is just the variables "dauer"(runtime), "cd"(cooldown), "eusec"(EU/sec)
and "geseu" (EU/cycle)

like i said just the code postet where i got problems. i think it's because the long runtime but i don't know how to optimize that.
what would a "while true loop" look like in my case?
Lyqyd #4
Posted 12 August 2012 - 09:33 PM
So, this program is m1menu, and every time the neustart() function is called, it runs m1menu? What's happening is that each time you try to "restart" the program this way, the instance that started the new instance is still running, waiting for the new one to end. So, over time, you build a massive pile of programs waiting for each next one to end. This eventually overflows the stack and causes the java errors.

I can't be entirely certain, but this may do what you want:


while true do
    geseu = 8000000
    eusec = 40 * 20
    dauer = 10000
    cd = 14
    --core
    term.clear()
    term.setCursorPos(1,1)
    print(" Mark I Reaktor  ")
    print("======================")
    print()
    print("Bitte Funktion waehlen")
    print("1/1-Zyklus starten (1)")
    print("1/2-Zyklus starten (2)")
    print("1/4-Zyklus starten (3)")
    print("1/8-Zyklus starten (4)")
    print("Reaktortyp aendern (5)")
    auswahl = read()
    if auswahl == "1" then
      zyklus100()
    elseif auswahl == "2" then
      zyklus50()
    elseif auswahl == "3" then
      zyklus25()
    elseif auswahl == "4" then
      zyklus12()
    elseif auswahl == "5" then
      reinstall()
    end
end

Obviously, you need to remove or modify the other calls to neustart(), so that you don't keep running another instance of the program.
pokeball3000 #5
Posted 14 August 2012 - 07:53 PM
the restart function is only triggered by "wrong" input or if something is canceld with "nein" (no)

and using while true didn't work either, at least not built in my original code. it gave me some ideas to test around with though.

but i realized something while looking at the code again now…
how do i terminate a program by itself but also run it again afterwards? (to prevent running double instances (?if i can say it like that))
Lyqyd #6
Posted 15 August 2012 - 12:19 AM
the restart function is only triggered by "wrong" input or if something is canceld with "nein" (no)

and using while true didn't work either, at least not built in my original code. it gave me some ideas to test around with though.

but i realized something while looking at the code again now…
how do i terminate a program by itself but also run it again afterwards? (to prevent running double instances (?if i can say it like that))

If you mean exiting the program because of a user instruction to exit, the 'break' keyword exits loops. If you mean keeping the program running by killing the current instance and starting a new one, you cannot do so. You must use a loop inside the program to continuously do whatever it is supposed to do.
Cranium #7
Posted 15 August 2012 - 12:51 AM
I thought you could use if shell.getRunningProgram() to tell if a program is running, and shell.exit() if it is.
Lyqyd #8
Posted 15 August 2012 - 12:59 AM
I'm not sure I want to know what would happen if a program were to attempt to kill its parent program. I do not suggest this.
Cranium #9
Posted 15 August 2012 - 01:33 AM
I've done it. If you use shell.exit() if no other shell is running, it acts like os.shutdown() Not sure if anything else is going on in the background, but leaving the GUI and coming back just restarts the computer.
Lyqyd #10
Posted 15 August 2012 - 01:34 AM
Ah, I had misinterpreted what you meant. My original post was referring to starting a new instance of a program, then that instance trying to in some way exit the instance that had started it, not itself.