]> git.pond.sub.org Git - empserver/blob - src/lib/gen/emp_config.c
Import of Empire 4.2.12
[empserver] / src / lib / gen / emp_config.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  emp_config.c: Allows config file to control server config. from a file
29  * 
30  *  Known contributors to this file:
31  *     Julian Onions, 1995
32  *     Steve McClure, 1998-2000
33  */
34
35 /*
36  * STILL TO DO
37  *
38  * 1. Change other constants - such as Num Countries etc.
39  *    Just requires variables to be assigned, then dynamic allocation in
40  *    a few places. Some checks needed in the server to check the world
41  *    hasn't changed size etc.
42  * 2. Could look at loading in planes, units etc. Should be easy enough.
43  *
44  */
45
46 #include <stdio.h>
47 #include <stdlib.h> /* atoi free atol */
48 #ifdef Rel4
49 #include <string.h>
50 #endif /* Rel4 */
51
52 #include "misc.h"
53 #include "com.h"
54 #include "match.h"
55 #include "file.h"
56 #include "optlist.h"
57 #include "gen.h" /* parse */
58
59 /* for systems without strdup  */
60 #ifdef NOSTRDUP
61 extern char *strdup();
62 #endif /* NOSTRDUP */
63
64 /* All the configurable variables ... */
65 extern s_char *infodir, *datadir, *gamedir, *loginport, *privname, *privlog;
66
67 extern double buil_tower_bt, buil_tower_bc;
68 extern double  buytax, flakscale, maxmult, minmult, tradetax, uwbrate;
69 extern double buil_bc, buil_bt, combat_mob, edu_cons, hap_cons, money_civ;
70 extern double money_land, money_mil, money_plane, money_res, money_ship;
71 extern double money_uw, people_damage, powe_cost, unit_damage, babyeat;
72 extern double collateral_dam, assault_penalty;
73 extern double bankint, eatrate, fcrate, fgrate, obrate, mission_mob_cost;
74
75 extern float btu_build_rate, easy_tech, hard_tech, land_mob_scale;
76 extern float level_age_rate, plane_mob_scale, sect_mob_scale;
77 extern float ship_mob_scale, tech_log_base, ally_factor, edu_avg, hap_avg;
78
79 extern int buil_tower_bh;
80 extern int startmob, at_least_one_100, buil_bh, etu_per_update;
81 extern int land_grow_scale, land_mob_max, m_m_p_d, max_btus, max_idle;
82 extern int plane_grow_scale, plane_mob_max, players_at_00, sect_mob_max;
83 extern int ship_grow_scale, ship_mob_max, torpedo_damage;
84 extern int fort_max_interdiction_range;
85 extern int land_max_interdiction_range;
86 extern int ship_max_interdiction_range;
87 extern int sect_mob_neg_factor;
88 extern int lost_items_timeout;
89 extern int WORLD_X;
90 extern int WORLD_Y;
91 extern int MARK_DELAY;
92 extern int TRADE_DELAY;
93
94 extern int morale_base;
95 extern float fire_range_factor;
96
97 extern long adj_update; 
98 extern long s_p_etu;    
99 extern int update_policy;
100 extern s_char *update_times;
101 extern int hourslop;
102 extern int update_window;
103 extern int blitz_time;
104 extern int update_demandpolicy;
105 extern s_char *update_demandtimes;
106 extern int update_wantmin;
107 extern int update_missed;
108 extern s_char *game_days;
109 extern s_char *game_hours;
110
111 /* conditional ones */
112 extern double    decay_per_etu, fallout_spread;
113
114 extern int fuel_mult;
115
116 extern int War_Cost;    
117 extern long last_demand_update; 
118
119 extern float    drnuke_const;
120 extern float start_education, start_happiness;
121 extern float start_technology, start_research;
122
123 extern int   trade_1_dist, trade_2_dist, trade_3_dist;
124 extern float trade_1, trade_2, trade_3, trade_ally_bonus, trade_ally_cut;
125
126 /* Dummy one */
127 static int emp_config_dummy;
128
129 /* things that can be changed */
130 struct keymatch configkeys[] = {
131 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n### Server configuration and information" },
132 { "data",               optstrset,      (caddr_t) &datadir, 0,
133     "Directory the data is stored in" },
134 { "info",               optstrset,      (caddr_t) &infodir, 0,
135     "Directory the info pages are stored in" },
136 { "port",               optstrset,      (caddr_t) &loginport, 0,
137     "TCP/IP port the server will start up on" },
138 { "privname",           optstrset,      (caddr_t) &privname, 0,
139     "Name of the deity" },
140 { "privlog",            optstrset, (caddr_t) &privlog, 0,
141     "E-mail of the deity" },
142 { "WORLD_X",            worldxset, (caddr_t) &WORLD_X, 0,
143     "World size X dimension (enforced to be even by subtracting 1 if necessary)" },
144 { "WORLD_Y",            intset, (caddr_t) &WORLD_Y, 0,
145     "World size Y dimension" },
146
147 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Update policy" },
148 { "update_policy",      intset, (caddr_t) &update_policy, 0,
149     "0 - normal, 1 - update_times, 2 - blitz, 3 - demand only" },
150 { "etu_per_update",     intset, (caddr_t) &etu_per_update, 0,
151     "Number of ETUs per update" },
152 { "s_p_etu",            longset,   (caddr_t) &s_p_etu, 0,
153     "Seconds per etu, updates will occur every s_p_etu * etu_per_update seconds" },
154 { "adj_update",         longset,   (caddr_t) &adj_update, 0,
155     "Move the update forward or backward (in seconds)" },
156 { "update_window",      intset,   (caddr_t) &update_window, 0,
157     "Window the update will occur in (in seconds) before and after the update time" },
158 { "update_times",       optstrset,      (caddr_t) &update_times, 0,
159     "Times when updates occur under policy #1.  Must coincide with schedule." },
160 { "hourslop",           intset, (caddr_t) &hourslop, 0,
161     "Number of minutes update check can slip to match update_times" },
162 { "blitz_time",         intset, (caddr_t) &blitz_time, 0,
163     "Number of minutes between updates under policy #2." },
164
165 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Demand update policy" },
166 { "update_demandpolicy",intset, (caddr_t) &update_demandpolicy, 0,
167     "0 - emp_tm checks, 1 - after setting, 2 - demand updates disabled" },
168 { "update_wantmin",     intset, (caddr_t) &update_wantmin, 0,
169     "number of requests needed for demand update" },
170 { "update_missed",      intset, (caddr_t) &update_missed, 0,
171     "number of demand updates country can miss before veto update" },
172 { "update_demandtimes", optstrset,      (caddr_t) &update_demandtimes, 0,
173     "Times when demand updates can occur.  Ranges CANNOT cross midnight." },
174
175 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Game hours restrictions" },
176 { "game_days",          optstrset,      (caddr_t) &game_days, 0,
177     "Days game is up and running (Su Mo Tu We Th Fr Sa)" },
178 { "game_hours",         optstrset,      (caddr_t) &game_hours, 0,
179     "Hours game is up and running (6:00-18:00)" },
180
181 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Options\n" },
182 { "option",             optionset, (caddr_t) NULL, 0, NULL },
183 { "nooption",           optiondel, (caddr_t) NULL, 0, NULL },
184
185
186 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Countries" },
187 { "btu_build_rate",     floatset, (caddr_t) &btu_build_rate, 0,
188     "Rate at which BTUs accumulate (etu * civ * eff * btu_build_rate)" },
189 { "m_m_p_d",            intset, (caddr_t) &m_m_p_d, 0,
190     "Maximum minutes per day a country is allowed to be logged in" },
191 { "max_btus",           intset, (caddr_t) &max_btus, 0,
192     "Maximum number of BTUs a country can have" },
193 { "max_idle",           intset, (caddr_t) &max_idle, 0,
194     "Maximum number of minutes a player can sit idle while logged in" },
195 { "players_at_00",      intset, (caddr_t) &players_at_00, 0,
196     "Players have their coordinate system at deity 0,0 (0 - no, 1 - yes)" },
197 { "at_least_one_100",   intset, (caddr_t) &at_least_one_100, 0,
198     "Initialize new countries with at least one sector with 100 of all resource" },
199 { "powe_cost",          doubleset, (caddr_t) &powe_cost, 0,
200     "Number of BTUs needed to generate a new power report" },   
201 { "war_cost",   intset, (caddr_t) &War_Cost, 0,
202     "Cost to declare war (if SLOW_WAR is on)" },
203
204 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Technology/Research/Education/Happiness" },
205 { "easy_tech",          floatset, (caddr_t) &easy_tech, 0,
206     "Amount of tech built with no penalty" },   
207 { "hard_tech",          floatset, (caddr_t) &hard_tech, 0,
208     "Amount of in-efficiently built tech" },    
209 { "start_tech",         floatset, (caddr_t) &start_technology, 0,
210     "Starting technology for new countries" },
211 { "start_happy",        floatset, (caddr_t) &start_happiness, 0,
212     "Starting happiness for new countries" },
213 { "start_research",     floatset, (caddr_t) &start_research, 0,
214     "Starting research for new countries" },
215 { "start_edu",          floatset, (caddr_t) &start_education, 0,
216     "Starting education for new countries" },
217 { "level_age_rate",     floatset, (caddr_t) &level_age_rate, 0,
218     "ETU rate at which tech decays (0 -> no decline)" },        
219 { "tech_log_base",      floatset, (caddr_t) &tech_log_base, 0,
220     "Log base to apply to tech breakthroughs above the easy tech level" },
221 { "ally_factor",        floatset, (caddr_t) &ally_factor, 0,
222     "Shared tech with allies (1 / ally_factor)" },
223 { "edu_avg",            floatset, (caddr_t) &edu_avg, 0,
224     "Number of ETUs education is averaged over" },
225 { "hap_avg",            floatset, (caddr_t) &hap_avg, 0,
226     "Number of ETUs happiness is averaged over" },
227 { "edu_cons",           doubleset, (caddr_t) &edu_cons, 0,
228     "Education consumption (1 breakthrough per edu_cons)" },
229 { "hap_cons",           doubleset, (caddr_t) &hap_cons, 0,
230     "Happiness consumption (1 breakthrough per hap_cons)" },
231
232 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Sectors" },
233 { "startmob",           intset, (caddr_t) &startmob, 0,
234     "Starting mobility for sanctuaries" },
235 { "sect_mob_scale",     floatset, (caddr_t) &sect_mob_scale, 0,
236     "Sector mobility accumulation (sect_mob_scale * ETUs per update)" },
237 { "sect_mob_max",       intset, (caddr_t) &sect_mob_max, 0,
238     "Maximum mobility for sectors" },
239 { "buil_bh",            intset, (caddr_t) &buil_bh, 0,
240     "Number of hcms required to build a bridge span" },
241 { "buil_bc",            doubleset, (caddr_t) &buil_bc, 0,
242     "Cash required to build a bridge span" },
243 { "buil_bt",            doubleset, (caddr_t) &buil_bt, 0,
244     "Technology required to build a bridge span" },
245 { "buil_tower_bh",      intset, (caddr_t) &buil_tower_bh, 0,
246     "Number of hcms required to build a bridge tower" },
247 { "buil_tower_bc",      doubleset, (caddr_t) &buil_tower_bc, 0,
248     "Cash required to build a bridge tower" },
249 { "buil_tower_bt",      doubleset, (caddr_t) &buil_tower_bt, 0,
250     "Technology required to build a bridge tower" },
251
252 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Land Units" },
253 { "land_mob_scale",     floatset, (caddr_t) &land_mob_scale, 0,
254     "Land unit mobility accumulation (land_mob_scale * ETUs per update)" },
255 { "land_grow_scale",    intset, (caddr_t) &land_grow_scale, 0,
256     "How fast efficiency grows for land units each update (* ETUs)" },
257 { "land_mob_max",       intset, (caddr_t) &land_mob_max, 0,
258     "Maximum mobility for land units" },
259 { "money_land",         doubleset, (caddr_t) &money_land, 0,
260     "Cost per ETU to maintain land units (percentage of unit price)" },
261 { "morale_base", intset, (caddr_t) &morale_base, 0,
262     "Base level for setting morale of land units" },
263
264 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Planes" },
265 { "plane_mob_scale",    floatset, (caddr_t) &plane_mob_scale, 0,
266     "Plane mobility accumulation (plane_mob_scale * ETUs per update)" },
267 { "plane_grow_scale",   intset, (caddr_t) &plane_grow_scale, 0,
268     "How fast efficiency grows for planes each update (* ETUs)" },
269 { "plane_mob_max",      intset, (caddr_t) &plane_mob_max, 0,
270     "Maximum mobility for planes" },
271 { "money_plane",        doubleset, (caddr_t) &money_plane, 0,
272     "Cost per ETU to maintain planes (percentage of plane price)" },
273
274 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Ships" },
275 { "ship_mob_scale",     floatset, (caddr_t) &ship_mob_scale, 0,
276     "Ship mobility accumulation (ship_mob_scale * ETUs per update)" },
277 { "ship_grow_scale",    intset, (caddr_t) &ship_grow_scale, 0,
278     "How fast efficiency grows for ships each update (* ETUs)" },
279 { "ship_mob_max",       intset, (caddr_t) &ship_mob_max, 0,
280     "Maximum mobility for ships" },
281 { "money_ship",         doubleset, (caddr_t) &money_ship, 0,
282     "Cost per ETU to maintain ships (percentage of ship price)" },
283 { "torpedo_damage",     intset, (caddr_t) &torpedo_damage, 0,
284     "Torpedo damage (damage is X + 1dX + 1dX)" },       
285
286 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Combat/Damage" },
287 { "fort_max_interdiction_range", intset, (caddr_t)&fort_max_interdiction_range, 0,
288     "Maximum range (in sectors) a fort will try to interdict another country" },
289 { "land_max_interdiction_range", intset, (caddr_t)&land_max_interdiction_range, 0,
290     "Maximum range (in sectors) a land unit will try to interdict another country" },
291 { "ship_max_interdiction_range", intset, (caddr_t)&ship_max_interdiction_range, 0,
292     "Maximum range (in sectors) a ship will try to interdict another country" },
293 { "flakscale",          doubleset, (caddr_t) &flakscale, 0,
294     "Scale factor for flak damage" },
295 { "combat_mob",         doubleset, (caddr_t) &combat_mob, 0,
296     "How much mobility do units spend for combat (* casualties/bodies)" },
297 { "people_damage",      doubleset, (caddr_t) &people_damage, 0,
298     "People take this amount of normal damage" },
299 { "unit_damage",        doubleset, (caddr_t) &unit_damage, 0,
300     "Land units take this amount of normal damage" },
301 { "collateral_dam",     doubleset, (caddr_t) &collateral_dam, 0,
302     "Side effect damage amount done to sector" },       
303 { "assault_penalty",    doubleset, (caddr_t) &assault_penalty, 0,
304     "Amount of normal attacking efficiency for paratroopers and assaulting" },
305 { "fire_range_factor",  floatset, (caddr_t) &fire_range_factor, 0,
306     "Scale normal firing ranges by this amount" },
307 { "sect_mob_neg_factor", intset, (caddr_t) &sect_mob_neg_factor, 0,
308     "Amount of negative mobility a sector has after takeover (ETU / x) (MOB_ACCESS)" },
309 { "mission_mob_cost",   doubleset, (caddr_t) &mission_mob_cost, 0,
310     "Cost to put something on a mission (percentage of max mob)" },     
311
312 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Populace" },
313 { "uwbrate",            doubleset, (caddr_t) &uwbrate, 0,
314     "Birth rate for uw's" },
315 { "money_civ",          doubleset, (caddr_t) &money_civ, 0,
316     "Money gained from taxes on a civilian in one ETU" },
317 { "money_mil",          doubleset, (caddr_t) &money_mil, 0,
318     "Money gained from taxes on an active soldier in one ETU" },
319 { "money_res",          doubleset, (caddr_t) &money_res, 0,
320     "Money gained from taxes on a soldier on active reserve in one ETU" },
321 { "money_uw",           doubleset, (caddr_t) &money_uw, 0,
322     "Money gained from taxes on an uncompensated worker in one ETU" },
323 { "babyeat",            doubleset, (caddr_t) &babyeat, 0,
324     "Amount of food to mature 1 baby into a civilian" },        
325 { "bankint",            doubleset, (caddr_t) &bankint, 0,
326     "Bank dollar gain (per bar per etu)" },     
327 { "eatrate",            doubleset, (caddr_t) &eatrate, 0,
328     "Food eating rate for mature people" },     
329 { "fcrate",             doubleset, (caddr_t) &fcrate, 0,
330     "Food cultivation rate (* workforce in sector)" },  
331 { "fgrate",             doubleset, (caddr_t) &fgrate, 0,
332     "Food growth rate (* fertility of sector)" },       
333 { "obrate",             doubleset, (caddr_t) &obrate, 0,
334     "Civilian birth rate" },    
335
336 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Nukes" },
337 { "decay_per_etu",      doubleset, (caddr_t) &decay_per_etu, 0,
338     "Decay of fallout per ETU" },
339 { "fallout_spread",     doubleset, (caddr_t) &fallout_spread, 0,
340     "Amount of fallout that leaks into surrounding sectors" },
341 { "drnuke_const",       floatset, (caddr_t) &drnuke_const, 0,
342     "Amount of research to tech needed to build a nuke (if DR_NUKE is on)" },
343
344 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Market/Trade" },
345 { "MARK_DELAY",         intset, (caddr_t) &MARK_DELAY, 0,
346     "Number of seconds commodities stay on the market for bidding" },
347 { "TRADE_DELAY",         intset, (caddr_t) &TRADE_DELAY, 0,
348     "Number of seconds ships, planes, and units stay on the market for bidding" },
349 { "maxmult",            doubleset, (caddr_t) &maxmult, 0,
350     "Maximum trade multiple (used for mult command)" },
351 { "minmult",            doubleset, (caddr_t) &minmult, 0,
352     "Minimum trade multiple (used for mult command)" },
353 { "buytax",             doubleset, (caddr_t) &buytax, 0,
354     "Tax (in percentage points) charged to the buyer on market purchases" },
355 { "tradetax",           doubleset, (caddr_t) &tradetax, 0,
356     "Amount of a trade transaction the seller makes (the rest is tax)" },
357
358 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Trade ships" },
359 { "trade_1_dist",       intset, (caddr_t) &trade_1_dist, 0,
360     "Less than this distance no money for cashing in" },
361 { "trade_2_dist",       intset, (caddr_t) &trade_2_dist, 0,
362     "Less than this distance gets trade_1 money for cashing in" },
363 { "trade_3_dist",       intset, (caddr_t) &trade_3_dist, 0,
364     "Less than this distance gets trade_2 money for cashing in (>= gets trade_3" },
365 { "trade_1",    floatset, (caddr_t) &trade_1, 0,
366     "Return per sector on trade_1 distance amount" },
367 { "trade_2",    floatset, (caddr_t) &trade_2, 0,
368     "Return per sector on trade_2 distance amount" },
369 { "trade_3",    floatset, (caddr_t) &trade_3, 0,
370     "Return per sector on trade_3 distance amount" },
371 { "trade_ally_bonus",   floatset, (caddr_t) &trade_ally_bonus, 0,
372     "Bonus you get for cashing in with an ally" },
373 { "trade_ally_cut",     floatset, (caddr_t) &trade_ally_cut, 0,
374     "Bonus your ally gets for you cashing in with them" },
375
376 { "", intset, (caddr_t)&emp_config_dummy, 0, "\n\n### Misc." },
377 { "fuel_mult",  intset, (caddr_t) &fuel_mult, 0,
378     "Multiplier for fuel to mobility calculation" },
379 { "lost_items_timeout", intset,    (caddr_t) &lost_items_timeout, 0,
380     "Seconds before a lost item is timed out of the database" },
381 { "last_demand_update", longset, (caddr_t) &last_demand_update, 0,
382     "When was the last demand update occured" },
383
384 { NULL, NULL, (caddr_t)0, 0, NULL }
385 };
386
387 static void fixup_files _PROTO((void));
388 static struct keymatch *keylookup _PROTO((s_char *key, struct keymatch tbl[]));
389
390
391 /*
392  * read in empire configuration
393  */
394 int
395 emp_config (char *file)
396 {
397         FILE *fp;
398         s_char  scanspace[1024];
399         s_char *av[65];
400         char buf[BUFSIZ];
401         struct keymatch *kp;
402
403         if (file == NULL || (fp = fopen (file, "r")) == NULL) {
404                 fixup_files ();
405                 return RET_OK;
406         }
407         while (fgets (buf, sizeof buf, fp) != NULL) {
408                 if (buf[0] == '#' || buf[0] == '\n')
409                         continue;
410                 if (parse (buf, av, 0, scanspace, 0) < 0) {
411                         fprintf (stderr, "Can't parse line %s", buf);
412                         continue;
413                 }
414                 if ((kp = keylookup (av[0], configkeys)) != NULL) {
415                         (*kp -> km_func) (kp, av + 1);
416                 }
417                 else {
418                         fprintf (stderr, "Unknown config key %s\n", av[0]);
419                 }
420         }
421         fclose (fp);
422         fixup_files ();
423
424         return RET_OK;
425 }
426
427 struct otherfiles {
428         s_char **files;
429         char *name;
430 };
431
432 extern s_char *upfil, *downfil, *disablefil, *banfil, *authfil;
433 extern s_char *commfil, *annfil, *telfil, *teldir, *timestampfil;
434
435 /* list of other well known files... -maybe tailor these oneday
436  * anyway - meantime they are all relative to datadir */
437 static struct otherfiles ofiles[] = {
438 { &upfil,       "up" },
439 { &downfil,     "down"},
440 { &disablefil,  "disable"},
441 { &banfil,      "ban" },
442 { &authfil,     "auth" },
443 { &commfil,     "comm" },
444 { &annfil,      "ann" },
445 { &timestampfil, "timestamp" },
446 { &teldir,      "tel" },
447 #if !defined(_WIN32)
448 { &telfil,      "tel/tel" },
449 #else
450 { &telfil,      "tel\\tel" },
451 #endif
452 { NULL,         NULL }
453 };
454
455 /* fix up the empfile struct to reference full path names */
456 static void fixup_files (void)
457 {
458         struct empfile *ep;
459         struct otherfiles *op;
460         s_char buf[1024];
461
462         for (ep = empfile; ep < &empfile[EF_MAX]; ep ++) {
463 #if !defined(_WIN32)
464                 sprintf (buf, "%s/%s", datadir, ep->name);
465 #else
466                 sprintf (buf, "%s\\%s", datadir, ep->name);
467 #endif
468                 ep->file = strdup (buf);
469         }
470
471         for (op = ofiles; op -> files; op ++) {
472 #if !defined(_WIN32)
473                 sprintf (buf, "%s/%s", datadir, op -> name);
474 #else
475                 sprintf (buf, "%s\\%s", datadir, op -> name);
476 #endif
477                 *op -> files = strdup (buf);
478         }
479 }
480
481 /* find the key in the table */
482 static struct keymatch *keylookup (register s_char *command, struct keymatch *tbl)
483 {
484         register struct keymatch *kp;
485
486         if (command == 0 || *command == 0)
487                 return 0;
488         for (kp = tbl; kp->km_key != 0; kp++) {
489                 if (strcmp (kp -> km_key, command) == 0)
490                         return kp;
491         }
492         return NULL;
493 }
494
495 /* worldx int setting function */
496 void worldxset (struct keymatch *kp, s_char **av)
497 {
498         int *intptr = (int *)kp -> km_data;
499
500         if (*av == NULL || intptr == NULL)
501                 return;
502         *intptr = atoi (*av);
503         if (!((*intptr % 2) == 0)) {
504           /* Must be div / 2, so subtract one */
505           *intptr = *intptr - 1;
506         }
507 }
508
509 /* generic int setting function */
510 void intset (struct keymatch *kp, s_char **av)
511 {
512         int *intptr = (int *)kp -> km_data;
513
514         if (*av == NULL || intptr == NULL)
515                 return;
516         *intptr = atoi (*av);
517 }
518
519 /* generic float set function */
520 void floatset (struct keymatch *kp, s_char **av)
521 {
522         float *floatptr = (float *)kp -> km_data;
523
524         if (*av == NULL || floatptr == NULL)
525                 return;
526         *floatptr = atof (*av);
527 }
528
529
530
531 /* generic string set function */
532 void optstrset (struct keymatch *kp, s_char **av)
533 {
534         s_char **confstrp = (s_char **)kp -> km_data;
535
536         if (*av == NULL || confstrp == NULL)
537                 return;
538         if (kp -> km_flags & KM_ALLOC)
539                 free (*confstrp);
540         *confstrp = strdup (*av);
541         kp -> km_flags |= KM_ALLOC;
542 }
543
544 /* generic double set function */
545 void doubleset (struct keymatch *kp, s_char **av)
546 {
547         double *doublep = (double *)kp -> km_data;
548
549         if (*av == NULL || doublep == NULL)
550                 return;
551         *doublep = atof (*av);
552 }
553
554 /* generic long set function */
555 void longset (struct keymatch *kp, s_char **av)
556 {
557         long int *longp = (long int *)kp -> km_data;
558
559         if (*av == NULL || longp == NULL)
560                 return;
561         *longp = atol (*av);
562 }
563
564 void
565 print_config (FILE *fp)
566 {
567         struct empfile *ep;
568         struct otherfiles *op;
569         struct keymatch *kp;
570
571         fprintf(fp, "# Empire Configuration File:\n");
572         for (kp = configkeys; kp -> km_key; kp ++) {
573           /* We print a few special things here */
574                 if (kp->km_comment) {
575                   if (kp->km_comment[0]) {
576                     if (kp->km_comment[0] != '\n')
577                       fprintf(fp, "\n# ");
578                     fprintf(fp, "%s\n", kp->km_comment);
579                   }
580                 }
581                 if (!kp->km_key[0])
582                   continue;
583                 if (kp -> km_func == optstrset) {
584                         fprintf (fp, "%s \"%s\"\n", kp -> km_key,
585                                  *(s_char **)kp -> km_data);
586                 }
587                 else if (kp -> km_func == intset) {
588                         fprintf (fp, "%s %d\n", kp -> km_key,
589                                  *(int *)kp -> km_data);
590                 }
591                 else if (kp -> km_func == worldxset) {
592                         fprintf (fp, "%s %d\n", kp -> km_key,
593                                  *(int *)kp -> km_data);
594                 }
595                 else if (kp -> km_func == floatset) {
596                         fprintf (fp, "%s %g\n", kp -> km_key,
597                                  *(float *)kp -> km_data);
598                 }
599                 else if (kp -> km_func == doubleset) {
600                         fprintf (fp, "%s %g\n", kp -> km_key,
601                                  *(double *)kp -> km_data);
602                 }
603                 else if (kp -> km_func == longset) {
604                         fprintf (fp, "%s %ld\n", kp -> km_key,
605                                  *(long *)kp -> km_data);
606                 }
607                 else if (kp -> km_func == optionset) {
608                         struct option_list *op;
609
610                         for (op = Options; op -> opt_key; op++) {
611                                 if (*op -> opt_valuep)
612                                         fprintf (fp, "%s %s\n", kp -> km_key,
613                                                  op -> opt_key);
614                         }
615                 }
616                 else if (kp -> km_func == optiondel) {
617                         struct option_list *op;
618
619                         for (op = Options; op -> opt_key; op++) {
620                                 if (*op -> opt_valuep == 0)
621                                         fprintf (fp, "%s %s\n", kp -> km_key,
622                                                  op -> opt_key);
623                         }
624                 }
625                 else fprintf (fp, "# Unknown format %s\n", kp -> km_key);
626         }
627
628         fprintf(fp, "\n");
629         for (ep = empfile; ep < &empfile[EF_MAX]; ep ++) 
630                 fprintf (fp, "# File %s -> %s\n", ep -> name, ep -> file);
631         for (op = ofiles; op -> files; op ++)
632                 fprintf (fp, "# File %s -> %s\n", op -> name, *(op -> files));
633
634 }
635
636     
637 /* add an option to the list */
638 void set_option (const char *s)
639 {
640         struct option_list *op;
641
642         for (op = Options; op -> opt_key; op++) {
643                 if (strcmp (op -> opt_key, s) == 0)
644                         *op -> opt_valuep = 1;
645         }
646 }
647
648 /* delete an option from the list */
649 void delete_option (const char *s)
650 {
651         struct option_list *op;
652
653         for (op = Options; op -> opt_key; op++) {
654                 if (strcmp (op -> opt_key, s) == 0)
655                         *op -> opt_valuep = 0;
656         }
657 }
658
659 /* config interface */
660 void optionset (struct keymatch *kp, s_char **av)
661                                 /* unused - we have a well known global */
662             
663 {
664     char **cpp;
665
666     for (cpp = (char **)av; *cpp; cpp++)
667         set_option (*cpp);
668 }
669
670 /* config interface */
671 void optiondel (struct keymatch *kp, s_char **av)
672                                 /* unused - we have a well known global */
673             
674 {
675     char **cpp;
676
677     for (cpp = (char **)av; *cpp; cpp++)
678         delete_option (*cpp);
679 }