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