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

MineByNight's Programs

Started by MineByNight, 19 December 2012 - 12:00 PM
MineByNight #1
Posted 19 December 2012 - 01:00 PM
Hey there, I'm MineByNight, and I just started to make some computercraft programs yesterday. I am almost completly new to programming and I want to be a programmer as a profession when I so I decided to start with this, since I love Minecraft and computercraft.

This is my first program. It is very simple, as all it does is mine in a single direction without end.


while true do --Always loop
if turtle.dig() == true then
  turtle.digUp()
elseif turtle.forward() == true then
  turtle.digUp()
   end
  end


This was all created by me. Feel free to use it in anyway. In my future code, where it is longer, i wll be posting on pastebin for easy access. I will be creating more program as time moves on, so look forward to my computercraft programs.
Please leave tips, edits, constructed feedback. Thanks!
ChunLing #2
Posted 19 December 2012 - 01:06 PM
Dig up and down for each forward move, so that you leave a tunnel. Otherwise you could find it difficult to retrieve your turtle.
MineByNight #3
Posted 19 December 2012 - 01:22 PM
Dig up and down for each forward move, so that you leave a tunnel. Otherwise you could find it difficult to retrieve your turtle.
Thanks :D/> I will edit it accordingly.
MineByNight #4
Posted 19 December 2012 - 01:37 PM
Dig up and down for each forward move, so that you leave a tunnel. Otherwise you could find it difficult to retrieve your turtle.
Dig up and down for each forward move, so that you leave a tunnel. Otherwise you could find it difficult to retrieve your turtle.
Thanks :D/> I will edit it accordingly.
Sorry, i don't know if you are still there but i am at

while true do --Always loop
if turtle.dig() == true then
  turtle.digUp()
elseif turtle.forward() == true then
  turtle.digUp()
   end
  end
and i want to make it so it mines up and it says text. is that possible or do i need to add on something like

if turtle.dig() == true then
print("Block Dug")
elseif turtle.forward() == true then
print ("No block found, moving on")
ChunLing #5
Posted 19 December 2012 - 02:25 PM
Conditionals in Lua can evaluate anything. If it isn't nil or false, then it counts as true. So you could do something like this:
local blocksDug,moves = 0,0
repeat
    turtle.digUp()
    turtle.digDown()
    if turtle.forward() then
        moves = moves+1
        print(moves.." Meters Moved")
    end
    if turtle.dig() then
        blocksDug = blocksDug+1
        print(blocksDug.." Blocks Dug")
    end
until turtle.detect() and not turtle.dig()
print("Unbreakable Block")
Of course, this doesn't count the blocks above and below the turtle that were dug, nor does it count all of the gravel/sand that might fall into the turtle's path (I would normally count these by checking the size of the stacks in the turtle's inventory anyway, not checking for success in dig operations). The point is the conditionals.

Using "turtle.dig() == true" instead of just turtle.dig() doesn't just take more space to write, it also makes the code execute an additional operation. Not, granted, a very difficult operation. But it does mean that just using turtle.dig() directly is definitely preferable.
MineByNight #6
Posted 19 December 2012 - 02:44 PM
Conditionals in Lua can evaluate anything. If it isn't nil or false, then it counts as true. So you could do something like this:
local blocksDug,moves = 0,0
repeat
	turtle.digUp()
	turtle.digDown()
	if turtle.forward() then
		moves = moves+1
		print(moves.." Meters Moved")
	end
	if turtle.dig() then
		blocksDug = blocksDug+1
		print(blocksDug.." Blocks Dug")
	end
until turtle.detect() and not turtle.dig()
print("Unbreakable Block")
Of course, this doesn't count the blocks above and below the turtle that were dug, nor does it count all of the gravel/sand that might fall into the turtle's path (I would normally count these by checking the size of the stacks in the turtle's inventory anyway, not checking for success in dig operations). The point is the conditionals.

Using "turtle.dig() == true" instead of just turtle.dig() doesn't just take more space to write, it also makes the code execute an additional operation. Not, granted, a very difficult operation. But it does mean that just using turtle.dig() directly is definitely preferable.

Thanks a lot :D/> this will really help!!
MineByNight #7
Posted 19 December 2012 - 03:08 PM
okay so now im at

local blocksDug, moves = 0,0
repeat
				   	  turtle.digDown()
						 turtle.forward()
					 if turtle.digDown() then
						 blocksDug = blocksDug+1
					 if blocksDug == 5 then
				   	  turtle.turnLeft()
					 end
until turtle.detect() and not turtle.digDown()
print("Bedrock Reached")
and i'm trying to make it so once it reaches 5 blocks foward, it turns. so it will make a square
MineByNight #8
Posted 19 December 2012 - 03:32 PM
i also just noticed that it said if you need help, go to the ask a pro. i can't seem to delete the post or move it, so i guess it'l stay until a moderator moves it
ChunLing #9
Posted 19 December 2012 - 03:54 PM
You can report your own thread if you really need it moved. But at this stage of development, I'd just refer you to examples of other programs in this forum anyway. Which you should do, of course. Ask a Pro is more for if you have error messages or unexpected behavior from your code you can't figure out.

You can nest loops, so let's say you wanted to do the basic "x by y" program.
local function tunnel()
    turtle.digUp()
    turtle.digDown()
    while not turtle.forward() do
        if turtle.detect() and not turtle.dig() then print("Unbreakable Block") return false
        else turtle.attack()
        end
    end
    return true
end
write("Enter tunnel width: ")
local width = tonumber(read())
write("Enter tunnel length: ")
local length = tonumber(read())
for i=1,width do
    for j=1,length do
        tunnel()
    end
    if i%2 == 0 then
        turtle.turnLeft()
        tunnel()
        turtle.turnLeft()
    else
        turtle.turnRight()
        tunnel()
        turtle.turnRight()
    end
end
Here we define the basic tunneling behavior as a function with a true/false return (we don't actually use this in this program, but it's good practice to design functions with a return). Then we prompt for user input of the width and length of the tunnel. And we use those values in a pair of nested loops so the turtle goes back and forth, tunneling.