1 | |
1 | |
2 | /* |
2 | /* |
3 | * Expands a layout by 2x in each dimension. |
3 | * Expands a layout by 2x in each dimension. |
4 | * H. S. Teoh |
4 | * H. S. Teoh |
5 | * -------------------------------------------------------------------------- |
5 | * -------------------------------------------------------------------------- |
6 | * $Id: expand2x.C,v 1.5 2006/12/31 19:02:24 root Exp $ |
6 | * $Id: expand2x.C,v 1.6 2008/04/11 21:09:53 root Exp $ |
7 | * |
7 | * |
8 | * ALGORITHM |
8 | * ALGORITHM |
9 | * |
9 | * |
10 | * ... (TBW) |
10 | * ... (TBW) |
11 | */ |
11 | */ |
12 | |
12 | |
13 | #include <stdlib.h> /* just in case */ |
13 | #include "global.h" |
14 | #include <expand2x.h> /* use compiler to do sanity check */ |
14 | #include "random_map.h" |
15 | |
15 | #include "rproto.h" |
16 | |
16 | |
17 | /* PROTOTYPES */ |
17 | /* PROTOTYPES */ |
18 | |
|
|
19 | static void expand_misc (char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
18 | static void expand_misc (char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
20 | static void expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
19 | static void expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
21 | static void expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
20 | static void expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
22 | |
21 | |
23 | |
|
|
24 | /* FUNCTIONS */ |
22 | /* FUNCTIONS */ |
25 | |
23 | Maze |
26 | char ** |
|
|
27 | expand2x (char **layout, int xsize, int ysize) |
24 | expand2x (Maze layout, int xsize, int ysize) |
28 | { |
25 | { |
29 | int i, j; |
26 | int i, j; |
30 | int nxsize = xsize * 2 - 1; |
27 | int nxsize = xsize * 2 - 1; |
31 | int nysize = ysize * 2 - 1; |
28 | int nysize = ysize * 2 - 1; |
32 | |
29 | |
33 | /* Allocate new layout */ |
30 | Maze newlayout (nxsize, nysize); |
34 | char **newlayout = (char **) calloc (sizeof (char *), nxsize); |
|
|
35 | |
|
|
36 | for (i = 0; i < nxsize; i++) |
|
|
37 | { |
|
|
38 | newlayout[i] = (char *) calloc (sizeof (char), nysize); |
|
|
39 | } |
|
|
40 | |
31 | |
41 | for (i = 0; i < xsize; i++) |
32 | for (i = 0; i < xsize; i++) |
42 | { |
|
|
43 | for (j = 0; j < ysize; j++) |
33 | for (j = 0; j < ysize; j++) |
|
|
34 | switch (layout[i][j]) |
44 | { |
35 | { |
45 | switch (layout[i][j]) |
|
|
46 | { |
|
|
47 | case '#': |
36 | case '#': |
48 | expand_wall (newlayout, i, j, layout, xsize, ysize); |
37 | expand_wall (newlayout, i, j, layout, xsize, ysize); |
49 | break; |
38 | break; |
50 | case 'D': |
39 | case 'D': |
51 | expand_door (newlayout, i, j, layout, xsize, ysize); |
40 | expand_door (newlayout, i, j, layout, xsize, ysize); |
52 | break; |
41 | break; |
53 | default: |
42 | default: |
54 | expand_misc (newlayout, i, j, layout, xsize, ysize); |
43 | expand_misc (newlayout, i, j, layout, xsize, ysize); |
55 | } |
|
|
56 | } |
44 | } |
57 | } |
|
|
58 | |
45 | |
59 | /* Dump old layout */ |
46 | layout.free (); |
60 | for (i = 0; i < xsize; i++) |
|
|
61 | { |
|
|
62 | free (layout[i]); |
|
|
63 | } |
|
|
64 | free (layout); |
|
|
65 | |
47 | |
66 | return newlayout; |
48 | return newlayout; |
67 | } |
49 | } |
68 | |
50 | |
69 | /* Copy the old tile X into the new one at location (i*2, j*2) and |
51 | /* Copy the old tile X into the new one at location (i*2, j*2) and |
… | |
… | |
157 | |
139 | |
158 | /* Doors "like" to connect to walls more than other doors. If there is |
140 | /* Doors "like" to connect to walls more than other doors. If there is |
159 | * a wall and another door, this door will connect to the wall and |
141 | * a wall and another door, this door will connect to the wall and |
160 | * disconnect from the other door. */ |
142 | * disconnect from the other door. */ |
161 | if (wall_pattern & 3) |
143 | if (wall_pattern & 3) |
162 | { |
|
|
163 | join_pattern = wall_pattern; |
144 | join_pattern = wall_pattern; |
164 | } |
|
|
165 | else |
145 | else |
166 | { |
|
|
167 | join_pattern = door_pattern; |
146 | join_pattern = door_pattern; |
168 | } |
|
|
169 | |
147 | |
170 | newlayout[i * 2][j * 2] = 'D'; |
148 | newlayout[i * 2][j * 2] = 'D'; |
|
|
149 | |
171 | if (i + 1 < xsize) |
150 | if (i + 1 < xsize) |
172 | { |
|
|
173 | if (join_pattern & 1) |
151 | if (join_pattern & 1) |
174 | { /* there is a door/wall to the right */ |
152 | /* there is a door/wall to the right */ |
175 | newlayout[i * 2 + 1][j * 2] = 'D'; |
153 | newlayout[i * 2 + 1][j * 2] = 'D'; |
176 | } |
|
|
177 | } |
|
|
178 | |
154 | |
179 | if (j + 1 < ysize) |
155 | if (j + 1 < ysize) |
180 | { |
|
|
181 | if (join_pattern & 2) |
156 | if (join_pattern & 2) |
182 | { /* there is a door/wall below */ |
157 | /* there is a door/wall below */ |
183 | newlayout[i * 2][j * 2 + 1] = 'D'; |
158 | newlayout[i * 2][j * 2 + 1] = 'D'; |
184 | } |
|
|
185 | } |
|
|
186 | } |
159 | } |