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

Need help with turtle program

Started by shawnmick23, 26 November 2014 - 09:25 AM
shawnmick23 #1
Posted 26 November 2014 - 10:25 AM
I'm practicing my turtle programming so that i can program a more advanced mining program to my liking however i can't seem to get a basic if statement to work the way that i think it should be working. if you look near the bottom i try to make it so at the end of each forward row it checks to see if we are on the last row. and if it is it should not round off to the next row. but for some reason even though my x is equal to my width it runs as if it isn't. not quite sure how to fix this. this code can also be found at http://pastebin.com/50MC25UQ.

some other possibly needed information.
computer craft version: 1.58
server: techworld 2 v1.1.6

local tArgs = { ... }
if #tArgs ~= 3 then
  print("Usage: mine <length> <width> <depth>")
  return
end
local function move(x,y)
shell.run("move",x,y)
end
local function status(x,y,z,depth,width,length)
print("x="..x.." y="..y.." z="..z.." depth="..depth.." length="..length.." width="..width)
end
local length = tArgs[1] - 1
local width = tArgs[2]
local depth = tArgs[3]
local distance = tArgs[1] + tArgs[2] + tArgs[3]
local x=0
local y=0
local z=0
for z=1,depth do
for x=1,width do
for y=1,length do
status(x,y,z,depth,width,length)
turtle.dig()
move("f","1")
sleep(.1)
end
status(x,y,z,length,width,depth)
d=bit.band(x,1)
if x == a then
print("x=a")
else
if d==0 then
print("x does not equal width")
status(x,y,z,length,width,depth)
move("l","0")
turtle.dig()
move("f","1")
move("l","0")
elseif d==1 then
move("r","0")
turtle.dig()
move("f","1")
move("r","0")
else
move("f","0")
end
end
end
end
shawnmick23 #2
Posted 26 November 2014 - 03:01 PM
sry i forgot i have a move program that is how the turtle moves
pastebin code Rpte7cw2

local tArgs = { ... }
if #tArgs ~= 2 then
  print("Usage: move <u/d/l/r/f/b> <amount>")
  return
end
local function move(x)
if x == "l" then
turtle.turnLeft()
elseif x == "f" then
turtle.forward()
elseif x == "r" then
turtle.turnRight()
elseif x == "b" then
turtle.back()
elseif x == "u" then
turtle.up()
elseif x == "d" then
turtle.down()
end
end
local d = tArgs[1]
local n = tArgs[2]
local function nlr(d,n)
for i = 1, n do
  move(d)
end
end
local function lr(d,n)
move(d)
dir="f"
nlr(dir,n)
end
if d=="l" then
lr(d,n)
elseif d=="r" then
lr(d,n)
else
nlr(d,n)
end
valithor #3
Posted 26 November 2014 - 04:25 PM
Maybe I am completely blind and just cant see it, but where are you defining the variable "a"

edit: misread part of the code… this variable really isnt relevant
Edited on 26 November 2014 - 03:25 PM
Buho #4
Posted 26 November 2014 - 04:27 PM
EDIT: I was going off the first post, not the 2nd post which I didn't see.

First of all, shell.run("move", x, y) will not do what you think it will do. Just off the top of my head, this would be similar to what you want, though there are probably more efficient ways of doing things:


local function forward(dist)
	for x = 1, dist do
   	 while not turtle.forward() do --Tries to move forward 1, if it fails, it will keep trying until it succeedes
			--You may want to put turtle.dig() in here to clear out gravel (and simplify your code)
			--You may want to put turtle.attack() in here to get rid of mobs in the way
			os.sleep(1)
		end
	end
end

local function move(dir, dist)
	if dir == "f" then forward(dist) end
	if dir == "r" then turtle.turnRight() end
	if dir == "l" then turtle.turnLeft() end
end

That's just something to get you started using the Turtle API.

Regarding your specific problem, d is undefined, and so is bit.band(), I think. Here's what I use in my zig-zag algorithms (using the forward() function above, but one that includes a turtle.dig()):


	if x < width then --Do not go beyond the bounds if last row
		if x % 2 == 0 then
			turtle.turnLeft() --Turn left (assuming 1st row is x=1)
	 	   forward(1)
	 	   turtle.turnLeft()
		else
	 	   turtle.turnRight() --Turn right
	 	   forward(1)
	 	   turtle.turnRight()
		end
	end

A similar pattern for z can be done since it's similar to x and you don't want to descend too far down.

Also, since the forward() function has a distance parameter, you don't need the y variable anymore. Just call forward(length - 1) with a dig line inside forward(). (The -1 is because you're starting on the 1st spot.)

Finally, a tip, in Minecraft, z refers to a horizontal dimention, not the vertical dimension. In my own programs, I treat z as "forward" in local/relative coordinate space (with x as "rightward" and y as "downward" or "upward"). Using z as a vertical unit may confuse you or others in the future. Just something to keep in mind. As variables, it won't change how your program behaves.

Your first program is off to a great start, Shawn!
Edited on 26 November 2014 - 03:29 PM
shawnmick23 #5
Posted 26 November 2014 - 04:59 PM
cool thank you very much guys i really appreciate it ill test this
valithor #6
Posted 26 November 2014 - 05:42 PM
Just clarrifying that bit.band is actually part of the bit api.

Although it does exist, it is not the best thing for this situation a few examples

bit.band(3,1) == 1
bit.band(5,1) == 1
bit.band(7,1) == 1

bit.band(2,1) == 0
bit.band(4,1) == 0
bit.band(6,1) == 0

I expect that it acts differently than you expected it to act.
Edited on 26 November 2014 - 04:42 PM
Lyqyd #7
Posted 26 November 2014 - 07:59 PM
I'm fairly certain that that is what he's wanting that line to do. It's effectively the same as x % 2, but perhaps slower. Probably not enough to matter, though.
Bomb Bloke #8
Posted 26 November 2014 - 11:38 PM
Depends on the value of x - the higher it is, the slower getting the modulus will be. Performing a binary AND should always take the same amount of time, however.

Maybe I am completely blind and just cant see it, but where are you defining the variable "a"

edit: misread part of the code… this variable really isnt relevant

Actually I reckon you were on the right track - seems to me that all that needs to be done is to change the test from "x == a" to "x == width" and the script should then run pretty much as expected.
Edited on 26 November 2014 - 10:40 PM