I've created a turtle to process stuff for me. I've built a number of alvearies (beehives), and a BuildCraft pipe system deposits the honey combs they produce (which are of various types) into a collection of barrels. The turtle's job is to collect resources from these barrels and take them to machines for processing. Initially I had it take them to a centrifuge, wait around (sleep) until the stack was complete, then dump the results back into the same sorting system the alvearies feed. I added in a few other such jobs over time (eg, running dust through a furnace).
... Tables and variable declarations, etc...
function processComb()
... Check with a rednet inventory server to see if the current barrel is worth dealing with, ...
... collect combs, drag them to the centrifuge, drop them in...
sleep(math.floor(turtle.getItemCount(1) * 5.25) + 8)
... collect the results, drop them off, increment the barrel counter...
end
... More functions for other jobs, movement, etc...
... Init actions; rednet handshakes, GPS stuff, etc...
-- Main work loop
while true do
... Make sure my generators (and the turtle) have fuel...
cookStuff()
compressStuff()
processComb()
end
Because it now also does these other jobs, I wanted to remove the "waiting around" bit and simply have it move on to the next task directly after feeding each machine. I looked into timers as a way of doing this, but os.startTimer() appeared completely unsuitable as the turtle must be sitting around doing nothing in order to catch the resulting event generated.
So I decided to go with os.clock() (per the below pseudo-code), but I was a little apprehensive about doing so. As it tracks the number of seconds since the turtle booted I figured this'd involve dealing with some very large numbers.
... Tables and variable declarations, etc...
function processComb()
... Check with a rednet inventory server to see if the current barrel is worth dealing with, ...
... collect combs, drag them to the centrifuge...
centritimer = os.clock() + math.floor(turtle.getItemCount(1) * 5.25) + 8
... Drop the current set of combs, collect the results from the last batch of combs ...
... that were processed, drop them off, increment the barrel counter...
end
... More functions for other jobs, movement, etc...
... Init actions; rednet handshakes, GPS stuff, etc...
-- Main work loop
while true do
... Make sure my generators (and the turtle) have fuel...
if os.clock() > cooktimer then cookStuff() end
if os.clock() > compresstimer then compressStuff() end
if os.clock() > centritimer then processComb() end
end
So question number one: Is there a more elegant way of doing this then tracking numbers that increase forever?
Anyway, I figured that as it's dealing with double precision floats it'd probably be good for at least a few months or so before I had to worry about overflows or any such thing (longer then the server's average uptime at least), and hence I decided to give it a shot. Sure enough, the turtle merrily floated around my base and so I logged off for the night. Only to come back in the morning to find my server (which runs on a local computer in my LAN and had been going about a week) had completely stalled - all logging stopped about two hours after I logged off. Prior to that there were some "ConcurrentModificationException"s logged but they appear all the time for me anyways.
I tried to close the server program normally, which triggered a little bit of info in the output terminal:
2013-05-25 08:32:10 [INFO] [Minecraft] Stopping server
2013-05-25 08:32:10 [INFO] [Minecraft] Saving players
2013-05-25 08:32:10 [INFO] [Minecraft] Saving worlds
2013-05-25 08:32:10 [INFO] [Minecraft] Saving chunks for level 'world'/Overworld
2013-05-25 08:32:11 [INFO] [STDERR] java.net.SocketException: socket closed
2013-05-25 08:32:11 [INFO] [STDERR] at java.net.TwoStacksPlainSocketImpl.socketAccept(Native Method)
2013-05-25 08:32:11 [INFO] [STDERR] at java.net.AbstractPlainSocketImpl.accept(Unknown Source)
2013-05-25 08:32:11 [INFO] [STDERR] at java.net.PlainSocketImpl.accept(Unknown Source)
2013-05-25 08:32:11 [INFO] [STDERR] at java.net.ServerSocket.implAccept(Unknown Source)
2013-05-25 08:32:11 [INFO] [STDERR] at java.net.ServerSocket.accept(Unknown Source)
2013-05-25 08:32:11 [INFO] [STDERR] at hu.run(ServerListenThread.java:81)
2013-05-25 08:32:11 [INFO] [STDOUT] Closing listening thread
And then it just hung there until I ended the task.
I restarted it to find the turtle (a wireless crafty model) had reverted to a regular turtle with no label - not surprising, it'd done the same thing when the server had crashed quite some time before (for reasons entirely unrelated), so I left the server running while at work and then on my return I rebuilt the turtle and set it to run overnight again. Again, the server completely stalled some time after log out, this time without the "ConcurrentModificationException"s to clue me in as to when (nothing was logged since I logged out, though trying to stop the server resulted in the exact same extra messages as the previous day, the same need to end the task, and the same need to rebuild the turtle).
So, barring some big coincedence, my turtle is killing my server somehow. It'd take a week of testing to say for sure how (as it runs for hours without issue and nothing gets recorded when it DOES die), so whether or not my use of the os.clock() function has anything to do with it is purely conjecture at this point.
So question number two: Has ComputerCraft been known to trigger server hangs, or are there any known issues with os.clock()?