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