2017-12-29 06:13:28 +01:00
|
|
|
#ifdef _MSC_VER
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
2017-12-29 06:13:28 +01:00
|
|
|
#endif
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "calendar.h"
|
2017-05-07 17:46:51 +02:00
|
|
|
#include "move.h" /* storms */
|
|
|
|
|
2018-05-06 18:05:35 +02:00
|
|
|
#include "kernel/config.h"
|
|
|
|
#include "util/log.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <stdlib.h>
|
2017-05-07 17:46:51 +02:00
|
|
|
#include <stdio.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2018-02-14 20:00:48 +01:00
|
|
|
int turn = 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
int first_month = 0;
|
2017-05-07 15:50:19 +02:00
|
|
|
int weeks_per_month = 3;
|
2014-06-12 05:30:34 +02:00
|
|
|
int months_per_year = 12;
|
2017-05-07 17:46:51 +02:00
|
|
|
const char *seasonnames[CALENDAR_SEASONS] = { "winter", "spring", "summer", "fall" };
|
2010-08-08 10:06:34 +02:00
|
|
|
char **weeknames = NULL;
|
|
|
|
char **weeknames2 = NULL;
|
2011-03-07 08:02:35 +01:00
|
|
|
int *month_season = NULL;
|
2017-05-07 17:46:51 +02:00
|
|
|
|
|
|
|
const char *calendar_month(int index)
|
|
|
|
{
|
|
|
|
static char result[20];
|
|
|
|
snprintf(result, sizeof(result), "month_%d", index + 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *calendar_era(void)
|
|
|
|
{
|
|
|
|
static char result[20];
|
|
|
|
int era = config_get_int("game.era", 1);
|
|
|
|
snprintf(result, sizeof(result), "age_%d", era);
|
|
|
|
return result;
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-29 06:13:28 +01:00
|
|
|
const gamedate *get_gamedate(int turn_now, 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-12-29 06:13:28 +01:00
|
|
|
int t = turn_now - first_turn();
|
2015-01-30 20:37:14 +01:00
|
|
|
|
|
|
|
assert(gd);
|
2018-05-06 18:05:35 +02:00
|
|
|
if (t<0) {
|
2018-05-12 22:12:14 +02:00
|
|
|
t = 0;
|
|
|
|
log_error("current turn %d is before first %d",
|
2018-05-06 18:05:35 +02:00
|
|
|
turn_now, first_turn());
|
|
|
|
}
|
2017-05-07 17:46:51 +02:00
|
|
|
assert(t>=0);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
2017-12-29 06:13:28 +01:00
|
|
|
gd->turn = turn_now;
|
2015-01-30 20:37:14 +01:00
|
|
|
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 */
|
2017-12-22 11:15:17 +01:00
|
|
|
gd->year = 1 + t / weeks_per_year;
|
2015-01-30 20:37:14 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-05-07 17:46:51 +02:00
|
|
|
|
|
|
|
free(storms);
|
|
|
|
storms = 0;
|
|
|
|
free(month_season);
|
|
|
|
month_season = 0;
|
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;
|
2017-05-07 15:50:19 +02:00
|
|
|
|
|
|
|
first_month = 0;
|
|
|
|
weeks_per_month = 3;
|
|
|
|
months_per_year = 12;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|