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.7 2008/04/14 22:41:17 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 | |
|
|
17 | /* PROTOTYPES */ |
|
|
18 | |
|
|
19 | 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); |
|
|
21 | static void expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
|
|
22 | |
|
|
23 | |
|
|
24 | /* FUNCTIONS */ |
|
|
25 | |
|
|
26 | char ** |
|
|
27 | expand2x (char **layout, int xsize, int ysize) |
|
|
28 | { |
|
|
29 | int i, j; |
|
|
30 | int nxsize = xsize * 2 - 1; |
|
|
31 | int nysize = ysize * 2 - 1; |
|
|
32 | |
|
|
33 | /* Allocate new layout */ |
|
|
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 | |
|
|
41 | for (i = 0; i < xsize; i++) |
|
|
42 | { |
|
|
43 | for (j = 0; j < ysize; j++) |
|
|
44 | { |
|
|
45 | switch (layout[i][j]) |
|
|
46 | { |
|
|
47 | case '#': |
|
|
48 | expand_wall (newlayout, i, j, layout, xsize, ysize); |
|
|
49 | break; |
|
|
50 | case 'D': |
|
|
51 | expand_door (newlayout, i, j, layout, xsize, ysize); |
|
|
52 | break; |
|
|
53 | default: |
|
|
54 | expand_misc (newlayout, i, j, layout, xsize, ysize); |
|
|
55 | } |
|
|
56 | } |
|
|
57 | } |
|
|
58 | |
|
|
59 | /* Dump old layout */ |
|
|
60 | for (i = 0; i < xsize; i++) |
|
|
61 | { |
|
|
62 | free (layout[i]); |
|
|
63 | } |
|
|
64 | free (layout); |
|
|
65 | |
|
|
66 | return newlayout; |
|
|
67 | } |
|
|
68 | |
16 | |
69 | /* Copy the old tile X into the new one at location (i*2, j*2) and |
17 | /* Copy the old tile X into the new one at location (i*2, j*2) and |
70 | * fill up the rest of the 2x2 result with \0: |
18 | * fill up the rest of the 2x2 result with \0: |
71 | * X ---> X \0 |
19 | * X ---> X \0 |
72 | * \0 \0 |
20 | * \0 \0 |
73 | */ |
21 | */ |
74 | static void |
22 | static void |
75 | expand_misc (char **newlayout, int i, int j, char **layout, int xsize, int ysize) |
23 | expand_misc (Maze newlayout, int i, int j, Maze layout) |
76 | { |
24 | { |
77 | newlayout[i * 2][j * 2] = layout[i][j]; |
25 | newlayout[i * 2][j * 2] = layout[i][j]; |
78 | /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
26 | /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
79 | * for us.) */ |
27 | * for us.) */ |
80 | } |
28 | } |
… | |
… | |
85 | * 2 match on (i, j+1) |
33 | * 2 match on (i, j+1) |
86 | * 4 match on (i+1, j+1) |
34 | * 4 match on (i+1, j+1) |
87 | * and the possible combinations thereof. |
35 | * and the possible combinations thereof. |
88 | */ |
36 | */ |
89 | static int |
37 | static int |
90 | calc_pattern (char ch, char **layout, int i, int j, int xsize, int ysize) |
38 | calc_pattern (char ch, Maze layout, int i, int j) |
91 | { |
39 | { |
92 | int pattern = 0; |
40 | int pattern = 0; |
93 | |
41 | |
94 | if (i + 1 < xsize && layout[i + 1][j] == ch) |
42 | if (i + 1 < layout->w && layout[i + 1][j] == ch) |
95 | pattern |= 1; |
43 | pattern |= 1; |
96 | |
44 | |
97 | if (j + 1 < ysize) |
45 | if (j + 1 < layout->h) |
98 | { |
46 | { |
99 | if (layout[i][j + 1] == ch) |
47 | if (layout[i][j + 1] == ch) |
100 | pattern |= 2; |
48 | pattern |= 2; |
101 | if (i + 1 < xsize && layout[i + 1][j + 1] == ch) |
49 | if (i + 1 < layout->w && layout[i + 1][j + 1] == ch) |
102 | pattern |= 4; |
50 | pattern |= 4; |
103 | } |
51 | } |
104 | |
52 | |
105 | return pattern; |
53 | return pattern; |
106 | } |
54 | } |
… | |
… | |
108 | /* Expand a wall. This function will try to sensibly connect the resulting |
56 | /* Expand a wall. This function will try to sensibly connect the resulting |
109 | * wall to adjacent wall squares, so that the result won't have disconnected |
57 | * wall to adjacent wall squares, so that the result won't have disconnected |
110 | * walls. |
58 | * walls. |
111 | */ |
59 | */ |
112 | static void |
60 | static void |
113 | expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize) |
61 | expand_wall (Maze newlayout, int i, int j, Maze layout) |
114 | { |
62 | { |
115 | int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); |
63 | int wall_pattern = calc_pattern ('#', layout, i, j); |
116 | int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); |
64 | int door_pattern = calc_pattern ('D', layout, i, j); |
117 | int both_pattern = wall_pattern | door_pattern; |
65 | int both_pattern = wall_pattern | door_pattern; |
118 | |
66 | |
119 | newlayout[i * 2][j * 2] = '#'; |
67 | newlayout[i * 2][j * 2] = '#'; |
120 | if (i + 1 < xsize) |
68 | |
|
|
69 | if (i + 1 < layout->w) |
121 | { |
70 | { |
122 | if (both_pattern & 1) |
71 | if (both_pattern & 1) |
123 | { /* join walls/doors to the right */ |
72 | { /* join walls/doors to the right */ |
124 | |
|
|
125 | /* newlayout[i*2+1][j*2] = '#'; */ |
73 | /* newlayout[i*2+1][j*2] = '#'; */ |
126 | newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; |
74 | newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; |
127 | } |
75 | } |
128 | } |
76 | } |
129 | |
77 | |
130 | if (j + 1 < ysize) |
78 | if (j + 1 < layout->h) |
131 | { |
79 | { |
132 | if (both_pattern & 2) |
80 | if (both_pattern & 2) |
133 | { /* join walls/doors to the bottom */ |
81 | { /* join walls/doors to the bottom */ |
134 | |
|
|
135 | /* newlayout[i*2][j*2+1] = '#'; */ |
82 | /* newlayout[i*2][j*2+1] = '#'; */ |
136 | newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; |
83 | newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; |
137 | } |
84 | } |
138 | |
85 | |
139 | if (wall_pattern == 7) |
86 | if (wall_pattern == 7) |
… | |
… | |
147 | /* This function will try to sensibly connect doors so that they meet up with |
94 | /* This function will try to sensibly connect doors so that they meet up with |
148 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
95 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
149 | * that it doesn't know how to correctly expand. |
96 | * that it doesn't know how to correctly expand. |
150 | */ |
97 | */ |
151 | static void |
98 | static void |
152 | expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize) |
99 | expand_door (Maze newlayout, int i, int j, Maze layout) |
153 | { |
100 | { |
154 | int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); |
101 | int wall_pattern = calc_pattern ('#', layout, i, j); |
155 | int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); |
102 | int door_pattern = calc_pattern ('D', layout, i, j); |
156 | int join_pattern; |
103 | int join_pattern; |
157 | |
104 | |
158 | /* Doors "like" to connect to walls more than other doors. If there is |
105 | /* 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 |
106 | * a wall and another door, this door will connect to the wall and |
160 | * disconnect from the other door. */ |
107 | * disconnect from the other door. */ |
161 | if (wall_pattern & 3) |
108 | if (wall_pattern & 3) |
162 | { |
|
|
163 | join_pattern = wall_pattern; |
109 | join_pattern = wall_pattern; |
164 | } |
|
|
165 | else |
110 | else |
166 | { |
|
|
167 | join_pattern = door_pattern; |
111 | join_pattern = door_pattern; |
168 | } |
|
|
169 | |
112 | |
170 | newlayout[i * 2][j * 2] = 'D'; |
113 | newlayout[i * 2][j * 2] = 'D'; |
171 | if (i + 1 < xsize) |
114 | |
172 | { |
115 | if (i + 1 < layout->w) |
173 | if (join_pattern & 1) |
116 | if (join_pattern & 1) |
174 | { /* there is a door/wall to the right */ |
117 | /* there is a door/wall to the right */ |
175 | newlayout[i * 2 + 1][j * 2] = 'D'; |
118 | newlayout[i * 2 + 1][j * 2] = 'D'; |
|
|
119 | |
|
|
120 | if (j + 1 < layout->h) |
|
|
121 | if (join_pattern & 2) |
|
|
122 | /* there is a door/wall below */ |
|
|
123 | newlayout[i * 2][j * 2 + 1] = 'D'; |
|
|
124 | } |
|
|
125 | |
|
|
126 | void |
|
|
127 | expand2x (Maze layout) |
|
|
128 | { |
|
|
129 | Maze newlayout (layout->w * 2 - 1, layout->h * 2 - 1); |
|
|
130 | newlayout->clear (); |
|
|
131 | |
|
|
132 | for (int i = 0; i < layout->w; i++) |
|
|
133 | for (int j = 0; j < layout->h; j++) |
|
|
134 | switch (layout[i][j]) |
|
|
135 | { |
|
|
136 | case '#': expand_wall (newlayout, i, j, layout); break; |
|
|
137 | case 'D': expand_door (newlayout, i, j, layout); break; |
|
|
138 | default: expand_misc (newlayout, i, j, layout); break; |
176 | } |
139 | } |
177 | } |
|
|
178 | |
140 | |
179 | if (j + 1 < ysize) |
141 | layout.swap (newlayout); |
180 | { |
|
|
181 | if (join_pattern & 2) |
|
|
182 | { /* there is a door/wall below */ |
|
|
183 | newlayout[i * 2][j * 2 + 1] = 'D'; |
|
|
184 | } |
|
|
185 | } |
|
|
186 | } |
142 | } |
|
|
143 | |