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

Question: Keeping track of time and inputs for brewing beer

Started by Xonto, 30 October 2012 - 09:59 PM
Xonto #1
Posted 30 October 2012 - 10:59 PM
Hello everyone, I am new to Computercraft and fairly new to programming generally (I have written some very basic python programs like a roulette simulator) and I have a few questions about keeping track of time and detecting inputs. At least, I think I do, because that seems like it would be needed, but I may be (read: I am almost certainly) tackling this problem the wrong way so I was hoping for any and all input. Sorry for the WALL OF TEXT, but I have tried to make it at least somewhat readable.

What I want to do relates to Industrialcraft brewing. Because it takes many hours it brew a beer I thought it would be nice to have a program where I could input the beer I am making and then have the computer keep track of how long all the beers have been fermenting and how many drinks are left in each barrel. The high level overview is as follows:

Overview:

A large monitor displays the name of beers that are being brewed and how long they have been fermenting. In another column it displays the names of the beers that are done fermenting and how many pints are left in each barrel. When a new beer is to be brewed, the name and quantity of water is entered into a computer and a lever or button near the barrel is pressed after adding the ingredients. This starts the timer and causes that beer to be displayed on the "fermenting" side of the monitor. pressing the button/lever again will signify the end of the fermentation and will move the beer to the other side of the display that shows the number of beers left in the barrel. Subsequent presses of the button/lever (which will be done every time a pint of beer is extracted from the barrel) will decrement the counter for that beer down by 1 until it reaches 0 at which point it will be removed from the list.

Concepts:

The way that I intend to tackle this is to use redstone bundled cable to attach to the back of the computer. Each lever near a beer barrel will be connected to the bundle with a different colored redstone wire. The program will associate the color of wire with the name and quantity of each beer upon creation. Each pulse of that color will then update the information associated with that beer.

Potential problems: I am not sure the best way to actually track the time because all time tracking I know if is linked to world time and sleeping will throw this off. I was considering using redpower timers and counters to emit a pulse to the computer every hour to update the time on the computer because setting the timer to a couple seconds with a long counter will make any time sync problems negligible. However, this breaks the idea of using a pulse to signify the end of the brewing unless we also include how long we want to brew each beer in the creation process so that it automatically marks it as done and moves it over when that many pulses are received. However, I will have to remember to quickly tap the barrel shortly after this or the beer may over-ferment compared to what the computer thinks it is.

Questions:
1. Am I going about the basic task in a good way? Are there concepts that I just don't know about that would allow me to work this more efficiently? I would greatly appreciate a high-level critique of the concept.
2. Is using redpower counters with short timers the most effective way to minimize time divergence when trying to have the server accurately track 12 hours of real life time as opposed to world-time? Can ComputerCraft do this by itself? How do server reboots affect systems like this?

—-
Thanks for any help you could offer and I will be sure to share my code once I start–I just wanted to make sure that my conceptual overview made sense and I am not missing anything important before I begin working it out.
Harcole #2
Posted 31 October 2012 - 09:33 AM
AFAIK - Redstone timers reset/skip when you sleep so would not keep time either, the only way I can think of keeping time is knowing that it takes a fixed amount of time to cycle a day with no sleep, use computer craft to get the local time then perform some mathematics on that to see how much real time has passed.

You'd need to keep last time and current time to check for time skips (sleeping) and then with the corrected counter you'd be able to divide that by the right number know the real time passed (1 sec in real time = 1 minute in game)…

I'm just think out loud at the moment whilst at work so not really put the proper thought in to it… I believe it's do able, I need to do some thinking as I'm just not making it seem very straight forward


Lets try this again!

ComputerCraft can either sleep(<time>), pull the world time os.time() or start a timer to activate an action os.startTimer(<timeout>) more details found here: http://computercraft...?title=OS_(API)

Basically 1 second in real time is equivalent to 1 minute in minecraft if I recall correctly, to ensure you don't have issues with time skip (sleeping) I would use the following approach


while(true) do
	if(counter > 3599) -- one hour ((60 seconds * 60 minutes) - 1 as we will increment it in the code loop) then
		-- do all the things that should happen every hour here
		counter = 0; -- reset counter to 0 so we start again
	end

	-- Any thing that should happen every second should happen here
	counter = counter + 1;
	sleep(1);
end

Edit: Note this code will run forever, you'd need to have a break out if this is not what you wanted (something that changes the true to false such as a redstone input etc).
Edited on 31 October 2012 - 08:50 AM
Xonto #3
Posted 31 October 2012 - 08:30 PM
Ah, interesting–I suppose that handles everything at least as well as a redpower timer would. I was under the impression that the sleep() function was also linked to world time so sleeping would cause an instant tick of a counter just like it would cause a redpower logic gate timer to instantly pulse. Because I am more familiar with working with redpower gates my mind went there first because it seemed like it would work as well as doing it in CC and would be one less thing to worry about with this program. However, if sleep() will be more consistent than a RP timer, all the better, thanks for the idea.

Another thing that I thought about after I first posted was the fact that CC programs are ended when the server is brought down. Obviously this would wipe out all the data so it got me thinking that if I am to use CC at all with this project I will need to write ever bit of information to actual files so it can be read when the server restarts so the program will remember what beers are on tap and how long each has been brewing. This is somewhat important as my server restarts often and I don't see that changing anytime soon. Unfortunately that adds yet another layer of complexity and I am still pondering the best way to organize it all.
MaHuJa #4
Posted 02 November 2012 - 04:09 AM
However, if sleep() will be more consistent than a RP timer, all the better, thanks for the idea.
I believe the sleep() will be consistent with real time, rather than game time - meaning that if the server is running at a low tick rate (as in, being overloaded) then it may be mismatched with the brewing time. (I'm assuming the ic2 brewing mechanics are using "tick counts" rather than real time.)

CC programs are ended when the server is brought down. Obviously this would wipe out all the data

Maintain all the data you want to keep in a table.
Make a function that will take that (or better yet, ANY) table, and write it to a file - in lua format.
Have your startup dofile() this file, and the table is back up.
Then just run that function every, say, 60 seconds.

For other cases one will wish to save this on updates rather than on time intervals, but that's different from your case. You're dealing with timings - that are continuously changing - and if you lose a minute's worth of data it's not a big deal.

Btw, make sure you keep the computer in the same chunk as the brews, or have all of it covered by chunkloaders.
tebbenjo #5
Posted 30 November 2012 - 12:37 PM
http://www.computercraft.info/forums2/index.php?/topic/6544-error-printing-12-and-exiting-with-no-printwrite-12-or-a-vairable-that-could-contain-12/page__fromsearch__1

#8 view pleas