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

[Question][Solved]Debugging a program help?

Started by BurntSideUp, 18 December 2012 - 10:09 AM
BurntSideUp #1
Posted 18 December 2012 - 11:09 AM
I created program to dig a 5x5 tunnel that will go however many blocks you want long. At every 10 blocks, it should place a torch in the middle of the tunnel and at every 50 blocks it should place a chest and empty its inventory and continue along. It works fine for the first two rows of blocks forward, but after that, it bugs out and starts doing weird things, so I haven't been able to test the torch and chest placements. Here's the code (Sorry for the size! I wanted to be as thorough as possible!):

Just to help, slot 1 in inventory is torches, slot 2 is a block that will be placed down, slot 15 is where the chests will be, and slot 16 is where the fuel source is going. Also, any places where I could make the code smaller would be greatly appreciated! Thanks!


--Variables
turtle.select(1)
local tArgs={...}
local togo=tonumber(tArgs[1])
togo=togo or 1
print ("Making tunnel!")

--Functions
function tfuel()
  turtle.select(16)
  turtle.refuel()
  turtle.select(1)
end

function digRightFirst()
d = 1
repeat
  if turtle.detect() then
  repeat
   turtle.dig()
   sleep(1.0)
  until turtle.detect()==false
   turtle.forward()
   if turtle.detectDown() then
	sleep(0.25)
   else
	turtle.select(2)
	turtle.placeDown(1)
   end
  else
   turtle.forward()
   if turtle.detectDown() then
	sleep(0.25)
   else
	turtle.select(2)
	turtle.placeDown(1)
   end
  end
d = d + 1
until d > 4
end

function moveUp()
if turtle.detectUp() then
  repeat
  turtle.digUp()
  sleep(1.0)
until turtle.detectUp()==false
  turtle.up()
else
  turtle.up()
end

function dig()
j = 1
repeat
  if turtle.detect() then
  repeat
   turtle.dig()
   sleep(1.0)
  until turtle.detect()==false
   turtle.forward()
  else
   turtle.forward()
  end
until j > 4
end

function digRightFinal()
b = 1
repeat
  if turtle.detect() then
  repeat
   turtle.dig()
   sleep(1.0)
  until turtle.detect()==false
   turtle.forward()
  else
   turtle.forward()
  end
b = b + 1
until b > 4
end
turtle.turnRight()
turtle.turnRight()
end

function turnaround()
turtle.turnRight()
turtle.turnRight()
end

function inChest()
turtle.select(15)
turtle.drop()
  r = 3
  repeat
   turtle.select(r)
   turtle.drop()
   r = r + 1
  until r > 14
end

function down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
end

function forward2()
turtle.forward()
turtle.forward()
end

function reset()
turtle.forward()
turtle.forward()
turtle.turnRight()
end

--Main
for i = 1, togo do
tfuel()
turtle.dig()
turtle.forward()
turtle.turnRight()
digRightFirst()
moveUp()
dig()
moveUp()
dig()
moveUp()
dig()
moveUp()
digRightFinal()
turnaround()
down()
forward2()
if togo >= 10 then
  if(i % 10 == 0) then
   turtle.turnLeft()
   turtle.place()
   if(i % 50 == 0) then
	turtle.up()
	inChest()
	turtle.select(1)
   end
   turtle.turnRight()
  end
end
reset()
end

--Returning to start
turnaround()
for q = 1, togo do
tfuel(1)
turtle.forward()
end
turnaround()
turtle.select(1)

EDIT: Silly me! I figured out the problem. The code works as I want, but it was running out of fuel and not making it back to the beginning of the for loop before it ran out of fuel and would just start back up where it left off. I updated code for anybody who would like to use this!
remiX #2
Posted 18 December 2012 - 11:39 AM
First two blocks forward, what do you mean by that? Going to test it and check it out, brb :P/>

Edit: Are you sure it worked even for a bit? Because I copied and pasted and ran it and gave me an error for a missing 'then'. Added it and
Edit2: ^ Don't know what happened to that edit :P/> I added your code but it's bugging out hectically lol. When it get's to the second row it carries on digging, is that your problem>

Edit3: I'm not sure what you were doing with all those functions but this is how I would do it:

Spoiler
--Variables
turtle.select(1)
local tArgs={...}
local togo=tonumber(tArgs[1])
if #tArgs ~= 1 or not togo then
    print("Usage: " .. fs.getName(shell.getRunningProgram()) .. " <length>")
    return
end
digF = turtle.dig
digU = turtle.digUp
moveF = turtle.forward
moveR = turtle.turnRight
moveL = turtle.turnLeft
moveB = turtle.back
moveU = turtle.up
moveD = turtle.down
place = turtle.placeDown
slotSel = turtle.select

print ("Making tunnel!")

turtle.select(16)
turtle.refuel()

--Main
for i = 1, togo do
    digF()
    moveF()
    digU()
    moveR()
    digF()
    moveF()
    digU()
    digF()
    moveF()
    digU()
    moveR() moveR()
    moveF() moveF()
    digF()
    moveF()
    digU()
    digF()
    moveF()
    digU()
    moveR() moveR()
    moveF() moveF()
    moveL()
    if togo >= 10 then
        if(i % 10 == 0) then
            moveU()
            moveB()
            slotSel(1)
            place()
            moveF()
            moveD()
        end
    end
end

--Returning to start
for i = 1, togo do
    moveB()
end

term.clear()
term.setCursorPos(1,1)
print("Done!")

I have no clue how to dropping items into chests works so I removed that but I'm sure you can add it in :)/> Have fun
BurntSideUp #3
Posted 18 December 2012 - 01:04 PM
Thank you! I wasn't sure how to get the code from outside of the game, so I was writing it down and didn't think of any syntax errors.
theoriginalbit #4
Posted 18 December 2012 - 01:35 PM
To get it out of the game, if the HTTP api is enabled, use "pastebin put programName" without quotes obviously