Posted 02 January 2013 - 02:35 AM
[Lua] [Question] Hi, I've begun trying to teach myself how to code in cc by writing a program (which is slightly useless, but that's not the point), but I've reached a wall. I'm wanting to add a new function that uses turtle.detect() to mine or attack if it can't move forward. I'll paste the full code, then the part with the functions separate before I go on;
What I'm asking is, how would I go about using detect() to first check if there's a block in front of it, then an entity, dig or attack if there is, and then move forward?
I've already used this command once in another program to just dig if detect=true, but I don't know how the code works in regards to entities such as mobs, and Google does not seem to be my friend today…
Also, if I've made any mistakes, or if I can clean the code in any way (Remembering that I'm using this to learn and am pretty much a beginner as of last night) I'd much appreciate it if they were pointed out.
Thanks for your time. ~Ben
EDIT: I messed up with the topic title, sorry…
-- Variables:
local f = ""
local x = ""
local y = ""
local z = ""
local cvar = ""
-- functions:
function sweep()
term.clear()
term.setCursorPos(1,1)
end
function tleft()
turtle.turnLeft()
if f == 1 or f == 2 or f == 3 then
f=f-1
else
f=3
end
end
function tright()
turtle.turnRight()
if f == 0 or f == 1 or f == 2 then
f=f+1
else
f=0
end
end
function frwd()
turtle.forward()
if f == 0 then
z=z+1
elseif f == 1 then
x=x-1
elseif f == 2 then
z=z-1
else
x=x+1
end
end
function up()
turtle.up()
y=y+1
end
function down()
turtle.down()
y=y-1
end
function detect()
if turtle.detect() then
-- MAIN PROGRAM:
-- Set Home:
print("Please state whether the turtle is facing N,E,S,W:")
term.write("...")
f=read()
if f == "N" then
f=2
elseif f == "E" then
f=3
elseif f == "S" then
f=0
elseif f == "W" then
f=1
else
f="You entered an incorrect variable!"
os.reboot()
end
sweep()
print("Please enter the X, Y & Z coordinates:")
print("...")
term.write("X = ")
x = read()
term.write("Y = ")
y = read()
term.write("Z = ")
z = read()
sweep()
print("F: "..f)
print("X: "..x)
print("Y: "..y)
print("Z: "..z)
term.write("Is this correct? y/n: ")
cvar = read()
if cvar == "n" then
os.reboot()
elseif cvar == "y" then
sweep()
print("Continuing...")
else
print("Incorrect input!")
os.reboot()
end
-- Set Ammount:
-- functions:
function sweep()
term.clear()
term.setCursorPos(1,1)
end
function tleft()
turtle.turnLeft()
if f == 1 or f == 2 or f == 3 then
f=f-1
else
f=3
end
end
function tright()
turtle.turnRight()
if f == 0 or f == 1 or f == 2 then
f=f+1
else
f=0
end
end
function frwd()
turtle.forward()
if f == 0 then
z=z+1
elseif f == 1 then
x=x-1
elseif f == 2 then
z=z-1
else
x=x+1
end
end
function up()
turtle.up()
y=y+1
end
function down()
turtle.down()
y=y-1
end
function detect()
if turtle.detect() then
What I'm asking is, how would I go about using detect() to first check if there's a block in front of it, then an entity, dig or attack if there is, and then move forward?
I've already used this command once in another program to just dig if detect=true, but I don't know how the code works in regards to entities such as mobs, and Google does not seem to be my friend today…
Also, if I've made any mistakes, or if I can clean the code in any way (Remembering that I'm using this to learn and am pretty much a beginner as of last night) I'd much appreciate it if they were pointed out.
Thanks for your time. ~Ben
EDIT: I messed up with the topic title, sorry…