Crossfire Server, Branch 1.12  R12190
snake.c
Go to the documentation of this file.
00001 
00002 
00008 #include <stdio.h>
00009 #include <global.h>
00010 #include <time.h>
00011 
00020 char **make_snake_layout(int xsize, int ysize) {
00021     int i, j;
00022 
00023     /* allocate that array, set it up */
00024     char **maze = (char **)calloc(sizeof(char *), xsize);
00025     for (i = 0; i < xsize; i++) {
00026         maze[i] = (char *)calloc(sizeof(char), ysize);
00027     }
00028 
00029     /* write the outer walls */
00030     for (i = 0; i < xsize; i++)
00031         maze[i][0] = maze[i][ysize-1] = '#';
00032     for (j = 0; j < ysize; j++)
00033         maze[0][j] = maze[xsize-1][j] = '#';
00034 
00035     /* Bail out if the size is too small to make a snake. */
00036     if (xsize < 8 || ysize < 8)
00037         return maze;
00038 
00039     /* decide snake orientation--vertical or horizontal , and
00040        make the walls and place the doors. */
00041 
00042     if (RANDOM()%2) { /* vertical orientation */
00043         int n_walls = RANDOM()%((xsize-5)/3)+1;
00044         int spacing = xsize/(n_walls+1);
00045         int orientation = 1;
00046 
00047         for (i = spacing; i < xsize-3; i += spacing) {
00048             if (orientation) {
00049                 for (j = 1; j < ysize-2; j++) {
00050                     maze[i][j] = '#';
00051                 }
00052                 maze[i][j] = 'D';
00053             } else {
00054                 for (j = 2; j < ysize; j++) {
00055                     maze[i][j] = '#';
00056                 }
00057                 maze[i][1] = 'D';
00058             }
00059             orientation ^= 1; /* toggle the value of orientation */
00060         }
00061     } else { /* horizontal orientation */
00062         int n_walls = RANDOM()%((ysize-5)/3)+1;
00063         int spacing = ysize/(n_walls+1);
00064         int orientation = 1;
00065 
00066         for (i = spacing; i < ysize-3; i += spacing) {
00067             if (orientation) {
00068                 for (j = 1; j < xsize-2; j++) {
00069                     maze[j][i] = '#';
00070                 }
00071                 maze[j][i] = 'D';
00072             } else {
00073                 for (j = 2; j < xsize; j++) {
00074                     maze[j][i] = '#';
00075                 }
00076                 maze[1][i] = 'D';
00077             }
00078             orientation ^= 1; /* toggle the value of orientation */
00079         }
00080     }
00081 
00082     /* place the exit up/down */
00083     if (RANDOM()%2) {
00084         maze[1][1] = '<';
00085         maze[xsize-2][ysize-2] = '>';
00086     } else {
00087         maze[1][1] = '>';
00088         maze[xsize-2][ysize-2] = '<';
00089     }
00090 
00091     return maze;
00092 }