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

Simple Turtle Builder

Started by thedriver87, 20 November 2013 - 02:04 AM
thedriver87 #1
Posted 20 November 2013 - 03:04 AM
This program is designed to allow a user to task turtles with basic building or digging jobs using an easy to understand interface, There are four patterns currently included in the turtles program, The Line. Square, Floor, and Wall patterns can be used to form most if not any basic structures, Giving the user time to focus on decorating, or other tasks while the rooms and hallways are built/hollowed. All patterns take in values for the length, height, and width when required. The Line building pattern will build until the turtle encounters an obstacle, making it ideal for long bridge builds or filling in floors and roofs with odd shapes that are just too large to spend the time building manually. In time I hope to incorporate a feature that will check for a config file on the disk first, if found the turtle will execute the set pattern immediately rather than needing to input the information repeatedly. The hope is to increase the ease of use when operating numerous turtles on a complex pattern.
It is also worth noting the predictability of the patterns in complete and as a result you can place blocks in the turtle in an order to produce any pattern you like an example of this is included at the bottom of the images

Source:
Spoiler

-- drone program used in building. written by jonathan ortiz
slot=1
choice=3
isFull=false
builder=true
gatherer=false
patternString="Line"
pattern=1
sideLength=0
sideHeight=0
sideWidth=0
builderString="Building"
drawinglength=false
drawingheight=false
drawingwidth=false
running=false
--checks if the turtle has material in any of the first 14 slots (last two slots are for fuel) will place a block below the turtle.--
--It is possible to script a pattern of 14 different stages by placing specific quantities into the turtle in from top left across then down.--
drawWorking=function()
term.setCursorPos(2,3)
term.write(" Currently:")term.write(builderString)term.write(" a ")term.write(patternString)
end
boarder=function()
term.clear()
term.setCursorPos(1,1)
print("===========Turtle Builder==============")
print("|									 |")
print("|									 |")
print("|									 |")
print("|									 |")
print("|									 |")
print("|									 |")
print("|									 |")
print("|									 |")
print("|									 |")
print("|									 |")
print("===========Turtle Builder==============")
end
mainhud=function()
term.setCursorPos(2,3)
term.write(" 1  Build/Break:")
term.setCursorPos(2,4)
term.write(" 2  Gather:")
term.setCursorPos(2,5)
term.write(" 3  Pattern:")
term.setCursorPos(2,6)
term.write(" 4  Run!")
term.setCursorPos(2,7)
term.write(" 5  Exit!")
if pattern>1 then
term.setCursorPos(1,8)
term.write("< 7  > Side Length: ")term.write(tostring(sideLength))
drawinglength=true
else
drawinglength=false
end
if pattern==4 or pattern==2 or pattern==3 then
term.setCursorPos(1,9)
term.write("< 8  > Side Height: ")term.write(tostring(sideHeight))
drawingheight=true
else
drawingheight=false
end
if pattern==1 and not builder then
term.setCursorPos(1,8)
term.write("< 7  > Side Length: ")term.write(tostring(sideLength))
end
if pattern==3 then
term.setCursorPos(1,10)
term.write("< 9  > Side Width: ")term.write(tostring(sideWidth))
drawingwidth=true
else
drawingwidth=false
end
end
drawStatus=function(builder,gatherer,patternString)
term.setCursorPos(18,3)
if builder then
builderString="Building"
else
builderString="Breaking"
end
term.write(builderString)
term.setCursorPos(13,4)
term.write(gatherer)
term.setCursorPos(14,5)
if pattern==1 then patternString="Line"
elseif pattern==2 then patternString="Square"
elseif pattern==3 then patternString="Floor"
elseif pattern==4 then patternString="Wall"
end
term.write(patternString)
end
drawChoice=function(choiceNum)
term.setCursorPos(2,choiceNum)
term.write("[")
term.setCursorPos(4,choiceNum)
term.write("]")
end
place=function()
counted=turtle.getItemCount(slot)
turtle.select(slot)
if counted<1 then
  empty=true
end

while empty do
  slot=slot+1
  if slot>14 then
   error("out of blocks")
  end

turtle.select(slot)
counted=turtle.getItemCount(slot)

  if counted>0 then
   empty=false
  end

  term.write("still empty")

end

turtle.placeDown()
end
--IMPORTANT! when a turtle is being used to collect blocks with this program the first slot must remain open as a buffer for new items coming in--
sort=function()
for i=2,14,1 do
  turtle.select(1)
  sames=turtle.compareTo(i)
  if sames then
   turtle.select(1)
   turtle.transferTo(i)
  end
end
  if turtle.getItemCount(1)>0 then
   for j=2,14,1 do
	empty=turtle.getItemCount(j)
	if empty==0 and turtle.getItemCount(1)>0 then
	 turtle.select(1)
	 turtle.transferTo(j)
	end
   end
   if turtle.getItemCount(1)>0 then
	isFull=true
	return
   end
  end
end
--takes up blocks around the turtle making use of the sort function to store them--
gather=function()
turtle.drop()
end
--Breaks the block below the turtle and gathers it optionaly. depends on boolean 'shouldGather'--
dig=function()
turtle.digDown()
if not gatherer==true then
gather()
end
end
digFront=function()
turtle.dig()
if gatherer==true then
--gather()
end
end
--breaks the block in front of the turtle and gathers it optionally. depends on the boolean 'shouldGather'--
--will check for fuel and the need for it then take the appropriate action.--
fuel=function()
fuelSlot=15
hasFuel=turtle.getFuelLevel()
if hasFuel<2 then
  turtle.select(fuelSlot)
  counted=turtle.getItemCount(fuelSlot)
  if counted>0 then
   turtle.refuel(1)
  else
   fuelSlot=fuelSlot+1
   turtle.select(fuelSlot)
   counted=turtle.getItemCount(fuelSlot)
   if counted>0 then
	turtle.refuel(1)
   end
  end
  hasFuel=turtle.getFuelLevel()
  if hasFuel<2 then
   error("Out of Fuel")
  end
  term.write("fuel complete")
end
end
-- will place blocks in a line untill the turtle discovers an obstruction.--
-- It is possible to controll the length of the line without an obstruction by limiting the building materials located in the first 14 slots of the turtle.--
bLine=function()
runsline=true
term.clear()
boarder()
term.setCursorPos(2,3)
term.write("Starting")

while runsline do
  if turtle.getFuelLevel()<2 then
   fuel()
   end

  isBlocked=turtle.detect()
  hasPlaced=turtle.detectDown()

   if hasPlaced and isBlocked then
	term.write("Operation Stopped due to obstruction")
	runsline=false
   elseif isBlocked and not hasPlaced then
	place()
	runsline=false
   elseif not isBlocked and not hasPlaced then
	place()
   end
   turtle.forward()
end
term.write("Finshed")
end
brLine=function(sideLength)
term.clear()
boarder()
term.setCursorPos(2,3)
term.write("Starting")
   for i=0,sideLength,1 do
   if turtle.getFuelLevel()<2 then
   fuel()
   end
   turtle.dig()
   if gatherer==true then
   term.write("stupid gatherer")
   --gather()
   end
   turtle.forward()
   end
end
bSquare=function(sideLength,sideHeight)
term.clear()
boarder()
term.setCursorPos(2,3)
term.write("Starting")
building=true
while building do
  place()
  for i=1,sideHeight do
   for j=2,sideLength do
	if turtle.getFuelLevel()<2 then
	fuel()
	end
	place()
	isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
   end
   turtle.turnRight()
   for j=2,sideLength do
	if turtle.getFuelLevel()<2 then
	fuel()
	end
	place()
	isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
   end
   turtle.turnRight()
   for j=2,sideLength do
	if turtle.getFuelLevel()<2 then
	fuel()
	end
	place()
	isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
   end
   turtle.turnRight()
   for j=3,sideLength do
	if turtle.getFuelLevel()<2 then
	fuel()
	end
	place()
	isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
   end
   place()
   turtle.up()
   isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
	place()
	turtle.turnRight()
   end
end
running=false
end
brSquare=function(sideLength,sideHeight)
term.clear()
boarder()
term.setCursorPos(2,3)
term.write("Starting")
building=true
while building do
  dig()
  for i=1,sideHeight do
   for j=2,sideLength do
	if turtle.getFuelLevel()<2 then
	fuel()
	end
	dig()
	isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
   end
   turtle.turnRight()
   for j=2,sideLength do
	if turtle.getFuelLevel()<2 then
	fuel()
	end
	dig()
	isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
   end
   turtle.turnRight()
   for j=2,sideLength do
	if turtle.getFuelLevel()<2 then
	fuel()
	end
	dig()
	isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
   end
   turtle.turnRight()
   for j=3,sideLength do
	if turtle.getFuelLevel()<2 then
	fuel()
	end
	dig()
	isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
   end
   dig()
   turtle.down()
   isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
	dig()
	turtle.turnRight()
   end
   building=false
end
running=false
end
bFloor=function(sideLength,sideWidth,sideHeight)
term.clear()
boarder()
term.setCursorPos(2,3)
term.write("Starting")
building=true
turnRight=true
while building do
for i=1,sideHeight,1 do
  for i=1,sideWidth,1 do
   for i=2,sideLength,1 do
   --walking forward placing blocks
   if turtle.getFuelLevel()<2 then
   fuel()
   end
   place()
   isBlocked=turtle.detect()
	if isBlocked then
	 error("path blocked")
	else
	 turtle.forward()
	end
			place()  
   end
   --decides to zig or zag then does it placing it facing back the opposite way over an empty space
   if turnRight then
   turtle.turnRight()
   isBlocked=turtle.detect()
	 if isBlocked then
	  error("path blocked")
	 else
	  turtle.forward()
	  end
   turtle.turnRight()
   end
   if not turnRight then
	turtle.turnLeft()
	isBlocked=turtle.detect()
	 if isBlocked then
	  error("path blocked")
	 else
	  turtle.forward()
	 end
	  turtle.turnLeft()
	 end
	turnRight= not turnRight
   end
   --moves turtle up then checks to see if it just went left or right
   turtle.up()
   if not turnRight then
   --if the turtle just went right then it should
	turtle.turnRight()
	isBlocked=turtle.detect()
	 if isBlocked then
	  error("path blocked")
	 else
	  turtle.forward()
	  end
	  turtle.turnLeft()
	  end
   if turnRight then
	 turtle.turnLeft()
	 isBlocked=turtle.detect()
	 if isBlocked then
	  error("path blocked")
	 else
	  turtle.forward()
	  end
	  turtle.turnRight()
	  end
	  turnRight=not turnRight
  end
  building=false
end
end
brFloor=function()
term.clear()
boarder()
term.setCursorPos(2,3)
term.write("Starting")
building=true
turnRight=true
while building do
for i=1,sideHeight,1 do
  for j=1,sideWidth,1 do
   for i=2,sideLength,1 do
   --walking forward placing blocks
   if turtle.getFuelLevel()<2 then
   fuel()
   end
   dig()
   isBlocked=turtle.detect()
	if isBlocked and i<sideLength then
	 digFront()
	  turtle.forward()
	else
	 turtle.forward()
	end
			dig()
   end
   --decides to zig or zag then does it placing it facing back the opposite way over an empty space
   if turnRight and j<sideWidth then
   turtle.turnRight()
   isBlocked=turtle.detect()
	 if isBlocked then
	  dig()
	  turtle.forward()
	 else
	  turtle.forward()
	  end
   turtle.turnRight()
   end
   if not turnRight and j<sideWidth then
	turtle.turnLeft()
	isBlocked=turtle.detect()
	 if isBlocked then
	  dig()
	  turtle.forward()
	 else
	  turtle.forward()
	 end
	  turtle.turnLeft()
	 end
	turnRight= not turnRight
   end
  --after completing a whole floor turtle turns about and descends and contunues on.
  turtle.turnRight()
  turtle.turnRight()
  turtle.down()
  turnRight=not turnRight
   end
  building=false
end
end
bWall=function(sideLength,sideHeight)
term.clear()
boarder()
term.setCursorPos(2,3)
term.write("Starting")
for i=1,sideHeight,1 do
  for j=2,sideLength,1 do
  if turtle.getFuelLevel()<2 then
   fuel()
   end
   place()
   isBlocked=turtle.detect()
	if isBlocked and i<sideLength then
	 digFront()
	  turtle.forward()
	else
	 turtle.forward()
	end
			place()
  end
  turtle.turnRight()
  turtle.turnRight()
  turtle.up()
end
end
brWall=function()
term.clear()
boarder()
term.setCursorPos(2,3)
term.write("Starting")
for i=1,sideHeight,1 do
  for j=2,sideLength,1 do
  if turtle.getFuelLevel()<2 then
   fuel()
   end
   dig()
   isBlocked=turtle.detect()
	if isBlocked and i<sideLength then
	 digFront()
	  turtle.forward()
	else
	 turtle.forward()
	end
			dig()
  end
  turtle.turnRight()
  turtle.turnRight()
  turtle.down()
end
end
while true do
boarder()
mainhud()
drawStatus(builder,gatherer,patternString)
drawChoice(choice)
event,param1=os.pullEvent("key")
if event== "key" and param1==200 then
choice = choice-1
  if choice<3 and drawingwidth then
   choice=10
  end
  if choice<3 and drawingheight and not drawingwidth then
  choice=9
  end
  if choice<3 and drawinglength and not drawingheight then
  choice=8
  end
  if choice<3 and not drawinglength then
  choice=7
  end
	end
if event== "key" and param1==208 then
choice=choice+1
  if choice>10 then
   choice=3
   end
   if choice>7 and not drawinglength and not drawingwidth then
   choice=3
   end
   if choice>8 and not drawingheight then
   choice=3
   end
   if choice>9 and not drawingwidth then
   choice=3
   end
   end
if event== "key" and param1==28 then
  if choice==3 then
   builder=not builder
   end
  if choice==4 then
  gatherer= not gatherer
  end
  if choice==5 then
  pattern=pattern+1
   if pattern>4 then
   pattern=1
   end
  end
  if choice==6 and param1==28 then
  running=true
  --primary loop when all choices have been made--
   while running do
   --line order--
	if pattern==1 then
	 if builder and running then
	 bLine()
	 elseif not builder then
	 brLine(sideLength)
	 end
   --square order--
	elseif pattern==2 then
	if builder then
	bSquare(sideLength,sideHeight)
	end
	if not builder then
	brSquare(sideLength,sideHeight)
	end
   --floor order--
	elseif pattern==3 then
	if builder then
	bFloor(sideLength,sideWidth,sideHeight)
	end
	if not builder then
	brFloor(sideLength,sideWidth,sideHeight)
	end
   --wall order--
	elseif pattern==4 then
	if builder then
	bWall(sideLength,sideHeight)
	end
	if not builder then
	brWall(sideLength,sideHeight)
	end
	end
	term.setCursorPos(2,6)
	term.write("Operation Finished")
	os.sleep(1)
	running=false  
   end
  --end of the main loop area--
  end
  if choice==7 then
  term.clear()
   error("thank you for using Turtle Builder!")
  end

  end
if event== "key" and param1==205 and choice==8 then
  sideLength=sideLength+1
  end
if event== "key" and param1==203 and choice==8 then
  sideLength=sideLength-1
  if sideLength<=0 then
  sideLength=0
  end
end
if event== "key" and param1==205 and choice==9 then
  sideHeight=sideHeight+1
  end
if event== "key" and param1==203 and choice==9 then
  sideHeight=sideHeight-1
  if sideHeight<=0 then
  sideHeight=0
  end
  end
if event== "key" and param1==205 and choice==10 then
  sideWidth=sideWidth+1
  end
if event== "key" and param1==203 and choice==10 then
  sideWidth=sideWidth-1
  if sideWidth<=0 then
  sideWidth=0
  end
end
end

examples left to right Square, Wall, Floor(with a height of 4 to show the potential to fill), and Line showing use of obstruction.

example of a wall pattern using special blocks

and the result of that pattern


this is my first official program release so any advice, or criticisms are welcome! thanks for having a look, and if you find yourself using this program as I do to make your build efforts go farther than enjoy!
Edited on 20 November 2013 - 08:25 PM
Zudo #2
Posted 20 November 2013 - 12:27 PM
Please please please localize your variables!
And put the source code in a
Spoileror a
Spoiler
Bomb Bloke #3
Posted 20 November 2013 - 05:36 PM
Indeed, use of spoiler tags would be good. ;)/>

For whatever reason, my turtle's display has one row and column less then yours, apparently. Default CC 1.57 install.

If the turtle encounters a pre-existing structure while working, it gets stuck. This includes when it runs into blocks in front of itself while "breaking".

You might consider having it wait until it has an entire stack of blocks to discard instead of spitting out lots of individual items as it goes.

Your "square" building system actually seems to form pipes - does it just go until it runs out of blocks? I asked for a 4x5 "square", and got a 5x5 pipe that seemed to stretch upwards forever… It'd be more useful if one could specify three dimensions, as with the "floor".

Use of the keys API makes for less obfuscation in your code. "keys.left" and "keys.right" is a lot easier for people reading to quickly identify than numbers like 203 and 205.

If you use "os.pullEvent("key")", then the event you'll get back will be a key. There's no need to perform additional checks for this lower down.

Otherwise in my quick tests, it seems to more or less work as intended. :)/>
thedriver87 #4
Posted 20 November 2013 - 09:38 PM
thanks for the advice, as for the square it is intended for building a square room the specified height. Seems to be working just fine for me so I am confused as to why it does not stop at the height of your second value. also the stopping when encountering a structure was intended as i use this little guy for clearing out mistakes and i wanted to be very specific about what blocks to break. in this case anything outside the area defined will not be broken and instead the turtle stops. I will be working on a rewrite in the near future. again thanks for the advice!