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

Fence laying program

Started by Narny123, 22 February 2014 - 04:28 PM
Narny123 #1
Posted 22 February 2014 - 05:28 PM
Could someone help me. I'm terrible with all kinds of programming.

Is there a program out there that is easily modifiable… Or would some kind individual knock me one up. Basically I want a turtle to lay a 64 x 64 square fence. All on one level.

This is for use with the new ender quarry. I'm sure there are loads of people out there who would find this invaluable as well as myself.

For those that don't know the ender quarry, it's very similar to the usual quarry. But this one fills in all the blocks it takes out with dirt. Or grass if it's on surface level. Less lag, less hassle. But instead of using 3 markers like a regular quarry, this one requires a 64 x 64 (or larger I believe) fence laid out all on one level instead. It's such a pain in the arse to manually lay, but I'm told a turtle would be able to do this quite easily.

Thanks in advance.
Lyqyd #2
Posted 22 February 2014 - 07:13 PM
This is pretty trivial, really.


local selection = 1
turtle.select(1)

local function placeFence()
  turtle.digDown()
  turtle.placeDown()
  if turtle.getItemCount(selection) == 0 then
    selection = selection + 1
    turtle.select(selection)
  end
end

placeFence()
for i = 1, 4 do
  for i = 1, 63 do
    turtle.dig()
    turtle.forward()
    placeFence()
  end
  turtle.turnLeft()
end

It should be pretty obvious what's going on here. You'd fuel the turtle, then load it up with fences starting from slot one. Every time it places a fence, it checks to see if the current slot is empty, and if so, moves on to placing fences from the next slot. At the end of a row, it turns left (square will be placed in front of and to the left of the turtle's initial position), and it makes four rows of 63 blocks, for side lengths of 64.
Narny123 #3
Posted 24 February 2014 - 03:49 AM
thank you so much. I'll give this a try today.

What happens if the turtle runs into an obstacle? will it go through it? or just stop or turn?
Lyqyd #4
Posted 24 February 2014 - 10:18 AM
That script should just dig through most obstacles. It won't handle digging through stacks of gravel very well, so I'd suggest using it at the surface.
Narny123 #5
Posted 26 February 2014 - 09:15 PM
Works perfectly, thank you so much :)/>
Zaflis #6
Posted 01 March 2014 - 02:00 AM
I modified the above script a little to better suit my enderquarry needs (it does manage with the gravel, and digs high enough tunnel for player to follow through):
local size = 64

local index = 1
turtle.select(1)
local function place()
  while turtle.detectUp() do
    turtle.digUp()
  end
  turtle.digDown()
  turtle.placeDown()
  if turtle.getItemCount(index) == 0 then
    index = index + 1
    if index > 16 then index = 1 end
    turtle.select(index)
  end
end

place()
for i = 1, 4 do
  for j = 1, size-1 do
    while turtle.detect() do
      turtle.dig()
    end 
    turtle.forward()
    place()
  end
  turtle.turnRight() -- Goes clockwise
end

Remove the fence when done:
local size = 64
turtle.digDown()
for i = 1, 4 do
  for j = 1, size-1 do
    turtle.dig()
    turtle.forward()
    turtle.digDown()
  end
  turtle.turnRight()
end
edit: fixed tabs
Edited on 01 March 2014 - 01:09 AM
kreezxil #7
Posted 10 May 2014 - 09:37 PM
I added some function wrappers to this so that it is a more robust program now. Takes arguments for length and width and also whether or not build or remove a fence. Lots of credit is given to folks in this thread.

source at http://pastebin.com/xkPnRQZW and http://turtlescripts...9-Fence-Builder

pastebin: xkPnRQZW
turtlescripts: gjdhu9

you can view the code here as well,
Spoiler

--[[
Source: http://adf.ly/mQGKM
Authors: Lyqyd, Zaflis, Kreezxil
Ripped by: Kreezxil
Notes: Until now this code sat freely in forum, its still free, but now its encapsulated in a program that can be run and given arguments as well as loaded with 'pastebin get'
Changelog:
2/22/14 - Requested Concept by Narny123 - http://adf.ly/mQFxT
2/22/14 - Original Code by Lyqyd - http://adf.ly/mQG5h
2/28/14 - Modified Code by Zaflis - http://adf.ly/mQGD2
- now supports digging through gravel, and digs high enough tunnel for player to follow through
- added code to allow fence removal
5/10/14 - Modified Code by Kreezxil
- rearranged and encapsulated with function declarations parts of code to allow it to be in a program much easier
- added arguments for variable sized fence building and removing
- added help when arguments fail to match or nothing slot 1
--]]

local index = 1
turtle.select(1)

function place()
  while turtle.detectUp() do
	turtle.digUp()
  end
  turtle.digDown()
  turtle.placeDown()
  if turtle.getItemCount(index) == 0 then
	index = index + 1
  end
  if index > 16 then
	  index = 1
  end
  turtle.select(index)
end

function removeFence(length,width)
	turtle.digDown()
    for i = 1, 4 do
	  if i==1 or i==3 then
		  side=length
	  else
		  side=width
	  end
      for j = 1, side-1 do
        turtle.dig()
        turtle.forward()
        turtle.digDown()
      end
      turtle.turnRight()
   end
end

function buildFence(length,width)
  place()
  for i = 1, 4 do
    if i==1 or i==3 then
      side=length
    else
      side=width
    end
    for j = 1, side-1 do
      while turtle.detect() do
         turtle.dig()
      end
      turtle.forward()
      place()
    end
    turtle.turnRight() -- Goes clockwise
  end
end

function myName()
  fullName = shell.getRunningProgram()
  return fs.getName(fullName)
end

function mats()
  count=0
  for i=1,16 do
    count = count + turtle.getItemCount(i)
  end
  return count
end

function needed(length,width)
   return (length*2)+(width*2)
end

args = {...}

if #args < 3 then
  print("Usage:")
  print(" "..myName().." <length> <width> <remove|build>")
  print(" Turtle should also contain materials to be used as fences, preferably fences.")
  return
end

if tonumber(args[1])==nil or tonumber(args[2])==nil then
	print("Dimensions must be numerical.")
	return
end

if tonumber(args[1]) < 1 or tonumber(args[2]) < 1 then
  print("A dimension may not be less than 1")
  return
end

if mats() < needed(tonumber(args[1]),tonumber(args[2])) then
  print("Not enough materials in turtle to build fence, please add more materials.")
  return
end

if args[3]== "remove" then
  removeFence(tonumber(args[1]),tonumber(args[2]))
elseif args[3]=="build" then
  buildFence(tonumber(args[1]),tonumber(args[2]))
else
  print("The third option must be either 'remove' or 'build'")
end
Edited on 10 May 2014 - 11:15 PM