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.4 by root, Thu Sep 14 22:34:02 2006 UTC vs.
Revision 1.8 by root, Tue Apr 15 03:00:24 2008 UTC

1
1/* 2/*
2 * Expands a layout by 2x in each dimension. 3 * Expands a layout by 2x in each dimension.
3 * H. S. Teoh 4 * H. S. Teoh
4 * -------------------------------------------------------------------------- 5 * --------------------------------------------------------------------------
5 * $Id: expand2x.C,v 1.4 2006/09/14 22:34:02 root Exp $ 6 * $Id: expand2x.C,v 1.8 2008/04/15 03:00:24 root Exp $
6 * 7 *
7 * ALGORITHM 8 * ALGORITHM
8 * 9 *
9 * ... (TBW) 10 * ... (TBW)
10 */ 11 */
11 12
12#include <stdlib.h> /* just in case */ 13#include "global.h"
13#include <expand2x.h> /* use compiler to do sanity check */ 14#include "random_map.h"
14 15#include "rproto.h"
15
16/* PROTOTYPES */
17
18static void expand_misc (char **newlayout, int i, int j, char **layout, int xsize, int ysize);
19static void expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize);
20static void expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize);
21
22
23/* FUNCTIONS */
24
25char **
26expand2x (char **layout, int xsize, int ysize)
27{
28 int i, j;
29 int nxsize = xsize * 2 - 1;
30 int nysize = ysize * 2 - 1;
31
32 /* Allocate new layout */
33 char **newlayout = (char **) calloc (sizeof (char *), nxsize);
34
35 for (i = 0; i < nxsize; i++)
36 {
37 newlayout[i] = (char *) calloc (sizeof (char), nysize);
38 }
39
40 for (i = 0; i < xsize; i++)
41 {
42 for (j = 0; j < ysize; j++)
43 {
44 switch (layout[i][j])
45 {
46 case '#':
47 expand_wall (newlayout, i, j, layout, xsize, ysize);
48 break;
49 case 'D':
50 expand_door (newlayout, i, j, layout, xsize, ysize);
51 break;
52 default:
53 expand_misc (newlayout, i, j, layout, xsize, ysize);
54 }
55 }
56 }
57
58 /* Dump old layout */
59 for (i = 0; i < xsize; i++)
60 {
61 free (layout[i]);
62 }
63 free (layout);
64
65 return newlayout;
66}
67 16
68/* 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
69 * fill up the rest of the 2x2 result with \0: 18 * fill up the rest of the 2x2 result with \0:
70 * X ---> X \0 19 * X ---> X \0
71 * \0 \0 20 * \0 \0
72 */ 21 */
73static void 22static void
74expand_misc (char **newlayout, int i, int j, char **layout, int xsize, int ysize) 23expand_misc (Layout newlayout, int i, int j, Layout layout)
75{ 24{
76 newlayout[i * 2][j * 2] = layout[i][j]; 25 newlayout[i * 2][j * 2] = layout[i][j];
77 /* (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
78 * for us.) */ 27 * for us.) */
79} 28}
84 * 2 match on (i, j+1) 33 * 2 match on (i, j+1)
85 * 4 match on (i+1, j+1) 34 * 4 match on (i+1, j+1)
86 * and the possible combinations thereof. 35 * and the possible combinations thereof.
87 */ 36 */
88static int 37static int
89calc_pattern (char ch, char **layout, int i, int j, int xsize, int ysize) 38calc_pattern (char ch, Layout layout, int i, int j)
90{ 39{
91 int pattern = 0; 40 int pattern = 0;
92 41
93 if (i + 1 < xsize && layout[i + 1][j] == ch) 42 if (i + 1 < layout->w && layout[i + 1][j] == ch)
94 pattern |= 1; 43 pattern |= 1;
95 44
96 if (j + 1 < ysize) 45 if (j + 1 < layout->h)
97 { 46 {
98 if (layout[i][j + 1] == ch) 47 if (layout[i][j + 1] == ch)
99 pattern |= 2; 48 pattern |= 2;
49
100 if (i + 1 < xsize && layout[i + 1][j + 1] == ch) 50 if (i + 1 < layout->w && layout[i + 1][j + 1] == ch)
101 pattern |= 4; 51 pattern |= 4;
102 } 52 }
103 53
104 return pattern; 54 return pattern;
105} 55}
107/* 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
108 * 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
109 * walls. 59 * walls.
110 */ 60 */
111static void 61static void
112expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize) 62expand_wall (Layout newlayout, int i, int j, Layout layout)
113{ 63{
114 int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); 64 int wall_pattern = calc_pattern ('#', layout, i, j);
115 int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); 65 int door_pattern = calc_pattern ('D', layout, i, j);
116 int both_pattern = wall_pattern | door_pattern; 66 int both_pattern = wall_pattern | door_pattern;
117 67
118 newlayout[i * 2][j * 2] = '#'; 68 newlayout[i * 2][j * 2] = '#';
119 if (i + 1 < xsize) 69
70 if (i + 1 < layout->w)
120 { 71 {
121 if (both_pattern & 1) 72 if (both_pattern & 1)
122 { /* join walls/doors to the right */ 73 { /* join walls/doors to the right */
123
124/* newlayout[i*2+1][j*2] = '#'; */ 74/* newlayout[i*2+1][j*2] = '#'; */
125 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; 75 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j];
126 } 76 }
127 } 77 }
128 78
129 if (j + 1 < ysize) 79 if (j + 1 < layout->h)
130 { 80 {
131 if (both_pattern & 2) 81 if (both_pattern & 2)
132 { /* join walls/doors to the bottom */ 82 { /* join walls/doors to the bottom */
133
134/* newlayout[i*2][j*2+1] = '#'; */ 83/* newlayout[i*2][j*2+1] = '#'; */
135 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; 84 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1];
136 } 85 }
137 86
138 if (wall_pattern == 7) 87 if (wall_pattern == 7)
146/* 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
147 * adjacent walls. Note that it will also presumptuously delete (ignore) doors 96 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
148 * that it doesn't know how to correctly expand. 97 * that it doesn't know how to correctly expand.
149 */ 98 */
150static void 99static void
151expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize) 100expand_door (Layout newlayout, int i, int j, Layout layout)
152{ 101{
153 int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); 102 int wall_pattern = calc_pattern ('#', layout, i, j);
154 int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); 103 int door_pattern = calc_pattern ('D', layout, i, j);
155 int join_pattern; 104 int join_pattern;
156 105
157 /* Doors "like" to connect to walls more than other doors. If there is 106 /* Doors "like" to connect to walls more than other doors. If there is
158 * a wall and another door, this door will connect to the wall and 107 * a wall and another door, this door will connect to the wall and
159 * disconnect from the other door. */ 108 * disconnect from the other door. */
160 if (wall_pattern & 3) 109 if (wall_pattern & 3)
161 {
162 join_pattern = wall_pattern; 110 join_pattern = wall_pattern;
163 }
164 else 111 else
165 {
166 join_pattern = door_pattern; 112 join_pattern = door_pattern;
167 }
168 113
169 newlayout[i * 2][j * 2] = 'D'; 114 newlayout[i * 2][j * 2] = 'D';
170 if (i + 1 < xsize) 115
171 { 116 if (i + 1 < layout->w)
172 if (join_pattern & 1) 117 if (join_pattern & 1)
173 { /* there is a door/wall to the right */ 118 /* there is a door/wall to the right */
174 newlayout[i * 2 + 1][j * 2] = 'D'; 119 newlayout[i * 2 + 1][j * 2] = 'D';
120
121 if (j + 1 < layout->h)
122 if (join_pattern & 2)
123 /* there is a door/wall below */
124 newlayout[i * 2][j * 2 + 1] = 'D';
125}
126
127void
128expand2x (Layout layout)
129{
130 Layout newlayout (layout->w * 2 - 1, layout->h * 2 - 1);
131 newlayout->clear ();
132
133 for (int i = 0; i < layout->w; i++)
134 for (int j = 0; j < layout->h; j++)
135 switch (layout[i][j])
136 {
137 case '#': expand_wall (newlayout, i, j, layout); break;
138 case 'D': expand_door (newlayout, i, j, layout); break;
139 default: expand_misc (newlayout, i, j, layout); break;
175 } 140 }
176 }
177 141
178 if (j + 1 < ysize) 142 layout.swap (newlayout);
179 { 143 newlayout.free ();
180 if (join_pattern & 2)
181 { /* there is a door/wall below */
182 newlayout[i * 2][j * 2 + 1] = 'D';
183 }
184 }
185} 144}
145

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines