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

[LUA]

Started by ksbd, 02 January 2013 - 01:35 AM
ksbd #1
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;


-- 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…
W00dyR #2
Posted 02 January 2013 - 02:44 AM

while turtle.detect() do
turtle.dig()
sleep(1)
end

This will simply detect if there is something, and while there is something, it will attack, and then sleep for 1 second, then detect again and see if there is anything, else it keeps repeating untill turtle.detect() == false
ksbd #3
Posted 02 January 2013 - 02:49 AM
so the turtle.dig() command also attacks?
ChunLing #4
Posted 02 January 2013 - 02:54 AM
This is what I use:
local mvfncs = {{turtle.forward,turtle.detect,turtle.dig,turtle.attack},
        {turtle.up,turtle.detectUp,turtle.digUp,turtle.attackUp},
        {turtle.down,turtle.detectDown,turtle.digDown,turtle.attackDown},}
local function mvdgK(drctn) -- i.e.(1 = forward, 2 = up, 3 = down)
    for i=1,64 do
        if mvfncs[drctn][1]() then return true end
        if mvfncs[drctn][2]() then
            if not mvfncs[drctn][3]() then print("unbreakable block in path") return false end
        elseif turtle.getFuelLevel() == 0 then print("Out of fuel" ) return false
        else mvfncs[drctn][4]() end
    end
    print("persistent impediment") return false
end
That's a bit obscure, so let's just do moving forward:
local function mvdgK()
    for i=1,64 do
        if turtle.forward() then return true end
        if turtle.detect() then
            if not turtle.dig() then print("unbreakable block in path") return false end
        elseif turtle.getFuelLevel() == 0 then print("Out of fuel" ) return false
        else turtle.attack() end
    end
    print("persistent impediment") return false
end
Now, we have a for loop to repeat all this up to 64 times, but mostly we'll exit that using one of several return values. The first thing we try is moving forward, if that works then we return true and call it a day (or a move, whatever). If it fails, then continue to the next conditional, if we detect a block then we try to dig it. If the dig is false (and it only occurs if the detect was true) then we have a block we can't break. Oh well, return false and give up. If there's no block in the way, we check to see if we're out of fuel, and if we have fuel then we attack whatever's in the way (we'll do that up to 64 times, so most things will die or leave and we'll be able to move).

This covers pretty much all the bases, but there are some things that can stop you from moving that you can't do anything about. So if the for loop ends without us moving, we give up.
ksbd #5
Posted 02 January 2013 - 03:12 AM
Okay, I think I get it. However, would this not move 64 blocks without something being in the way, but if there is something, it'll meet the obstruction, attempt to correct it the remaining number of times, and take away those attempts from the number of forward commands (If that makes any sense?) Or does it say for i = 1,64 move forward, and if you can't, then try correcting the problem, and once corrected, continue moving forward until you've hit 64 blocks?
I'll try testing it myself to see also.

Thank you, that was very informative.
ChunLing #6
Posted 02 January 2013 - 03:18 AM
No, it returns true (terminating the function) the first time that moving forward succeeds.
ksbd #7
Posted 02 January 2013 - 03:23 AM
Ah, right. Thank you very much!