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

Can someone please help me with this mining script

Started by Gob1000, 04 July 2017 - 03:07 PM
Gob1000 #1
Posted 04 July 2017 - 05:07 PM
I am aware it is messy and overly complex but I am incredibly new to programming and I'd appreciate any helpful solutions

– mining function
function mine()

for i = 1,200 do

– Dig a 3x3
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnLeft()
turtle.dig()
turtle.up()
turtle.dig()
turtle.digUp()
turtle.up()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.down()
turtle.dig()
turtle.down()
turtle.dig()
turtle.turnLeft()

– Refuel Function
if turtle.getFuelLevel <= 5
then

– Put fuel in this slot, slot 16
– Uses one coal per time
turtle.select(16)
turtle.refuel(1)
turtle.select(1)
end



if (turtle.getItemCount(16) < 15) then
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.down()
turtle.select(15)
turtle.placeUp()
turtle.select(16)
turtle.suckUp(30)
turtle.select(15)
turtle.digUp()
end

if (turtle.getItemCount(12) > 0) or (turtle.getItemCount(13) > 0) then

turtle.digUp()
turtle.up()
turtle.digUp()
turtle.down()
turtle.select(14)
turtle.placeUp()
turtle.select(1)
turtle.dropUp(64)
turtle.select(2)
turtle.dropUp(64)
turtle.select(3)
turtle.dropUp(64)
turtle.select(4)
turtle.dropUp(64)
turtle.select(5)
turtle.dropUp(64)
turtle.select(6)
turtle.dropUp(64)
turtle.select(7)
turtle.dropUp(64)
turtle.select(8)
turtle.dropUp(64)
turtle.select(9)
turtle.dropUp(64)
turtle.select(10)
turtle.dropUp(64)
turtle.select(11)
turtle.dropUp(64)
turtle.select(12)
turtle.dropUp(64)
turtle.select(13)
turtle.dropUp(64)
turtle.select(14)
turtle.digUp()
turtle.select(1)
end

function stopDig()
print("Program ended by user")
for c = 1,distanceTraveled do turtle.back() end
distanceTraveled = 0
end
end
end
Lyqyd #2
Posted 05 July 2017 - 01:45 AM
Moved to Ask a Pro.
KingofGamesYami #3
Posted 05 July 2017 - 05:33 AM
I'm not really sure what the issue is – I don't know what the program is trying to achieve, or what it's doing wrong. However, I've cleaned it up a little.

-localized the functions (yay locals!)
-moved function declaration out of giant for loop (possibly part of a scope issue?)
-Indented code properly (always good)
-Replaced unnecessarily repetitive code with a for loop (shrinks the script, easier to understand)
-Some formatting changes (ei moved "then" to the same line as if, changed spacing up, etc.)

Spoiler

-- mining function
local function mine() 

	for i = 1,200 do

		-- Dig a 3x3 
		turtle.dig()
		turtle.forward()
		turtle.digUp()
		turtle.turnLeft()
		turtle.dig()
		turtle.up()
		turtle.dig()
		turtle.digUp()
		turtle.up()
		turtle.dig()
		turtle.turnRight()
		turtle.turnRight()
		turtle.dig()
		turtle.down()
		turtle.dig()
		turtle.down()
		turtle.dig()
		turtle.turnLeft() 

		-- Refuel
		if turtle.getFuelLevel <= 5 then 
			-- Put fuel in this slot, slot 16
			-- Uses one coal per time
			turtle.select(16) 
			turtle.refuel(1) 
			turtle.select(1)
		end



		if (turtle.getItemCount(16) < 15) then
			turtle.digUp()
			turtle.up()
			turtle.digUp()
			turtle.down()
			turtle.select(15)
			turtle.placeUp()
			turtle.select(16)
			turtle.suckUp(30)
			turtle.select(15)
			turtle.digUp()
		end

		if (turtle.getItemCount(12) > 0) or (turtle.getItemCount(13) > 0) then

			turtle.digUp()
			turtle.up()
			turtle.digUp()
			turtle.down()
			turtle.select(14)
			turtle.placeUp()

			for i = 1, 13 do
				turtle.select(i)
				turtle.dropUp(64)
			end

			turtle.select(14)
			turtle.digUp()
			turtle.select(1)
		end 
	end
end 

local function stopDig()
	print("Program ended by user")
	for c = 1,distanceTraveled do turtle.back() end
	distanceTraveled = 0
end