#include #include #include #include #include #include "census.h" #include "fate.h" #include "history.h" #include "post.h" #include "town.h" /* Questions fate is asked, each month. */ enum { DIES, MOVES, GOES_TO_CHURCH, CHANGES_HABITS, HOW_MUCH, WHERE_TO, CHILD_NAME, CHILD_WEEKDAYS, CHILD_WEEKENDS, CHILD_WRITES, CHILD_CLOCK, }; #define EARLIEST (4 * 60) #define LATEST (11 * 60) void (*chronicle)(struct day day, const struct household *h, const char *what); static void note(struct day day, const struct household *h, const char *format, ...) { if (!chronicle) return; char what[256]; va_list args; va_start(args, format); vsnprintf(what, sizeof what, format, args); va_end(args); chronicle(day, h, what); } int moves(const struct household *h, int month) { return h->kind == RESIDENT && fate(h->seed, month, MOVES, 240) == 0; } int active(const struct household *h, struct day day) { return day_number(day) >= day_number(h->since); } int fast(const struct household *h, struct day day) { int days = day_number(day) - day_number(h->set); return days * h->gains / 60 + h->adjusted; /* Nobody counts the part of a minute. */ } static int clamp(int time) { return time < EARLIEST ? EARLIEST : time > LATEST ? LATEST : time; } static struct act acts[MAX_ACTS]; static int count, known; static const char *const deeds[] = {"sees to the naming", "finds them a house", "sets the clock", "has a word"}; void set_society(const struct act *from, int n) { if (n > MAX_ACTS) { fprintf(stderr, "the Society does no more than %d things\n", MAX_ACTS); exit(1); } memcpy(acts, from, sizeof acts[0] * (size_t)n); count = n; known = 1; } int society(const struct act **out) { if (!known) { const struct record *record = read_record("society"); struct act read[MAX_ACTS]; int n = 0; for (int row = 0; row < record->rows; row++) { const char(*f)[FIELD] = record->field[row]; struct act *a = &read[n++]; memset(a, 0, sizeof *a); a->month = month_of(parse_day(f[0])); snprintf(a->line, sizeof a->line, "%s", f[1]); a->deed = NAMES; while (strcmp(deeds[a->deed], f[2]) != 0) if (++a->deed > WORDS) { fprintf(stderr, "the Society does not do \"%s\"\n", f[2]); exit(1); } snprintf(a->what, FIELD, "%s", f[3]); a->minutes = atoi(f[3]); } set_society(read, n); } *out = acts; return count; } static const char *minutes(int n) { return abs(n) == 1 ? "minute" : "minutes"; } /* How the chronicle speaks of a household. */ static const char *called(const struct household *h) { static char name[FIELD + 8]; snprintf(name, sizeof name, "%s%s", h->kind == RESIDENT ? "" : "the ", h->name); return name; } static struct household *line_of(struct town *town, const char *line) { for (int i = 0; i < town->lines; i++) if (strcmp(town->line[i].line, line) == 0) return &town->line[i]; return NULL; } /* One act, done by a member if one can get to the household. Naming is done at the * christening, in church, where anyone may be. */ static void perform(struct household *h, const struct household *member, const struct act *a, struct day day) { if (a->deed != NAMES && (!member || distance(day, member->home, h->home) > REACH)) { note(day, h, "The Society could not reach %s.", called(h)); return; } switch (a->deed) { case NAMES: note(day, h, "The Society sees to it that the child is called %s, not %s.", a->what, h->name); snprintf(h->name, FIELD, "%s", a->what); break; case HOUSES: note(day, h, "The Society finds %s a house at %s.", called(h), a->what); snprintf(h->home, FIELD, "%s", a->what); break; case CLOCKS: note(day, h, "The Society puts %s's clock %d %s %s.", called(h), abs(a->minutes), minutes(a->minutes), a->minutes > 0 ? "fast" : "slow"); h->adjusted += a->minutes; break; case WORDS: note(day, h, "The Society has a word with %s, who %s %d %s %s.", called(h), h->kind == RESIDENT ? "starts getting up" : "starts going to the post", abs(a->minutes), minutes(a->minutes), a->minutes > 0 ? "later" : "earlier"); h->weekdays = clamp(h->weekdays + a->minutes); h->weekends = clamp(h->weekends + a->minutes); break; } } /* What the Society does this month, after everything else. It sees to its own first. */ void carry_out(struct town *town, int month) { struct day day = month_day(month); const struct act *a; int n = society(&a); for (int own = 1; own >= 0; own--) { for (int i = 0; i < n; i++) { struct household *h = a[i].month == month ? line_of(town, a[i].line) : NULL; if (!h || h->member != own) continue; const struct household *member = NULL; /* Prefer a member who can walk to the household; the deputy keeps the office too. */ for (int j = 0; j < town->lines && !member; j++) if (town->line[j].member && active(&town->line[j], day) && distance(day, town->line[j].home, h->home) <= REACH) member = &town->line[j]; if (!member) for (int j = 0; j < town->lines && !member; j++) if (town->line[j].member && active(&town->line[j], day)) member = &town->line[j]; perform(h, member, &a[i], day); } } } int real_time(const struct household *h, int shown, struct day day) { return shown - fast(h, day); } int names_for(char initial, const char *out[]) { const struct record *names = read_record("names"); for (int row = 0; row < names->rows; row++) { if (names->field[row][0][0] != initial) continue; static char split[MAX_ROWS][8][FIELD]; char list[FIELD]; int n = 0; snprintf(list, sizeof list, "%s", names->field[row][1]); for (char *name = list, *comma; name && n < 8; name = comma ? comma + 1 : NULL) { if ((comma = strchr(name, ','))) *comma = '\0'; while (*name == ' ') name++; snprintf(split[row][n], FIELD, "%s", name); out[n] = split[row][n]; n++; } return n; } fprintf(stderr, "no names begin with %c\n", initial); exit(1); } int homes(const char *out[]) { const char *all[MAX_PLACES]; int n = every_place(all), count = 0; for (int i = 0; i < n; i++) if (strcmp(all[i], OFFICE) != 0 && strcmp(all[i], "Fire Station") != 0) out[count++] = all[i]; return count; } /* One in how many heads of household die in a month, by how long they have kept the house. */ static unsigned mortality(int months) { int years = months / 12; return years < 25 ? 900 : years < 40 ? 180 : years < 50 ? 48 : 12; } static void succeed(struct household *h, int month, struct day day) { const char *names[8]; int n = names_for(h->initial, names); char parent[FIELD]; snprintf(parent, FIELD, "%s", h->name); snprintf(h->name, FIELD, "%s", names[fate(h->seed, month, CHILD_NAME, (unsigned)n)]); note(day, h, "%s has died. %s has the house at %s.", parent, h->name, h->home); h->weekdays = clamp(h->weekdays + (int)fate(h->seed, month, CHILD_WEEKDAYS, 61) - 30); h->weekends = clamp(h->weekends + (int)fate(h->seed, month, CHILD_WEEKENDS, 61) - 30); h->writes = 10 + (int)fate(h->seed, month, CHILD_WRITES, 31); h->gains = (int)fate(h->seed, month, CHILD_CLOCK, 21) - 10; h->set = day; /* The first thing a child of the house does is go to church. */ h->adjusted = 0; h->generation++; h->head_since = month; h->misses = h->early = 0; } void live(struct household *h, int month) { struct day day = month_day(month); if (h->kind == BELL || !active(h, day)) return; struct sender sender = sender_of(h, day); struct letter letter; if (h->member) { /* Those at the dead letter office do not write to the World. They read what is written. */ } else if (post(&sender, day, &letter)) { h->misses = 0; if (letter.visit.at - letter.posted <= 60) { h->early = 0; } else if (++h->early == 3) { h->weekdays = clamp(h->weekdays + 10); h->weekends = clamp(h->weekends + 10); h->early = 0; note(day, h, "%s has been an hour early for the post three times, and will stay in bed ten minutes longer.", h->name); } } else { h->early = 0; if (++h->misses == 3) { h->weekdays = clamp(h->weekdays - 10); h->weekends = clamp(h->weekends - 10); h->misses = 0; note(day, h, "%s has missed the post three times, and will get up ten minutes earlier.", h->name); } } if (fate(h->seed, month, GOES_TO_CHURCH, 12) == 0) { if (fast(h, day)) note(day, h, "%s sets the clock by the bell. It was %d minutes %s.", h->name, abs(fast(h, day)), fast(h, day) > 0 ? "fast" : "slow"); h->set = day; h->adjusted = 0; } if (h->kind == INSTITUTION) return; if (fate(h->seed, month, DIES, mortality(month - h->head_since)) == 0) succeed(h, month, day); if (moves(h, month)) { const char *places[MAX_PLACES]; int n = homes(places); const char *to = places[fate(h->seed, month, WHERE_TO, (unsigned)n)]; if (strcmp(to, h->home) != 0) note(day, h, "%s moves from %s to %s.", h->name, h->home, to); snprintf(h->home, FIELD, "%s", to); } if (fate(h->seed, month, CHANGES_HABITS, 24) == 0) { int change = 5 * ((int)fate(h->seed, month, HOW_MUCH, 3) - 1); h->weekdays = clamp(h->weekdays + change); h->weekends = clamp(h->weekends + change); if (change) note(day, h, "%s starts getting up five minutes %s.", h->name, change < 0 ? "earlier" : "later"); } } void advance(struct town *town) { for (int i = 0; i < town->lines; i++) live(&town->line[i], town->month); carry_out(town, town->month); town->month++; } void found(struct town *town) { struct day founded = calendar("Founded"); memset(town, 0, sizeof *town); town->month = month_of(founded); const struct record *founding = read_record("founding"); for (int row = 0; row < founding->rows; row++) { const char(*f)[FIELD] = founding->field[row]; struct household *h = &town->line[town->lines++]; h->kind = RESIDENT; snprintf(h->line, sizeof h->line, "%s", f[0]); h->initial = f[0][0]; snprintf(h->name, FIELD, "%s", f[1]); snprintf(h->home, FIELD, "%s", f[2]); h->weekdays = clock_time(f[3]); h->weekends = clock_time(f[4]); h->writes = atoi(f[5]); h->gains = atoi(f[6]); h->seed = strtoull(f[7], NULL, 10); h->since = h->set = founded; h->head_since = town->month; } const struct record *office = read_record("office"); for (int row = 0; row < office->rows; row++) for (int i = 0; i < town->lines; i++) if (strcmp(office->field[row][1], town->line[i].line) == 0) town->line[i].member = 1; const struct record *institutions = read_record("institutions"); for (int row = 0; row < institutions->rows; row++) { const char(*f)[FIELD] = institutions->field[row]; struct household *h = &town->line[town->lines++]; snprintf(h->name, FIELD, "%s", f[0]); snprintf(h->line, sizeof h->line, "%s", f[0]); h->kind = strcmp(h->name, CHURCH) == 0 ? BELL : INSTITUTION; snprintf(h->home, FIELD, "%s", f[1]); h->since = h->set = parse_day(f[2]); h->weekdays = h->weekends = clock_time(f[3]); /* What is sent is written in quotation marks, so that a space can be seen. */ snprintf(h->sends, sizeof h->sends, "%.*s", (int)strlen(f[4]) - 2, f[4] + 1); h->gains = atoi(f[5]); h->seed = strtoull(f[6], NULL, 10); } } const struct town *town_on(struct day day) { static struct town town; static int founded; int month = month_of(day); if (!founded || month < town.month) { found(&town); founded = 1; } while (town.month < month) advance(&town); return &town; }