|
|
1 | /* |
|
|
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) |
|
|
6 | * |
|
|
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
|
|
8 | * the terms of the Affero GNU General Public License as published by the |
|
|
9 | * Free Software Foundation, either version 3 of the License, or (at your |
|
|
10 | * option) any later version. |
|
|
11 | * |
|
|
12 | * This program is distributed in the hope that it will be useful, |
|
|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
15 | * GNU General Public License for more details. |
|
|
16 | * |
|
|
17 | * You should have received a copy of the Affero GNU General Public License |
|
|
18 | * and the GNU General Public License along with this program. If not, see |
|
|
19 | * <http://www.gnu.org/licenses/>. |
|
|
20 | * |
|
|
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
|
|
22 | */ |
1 | |
23 | |
2 | /* |
24 | /* |
3 | * Expands a layout by 2x in each dimension. |
25 | * Expands a layout by 2x in each dimension. |
4 | * H. S. Teoh |
26 | * H. S. Teoh |
5 | * -------------------------------------------------------------------------- |
27 | * -------------------------------------------------------------------------- |
6 | * $Id: expand2x.C,v 1.8 2008/04/15 03:00:24 root Exp $ |
28 | * $Id: expand2x.C,v 1.10 2010/07/01 01:22:44 root Exp $ |
7 | * |
29 | * |
8 | * ALGORITHM |
30 | * ALGORITHM |
9 | * |
31 | * |
10 | * ... (TBW) |
32 | * ... (TBW) |
11 | */ |
33 | */ |
… | |
… | |
18 | * fill up the rest of the 2x2 result with \0: |
40 | * fill up the rest of the 2x2 result with \0: |
19 | * X ---> X \0 |
41 | * X ---> X \0 |
20 | * \0 \0 |
42 | * \0 \0 |
21 | */ |
43 | */ |
22 | static void |
44 | static void |
23 | expand_misc (Layout newlayout, int i, int j, Layout layout) |
45 | expand_misc (Layout &newlayout, int i, int j, Layout &layout) |
24 | { |
46 | { |
25 | newlayout[i * 2][j * 2] = layout[i][j]; |
47 | newlayout[i * 2][j * 2] = layout[i][j]; |
26 | /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
48 | /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
27 | * for us.) */ |
49 | * for us.) */ |
28 | } |
50 | } |
… | |
… | |
33 | * 2 match on (i, j+1) |
55 | * 2 match on (i, j+1) |
34 | * 4 match on (i+1, j+1) |
56 | * 4 match on (i+1, j+1) |
35 | * and the possible combinations thereof. |
57 | * and the possible combinations thereof. |
36 | */ |
58 | */ |
37 | static int |
59 | static int |
38 | calc_pattern (char ch, Layout layout, int i, int j) |
60 | calc_pattern (char ch, Layout &layout, int i, int j) |
39 | { |
61 | { |
40 | int pattern = 0; |
62 | int pattern = 0; |
41 | |
63 | |
42 | if (i + 1 < layout->w && layout[i + 1][j] == ch) |
64 | if (i + 1 < layout.w && layout[i + 1][j] == ch) |
43 | pattern |= 1; |
65 | pattern |= 1; |
44 | |
66 | |
45 | if (j + 1 < layout->h) |
67 | if (j + 1 < layout.h) |
46 | { |
68 | { |
47 | if (layout[i][j + 1] == ch) |
69 | if (layout[i][j + 1] == ch) |
48 | pattern |= 2; |
70 | pattern |= 2; |
49 | |
71 | |
50 | if (i + 1 < layout->w && layout[i + 1][j + 1] == ch) |
72 | if (i + 1 < layout.w && layout[i + 1][j + 1] == ch) |
51 | pattern |= 4; |
73 | pattern |= 4; |
52 | } |
74 | } |
53 | |
75 | |
54 | return pattern; |
76 | return pattern; |
55 | } |
77 | } |
… | |
… | |
57 | /* Expand a wall. This function will try to sensibly connect the resulting |
79 | /* Expand a wall. This function will try to sensibly connect the resulting |
58 | * wall to adjacent wall squares, so that the result won't have disconnected |
80 | * wall to adjacent wall squares, so that the result won't have disconnected |
59 | * walls. |
81 | * walls. |
60 | */ |
82 | */ |
61 | static void |
83 | static void |
62 | expand_wall (Layout newlayout, int i, int j, Layout layout) |
84 | expand_wall (Layout &newlayout, int i, int j, Layout &layout) |
63 | { |
85 | { |
64 | int wall_pattern = calc_pattern ('#', layout, i, j); |
86 | int wall_pattern = calc_pattern ('#', layout, i, j); |
65 | int door_pattern = calc_pattern ('D', layout, i, j); |
87 | int door_pattern = calc_pattern ('D', layout, i, j); |
66 | int both_pattern = wall_pattern | door_pattern; |
88 | int both_pattern = wall_pattern | door_pattern; |
67 | |
89 | |
68 | newlayout[i * 2][j * 2] = '#'; |
90 | newlayout[i * 2][j * 2] = '#'; |
69 | |
91 | |
70 | if (i + 1 < layout->w) |
92 | if (i + 1 < layout.w) |
71 | { |
93 | { |
72 | if (both_pattern & 1) |
94 | if (both_pattern & 1) |
73 | { /* join walls/doors to the right */ |
95 | { /* join walls/doors to the right */ |
74 | /* newlayout[i*2+1][j*2] = '#'; */ |
96 | /* newlayout[i*2+1][j*2] = '#'; */ |
75 | newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; |
97 | newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; |
76 | } |
98 | } |
77 | } |
99 | } |
78 | |
100 | |
79 | if (j + 1 < layout->h) |
101 | if (j + 1 < layout.h) |
80 | { |
102 | { |
81 | if (both_pattern & 2) |
103 | if (both_pattern & 2) |
82 | { /* join walls/doors to the bottom */ |
104 | { /* join walls/doors to the bottom */ |
83 | /* newlayout[i*2][j*2+1] = '#'; */ |
105 | /* newlayout[i*2][j*2+1] = '#'; */ |
84 | newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; |
106 | newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; |
… | |
… | |
95 | /* This function will try to sensibly connect doors so that they meet up with |
117 | /* This function will try to sensibly connect doors so that they meet up with |
96 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
118 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
97 | * that it doesn't know how to correctly expand. |
119 | * that it doesn't know how to correctly expand. |
98 | */ |
120 | */ |
99 | static void |
121 | static void |
100 | expand_door (Layout newlayout, int i, int j, Layout layout) |
122 | expand_door (Layout &newlayout, int i, int j, Layout &layout) |
101 | { |
123 | { |
102 | int wall_pattern = calc_pattern ('#', layout, i, j); |
124 | int wall_pattern = calc_pattern ('#', layout, i, j); |
103 | int door_pattern = calc_pattern ('D', layout, i, j); |
125 | int door_pattern = calc_pattern ('D', layout, i, j); |
104 | int join_pattern; |
126 | int join_pattern; |
105 | |
127 | |
… | |
… | |
111 | else |
133 | else |
112 | join_pattern = door_pattern; |
134 | join_pattern = door_pattern; |
113 | |
135 | |
114 | newlayout[i * 2][j * 2] = 'D'; |
136 | newlayout[i * 2][j * 2] = 'D'; |
115 | |
137 | |
116 | if (i + 1 < layout->w) |
138 | if (i + 1 < layout.w) |
117 | if (join_pattern & 1) |
139 | if (join_pattern & 1) |
118 | /* there is a door/wall to the right */ |
140 | /* there is a door/wall to the right */ |
119 | newlayout[i * 2 + 1][j * 2] = 'D'; |
141 | newlayout[i * 2 + 1][j * 2] = 'D'; |
120 | |
142 | |
121 | if (j + 1 < layout->h) |
143 | if (j + 1 < layout.h) |
122 | if (join_pattern & 2) |
144 | if (join_pattern & 2) |
123 | /* there is a door/wall below */ |
145 | /* there is a door/wall below */ |
124 | newlayout[i * 2][j * 2 + 1] = 'D'; |
146 | newlayout[i * 2][j * 2 + 1] = 'D'; |
125 | } |
147 | } |
126 | |
148 | |
127 | void |
149 | void |
128 | expand2x (Layout layout) |
150 | expand2x (Layout &layout) |
129 | { |
151 | { |
130 | Layout newlayout (layout->w * 2 - 1, layout->h * 2 - 1); |
152 | Layout new_layout (layout.w * 2 - 1, layout.h * 2 - 1); |
131 | newlayout->clear (); |
|
|
132 | |
153 | |
|
|
154 | new_layout.clear (); |
|
|
155 | |
133 | for (int i = 0; i < layout->w; i++) |
156 | for (int i = 0; i < layout.w; i++) |
134 | for (int j = 0; j < layout->h; j++) |
157 | for (int j = 0; j < layout.h; j++) |
135 | switch (layout[i][j]) |
158 | switch (layout[i][j]) |
136 | { |
159 | { |
137 | case '#': expand_wall (newlayout, i, j, layout); break; |
160 | case '#': expand_wall (new_layout, i, j, layout); break; |
138 | case 'D': expand_door (newlayout, i, j, layout); break; |
161 | case 'D': expand_door (new_layout, i, j, layout); break; |
139 | default: expand_misc (newlayout, i, j, layout); break; |
162 | default: expand_misc (new_layout, i, j, layout); break; |
140 | } |
163 | } |
141 | |
164 | |
142 | layout.swap (newlayout); |
165 | layout.swap (new_layout); |
143 | newlayout.free (); |
|
|
144 | } |
166 | } |
145 | |
167 | |