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

Hollowhill mining program, multiple while loop conditions

Started by Cthonia, 18 August 2016 - 08:45 AM
Cthonia #1
Posted 18 August 2016 - 10:45 AM
Hello code-dwellers, I'd like get some help with my mining program. It's not a usual program, because it's made to mine out the Twilight Forest hollow hills. The concept is: the turtle clears the area layer by layer (16x16) then proceeds to the next level (16 cube high). It's quite slow for my liking, so I made new function to speed it up a litlle bit. My version sadly doesn't include the inspect commands (version 1.5). The process is, as the turtle dig the layers, it's scanning the blocks above it and if it's not dirt,stone or marble it excavate the ore pillar then return to the original layer and resume the spelunking. The problem is I don't know how to make a while loop with multiple conditions, meaning the turtle should descend whether it reaches dirt marble or stone. Any help appreciated:

Cthonia


function digv()
  if not turtle.detectDown() then
	turtle.select(13)
	turtle.placeDown()
  end
  while not turtle.compareUp() do
	turtle.select(12)
	while not turtle.compareUp() do
	  turtle.select(14)
	  while not turtle.compareUp() do
		turtle.digUp()
		turtle.up()
  end
  while not turtle.detectDown() do
	turtle.down()
  end
  turtle.digDown()
end
function strip()
  for i = 1,16 do
	turtle.dig()
	turtle.forward()
	turtle.select(12)
	if not turtle.compareUp() then
	  turtle.select(13)
	  if not turtle.compareUp() then
		turtle.select(14)
		if not turtle.compare() then
		  if turtle.detectUp() then
			digv()
		  end
		end
	  end
	end
  end
end
function fuel()
  if turtle.getFuelLevel() < 80 then
	turtle.select(16)
	turtle.refuel(1)
  end
end
function turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
end
function turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
end
function dump()
  turtle.select(15)
  turtle.dig()
  turtle.place()
  for i = 1,11 do
	turtle.select(i)
	turtle.drop()
  end
  turtle.select(15)
  turtle.dig()
end
function layer()
  fuel()
  strip()
  turnLeft()
  strip()
  turnRight()
  strip()
  fuel()
  dump()
  turnLeft()
  strip()
  turnRight()
  strip()
  turnLeft()
  strip()
  fuel()
  dump()
  turnRight()
  strip()
  turnLeft()
  strip()
  turnRight()
  strip()
  fuel()
  dump()
  turnLeft()
  strip()
  turnRight()
  strip()
  turnLeft()
  strip()
  fuel()
  dump()
  turnRight()
  strip()
  turnLeft()
  strip()
  turnRight()
  strip()
  fuel()
  dump()
  turnLeft()
  strip()
  turtle.forward()
  turtle.turnLeft()
  for i = 1,15 do
	turtle.dig()
	turtle.forward()
  end
  turtle.turnLeft()
  fuel()
  dump()
  turtle.digUp()
  turtle.up()
end
function cube()
  for i = 1,16 do
	layer()
  end
end
cube()
Bomb Bloke #2
Posted 18 August 2016 - 12:34 PM
The problem is I don't know how to make a while loop with multiple conditions, meaning the turtle should descend whether it reaches dirt marble or stone.

Simply link the conditions together using "and" / "or" keywords. You can furthermore use parentheses to group them.

In your case, a custom function for the sake of comparisons would also help. For example:

local function compareSlotUp(slotNum)
	turtle.select(slotNum)
	return turtle.compareUp()
end

while not (compareSlotUp(12) or compareSlotUp(13) or compareSlotUp(14)) do
	turtle.digUp()
	turtle.up()
end

You may also like to use custom functions to make your movement calls a bit more reliable - what happens if a mob gets in the way of a movement, for example? Or if you hit gravel or something?

local function up()
	while not turtle.up() do
		turtle.digUp()
		turtle.attackUp()
	end
end
Cthonia #3
Posted 18 August 2016 - 03:27 PM
Thanks, I searched heaven and earth but haven't found the answer. Should have known the solution is so simple!
I haven't thought about possible obstructions but makes sense.

Cthonia