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

Mining Program Help

Started by HunterForce, 03 September 2014 - 03:49 AM
HunterForce #1
Posted 03 September 2014 - 05:49 AM
Being extremely new to programming Ive been having trouble with a program for a mining turtle. I keep getting "turtle: 22: Expected number" but line 22 is the start of a new function. My lua editor says the syntax is valid. The program in question is supposed to mine a layer determined by user input length and width then decend 3 layers before repeating. As is mines it scans the ore above and below and compares if it is similiar to the trash slots 1-3. If its not then it mines. After every pass it empties any trash thats compared to slots 1-3. After every second pass it checks if slot 14 is occupied. If so it then returns the ore to a chest nest to the starting point.

Ive checked multiple sites as well as a lua editor that can debug and check syntax. So far nothing has helped. This is my third re-write with alterations so Im at my whits end.

Any help would be greatly appreciated be it a solution to the error or general adivce to help me improve my programming.

The program can be viewed at "http://pastebin.com/tFRpugwy".

Thanks in advance!
Edited on 03 September 2014 - 06:06 AM
Bomb Bloke #2
Posted 03 September 2014 - 08:33 AM
The error is saying that the problem is occurring on the 22nd line within the turtle API. Odds are that's because you're calling one of the turtle's functions with nil instead of a number, on line 79 of your own script - you put "i" instead of "k".
KingofGamesYami #3
Posted 03 September 2014 - 12:26 PM
Little suggestion for getting numerical values:

local level
repeat
  term.write( "Current Level: " )
  level = tonumber( read() )
until level
This prevents someone from entering something like "cow", because tonumber returns nil if it isn't a number.
HunterForce #4
Posted 03 September 2014 - 05:03 PM
The error is saying that the problem is occurring on the 22nd line within the turtle API. Odds are that's because you're calling one of the turtle's functions with nil instead of a number, on line 79 of your own script - you put "i" instead of "k".

Thanks for spotting that error and the explination. Ill try to look over it again and try to find if Im not calling a nil.


Little suggestion for getting numerical values:

local level
repeat
  term.write( "Current Level: " )
  level = tonumber( read() )
until level
This prevents someone from entering something like "cow", because tonumber returns nil if it isn't a number.

Thanks for the suggestion. Currently this program was only meant for me but that could be a very useful tip for other projects.
HunterForce #5
Posted 03 September 2014 - 08:29 PM
After making some edits and rearraging some things Ive got it to at least run. Now the problem is its not properly mining detected ores above and below. That would be in the cpmpareToOre() function someone if anyone wants to help take a look. Thanks for the input and advice to help me get it running though! A major step forward.
Edited on 03 September 2014 - 07:15 PM
Bomb Bloke #6
Posted 04 September 2014 - 03:40 AM
Assuming your compareToOre() function now looks like this:

--FUNCTIONS FOR GOLINE
function compareToOre()--detects ore on ceiling and floor and mines
        for k=1, 3 do
                turtle.select(k)
                        if not turtle.compareUp()then
                        turtle.digUp()
                        end
                        if not turtle.compareDown()then
                        turtle.digDown()
                        end
        end
end

Let's assume you've got stone, dirt and gravel in slots 1, 2 and 3 respectively.

Let's say the turtle moves above some stone. On the first iteration of this loop, the turtle will see that the block under it matches the block it has in slot 1, and so it won't dig.

On the second iteration, however, it'll compare that stone to its second slot - dirt - find a mismatch, and so dig up the stone.

Instead, you'd want to check against all three slots before deciding whether to dig. Something like:

--FUNCTIONS FOR GOLINE
function compareToOre()--detects ore on ceiling and floor and mines
        local shouldDigDown, shouldDigUp = true, true

	for k = 1, 3 do
		turtle.select(k)
		if turtle.compareUp() then shouldDigUp = false end
		if turtle.compareDown() then shouldDigDown = false end
	end
	
	if shouldDigUp then turtle.digUp() end
	if shouldDigDown then turtle.digDown() end
end
HunterForce #7
Posted 04 September 2014 - 06:05 PM
A much more elegant way of solving the problem than what I had come up with.
My ugly solution was:
function compareToOre()--detects ore on ceiling and floor and mines
turtle.select(1)
if not turtle.compareUp() then
turtle.select(2)
if not turtle.compareUp() then
turtle.select(3)
if not turtle.compareUp() then
turtle.select(4)
if not turtle.compareUp() then
turtle.digUp()
end
end
end
end
turtle.select(1)
if not turtle.compareDown() then
turtle.select(2)
if not turtle.compareDown() then
turtle.select(3)
if not turtle.compareDown() then
turtle.select(4)
if not turtle.compareDown() then
turtle.digDown()
end
end
end
end
end

Ive done some testing and the only issue left is the returnOre function not travel back to the main shaft before trying to go up to the chest.

My updated code is at http://pastebin.com/qubur7y3

Thanks again for all your help. I love seeing different and more elegant ways to do the same fuction.
Edited on 04 September 2014 - 04:09 PM