#include #include #include #include "fate.h" #include "weather.h" static uint64_t seed; static int seeded; void set_almanac(uint64_t s) { seed = s; seeded = 1; } static uint64_t almanac_seed(void) { if (!seeded) set_almanac(strtoull(read_record("almanac")->field[0][1], NULL, 10)); return seed; } const char *weather(struct day day) { const struct record *climate = read_record("climate"); unsigned total = 0; for (int row = 0; row < climate->rows; row++) if (atoi(climate->field[row][0]) == day.m) total += (unsigned)atoi(climate->field[row][2]); unsigned roll = fate(almanac_seed(), day_number(day), 0, total); for (int row = 0; row < climate->rows; row++) { if (atoi(climate->field[row][0]) != day.m) continue; unsigned weight = (unsigned)atoi(climate->field[row][2]); if (roll < weight) return climate->field[row][1]; roll -= weight; } fprintf(stderr, "no climate for month %d\n", day.m); exit(1); } struct conditions conditions(struct day day) { const char *what = weather(day); const struct record *effects = read_record("weather"); for (int row = 0; row < effects->rows; row++) { if (strcmp(effects->field[row][0], what) == 0) { struct conditions c; snprintf(c.weather, FIELD, "%s", what); c.walk = atoi(effects->field[row][1]); c.van = atoi(effects->field[row][2]); return c; } } fprintf(stderr, "no record of what %s does\n", what); exit(1); }