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

Dance 2.0

Started by Zee, 03 January 2013 - 05:49 AM
Zee #1
Posted 03 January 2013 - 06:49 AM
Hey guys! I am kinda new in the turtles area, so I made a Dance 2.0 program to learn basics.
It has an auto-refueler.


-- Dance 2.0
-- By DJZ0404
-- This is my first venture into turtle programming.
-- EDITED 7/17/2013: Fixed it so it WOULD work. Also functioned it.
function refuel()
if turtle.getFuelLevel() > 200 then
  turtle.select(1)
  turtle.refuel(10)
  if turtle.refuel() == false then
   turtle.select(2)
   turtle.refuel(10)
   if turtle.refuel == false then
	turtle.select(3)
	turtle.refuel(10)
	if turtle.refuel() == false then
	 turtle.select(4)
	 turtle.refuel(10)
	 if turtle.refuel() == false then
	  turtle.select(5)
	  turtle.refuel(10)
	  if turtle.refuel() == false then
	   turtle.select(6)
	   turtle.refuel(10)
	   if turtle.refuel() == false then
		turtle.select(7)
		turtle.refuel(10)
		if turtle.refuel() == false then
		 turtle.select(8)
		 turtle.refuel(10)
		 if turtle.refuel() == false then
		  turtle.select(9)
		  turtle.refuel(10)
		  if turtle.refuel() == false then
		   turtle.select(10)
		   turtle.refuel(10)
		   if turtle.refuel() == false then
			turtle.select(11)
			turtle.refuel(10)
			if turtle.refuel() == false then
			 turtle.select(12)
			 turtle.refuel(10)
			 if turtle.refuel() == false then
			  turtle.select(13)
			  turtle.refuel(10)
			  if turtle.refuel() == false then
			   turtle.select(14)
			   turtle.refuel(10)
			   if turtle.refuel() == false then
				turtle.select(15)
				turtle.refuel(10)
				if turtle.refuel() == false then
				 turtle.select(16)
				 turtle.refuel(10)
				 if turtle.refuel() == false then
				  print("No fuel detected. Stopping...")
				  term.clear()
				  term.setCursorPos(1, 1)
				 end
				end
			   end
			  end
			 end
			end
		   end
		  end
		 end	  
		end
	   end
	  end  
	 end	
	end	  
   end		
  end		
end
end
repeat
sleep(.5)
turtle.turnLeft()
turtle.up()
turtle.down()
print("Get down!")
sleep(.5)
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.up()
turtle.backward()
turtle.turnLeft()
sleep(.5)
print("Check me out, I'm dancin'")
sleep(.5)
turtle.turnLeft()
print("I'm dancin'")
turtle.up()
turtle.down()
turtle.down()
until turtle.getFuelLevel() > 200
refuel()

EDIT: This was the best I could get it. I didn't know about loops when I made this code. I typed all of the original. Also, I included a certain code in the dancing function.


-- Dance 2.0
-- By DanJZ0404

-- Fueling.
function refuel()
  local ItemSlot = 1
  repeat
  turtle.select(ItemSlot)
  turtle.refuel(1)
  until(ItemSlot == 17 or turtle.refuel(1))
end
  turtle.getFuelLevel()
if turtle.getFuelLevel() > 200 then
  refuel()
end

-- Dance function
function dance()
  turtle.up()
  sleep(.5)
  turtle.up()
  sleep(.5)
  turtle.down()
  sleep(.5)
  turtle.down()
  sleep(.5)
  turtle.left()
  sleep(.5)
  turtle.right()
  sleep(.5)
  turtle.left()
  sleep(.5)
  turtle.right()
end

if turtle.getFuelLevel < 200 then
  repeat
	dance()
  until turtle.getFuelLevel > 200
end
I figured I would do this some justice. v3 here:

-- DanCe 3.0
-- by Dan

--Fueling code:
function refuel(fNum, slotNum)
 for i = 1, slotNum do
  turtle.select(slotNum)
  turtle.refuel(fNum)
 end
end

function dance()
  turtle.up()
  turtle.up()
  turtle.down()
  turtle.down()
  turtle.down()
  turtle.down()
  turtle.turnLeft()
  turtle.forward()
  turtle.back()
  turtle.turnRight()
  turtle.turnRight()
  turtle.forward()
  turtle.back()
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.forward()
  turtle.back()
  turtle.turnRight()
  turtle.turnRight()
  turtle.turnLeft()
  turtle.up()
  turtle.up()
end
press = os.pullEvent("char")
repeat
dance()
until press==28 or turtle.getFuelLevel() > 200
Vilsol #2
Posted 03 January 2013 - 07:02 AM
dear god the code is a mess
Ulthean #3
Posted 03 January 2013 - 07:21 AM
What Vilsol meant to say is that you can improve on the refueling code.
Imagine for example the following code:

-- Dance 2.0
-- By DJZ0404
-- This is my first venture into turtle programming.

-- ------------------ --
-- Refuels the turtle --
-- ------------------ --
function refuel()
  local fuelSlot=1
  repeat
    select(fuelSlot)
    fuelSlot=fuelSlot+1
  until (turtle.refuel(10) or (fuelSlot==17))
end

-- ------------------ --
-- Main dance routine --
-- ------------------ --
if turtle.getFuelLevel() > 200 then
  refuel()
end
sleep(.5)
turtle.turnLeft()
turtle.up()
turtle.down()
print("Get down!")
sleep(.5)
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.up()
turtle.backward()
turtle.turnLeft()
sleep(.5)
print("Check me out, I'm dancin'")
sleep(.5)
turtle.turnLeft()
print("I'm dancin'")
turtle.up()
turtle.down()
turtle.down()
print("I'm done with dancin'!")

Some small comments on your original code:

turtle.forward
if turtle.forward == false then
turtle.checkFuelLevel()
if turtle.checkFuelLevel() > 200 then
The first and third line here do nothing. Not even counting the fact that it should be turtle.forward() and turtle.getFuelLevel().
The return value of each function just gets dropped. You could either just do it in the if-statement, like you did in the second
and fourth line or store the return value in a variable.

Example:

success = turtle.forward()
if success == false then
  ...
end

And a final suggestion, when checking against boolean values (like you did in the if-statement of the last code fragment) you can just use;

success = turtle.forward()
if not success then
  print("Moving forward wasn't successful *sadface*")
end
or

success = turtle.forward()
if success then
  print("Moving forward WAS successful! *cheer*")
end
Ikkalyzte #4
Posted 03 January 2013 - 08:23 AM
Oh my eyes. Loops, they are useful. No offence, I had code like that once. But please, keep learning, so that can be cleaned up.
Zee #5
Posted 03 January 2013 - 08:47 AM
dear god the code is a mess
Oh my eyes. Loops, they are useful. No offence, I had code like that once. But please, keep learning, so that can be cleaned up.
What Vilsol meant to say is that you can improve on the refueling code.
Imagine for example the following code:

-- Dance 2.0
-- By DJZ0404
-- This is my first venture into turtle programming.

-- ------------------ --
-- Refuels the turtle --
-- ------------------ --
function refuel()
  local fuelSlot=1
  repeat
	select(fuelSlot)
	fuelSlot=fuelSlot+1
  until (turtle.refuel(10) or (fuelSlot==17))
end

-- ------------------ --
-- Main dance routine --
-- ------------------ --
if turtle.getFuelLevel() > 200 then
  refuel()
end
sleep(.5)
turtle.turnLeft()
turtle.up()
turtle.down()
print("Get down!")
sleep(.5)
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.up()
turtle.backward()
turtle.turnLeft()
sleep(.5)
print("Check me out, I'm dancin'")
sleep(.5)
turtle.turnLeft()
print("I'm dancin'")
turtle.up()
turtle.down()
turtle.down()
print("I'm done with dancin'!")

Some small comments on your original code:

turtle.forward
if turtle.forward == false then
turtle.checkFuelLevel()
if turtle.checkFuelLevel() > 200 then
The first and third line here do nothing. Not even counting the fact that it should be turtle.forward() and turtle.getFuelLevel().
The return value of each function just gets dropped. You could either just do it in the if-statement, like you did in the second
and fourth line or store the return value in a variable.

Example:

success = turtle.forward()
if success == false then
  ...
end

And a final suggestion, when checking against boolean values (like you did in the if-statement of the last code fragment) you can just use;

success = turtle.forward()
if not success then
  print("Moving forward wasn't successful *sadface*")
end
or

success = turtle.forward()
if success then
  print("Moving forward WAS successful! *cheer*")
end
Lol. I just starting messing with ComputerCraft yesterday. But, thanks for the info, guys!