-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.c
185 lines (156 loc) · 4.2 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Atlantis v1.0 13 September 1993
* Copyright 1993 by Russell Wallace
*
* This program may be freely used, modified and distributed. It may not be
* sold or used commercially without prior written permission from the author.
*/
#include <atlantis.h>
#include <region.h>
#include <faction.h>
#include <game.h>
#include <settings.h>
#include <rtl.h>
#include "json.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <quicklist.h>
#include <stream.h>
#include <filestream.h>
#include <cJSON.h>
static void fixme() {
if (turn==0) { /* forgot to initialize regions with money */
ql_iter rli;
for (rli = qli_init(®ions); qli_more(rli);) {
region * r = (region *)qli_next(&rli);
r->money = r->peasants * 3 / 2;
}
}
}
static void reports(void)
{
FILE * F;
ql_iter fli;
_mkdir("reports");
for (fli = qli_init(&factions); qli_more(fli);) {
faction * f = (faction *)qli_next(&fli);
write_reports(f);
}
F = fopen("send", "w");
puts("Writing send file...");
for (fli = qli_init(&factions); qli_more(fli);) {
faction * f = (faction *)qli_next(&fli);
const char * addr = faction_getaddr(f);
if (addr) {
fprintf(F, "mail %d-%d.r\n", turn, f->no);
fprintf(F, "in%%\"%s\"\n", addr);
fprintf(F, "Atlantis Report for %s\n", gamedate());
}
}
fclose(F);
F = fopen("maillist", "w");
puts("Writing maillist file...");
for (fli = qli_init(&factions); qli_more(fli);) {
faction * f = (faction *)qli_next(&fli);
const char * addr = faction_getaddr(f);
if (addr) {
fprintf(F, "%s\n", addr);
}
}
fclose(F);
}
static int processturn(const char *orders)
{
turn++;
readorders(orders);
processorders();
reports();
writesummary();
writegame();
return 0;
}
int main(int argc, char **argv)
{
int i;
char buf[12];
const char *arg, *orders = 0, *cfgfile = 0;
rnd_seed((unsigned int) time(0));
puts("Atlantis v1.0 " __DATE__ "\n"
"Copyright 1993 by Russell Wallace.\n"
"Type ? for list of commands.");
turn = -1;
for (i = 1; i != argc; ++i) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'c':
cfgfile = (argv[i][2]) ? (argv[i] + 2) : argv[++i];
break;
case 'i':
ignore_password = 1;
break;
case 'p': /* process */
orders = (argv[i][2]) ? (argv[i] + 2) : argv[++i];
break;
case 't': /* turn */
arg = (argv[i][2]) ? (argv[i] + 2) : argv[++i];
turn = atoi(arg);
break;
default:
fprintf(stderr, "invalid argument %d: '%s'\n", i, argv[i]);
return -1;
}
}
}
if (cfgfile) {
int err = read_config(cfgfile);
if (err) return err;
}
initgame();
if (orders) {
return processturn(orders);
}
for (;;) {
printf("> ");
fgets(buf, sizeof(buf), stdin);
switch (tolower(buf[0])) {
case 'm':
writemap(stdout);
break;
case 'g':
turn = 0;
cleargame(false);
autoworld("players");
writemap(stdout);
break;
case 'r':
reports();
break;
case 'f':
fixme();
break;
case 'w':
writesummary();
writegame();
break;
case 'p':
printf("Name of orders file? ");
fgets(buf, sizeof(buf), stdin);
if (!buf[0]) return -1;
return processturn(buf);
case 'q':
return 0;
default:
puts("C - Create New Continent.\n"
"A - Add New Players.\n"
"M - Draw Map.\n"
"P - Process Game Turn.\n"
"R - Write Reports.\n"
"G - Generate New World.\n"
"Q - Quit.\n"
"W - Write Game.\n");
}
}
}