]> git.pond.sub.org Git - empserver/blob - src/util/files.c
Clean up how game state file sizes are checked
[empserver] / src / util / files.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  files.c: Create all the misc files
28  *
29  *  Known contributors to this file:
30  *     Thomas Ruschak
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1998
33  *     Markus Armbruster, 2004-2010
34  */
35
36 #include <config.h>
37
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include "file.h"
43 #include "game.h"
44 #include "land.h"
45 #include "misc.h"
46 #include "nat.h"
47 #include "nuke.h"
48 #include "optlist.h"
49 #include "plane.h"
50 #include "power.h"
51 #include "prototypes.h"
52 #include "sect.h"
53 #include "ship.h"
54 #include "tel.h"
55 #include "trade.h"
56 #include "version.h"
57
58 static void file_sct_init(coord, coord, struct sctstr *ptr);
59
60 static void
61 print_usage(char *program_name)
62 {
63     printf("Usage: %s [OPTION]...\n"
64            "  -e CONFIG-FILE  configuration file\n"
65            "                  (default %s)\n"
66            "  -f              force overwrite of existing game\n"
67            "  -h              display this help and exit\n"
68            "  -v              display version information and exit\n",
69            program_name, dflt_econfig);
70 }
71
72 int
73 main(int argc, char *argv[])
74 {
75     char buf[255];
76     char *filename;
77     int x, y;
78     struct gamestr game;
79     struct natstr nat;
80     struct realmstr realm;
81     struct sctstr sct;
82     int i, j;
83     char *map;
84     int opt;
85     char *config_file = NULL;
86     int force = 0;
87
88     while ((opt = getopt(argc, argv, "e:fhv")) != EOF) {
89         switch (opt) {
90         case 'e':
91             config_file = optarg;
92             break;
93         case 'f':
94             force = 1;
95             break;
96         case 'h':
97             print_usage(argv[0]);
98             exit(0);
99         case 'v':
100             printf("%s\n\n%s", version, legal);
101             exit(0);
102         default:
103             fprintf(stderr, "Try -h for help.\n");
104             exit(1);
105         }
106     }
107
108     if (argv[optind]) {
109         fprintf(stderr, "%s: does not take operands\n", argv[0]);
110         fprintf(stderr, "Try -h for help.\n");
111         exit(1);
112     }
113
114     empfile_init();
115     if (emp_config(config_file) < 0)
116         exit(1);
117     empfile_fixup();
118
119     if (mkdir(gamedir, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
120         perror(gamedir);
121         printf("Can't make game directory\n");
122         exit(1);
123     }
124     if (chdir(gamedir)) {
125         fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
126         exit(EXIT_FAILURE);
127     }
128
129     if (!force) {
130         printf("WARNING: this blasts the existing game in %s (if any)\n",
131                gamedir);
132         printf("continue? ");
133         if (!fgets(buf, sizeof(buf), stdin) || (*buf != 'y' && *buf != 'Y'))
134             exit(1);
135     }
136     for (i = 0; i < EF_MAX; i++) {
137         if (!EF_IS_GAME_STATE(i))
138             continue;
139         if (!ef_open(i, EFF_CREATE | EFF_NOTIME)) {
140             perror("ef_open");
141             exit(1);
142         }
143     }
144     memset(&game, 0, sizeof(game));
145     game.ef_type = EF_GAME;
146     game.game_turn = 1;
147     ef_write(EF_GAME, 0, &game);
148     memset(&nat, 0, sizeof(nat));
149     nat.ef_type = EF_NATION;
150     strcpy(nat.nat_cnam, "POGO");
151     strcpy(nat.nat_pnam, "peter");
152     nat.nat_stat = STAT_GOD;
153     nat.nat_btu = 255;
154     nat.nat_money = 123456789;
155     nat.nat_cnum = 0;
156     nat.nat_flags |= NF_FLASH;
157     putnat((&nat));
158     printf("All praise to %s!\n", nat.nat_cnam);
159     memset(&nat, 0, sizeof(nat));
160     for (i = 1; i < MAXNOC; i++) {
161         nat.ef_type = EF_NATION;
162         nat.nat_cnum = nat.nat_uid = i;
163         nat.nat_seqno = 0;
164         putnat((&nat));
165     }
166     memset(&realm, 0, sizeof(realm));
167     realm.ef_type = EF_REALM;
168     for (i = 0; i < MAXNOC; i++) {
169         realm.r_cnum = i;
170         for (j = 0; j < MAXNOR; j++) {
171             realm.r_realm = j;
172             realm.r_uid = (i * MAXNOR) + j;
173             realm.r_seqno = 0;
174             putrealm(&realm);
175         }
176     }
177     if (mkdir(teldir, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
178         perror(teldir);
179         printf("Can't make telegram directory\n");
180         exit(1);
181     }
182     for (i = 0; i < MAXNOC; i++) {
183         filename = mailbox(buf, i);
184         close(creat(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
185     }
186     close(creat(annfil, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
187
188     /* create a zero-filled sector file */
189     memset(&sct, 0, sizeof(sct));
190     for (y = 0; y < WORLD_Y; y++) {
191         for (x = 0; x < WORLD_X / 2; x++) {
192             file_sct_init(x * 2 + (y & 1), y, &sct);
193             putsect(&sct);
194         }
195     }
196     map = calloc(WORLD_SZ(), sizeof(*map));
197     for (i = 0; i < MAXNOC; i++) {
198         ef_write(EF_MAP, i, map);
199     }
200     for (i = 0; i < MAXNOC; i++) {
201         ef_write(EF_BMAP, i, map);
202     }
203     for (i = 0; i < EF_MAX; i++) {
204         if (!EF_IS_GAME_STATE(i))
205             continue;
206         ef_close(i);
207     }
208
209     exit(0);
210 }
211
212 static void
213 file_sct_init(coord x, coord y, struct sctstr *ptr)
214 {
215     struct sctstr *sp = (struct sctstr *)ptr;
216
217     sp->ef_type = EF_SECTOR;
218     sp->sct_uid = XYOFFSET(x, y);
219     sp->sct_seqno = 0;
220     sp->sct_x = x;
221     sp->sct_y = y;
222     sp->sct_dist_x = x;
223     sp->sct_dist_y = y;
224     sp->sct_newtype = sp->sct_type = SCT_WATER;
225     sp->sct_coastal = 1;
226 }