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