Debian Codegraph @

random_maps/snake.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
16 * Snake-like layout generator.
17 * @author [email protected]
18 */
19
20 #include "global.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 /**
27 * Generate a snake-like layout.
28 * @param xsize
29 * @param ysize
30 * layout size.
31 * @return
32 * generated layout.
33 */
34 char **make_snake_layout(int xsize, int ysize, int _unused_option, int _unused_layers)
35 {
36 int i, j;
37
38 (void)_unused_option;
39 (void)_unused_layers;
40
41 /* allocate that array, set it up */
42 char **maze = (char **)calloc(sizeof(char *), xsize);
43 for (i = 0; i < xsize; i++) {
44 maze[i] = (char *)calloc(sizeof(char), ysize);
45 }
46
47 /* write the outer walls */
48 for (i = 0; i < xsize; i++) {
49 maze[i][0] = maze[i][ysize-1] = '#';
50 }
51 for (j = 0; j < ysize; j++) {
52 maze[0][j] = maze[xsize-1][j] = '#';
53 }
54
55 /* Bail out if the size is too small to make a snake. */
56 if (xsize < 8 || ysize < 8) {
57 return maze;
58 }
59
60 /* decide snake orientation--vertical or horizontal , and
61 make the walls and place the doors. */
62
63 if (RANDOM()%2) { /* vertical orientation */
64 int n_walls = RANDOM()%((xsize-5)/3)+1;
65 int spacing = xsize/(n_walls+1);
66 int orientation = 1;
67
68 for (i = spacing; i < xsize-3; i += spacing) {
69 if (orientation) {
70 for (j = 1; j < ysize-2; j++) {
71 maze[i][j] = '#';
72 }
73 maze[i][j] = 'D';
74 } else {
75 for (j = 2; j < ysize; j++) {
76 maze[i][j] = '#';
77 }
78 maze[i][1] = 'D';
79 }
80 orientation ^= 1; /* toggle the value of orientation */
81 }
82 } else { /* horizontal orientation */
83 int n_walls = RANDOM()%((ysize-5)/3)+1;
84 int spacing = ysize/(n_walls+1);
85 int orientation = 1;
86
87 for (i = spacing; i < ysize-3; i += spacing) {
88 if (orientation) {
89 for (j = 1; j < xsize-2; j++) {
90 maze[j][i] = '#';
91 }
92 maze[j][i] = 'D';
93 } else {
94 for (j = 2; j < xsize; j++) {
95 maze[j][i] = '#';
96 }
97 maze[1][i] = 'D';
98 }
99 orientation ^= 1; /* toggle the value of orientation */
100 }
101 }
102
103 /* place the exit up/down */
104 if (RANDOM()%2) {
105 maze[1][1] = '<';
106 maze[xsize-2][ysize-2] = '>';
107 } else {
108 maze[1][1] = '>';
109 maze[xsize-2][ysize-2] = '<';
110 }
111
112 return maze;
113 }