Crossfire Server  1.75.0
main.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23 
24 #include "global.h"
25 #include "maze_gen.h"
26 #include "random_map.h"
27 #include "room_gen.h"
28 #include "rproto.h"
29 #include "sproto.h"
30 
31 #define LO_NEWFILE 2
32 
33 static void generate_map(char *OutFileName) {
34  RMParms rp;
35  mapstruct *newMap;
36 
37  fprintf(stderr, "Reading parameters from stdin...\n");
38 
39  /* Initialize parameters and set initial settings. */
40  memset(&rp, 0, sizeof(RMParms));
41  rp.Xsize = -1;
42  rp.Ysize = -1;
43 
44  /* Initialize Crossfire library. */
45  init_globals();
46  init_library();
47  init_readable();
48  init_gods();
49 
50  load_parameters(stdin, LO_NEWFILE, &rp);
51  fclose(stdin);
52 
53  newMap = generate_random_map(OutFileName, &rp, NULL, NULL);
54  save_map(newMap, SAVE_MODE_INPLACE);
55 }
56 
60 static void print_map(char **layout, int width, int height) {
61  int i, j;
62 
63  for (j = 0; j < height; ++j) {
64  for (i = 0; i < width; ++i) {
65  char display_char;
66  display_char = layout[i][j];
67 
68  switch (display_char) {
69  case 0:
70  display_char = '.';
71  break;
72  case 'D':
73  display_char = '+';
74  break;
75  }
76 
77  putchar(display_char);
78  }
79 
80  putchar('\n');
81  }
82 }
83 
84 struct layout {
85  const char *name;
86  char **(*func)(int, int, int, int);
87 };
88 
90  // Most of these need to be cast to silence warnings.
91  // The fourth paramter (and sometimes the third) is ignored in most cases.
92  // xsize,ysize,option,layers
93  { "rogue", &roguelike_layout_gen },
94  { "snake", &make_snake_layout },
95  { "sspiral", &make_square_spiral_layout },
96  { "spiral", &map_gen_spiral },
97  { "maze", &maze_gen },
98  { "onion", &map_gen_onion }
99 };
100 
113 static void test_layout(int width, int height, char **(*layout_func)(int, int, int, int)) {
114  char **layout;
115  SRANDOM(time(0));
116 
117  // Bail if no layout -- shouldn't occur, but just to be safe
118  if (layout_func == NULL)
119  return;
120 
121  layout = layout_func(width, height, 0, 0);
122 
123  print_map(layout, width, height);
124  free(layout);
125 }
126 
128 static void print_quickhelp(void) {
129  fprintf(stderr, "Type 'random_map -h' for usage instructions.\n");
130 }
131 
133 static void print_usage(void) {
134  printf(
135  "Usage: random_map [options]\n"
136  "\n"
137  "Options:\n"
138  " -h display this help message\n"
139  " -g <file> randomly generate the specified map file\n"
140  " -l <layout> layout to use. See Layouts for valid layouts.\n"
141  " (overridden by -g)\n"
142  " -t test map layout (overriden by -g)\n"
143  " -x <width> specify map width\n"
144  " -y <height> specify map height\n"
145  "\n"
146  "Layouts:\n"
147  " rogue -- roguelike map generator\n"
148  " snake -- snake map generator\n"
149  " sspiral -- square spiral map generator\n"
150  " spiral -- spiral map generator\n"
151  " maze -- maze map generator\n"
152  " onion -- onion map generator\n"
153  );
154 }
155 
156 int main(int argc, char *argv[]) {
157  int flag, mode = 0, width = 80, height = 23;
158  char *filename_out=NULL;
159  // Make default behavior be roguelike generation, like old behavior
160  // NOTE: The ugly function pointer cast silences compiler warnings.
161  char **(*func)(int, int, int, int) = (char **(*)(int, int, int, int))&roguelike_layout_gen;
162 
163  /* Parse command-line arguments. */
164  while ((flag = getopt(argc, argv, "g:hl:tx:y:")) != -1) {
165  switch (flag) {
166  case 'g':
167  mode = 2;
168  filename_out = optarg;
169  break;
170  case 'h':
171  print_usage();
172  exit(EXIT_SUCCESS);
173  break;
174  case 'l':
175  for (int i = 0; i < NROFLAYOUTS; ++i)
176  {
177  if (strcmp(optarg, layout_list[i].name) == 0)
178  func = layout_list[i].func;
179  }
180  break;
181  case 't':
182  mode = 1;
183  break;
184  case 'x':
185  width = atoi(optarg);
186  break;
187  case 'y':
188  height = atoi(optarg);
189  break;
190  case '?':
191  print_quickhelp();
192  exit(EXIT_FAILURE);
193  break;
194  }
195  }
196 
197  if (mode == 1) {
198  test_layout(width, height, func);
199  exit(EXIT_SUCCESS);
200  } else if (mode == 2) {
201  generate_map(filename_out);
202  exit(EXIT_SUCCESS);
203  } else {
204  print_quickhelp();
205  exit(EXIT_FAILURE);
206  }
207 }
208 
209 /* some plagarized code from apply.c--I needed just these two functions
210 without all the rest of the junk, so.... */
211 int apply_auto(object *op)
212 {
213  object *tmp = NULL;
214  int i;
215 
216  switch (op->type) {
217  case SHOP_FLOOR:
218  if (!HAS_RANDOM_ITEMS(op)) {
219  return 0;
220  }
221  do {
222  i = 10; /* let's give it 10 tries */
223  while ((tmp = generate_treasure(op->randomitems, op->stats.exp ? op->stats.exp : 5)) == NULL && --i)
224  ;
225  if (tmp == NULL) {
226  return 0;
227  }
228  if (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED)) {
230  tmp = NULL;
231  }
232  } while (!tmp);
233 
234  SET_FLAG(tmp, FLAG_UNPAID);
235  object_insert_in_map_at(tmp, op->map, NULL, 0, op->x, op->y);
237  tmp = identify(tmp);
238  break;
239 
240  case TREASURE:
241  if (HAS_RANDOM_ITEMS(op))
242  while ((op->stats.hp--) > 0) {
243  create_treasure(op->randomitems, op, GT_ENVIRONMENT, op->stats.exp ? op->stats.exp : op->map == NULL ? 14 : op->map->difficulty, 0);
244  }
245  object_remove(op);
247  break;
248  }
249 
250  return tmp ? 1 : 0;
251 }
252 
253 /* apply_auto_fix goes through the entire map (only the first time
254  * when an original map is loaded) and performs special actions for
255  * certain objects (most initialization of chests and creation of
256  * treasures and stuff). Calls apply_auto if appropriate.
257  */
259 {
260  int x, y;
261 
262  for (x = 0; x < MAP_WIDTH(m); x++)
263  for (y = 0; y < MAP_HEIGHT(m); y++)
264  FOR_MAP_PREPARE(m, x, y, tmp) {
265  if (QUERY_FLAG(tmp, FLAG_AUTO_APPLY)) {
266  apply_auto(tmp);
267  } else if (tmp->type == TREASURE) {
268  if (HAS_RANDOM_ITEMS(tmp))
269  while ((tmp->stats.hp--) > 0) {
270  create_treasure(tmp->randomitems, tmp, 0, m->difficulty, 0);
271  }
272  }
273  if (tmp && tmp->arch
274  && tmp->type != PLAYER
275  && tmp->type != TREASURE
276  && tmp->randomitems) {
277  if (tmp->type == CONTAINER) {
278  if (HAS_RANDOM_ITEMS(tmp))
279  while ((tmp->stats.hp--) > 0) {
280  create_treasure(tmp->randomitems, tmp, 0, m->difficulty, 0);
281  }
282  } else if (HAS_RANDOM_ITEMS(tmp)) {
283  create_treasure(tmp->randomitems, tmp, 0, m->difficulty, 0);
284  }
285  }
286  }
287  FOR_MAP_FINISH();
288  for (x = 0; x < MAP_WIDTH(m); x++)
289  for (y = 0; y < MAP_HEIGHT(m); y++)
290  FOR_MAP_PREPARE(m, x, y, tmp) {
291  if (tmp->above
292  && (tmp->type == TRIGGER_BUTTON || tmp->type == TRIGGER_PEDESTAL)) {
293  check_trigger(tmp, tmp->above);
294  }
295  }
296  FOR_MAP_FINISH();
297 }
298 
299 /*
300  * The following dummy variables are only used to resolve symbols at compile
301  * time. They don't actually do anything useful.
302  */
303 
304 void set_map_timeout(mapstruct *oldmap) {
305  (void)oldmap;
306 }
307 
308 void draw_ext_info(int flags, int pri, const object *pl, uint8_t type,
309  uint8_t subtype, const char *message) {
310  (void)flags;
311  (void)pri;
312  (void)pl;
313  (void)type;
314  (void)subtype;
315  fprintf(logfile, "%s\n", message);
316 }
317 
318 void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type,
319  uint8_t subtype, const char *format, ...) {
320  va_list ap;
321 
322  (void)flags;
323  (void)pri;
324  (void)pl;
325  (void)type;
326  (void)subtype;
327 
328  va_start(ap, format);
329  vfprintf(logfile, format, ap);
330  va_end(ap);
331 }
332 
333 
334 void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype,
335  const char *str1) {
336  (void)color;
337  (void)map;
338  (void)type;
339  (void)subtype;
340  fprintf(logfile, "ext_info_map: %s\n", str1);
341 }
342 
343 void move_firewall(object *ob) {
344  (void)ob;
345 }
346 
347 void emergency_save(int x) {
348  (void)x;
349 }
350 
351 void clean_tmp_files(void) {
352 }
353 
354 void esrv_send_item(object *ob, object *obx) {
355  (void)ob;
356  (void)obx;
357 }
358 
359 void esrv_update_item(int flags, object *pl, object *op) {
360  (void)flags;
361  (void)pl;
362  (void)op;
363 }
364 
365 void dragon_ability_gain(object *ob, int x, int y) {
366  (void)ob;
367  (void)x;
368  (void)y;
369 }
370 
372  (void)m;
373 }
374 
375 object *find_skill_by_number(object *who, int skillno) {
376  (void)who;
377  (void)skillno;
378  return NULL;
379 }
380 
381 void esrv_del_item(player *pl, object *ob) {
382  (void)pl;
383  (void)ob;
384 }
385 
387  (void)pl;
388 }
389 
390 void rod_adjust(object *rod) {
391  (void)rod;
392 }
Random map parameters.
Definition: random_map.h:14
#define FLAG_DAMNED
The object is very cursed.
Definition: define.h:305
#define FLAG_UNPAID
Object hasn&#39;t been paid for yet.
Definition: define.h:223
void esrv_update_spells(player *pl)
Definition: main.cpp:386
void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype, const char *str1)
Definition: main.cpp:334
FILE * logfile
Used by server/daemon.c.
Definition: init.cpp:114
void init_readable(void)
Initialize linked lists utilized by message functions in tailor_readable_ob()
Definition: readable.cpp:904
int save_map(mapstruct *m, int flag)
Saves a map to file.
Definition: map.cpp:1404
char ** map_gen_spiral(int xsize, int ysize, int option, int _unused_layers)
Generates a spiral layout.
int apply_auto(object *op)
Definition: main.cpp:211
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to...
Definition: object.cpp:1818
void init_library(void)
It is vital that init_library() is called by any functions using this library.
Definition: init.cpp:315
Random map related variables.
#define MAP_HEIGHT(m)
Map height.
Definition: map.h:78
static void print_quickhelp(void)
Print a message stating how to get help.
Definition: main.cpp:128
int16_t y
Position in the map for this object.
Definition: object.h:335
See Button Trigger.
Definition: object.h:137
static void generate_map(char *OutFileName)
Definition: main.cpp:33
int16_t x
Definition: object.h:335
const char * name
Definition: main.cpp:85
Global type definitions and header inclusions.
void init_globals(void)
Initialises all global variables.
Definition: init.cpp:387
void rod_adjust(object *rod)
Definition: main.cpp:390
#define SRANDOM(seed)
Definition: define.h:668
static void print_usage(void)
Print out usage information.
Definition: main.cpp:133
void set_darkness_map(mapstruct *m)
Definition: main.cpp:371
static void print_map(char **layout, int width, int height)
Print the human-readable layout of a map.
Definition: main.cpp:60
#define NROFLAYOUTS
Definition: random_map.h:119
char ** roguelike_layout_gen(int xsize, int ysize, int options, int _unused_layers)
Actually make the rogue layout.
void create_treasure(treasurelist *t, object *op, int flag, int difficulty, int tries)
This calls the appropriate treasure creation function.
Definition: treasure.cpp:287
object * identify(object *op)
Identifies an item.
Definition: item.cpp:1446
int check_trigger(object *op, object *cause)
Definition: button.cpp:518
See Treasure.
Definition: object.h:115
void esrv_del_item(player *pl, object *ob)
Definition: main.cpp:381
#define LO_NEWFILE
Definition: main.cpp:31
#define SET_FLAG(xyz, p)
Definition: define.h:384
#define QUERY_FLAG(xyz, p)
Definition: define.h:386
char ** make_square_spiral_layout(int xsize, int ysize, int _unused_options, int _unused_layers)
Generates a square-spiral layout.
char ** maze_gen(int xsize, int ysize, int option, int _unused_layers)
This function generates a random blocked maze with the property that there is only one path from one ...
Definition: maze_gen.cpp:59
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.cpp:1545
This is a game-map.
Definition: map.h:320
static layout layout_list[NROFLAYOUTS]
Definition: main.cpp:89
void esrv_send_item(object *ob, object *obx)
Definition: main.cpp:354
void set_map_timeout(mapstruct *oldmap)
Definition: main.cpp:304
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.cpp:308
void emergency_save(int x)
Definition: main.cpp:347
object * find_skill_by_number(object *who, int skillno)
Definition: main.cpp:375
int main(int argc, char *argv[])
Definition: main.cpp:156
living stats
Str, Con, Dex, etc.
Definition: object.h:378
#define SAVE_MODE_INPLACE
Map is saved from where it was loaded.
Definition: map.h:120
struct mapstruct * map
Pointer to the map in which this object is present.
Definition: object.h:305
int Ysize
Definition: random_map.h:75
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.cpp:318
See Container.
Definition: object.h:236
static const flag_definition flags[]
Flag mapping.
#define FOR_MAP_FINISH()
Finishes FOR_MAP_PREPARE().
Definition: define.h:753
int Xsize
Definition: random_map.h:74
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.cpp:359
#define FLAG_CURSED
The object is cursed.
Definition: define.h:304
See Player.
Definition: object.h:112
struct treasurelist * randomitems
Items to be generated.
Definition: object.h:395
#define FLAG_AUTO_APPLY
Will be applied when created.
Definition: define.h:237
int load_parameters(FILE *fp, int bufstate, RMParms *RP)
static event_registration m
Definition: citylife.cpp:424
object * generate_treasure(treasurelist *t, int difficulty)
Generate a treasure from a list generating a single item.
Definition: treasure.cpp:319
void apply_auto_fix(mapstruct *m)
Definition: main.cpp:258
See Shop Floor.
Definition: object.h:188
void clean_tmp_files(void)
Definition: main.cpp:351
#define MAP_WIDTH(m)
Map width.
Definition: map.h:76
#define CLEAR_FLAG(xyz, p)
Definition: define.h:385
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:348
void move_firewall(object *ob)
Definition: main.cpp:343
void init_gods(void)
This takes a look at all of the archetypes to find the objects which correspond to the GODS (type GOD...
Definition: holy.cpp:59
int64_t exp
Experience.
Definition: living.h:47
char ** map_gen_onion(int xsize, int ysize, int option, int layers)
Generates an onion layout.
One player.
Definition: player.h:107
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Constructs a loop iterating over all objects of a map tile.
Definition: define.h:746
uint16_t difficulty
What level the player should be to play here.
Definition: map.h:338
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Same as object_insert_in_map() except it handle separate coordinates and do a clean job preparing mul...
Definition: object.cpp:2085
char **(* func)(int, int, int, int)
Definition: main.cpp:86
void dragon_ability_gain(object *ob, int x, int y)
Definition: main.cpp:365
static void test_layout(int width, int height, char **(*layout_func)(int, int, int, int))
Test the map layout produced by the specified generator.
Definition: main.cpp:113
char ** make_snake_layout(int xsize, int ysize, int _unused_options, int _unused_layers)
Generate a snake-like layout.
Definition: snake.cpp:36
#define HAS_RANDOM_ITEMS(op)
This return TRUE if object has still randomitems which could be expanded.
Definition: define.h:184
int16_t hp
Hit Points.
Definition: living.h:40
mapstruct * generate_random_map(const char *OutFileName, RMParms *RP, char **use_layout, sstring reset_group)
Main random map routine.
Definition: random_map.cpp:75
Definition: main.cpp:84