1 #define _POSIX_C_SOURCE 200112L
12 void dumpgenjson(struct afrhex *map, int gen, int year);
13 void hexlistjson(struct afrhex *map);
15 static void setpop(struct afrhex *map, int hexid, int *pop) {
19 hex = find_hex(map, hexid);
20 for (i=0; i < 2; i++) {
21 hex->freq[i] = pop[i];
25 static void setpopstr(struct afrhex *map, char *spec) {
33 hex = strtol(spec, &resume, 10);
34 if (errno) goto error;
36 for (i = 0; i < 2; i++) {
37 if (*resume != ':' && *resume != ',') goto error;
38 start = resume+1; /* skip the colon or comma */
40 freq[i] = strtol(start, &resume, 10);
41 if (errno) goto error;
43 setpop(map, hex, freq);
47 fprintf(stderr, "unable to parse popspec '%s'\n", spec);
48 fprintf(stderr, "format is hex:freq,freq\n");
52 int main (int ac, char *av[]) {
60 unsigned int seed = 0;
66 double capfactor = 0.25;
69 map = map_init(maphexes, 100.0, capfactor);
71 clock_gettime(CLOCK_REALTIME, &ts);
72 seed = ((unsigned long)(ts.tv_sec) ^ (unsigned long)ts.tv_nsec) & 0xffffffff;
74 while ((opt = getopt(ac, av, "r:s:ehp:y:g:m:t:d:")) != -1) {
76 case 's': seed = atoi(optarg); break;
77 case 'r': run = atoi(optarg); break;
78 case 'y': year = atoi(optarg); break;
79 case 'g': gens = atoi(optarg); break;
80 case 'm': capfactor = strtod(optarg, 0); climate_init(100.0, capfactor); break;
81 case 't': strcpy(tag, optarg);
82 case 'e': eemian = 1; break;
83 case 'h': holocene = 1; break;
84 case 'p': setpopstr(map, optarg); defaultpop = 0;break;
86 fprintf(stderr, "Usage: %s [-s seed] [-r run] [-y startyear] [-eh] [-p hex:freq,freq[:freq,freq...]\n",
96 hex = find_hex(map, 4788);
97 hex->freq[0] = hex->freq[1] = 2000;
98 hex->pop = hex->freq[0] + hex->freq[1];
102 r = var_rhyper(1, 5, 5);
103 printf("rhg = %u\n", r);
106 /* dump out the coordinates of the hexes */
108 printf("map init complete (%p)\n", map);
112 for (hex = map; hex; hex = hex->next) {
113 HL_vertices(hex->hex, hexvert);
114 printf("\t{ \"hex\": %lld, \"coords\": [", hex->hex);
115 for (i=0; i<5; i++) {
116 printf("%f,%f, ", hexvert[i*2], hexvert[i*2+1]);
118 printf("%f,%f", hexvert[5*2], hexvert[5*2+1]);
125 printf(" \"run\": %d, \"seed\": %d, \"gens\": %d", run, seed, gens);
126 printf(",\n\"gendata\": [\n");
128 dumpgenjson(map, 0, year);
130 for (gen = 1; gen <= gens; gen++) {
132 nextgen(map, year, holocene + eemian > 0);
134 dumpgenjson(map, gen, year);
138 printf(",\n\"hexlist\": ");
146 void dumpgenjson(struct afrhex *map, int gen, int year) {
149 unsigned int maxpop = 0;
151 printf("{ \"gen\": %d, \"year\": %d, \"pop\": %llu, \"hexes\": [\n",
152 gen, year, total_pop(map)
154 for (hex = map; hex; hex = hex->next) {
155 if (hex->pop == 0) continue;
156 if (hex->pop > maxpop) {
160 printf("%s\t{ \"hex\": %lld, \"pop\": %u, \"freq\": [%u, %u], "
161 "\"climate\": \"%s\", \"cap\": %u, \"outpop\": [%u, %u], "
162 "\"inpop\": [%u, %u]"
164 count > 1 ? ",\n" : "",
165 hex->hex, hex->pop, hex->freq[0], hex->freq[1],
166 hex->climate->code, hex->climate->cap,
167 hex->outmigrate[0], hex->outmigrate[1],
168 hex->inmigrate[0], hex->inmigrate[1]
171 printf(" ],\n\t\"maxpop\": %u}", maxpop);
174 void hexlistjson(struct afrhex *map) {
179 for (hex = map; hex; hex = hex->next) {
181 count > 0 ? "," : "",
182 count % 10 == 0 ? "\n" : "",