Crossfire Server, Trunk
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  }
230  tmp = NULL;
231  }
232  } while (!tmp);
233 
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) {
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 }
map_gen_onion
char ** map_gen_onion(int xsize, int ysize, int option, int layers)
Definition: room_gen_onion.cpp:70
init_globals
void init_globals(void)
Definition: init.cpp:396
HAS_RANDOM_ITEMS
#define HAS_RANDOM_ITEMS(op)
Definition: define.h:184
SAVE_MODE_INPLACE
#define SAVE_MODE_INPLACE
Definition: map.h:117
PLAYER
@ PLAYER
Definition: object.h:112
layout::func
char **(* func)(int, int, int, int)
Definition: main.cpp:86
global.h
apply_auto
int apply_auto(object *op)
Definition: main.cpp:211
random_map.h
FOR_MAP_FINISH
#define FOR_MAP_FINISH()
Definition: define.h:730
roguelike_layout_gen
char ** roguelike_layout_gen(int xsize, int ysize, int options, int _unused_layers)
Definition: rogue_layout.cpp:309
layout
Definition: main.cpp:84
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
player
Definition: player.h:105
diamondslots.x
x
Definition: diamondslots.py:15
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
TRIGGER_PEDESTAL
@ TRIGGER_PEDESTAL
Definition: object.h:139
print_usage
static void print_usage(void)
Definition: main.cpp:133
SHOP_FLOOR
@ SHOP_FLOOR
Definition: object.h:188
RMParms::Ysize
int Ysize
Definition: random_map.h:75
guildjoin.ob
ob
Definition: guildjoin.py:42
SRANDOM
#define SRANDOM(seed)
Definition: define.h:645
make_square_spiral_layout
char ** make_square_spiral_layout(int xsize, int ysize, int _unused_options, int _unused_layers)
Definition: square_spiral.cpp:80
room_gen.h
TREASURE
@ TREASURE
Definition: object.h:115
maze_gen
char ** maze_gen(int xsize, int ysize, int option, int _unused_layers)
Definition: maze_gen.cpp:59
flags
static const flag_definition flags[]
Definition: gridarta-types-convert.cpp:101
Ice.tmp
int tmp
Definition: Ice.py:207
generate_random_map
mapstruct * generate_random_map(const char *OutFileName, RMParms *RP, char **use_layout, sstring reset_group)
Definition: random_map.cpp:75
test_layout
static void test_layout(int width, int height, char **(*layout_func)(int, int, int, int))
Definition: main.cpp:113
maze_gen.h
TRIGGER_BUTTON
@ TRIGGER_BUTTON
Definition: object.h:137
smoking_pipe.color
color
Definition: smoking_pipe.py:5
create_treasure
void create_treasure(treasurelist *t, object *op, int flag, int difficulty, int tries)
Definition: treasure.cpp:263
map_gen_spiral
char ** map_gen_spiral(int xsize, int ysize, int option, int _unused_layers)
Definition: room_gen_spiral.cpp:62
esrv_update_spells
void esrv_update_spells(player *pl)
Definition: main.cpp:386
RMParms
Definition: random_map.h:14
set_map_timeout
void set_map_timeout(mapstruct *oldmap)
Definition: main.cpp:304
set_darkness_map
void set_darkness_map(mapstruct *m)
Definition: main.cpp:371
esrv_del_item
void esrv_del_item(player *pl, object *ob)
Definition: main.cpp:381
init_gods
void init_gods(void)
Definition: holy.cpp:59
m
static event_registration m
Definition: citylife.cpp:425
autojail.who
who
Definition: autojail.py:3
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.cpp:1560
load_parameters
int load_parameters(FILE *fp, int bufstate, RMParms *RP)
Definition: reader.cpp:2476
disinfect.map
map
Definition: disinfect.py:4
ext_info_map
void ext_info_map(int color, const mapstruct *map, uint8_t type, uint8_t subtype, const char *str1)
Definition: main.cpp:334
LO_NEWFILE
#define LO_NEWFILE
Definition: main.cpp:31
layout_list
static layout layout_list[NROFLAYOUTS]
Definition: main.cpp:89
layout::name
const char * name
Definition: main.cpp: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.cpp:318
rod_adjust
void rod_adjust(object *rod)
Definition: main.cpp:390
CONTAINER
@ CONTAINER
Definition: object.h:236
init_readable
void init_readable(void)
Definition: readable.cpp:904
move_firewall
void move_firewall(object *ob)
Definition: main.cpp:343
apply_auto_fix
void apply_auto_fix(mapstruct *m)
Definition: main.cpp:258
FLAG_DAMNED
#define FLAG_DAMNED
Definition: define.h:317
emergency_save
void emergency_save(int x)
Definition: main.cpp:347
rproto.h
sproto.h
logfile
FILE * logfile
Definition: init.cpp:114
GT_ENVIRONMENT
@ GT_ENVIRONMENT
Definition: treasure.h:31
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
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.cpp:2100
esrv_update_item
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.cpp:359
MAP_WIDTH
#define MAP_WIDTH(m)
Definition: map.h:73
esrv_send_item
void esrv_send_item(object *ob, object *obx)
Definition: main.cpp:354
FOR_MAP_PREPARE
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:723
dragon_ability_gain
void dragon_ability_gain(object *ob, int x, int y)
Definition: main.cpp:365
diamondslots.message
string message
Definition: diamondslots.py:57
check_trigger
int check_trigger(object *op, object *cause)
Definition: button.cpp:518
print_map
static void print_map(char **layout, int width, int height)
Definition: main.cpp:60
init_library
void init_library(void)
Definition: init.cpp:324
find_skill_by_number
object * find_skill_by_number(object *who, int skillno)
Definition: main.cpp:375
main
int main(int argc, char *argv[])
Definition: main.cpp:156
mapstruct
Definition: map.h:313
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
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
MAP_HEIGHT
#define MAP_HEIGHT(m)
Definition: map.h:75
make_snake_layout
char ** make_snake_layout(int xsize, int ysize, int _unused_options, int _unused_layers)
Definition: snake.cpp:36
make_face_from_files.int
int
Definition: make_face_from_files.py:32
object_remove
void object_remove(object *op)
Definition: object.cpp:1833
FLAG_UNPAID
#define FLAG_UNPAID
Definition: define.h:236
clean_tmp_files
void clean_tmp_files(void)
Definition: main.cpp:351
generate_treasure
object * generate_treasure(treasurelist *t, int difficulty)
Definition: treasure.cpp:295
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.cpp:308
save_map
int save_map(mapstruct *m, int flag)
Definition: map.cpp:1396
NROFLAYOUTS
#define NROFLAYOUTS
Definition: random_map.h:118
FLAG_CURSED
#define FLAG_CURSED
Definition: define.h:316
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28
generate_map
static void generate_map(char *OutFileName)
Definition: main.cpp:33
RMParms::Xsize
int Xsize
Definition: random_map.h:74
print_quickhelp
static void print_quickhelp(void)
Definition: main.cpp:128
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
give.name
name
Definition: give.py:27
identify
object * identify(object *op)
Definition: item.cpp:1421