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

Forcing Turtle Location Save Before Server Restart, Or Making Location Saving Better

Started by Tuxi, 13 September 2013 - 10:21 AM
Tuxi #1
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/g2qGkbkB

Thanks for any responses :)/>
Lyqyd #2
Posted 13 September 2013 - 06:26 PM
Split into new topic.
Yevano #3
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.
Tuxi #4
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.
Yevano #5
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.
Tuxi #6
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.
Yevano #7
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.
Tuxi #8
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 :)/>