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

RXDV "flatit"

Started by Dalva, 01 May 2012 - 09:08 AM
Dalva #1
Posted 01 May 2012 - 11:08 AM
greetings everyone, I just want to share a program I made. It's my very first attempt at scripting in LUA, ever.
called "Flatit", it can be used to flat a vast area, as long as the tutle have sufficient blocks in the inventory. It can flat both ways, so it will still flat an area even if there's a depression or a hill.

Just place the Turtle in the desired elevation and run the program. one limitation is that it thinks that a wild grass is a block, so if a wild grass happens to be at the correct flat elevation as the starting point, it ignores it. so you have to do grass check manually after the turtle finished its job

here's the code:
Spoiler

local function printheader()
term.clear()
term.setCursorPos(1,1)
print("-----------------------------------")
print("FlatIt		  | RXDV Laboratories")
print("-----------------------------------")
end
local function invcheck()
while true do
for n=1,9 do
  if turtle.getItemCount(n) > 0 then
   return n
  end
end
printheader()
print("")
print("")
print("ERROR: No Blocks in Inventory. ")
print("	   Please load any block into ")
print("	   inventory.")
sleep(1)
end
end
local function eatforward()
-- x,y,z = gps.locate( 2, true )
while not turtle.detectDown() do
  turtle.down()
  dy = dy - 1
end
while dy < 0 do
  turtle.up()
  dy = dy + 1
  turtle.select(invcheck())
  turtle.placeDown()
end
while turtle.detect() do
  turtle.dig()
  turtle.up()
  dy = dy + 1
end
while dy > 0 do
  turtle.down()
  dy = dy - 1
end
turtle.forward()
print("nom")
end
local function checkturn()
if curdir == 0 then
if dir == "left" then
  curdir = 1
  turtle.turnLeft()
  eatforward()
  turtle.turnLeft()
  print("1")
  return true
end
if dir == "right" then
  curdir = 1
  turtle.turnRight()
  eatforward()
  turtle.turnRight()
  print("2")
  return true
end
end
if curdir == 1 then
if dir == "right" then
  curdir = 0
  turtle.turnLeft()
  eatforward()
  turtle.turnLeft()
  print("3")
  return true
end
if dir == "left" then
  curdir = 0
  turtle.turnRight()
  eatforward()
  turtle.turnRight()
  print("4")
  return true
end
end
end
local function eatfar()
while ddist < dist do
  ddist = ddist + 1
--  eaten = eaten + 1
  eatforward()
end
end
local function eatall()
while dside < side do
  eatfar()
  ddist = 0
  checkturn()
  dside = dside + 1
end
eatfar()
end
local tArgs = { ... }
if #tArgs < 3  then
printheader()
print("Land Flatter for Turtles.  by Dalva")
print("")
print("")
print("USAGE : flatit <left/right>  ")
print("		<length> <width>")
print("EX	: flatit 10 right 5")
print("NOTE  : Do not use in areas where ")
print("		trees are present.")
return false
end
if not tArgs[1] == left and tArgs[1] == right then
printheader()
print("")
print("")
print("ERROR: Unknown Direction: " ..tArgs[1])
print("Please input either left/right")
print("")
return false
end
dy = 0
ddist = 0
dside = 0
curdir = 0
dist = tonumber(tArgs[2])
side = tonumber(tArgs[3])
dir = (tArgs[1])
printheader()
print("")
print("")
print("Beginning Flattening Operation...")
print("Length	: " ..dist)
print("Width	 : " ..side)
print("Direction : " ..dir)
print("")
eatall()
printheader()
print("")
print("")
print("SUCCESS: Flattening Completed")
I know, the code might be very messy, inefficient and all, but its my first attempt in scripting in LUA. so please dont be too harsh on me :)/>/>
RXDV Laboratories is my fictional company where i create all my program in. Just to add a little roleplay value :)/>/>
might as well create full suite of turtle programs.i am just sooo interested in turtles!

comments and suggestions please
Mtdj2 #2
Posted 17 June 2012 - 08:02 PM
Here you go, comment AND suggestion :(/>/>
Well, sounds like an ok program (Can't really see the code, I'm on Ipad.). You could ad a thing that makes it dig the block underneath for then placing it again.
Also, you could make the code http api compatible by using pastebin.com. Awesome code paste page.
Hope this helped.
kazagistar #3
Posted 18 June 2012 - 03:29 PM
I recommend indentation. I like to read through people's code, but my eyes glaze over when there are not code blocks of some kind.