| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <expand2x.h> |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
static void expand_misc(char **newlayout, int i, int j, char **layout); |
| 28 |
static void expand_wall(char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
| 29 |
static void expand_door(char **newlayout, int i, int j, char **layout, int xsize, int ysize); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
char **expand2x(char **layout, int xsize, int ysize) |
| 44 |
{ |
| 45 |
int i, j; |
| 46 |
int nxsize = xsize*2-1; |
| 47 |
int nysize = ysize*2-1; |
| 48 |
|
| 49 |
|
| 50 |
char **newlayout = (char **)calloc(sizeof(char *), nxsize); |
| 51 |
for (i = 0; i < nxsize; i++) { |
| 52 |
newlayout[i] = (char *)calloc(sizeof(char), nysize); |
| 53 |
} |
| 54 |
|
| 55 |
for (i = 0; i < xsize; i++) { |
| 56 |
for (j = 0; j < ysize; j++) { |
| 57 |
switch (layout[i][j]) { |
| 58 |
case '#': |
| 59 |
expand_wall(newlayout, i, j, layout, xsize, ysize); |
| 60 |
break; |
| 61 |
|
| 62 |
case 'D': |
| 63 |
expand_door(newlayout, i, j, layout, xsize, ysize); |
| 64 |
break; |
| 65 |
|
| 66 |
default: |
| 67 |
expand_misc(newlayout, i, j, layout); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
for (i = 0; i < xsize; i++) { |
| 74 |
free(layout[i]); |
| 75 |
} |
| 76 |
free(layout); |
| 77 |
|
| 78 |
return newlayout; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
static void expand_misc(char **newlayout, int i, int j, char **layout) |
| 95 |
{ |
| 96 |
newlayout[i*2][j*2] = layout[i][j]; |
| 97 |
|
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static int calc_pattern(char ch, char **layout, int i, int j, int xsize, int ysize) |
| 122 |
{ |
| 123 |
int pattern = 0; |
| 124 |
|
| 125 |
if (i+1 < xsize && layout[i+1][j] == ch) { |
| 126 |
pattern |= 1; |
| 127 |
} |
| 128 |
|
| 129 |
if (j+1 < ysize) { |
| 130 |
if (layout[i][j+1] == ch) { |
| 131 |
pattern |= 2; |
| 132 |
} |
| 133 |
if (i+1 < xsize && layout[i+1][j+1] == ch) { |
| 134 |
pattern |= 4; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return pattern; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static void expand_wall(char **newlayout, int i, int j, char **layout, int xsize, int ysize) |
| 157 |
{ |
| 158 |
int wall_pattern = calc_pattern('#', layout, i, j, xsize, ysize); |
| 159 |
int door_pattern = calc_pattern('D', layout, i, j, xsize, ysize); |
| 160 |
int both_pattern = wall_pattern|door_pattern; |
| 161 |
|
| 162 |
newlayout[i*2][j*2] = '#'; |
| 163 |
if (i+1 < xsize) { |
| 164 |
if (both_pattern&1) { |
| 165 |
|
| 166 |
newlayout[i*2+1][j*2] = layout[i+1][j]; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if (j+1 < ysize) { |
| 171 |
if (both_pattern&2) { |
| 172 |
|
| 173 |
newlayout[i*2][j*2+1] = layout[i][j+1]; |
| 174 |
} |
| 175 |
|
| 176 |
if (wall_pattern == 7) { |
| 177 |
|
| 178 |
|
| 179 |
newlayout[i*2+1][j*2+1] = '#'; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
static void expand_door(char **newlayout, int i, int j, char **layout, int xsize, int ysize) |
| 200 |
{ |
| 201 |
int wall_pattern = calc_pattern('#', layout, i, j, xsize, ysize); |
| 202 |
int door_pattern = calc_pattern('D', layout, i, j, xsize, ysize); |
| 203 |
int join_pattern; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
if (wall_pattern&3) { |
| 209 |
join_pattern = wall_pattern; |
| 210 |
} else { |
| 211 |
join_pattern = door_pattern; |
| 212 |
} |
| 213 |
|
| 214 |
newlayout[i*2][j*2] = 'D'; |
| 215 |
if (i+1 < xsize) { |
| 216 |
if (join_pattern&1) { |
| 217 |
|
| 218 |
newlayout[i*2+1][j*2] = 'D'; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (j+1 < ysize) { |
| 223 |
if (join_pattern&2) { |
| 224 |
|
| 225 |
newlayout[i*2][j*2+1] = 'D'; |
| 226 |
} |
| 227 |
} |
| 228 |
} |