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.7 2008/04/14 22:41:17 root Exp $ |
6 | * $Id: expand2x.C,v 1.8 2008/04/15 03:00:24 root Exp $ |
7 | * |
7 | * |
8 | * ALGORITHM |
8 | * ALGORITHM |
9 | * |
9 | * |
10 | * ... (TBW) |
10 | * ... (TBW) |
11 | */ |
11 | */ |
… | |
… | |
18 | * fill up the rest of the 2x2 result with \0: |
18 | * fill up the rest of the 2x2 result with \0: |
19 | * X ---> X \0 |
19 | * X ---> X \0 |
20 | * \0 \0 |
20 | * \0 \0 |
21 | */ |
21 | */ |
22 | static void |
22 | static void |
23 | expand_misc (Maze newlayout, int i, int j, Maze layout) |
23 | expand_misc (Layout newlayout, int i, int j, Layout layout) |
24 | { |
24 | { |
25 | newlayout[i * 2][j * 2] = layout[i][j]; |
25 | newlayout[i * 2][j * 2] = layout[i][j]; |
26 | /* (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 |
27 | * for us.) */ |
27 | * for us.) */ |
28 | } |
28 | } |
… | |
… | |
33 | * 2 match on (i, j+1) |
33 | * 2 match on (i, j+1) |
34 | * 4 match on (i+1, j+1) |
34 | * 4 match on (i+1, j+1) |
35 | * and the possible combinations thereof. |
35 | * and the possible combinations thereof. |
36 | */ |
36 | */ |
37 | static int |
37 | static int |
38 | calc_pattern (char ch, Maze layout, int i, int j) |
38 | calc_pattern (char ch, Layout layout, int i, int j) |
39 | { |
39 | { |
40 | int pattern = 0; |
40 | int pattern = 0; |
41 | |
41 | |
42 | if (i + 1 < layout->w && layout[i + 1][j] == ch) |
42 | if (i + 1 < layout->w && layout[i + 1][j] == ch) |
43 | pattern |= 1; |
43 | pattern |= 1; |
44 | |
44 | |
45 | if (j + 1 < layout->h) |
45 | if (j + 1 < layout->h) |
46 | { |
46 | { |
47 | if (layout[i][j + 1] == ch) |
47 | if (layout[i][j + 1] == ch) |
48 | pattern |= 2; |
48 | pattern |= 2; |
|
|
49 | |
49 | if (i + 1 < layout->w && layout[i + 1][j + 1] == ch) |
50 | if (i + 1 < layout->w && layout[i + 1][j + 1] == ch) |
50 | pattern |= 4; |
51 | pattern |= 4; |
51 | } |
52 | } |
52 | |
53 | |
53 | return pattern; |
54 | return pattern; |
… | |
… | |
56 | /* Expand a wall. This function will try to sensibly connect the resulting |
57 | /* Expand a wall. This function will try to sensibly connect the resulting |
57 | * wall to adjacent wall squares, so that the result won't have disconnected |
58 | * wall to adjacent wall squares, so that the result won't have disconnected |
58 | * walls. |
59 | * walls. |
59 | */ |
60 | */ |
60 | static void |
61 | static void |
61 | expand_wall (Maze newlayout, int i, int j, Maze layout) |
62 | expand_wall (Layout newlayout, int i, int j, Layout layout) |
62 | { |
63 | { |
63 | int wall_pattern = calc_pattern ('#', layout, i, j); |
64 | int wall_pattern = calc_pattern ('#', layout, i, j); |
64 | int door_pattern = calc_pattern ('D', layout, i, j); |
65 | int door_pattern = calc_pattern ('D', layout, i, j); |
65 | int both_pattern = wall_pattern | door_pattern; |
66 | int both_pattern = wall_pattern | door_pattern; |
66 | |
67 | |
… | |
… | |
94 | /* This function will try to sensibly connect doors so that they meet up with |
95 | /* This function will try to sensibly connect doors so that they meet up with |
95 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
96 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
96 | * that it doesn't know how to correctly expand. |
97 | * that it doesn't know how to correctly expand. |
97 | */ |
98 | */ |
98 | static void |
99 | static void |
99 | expand_door (Maze newlayout, int i, int j, Maze layout) |
100 | expand_door (Layout newlayout, int i, int j, Layout layout) |
100 | { |
101 | { |
101 | int wall_pattern = calc_pattern ('#', layout, i, j); |
102 | int wall_pattern = calc_pattern ('#', layout, i, j); |
102 | int door_pattern = calc_pattern ('D', layout, i, j); |
103 | int door_pattern = calc_pattern ('D', layout, i, j); |
103 | int join_pattern; |
104 | int join_pattern; |
104 | |
105 | |
… | |
… | |
122 | /* there is a door/wall below */ |
123 | /* there is a door/wall below */ |
123 | newlayout[i * 2][j * 2 + 1] = 'D'; |
124 | newlayout[i * 2][j * 2 + 1] = 'D'; |
124 | } |
125 | } |
125 | |
126 | |
126 | void |
127 | void |
127 | expand2x (Maze layout) |
128 | expand2x (Layout layout) |
128 | { |
129 | { |
129 | Maze newlayout (layout->w * 2 - 1, layout->h * 2 - 1); |
130 | Layout newlayout (layout->w * 2 - 1, layout->h * 2 - 1); |
130 | newlayout->clear (); |
131 | newlayout->clear (); |
131 | |
132 | |
132 | for (int i = 0; i < layout->w; i++) |
133 | for (int i = 0; i < layout->w; i++) |
133 | for (int j = 0; j < layout->h; j++) |
134 | for (int j = 0; j < layout->h; j++) |
134 | switch (layout[i][j]) |
135 | switch (layout[i][j]) |
… | |
… | |
137 | case 'D': expand_door (newlayout, i, j, layout); break; |
138 | case 'D': expand_door (newlayout, i, j, layout); break; |
138 | default: expand_misc (newlayout, i, j, layout); break; |
139 | default: expand_misc (newlayout, i, j, layout); break; |
139 | } |
140 | } |
140 | |
141 | |
141 | layout.swap (newlayout); |
142 | layout.swap (newlayout); |
|
|
143 | newlayout.free (); |
142 | } |
144 | } |
143 | |
145 | |