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