| 1 |
|
| 2 |
/* |
| 3 |
* Expands a layout by 2x in each dimension. |
| 4 |
* H. S. Teoh |
| 5 |
* -------------------------------------------------------------------------- |
| 6 |
* $Id: expand2x.C,v 1.7 2008-04-14 22:41:17 root Exp $ |
| 7 |
* |
| 8 |
* ALGORITHM |
| 9 |
* |
| 10 |
* ... (TBW) |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "global.h" |
| 14 |
#include "random_map.h" |
| 15 |
#include "rproto.h" |
| 16 |
|
| 17 |
/* Copy the old tile X into the new one at location (i*2, j*2) and |
| 18 |
* fill up the rest of the 2x2 result with \0: |
| 19 |
* X ---> X \0 |
| 20 |
* \0 \0 |
| 21 |
*/ |
| 22 |
static void |
| 23 |
expand_misc (Layout newlayout, int i, int j, Layout layout) |
| 24 |
{ |
| 25 |
newlayout[i * 2][j * 2] = layout[i][j]; |
| 26 |
/* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
| 27 |
* for us.) */ |
| 28 |
} |
| 29 |
|
| 30 |
/* Returns a bitmap that represents which squares on the right and bottom |
| 31 |
* edges of a square (i,j) match the given character: |
| 32 |
* 1 match on (i+1, j) |
| 33 |
* 2 match on (i, j+1) |
| 34 |
* 4 match on (i+1, j+1) |
| 35 |
* and the possible combinations thereof. |
| 36 |
*/ |
| 37 |
static int |
| 38 |
calc_pattern (char ch, Layout layout, int i, int j) |
| 39 |
{ |
| 40 |
int pattern = 0; |
| 41 |
|
| 42 |
if (i + 1 < layout->w && layout[i + 1][j] == ch) |
| 43 |
pattern |= 1; |
| 44 |
|
| 45 |
if (j + 1 < layout->h) |
| 46 |
{ |
| 47 |
if (layout[i][j + 1] == ch) |
| 48 |
pattern |= 2; |
| 49 |
|
| 50 |
if (i + 1 < layout->w && layout[i + 1][j + 1] == ch) |
| 51 |
pattern |= 4; |
| 52 |
} |
| 53 |
|
| 54 |
return pattern; |
| 55 |
} |
| 56 |
|
| 57 |
/* 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 |
| 59 |
* walls. |
| 60 |
*/ |
| 61 |
static void |
| 62 |
expand_wall (Layout newlayout, int i, int j, Layout layout) |
| 63 |
{ |
| 64 |
int wall_pattern = calc_pattern ('#', layout, i, j); |
| 65 |
int door_pattern = calc_pattern ('D', layout, i, j); |
| 66 |
int both_pattern = wall_pattern | door_pattern; |
| 67 |
|
| 68 |
newlayout[i * 2][j * 2] = '#'; |
| 69 |
|
| 70 |
if (i + 1 < layout->w) |
| 71 |
{ |
| 72 |
if (both_pattern & 1) |
| 73 |
{ /* join walls/doors to the right */ |
| 74 |
/* newlayout[i*2+1][j*2] = '#'; */ |
| 75 |
newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (j + 1 < layout->h) |
| 80 |
{ |
| 81 |
if (both_pattern & 2) |
| 82 |
{ /* join walls/doors to the bottom */ |
| 83 |
/* newlayout[i*2][j*2+1] = '#'; */ |
| 84 |
newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; |
| 85 |
} |
| 86 |
|
| 87 |
if (wall_pattern == 7) |
| 88 |
{ /* if orig layout is a 2x2 wall block, |
| 89 |
* we fill the result with walls. */ |
| 90 |
newlayout[i * 2 + 1][j * 2 + 1] = '#'; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/* 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 |
| 97 |
* that it doesn't know how to correctly expand. |
| 98 |
*/ |
| 99 |
static void |
| 100 |
expand_door (Layout newlayout, int i, int j, Layout layout) |
| 101 |
{ |
| 102 |
int wall_pattern = calc_pattern ('#', layout, i, j); |
| 103 |
int door_pattern = calc_pattern ('D', layout, i, j); |
| 104 |
int join_pattern; |
| 105 |
|
| 106 |
/* Doors "like" to connect to walls more than other doors. If there is |
| 107 |
* a wall and another door, this door will connect to the wall and |
| 108 |
* disconnect from the other door. */ |
| 109 |
if (wall_pattern & 3) |
| 110 |
join_pattern = wall_pattern; |
| 111 |
else |
| 112 |
join_pattern = door_pattern; |
| 113 |
|
| 114 |
newlayout[i * 2][j * 2] = 'D'; |
| 115 |
|
| 116 |
if (i + 1 < layout->w) |
| 117 |
if (join_pattern & 1) |
| 118 |
/* there is a door/wall to the right */ |
| 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 |
|
| 127 |
void |
| 128 |
expand2x (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; |
| 140 |
} |
| 141 |
|
| 142 |
layout.swap (newlayout); |
| 143 |
newlayout.free (); |
| 144 |
} |
| 145 |
|