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

Expanding a layfence program

Started by TheGreekMan2, 16 June 2014 - 01:40 AM
TheGreekMan2 #1
Posted 16 June 2014 - 03:40 AM
for some reason this layfence program stops selecting slots at the 2nd one. Its supposed to lay fences as long as i want it to if the ender chest in slot 16 is full of fence which it will be. Idk if there are problems with the rest of the code(placing the chest and getting items out) because i havent gotten past the laying part. Any help is awesome thanks
ps i know the functions are unecesary but i didnt edit them out of my pastebin




local selection = 1
local z = 1
turtle.select(1)

local function placeFence()
turtle.digDown()
turtle.placeDown()
while not turtle.forward() do
turtle.dig()
end
end

local function howBig()
term.clear()
term.setCursorPos(1,1)
print("How long do you want the quarry?")
x = tonumber(read())
print("How wide do you want the quarry?")
y = tonumber(read())
print("Laying fence "..x.." by "..y)
end



local function restock()
if selection == 16 then
turtle.dig()
turtle.select(16)
turtle.place()
turtle.suck(z)
if turtle.getItemCount(z) == 64 then
z=z+1
end
if z == 16 then
turtle.dig()
end
end
end

local function layinFence()
if selection == 16 then
turtle.dig()
turtle.select(16)
turtle.place()
turtle.suck(z)
if turtle.getItemCount(z) == 64 then
z=z+1
end
if z == 16 then
turtle.dig()
end
end
if turtle.getItemCount(selection) == 0 then
selection = selection+1
turtle.select(selection)
end
for i=1,x-1 do
placeFence()
end
turtle.turnLeft()
for i=1,y-1 do
placeFence()
end
turtle.turnLeft()
for i=1, x-1 do
placeFence()
end
turtle.turnLeft()
for i=1, y-1 do
placeFence()
end
turtle.turnLeft()
end

howBig()
layinFence()
Edited on 16 June 2014 - 01:52 AM
Bomb Bloke #2
Posted 16 June 2014 - 04:58 AM
Your code runs from top to bottom; you can use functions to re-order things somewhat, but otherwise, just having a few lines of code to select the inventory slot doesn't mean the slot will change as soon as the current one runs dry - the slot will only change if you run that bit of code when the slot runs dry.

Like this:

local function placeFence()
	turtle.digDown()
	turtle.placeDown()

	while not turtle.forward() do
		turtle.dig()
	end

	if turtle.getItemCount(selection) == 0 then
		selection = selection + 1
		turtle.select(selection)
	end
end

You'd want to flesh that out so that once the selection hits 16, you run your enderchest code.
TheGreekMan2 #3
Posted 16 June 2014 - 06:49 AM
so something like this?
just tried to run this and my turtle is just standing still after placing the first block :/
okok it was just out of fuel the moving works fine but it skips placing a block when it moves and the ender chest sucking doesnt work for some reason
sorry for the mix up

local selection = 1
local z = 1
turtle.select(1)

local function placeFence()
turtle.digDown()
turtle.placeDown()

while not turtle.forward() do
turtle.dig()
end

if turtle.getItemCount(selection) == 0 then
selection = selection + 1
turtle.select(selection)
end
end
if selection == 16 then
turtle.dig()
turtle.select(selection)
turtle.place()
for i=1, 15 do
turtle.suck(z)
z=z+1
end
end
if z == 16 then
turtle.dig()
selection=1
end
if turtle.getItemCount(selection) == 0 then
selection = selection+1
turtle.select(selection)
end
end

local function howBig()
term.clear()
term.setCursorPos(1,1)
print("How long do you want the quarry?")
x = tonumber(read())
print("How wide do you want the quarry?")
y = tonumber(read())
print("Laying fence "..x.." by "..y)
end





local function layinFence()
for i=1,x-1 do
placeFence()
end
turtle.turnLeft()
for i=1,y-1 do
placeFence()
end
turtle.turnLeft()
for i=1, x-1 do
placeFence()
end
turtle.turnLeft()
for i=1, y-1 do
placeFence()
end
turtle.turnLeft()
end

howBig()
layinFence()

http://pastebin.com/KBK6nYyx
Edited on 16 June 2014 - 05:05 AM
Bomb Bloke #4
Posted 16 June 2014 - 07:06 AM
Does this turtle have any fuel?

You've got at least one "end" in the wrong place. Go through and indent your code, you'll have a much easier time keeping track of such things.
Edited on 16 June 2014 - 05:06 AM
TheGreekMan2 #5
Posted 16 June 2014 - 07:09 AM
i had it indented when i did it on my notepad xD
theoriginalbit #6
Posted 16 June 2014 - 07:11 AM
then when posting it on the forums, place it in [code][/code] tags
TheGreekMan2 #7
Posted 16 June 2014 - 07:20 AM
editted
Edited on 16 June 2014 - 04:42 PM
TheGreekMan2 #8
Posted 16 June 2014 - 06:42 PM
edit: the turtle still skips everyonce ina while but hes properly filling up now
problems: slot 17 is out of range, ender chest gets put in slot 15, skipping placing fences
any help is appreciated i know i suck :P/>


local selection = 1
local z = 1
turtle.select(1)
local function restock()
  turtle.select(16)
  turtle.place()
  for i=1,15 do
	turtle.select(z)
	turtle.suck(z)
	z=z+1
	if z==15 then
	  turtle.suck()
	  turtle.select(16)
	  turtle.dig()
	  turtle.select(1)
	end
  end
end

local function placeFence()
   turtle.digDown()
  turtle.placeDown()
  while not turtle.forward() do
	turtle.dig()
   end
  if turtle.getItemCount(selection) == 0 then
	selection = selection + 1
	turtle.select(selection)
   end

  if selection == 16 then
	turtle.restock()
  end
end

local function howBig()
  term.clear()
  term.setCursorPos(1,1)
  print("How long do you want the quarry?")
  x = tonumber(read())
  print("How wide do you want the quarry?")
  y = tonumber(read())
  print("Laying fence "..x.." by "..y)
end

local function layinFence()
  for i=1,x-1 do
	placeFence()
  end
  turtle.turnLeft()
  for i=1,y-1 do
	placeFence()
  end
  turtle.turnLeft()
  for i=1, x-1 do
	placeFence()
  end
  turtle.turnLeft()
  for i=1, y-1 do
	placeFence()
  end
  turtle.turnLeft()
end

howBig()
layinFence()
Edited on 16 June 2014 - 05:03 PM
TheGreekMan2 #9
Posted 16 June 2014 - 11:27 PM
anyone?
Bomb Bloke #10
Posted 17 June 2014 - 01:28 AM
Your restocking code looks a little more complex than it needs to be, and won't be very reliable as it is. How about:

local function restock()
	turtle.select(16)
	turtle.place()

	for i=1,15 do
		turtle.select(i)
		turtle.suck()  -- You can't specify a slot to suck "from" nor "into" with turtle.suck().
	end

	turtle.select(16)
	turtle.dig()
	turtle.select(1)
end

You might consider implementing a "while" loop in the "is the current slot empty?" check, for those situations where it finds the newly selected slot is also empty…
Edited on 16 June 2014 - 11:37 PM
TheGreekMan2 #11
Posted 17 June 2014 - 03:44 AM
Your restocking code looks a little more complex than it needs to be, and won't be very reliable as it is. How about:

local function restock()
	turtle.select(16)
	turtle.place()

	for i=1,15 do
		turtle.select(i)
		turtle.suck()  -- You can't specify a slot to suck "from" nor "into" with turtle.suck().
	end

	turtle.select(16)
	turtle.dig()
	turtle.select(1)
end

You might consider implementing a "while" loop in the "is the current slot empty?" check, for those situations where it finds the newly selected slot is also empty…

can you give an example
Bomb Bloke #12
Posted 17 June 2014 - 03:58 AM
while turtle.getItemCount(selection) == 0 do
  selection = selection + 1

  if selection == 16 then
    restock()
    selection = 1
  end

  turtle.select(selection)
end

Edit: Chopped out extra =
Edited on 18 June 2014 - 12:34 AM
TheGreekMan2 #13
Posted 17 June 2014 - 05:30 PM
why are there double equals in teh selection == selection + 1

i thought to redefine a variable for use you need to use one equals
TheGreekMan2 #14
Posted 17 June 2014 - 05:40 PM
WOOO it works beautifully ^ i used a single equal buuuuuuuuuuuut now the problem is when it destroys cobble (i wanna make a 2k by 2k ender quarry in the deep dark) it keeps cobble in its inventory which is a nuisance and idk how to get rid of it. Its a problem because when the turtle gets fences it gets 15 stacks so if there is cobble there wont be room for the chest


local selection = 1
local z = 1
turtle.select(1)
local function restock()
		turtle.select(16)
		turtle.place()
		for i=1,15 do
				turtle.select(i)
				turtle.suck()
		turtle.select(16)
		turtle.dig()
		turtle.select(1)
end
local function placeFence()
		turtle.digDown()
		turtle.placeDown()
        while not turtle.forward() do
				turtle.dig()
		end
        while turtle.getItemCount(selection) == 0 do
				selection = selection + 1
				turtle.select(selection)
		end

         if selection == 16 then
                restock()
                selection = 1
         end
         turtle.select(selection)
end

local function howBig()
     term.clear()
     term.setCursorPos(1,1)
     print("How long do you want the quarry?")
     x = tonumber(read())
     print("How wide do you want the quarry?")
     y = tonumber(read())
     print("Laying fence "..x.." by "..y)
end

local function layinFence()
     for i=1,x-1 do
          placeFence()
     end
     turtle.turnLeft()
     for i=1,y-1 do
          placeFence()
     end
     turtle.turnLeft()
     for i=1, x-1 do
          placeFence()
     end
     turtle.turnLeft()
     for i=1, y-1 do
          placeFence()
     end
     turtle.turnLeft()
end

howBig()
layinFence()


Final code if anyone wants to use/make corrections or improvments
Edited on 17 June 2014 - 03:43 PM
Bomb Bloke #15
Posted 18 June 2014 - 02:36 AM
why are there double equals in teh selection == selection + 1

My mistake.

buuuuuuuuuuuut now the problem is when it destroys cobble (i wanna make a 2k by 2k ender quarry in the deep dark) it keeps cobble in its inventory which is a nuisance and idk how to get rid of it.

Have the turtle only fill slots 1-14 with fences. Whenever it goes to dig, have it select slot 15 first, and then drop whatever's in that slot directly afterwards.
TheGreekMan2 #16
Posted 18 June 2014 - 03:07 AM
why are there double equals in teh selection == selection + 1

My mistake.

buuuuuuuuuuuut now the problem is when it destroys cobble (i wanna make a 2k by 2k ender quarry in the deep dark) it keeps cobble in its inventory which is a nuisance and idk how to get rid of it.

Have the turtle only fill slots 1-14 with fences. Whenever it goes to dig, have it select slot 15 first, and then drop whatever's in that slot directly afterwards.

what if it digs more cobble than 64? should i code a loop that makes it drop spot 15 (except one of them) if it gets to 64
Bomb Bloke #17
Posted 18 June 2014 - 03:10 AM
I'm saying do this for every dig action - select the slot, dig, then drop. It can only dig up one block at a time.

Obviously you'll need to switch the slot back to "selection" afterwards.
TheGreekMan2 #18
Posted 18 June 2014 - 03:57 AM
thank you sir bloke
Edited on 18 June 2014 - 02:05 AM