Clock Design

Error message

Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /var/www/pied-piper.ermarian.net/includes/common.inc).
AuthorTopic: Clock Design
Warrior
Member # 6846
Profile #0
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:

beginstate START_STATE;

dayticks = get_current_tick() - (what_day_of_scenario() * 5000);

//Total minutes elapsed today
mins1 = (dayticks * 1000) / 3472;

//Minutes to be displayed
mins2 = mins1 - (hours * 60);

//Hours to be displayed
hours = 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!

[ Thursday, June 01, 2006 12:17: Message edited by: Pyrulen ]

--------------------
"Build a man a fire, and he´ll be warm for a day; set a man on fire, and he´ll be warm for the rest of his life."
Posts: 65 | Registered: Thursday, March 2 2006 08:00
Agent
Member # 2820
Profile #1
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.

// 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;


--------------------
Thuryl: I mean, most of us don't go around consuming our own bodily fluids, no matter how delicious they are.
====
Alorael: War and violence would end if we all had each other's babies!
====
Drakefyre: Those are hideous mangos.
Posts: 1415 | Registered: Thursday, March 27 2003 08:00
Warrior
Member # 6846
Profile #2
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.

--------------------
"Build a man a fire, and he´ll be warm for a day; set a man on fire, and he´ll be warm for the rest of his life."
Posts: 65 | Registered: Thursday, March 2 2006 08:00
Lifecrafter
Member # 6193
Profile Homepage #3
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.

[ Thursday, June 01, 2006 14:24: Message edited by: Lazarus. ]

--------------------
Guaranteed to blow your mind.

Frostbite: Get It While It's...... Hot?
Posts: 900 | Registered: Monday, August 8 2005 07:00
Agent
Member # 2820
Profile #4
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.

[ Thursday, June 01, 2006 15:05: Message edited by: Garrison ]

--------------------
Thuryl: I mean, most of us don't go around consuming our own bodily fluids, no matter how delicious they are.
====
Alorael: War and violence would end if we all had each other's babies!
====
Drakefyre: Those are hideous mangos.
Posts: 1415 | Registered: Thursday, March 27 2003 08:00
Off With Their Heads
Member # 4045
Profile Homepage #5
I believe that Avernumscript calculates math right-to-left, so daytick * 36 / 125 = daytick * 0 = 0. Parentheses should fix this.

--------------------
Arancaytar: Every time you ask people to compare TM and Kel, you endanger the poor, fluffy kittens.
Smoo: Get ready to face the walls!
Ephesos: In conclusion, yarr.

Kelandon's Pink and Pretty Page!!: the authorized location for all things by me
The Archive of all released BoE scenarios ever
Posts: 7968 | Registered: Saturday, February 28 2004 08:00
Agent
Member # 2820
Profile #6
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.

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.

// 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);


[ Thursday, June 01, 2006 20:17: Message edited by: Garrison ]

--------------------
Thuryl: I mean, most of us don't go around consuming our own bodily fluids, no matter how delicious they are.
====
Alorael: War and violence would end if we all had each other's babies!
====
Drakefyre: Those are hideous mangos.
Posts: 1415 | Registered: Thursday, March 27 2003 08:00
Warrior
Member # 6846
Profile #7
It works, give or take a missed bracket. Nicely done.

Not that this problem can really be helped, but the hours of darkness seem too short (since when has the sun risen at 1:00 AM outside of the arctic circle?).

--------------------
"Build a man a fire, and he´ll be warm for a day; set a man on fire, and he´ll be warm for the rest of his life."
Posts: 65 | Registered: Thursday, March 2 2006 08:00
Agent
Member # 2820
Profile #8
Well, according to this system, the daylight lasts 50% longer than in real life. The sun would set at 9:00 p.m. and rise at 3:00 a.m. I wonder if anyone else wants to have a watch in their scenario...

--------------------
Thuryl: I mean, most of us don't go around consuming our own bodily fluids, no matter how delicious they are.
====
Alorael: War and violence would end if we all had each other's babies!
====
Drakefyre: Those are hideous mangos.
Posts: 1415 | Registered: Thursday, March 27 2003 08:00