How to calculate day of week

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

How to calculate day of week

Post by Bob the Hamster »

So RMZ asked me today if it was possible to calculate the current day of the week based on system year, system month, and system day plotscripting commands.

I was about to reply that it was too hard to be worth the trouble... then it got me thinking, and I realized that although it is a bit tricky, it is certainly not too hard.

I whipped up a script that does it, and an example rpg file to demonstrate it.

The demo rpg is here

And this is the script:

Code: Select all

script, calc weekday number, y, m, d, begin
  # Return value is:
  #
  # 0 = Sunday
  # 1 = Monday
  # 2 = Tuesday
  # 3 = Wednesday
  # 4 = Thursday
  # 5 = Friday
  # 6 = Saturday
  #
  # This function is only smart enough to handle days starting with 1900AD
  variable(dnum)
  dnum := 1 # January 1900 began on a Monday
  variable(i)
  for(i, 1900, y -- 1) do(
    dnum += days in year(i)
  )
  for(i, 1, m -- 1) do(
    dnum += days in month(y, i)
  )
  dnum += d -- 1
  exit returning(dnum ,mod, 7)
end

script, days in month, y, m, begin
  variable(d)
  switch(m) do(
    case(1) d := 31
    case(2) d := days in february(y)
    case(3) d := 31
    case(4) d := 30
    case(5) d := 31
    case(6) d := 30
    case(7) d := 31
    case(8) d := 31
    case(9) d := 30
    case(10) d := 31
    case(11) d := 30
    case(12) d := 31
  )
  exit returning(d)
end

script, days in february, y, begin
  if(is leap year(y)) then(
    exit returning(29)
  )
  exit returning(28)
end

script, days in year, y, begin
  if(is leap year(y)) then(
    exit returning(366)
  )
  exit returning(365)
end

script, is leap year, y, begin
  variable(leap year)
  leap year := false # Common year
  if((y ,mod, 4) == 0) then(
    leap year := true # Leap year
    if(y ,mod, 100 == 0) then(
      leap year := false # Exceptional Common Year
      if(y ,mod, 400 == 0) then(
        leap year := true # Century leap year
      )
    )
  )
  exit returning(leap year)
end
the important command is calc weekday number

Example:

Code: Select all

  variable(n)
  n := calc weekday number(system year, system month, system day)
  switch(n) do(
    case(0) $0="Sunday"
    case(1) $0="Monday"
    case(2) $0="Tuesday"
    case(3) $0="Wednesday"
    case(4) $0="Thursday"
    case(5) $0="Friday"
    case(6) $0="Saturday"
  )
  show string(0)
User avatar
RMZ
King Slime
Posts: 1698
Joined: Tue Oct 16, 2007 12:39 am
Contact:

Post by RMZ »

This is seriously awesome! Major thanks for helping with this and making it look so easy. I hope other people use it for a real time project as well.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Slick, that's going the extra mile! I copied the script over to the wiki.
User avatar
Greenyel
Slime
Posts: 23
Joined: Mon Jan 16, 2017 2:58 pm
Location: Tubarao/SC - Brazil

Deleting Old Saved Games

Post by Greenyel »

Its possible to make a script that when I make a new version of my game, it deletes all old saved games (before the new release date), if there exist ones? I want to make something like this to prevent the player from playing with an old save file if there is some critical changes on the new release of the game.
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Yes, you can do that, but rather than using the date, I recommend using a version number in a global variable. Each time you release an update you can increment that global.

I think I have an example script that actually does this, but I don't remember what game I made it for, so I will have to hunt around and see if I can find it.
User avatar
Greenyel
Slime
Posts: 23
Joined: Mon Jan 16, 2017 2:58 pm
Location: Tubarao/SC - Brazil

Post by Greenyel »

Thanks Bob The Hamster!
If you find that example it would be nice!
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

I also used a save versioning system in one of my games.

If you just wanted to show a warning when you load a game, you could do this:

Code: Select all

define constant(2, current game version)  # increment this as necessary
global variable(1, save version)  # pick an unused global ID but do NOT increment this

plotscript, newgame, begin
  save version := current game version
  #...
end

plotscript, my loadgame script, begin
  if &#40;save version < current game version&#41; then &#40;
    show textbox &#40;42&#41;  # "Save is from a previous version!"
    wait for textbox
    game over
  &#41; else if &#40;save version > current game version&#41; then &#40;
    show textbox &#40;43&#41;  # "Save is from a future version!"
    wait for textbox
    game over
  &#41;
end
If you want to check saves before loading them, then you can use the import globals command to read the value of the "save version" global, like so:

Code: Select all

variable &#40;slot&#41;
slot &#58;= load menu&#40;false&#41;
if &#40;slot > 0&#41; then &#40;
  if &#40;import globals &#40;slot, @save version&#41; < current game version&#41; then &#40;
    show textbox &#40;42&#41;  # "Save is from a previous version!"
    wait for textbox
    game over
  &#41; else if &#40;import globals &#40;slot, @save version&#41; > current game version&#41; then &#40;
    show textbox &#40;43&#41;  # "Save is from a future version!"
    wait for textbox
    game over
  &#41; else &#40;
    load from slot&#40;slot&#41;
  &#41;
&#41;
If you want to delete old saves, then use a for loop to loop over the slot numbers, and call "delete save(slot)" if it's too old. It's best not to delete saves from future versions!
Last edited by TMC on Sun Jan 29, 2017 8:43 am, edited 1 time in total.
Post Reply