Crossfire Server, Trunk  R20513
square_spiral.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 
21 /* peterm@langmuir.eecs.berkeley.edu: this function generates a random
22 snake-type layout.
23 */
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <global.h>
28 #include <time.h>
29 
30 #include <maze_gen.h>
31 #include <room_gen.h>
32 #include <random_map.h>
33 #include <sproto.h>
34 #include <rproto.h>
35 
36 char **map_gen_onion(int xsize, int ysize, int option, int layers);
37 
38 /* These are some helper functions which help with
39  manipulating a centered onion and turning it into
40  a square spiral */
41 
53 void find_top_left_corner(char **maze, int *cx, int *cy)
54 {
55  (*cy)--;
56  /* find the top wall. */
57  while (maze[*cx][*cy] == 0) {
58  (*cy)--;
59  }
60  /* proceed right until a corner is detected */
61  while (maze[*cx][*cy+1] == 0) {
62  (*cx)++;
63  }
64 
65  /* cx and cy should now be the top-right corner of the onion layer */
66 }
67 
78 char **make_square_spiral_layout(int xsize, int ysize)
79 {
80  int i, j;
81  int cx, cy;
82  int tx, ty;
83 
84  /* generate and allocate a doorless, centered onion */
85  char **maze = map_gen_onion(xsize, ysize, OPT_CENTERED|OPT_NO_DOORS, 0);
86 
87  /* find the layout center. */
88  cx = 0;
89  cy = 0;
90  for (i = 0; i < xsize; i++)
91  for (j = 0; j < ysize; j++) {
92  if (maze[i][j] == 'C') {
93  cx = i;
94  cy = j;
95  }
96  }
97  tx = cx;
98  ty = cy;
99  while (1) {
100  find_top_left_corner(maze, &tx, &ty);
101 
102  if (ty < 2 || tx < 2 || tx > xsize-2 || ty > ysize-2) {
103  break;
104  }
105  make_wall(maze, tx, ty-1, 1); /* make a vertical wall with a door */
106 
107  maze[tx][ty-1] = '#'; /* convert the door that make_wall puts here to a wall */
108  maze[tx-1][ty] = 'D';/* make a doorway out of this layer */
109 
110  /* walk left until we find the top-left corner */
111  while ((tx > 2) && maze[tx-1][ty]) {
112  tx--;
113  }
114 
115  make_wall(maze, tx-1, ty, 0); /* make a horizontal wall with a door */
116 
117  /* walk down until we find the bottom-left corner */
118  while (((ty+1) < ysize) && maze[tx][ty+1]) {
119  ty++;
120  }
121 
122  make_wall(maze, tx, ty+1, 1); /* make a vertical wall with a door */
123 
124  /* walk rightuntil we find the bottom-right corner */
125  while (((tx+1) < xsize) && maze[tx+1][ty]) {
126  tx++;
127  }
128 
129  make_wall(maze, tx+1, ty, 0); /* make a horizontal wall with a door */
130  tx++; /* set up for next layer. */
131  }
132 
133  /* place the exits. */
134  if (RANDOM()%2) {
135  maze[cx][cy] = '>';
136  maze[xsize-2][1] = '<';
137  } else {
138  maze[cx][cy] = '<';
139  maze[xsize-2][1] = '>';
140  }
141 
142  return maze;
143 }
void find_top_left_corner(char **maze, int *cx, int *cy)
This starts from within a centered onion layer (or between two layers), and looks up until it finds a...
Definition: square_spiral.c:53
Random map related variables.
Global type definitions and header inclusions.
#define OPT_CENTERED
Centered.
Definition: random_map.h:122
#define RANDOM()
Definition: define.h:679
char ** map_gen_onion(int xsize, int ysize, int option, int layers)
Generates an onion layout.
char ** make_square_spiral_layout(int xsize, int ysize)
Generates a square-spiral layout.
Definition: square_spiral.c:78
#define OPT_NO_DOORS
Place walls insead of doors.
Definition: random_map.h:129
int make_wall(char **maze, int x, int y, int dir)
Cuts the layout horizontally or vertically by a wall with a door.
Definition: random_map.c:644