Crossfire Server, Trunk  R21017
init.c
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
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  * You unforunately need to looking in include/global.h to see what these
38  * correspond to.
39  */
40 struct Settings settings = {
41  NULL, /* Logfile */
42  CSPORT, /* Client/server port */
43 
44  llevInfo, /* Debug level */
45  0, NULL, /* dumpvalues, dumparg */
46  CONFDIR,
47  DATADIR,
48  LOCALDIR,
51  TMPDIR,
59  SIMPLE_EXP,
61  SET_TITLE,
67  REAL_WIZ,
72  "", /* Who format specifier */
73  "", /* who wiz format specifier */
74  MOTD,
75  "rules",
76  "news",
77  "", /* DM_MAIL */
78  0, /* This and the next 3 values are metaserver values */
79  "",
80  "",
81  0,
82  "",
83  0, 0, 0, 0, 0, 0, 0, /* worldmap settings*/
84  NULL, EMERGENCY_X, EMERGENCY_Y,
85  0,
86  1.0,
87  /* Armor enchantment stuff */
93  1, /* no_player_stealing */
94  0, /* create_home_portals */
95  1, /* personalized_blessings */
96  5000000, /* pk_max_experience */
97  10, /* pk_max_experience_percent */
98  0, /* allow_denied_spells_writing */
99  0, /* allow_broken_converters */
100  0, /* log_timestamp */
101  NULL, /* log_timestamp_format */
102  3, /* starting_stat_min */
103  18, /* starting_stat_max */
104  85, /* starting_stat_points */
105  115, /* roll_stat_points */
106  0, /* max_stat - will be loaded from stats file */
107  1, /* special_break_map, 1 for historical reasons */
108  NULL, /* disabled_plugins */
109  0, /* ignore_plugin_compatibility */
110  FALSE, /* account_block_create */
111  NULL, /* Trusted host for account creation. */
112  0, /* Unless explicitly set, use legacy crypt mode */
113 };
114 
116 
122 const char *const spellpathnames[NRSPELLPATHS] = {
123  "Protection",
124  "Fire",
125  "Frost",
126  "Electricity",
127  "Missiles",
128  "Self",
129  "Summoning",
130  "Abjuration",
131  "Restoration",
132  "Detonation",
133  "Mind",
134  "Creation",
135  "Teleportation",
136  "Information",
137  "Transmutation",
138  "Transferrence",
139  "Turning",
140  "Wounding",
141  "Death",
142  "Light"
143 };
144 
145 
157 static void init_emergency_mappath(void) {
158  char filename[MAX_BUF], tmpbuf[MAX_BUF];
159  FILE *fp;
160  int online = 0;
161 
163 
164  /* If this file doesn't exist, not a big deal */
165  snprintf(filename, sizeof(filename), "%s/%s/.emergency", settings.datadir, settings.mapdir);
166  fp = fopen(filename, "r");
167  if (fp != NULL) {
168  while (fgets(tmpbuf, MAX_BUF-1, fp)) {
169  if (tmpbuf[0] == '#')
170  continue; /* ignore comments */
171 
172  if (online == 0) {
173  tmpbuf[strlen(tmpbuf)-1] = 0; /* kill newline */
174  free(settings.emergency_mapname);
175  settings.emergency_mapname = strdup_local(tmpbuf);
176  } else if (online == 1) {
177  settings.emergency_x = atoi(tmpbuf);
178  } else if (online == 2) {
179  settings.emergency_y = atoi(tmpbuf);
180  }
181  online++;
182  if (online > 2)
183  break;
184  }
185  fclose(fp);
186  if (online <= 2)
187  LOG(llevError, "Online read partial data from %s\n", filename);
188  LOG(llevDebug, "emergency map set to %s (%d, %d)\n",
189  settings.emergency_mapname,
190  settings.emergency_x, settings.emergency_y);
191  }
192 }
193 
194 
203 void init_library(void) {
204  init_environ();
205  init_globals();
206  init_stats(FALSE); /* Needs to be fairly early, since the loader will check
207  * against the settings.max_stat value
208  */
209  init_hash_table();
210  i18n_init();
211  init_objects();
212  init_block();
213  read_bmap_names();
214  read_smooth();
215  init_anim(); /* Must be after we read in the bitmaps */
216  init_archetypes(); /* Reads all archetypes from file */
217  init_attackmess();
218  init_clocks();
220  init_experience();
221 
222  /* init_dynamic() loads a map, so needs a region */
223  if (init_regions() != 0) {
224  LOG(llevError, "Please check that your maps are correctly installed.\n");
225  exit(EXIT_FAILURE);
226  }
227 
228  init_dynamic();
229 }
230 
236 static void init_environ(void) {
237  char *cp;
238 
239  cp = getenv("CROSSFIRE_LIBDIR");
240  if (cp)
241  settings.datadir = cp;
242  cp = getenv("CROSSFIRE_LOCALDIR");
243  if (cp)
244  settings.localdir = cp;
245  cp = getenv("CROSSFIRE_PLAYERDIR");
246  if (cp)
247  settings.playerdir = cp;
248  cp = getenv("CROSSFIRE_MAPDIR");
249  if (cp)
250  settings.mapdir = cp;
251  cp = getenv("CROSSFIRE_ARCHETYPES");
252  if (cp)
253  settings.archetypes = cp;
254  cp = getenv("CROSSFIRE_TREASURES");
255  if (cp)
256  settings.treasures = cp;
257  cp = getenv("CROSSFIRE_UNIQUEDIR");
258  if (cp)
259  settings.uniquedir = cp;
260  cp = getenv("CROSSFIRE_TEMPLATEDIR");
261  if (cp)
262  settings.templatedir = cp;
263  cp = getenv("CROSSFIRE_TMPDIR");
264  if (cp)
265  settings.tmpdir = cp;
266 }
267 
274 void init_globals(void) {
275  memset(&statistics, 0, sizeof(struct Statistics));
276 
277  /* Log to stderr by default. */
278  logfile = stderr;
279 
280  /* Try to open the log file specified on the command-line. */
281  if (settings.logfilename != NULL) {
282  logfile = fopen(settings.logfilename, "a");
283 
284  /* If writable, set buffer mode to per-line. */
285  if (logfile != NULL) {
286  setvbuf(logfile, NULL, _IOLBF, 0);
287  } else {
288  logfile = stderr;
289 
290  LOG(llevError, "Could not open '%s' for logging.\n",
291  settings.logfilename);
292  }
293  }
294 
295  exiting = 0;
296  first_player = NULL;
297  first_friendly_object = NULL;
298  first_map = NULL;
299  first_treasurelist = NULL;
300  first_artifactlist = NULL;
301  first_archetype = NULL;
302  *first_map_ext_path = 0;
303  nroftreasures = 0;
304  nrofartifacts = 0;
305  nrofallowedstr = 0;
306  ring_arch = NULL;
307  amulet_arch = NULL;
308  staff_arch = NULL;
309  undead_name = add_string("undead");
311  num_animations = 0;
312  animations = NULL;
314  init_defaults();
315 }
316 
327 void free_globals(void) {
328  int msg, attack;
329  objectlink *friend;
330  region *reg;
331 
333  for (msg = 0; msg < NROFATTACKMESS; msg++)
334  for (attack = 0; attack < MAXATTACKMESS; attack++) {
335  free(attack_mess[msg][attack].buf1);
336  free(attack_mess[msg][attack].buf2);
337  free(attack_mess[msg][attack].buf3);
338  }
339 
340  free(settings.emergency_mapname);
341 
342  while (first_friendly_object) {
343  friend = first_friendly_object->next;
345  first_friendly_object = friend;
346  }
347 
348  free_experience();
349 
350  while (first_region) {
351  reg = first_region->next;
358  first_region = reg;
359  }
360 }
361 
366 static void init_defaults(void) {
367  nroferrors = 0;
368 }
369 
378 static void init_dynamic(void) {
380  while (at) {
381  if (at->clone.type == MAP && at->clone.subtype == MAP_TYPE_LEGACY) {
382  if (at->clone.race) {
384  sizeof(first_map_ext_path));
385  }
386  if (EXIT_PATH(&at->clone)) {
387  mapstruct *first;
388 
389  snprintf(first_map_path, sizeof(first_map_path), "%s", EXIT_PATH(&at->clone));
390  first = ready_map_name(first_map_path, 0);
391  if (!first) {
392  LOG(llevError, "Initial map %s can't be found! Please ensure maps are correctly installed.\n", first_map_path);
393  LOG(llevError, "Unable to continue without initial map.\n");
394  abort();
395  }
396  delete_map(first);
397  return;
398  }
399  }
400  at = at->next;
401  }
402  LOG(llevError, "You need a archetype called 'map' and it have to contain start map\n");
403  exit(-1);
404 }
405 
407 unsigned long todtick;
408 
413 void write_todclock(void) {
414  char filename[MAX_BUF];
415  FILE *fp;
416  OutputFile of;
417 
418  snprintf(filename, sizeof(filename), "%s/clockdata", settings.localdir);
419  fp = of_open(&of, filename);
420  if (fp == NULL)
421  return;
422  fprintf(fp, "%lu", todtick);
423  of_close(&of);
424 }
425 
430 static void init_clocks(void) {
431  char filename[MAX_BUF];
432  FILE *fp;
433  static int has_been_done = 0;
434 
435  if (has_been_done)
436  return;
437  else
438  has_been_done = 1;
439 
440  snprintf(filename, sizeof(filename), "%s/clockdata", settings.localdir);
441  fp = fopen(filename, "r");
442  if (fp == NULL) {
443  LOG(llevError, "Can't open %s.\n", filename);
444  todtick = 0;
445  write_todclock();
446  return;
447  }
448  /* Read TOD and default to 0 on failure. */
449  if (fscanf(fp, "%lu", &todtick) == 1) {
450  LOG(llevDebug, "clockdata: todtick is %lu\n", todtick);
451  fclose(fp);
452  } else {
453  LOG(llevError, "Couldn't parse todtick, using default value 0\n");
454  todtick = 0;
455  fclose(fp);
456  write_todclock();
457  }
458 }
459 
466 static void init_attackmess(void) {
467  char buf[MAX_BUF];
468  char filename[MAX_BUF];
469  char *cp, *p;
470  FILE *fp;
471  static int has_been_done = 0;
472  int mess = -1, level;
473  int mode = 0, total = 0;
474 
475  if (has_been_done)
476  return;
477  else
478  has_been_done = 1;
479 
480  snprintf(filename, sizeof(filename), "%s/attackmess", settings.datadir);
481  fp = fopen(filename, "r");
482  if (fp == NULL) {
483  LOG(llevError, "Can't open %s.\n", filename);
484  return;
485  }
486 
487  level = 0;
488  while (fgets(buf, MAX_BUF, fp) != NULL) {
489  if (*buf == '#')
490  continue;
491  // Find the end of the line and strip the newline
492  cp = strchr(buf, '\n');
493  if (cp != NULL)
494  *cp = '\0';
495  /*
496  * Skip blanks -- strspn is slightly faster than a loop w/ optimization on
497  * Also, note we go from the beginning of the line again, since cp was at the end.
498  * While here, also skip tabs for more complete whitespace handling.
499  *
500  * SilverNexus 2018-01-21
501  */
502  cp = buf + strspn(buf, " \t");
503 
504  if (strncmp(cp, "TYPE:", 5) == 0) {
505  p = strtok(buf, ":");
506  p = strtok(NULL, ":");
507  if (mode == 1) {
508  attack_mess[mess][level].level = -1;
509  attack_mess[mess][level].buf1 = NULL;
510  attack_mess[mess][level].buf2 = NULL;
511  attack_mess[mess][level].buf3 = NULL;
512  }
513  level = 0;
514  mess = atoi(p);
515  mode = 1;
516  continue;
517  }
518  if (mode == 1) {
519  p = strtok(buf, "=");
520  attack_mess[mess][level].level = atoi(buf);
521  p = strtok(NULL, "=");
522  if (p != NULL)
523  attack_mess[mess][level].buf1 = strdup_local(p);
524  else
525  attack_mess[mess][level].buf1 = strdup_local("");
526  mode = 2;
527  continue;
528  } else if (mode == 2) {
529  p = strtok(buf, "=");
530  attack_mess[mess][level].level = atoi(buf);
531  p = strtok(NULL, "=");
532  if (p != NULL)
533  attack_mess[mess][level].buf2 = strdup_local(p);
534  else
535  attack_mess[mess][level].buf2 = strdup_local("");
536  mode = 3;
537  continue;
538  } else if (mode == 3) {
539  p = strtok(buf, "=");
540  attack_mess[mess][level].level = atoi(buf);
541  p = strtok(NULL, "=");
542  if (p != NULL)
543  attack_mess[mess][level].buf3 = strdup_local(p);
544  else
545  attack_mess[mess][level].buf3 = strdup_local("");
546  mode = 1;
547  level++;
548  total++;
549  continue;
550  }
551  }
552  LOG(llevDebug, "attackmsg: %d messages in %d categories\n", total, mess+1);
553  fclose(fp);
554 }
EXTERN FILE * logfile
Definition: global.h:142
#define RESURRECTION
Definition: config.h:161
#define STAT_LOSS_ON_DEATH
Definition: config.h:164
#define SPELL_FAILURE_EFFECTS
Definition: config.h:157
#define BALANCED_STAT_LOSS
Definition: config.h:149
#define SPELLPOINT_LEVEL_DEPEND
Definition: config.h:155
int init_regions(void)
Definition: region.c:292
void init_anim(void)
Definition: anim.c:44
Definition: object.h:125
struct Statistics statistics
Definition: init.c:115
Definition: object.h:442
const char * race
Definition: object.h:318
void init_block(void)
Definition: los.c:88
#define TEMPLATE_DIR
Definition: config.h:556
#define DEATH_PENALTY_RATIO
Definition: config.h:151
int level
Definition: attack.h:119
#define SEARCH_ITEMS
Definition: config.h:162
EXTERN int num_animations
Definition: global.h:164
#define strdup_local
Definition: compat.h:25
EXTERN objectlink * first_friendly_object
Definition: global.h:123
#define PERMANENT_EXPERIENCE_RATIO
Definition: config.h:150
#define ARMOR_SPEED_IMPROVEMENT
Definition: config.h:171
char * buf1
Definition: attack.h:120
#define PK_LUCK_PENALTY
Definition: config.h:165
void init_objects(void)
Definition: object.c:93
#define EMERGENCY_MAPPATH
Definition: config.h:546
mapstruct * ready_map_name(const char *name, int flags)
Definition: map.c:1815
const char * playerdir
Definition: global.h:244
object clone
Definition: object.h:470
void init_archetypes(void)
Definition: arch.c:182
#define TMPDIR
Definition: config.h:528
void init_globals(void)
Definition: init.c:274
#define ARMOR_SPEED_LINEAR
Definition: config.h:172
uint8_t subtype
Definition: object.h:339
#define NOT_PERMADETH
Definition: config.h:163
#define ARMOR_MAX_ENCHANT
Definition: config.h:168
void init_library(void)
Definition: init.c:203
static void init_attackmess(void)
Definition: init.c:466
void init_experience(void)
Definition: exp.c:167
#define FALSE
Definition: compat.h:11
#define MAP_TYPE_LEGACY
Definition: map.h:57
#define SPELL_ENCUMBRANCE
Definition: config.h:156
EXTERN char first_map_ext_path[MAX_BUF]
Definition: global.h:152
#define DEATH_PENALTY_LEVEL
Definition: config.h:152
#define safe_strncpy
Definition: compat.h:23
const char * treasures
Definition: global.h:248
Definition: object.h:465
const char * logfilename
Definition: global.h:236
const char *const spellpathnames[NRSPELLPATHS]
Definition: init.c:122
uint16_t emergency_y
Definition: global.h:297
EXTERN archetype * amulet_arch
Definition: global.h:160
char * name
Definition: map.h:278
void init_hash_table(void)
Definition: shstr.c:55
void i18n_init(void)
Definition: languages.c:168
#define ARMOR_WEIGHT_LINEAR
Definition: config.h:170
int read_smooth(void)
Definition: image.c:323
void write_todclock(void)
Definition: init.c:413
static void init_emergency_mappath(void)
Definition: init.c:157
static void init_defaults(void)
Definition: init.c:366
static void init_environ(void)
Definition: init.c:236
EXTERN const char * undead_name
Definition: global.h:161
const char * tmpdir
Definition: global.h:251
#define ALWAYS_SHOW_HP
Definition: config.h:160
#define EMERGENCY_Y
Definition: config.h:549
struct regiondef * next
Definition: map.h:277
Definition: map.h:276
#define EMERGENCY_X
Definition: config.h:548
#define snprintf
Definition: win32.h:46
#define MAXATTACKMESS
Definition: attack.h:19
#define RESET_LOCATION_TIME
Definition: config.h:673
int of_close(OutputFile *of)
Definition: output_file.c:61
#define SIMPLE_EXP
Definition: config.h:154
char * parent_name
Definition: map.h:279
#define EXIT_PATH(xyz)
Definition: define.h:455
EXTERN long nroferrors
Definition: global.h:134
uint16_t emergency_x
Definition: global.h:297
EXTERN Animations * animations
Definition: global.h:163
#define REGIONS
Definition: config.h:558
static void init_dynamic(void)
Definition: init.c:378
#define NROFATTACKMESS
Definition: attack.h:18
#define LOCALDIR
Definition: win32.h:103
#define PLAYERDIR
Definition: config.h:593
const char * templatedir
Definition: global.h:250
void free_experience(void)
Definition: exp.c:261
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:206
const char * archetypes
Definition: global.h:246
void free_globals(void)
Definition: init.c:327
EXTERN treasurelist * first_treasurelist
Definition: global.h:120
EXTERN artifactlist * first_artifactlist
Definition: global.h:121
#define MAX_BUF
Definition: define.h:35
#define NRSPELLPATHS
Definition: spells.h:40
#define ARCHETYPES
Definition: config.h:557
#define RECYCLE_TMP_MAPS
Definition: config.h:159
void read_bmap_names(void)
Definition: image.c:180
const char * uniquedir
Definition: global.h:249
#define CSPORT
Definition: config.h:363
EXTERN long nrofartifacts
Definition: global.h:146
const char * datadir
Definition: global.h:242
#define TREASURES
Definition: config.h:560
EXTERN long nrofallowedstr
Definition: global.h:147
EXTERN attackmess_t attack_mess[NROFATTACKMESS][MAXATTACKMESS]
Definition: attack.h:131
const char * localdir
Definition: global.h:243
#define DATADIR
Definition: win32.h:98
char * jailmap
Definition: map.h:297
struct oblnk * next
Definition: object.h:444
void init_stats(int reload)
Definition: living.c:2513
EXTERN archetype * staff_arch
Definition: global.h:160
const char * mapdir
Definition: global.h:245
EXTERN int exiting
Definition: global.h:144
uint8_t type
Definition: object.h:338
struct Settings settings
Definition: init.c:40
EXTERN long trying_emergency_save
Definition: global.h:133
FILE * of_open(OutputFile *of, const char *fname)
Definition: output_file.c:30
struct archt * next
Definition: object.h:467
EXTERN int animations_allocated
Definition: global.h:164
void delete_map(mapstruct *m)
Definition: map.c:1753
EXTERN char first_map_path[MAX_BUF]
Definition: global.h:151
#define MAPDIR
Definition: config.h:555
#define UNIQUE_DIR
Definition: config.h:536
sstring add_string(const char *str)
Definition: shstr.c:124
EXTERN player * first_player
Definition: global.h:117
#define ARMOR_WEIGHT_REDUCTION
Definition: config.h:169
#define MOTD
Definition: config.h:488
#define CASTING_TIME
Definition: config.h:166
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
char * msg
Definition: map.h:293
EXTERN archetype * ring_arch
Definition: global.h:160
EXTERN region * first_region
Definition: global.h:119
static void init_clocks(void)
Definition: init.c:430
char * buf3
Definition: attack.h:122
unsigned long todtick
Definition: init.c:407
char * emergency_mapname
Definition: global.h:296
Definition: map.h:325
char * buf2
Definition: attack.h:121
#define REAL_WIZ
Definition: config.h:158
#define FREE_AND_CLEAR(xyz)
Definition: global.h:201
#define SET_TITLE
Definition: config.h:153
#define SET_FRIENDLY_FIRE
Definition: config.h:167
EXTERN mapstruct * first_map
Definition: global.h:118
EXTERN archetype * first_archetype
Definition: global.h:122
char * longname
Definition: map.h:291
#define CONFDIR
Definition: win32.h:100
EXTERN long nroftreasures
Definition: global.h:145