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

First program tree chopper

Started by valithor, 17 May 2013 - 10:52 AM
valithor #1
Posted 17 May 2013 - 12:52 PM
This is my first program that actually does more than just moving forward and turning

This program will cut the trees replant and gather any drops from the trees any mobs though will interrupt the script. I know how to fix this problem just have not had time to do any corrections. I am open to advice on how to make this better.

Spoiler

local j = 0
local sleeptime = 0
term.write("enter sleep time")
sleeptime = read()
--countdown till next cycle
function timer()
  local time = sleeptime
	os.sleep(1)
	time = time-1
	print("time till next cycle:"..time)
  for i = 1, sleeptime do
	os.sleep(1)
	time = time-1
	print("time till next cycle:"..time)
end
end
  
-- digs and moves forward one
function dig()
  turtle.dig()
  turtle.forward()
end
--banks the wood
function bankwood()
  if turtle.getItemCount(1)>50 then
	turtle.select(1)
	turtle.dropUp (40)
  end
end
--keeps turtle stocked with saplings
function sapstock()
  if turtle.getItemCount(2)<1 then
	turtle.turnRight()
	turtle.turnRight()
	turtle.suck()
	turtle.turnLeft()
	turtle.turnLeft()
  end
end
--checks fuel and refuels if needed
function refuel()
  if turtle.getFuelLevel()<100 then
	print("getting fuel")
	if turtle.getItemCount(3)<1 then
	  turtle.suck()
	  turtle.select(3)
	  turtle.refuel(2)
	  turtle.select(1)
	else
	  turtle.select(3)
	  turtle.refuel(2)
	end
  end
end--digs up and moves up one
function digup()
  turtle.digUp()
  turtle.up()
end
-- checks if there is a sapling planted
-- where it is suppose to be
function sapling()
turtle.select(2)
  if turtle.compare() == false then
	turtle.place()
  end
end
--checks to see if there is grown tree
function grown()
turtle.select(1)
turtle.suck()
  if turtle.compare() then
	j = j+1
	dig()
	digup()
	digup()
	digup()
	digup()
	digup()
	turtle.down()
	turtle.down()
	turtle.down()
	turtle.down()
	turtle.down()
	turtle.back()
	turtle.select(2)
	turtle.place()
   end
end
-- to second tree
function second()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.turnRight()
end
--checks for other trees
function first()
  turtle.turnRight()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
end
function drops()
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.down()
  turtle.suckDown()
  turtle.suckDown()
  turtle.suckDown()
  turtle.up()
  turtle.back()
  turtle.turnRight()
  turtle.back()
  turtle.turnLeft()
  turtle.back()
  turtle.turnRight()
  turtle.back()
  turtle.turnLeft()
end
function extras()
  if turtle.getItemCount(4)>=1 then
	turtle.select(4)
	turtle.dropDown()
  end
  if turtle.getItemCount(5)>=1 then
	turtle.select(5)
	turtle.dropDown()
  end
end
print("logs = 1, sapling = 2, 3 = coal")
print("top chest = wood")
print("left chest = coal")
print("back chest = saplings")
print("warning mobs cause erros")
print("version 1.1")
local loops = 0
local sets = 0
term.write("how many sets were planted")
sets = read()
x = 3
while x==3 do
  for i = 1, sets do
	first()
	grown()
	sapling()
	drops()
	second()
	grown()
	sapling()
	first()
	grown()
	sapling()
	drops()
	second()
	grown()
	sapling()
  end
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
  for i = 1, sets do
	turtle.forward()
	turtle.forward()
	turtle.forward()
	turtle.forward()
	turtle.forward()
	turtle.forward()
  end
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
  sapstock()
  refuel()
  bankwood()
  extras()
  timer()
  print("ammount of fuel left:"..turtle.getFuelLevel())
  loops = loops+1
  local ratio = 0
  ratio = j/loops
  print("number of trees per loop:"..ratio)
  print("number of completed loops:"..loops)
  print("number of trees cut:"..j)
end

This is a very inefficient code that builds the farm that the above program uses all you have to do after using this is place the water to carry the drops
Spoiler

function tothetree()
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.turnLeft()
end
function chests()
  turtle.select(3)
  turtle.digDown()
  turtle.dig()
  turtle.digUp()
  turtle.place()
  turtle.placeUp()
  turtle.placeDown()
  turtle.turnRight()
  turtle.turnRight()
  turtle.dig()
  turtle.place()
  turtle.turnLeft()
  turtle.turnLeft()
endfunction signcount()
  if turtle.getItemCount(2)<=4 then
	turtle.select(2)
	if turtle.compareTo(4)==true then
	  turtle.select(4)
	  turtle.transferTo(2)
	end
	turtle.select(2)
	if turtle.compareTo(5)==true then
	  turtle.select(5)
	  turtle.transferTo(2)
	end
	turtle.select(2)
	if turtle.compareTo(6)==true then
	  turtle.select(6)
	  turtle.transferTo(2)
	end
	turtle.select(2)
	if turtle.compareTo(7)==true then
	  turtle.select(7)
	  turtle.transferTo(2)
	end
  end
  turtle.select(1)
end
function digup()
  turtle.digUp()
  turtle.up()
endfunction sign()
  turtle.turnRight()
  turtle.turnRight()
  turtle.select(2)
  turtle.place()
  turtle.select(1)
  turtle.turnRight()
  turtle.turnRight()
end
function digr()
  turtle.turnRight()
  turtle.dig()
  turtle.turnLeft()
endfunction dig()
  turtle.dig()
  turtle.forward()
end
function digd()
  dig()
  turtle.digDown()
endfunction base()
  digr()
  dig()
  dig()
  digr()
  dig()
  digr()
  dig()
  dig()
  turtle.turnRight()
  turtle.dig()
end
function between()
  dig()
  turtle.forward()
  turtle.digDown()
  turtle.turnRight()
  digd()
  sign()
  digd()
  sign()
  digd()
  sign()
  digd()
  sign()
  digd()
  sign()
  dig()
  sign()
  turtle.digDown()
  turtle.turnRight()
  dig()
  turtle.up()
  turtle.turnRight()
  turtle.turnRight()
  dig()
  dig()
  turtle.turnLeft()
  digd()
  turtle.down()
endfunction up()
  for i = 1, 7 do
   digup()
  end
end
function roof()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.place()
  turtle.turnRight()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.place()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
endfunction down()
  for i = 1, 7 do
	turtle.down()
  end
end
print("chest in slot 3")
print("sign in slot 2, fuel in slot 16")
print("extra signs in slot 4 - 7")
local times = 0
term.write("how many sets of trees: ")
times = read()
turtle.select(16)
turtle.refuel()  
turtle.select(1)chests()
tothetree()
turtle.digDown()
turtle.down()
function set()
  signcount()
  base()
  turtle.forward()
  between()
  up()
  roof()
  down()
  signcount()
  base()
  turtle.forward()
  between()
  up()
  roof()
  down()
endfor i = 1, times do
  set()
end

Both of these 2 programs were originally written about 3 or 4 days after i first started messing around with computer craft and slowly have been improved there are probably many ways to make this better.

http://www.youtube.com/watch?v=RI212RuTFus&feature=youtu.be
short little laggy video of the tree farm in action
mz2212 #2
Posted 09 August 2013 - 03:49 AM
This is some really impressive code for your first program