Debian Codegraph @

random_maps/main.c

raw symbols SCIP
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 <[email protected]>.
12 */
13
14 /**
15 * @file random_maps/main.c
16 * Command-line interface to the map generator used to test layouts.
17 */
18
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_archetypes();
49 init_artifacts();
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
61 /**
62 * Print the human-readable layout of a map.
63 */
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
93 static layout layout_list[NROFLAYOUTS] = {
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
105 /**
106 * Test the map layout produced by the specified generator.
107 *
108 * @param width
109 * Map width
110 *
111 * @param height
112 * Map height
113 *
114 * @param layout_func
115 * The layout method to use.
116 */
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
131 /** Print a message stating how to get help. */
132 static void print_quickhelp(void) {
133 fprintf(stderr, "Type 'random_map -h' for usage instructions.\n");
134 }
135
136 /** Print out usage information. */
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)) {
233 object_free_drop_inventory(tmp);
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);
240 CLEAR_FLAG(op, FLAG_AUTO_APPLY);
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);
250 object_free_drop_inventory(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 */
262 void apply_auto_fix(mapstruct *m)
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
349 void set_darkness_map(mapstruct *m) {
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
359 void esrv_update_spells(player *pl) {
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 }