Crossfire Server, Trunk  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 }
map_gen_onion
char ** map_gen_onion(int xsize, int ysize, int option, int layers)
Generates an onion layout.
Definition: room_gen_onion.cpp:70
init_globals
void init_globals(void)
Initialises all global variables.
Definition: init.cpp:387
living::exp
int64_t exp
Experience.
Definition: living.h:47
HAS_RANDOM_ITEMS
#define HAS_RANDOM_ITEMS(op)
This return TRUE if object has still randomitems which could be expanded.
Definition: define.h:184
SAVE_MODE_INPLACE
#define SAVE_MODE_INPLACE
Map is saved from where it was loaded.
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()
Finishes FOR_MAP_PREPARE().
Definition: define.h:724
roguelike_layout_gen
char ** roguelike_layout_gen(int xsize, int ysize, int options, int _unused_layers)
Actually make the rogue layout.
Definition: rogue_layout.cpp:309
layout
Definition: main.cpp:84
mapstruct::difficulty
uint16_t difficulty
What level the player should be to play here.
Definition: map.h:333
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
player
One player.
Definition: player.h:105
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)
Print out usage information.
Definition: main.cpp:133
SHOP_FLOOR
@ SHOP_FLOOR
Definition: object.h:188
object::x
int16_t x
Definition: object.h:335
RMParms::Ysize
int Ysize
Definition: random_map.h:75
object::map
struct mapstruct * map
Pointer to the map in which this object is present.
Definition: object.h:305
generate_treasure
object * generate_treasure(treasurelist *t, int difficulty)
Generate a treasure from a list generating a single item.
Definition: treasure.cpp:319
SRANDOM
#define SRANDOM(seed)
Definition: define.h:639
make_square_spiral_layout
char ** make_square_spiral_layout(int xsize, int ysize, int _unused_options, int _unused_layers)
Generates a square-spiral layout.
Definition: square_spiral.cpp:80
time
non standard information is not specified or uptime this means how long since the executable has been started A particular host may have been running a server for quite a long time
Definition: arch-handbook.txt:206
room_gen.h
TREASURE
@ TREASURE
Definition: object.h:115
maze_gen
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
flags
static const flag_definition flags[]
Flag mapping.
Definition: gridarta-types-convert.cpp:101
generate_random_map
mapstruct * generate_random_map(const char *OutFileName, RMParms *RP, char **use_layout, sstring reset_group)
Main random map routine.
Definition: random_map.cpp:75
test_layout
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
maze_gen.h
TRIGGER_BUTTON
@ TRIGGER_BUTTON
Definition: object.h:137
map_gen_spiral
char ** map_gen_spiral(int xsize, int ysize, int option, int _unused_layers)
Generates a spiral layout.
Definition: room_gen_spiral.cpp:62
esrv_update_spells
void esrv_update_spells(player *pl)
Definition: main.cpp:386
RMParms
Random map parameters.
Definition: random_map.h:14
set_map_timeout
void set_map_timeout(mapstruct *oldmap)
Definition: main.cpp:304
name
Plugin animator file specs[Config] name
Definition: animfiles.txt:4
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)
This takes a look at all of the archetypes to find the objects which correspond to the GODS (type GOD...
Definition: holy.cpp:59
object::y
int16_t y
Position in the map for this object.
Definition: object.h:335
m
static event_registration m
Definition: citylife.cpp:424
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects,...
Definition: object.cpp:1545
load_parameters
int load_parameters(FILE *fp, int bufstate, RMParms *RP)
Definition: reader.cpp:2492
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)
Initialize linked lists utilized by message functions in tailor_readable_ob()
Definition: readable.cpp:904
move_firewall
void move_firewall(object *ob)
Definition: main.cpp:343
object::type
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:348
message
TIPS on SURVIVING Crossfire is populated with a wealth of different monsters These monsters can have varying immunities and attack types In some of them can be quite a bit smarter than others It will be important for new players to learn the abilities of different monsters and learn just how much it will take to kill them This section discusses how monsters can interact with players Most monsters in the game are out to mindlessly kill and destroy the players These monsters will help boost a player s after he kills them When fighting a large amount of monsters in a single attempt to find a narrower hallway so that you are not being attacked from all sides Charging into a room full of Beholders for instance would not be open the door and fight them one at a time For there are several maps designed for them Find these areas and clear them out All throughout these a player can find signs and books which they can read by stepping onto them and hitting A to apply the book sign These messages will help the player to learn the system One more always keep an eye on your food If your food drops to your character will soon so BE CAREFUL ! NPCs Non Player Character are special monsters which have intelligence Players may be able to interact with these monsters to help solve puzzles and find items of interest To speak with a monster you suspect to be a simply move to an adjacent square to them and push the double ie Enter your message
Definition: survival-guide.txt:34
apply_auto_fix
void apply_auto_fix(mapstruct *m)
Definition: main.cpp:258
FLAG_DAMNED
#define FLAG_DAMNED
The object is very cursed.
Definition: define.h:317
emergency_save
void emergency_save(int x)
Definition: main.cpp:347
rproto.h
sproto.h
logfile
FILE * logfile
Used by server/daemon.c.
Definition: init.cpp:114
GT_ENVIRONMENT
@ GT_ENVIRONMENT
?
Definition: treasure.h:31
object_insert_in_map_at
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
esrv_update_item
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.cpp:359
MAP_WIDTH
#define MAP_WIDTH(m)
Map width.
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_)
Constructs a loop iterating over all objects of a map tile.
Definition: define.h:717
dragon_ability_gain
void dragon_ability_gain(object *ob, int x, int y)
Definition: main.cpp:365
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)
Print the human-readable layout of a map.
Definition: main.cpp:60
init_library
void init_library(void)
It is vital that init_library() is called by any functions using this library.
Definition: init.cpp:315
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
This is a game-map.
Definition: map.h:315
FLAG_AUTO_APPLY
#define FLAG_AUTO_APPLY
Will be applied when created.
Definition: define.h:250
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
MAP_HEIGHT
#define MAP_HEIGHT(m)
Map height.
Definition: map.h:75
make_snake_layout
char ** make_snake_layout(int xsize, int ysize, int _unused_options, int _unused_layers)
Generate a snake-like layout.
Definition: snake.cpp:36
object::randomitems
struct treasurelist * randomitems
Items to be generated.
Definition: object.h:395
object_remove
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
FLAG_UNPAID
#define FLAG_UNPAID
Object hasn't been paid for yet.
Definition: define.h:236
clean_tmp_files
void clean_tmp_files(void)
Definition: main.cpp:351
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)
Saves a map to file.
Definition: map.cpp:1405
object::stats
living stats
Str, Con, Dex, etc.
Definition: object.h:378
NROFLAYOUTS
#define NROFLAYOUTS
Definition: random_map.h:119
FLAG_CURSED
#define FLAG_CURSED
The object is cursed.
Definition: define.h:316
generate_map
static void generate_map(char *OutFileName)
Definition: main.cpp:33
RMParms::Xsize
int Xsize
Definition: random_map.h:74
living::hp
int16_t hp
Hit Points.
Definition: living.h:40
create_treasure
void create_treasure(treasurelist *t, object *op, int flag, int difficulty, int tries)
This calls the appropriate treasure creation function.
Definition: treasure.cpp:287
print_quickhelp
static void print_quickhelp(void)
Print a message stating how to get help.
Definition: main.cpp:128
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
identify
object * identify(object *op)
Identifies an item.
Definition: item.cpp:1449