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

help with my first cc program

Started by MaryuZ, 26 June 2012 - 02:51 PM
MaryuZ #1
Posted 26 June 2012 - 04:51 PM
Hello CC forum.
I am MaryuZ and I've been playing minecraft with the Tekkit mod-pack for about 3 months now.
Today I've decided to learn about CC and use it in my Minecraft so I head over to the wiki and had a read.
I'm no stranger to coding i have some experience in C and C++ but none in Lua.
So I wrote a small program for a turtle that I would like to use.
I thought I had it all figured out but the turtle just doesn't do what I expect it to do.
I wanted a program that digs out an square area of height 2 (K x K x 2) centered on where i place the turtle until the turtles inventory is full. I would use this to clear out areas at map height 11 because that's where the lava is and I use lava to power my IC2 machines.
I decided i'll settle just for one that digs at first and do with the inventory checks after i get it to dig.
Here's what I wrote and it doesn't seem to work…I really don't know what i'm missing

function dig2()
turtle.dig()
turtle.forward()
	   while turtle.detectUp() do
"this is for gravel removal if gravel is above the unit"
	   turtle.digUp()
	   end
end
local i=0
print("How big a square?")
i=read()
for j=1,i
	 while j>0 do
	 dig2()
	 j=j-1
	 end
	 turtle.turnRight()
	 j=i
	 while j>0 do
	 dig2()
	 j=j-1
	 end
	 turtle.turnRight()
end
print("done")
with this code i was expecting the turtle to do this:
dig, turn, dig, turn, dig 2 blocks, turn, dig 2 blocks, turn, dig 3 blocks, turn, dig 3 blocks, turn, etc.
Can someone with more experience in lua please tell me what i'm doing wrong?
Just read a tutorial on this forum and realised how stupid my gravel detection system is because it's checking above and after it has moved forward….instead on in front and before it has moved forward….but that's not the problem i tested this in 3 deep dirt and still doesn't work so even if I fix this it still won't work…it just stops after it digs one block in front of the turtle.
OmegaVest #2
Posted 26 June 2012 - 05:25 PM
Hmm. Well, it would be simpler if we knew what the turtle does do. Or if it does nothing, then I would suggest you look at what i is bringing in. Most times, read will make the input a string, so you might need a tonumber around it.

Otherwise, yeah, your gravel detection is backwards, but not because it comes afterward, but because it is looking up at all. It would be simpler to make your dig2 function pause before seeing if there is still something in front of it after having successfully mined before. Which, obviously, requires you keep track of something like that. Though, luckily, the function to dig returns a boolean.
Exerro #3
Posted 26 June 2012 - 05:36 PM
if it is that exact code then

"this is for gravel removal if gravel is above the unit"
would make it not work…you have to put –(text) for notes but the rest looks fine
MaryuZ #4
Posted 26 June 2012 - 05:42 PM
…it just stops after it digs one block in front of the turtle.
MaryuZ #5
Posted 26 June 2012 - 05:44 PM
if it is that exact code then

"this is for gravel removal if gravel is above the unit"
would make it not work…you have to put –(text) for notes but the rest looks fine
that text is not in the code on the turtle, i just put it there to explaing why it's there on this version of the code.
Exerro #6
Posted 26 June 2012 - 06:24 PM
try using i=tonumber(read())
MaryuZ #7
Posted 26 June 2012 - 06:37 PM
ok i got it working with the folowing code but other problem have risen…

function dig2()
while turtle.detect() do
turtle.dig()
sleep(0.5)
end
turtle.forward()
while turtle.detectUp() do
turtle.digUp()
sleep(0.5)
end
end
local i,j
i=0
j=0
while turtle.getItemCount(9)==0 do
for i=1, 20 , 1 do
j=i
while j>0 do
dig2()
j=j-1
end
turtle.turnRight()
j=i
while j>0 do
dig2()
j=j-1
end
turtle.turnRight()
end
end
it mines….it mines as i want it to mine but it won't stop when an item get placed on slot 9 ( this is just until i write something better to save those extra 63 slots)
I would like it to keep mining until slot 9 is full or the block it is about to mine is different from anything in it's inventory…i have an idea on how to do this but i need to now if when it compares the block and the block in its inventory it can handle redstone and lapiz because it will be mining on layer 11 and both restone and lapiz (+nikolite) have different id's block and what you get when you mine it….know what I mean?
why doesn't my code stop when slot 9 gets an item?
edit….i think i know why…i have to check it before each mine event…right?
Lyqyd #8
Posted 26 June 2012 - 06:57 PM
Look at the code for the excavate program that comes with ComputerCraft and use the empty slot detection.

Also, turtles can't handle checking their inventory against gravel (flint), stone, coal, redstone, lapis, diamond (and from redpower: nikolite, ruby, emerald, sapphire; from IC2: uranium), so trying to check blocks rather than empty slots is a useless waste of time–it would work only on dirt, iron ore and gold ore.
MaryuZ #9
Posted 26 June 2012 - 10:24 PM
I got it to stop mining when it's inventory is full but now I wrote a part at the end that returns the turtle to the centerpoint(aprox) and i can't seem to get it to wark that way.
I keep getting the error that for limit must be a number and i don't know what to do.
the code is:

function dig2()
if turtle.getItemCount(9)==0 then
while turtle.detect() do
turtle.dig()
sleep(0.5)
end
turtle.forward()
while turtle.detectUp() do
turtle.digUp()
sleep(0.5)
end
end
end
local i,j,k
k=0
i=0
j=0
for i=1, 64 , 1 do
j=i
while j>0 do
dig2()
j=j-1
end
turtle.turnRight()
j=i
while j>0 do
dig2()
j=j-1
end
turtle.turnRight()
end
turtle.turnRight(2)
while not turtle.detect() do
turtle.forward()
k=i/2
k=math.floor(k)
end
turtle.turnLeft()
for j=1, j<k, 1 do
turtle.forward()
end
turtle.turnLeft()
for j=1, j<k, 1 do
turtle.forward()
end
print("Turtle Full")
What am i doing wrong?
Lyqyd #10
Posted 27 June 2012 - 01:27 AM
The problem is that the for limit must be a number. j<k is not a number, it is an expression that will return a boolean. You would want to use a while loop there and increment j or k yourself. Also, I think your code is going to have more substantial logic problems even once it is syntactically correct.
MysticT #11
Posted 27 June 2012 - 01:37 AM
The for loop should be:

for j = 1, k do
  -- code
end
MaryuZ #12
Posted 28 June 2012 - 01:18 AM
such a noob…using c syntax in lua and not even noticing…thx for the heads up
MaryuZ #13
Posted 28 June 2012 - 02:31 PM
how would i write a piece of code that makes the turtle go back to the center? and face the exact same direction?
im thinking of using a counter that counts whenever it digs and reset it after each turn right so i would alwasy know how far the turtle is on the side of the square…and another counter for each turn right so i would know how many times it has turned….any other ideas?…maybe simpler?
OmegaVest #14
Posted 29 June 2012 - 05:43 PM
Counting, no. If you are still using a loop that makes two turns, then just make it a boolean. So, in the context of your code,


turned = false  -- Instantiate

   -- Code snip


for i=1, 64 , 1 do
   j=i
   while j>0 do
	  dig2()
	  j=j-1
   end
   turtle.turnRight()
   j=i
   while j>0 do
	  dig2()
	  j=j-1
   end
   turtle.turnRight()
end

turned = not turned        -- New line

  -- etc.


Then, when moving back, which should be as simple as backing up half the originally input area, turning based on whether turned or not, and moving back or forward the half-distance again. And, if you know how far you have gone, you can roughly determine how far to go back if it should hit bedrock or something it can't dig. Just call j, or if j is greater than half i, then move back to half i and move backward normally.
MaryuZ #15
Posted 10 July 2012 - 06:14 PM
You lost me….:)/>/>)
i'm working on something else right now but i will eventually get back to this and finish it….meanwhile could you like show me what you mean to use boolean instead of a counter to determine the turtles orientation?….i want the exact orientation not just the axis …with bool i think i'd only get the axis with a counter and then fmod(counted, 4) i could get the exact orientation and also have the number of turns to reorient the turtle. in a variable
must not get distracted ……must work on other project……thx for helping me
OmegaVest #16
Posted 10 July 2012 - 09:35 PM
Okay, so this does only get you the axis. But, if you are still using a spiral-like pattern of digging, with two sides per dig, you only ever need to know the axis. Everything thing else can be handled by sub-variables or i and j in tandem.

I would give you some code for this, but the snippet I had got so jumbled I lost my place. I'll work on it and see if I can't make an example later.