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

Level an area program for mining turtle based on original excavate

Started by Soapy, 30 November 2013 - 09:34 AM
Soapy #1
Posted 30 November 2013 - 10:34 AM
Hello Community and Pros,

I am completly new to programming in Lua. The last few days i have watched a lot of tutorials, read wikis and tutorials and all this stuff. I have to admit, that i am somehow a little bit proud, that, even if it is based on an existing program, my own LevelArea program at least does something (after at least a week of testing and searching). But unfortunatly not what i really want. I can see the beginnings of what i want, but now i don't know any further. So i ask you. Maybe you can help me.

The program:

What i want is a program that levels an area (mainly on the surface, not underground) where you give the Turtle not only a square area like in the excavate program but an area where i can choose width and lenght. This part somehow worked out.
The movement of the excavate program is good for me, (dig one lane, turn around, dig the second lane, etc. Hope you know what i mean), so i want to keep it. Heres the big change.
The turtle should dig one block forward and then detect if there are blocks above (the excavate program is to dig under the earth, i want the opposite direction). If there is, it should dig that block up, go one upward and detect if there is a block above again. If there is no block above the turtle, it should go two blocks upwards testing for blocks to make sure it isn't a hole and there really is nothing more about it. After that, it should go down to the starting level on the y-axis, move one block forward and detect again for blocks above. This whole procedure should be done until the area is cleaned.

Here are the problems and what it does until now:

The turtle starts and dig forward, geos forward, then digs upwards and goes one upwards. This procedure repeats until there is nothing in front of the turtle and it begins to circle in the given area (for example 3 x 4 blocks). When it turns and there is a block in front of it again, it goes forward and up again, until there is an area with no blocks in the given area.
Second problem is, the turtle does not stop. it circles around until there is no fuel left.

Maybe some on can help me. I choosed the excavate program because it is comfortable for big areas, with fuel refill and bring back the inventar when it is full. But because it is (for a beginner) an advanced program i have difficulties with it.

Here is the pastebin link to the code because it is about 400 lines big. I have written some comments in the code, that specify my explanations here to the questionable functions.

http://pastebin.com/YUez12RP


Thank you
Soapy
Kingdaro #2
Posted 30 November 2013 - 01:39 PM
Going sifting through stock programs and API is good to gain a better understanding of programs and turtle logic, but I personally think it'd be better if you created your own from scratch and built features upon it, rather than take said stock programs (which weren't meant to do what you want them to do) and edit them for another purpose.

If I understand your goal correctly, here would be a good start:
Spoiler

local depth = 5
local size = 5

for x=1, depth do --# dig five blocks deep
   --# get inside the current level
   turtle.dig() 
   turtle.forward()

   --# this is a little tricky; if we dig in a sort of zig-zag pattern,
   --# we'll end up on the opposite side of the tunnel if the max depth is odd
   --# and if we always turn right to start digging, we'll end up going the wrong way
   --# so we check if it's even first
   --# as in, "if the remainder of depth / 2 is 0", meaning the number is even
   if depth % 2 == 0 then
      turtle.turnRight()
   else
      --# if it isn't, we'll need to check our current x
      --# if our current x is odd, we turn right
      --# if it's even, we turn left.
      if x % 2 == 1 then
         turtle.turnRight()
      else
         turtle.turnLeft()
      end
   end

   for y=1, size do --# dig five blocks high (and wide)
      --# dig four blocks forward
      for i=1, size - 1 do
         turtle.dig()
         turtle.forward()
      end

      --# only go up if we aren't at the top of the cave
      if y < size then
         --# go up and turn around
         turtle.digUp()
         turtle.turnRight() turtle.turnRight()
      else
         --# otherwise, we'll have to check our depth
         --# if the depth is even, we'll be on the right side of the tunnel
         --# so we need to return to the left
         --# if our depth isn't even, we can just go back down and resume normally.
         if depth % 2 == 1 then
            turtle.turnRight() turtle.turnRight()
            for i=1, size - 1 do
               turtle.forward()
            end
         end

         turtle.turnRight()
         for i=1, size - 1 do
            turtle.down()
         end
      end
   end
end

And this is just a sort of "demo" to show the kind of code and logic you'll need. I haven't implemented a number of features in this, e.g. taking program arguments, fuel checks and such, but this is a good starting point to know exactly what you want your program to do.
Soapy #3
Posted 03 December 2013 - 06:41 AM
Thanks for the advice. I will try to do the program step by step. If i have questions again, i will ask them here.