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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines