Atrinik Server  4.0
snake.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
30 #include <global.h>
31 
41 char **make_snake_layout(int xsize, int ysize)
42 {
43  int i, j;
44  /* Allocate that array, set it up */
45  char **maze = ecalloc(sizeof(char *), xsize);
46 
47  for (i = 0; i < xsize; i++) {
48  maze[i] = ecalloc(sizeof(char), ysize);
49  }
50 
51  /* Write the outer walls */
52  for (i = 0; i < xsize; i++) {
53  maze[i][0] = maze[i][ysize - 1] = '#';
54  }
55 
56  for (j = 0; j < ysize; j++) {
57  maze[0][j] = maze[xsize - 1][j] = '#';
58  }
59 
60  /* Bail out if the size is too small to make a snake. */
61  if (xsize < 8 || ysize < 8) {
62  return maze;
63  }
64 
65  /* decide snake orientation--vertical or horizontal, and
66  * make the walls and place the doors. */
67 
68  /* vertical orientation */
69  if (rndm_chance(2)) {
70  int n_walls = RANDOM() % ((xsize - 5) / 3) + 1;
71  int spacing = xsize / (n_walls + 1);
72  int orientation = 1;
73 
74  for (i = spacing; i < xsize - 3; i += spacing) {
75  if (orientation) {
76  for (j = 1; j < ysize - 2; j++) {
77  maze[i][j] = '#';
78  }
79 
80  maze[i][j] = 'D';
81  } else {
82  for (j = 2; j < ysize; j++) {
83  maze[i][j] = '#';
84  }
85 
86  maze[i][1] = 'D';
87  }
88 
89  /* toggle the value of orientation */
90  orientation ^= 1;
91  }
92  } else {
93  int n_walls = RANDOM() % ((ysize - 5) / 3) + 1;
94  int spacing = ysize / (n_walls + 1);
95  int orientation = 1;
96 
97  /* horizontal orientation */
98 
99  for (i = spacing; i < ysize - 3; i += spacing) {
100  if (orientation) {
101  for (j = 1; j < xsize - 2; j++) {
102  maze[j][i] = '#';
103  }
104 
105  maze[j][i] = 'D';
106  } else {
107  for (j = 2; j < xsize; j++) {
108  maze[j][i] = '#';
109  }
110 
111  maze[1][i] = 'D';
112  }
113 
114  /* toggle the value of orientation */
115  orientation ^= 1;
116  }
117  }
118 
119  /* Place the exit up/down */
120  if (rndm_chance(2)) {
121  maze[1][1] = '<';
122  maze[xsize - 2][ysize - 2] = '>';
123  } else {
124  maze[1][1] = '>';
125  maze[xsize - 2][ysize - 2] = '<';
126  }
127 
128  return maze;
129 }
char ** make_snake_layout(int xsize, int ysize)
Definition: snake.c:41