Crossfire Server, Branches 1.12  R18729
expand2x.c
Go to the documentation of this file.
1 /*
2  * --------------------------------------------------------------------------
3  * $Id: expand2x.c 11578 2009-02-23 22:02:27Z lalo $
4  *
5  * ALGORITHM
6  *
7  * ... (TBW)
8  */
9 
17 #include <stdlib.h> /* just in case */
18 #include <expand2x.h> /* use compiler to do sanity check */
19 
20 
21 /* PROTOTYPES */
22 
23 static void expand_misc(char **newlayout, int i, int j, char **layout);
24 static void expand_wall(char **newlayout, int i, int j, char **layout, int xsize, int ysize);
25 static void expand_door(char **newlayout, int i, int j, char **layout, int xsize, int ysize);
26 
27 /* FUNCTIONS */
28 
39 char **expand2x(char **layout, int xsize, int ysize) {
40  int i, j;
41  int nxsize = xsize*2-1;
42  int nysize = ysize*2-1;
43 
44  /* Allocate new layout */
45  char **newlayout = (char **)calloc(sizeof(char *), nxsize);
46  for (i = 0; i < nxsize; i++) {
47  newlayout[i] = (char *)calloc(sizeof(char), nysize);
48  }
49 
50  for (i = 0; i < xsize; i++) {
51  for (j = 0; j < ysize; j++) {
52  switch (layout[i][j]) {
53  case '#':
54  expand_wall(newlayout, i, j, layout, xsize, ysize);
55  break;
56 
57  case 'D':
58  expand_door(newlayout, i, j, layout, xsize, ysize);
59  break;
60 
61  default:
62  expand_misc(newlayout, i, j, layout);
63  }
64  }
65  }
66 
67  /* Dump old layout */
68  for (i = 0; i < xsize; i++) {
69  free(layout[i]);
70  }
71  free(layout);
72 
73  return newlayout;
74 }
75 
89 static void expand_misc(char **newlayout, int i, int j, char **layout) {
90  newlayout[i*2][j*2] = layout[i][j];
91  /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that
92  * for us.) */
93 }
94 
115 static int calc_pattern(char ch, char **layout, int i, int j, int xsize, int ysize) {
116  int pattern = 0;
117 
118  if (i+1 < xsize && layout[i+1][j] == ch)
119  pattern |= 1;
120 
121  if (j+1 < ysize) {
122  if (layout[i][j+1] == ch)
123  pattern |= 2;
124  if (i+1 < xsize && layout[i+1][j+1] == ch)
125  pattern |= 4;
126  }
127 
128  return pattern;
129 }
130 
146 static void expand_wall(char **newlayout, int i, int j, char **layout, int xsize, int ysize) {
147  int wall_pattern = calc_pattern('#', layout, i, j, xsize, ysize);
148  int door_pattern = calc_pattern('D', layout, i, j, xsize, ysize);
149  int both_pattern = wall_pattern|door_pattern;
150 
151  newlayout[i*2][j*2] = '#';
152  if (i+1 < xsize) {
153  if (both_pattern&1) {
154  /* join walls/doors to the right */
155  newlayout[i*2+1][j*2] = layout[i+1][j];
156  }
157  }
158 
159  if (j+1 < ysize) {
160  if (both_pattern&2) {
161  /* join walls/doors to the bottom */
162  newlayout[i*2][j*2+1] = layout[i][j+1];
163  }
164 
165  if (wall_pattern == 7) {
166  /* if orig layout is a 2x2 wall block,
167  * we fill the result with walls. */
168  newlayout[i*2+1][j*2+1] = '#';
169  }
170  }
171 }
172 
188 static void expand_door(char **newlayout, int i, int j, char **layout, int xsize, int ysize) {
189  int wall_pattern = calc_pattern('#', layout, i, j, xsize, ysize);
190  int door_pattern = calc_pattern('D', layout, i, j, xsize, ysize);
191  int join_pattern;
192 
193  /* Doors "like" to connect to walls more than other doors. If there is
194  * a wall and another door, this door will connect to the wall and
195  * disconnect from the other door. */
196  if (wall_pattern&3) {
197  join_pattern = wall_pattern;
198  } else {
199  join_pattern = door_pattern;
200  }
201 
202  newlayout[i*2][j*2] = 'D';
203  if (i+1 < xsize) {
204  if (join_pattern&1) {
205  /* there is a door/wall to the right */
206  newlayout[i*2+1][j*2] = 'D';
207  }
208  }
209 
210  if (j+1 < ysize) {
211  if (join_pattern&2) {
212  /* there is a door/wall below */
213  newlayout[i*2][j*2+1] = 'D';
214  }
215  }
216 }
static void expand_wall(char **newlayout, int i, int j, char **layout, int xsize, int ysize)
Definition: expand2x.c:146
static int calc_pattern(char ch, char **layout, int i, int j, int xsize, int ysize)
Definition: expand2x.c:115
char ** expand2x(char **layout, int xsize, int ysize)
Definition: expand2x.c:39
static void expand_door(char **newlayout, int i, int j, char **layout, int xsize, int ysize)
Definition: expand2x.c:188
static void expand_misc(char **newlayout, int i, int j, char **layout)
Definition: expand2x.c:89