2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include "calendar.h"
|
2017-01-22 05:13:44 +01:00
|
|
|
#include <kernel/config.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <stdlib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
int first_month = 0;
|
2014-06-12 05:30:34 +02:00
|
|
|
int weeks_per_month = 4;
|
|
|
|
int months_per_year = 12;
|
2010-08-08 10:06:34 +02:00
|
|
|
char **seasonnames = NULL;
|
|
|
|
char **weeknames = NULL;
|
|
|
|
char **weeknames2 = NULL;
|
|
|
|
char **monthnames = NULL;
|
2011-03-07 08:02:35 +01:00
|
|
|
int *month_season = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
char *agename = NULL;
|
2011-03-07 08:02:35 +01:00
|
|
|
int seasons = 0;
|
|
|
|
|
2017-01-22 05:13:44 +01:00
|
|
|
int first_turn(void)
|
|
|
|
{
|
2017-03-01 08:28:15 +01:00
|
|
|
return config_get_int("game.start", 0);
|
2017-01-22 05:13:44 +01:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
const gamedate *get_gamedate(int turn, gamedate * gd)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int weeks_per_year = months_per_year * weeks_per_month;
|
2017-01-22 05:13:44 +01:00
|
|
|
int t = turn - first_turn();
|
2015-01-30 20:37:14 +01:00
|
|
|
|
|
|
|
assert(gd);
|
|
|
|
if (t < 0)
|
|
|
|
t = turn;
|
|
|
|
|
|
|
|
gd->week = t % weeks_per_month; /* 0 - weeks_per_month-1 */
|
|
|
|
gd->month = (t / weeks_per_month + first_month) % months_per_year; /* 0 - months_per_year-1 */
|
|
|
|
gd->year = t / (weeks_per_year)+1;
|
|
|
|
gd->season = month_season ? month_season[gd->month] : 0;
|
|
|
|
return gd;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void calendar_cleanup(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
free(agename);
|
|
|
|
|
2016-09-09 18:39:38 +02:00
|
|
|
if (seasonnames) {
|
|
|
|
for (i = 0; i != seasons; ++i) {
|
|
|
|
free(seasonnames[i]);
|
|
|
|
}
|
|
|
|
free(seasonnames);
|
|
|
|
seasonnames = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:39:38 +02:00
|
|
|
if (monthnames) {
|
|
|
|
for (i = 0; i != months_per_year; ++i) {
|
|
|
|
free(monthnames[i]);
|
|
|
|
}
|
|
|
|
free(storms);
|
|
|
|
storms = 0;
|
|
|
|
free(month_season);
|
|
|
|
month_season = 0;
|
|
|
|
free(monthnames);
|
|
|
|
monthnames = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i != weeks_per_month; ++i) {
|
2016-09-09 18:39:38 +02:00
|
|
|
if (weeknames)
|
|
|
|
free(weeknames[i]);
|
|
|
|
if (weeknames2)
|
|
|
|
free(weeknames2[i]);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
free(weeknames);
|
2016-09-09 18:39:38 +02:00
|
|
|
weeknames = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
free(weeknames2);
|
2016-09-09 18:39:38 +02:00
|
|
|
weeknames2 = 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|