Debian Codegraph @

common/init.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Basic initialization for the common library.
17 */
18
19 #define EXTERN
20 #define INIT_C
21
22 #include "global.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "object.h"
28 #include "output_file.h"
29
30 static void init_environ(void);
31 static void init_defaults(void);
32 static void init_dynamic(void);
33 static void init_clocks(void);
34 static void init_attackmess(void);
35
36 /*
37 * Anything with non-zero defaults in include/global.h must be set here.
38 */
39 struct Settings settings = {
40 .csport = CSPORT,
41 .debug = llevInfo,
42 .confdir = CONFDIR,
43 .datadir = DATADIR,
44 .localdir = LOCALDIR,
45 .playerdir = PLAYERDIR,
46 .mapdir = MAPDIR,
47 .archetypes = ARCHETYPES,
48 .regions = REGIONS,
49 .treasures = TREASURES,
50 .uniquedir = UNIQUE_DIR,
51 .templatedir = TEMPLATE_DIR,
52 .tmpdir = TMPDIR,
53 .stat_loss_on_death = STAT_LOSS_ON_DEATH,
54 .pk_luck_penalty = PK_LUCK_PENALTY,
55 .permanent_exp_ratio = PERMANENT_EXPERIENCE_RATIO,
56 .death_penalty_ratio = DEATH_PENALTY_RATIO,
57 .death_penalty_level = DEATH_PENALTY_LEVEL,
58 .balanced_stat_loss = BALANCED_STAT_LOSS,
59 .not_permadeth = NOT_PERMADETH,
60 .simple_exp = SIMPLE_EXP,
61 .reset_loc_time = RESET_LOCATION_TIME,
62 .set_title = SET_TITLE,
63 .resurrection = RESURRECTION,
64 .search_items = SEARCH_ITEMS,
65 .spell_encumbrance = SPELL_ENCUMBRANCE,
66 .spell_failure_effects = SPELL_FAILURE_EFFECTS,
67 .casting_time = CASTING_TIME,
68 .real_wiz = REAL_WIZ,
69 .recycle_tmp_maps = RECYCLE_TMP_MAPS,
70 .always_show_hp = ALWAYS_SHOW_HP,
71 .spellpoint_level_depend = SPELLPOINT_LEVEL_DEPEND,
72 .set_friendly_fire = SET_FRIENDLY_FIRE,
73 .motd = MOTD,
74 .rules = "rules",
75 .news = "news",
76 .emergency_x = EMERGENCY_X,
77 .emergency_y = EMERGENCY_Y,
78 .item_power_factor = 1.0,
79 /* Armor enchantment stuff */
80 .armor_max_enchant = ARMOR_MAX_ENCHANT,
81 .armor_weight_reduction = ARMOR_WEIGHT_REDUCTION,
82 .armor_weight_linear = ARMOR_WEIGHT_LINEAR,
83 .armor_speed_improvement = ARMOR_SPEED_IMPROVEMENT,
84 .armor_speed_linear = ARMOR_SPEED_LINEAR,
85 .no_player_stealing = 1,
86 .create_home_portals = 0,
87 .personalized_blessings = 1,
88 .pk_max_experience = 5000000,
89 .pk_max_experience_percent = 10,
90 .starting_stat_min = 3,
91 .starting_stat_max = 18,
92 .starting_stat_points = 85,
93 .roll_stat_points = 115,
94 .max_stat = 0, /* max_stat - will be loaded from stats file */
95 .special_break_map = 1, /* special_break_map, 1 for historical reasons */
96 };
97
98 struct Statistics statistics;
99
100 /**
101 * Perhaps not the best place for this, but needs to be
102 * in some file in the common area so that standalone
103 * programs, like the random map generator, can be built.
104 */
105 const char *const spellpathnames[NRSPELLPATHS] = {
106 "Protection",
107 "Fire",
108 "Frost",
109 "Electricity",
110 "Missiles",
111 "Self",
112 "Summoning",
113 "Abjuration",
114 "Restoration",
115 "Detonation",
116 "Mind",
117 "Creation",
118 "Teleportation",
119 "Information",
120 "Transmutation",
121 "Transferrence",
122 "Turning",
123 "Wounding",
124 "Death",
125 "Light"
126 };
127
128
129 /**
130 * This loads the emergency map information from a
131 * .emergency file in the map directory. Doing this makes
132 * it easier to switch between map distributions (don't need
133 * to recompile. Note that there is no reason I see that
134 * this could not be re-loaded during play, but it seems
135 * like there should be little reason to do that.
136 *
137 * @note
138 * If file doesn't exist, will not do anything.
139 */
140 static void init_emergency_mappath(void) {
141 char filename[MAX_BUF], tmpbuf[MAX_BUF];
142 FILE *fp;
143 int online = 0;
144
145 settings.emergency_mapname = strdup_local(EMERGENCY_MAPPATH);
146
147 /* If this file doesn't exist, not a big deal */
148 snprintf(filename, sizeof(filename), "%s/%s/.emergency", settings.datadir, settings.mapdir);
149 fp = fopen(filename, "r");
150 if (fp != NULL) {
151 while (fgets(tmpbuf, MAX_BUF-1, fp)) {
152 if (tmpbuf[0] == '#')
153 continue; /* ignore comments */
154
155 if (online == 0) {
156 tmpbuf[strlen(tmpbuf)-1] = 0; /* kill newline */
157 free(settings.emergency_mapname);
158 settings.emergency_mapname = strdup_local(tmpbuf);
159 } else if (online == 1) {
160 settings.emergency_x = atoi(tmpbuf);
161 } else if (online == 2) {
162 settings.emergency_y = atoi(tmpbuf);
163 }
164 online++;
165 if (online > 2)
166 break;
167 }
168 fclose(fp);
169 if (online <= 2)
170 LOG(llevError, "Online read partial data from %s\n", filename);
171 LOG(llevDebug, "emergency map set to %s (%d, %d)\n",
172 settings.emergency_mapname,
173 settings.emergency_x, settings.emergency_y);
174 }
175 }
176
177
178 /**
179 * It is vital that init_library() is called by any functions
180 * using this library.
181 * If you want to lessen the size of the program using the library,
182 * you can replace the call to init_library() with init_globals() and
183 * init_function_pointers(). Good idea to also call
184 * init_hash_table if you are doing any object loading.
185 */
186 void init_library(void) {
187 init_environ();
188 init_globals();
189 init_stats(FALSE); /* Needs to be fairly early, since the loader will check
190 * against the settings.max_stat value
191 */
192 init_hash_table();
193 i18n_init();
194 init_objects();
195 init_block();
196 read_bmap_names();
197 read_smooth();
198 init_anim(); /* Must be after we read in the bitmaps */
199 init_archetypes(); /* Reads all archetypes from file */
200 init_attackmess();
201 init_clocks();
202 init_emergency_mappath();
203 init_experience();
204
205 /* init_dynamic() loads a map, so needs a region */
206 if (init_regions() != 0) {
207 LOG(llevError, "Please check that your maps are correctly installed.\n");
208 exit(EXIT_FAILURE);
209 }
210
211 init_dynamic();
212 }
213
214 /**
215 * Initializes values from the environmental variables.
216 * it needs to be called very early, since command line options should
217 * overwrite these if specified.
218 */
219 static void init_environ(void) {
220 char *cp;
221
222 cp = getenv("CROSSFIRE_LIBDIR");
223 if (cp)
224 settings.datadir = cp;
225 cp = getenv("CROSSFIRE_LOCALDIR");
226 if (cp)
227 settings.localdir = cp;
228 cp = getenv("CROSSFIRE_PLAYERDIR");
229 if (cp)
230 settings.playerdir = cp;
231 cp = getenv("CROSSFIRE_MAPDIR");
232 if (cp)
233 settings.mapdir = cp;
234 cp = getenv("CROSSFIRE_ARCHETYPES");
235 if (cp)
236 settings.archetypes = cp;
237 cp = getenv("CROSSFIRE_TREASURES");
238 if (cp)
239 settings.treasures = cp;
240 cp = getenv("CROSSFIRE_UNIQUEDIR");
241 if (cp)
242 settings.uniquedir = cp;
243 cp = getenv("CROSSFIRE_TEMPLATEDIR");
244 if (cp)
245 settings.templatedir = cp;
246 cp = getenv("CROSSFIRE_TMPDIR");
247 if (cp)
248 settings.tmpdir = cp;
249 }
250
251 /**
252 * Initialises all global variables.
253 * Might use environment-variables as default for some of them.
254 *
255 * Setups logfile, and such variables.
256 */
257 void init_globals(void) {
258 memset(&statistics, 0, sizeof(struct Statistics));
259
260 /* Log to stderr by default. */
261 logfile = stderr;
262
263 /* Try to open the log file specified on the command-line. */
264 if (settings.logfilename != NULL) {
265 logfile = fopen(settings.logfilename, "a");
266
267 /* If writable, set buffer mode to per-line. */
268 if (logfile != NULL) {
269 setvbuf(logfile, NULL, _IOLBF, 0);
270 } else {
271 logfile = stderr;
272
273 LOG(llevError, "Could not open '%s' for logging.\n",
274 settings.logfilename);
275 }
276 }
277
278 exiting = 0;
279 first_player = NULL;
280 first_friendly_object = NULL;
281 first_map = NULL;
282 first_treasurelist = NULL;
283 first_artifactlist = NULL;
284 first_archetype = NULL;
285 *first_map_ext_path = 0;
286 nroftreasures = 0;
287 nrofartifacts = 0;
288 nrofallowedstr = 0;
289 ring_arch = NULL;
290 amulet_arch = NULL;
291 undead_name = add_string("undead");
292 trying_emergency_save = 0;
293 num_animations = 0;
294 animations = NULL;
295 init_defaults();
296 }
297
298 /**
299 * Cleans all memory allocated for global variables.
300 *
301 * Will clear:
302 * * attack messages
303 * * emergency map settings
304 * * friendly list
305 * * experience
306 * * regions
307 */
308 void free_globals(void) {
309 int msg, attack;
310 objectlink *friend;
311 region *reg;
312
313 FREE_AND_CLEAR_STR(undead_name);
314 for (msg = 0; msg < NROFATTACKMESS; msg++)
315 for (attack = 0; attack < MAXATTACKMESS; attack++) {
316 free(attack_mess[msg][attack].buf1);
317 free(attack_mess[msg][attack].buf2);
318 free(attack_mess[msg][attack].buf3);
319 }
320
321 free(settings.emergency_mapname);
322
323 while (first_friendly_object) {
324 friend = first_friendly_object->next;
325 FREE_AND_CLEAR(first_friendly_object);
326 first_friendly_object = friend;
327 }
328
329 free_experience();
330
331 while (first_region) {
332 reg = first_region->next;
333 FREE_AND_CLEAR(first_region->name);
334 FREE_AND_CLEAR(first_region->parent_name);
335 FREE_AND_CLEAR(first_region->jailmap);
336 FREE_AND_CLEAR(first_region->msg);
337 FREE_AND_CLEAR(first_region->longname);
338 FREE_AND_CLEAR(first_region);
339 first_region = reg;
340 }
341 }
342
343 /**
344 * Initialises global variables which can be changed by options.
345 * Called by init_library().
346 */
347 static void init_defaults(void) {
348 nroferrors = 0;
349 }
350
351 /**
352 * Initializes first_map_path from the archetype collection.
353 *
354 * Must be called after archetypes have been initialized.
355 *
356 * @note
357 * will call exit() if no MAP archetype was found.
358 */
359 static void init_dynamic(void) {
360 archetype *at = first_archetype;
361 while (at) {
362 if (at->clone.type == MAP && at->clone.subtype == MAP_TYPE_LEGACY) {
363 if (at->clone.race) {
364 safe_strncpy(first_map_ext_path, at->clone.race,
365 sizeof(first_map_ext_path));
366 }
367 if (EXIT_PATH(&at->clone)) {
368 mapstruct *first;
369
370 strlcpy(first_map_path, EXIT_PATH(&at->clone), sizeof(first_map_path));
371 first = ready_map_name(first_map_path, 0);
372 if (!first) {
373 LOG(llevError, "Initial map %s can't be found! Please ensure maps are correctly installed.\n", first_map_path);
374 LOG(llevError, "Unable to continue without initial map.\n");
375 abort();
376 }
377 delete_map(first);
378 return;
379 }
380 }
381 at = at->next;
382 }
383 LOG(llevError, "You need a archetype called 'map' and it have to contain start map\n");
384 exit(-1);
385 }
386
387 /** Ingame time */
388 unsigned long todtick;
389
390 /**
391 * Write out the current time to the file so time does not
392 * reset every time the server reboots.
393 */
394 void write_todclock(void) {
395 char filename[MAX_BUF];
396 FILE *fp;
397 OutputFile of;
398
399 snprintf(filename, sizeof(filename), "%s/clockdata", settings.localdir);
400 fp = of_open(&of, filename);
401 if (fp == NULL)
402 return;
403 fprintf(fp, "%lu", todtick);
404 of_close(&of);
405 }
406
407 /**
408 * Initializes the gametime and TOD counters
409 * Called by init_library().
410 */
411 static void init_clocks(void) {
412 char filename[MAX_BUF];
413 FILE *fp;
414 static int has_been_done = 0;
415
416 if (has_been_done)
417 return;
418 else
419 has_been_done = 1;
420
421 snprintf(filename, sizeof(filename), "%s/clockdata", settings.localdir);
422 fp = fopen(filename, "r");
423 if (fp == NULL) {
424 LOG(llevError, "Can't open %s.\n", filename);
425 todtick = 0;
426 write_todclock();
427 return;
428 }
429 /* Read TOD and default to 0 on failure. */
430 if (fscanf(fp, "%lu", &todtick) == 1) {
431 LOG(llevDebug, "clockdata: todtick is %lu\n", todtick);
432 fclose(fp);
433 } else {
434 LOG(llevError, "Couldn't parse todtick, using default value 0\n");
435 todtick = 0;
436 fclose(fp);
437 write_todclock();
438 }
439 }
440
441 /**
442 * Initializes the attack messages.
443 * Called by init_library().
444 *
445 * Memory will be cleared by free_globals().
446 */
447 static void init_attackmess(void) {
448 char buf[MAX_BUF];
449 char filename[MAX_BUF];
450 char *cp, *p;
451 FILE *fp;
452 static int has_been_done = 0;
453 int mess = -1, level;
454 int mode = 0, total = 0;
455
456 if (has_been_done)
457 return;
458 else
459 has_been_done = 1;
460
461 snprintf(filename, sizeof(filename), "%s/attackmess", settings.datadir);
462 fp = fopen(filename, "r");
463 if (fp == NULL) {
464 LOG(llevError, "Can't open %s.\n", filename);
465 return;
466 }
467
468 level = 0;
469 while (fgets(buf, MAX_BUF, fp) != NULL) {
470 if (*buf == '#')
471 continue;
472 // Find the end of the line and strip the newline
473 cp = strchr(buf, '\n');
474 if (cp != NULL)
475 *cp = '\0';
476 /*
477 * Skip blanks -- strspn is slightly faster than a loop w/ optimization on
478 * Also, note we go from the beginning of the line again, since cp was at the end.
479 * While here, also skip tabs for more complete whitespace handling.
480 *
481 * SilverNexus 2018-01-21
482 */
483 cp = buf + strspn(buf, " \t");
484
485 if (strncmp(cp, "TYPE:", 5) == 0) {
486 p = strtok(buf, ":");
487 p = strtok(NULL, ":");
488 if (mode == 1) {
489 attack_mess[mess][level].level = -1;
490 attack_mess[mess][level].buf1 = NULL;
491 attack_mess[mess][level].buf2 = NULL;
492 attack_mess[mess][level].buf3 = NULL;
493 }
494 level = 0;
495 mess = atoi(p);
496 mode = 1;
497 continue;
498 }
499 if (mode == 1) {
500 p = strtok(buf, "=");
501 attack_mess[mess][level].level = atoi(buf);
502 p = strtok(NULL, "=");
503 if (p != NULL)
504 attack_mess[mess][level].buf1 = strdup_local(p);
505 else
506 attack_mess[mess][level].buf1 = strdup_local("");
507 mode = 2;
508 continue;
509 } else if (mode == 2) {
510 p = strtok(buf, "=");
511 attack_mess[mess][level].level = atoi(buf);
512 p = strtok(NULL, "=");
513 if (p != NULL)
514 attack_mess[mess][level].buf2 = strdup_local(p);
515 else
516 attack_mess[mess][level].buf2 = strdup_local("");
517 mode = 3;
518 continue;
519 } else if (mode == 3) {
520 p = strtok(buf, "=");
521 attack_mess[mess][level].level = atoi(buf);
522 p = strtok(NULL, "=");
523 if (p != NULL)
524 attack_mess[mess][level].buf3 = strdup_local(p);
525 else
526 attack_mess[mess][level].buf3 = strdup_local("");
527 mode = 1;
528 level++;
529 total++;
530 continue;
531 }
532 }
533 LOG(llevDebug, "attackmsg: %d messages in %d categories\n", total, mess+1);
534 fclose(fp);
535 }