Jump to content

Clock Design


Recommended Posts

Recently I thought about creating a clock by calculating the time in hours and minutes with a number from 0 to 5000 (dayticks). Eventually, I intend to create an item (e.g. a watch or magic item) with a custom ability which displays the time. From there, I can create quests with time limits.

 

Here is my attempt so far in the scenario script:

 

Code:
 beginstate START_STATE;dayticks = get_current_tick() - (what_day_of_scenario() * 5000);//Total minutes elapsed todaymins1 = (dayticks * 1000) / 3472;//Minutes to be displayedmins2 = mins1 - (hours * 60);//Hours to be displayedhours = mins1 / 60;//These are just here to help me test the thing.print_big_str("dayticks = ",dayticks,"");print_big_str("mins1 = ",mins1,"");print_big_str("mins2 = ",mins2,"");print_big_str("hours = ",hours,"");break; 
We know that there are 1440 minutes in a day (24*60).

 

3.472 (recurring) comes from 5000/1440.

 

However, avernumscript doesn't like the decimal point sign and seems to want to round down variables which are not whole numbers. This means that there are about 27 hours in the day!

 

'(dayticks * 1000) / 3472' shows my attempt to get around the decimal problem, which failed (setting all values to 0).

 

Is there any way around this problem? Have others before me tried to do this?

 

EDIT: I've reached the meaning of life!

Link to comment
Share on other sites

I am going to post the code that I think will accomplish your goals, but you will have to test it yourself. Although I only skimmed your code, I think that the approach is flawed, though that is probably because you are not used to an engine that truncates all decimal or float values into integers.

 

Code:
 // In case you do not know, the % operator returns the remainder of the division// operation that would occur if the "%" was a "/"daytick = get_current_tick() % 5000; // Your 3.472 decimal is equal to 125/36.  Dividing daytick by this yields the total// number of minutes that have elapsed since the beginning of the current day, from// which the hours and minutes can be determined.totalminutes = daytick * 36 / 125; hours = totalminutes / 60;minutes = totalminutes % 60; 
Link to comment
Share on other sites

I must confess I didn't know about the % function, and now that I do it makes things simpler.

 

However, your code in the form that it is does not work. All values are bizzarely set to 0, and I think 'totalminutes = daytick * 36 / 125;' may be the problem. While mathematically correct, avernumscript doesn't seem to cope with it.

 

Neither does it help in any way to simply take an approximate value of 3.472 and multiply both sides by 1000 to give whole numbers - we then get negative numbers appearing for no apparent reason.

Link to comment
Share on other sites

Edit: test shows that this wasn't right... Back to the drawing board.

 

Edit: Like Garrison said, you need to put parentheses around it before you divide. Also, whenever an integer goes over 32766 it becomes negative, and counts down all the way to 65532. So the clock will only last for about 4 hours that way.

Link to comment
Share on other sites

Hmm, this peaks my curiosity. I will take a quick look at it and see if the values are truly being constantly reset to 0. I will try using some parenthesis. This should only take a few minutes...

 

Update: I did the coding too quickly to remember a few important limitations of the engine. One, it processes equations right to left, so parenthesis are indeed necessary ( (daytick * 36) / 125 ). Two, the the engine can only handle integers up to 32767. I'll come up with a quick workaround for this, but it will not be pretty at all.

Link to comment
Share on other sites

Well, this was surprisingly irritating but I got it to work pretty elegantly using fun, functional math.

 

This is the basic version that will tell the time in 24 hour notation. I incorporated the leading zero for the minutes if the time is, for instance, 1:09. For a very good reason, 0:00 will not coincide with the day number changing perfectly. The difference does not accumulate, however, and the effect is essentially negligible.

 

Code:
 daytick = get_current_tick() % 5000;totalminutes = 0;while (daytick > 875) {	daytick = daytick - 875;	totalminutes = totalminutes + 252;	}totalminutes = totalminutes + (daytick * 36) / 125;clear_buffer();	append_string("The time is ");	append_number(totalminutes / 60);	append_string(":");	if (totalminutes % 60 < 10)		append_string("0");	append_number(totalminutes % 60);	append_string(".");	get_buffer_text(str); 
EDIT: Since I figured out the mechanics of it anyway, I decided to add some small modifications to make this a complete time-telling script with all the aesthetic frills.

 

Code:
 	// To coordinate the changing of the days	if (is_outdoor())		daytick = (get_current_tick() + 10) % 5000;		else if (is_town())			daytick = (get_current_tick() + 1) % 5000;			else daytick = get_current_tick() % 5000;				// To take care of the value limits on the variables	totalminutes = 0;	while (daytick > 875) {		daytick = daytick - 875;		totalminutes = totalminutes + 252;	totalminutes = totalminutes + (daytick * 36) / 125;		// Printing the time	clear_buffer();	append_string("The time is ");	placeholder = totalminutes / 60;	if (placeholder % 12 == 0)		append_number(12);		else append_number(placeholder % 12);	append_string(":");	if (totalminutes % 60 < 10)		append_string("0");	append_number(totalminutes % 60);	if (placeholder < 12)		append_string(" AM.");		else append_string(" PM.");	get_buffer_text(str);	print_str_color(str,2); 
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...