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.8 by root, Tue Apr 15 03:00:24 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.8 2008/04/15 03:00:24 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 (Layout newlayout, int i, int j, Layout 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, Layout 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;
49
83 if (i + 1 < xsize && layout[i + 1][j + 1] == ch) 50 if (i + 1 < layout->w && layout[i + 1][j + 1] == ch)
84 pattern |= 4; 51 pattern |= 4;
85 } 52 }
86 53
87 return pattern; 54 return pattern;
88} 55}
90/* 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
91 * 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
92 * walls. 59 * walls.
93 */ 60 */
94static void 61static void
95expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize) 62expand_wall (Layout newlayout, int i, int j, Layout layout)
96{ 63{
97 int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); 64 int wall_pattern = calc_pattern ('#', layout, i, j);
98 int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); 65 int door_pattern = calc_pattern ('D', layout, i, j);
99 int both_pattern = wall_pattern | door_pattern; 66 int both_pattern = wall_pattern | door_pattern;
100 67
101 newlayout[i * 2][j * 2] = '#'; 68 newlayout[i * 2][j * 2] = '#';
102 if (i + 1 < xsize) 69
70 if (i + 1 < layout->w)
103 { 71 {
104 if (both_pattern & 1) 72 if (both_pattern & 1)
105 { /* join walls/doors to the right */ 73 { /* join walls/doors to the right */
106
107/* newlayout[i*2+1][j*2] = '#'; */ 74/* newlayout[i*2+1][j*2] = '#'; */
108 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; 75 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j];
109 } 76 }
110 } 77 }
111 78
112 if (j + 1 < ysize) 79 if (j + 1 < layout->h)
113 { 80 {
114 if (both_pattern & 2) 81 if (both_pattern & 2)
115 { /* join walls/doors to the bottom */ 82 { /* join walls/doors to the bottom */
116
117/* newlayout[i*2][j*2+1] = '#'; */ 83/* newlayout[i*2][j*2+1] = '#'; */
118 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; 84 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1];
119 } 85 }
120 86
121 if (wall_pattern == 7) 87 if (wall_pattern == 7)
129/* 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
130 * adjacent walls. Note that it will also presumptuously delete (ignore) doors 96 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
131 * that it doesn't know how to correctly expand. 97 * that it doesn't know how to correctly expand.
132 */ 98 */
133static void 99static void
134expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize) 100expand_door (Layout newlayout, int i, int j, Layout layout)
135{ 101{
136 int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); 102 int wall_pattern = calc_pattern ('#', layout, i, j);
137 int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); 103 int door_pattern = calc_pattern ('D', layout, i, j);
138 int join_pattern; 104 int join_pattern;
139 105
140 /* 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
141 * 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
142 * disconnect from the other door. */ 108 * disconnect from the other door. */
145 else 111 else
146 join_pattern = door_pattern; 112 join_pattern = door_pattern;
147 113
148 newlayout[i * 2][j * 2] = 'D'; 114 newlayout[i * 2][j * 2] = 'D';
149 115
150 if (i + 1 < xsize) 116 if (i + 1 < layout->w)
151 if (join_pattern & 1) 117 if (join_pattern & 1)
152 /* there is a door/wall to the right */ 118 /* there is a door/wall to the right */
153 newlayout[i * 2 + 1][j * 2] = 'D'; 119 newlayout[i * 2 + 1][j * 2] = 'D';
154 120
155 if (j + 1 < ysize) 121 if (j + 1 < layout->h)
156 if (join_pattern & 2) 122 if (join_pattern & 2)
157 /* there is a door/wall below */ 123 /* there is a door/wall below */
158 newlayout[i * 2][j * 2 + 1] = 'D'; 124 newlayout[i * 2][j * 2 + 1] = 'D';
159} 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;
140 }
141
142 layout.swap (newlayout);
143 newlayout.free ();
144}
145

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines