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

Felling Turtle suffers from acrophobia

Started by TheDeanoRama, 25 February 2015 - 10:50 PM
TheDeanoRama #1
Posted 25 February 2015 - 11:50 PM
The preamble:
I am very new to computer craft and decided to take it on this last week. I took my usual approach to just about everything and found something that kinda worked for what I wanted and started reverse engineering and modifying it. Due to using this approach, I don't know if what I'm experiencing is a bug with the mod, an error with the original LUA or something I messed up in the modification.

I basically wanted a turtle that would plant a single tree, and harvest/replant in a loop until I either stopped it or it ran out of resources. I found such a beast in: http://turtlescripts.com/file/gjdhn6
Of course me being me, I decided I wanted it to be able to do a bit more, so I added in a few new steps and a function, basically allowing it to refresh it's resources as it went along as well as ending up where it started no matter why it needed to backtrack, ending up with this: http://pastebin.com/T6fgFah8

The difference from the original being an optional chest on either side of the planted sapling each one carrying one or the other of more saplings or more bonemeal, with a mandatory 3rd chest empty on the opposite side of the sapling from the turtle as a dumping ground for slot #2 so that it would never over fill on logs. The only way I could make this work (with my very limited skill set at this point) beyond the initial setup was to fill all the slots from 1 to 13 with a log of the tree/sapling type (though really it could be anything, so long as #1 is of appropriate tree type). leaving 14 filled with bonemeal, 15 filled with the saplings and 16 filled with fuel.

The problem:
Everything goes off without a hitch but I find that if an entity of any type passes underneath the turtle while it's on either it's ascent or descent it will stop in mid air once it's done harvesting. The 1st time this happened I had thought I'd lost my turtle someplace - having wandered off due to some poor programming on my part, only to spot it sometime later hovering about 15 or so blocks in the air (pine is my current tree of choice for its ROI). After some testing to try to replicate what happened, it seems to be entity related, be it me, a zombie or even a fallen sapling.

I've managed to get it to park where it starts no matter the reason for it backtracking (out of bonemeal and waiting for the tree to pop or out of a critical resource). It's just the freezing in mid air that bugs me. The only thing I'd really like to add is a way to interrupt the program when this happens in hopes of resetting it back down to the ground without having to break it and physically set it on the ground at the starting point. This would allow me to run some kind of secondary reset program so it continues on it's merry way down the the ground and back into position, but that's for me to figure out at this stage :blink:/>. Of course it would all be unnecessary if I can find out what it is (possibly in my code) that causes the stoppage and what the work around is, if there is one.
KingofGamesYami #2
Posted 26 February 2015 - 12:29 AM
This is a relatively easy problem to fix. Simply add a loop to check if there is an entity. Generally this is done by checking if the movement succeeded - they will return true if it did, false if not. Example:


local function down()
  while not turtle.down() do
    if turtle.detectDown() then
      --#there's a block
      --#we should mine it
      turtle.digDown()
    else
      --#we can assume it's an entity
      --#we should attack it
      turtle.attackDown()
    end
  end
end
TheDeanoRama #3
Posted 26 February 2015 - 01:03 AM
Here is where my noobishness will really shines I see, it didn't even occur to me that it would be looking for anything, unless directed, that could impede it's path on descent. I can even follow your code and see what is going on here (I was surprised too), thanks for the assist!

I will give this a shot once I get home.

Well, after much messing around with the code and as straight forward as your solution seems to be, it either isn't working as expected or I didn't utilize it correctly. The pastebin link remains the same.

The newly introduced behaviour is that it no longer comes down but for 1 block and then executes everything after the down() function is called. Thankfully I'm enjoying and exercising my bat DNA :)/>

What I may try to do is with the if/else is turtle.attack.down otherwise just down as the likelihood of a block being placed under it is slim to none other than the potential of a griefing enderman. I've tried forcing in a turtle.down() after the if/else resolves but that didn't help at all. You will find the down() function being called in the same position as the old turtle down code used to reside @ ln 120. The original code prior to the new local function being
while not turtle.detectDown() do
turtle.down()
KingofGamesYami #4
Posted 26 February 2015 - 04:22 AM
this:

while not turtle.detectDown() do
  turtle.down()
end
cannot be replaced with my function alone. Instead, you would utilize it like this:

while not turtle.detectDown() do
  down()
end

Basically what the original code was doing was: "Go down until you hit a block".
Edited on 26 February 2015 - 03:22 AM
TheDeanoRama #5
Posted 26 February 2015 - 05:07 AM
Now I really feel like a noob. Not only does it work perfectly now, but I get the added bonus of getting slapped should I be standing under it myself. Thanks :D/>