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

Help refining program

Started by thatonedinoguy, 03 October 2014 - 06:09 PM
thatonedinoguy #1
Posted 03 October 2014 - 08:09 PM
Hi there! So, recently I just finished writing my first program, called "ZigZagerat" with the help of computercraft. It took me a little bit of trial and error, but for the most part it functions as I wanted it to. Just posting here before I upload it to the "programs" forum to see if anyone here has some input on what I could do to refine the code or improve functionality.

Here is what the program does
-digs a 1x3x1 tunnel in a pattern of 64 blocks, then turns, does another 64 blocks, then turns in the opposite direction, then turns again and repeats until terminated.
-places a torch every 8 blocks
-refuels itself when it detects that fuel source is less than 5
-refills torches when they are less than 5
-dedicates slots for fuel, torches, and 3 ender storage chests
-empty's inventory when the turtle can no longer pick up different types of items

So before I post, here is my to-do list.

-make a version that functions without ender storage
-remove "find fuel" and "find torch" function due to them being from before I used 3 ender storage chests
-implement the "press any key to stop" function
-allow input for custom length of tunnel
-provide more output for user friendly use
-temporarily stop the turtle if it runs out of torches
-make code more readable and with more comments!
-and more!

so, here it is!


local var1 = 0 -- forward movement count
local var2 = 0 -- torch placement count
local var3 = 1 -- torch slot
local var4 = 1 -- coal slot
local var5 = 0 -- starting inventory slot
local var6 = 0 -- turn left and turn right variable.
local fuelLevel = 0
local torchLevel = 0

local function placeFuelChest() -- this function makes sure that the area is ready to place the chest.
  if turtle.detectUp == true then
	turtle.digUp()
  end
  if turtle.detectDown() == true then
	turtle.up()
  end
  turtle.select(16)
  turtle.placeDown()
end
local function placeTorchChest()
  if turtle.detectUp == true then
	turtle.digUp()
  end
  if turtle.detectDown() == true then
	turtle.up()
  end
  turtle.select(12)
  turtle.placeDown()
end

local function placeItemChest()
  if turtle.detectUp == true then
	turtle.digUp()
  end
  if turtle.detectDown() == true then
	turtle.up()
  end
  turtle.select(13)
  turtle.placeDown()
end

local function removeFuelChest() -- removes chest after it's been placed
  turtle.select(16)
	if turtle.getItemCount(16) == 0 then
	  turtle.digDown()
	elseif turtle.getItemCount(16) > 1 then
	  turtle.dropDown()
	  turtle.digDown()
	end
end
local function removeTorchChest()
  turtle.select(12)
	if turtle.getItemCount(12) == 0 then
	  turtle.digDown()
	elseif tutle.getItemCount(12) > 1 then
	  turtle.dropDown()
	  turtle.digDown()
	end
end

local function removeItemChest()
  turtle.select(13)
	if turtle.getItemCount(13) == 0 then
	  turtle.digDown()
	else if turtle.getItemCount(13) > 1 then
	 turtle.dropDown()
	 turtle.digDown()
	end
end
end

local function findTorch() -- finds torch stack in inventory
  for i = 1,12 do
	turtle.select(14)
	  if turtle.compareTo(i) then
		var3 = i
	  end
	  turtle.select(var3)
  end
end

local function findFuel() -- finds the stack of fuel for turtle
  for i = 1,11 do
	turtle.select(15)
	if turtle.compareTo(i) then
	  var4 = i
	end
	turtle.select(var4)
  end
end

local function selectTorch()
  turtle.select(var3)
end

local function torch() -- counts down to 8 blocks in space, then places a torch
  if true then
	var2 = var2 + 1
	  if var2 == 8 then
		turtle.turnRight()
		selectTorch()
		turtle.dig()
		turtle.place()
		turtle.select(1)
		turtle.turnLeft()
		var2 = 0
	  end
  end
end

local function findFirstSlot()
  if true then
	if var3 > var4 then
	  var5 = var3 + 1
	elseif var3 < var4 then
	  var5 = var4 + 1
	end
  end
end

local function dumpToChest()
for i = var5,11 do
  if turtle.getItemCount(11) > 0 then
	placeItemChest()
	turtle.select(i)
	turtle.dropDown()
  end
  removeItemChest()
end
end

local function refillFuel()
  if turtle.getItemCount(var4) <= 5 then
	  placeFuelChest()
	  turtle.select(var4)
	  turtle.suckDown()
	  removeFuelChest()
  end
  if turtle.getFuelLevel() <= 5 then
	turtle.refuel(2)
  end
end
  
local function refillTorch()  
  if turtle.getItemCount(var3) <= 5 then
	placeTorchChest()
	turtle.select(var3)
	turtle.suckDown()
	removeTorchChest()
  end
end

local function inventoryManager()
  if true then
	refillTorch()
	refillFuel()
	dumpToChest()
  end
end

local function tunnel() -- makes 1x3x1 tunnel
	if true then
	  turtle.dig()
	  if turtle.forward() == false then
		repeat
		turtle.dig()
		sleep(0.25)
		until turtle.forward() == true
	  end
	  turtle.digUp()
	  turtle.digDown()
	end
end

local function tunnelRight()
  turtle.turnRight()
  repeat
	tunnel()
	torch()
	inventoryManager()
	var6 = var6 +1
  until var6 == 4
  turtle.turnRight()
  var6 = 0
end

local function tunnelLeft()
  turtle.turnLeft()
  repeat
	tunnel()
	torch()
	inventoryManager()
	var6 = var6 + 1
  until var6 == 4
  turtle.turnLeft()
  var6 = 0
end

local function tunnel64Forward() -- applies tunnel and forward motion as well as inventory management.
  repeat
	if not turtle.forward() then
	  tunnel()
	  print(var1)
	end
	inventoryManager()
	var1 = var1+1
	torch()
  until var1 >= 64
  var1 = 0
end

local function startUp()
  print("Thank you for choosing ZigZagarat as your main stripmining protacol. Please wait for startup")
  findFuel()
  turtle.refuel(2)
  findTorch()
  findFirstSlot()
end
local function movePattern()
  while true do
	tunnel64Forward()
	tunnelRight()
	tunnel64Forward()
	tunnelLeft()
  end
end
local function onKeyPress() -- planned feature to allow a "press any key to stop" command
  while true do
	print("Press any key to stop")
	local event = os.pullEvent()
	if event == "key" then
	  return
	end
  end
end

startUp()
inventoryManager()
movePattern()
Hiran #2
Posted 03 October 2014 - 09:08 PM
You can check if blocks on the sides arent stone/dirt/gravel. If they arent, they are ores, and you would want to mine them too.