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

Program help

Started by The_Awe35, 25 January 2013 - 11:41 AM
The_Awe35 #1
Posted 25 January 2013 - 12:41 PM
Hey, I have a problem with my program. See it it a land clearing program and it is supposed to be a customizable size one, but its not really working that way. Right now it can only go with odd width integers or it wont work. It is messing up when I try to make it go back to its start location. I have it doing alternating rows of coming forward and back, but if when it reaches its end coming forward, my reseting won't work.

this is how it is now
(I am really sorry, but I have no idea how you guys make the little white boxes for code.)

x = 0
l = 35

turtle.turnRight()
turtle.turnRight()
while x < l do
turtle.forward
x = x+1
end
x = 0
turtle.turnLeft()
while x < l do
turtle.forward
x = x+1
end
x = 0
end


How can I make it so it works when it ends going any direction?

If you want to see the entire code here it is: http://pastebin.com/sPAUjADM
grand_mind1 #2
Posted 25 January 2013 - 02:11 PM
I don't really understand what you want it to do. Could you please explain a bit farther.
The_Awe35 #3
Posted 25 January 2013 - 04:38 PM
If I try to make it get back to its original position, it will ram into a wall if it coming towards one direction. I think I may have an idea of how to fix it though.
LBPHacker #4
Posted 25 January 2013 - 10:52 PM
It'll be something like this

local sizeX = ({...})[1] -- just some fancy argument-getting code I like
local sizeY = ({...})[2]
local fuelSlot = ({...})[3] or 1 -- optional 3rd argument for fuel slot (default: 1) (top left slot)
-- So now you can call you program like "<program name> 6 8", and it will clear a 6x8 area

--[[

This is the 6x8 area

********# <--- This # is the turtle
********
********
********
********
********

]]

turtle.safeforward = function() -- You've got a new function! YAY! This will refuel the turtle automatically (if possible)
    if turtle.getFuelLevel() == 0 then
        turtle.select(fuelSlot)
        while not turtle.refuel() do
            print("Refuel me! (Put some coal into slot #" .. fuelSlot .. ")")
            coroutine.yield()
        end
    end
    turtle.forward()
end

local dir = false
for i = 1, sizeX do
    for j = 1, sizeY do
        turtle.dig()
        turtle.safeforward()
    end
    if dir then
        turtle.turnRight()
        turtle.dig()
        turtle.safeforward()
        turtle.turnRight()
    else
        turtle.turnLeft()
        turtle.dig()
        turtle.safeforward()
        turtle.turnLeft()
    end
    dir = not dir -- so we will know which direction to turn
end
if dir then
    for j = 1, sizeY do
        turtle.dig()
        turtle.safeforward()
    end
    turtle.turnLeft()
    turtle.turnLeft()
end
turtle.turnRight()
for i = 1, sizeX do
    turtle.dig()
    turtle.safeforward()
end
turtle.turnLeft()
print("Done")


BTW code blocks:

(code) and (/code) but enclosed by square brackets instead

So (code)local var(/local) will be
local var

Keep in mind: square brackets
remiX #5
Posted 26 January 2013 - 06:08 AM
turtle.safeforward = function() – You've got a new function! YAY! This will refuel the turtle automatically (if possible)
if turtle.getFuelLevel() == 0 then
turtle.select(fuelSlot)
while not turtle.refuel() do
print("Refuel me! (Put some coal into slot #" .. fuelSlot .. ")")
coroutine.yield()
end
end
turtle.forward()
end

I would change turtle.forward() to this for gravel safe-ness just incase…


while not turtle.forward() do
	turtle.dig()
	turtle.attack() -- CC 1.4 you will need this for monsters
	sleep(0.4)
end

BTW code blocks:

(code) and (/code) but enclosed by square brackets instead

So (code)local var(/local) will be
local var

Keep in mind: square brackets

(/local)? xD


code here[[b]/[/b]code]
LBPHacker #6
Posted 26 January 2013 - 06:11 AM
(/local)

lol I messed it up :D/>
remiX #7
Posted 26 January 2013 - 06:15 AM
(/local)

lol I messed it up :D/>

Lol yeah.

But

it is so..
simple,
don't you
see?
[[b]/[/b]code]