Posted 15 June 2014 - 05:24 PM
Hi there !
alright this is my first topic here and also my first ComputerCraft script (first "structured" one).
So now u've got the context i'll now explain the problem !
First of all im going to ask for help on a usual problem already listed in the FAQ and i'm sorry for that but i can't help myself im lost :(/>.
I've wrote this "diggingApi" witch is actualy an improved script of the native computerCraft Tunel API but with the possibility to pass Widht Lenght and Height parameters throught the program,
nothing exeptional so far i guess im not the first one to write something like this but i've got to start somewhere.
As u may have noticed in the topic title i've got a looping issue(ArrayOutOfBoundexeption), i do understand that im calling functions the wrong way witch gives me this error what i don't understand (and that's where i obviously need some help) is how to loop thoose functions correctly.
so here's the problem the programs runs perfectly until i pass too high values in parameters, the programs runs and then crashes due to Bound Exeption at some point.
Basicly the program dig a Widht/height layer done by diging a line of the parametered width, diging up/down (realtive to the position the turtle ends on each layer),turning right/left(relative to the position the turtle ends the line digging process) and then repeats it as long as the Lenghts Paramater allows it
So im gonna put my script here and try to explain it step by step
Step 1 : setting up the variables
Where hauteur is Height, Largeur is Widht and Longeur is Lenght (sorry french here) set up by the user.
the count_H and Count_L variables are "witnesses" that gives me the information if the turtle has reached the Max Height/max Lenght value according to the parameters passed in the program
the Temoin_H and Temoin_L variables are witnesses too that gives me the information if the turtle is on Left/right side or if it reached the max lenght of the digging area so i can exit or digUp/Down the right way.
Step 2 : placing up the turtle the right way before looping digging actions
Here im digging up the first block of my area and then move in the turtle, as it's done im substracting 1 to my widht and lenght parameters assuming i just start the first layer of the programed area, i'm also adding 1 to my count_H variable as the turtle is now on the first "height stage" of my area (im sorry if it sounds weird im trying to explain it the best i can)
the if condition with the Temoin_L variable is here to get the information if the turtle is on the right/left side of the set up area and then turn it on the right direction depending on the value returned
the if conditon with the count_L variable is here to stop the program where count_L reaches is maximum value.
Step 3 : creating digging functions to loop
Function ligne : this function commands the turtle to dig forward as long as the Width parameter allows it,once it's done it will set up the Temoin_L variable so i can know witch side of the area is the turtle(right/left).
The if Count_H < Hauteur condition is here to command the turtle to call for the down/up digging functions once the width digging part is done and setting up the Temoin_H "witness" variable to kown witch side of the area is the turtle(maxUp/maxDown).
the if Count_H > hauteur and if count_H == 0 conditions are here to call for the depth function witch allow the turtle to start a new layer.
Up and Down functions commands the turtle to digUp/down and then turnLeft/right to start a new line function the proper direction determinated by the witness variables.
function depth : commands the turtle to start a new layer by diging 1 block forward on the parametered lenght variable or exit the loop once the maxLenght is reached
So far my program runs perfectly as long as i pass low values throught it but once i set up something big the turtle ends up crashing(ArrayOutOfBoundExeption)
I know i got a loop issue I've read the common issues post and im sorry if it's not allowed to post on problems like that here but i can't figure this out i wanted to know how to build up thoose functions the right way with some whilte true loops or whatever so i can enter big values without getting this error
I hope my english is not too bad lol it's pretty hard to explain a script process in a language u don't speak :s and of course that u can help me ppl !
TruiteSauvage
alright this is my first topic here and also my first ComputerCraft script (first "structured" one).
So now u've got the context i'll now explain the problem !
First of all im going to ask for help on a usual problem already listed in the FAQ and i'm sorry for that but i can't help myself im lost :(/>.
I've wrote this "diggingApi" witch is actualy an improved script of the native computerCraft Tunel API but with the possibility to pass Widht Lenght and Height parameters throught the program,
nothing exeptional so far i guess im not the first one to write something like this but i've got to start somewhere.
As u may have noticed in the topic title i've got a looping issue(ArrayOutOfBoundexeption), i do understand that im calling functions the wrong way witch gives me this error what i don't understand (and that's where i obviously need some help) is how to loop thoose functions correctly.
so here's the problem the programs runs perfectly until i pass too high values in parameters, the programs runs and then crashes due to Bound Exeption at some point.
Basicly the program dig a Widht/height layer done by diging a line of the parametered width, diging up/down (realtive to the position the turtle ends on each layer),turning right/left(relative to the position the turtle ends the line digging process) and then repeats it as long as the Lenghts Paramater allows it
So im gonna put my script here and try to explain it step by step
Step 1 : setting up the variables
hauteur = tonumber(read())
largeur = tonumber(read())
longueur = tonumber(read())
count_H = 0
count_L = 0
temoin_L = 0
temoin_H = 0
Where hauteur is Height, Largeur is Widht and Longeur is Lenght (sorry french here) set up by the user.
the count_H and Count_L variables are "witnesses" that gives me the information if the turtle has reached the Max Height/max Lenght value according to the parameters passed in the program
the Temoin_H and Temoin_L variables are witnesses too that gives me the information if the turtle is on Left/right side or if it reached the max lenght of the digging area so i can exit or digUp/Down the right way.
Step 2 : placing up the turtle the right way before looping digging actions
function run()
turtle.dig()
turtle.forward()
largeur = largeur - 1
longeur = longeur - 1
count_H = count_H + 1
if temoin_L == 0 then
turtle.turnRight()
ligne()
else if temoin_L == 1 then
turtle.turnLeft()
ligne()
end
if count_L > longeur then
exit()
end
end
Here im digging up the first block of my area and then move in the turtle, as it's done im substracting 1 to my widht and lenght parameters assuming i just start the first layer of the programed area, i'm also adding 1 to my count_H variable as the turtle is now on the first "height stage" of my area (im sorry if it sounds weird im trying to explain it the best i can)
the if condition with the Temoin_L variable is here to get the information if the turtle is on the right/left side of the set up area and then turn it on the right direction depending on the value returned
the if conditon with the count_L variable is here to stop the program where count_L reaches is maximum value.
Step 3 : creating digging functions to loop
function ligne()
for i=1,largueur do
turtle.dig()
turtle.forward()
end
if temoin_L == 0 then temoin_L = 1
else if temoin_L == 1 then temoin_L = 0
end
if count_H < hauteur
if temoin_H == 1 then
down()
end
if temoin_H == 0 then
up()
end
end
if count_H > hauteur then
temoin_H = 1
depth()
end
if count_H == 0 then
temoin_H = 0
depth()
end
end
Function ligne : this function commands the turtle to dig forward as long as the Width parameter allows it,once it's done it will set up the Temoin_L variable so i can know witch side of the area is the turtle(right/left).
The if Count_H < Hauteur condition is here to command the turtle to call for the down/up digging functions once the width digging part is done and setting up the Temoin_H "witness" variable to kown witch side of the area is the turtle(maxUp/maxDown).
the if Count_H > hauteur and if count_H == 0 conditions are here to call for the depth function witch allow the turtle to start a new layer.
function up()
turtle.digUp()
turtle.up()
count_H = count_H + 1
if temoin_L == 0 then
turtle.turnRight()
else if temoin_L == 1 then
turtle.turnLeft()
end
ligne()
end
function down()
turtle.digDown()
turtle.down()
count_H = count_H - 1
if temoin_L == 0 then
turtle.turnRight()
turtle.turnRight()
else if temoin_L == 1 then
turtle.turnLeft()
turtle.turnLeft()
end
ligne()
end
Up and Down functions commands the turtle to digUp/down and then turnLeft/right to start a new line function the proper direction determinated by the witness variables.
function depth()
if count_L <= longeur then
if temoin_L == 0 then
turtle.turnRight()
else if temoin_L == 1
then turtle.turnLeft()
end
turtle.dig()
turtle.forward()
count_L = count_L + 1
if temoin_L == 0 then
turtle.turnRight()
else if temoin_L == 1
then turtle.turnLeft()
end
ligne()
end
if count_L > longeur then
exit()
end
end
function depth : commands the turtle to start a new layer by diging 1 block forward on the parametered lenght variable or exit the loop once the maxLenght is reached
So far my program runs perfectly as long as i pass low values throught it but once i set up something big the turtle ends up crashing(ArrayOutOfBoundExeption)
I know i got a loop issue I've read the common issues post and im sorry if it's not allowed to post on problems like that here but i can't figure this out i wanted to know how to build up thoose functions the right way with some whilte true loops or whatever so i can enter big values without getting this error
I hope my english is not too bad lol it's pretty hard to explain a script process in a language u don't speak :s and of course that u can help me ppl !
TruiteSauvage