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.6 by root, Fri Apr 11 21:09:53 2008 UTC vs.
Revision 1.7 by root, Mon Apr 14 22:41:17 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines