ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/expand2x.C
(Generate patch)

Comparing deliantra/server/random_maps/expand2x.C (file contents):
Revision 1.9 by root, Sat Nov 7 18:32:45 2009 UTC vs.
Revision 1.10 by root, Thu Jul 1 01:22:44 2010 UTC

23 23
24/* 24/*
25 * Expands a layout by 2x in each dimension. 25 * Expands a layout by 2x in each dimension.
26 * H. S. Teoh 26 * H. S. Teoh
27 * -------------------------------------------------------------------------- 27 * --------------------------------------------------------------------------
28 * $Id: expand2x.C,v 1.9 2009/11/07 18:32:45 root Exp $ 28 * $Id: expand2x.C,v 1.10 2010/07/01 01:22:44 root Exp $
29 * 29 *
30 * ALGORITHM 30 * ALGORITHM
31 * 31 *
32 * ... (TBW) 32 * ... (TBW)
33 */ 33 */
40 * fill up the rest of the 2x2 result with \0: 40 * fill up the rest of the 2x2 result with \0:
41 * X ---> X \0 41 * X ---> X \0
42 * \0 \0 42 * \0 \0
43 */ 43 */
44static void 44static void
45expand_misc (Layout newlayout, int i, int j, Layout layout) 45expand_misc (Layout &newlayout, int i, int j, Layout &layout)
46{ 46{
47 newlayout[i * 2][j * 2] = layout[i][j]; 47 newlayout[i * 2][j * 2] = layout[i][j];
48 /* (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
49 * for us.) */ 49 * for us.) */
50} 50}
55 * 2 match on (i, j+1) 55 * 2 match on (i, j+1)
56 * 4 match on (i+1, j+1) 56 * 4 match on (i+1, j+1)
57 * and the possible combinations thereof. 57 * and the possible combinations thereof.
58 */ 58 */
59static int 59static int
60calc_pattern (char ch, Layout layout, int i, int j) 60calc_pattern (char ch, Layout &layout, int i, int j)
61{ 61{
62 int pattern = 0; 62 int pattern = 0;
63 63
64 if (i + 1 < layout->w && layout[i + 1][j] == ch) 64 if (i + 1 < layout.w && layout[i + 1][j] == ch)
65 pattern |= 1; 65 pattern |= 1;
66 66
67 if (j + 1 < layout->h) 67 if (j + 1 < layout.h)
68 { 68 {
69 if (layout[i][j + 1] == ch) 69 if (layout[i][j + 1] == ch)
70 pattern |= 2; 70 pattern |= 2;
71 71
72 if (i + 1 < layout->w && layout[i + 1][j + 1] == ch) 72 if (i + 1 < layout.w && layout[i + 1][j + 1] == ch)
73 pattern |= 4; 73 pattern |= 4;
74 } 74 }
75 75
76 return pattern; 76 return pattern;
77} 77}
79/* 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
80 * 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
81 * walls. 81 * walls.
82 */ 82 */
83static void 83static void
84expand_wall (Layout newlayout, int i, int j, Layout layout) 84expand_wall (Layout &newlayout, int i, int j, Layout &layout)
85{ 85{
86 int wall_pattern = calc_pattern ('#', layout, i, j); 86 int wall_pattern = calc_pattern ('#', layout, i, j);
87 int door_pattern = calc_pattern ('D', layout, i, j); 87 int door_pattern = calc_pattern ('D', layout, i, j);
88 int both_pattern = wall_pattern | door_pattern; 88 int both_pattern = wall_pattern | door_pattern;
89 89
90 newlayout[i * 2][j * 2] = '#'; 90 newlayout[i * 2][j * 2] = '#';
91 91
92 if (i + 1 < layout->w) 92 if (i + 1 < layout.w)
93 { 93 {
94 if (both_pattern & 1) 94 if (both_pattern & 1)
95 { /* join walls/doors to the right */ 95 { /* join walls/doors to the right */
96/* newlayout[i*2+1][j*2] = '#'; */ 96/* newlayout[i*2+1][j*2] = '#'; */
97 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; 97 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j];
98 } 98 }
99 } 99 }
100 100
101 if (j + 1 < layout->h) 101 if (j + 1 < layout.h)
102 { 102 {
103 if (both_pattern & 2) 103 if (both_pattern & 2)
104 { /* join walls/doors to the bottom */ 104 { /* join walls/doors to the bottom */
105/* newlayout[i*2][j*2+1] = '#'; */ 105/* newlayout[i*2][j*2+1] = '#'; */
106 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; 106 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1];
117/* 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
118 * adjacent walls. Note that it will also presumptuously delete (ignore) doors 118 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
119 * that it doesn't know how to correctly expand. 119 * that it doesn't know how to correctly expand.
120 */ 120 */
121static void 121static void
122expand_door (Layout newlayout, int i, int j, Layout layout) 122expand_door (Layout &newlayout, int i, int j, Layout &layout)
123{ 123{
124 int wall_pattern = calc_pattern ('#', layout, i, j); 124 int wall_pattern = calc_pattern ('#', layout, i, j);
125 int door_pattern = calc_pattern ('D', layout, i, j); 125 int door_pattern = calc_pattern ('D', layout, i, j);
126 int join_pattern; 126 int join_pattern;
127 127
133 else 133 else
134 join_pattern = door_pattern; 134 join_pattern = door_pattern;
135 135
136 newlayout[i * 2][j * 2] = 'D'; 136 newlayout[i * 2][j * 2] = 'D';
137 137
138 if (i + 1 < layout->w) 138 if (i + 1 < layout.w)
139 if (join_pattern & 1) 139 if (join_pattern & 1)
140 /* there is a door/wall to the right */ 140 /* there is a door/wall to the right */
141 newlayout[i * 2 + 1][j * 2] = 'D'; 141 newlayout[i * 2 + 1][j * 2] = 'D';
142 142
143 if (j + 1 < layout->h) 143 if (j + 1 < layout.h)
144 if (join_pattern & 2) 144 if (join_pattern & 2)
145 /* there is a door/wall below */ 145 /* there is a door/wall below */
146 newlayout[i * 2][j * 2 + 1] = 'D'; 146 newlayout[i * 2][j * 2 + 1] = 'D';
147} 147}
148 148
149void 149void
150expand2x (Layout layout) 150expand2x (Layout &layout)
151{ 151{
152 Layout newlayout (layout->w * 2 - 1, layout->h * 2 - 1); 152 Layout new_layout (layout.w * 2 - 1, layout.h * 2 - 1);
153 newlayout->clear ();
154 153
154 new_layout.clear ();
155
155 for (int i = 0; i < layout->w; i++) 156 for (int i = 0; i < layout.w; i++)
156 for (int j = 0; j < layout->h; j++) 157 for (int j = 0; j < layout.h; j++)
157 switch (layout[i][j]) 158 switch (layout[i][j])
158 { 159 {
159 case '#': expand_wall (newlayout, i, j, layout); break; 160 case '#': expand_wall (new_layout, i, j, layout); break;
160 case 'D': expand_door (newlayout, i, j, layout); break; 161 case 'D': expand_door (new_layout, i, j, layout); break;
161 default: expand_misc (newlayout, i, j, layout); break; 162 default: expand_misc (new_layout, i, j, layout); break;
162 } 163 }
163 164
164 layout.swap (newlayout); 165 layout.swap (new_layout);
165 newlayout.free ();
166} 166}
167 167

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines