5 posts
Posted 13 September 2013 - 12:21 PM
Hi. I'm writing script for chunk loading mining turtle that mines in a straight line, makes a u-turn and mines back in a straight line and so on. It gets refueled by grabbing lava and it dumps items by using a enderchest. The script saves location and orientation after each forward move and before every orientation change. The problem so far is that I'm using
while turtle.forward() do turtle.dig() end so it won't move before it can move and saving afterwards which means that if the server gets shut down in the middle of turtle move animation the save file will be corrupted. Is there any way to force location save or wait for the next location save before the turtle yields?
http://pastebin.com/g2qGkbkBThanks for any responses :)/>
8543 posts
Posted 13 September 2013 - 06:26 PM
Split into new topic.
375 posts
Location
USA
Posted 14 September 2013 - 02:51 PM
Generally, there are no perfect solutions to saving a correct state on a server shutdown condition. However, you can increase the probability that the save will not be corrupt by decreasing the time between state change and a save. You could first save the position of where you know the turtle will be when it finishes moving, then actually move the turtle. This relies on the turtle's position immediately being saved to its next spot on the server when it starts its animation, which I'm not sure is true. The best you can do is start up your own local server and experiment. If the case is that the position is saved on the server during the animation (which I highly doubt), then you could run the save and turtle operation in parallel and save in the middle of the operation.
5 posts
Posted 14 September 2013 - 03:14 PM
The problem is that there might be a mob or a player blocking the turtles way. In this case the next position would be saved while the turtle was unable to move.
375 posts
Location
USA
Posted 14 September 2013 - 03:22 PM
The problem is that there might be a mob or a player blocking the turtles way. In this case the next position would be saved while the turtle was unable to move.
Good point. You would have to detect whether the call immediately returns or takes more than a predefined amount of time.This can be done by wrapping a function around the turtle movement call and running it parallel to a thread which waits to save the state. The turtle thread would signal to the save thread when it finishes movement, and the save thread would wait a predefined amount of time before saving the correct state. If the save thread gets the signal before it times out, it knows that the operation was unsuccessful, and can cancel its save operation.
5 posts
Posted 15 September 2013 - 04:25 AM
The problem is that there might be a mob or a player blocking the turtles way. In this case the next position would be saved while the turtle was unable to move.
Good point. You would have to detect whether the call immediately returns or takes more than a predefined amount of time.This can be done by wrapping a function around the turtle movement call and running it parallel to a thread which waits to save the state. The turtle thread would signal to the save thread when it finishes movement, and the save thread would wait a predefined amount of time before saving the correct state. If the save thread gets the signal before it times out, it knows that the operation was unsuccessful, and can cancel its save operation.
That is quite high tech :P/>
I might try doing that just for the fun of it. But I came up with another idea:
cont = true
x = 0
while cont do
x = x+1
saveLocation()
if turtle.forward() then
cont = false
else
x = x-1
saveLocation()
end
sleep(0)
end
This way the turtle saves the new location before the move and if the move fails it reverses the save. I think turtle.forward() is instant if it fails, but I will have to test this.
375 posts
Location
USA
Posted 15 September 2013 - 10:23 AM
cont = true
x = 0
while cont do
x = x+1
saveLocation()
if turtle.forward() then
cont = false
else
x = x-1
saveLocation()
end
sleep(0)
end
Heh, that's much simpler than what I was thinking of. Let me know if it works correctly.
5 posts
Posted 15 September 2013 - 10:42 AM
Heh, that's much simpler than what I was thinking of. Let me know if it works correctly.
Works like a champ :)/> You can check the code from the pastebin link in the first post. Thanks for the feedback, I've used parallel function for my elevator system where there are clients and a server and they communicate with each other, so I have some experience in using it. Using parallel function to save on demand would be quite nice system too :P/>
If anyone still has any suggestions, I'm open for them. I'll be checking this thread from time to time :)/>