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

Complete mining script compile errors

Started by HelveticaPenguin, 31 December 2012 - 01:45 PM
HelveticaPenguin #1
Posted 31 December 2012 - 02:45 PM
Hello!

I was making a complete mining script for my turtles. It's supposed to make a hole down to level 6, clear out a room (X long by Y wide by 10 tall, you specify X and Y) and then make some kind of a branch mine. Currently, I have all the code up until the branch mine. Obviously, this is optimized for diamonds.

Unfortunately, my script has a couple compiler errors. I am fairly good at programming in languages like C and Java, but Lua is just so different that i'm almost completely lost (starting arrays at 1? why!?).

If someone could please look through my code and tell me why its not compiling right i'd be so thankful. The only problem is that ComputerCraft only shows you the first error it encounters (unknown symbol on line 19). I managed to change "elseif !turtle.detectDown() then…" to "else if turtle.detectDown() == false then…" and that worked. The next error, however, said something about wanting me to end the DetectMove function at line 22 which I obviously don't. Since I don't know how to fix this, I can't see any more errors, and I cant provide any more of them here, sorry.

Please consider fixing my code, thanks!
Here it is:


--Vars
local tArgs = { ... }
local curh = toNumber(tArgs[1])
local roomWidth = toNumber(tArgs[2])
local roomLength = toNumber(tArgs[3])
local placeLadder = 0
local ladder = 1
local cobble = 2
local right = 1
local left = 0
--Functions
function DetectMove(dir)
  if dir == "d" then
    if turtle.detectDown() then
	  turtle.digDown()
	  turtle.down()
    end
    elseif !turtle.detectDown() then
	  turtle.down()
    end
    else
	  error "something went wrong in detectMoveDown"
    end
  end
  elseif dir == "u" then
    if turtle.detectUp() then
	  turtle.digUp()
	  turtle.up()
    end
    elseif !turtle.detectUp() then
	  turtle.up()
    end
    else
	  error "something went wrong in detectMoveUp"
    end
  end
  elseif dir == "f" then
    if turtle.detect() then
	  turtle.dig()
	  turtle.forward()
    end
    elseif !turtle.detect() then
	  turtle.forward()
    end   
    else
	  error "something went wrong in detectMoveForward"
    end
  end
  else error "something went wrong in detectMove"
end
--Main Script
for a=1, curh-6 do
  if turtle.detect() then
    placeLadder = 1
  end
  DetectMove("d")
  if placeLadder == 1 then
    turtle.select(ladder)
    turtle.placeUp()
  end
end
DetectMove("f")
turtle.digUp()
for b=1, roomWidth do
  for c=1, roomLength do
   DetectMove("f")
   for d=1, 10 do
	 DetectMove("u")
   end
   for e=1, 10 do
	 turtle.down()
   end
   if right == 1 then
	 turtle.turnRight()
	 right = 1
	 left = 0
   end
   elseif left == 1 then
	 turtle.turnLeft()
	 left = 0
	 right = 1
   end
  end
end
Lyqyd #2
Posted 31 December 2012 - 03:09 PM
Your ifs are structured incorrectly.

if x then
elseif y then
else
end

One if, one end.
HelveticaPenguin #3
Posted 31 December 2012 - 03:28 PM
Your ifs are structured incorrectly.

if x then
elseif y then
else
end

One if, one end.

That's a weird way to structure if statements, but it worked so thanks!
ChunLing #4
Posted 31 December 2012 - 11:19 PM
It's novel for programming, but it's very natural, and allows somewhat better control over scope than conventional if else if end end type usage.
remiX #5
Posted 01 January 2013 - 01:30 AM
Also for your DetectMove function, turtle.detectUp/Down/or just detect() can only return true or false so you do not need to have an elseif there.

function DetectMove(dir)
    if dir == "d" then
        if turtle.detectDown() then
            turtle.digDown()
            turtle.down()
        else
            turtle.down()
        end
    elseif dir == "u" then
        if turtle.detectUp() then
            turtle.digUp()
            turtle.up()
        else
            turtle.up()
        end
    elseif dir == "f" then
        if turtle.detect() then
            turtle.dig()
            turtle.forward()
        else
            turtle.forward()
        end   
    end
end