Crossfire Server, Trunk  R213250
main.c
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 "global.h"
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 
26 #include "maze_gen.h"
27 #include "random_map.h"
28 #include "room_gen.h"
29 #include "rproto.h"
30 #include "sproto.h"
31 
32 #define LO_NEWFILE 2
33 
34 static void generate_map(char *OutFileName) {
35  RMParms rp;
36  mapstruct *newMap;
37 
38  fprintf(stderr, "Reading parameters from stdin...\n");
39 
40  /* Initialize parameters and set initial settings. */
41  memset(&rp, 0, sizeof(RMParms));
42  rp.Xsize = -1;
43  rp.Ysize = -1;
44 
45  /* Initialize Crossfire library. */
46  init_globals();
47  init_library();
50  init_formulae();
51  init_readable();
52  init_gods();
53 
54  load_parameters(stdin, LO_NEWFILE, &rp);
55  fclose(stdin);
56 
57  newMap = generate_random_map(OutFileName, &rp, NULL);
58  save_map(newMap, SAVE_MODE_INPLACE);
59 }
60 
64 static void print_map(char **layout, int width, int height) {
65  int i, j;
66 
67  for (j = 0; j < height; ++j) {
68  for (i = 0; i < width; ++i) {
69  char display_char;
70  display_char = layout[i][j];
71 
72  switch (display_char) {
73  case 0:
74  display_char = '.';
75  break;
76  case 'D':
77  display_char = '+';
78  break;
79  }
80 
81  putchar(display_char);
82  }
83 
84  putchar('\n');
85  }
86 }
87 
88 typedef struct {
89  char *name;
90  char **(*func)(int, int, int, int);
91 } layout;
92 
94  // Most of these need to be cast to silence warnings.
95  // The fourth paramter (and sometimes the third) is ignored in most cases.
96  // xsize,ysize,option,layers
97  { "rogue", &roguelike_layout_gen },
98  { "snake", &make_snake_layout },
99  { "sspiral", &make_square_spiral_layout },
100  { "spiral", &map_gen_spiral },
101  { "maze", &maze_gen },
102  { "onion", &map_gen_onion }
103 };
104 
117 static void test_layout(int width, int height, char **(*layout_func)(int, int, int, int)) {
118  char **layout;
119  SRANDOM(time(0));
120 
121  // Bail if no layout -- shouldn't occur, but just to be safe
122  if (layout_func == NULL)
123  return;
124 
125  layout = layout_func(width, height, 0, 0);
126 
127  print_map(layout, width, height);
128  free(layout);
129 }
130 
132 static void print_quickhelp(void) {
133  fprintf(stderr, "Type 'random_map -h' for usage instructions.\n");
134 }
135 
137 static void print_usage(void) {
138  printf(
139  "Usage: random_map [options]\n"
140  "\n"
141  "Options:\n"
142  " -h display this help message\n"
143  " -g <file> randomly generate the specified map file\n"
144  " -l <layout> layout to use. See Layouts for valid layouts.\n"
145  " (overridden by -g)\n"
146  " -t test map layout (overriden by -g)\n"
147  " -x <width> specify map width\n"
148  " -y <height> specify map height\n"
149  "\n"
150  "Layouts:\n"
151  " rogue -- roguelike map generator\n"
152  " snake -- snake map generator\n"
153  " sspiral -- square spiral map generator\n"
154  " spiral -- spiral map generator\n"
155  " maze -- maze map generator\n"
156  " onion -- onion map generator\n"
157  );
158 }
159 
160 int main(int argc, char *argv[]) {
161  int flag, mode = 0, width = 80, height = 23;
162  char *filename_out=NULL;
163  // Make default behavior be roguelike generation, like old behavior
164  // NOTE: The ugly function pointer cast silences compiler warnings.
165  char **(*func)(int, int, int, int) = (char **(*)(int, int, int, int))&roguelike_layout_gen;
166 
167  /* Parse command-line arguments. */
168  while ((flag = getopt(argc, argv, "g:hl:tx:y:")) != -1) {
169  switch (flag) {
170  case 'g':
171  mode = 2;
172  filename_out = optarg;
173  break;
174  case 'h':
175  print_usage();
176  exit(EXIT_SUCCESS);
177  break;
178  case 'l':
179  for (int i = 0; i < NROFLAYOUTS; ++i)
180  {
181  if (strcmp(optarg, layout_list[i].name) == 0)
182  func = layout_list[i].func;
183  }
184  break;
185  case 't':
186  mode = 1;
187  break;
188  case 'x':
189  width = atoi(optarg);
190  break;
191  case 'y':
192  height = atoi(optarg);
193  break;
194  case '?':
195  print_quickhelp();
196  exit(EXIT_FAILURE);
197  break;
198  }
199  }
200 
201  if (mode == 1) {
202  test_layout(width, height, func);
203  exit(EXIT_SUCCESS);
204  } else if (mode == 2) {
205  generate_map(filename_out);
206  exit(EXIT_SUCCESS);
207  } else {
208  print_quickhelp();
209  exit(EXIT_FAILURE);
210  }
211 }
212 
213 /* some plagarized code from apply.c--I needed just these two functions
214 without all the rest of the junk, so.... */
215 int apply_auto(object *op)
216 {
217  object *tmp = NULL;
218  int i;
219 
220  switch (op->type) {
221  case SHOP_FLOOR:
222  if (!HAS_RANDOM_ITEMS(op)) {
223  return 0;
224  }
225  do {
226  i = 10; /* let's give it 10 tries */
227  while ((tmp = generate_treasure(op->randomitems, op->stats.exp ? op->stats.exp : 5)) == NULL && --i)
228  ;
229  if (tmp == NULL) {
230  return 0;
231  }
232  if (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED)) {
234  tmp = NULL;
235  }
236  } while (!tmp);
237 
238  SET_FLAG(tmp, FLAG_UNPAID);
239  object_insert_in_map_at(tmp, op->map, NULL, 0, op->x, op->y);
241  tmp = identify(tmp);
242  break;
243 
244  case TREASURE:
245  if (HAS_RANDOM_ITEMS(op))
246  while ((op->stats.hp--) > 0) {
247  create_treasure(op->randomitems, op, GT_ENVIRONMENT, op->stats.exp ? op->stats.exp : op->map == NULL ? 14 : op->map->difficulty, 0);
248  }
249  object_remove(op);
251  break;
252  }
253 
254  return tmp ? 1 : 0;
255 }
256 
257 /* apply_auto_fix goes through the entire map (only the first time
258  * when an original map is loaded) and performs special actions for
259  * certain objects (most initialization of chests and creation of
260  * treasures and stuff). Calls apply_auto if appropriate.
261  */
263 {
264  int x, y;
265 
266  for (x = 0; x < MAP_WIDTH(m); x++)
267  for (y = 0; y < MAP_HEIGHT(m); y++)
268  FOR_MAP_PREPARE(m, x, y, tmp) {
269  if (QUERY_FLAG(tmp, FLAG_AUTO_APPLY)) {
270  apply_auto(tmp);
271  } else if (tmp->type == TREASURE) {
272  if (HAS_RANDOM_ITEMS(tmp))
273  while ((tmp->stats.hp--) > 0) {
274  create_treasure(tmp->randomitems, tmp, 0, m->difficulty, 0);
275  }
276  }
277  if (tmp && tmp->arch
278  && tmp->type != PLAYER
279  && tmp->type != TREASURE
280  && tmp->randomitems) {
281  if (tmp->type == CONTAINER) {
282  if (HAS_RANDOM_ITEMS(tmp))
283  while ((tmp->stats.hp--) > 0) {
284  create_treasure(tmp->randomitems, tmp, 0, m->difficulty, 0);
285  }
286  } else if (HAS_RANDOM_ITEMS(tmp)) {
287  create_treasure(tmp->randomitems, tmp, 0, m->difficulty, 0);
288  }
289  }
290  }
291  FOR_MAP_FINISH();
292  for (x = 0; x < MAP_WIDTH(m); x++)
293  for (y = 0; y < MAP_HEIGHT(m); y++)
294  FOR_MAP_PREPARE(m, x, y, tmp) {
295  if (tmp->above
296  && (tmp->type == TRIGGER_BUTTON || tmp->type == TRIGGER_PEDESTAL)) {
297  check_trigger(tmp, tmp->above);
298  }
299  }
300  FOR_MAP_FINISH();
301 }
302 
303 /*
304  * The following dummy variables are only used to resolve symbols at compile
305  * time. They don't actually do anything useful.
306  */
307 
308 void set_map_timeout(mapstruct *oldmap) {
309 }
310 
311 void draw_ext_info(int flags, int pri, const object *pl, uint8_t type,
312  uint8_t subtype, const char *message) {
313  fprintf(logfile, "%s\n", message);
314 }
315 
316 void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type,
317  uint8_t subtype, const char *format, ...) {
318  va_list ap;
319 
320  va_start(ap, format);
321  vfprintf(logfile, format, ap);
322  va_end(ap);
323 }
324 
325 
326 void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype,
327  const char *str1) {
328  fprintf(logfile, "ext_info_map: %s\n", str1);
329 }
330 
331 void move_firewall(object *ob) {
332 }
333 
334 void emergency_save(int x) {
335 }
336 
337 void clean_tmp_files(void) {
338 }
339 
340 void esrv_send_item(object *ob, object *obx) {
341 }
342 
343 void esrv_update_item(int flags, object *pl, object *op) {
344 }
345 
346 void dragon_ability_gain(object *ob, int x, int y) {
347 }
348 
350 }
351 
352 object *find_skill_by_number(object *who, int skillno) {
353  return NULL;
354 }
355 
356 void esrv_del_item(player *pl, object *ob) {
357 }
358 
360 }
361 
362 void rod_adjust(object *rod) {
363 }
364 
365 int execute_event(object *op, int eventcode, object *activator, object *third,
366  const char *message, int fix) {
367  return 0;
368 }
369 
370 int execute_global_event(int eventcode, ...) {
371  return 0;
372 }
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:316
EXTERN FILE * logfile
Definition: global.h:142
void apply_auto_fix(mapstruct *m)
Definition: main.c:262
Definition: player.h:92
#define FLAG_DAMNED
Definition: define.h:318
mapstruct * generate_random_map(const char *OutFileName, RMParms *RP, char **use_layout)
Definition: random_map.c:74
#define FLAG_UNPAID
Definition: define.h:236
PyObject * third
Definition: cfpython.h:118
static void print_usage(void)
Definition: main.c:137
static void test_layout(int width, int height, char **(*layout_func)(int, int, int, int))
Definition: main.c:117
#define SET_FLAG(xyz, p)
Definition: define.h:223
unsigned char uint8_t
Definition: win32.h:161
static void print_quickhelp(void)
Definition: main.c:132
char ** map_gen_spiral(int xsize, int ysize, int option, int _unused_layers)
PyObject * activator
Definition: cfpython.h:117
void esrv_send_item(object *ob, object *obx)
Definition: main.c:340
void set_map_timeout(mapstruct *oldmap)
Definition: main.c:308
struct treasureliststruct * randomitems
Definition: object.h:387
#define MAP_HEIGHT(m)
Definition: map.h:80
void init_archetypes(void)
Definition: arch.c:182
void init_globals(void)
Definition: init.c:257
static layout layout_list[NROFLAYOUTS]
Definition: main.c:93
int64_t exp
Definition: living.h:47
int execute_global_event(int eventcode,...)
Definition: main.c:370
char message[1024]
Definition: cfpython.h:120
void init_library(void)
Definition: init.c:186
int main(int argc, char *argv[])
Definition: main.c:160
void rod_adjust(object *rod)
Definition: main.c:362
#define SRANDOM(seed)
Definition: define.h:682
int16_t hp
Definition: living.h:40
void init_formulae(void)
Definition: recipe.c:143
static void generate_map(char *OutFileName)
Definition: main.c:34
void create_treasure(treasurelist *t, object *op, int flag, int difficulty, int tries)
Definition: treasure.c:488
void set_darkness_map(mapstruct *m)
Definition: main.c:349
void object_free_drop_inventory(object *ob)
Definition: object.c:1389
int16_t y
Definition: object.h:326
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1933
void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype, const char *str1)
Definition: main.c:326
#define NROFLAYOUTS
Definition: random_map.h:111
char ** roguelike_layout_gen(int xsize, int ysize, int options, int _unused_layers)
Definition: rogue_layout.c:332
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.c:343
char ** make_square_spiral_layout(int xsize, int ysize, int _unused_options, int _unused_layers)
Definition: square_spiral.c:78
struct mapdef * map
Definition: object.h:297
char ** maze_gen(int xsize, int ysize, int option, int _unused_layers)
Definition: maze_gen.c:58
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:311
void esrv_update_spells(player *pl)
Definition: main.c:359
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
object * find_skill_by_number(object *who, int skillno)
Definition: main.c:352
void init_readable(void)
Definition: readable.c:1027
int apply_auto(object *op)
Definition: main.c:215
#define SAVE_MODE_INPLACE
Definition: map.h:121
int16_t x
Definition: object.h:326
PyObject * who
Definition: cfpython.h:116
int Ysize
Definition: random_map.h:70
uint16_t difficulty
Definition: map.h:343
static const flag_definition flags[]
void clean_tmp_files(void)
Definition: main.c:337
#define FOR_MAP_FINISH()
Definition: define.h:767
int Xsize
Definition: random_map.h:69
int save_map(mapstruct *m, int flag)
Definition: map.c:1442
#define FLAG_CURSED
Definition: define.h:317
Definition: object.h:107
int execute_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: main.c:365
#define FLAG_AUTO_APPLY
Definition: define.h:250
int load_parameters(FILE *fp, int bufstate, RMParms *RP)
living stats
Definition: object.h:369
#define MAP_WIDTH(m)
Definition: map.h:78
static void print_map(char **layout, int width, int height)
Definition: main.c:64
uint8_t type
Definition: object.h:339
void init_gods(void)
Definition: holy.c:53
object * identify(object *op)
Definition: item.c:1432
void move_firewall(object *ob)
Definition: main.c:331
char ** map_gen_onion(int xsize, int ysize, int option, int layers)
void emergency_save(int x)
Definition: main.c:334
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:760
int check_trigger(object *op, object *cause)
Definition: button.c:523
void esrv_del_item(player *pl, object *ob)
Definition: main.c:356
object * generate_treasure(treasurelist *t, int difficulty)
Definition: treasure.c:516
Definition: map.h:325
char **(* func)(int, int, int, int)
Definition: main.c:90
char ** make_snake_layout(int xsize, int ysize, int _unused_options, int _unused_layers)
Definition: snake.c:34
#define LO_NEWFILE
Definition: main.c:32
void dragon_ability_gain(object *ob, int x, int y)
Definition: main.c:346
void init_artifacts(void)
Definition: artifact.c:530
#define HAS_RANDOM_ITEMS(op)
Definition: define.h:183
void object_remove(object *op)
Definition: object.c:1666
Definition: main.c:88
char * name
Definition: main.c:89