r/ComputerCraft • u/DereChen • Jan 27 '24
How do you make a Mining Turtle deposit items in a chest?
I would like the turtle to deposit blocks it mined so they don't go to waste; besides the coal for fuel. How can this be done?
3
Upvotes
2
u/fatboychummy Jan 28 '24 edited Jan 28 '24
Unloading items
Emptying a turtle's inventory into a chest is easy enough. You can have the turtle consume fuel as it goes and drop off any extra very easily.
To do this, when dropping items, simply
turtle.refuel(64) turtle.drop().What happens when you do that, is the turtle first attempts to "eat" the item. If it fails, nothing bad happens (no errors, item is not deleted). If it succeeds, it refuels itself only as much as it needs (so if it is full already, it will not refuel further/consume more items). Then, if any items remain in the slot, it drops them in front (where hopefully there is a chest).
Code example
This code also prevents the turtle from getting an error if the chest is full. Instead, it will just wait until space opens up in the chest.
The bigger issue
The main issue comes down to returning home. In order to go home, you need to know the following 3 things:
You need all three of these to determine which way you need to go from where you currently are.
Now, there are multiple methods to go about this...
Method 1: GPS
You can set up a GPS cluster that can help the turtle locate itself. The turtle can request the GPS for a ping, and then it can determine its location from the cluster's responses.
Do this when the turtle starts the program and it can mark that x,y,z position as "home", then you can start mining.
Pros
Cons
gps.locate()s (one before moving and one after) and a bit of math, though.Method 2: Keeping track of everything
This method is a bit more tedious, but can be more robust if done correctly. It is the method I use for all my turtle programs as I prefer to not rely on GPS at all, unless I specifically need world coordinates for something.
Essentially, when you start the program you mark the turtle's current position as 0,0,0 -- Home. Then, you make wrapper functions for every turtle movement method that increments that value based on what direction you are facing and the movement performed.
I'm on my phone right now so the following example will be more pseudocode than anything, but this is what I mean:
Pros
Cons
2
u/BurningCole Jan 28 '24
Easiest method is probably turtle.drop() or the similar functions dropUp() and dropDown() depending on the location of the chest. just iteraterate through the turtles inventory, check if the item is coal and if not use the drop function.