server/src/kernel/calendar.c

88 lines
1.9 KiB
C
Raw Normal View History

#ifdef _MSC_VER
2010-08-08 10:06:34 +02:00
#include <platform.h>
#endif
2010-08-08 10:06:34 +02:00
#include "calendar.h"
#include "move.h" /* storms */
#include "kernel/config.h"
#include "util/log.h"
2010-08-08 10:06:34 +02:00
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
2010-08-08 10:06:34 +02: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;
int months_per_year = 12;
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;
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
int first_turn(void)
{
2017-03-01 08:28:15 +01:00
return config_get_int("game.start", 0);
}
const gamedate *get_gamedate(int turn_now, gamedate * gd)
2010-08-08 10:06:34 +02:00
{
int weeks_per_year = months_per_year * weeks_per_month;
int t = turn_now - first_turn();
assert(gd);
if (t<0) {
t = 0;
log_error("current turn %d is before first %d",
turn_now, first_turn());
}
assert(t>=0);
gd->turn = turn_now;
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 = 1 + t / weeks_per_year;
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
{
int i;
for (i = 0; i != weeks_per_month; ++i) {
if (weeknames)
free(weeknames[i]);
if (weeknames2)
free(weeknames2[i]);
}
free(storms);
storms = 0;
free(month_season);
month_season = 0;
free(weeknames);
weeknames = 0;
free(weeknames2);
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
}