]> git.pond.sub.org Git - empserver/blob - src/lib/subs/fileinit.c
Update copyright notice
[empserver] / src / lib / subs / fileinit.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  fileinit.c: Initialize Empire tables for full server operations.
29  *
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  *     Markus Armbruster, 2005-2008
33  */
34
35 #include <config.h>
36
37 #include "file.h"
38 #include "nat.h"
39 #include "optlist.h"
40 #include "prototypes.h"
41 #include "unit.h"
42
43 struct fileinit {
44     int ef_type;
45     void (*oninit)(void *);
46     void (*postread)(int, void *);
47     void (*prewrite)(int, void *, void *);
48     int (*onresize)(int);
49 };
50
51 static struct fileinit fileinit[] = {
52     {EF_SECTOR, NULL, sct_postread, sct_prewrite, NULL},
53     {EF_SHIP, NULL, shp_postread, shp_prewrite, unit_onresize},
54     {EF_PLANE, pln_oninit, pln_postread, pln_prewrite, unit_onresize},
55     {EF_LAND, lnd_oninit, lnd_postread, lnd_prewrite, unit_onresize},
56     {EF_NUKE, nuk_oninit, nuk_postread, nuk_prewrite, unit_onresize}
57 };
58
59 static void ef_open_srv(void);
60 static void ef_close_srv(void);
61
62 /*
63  * Initialize empfile for full server operations.
64  */
65 void
66 ef_init_srv(void)
67 {
68     unsigned i;
69
70     for (i = 0; i < sizeof(fileinit) / sizeof(fileinit[0]); i++) {
71         empfile[fileinit[i].ef_type].oninit = fileinit[i].oninit;
72         empfile[fileinit[i].ef_type].postread = fileinit[i].postread;
73         empfile[fileinit[i].ef_type].prewrite = fileinit[i].prewrite;
74         empfile[fileinit[i].ef_type].onresize = fileinit[i].onresize;
75     }
76
77     nsc_init();
78     ef_open_srv();
79     if (ef_verify() < 0)
80         exit(EXIT_FAILURE);
81     global_init();
82     unit_cargo_init();
83 }
84
85 void
86 ef_fin_srv(void)
87 {
88     ef_close_srv();
89 }
90
91 static void
92 ef_open_srv(void)
93 {
94     int failed = 0;
95
96     failed |= !ef_open(EF_NATION, EFF_MEM, MAXNOC);
97     failed |= !ef_open(EF_SECTOR, EFF_MEM, WORLD_SZ());
98     failed |= !ef_open(EF_SHIP, EFF_MEM, -1);
99     failed |= !ef_open(EF_PLANE, EFF_MEM, -1);
100     failed |= !ef_open(EF_LAND, EFF_MEM, -1);
101     failed |= !ef_open(EF_GAME, EFF_MEM, 1);
102     failed |= !ef_open(EF_NEWS, 0, -1);
103     failed |= !ef_open(EF_LOAN, 0, -1);
104     failed |= !ef_open(EF_TREATY, 0, -1);
105     failed |= !ef_open(EF_NUKE, EFF_MEM, -1);
106     failed |= !ef_open(EF_POWER, 0, -1);
107     failed |= !ef_open(EF_TRADE, 0, -1);
108     failed |= !ef_open(EF_MAP, EFF_MEM, MAXNOC);
109     failed |= !ef_open(EF_BMAP, EFF_MEM, MAXNOC);
110     failed |= !ef_open(EF_COMM, 0, -1);
111     failed |= !ef_open(EF_LOST, 0, -1);
112     failed |= !ef_open(EF_REALM, EFF_MEM, MAXNOC * MAXNOR);
113     if (!failed)
114         failed |= ef_open_view(EF_COUNTRY, EF_NATION);
115     if (failed) {
116         logerror("Missing files, giving up");
117         exit(EXIT_FAILURE);
118     }
119 }
120
121 static void
122 ef_close_srv(void)
123 {
124     ef_close(EF_COUNTRY);
125     ef_close(EF_NATION);
126     ef_close(EF_SECTOR);
127     ef_close(EF_SHIP);
128     ef_close(EF_PLANE);
129     ef_close(EF_LAND);
130     ef_close(EF_GAME);
131     ef_close(EF_NEWS);
132     ef_close(EF_LOAN);
133     ef_close(EF_TREATY);
134     ef_close(EF_NUKE);
135     ef_close(EF_POWER);
136     ef_close(EF_TRADE);
137     ef_close(EF_MAP);
138     ef_close(EF_COMM);
139     ef_close(EF_BMAP);
140     ef_close(EF_LOST);
141     ef_close(EF_REALM);
142 }