ID:151281
 
Hello :D im new to DM programming and im kind of proud of myself for making a little in-game clock!

What i am going to do is create a graveyard code for my game. people can "Grave-rob" as a way to make money, get loot and sometimes have to fight zombies.. Lol it isnt a zombie game its just...a weird game.

Anyway I figured i would start with an in-game clock so people can only grave-rob from 12:00 AM to 3:00 AM in-game.

Why im here and showing this is i would like to ask you guys(and gals) to maybe look my code over and tell me if all is in order...like i said im new to DM programming and i do not want to create something that will make my game freak out lol.

Here is my in-game clock code. Complete with confusing notes! I understand them i dont know if others will..

/*
NOTES:

*Reason for "minutes1" and "minutes2" is
so my clock would read: 12:00 instead
of 12:0 when "minutes1" reaches 12:09 it will
add one number on to "minutes2" so the clock
will read 12:10.


*Clock likes to run two extra seconds.

Seconds : Set to 1 = 3 seconds.

Minutes1 : Set to 8 = 10 minutes

Minutes2 : Set to 6 = 8 minutes

*Minutes2 note:
The reason it turns 8 and not 9 to read "59" on my clock
is because hour doesn't switch over till one more loop after
12:59 it will read 1:00 and if its AM it switches to PM


*Hour note:
Each hour takes 2 min 55 seconds


*AM to PM cycle Note:
4200 seconds = 70 minutes for full day cycle.
AM to PM (12:00 PM to 12:00 AM)

*/


proc
get_time()
time = ("[hour]:[minutes2][minutes1] [ToD] - [seconds]")


var
seconds = 0
minutes1 = 0
minutes2 = 0
hour = 12
ToD = "PM"
time

world
New()
set background = 1
..()
spawn()
while (src)
sleep (10)
if (seconds <= 1)
seconds ++
else
seconds = 0
if (minutes1 <= 8)
minutes1 ++
else
minutes1 = 0
minutes2 ++
if (minutes2 >= 6)
hour ++
minutes2 = 0
if (hour >= 12)
hour = 1
if (hour >= 12)
switch(ToD)
if("AM")
ToD = "PM"
if("PM")
ToD = "AM"
get_time()
Very cool, this could also have uses in creating other forms of phases as well. Quite neatly too.
I dont get it? The second veriable goes:
1
0
1
0
1
0

and every 2 seconds it goes up a minute?
In response to Saucepan Man
My notes explain it. every three seconds minutes1 goes up by one.
In response to NotS
OoOhh right; sorry I assumed it was a real time clock.
my bad
In response to Saucepan Man
:D nope! im going to base just about everything in my game around an in-game clock system. So i created this code. My math is horrible but i know the programming it took me a long time to try and work it all out. Now that im finished i just wanted to see if it had any errors. Just because it works doesnt make it right, knowwhatimean? lol
You don't need the seconds counter, you can just sleep for 30 ticks (3 seconds) rather than keeping track of it with a counter. You also don't need the multiple variables to keep track of minutes. When you hit 60 minutes just increment the hour, and set minutes back to zero so that 60 never shows up. When you hit 12 hours, change AM and PM as needed, when hours hits 13, change it to 1:

var
minutes=0
hours=12
timer=0
ampm="AM"
clock="12:00 AM"


proc
timer()
while(world)
sleep(30)
minutes++
if(minutes==60)
minutes=0
hours++
if(hours==12)
ampm="[ampm=="AM"?"PM":"AM"]"
if(hours>12)
hours=1

clock="[hours]:[minutes<10?"0[minutes]":"[minutes]"] [ampm]"
// '? Operator' - If "minutes is less than 10, add a 0 in front of it, otherwise just show the number
world
New()
spawn() timer()
..()


//Example Display
/*mob
Stat()
statpanel("test")
stat(world.time)
stat(clock)*/


The following uses modulo to calculate the time directly from world.time, based on 70 minutes to a day.

var/clock="12:00 AM"

proc
timer()
while(world)
sleep(10)
var/ampm="AM" //It's AM...
if(world.time%43200>=21600)ampm="PM" //...Unless each day is half over
var/min=round((world.time%1800)/30) //Use Modulo (%) to strip world.time down to just the minutes since the last hour, since 30 ticks= 3 seconds= 1 minute, divide that by 30
if(min<10)min="0[min]" //If we aren't at 10 minutes, stick the minutes into a string with the preceeding zero
var/hour=round((world.time/1800))%12 //For hours, divide by world.time by 1,800 (60 minutes at 30 ticks each) and round down so we have just the number of hours past. Use Modulo to strip it down just to the last 12 hours
if(hour<1)hour=12 //If there wasn't enough time past to equal an hour, we need to set it to 12:35 rather than it showing as 0:35
clock="[hour]:[min] [ampm]" //Stick everything together into the clock variable

world
New()
spawn()timer()
..()


//Example Display:

/*mob
Stat()
statpanel("test")
stat(world.time)
stat("Clock:","[clock]")
stat("Time to Hunt?","[(world.time%43200<5400)?"Yes":"No"]")*/

In response to Xioden
You'be obviously missed the point of the OP completely. Also your second 'suggestion' is completely ridiculous, look up <code>time2text</code>
In response to El Wookie
El Wookie wrote:
You'be obviously missed the point of the OP completely. Also your second 'suggestion' is completely ridiculous, look up <code>time2text</code>


Apparently I am completely missing something, as the first example runs identically to the OP's without dealing with a second minutes variable which is entirely unnecessary.

As for the second 'ridiculous' 'suggestion'... It is entirely applicable, not to mention an entirely valid method in this situation.

Time2text is not ideal for these types of things. It makes things a lot more complicated since it is based off of GMT. EST for example, needs a 180,000 offset to be accurate, while someone on the West coast, needs a 288,000 offset. So you then need to assume they set it up correctly, or start importing time from a website, which makes offline play impossible.

So basically to use time2text() you end up making a loop to keep track of time, have to provide a way for the player to choose a timezone, or rely on grabbing the information online, and then you must assume the information provided or returned is accurate (or potentially give players a way to cheat by changing the time of day the game starts).

Or we can just do a little math, and pull the exact information we need from a timer we already have. Of course that's ridiculous though, so never mind...
I don't get how you did your math.
The important thing is 3 secs = 1 min.
That means 1 hr = 60 mins * 3 secs = 180 secs = 3 mins (real time), not 2 mins 55 secs.
Also, 1 day = 24 hrs * 3 mins (real time) = 72 mins (real time).

If you like, you could also write your code like this
world
New()
set background = 1
..()
spawn(10)
var/list/tods = list("PM","AM")
for()
for(ToD in tods)
for(hour in 0 to 11)
hour = hour || 12 // 0 will be 12
for(minutes2 in 0 to 5) // mins (tens)
for(minutes1 in 0 to 9) // mins (ones)
for(seconds in 0 to 2)
get_time()
sleep(10)
In response to Jemai1
Lol, i didnt do my math..i just timed stuff with my phones stop watch and the computers calculator when i was bug testing! :D


Edit:
I tried your code, i typed it out so i can better understand it...i can read code better if i type it out..im complicated.

anyway, i like your idea of using lists. your way is a lot better and shorter than my mess of a code lol. It makes me regret not reading more into List's on the DM guide.


my version was okay though? even if it was a confusing mess of if and else commands lol
In response to NotS
Your transitions are confusing. If you really like to have it in if and else form, you should write it as
sleep(10) // sleep 10 ticks
seconds++ // increment sec
if(seconds >= 3) // if 3 secs passed
seconds = 0 // reset sec
minutes1++ // increment min1
if(minutes1 >= 10) // every 10 min1
minutes1 = 0 // reset min1
minutes2++ // increment min2
if(minutes2 >= 6) // every 6 min2
minutes2 = 0 // reset min2
hour++ // increment hours
if(hour==12) // change of tod during 12:00
if(ToD=="AM") // if tod is AM
ToD = "PM" // change to PM
else // otherwise
ToD = "AM" // change to AM
else if(hour>12) // after 12 hr
hour = 1 // reset hr to 1
get_time()


Btw, your second if (hour >= 12) will never be true since you set hour = 1 before it.
In response to Jemai1
I like your way of doing Lists more. its shorter, and more neat than my confusing complex way. I was just wondering if my first code would of worked too. Its my first DM code..:D it took me forever because of the math.


Edit:

If i give credit would it be okay to use the little code you created?
In response to Jemai1
I'd just use a single var to store how many seconds have elapsed, then convert that single value to hours, minutes, and seconds to be displayed. That way you just have to increment one var, you don't have to worry about separate vars for each digit.

For example:

var
game_time = 0
hours
minutes
seconds

world
New()
..()

spawn()
while(1)
// one second of real time...
sleep(10)

// ...counts as 20 seconds in game time
game_time += 20

// compute hours, minutes, and seconds based on game_time
seconds = game_time % 60
minutes = round(game_time / 60) % 60
hours = round(game_time / 3600)


You can also find a way to use time2text() to generate the timestamp.
In response to Jemai1
What is this I dont even
In response to Popisfizzy
I'm sorry?
In response to NotS
Sure, if that makes you happy. You don't need to give me credit.