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.10 by root, Thu Jul 1 01:22:44 2010 UTC

1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) Crossfire Development Team (restored, original file without copyright notice)
6 *
7 * Deliantra is free software: you can redistribute it and/or modify it under
8 * the terms of the Affero GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the Affero GNU General Public License
18 * and the GNU General Public License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
1 23
2/* 24/*
3 * Expands a layout by 2x in each dimension. 25 * Expands a layout by 2x in each dimension.
4 * H. S. Teoh 26 * H. S. Teoh
5 * -------------------------------------------------------------------------- 27 * --------------------------------------------------------------------------
6 * $Id: expand2x.C,v 1.6 2008/04/11 21:09:53 root Exp $ 28 * $Id: expand2x.C,v 1.10 2010/07/01 01:22:44 root Exp $
7 * 29 *
8 * ALGORITHM 30 * ALGORITHM
9 * 31 *
10 * ... (TBW) 32 * ... (TBW)
11 */ 33 */
12 34
13#include "global.h" 35#include "global.h"
14#include "random_map.h" 36#include "random_map.h"
15#include "rproto.h" 37#include "rproto.h"
16 38
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 39/* 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: 40 * fill up the rest of the 2x2 result with \0:
53 * X ---> X \0 41 * X ---> X \0
54 * \0 \0 42 * \0 \0
55 */ 43 */
56static void 44static void
57expand_misc (char **newlayout, int i, int j, char **layout, int xsize, int ysize) 45expand_misc (Layout &newlayout, int i, int j, Layout &layout)
58{ 46{
59 newlayout[i * 2][j * 2] = layout[i][j]; 47 newlayout[i * 2][j * 2] = layout[i][j];
60 /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that 48 /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that
61 * for us.) */ 49 * for us.) */
62} 50}
67 * 2 match on (i, j+1) 55 * 2 match on (i, j+1)
68 * 4 match on (i+1, j+1) 56 * 4 match on (i+1, j+1)
69 * and the possible combinations thereof. 57 * and the possible combinations thereof.
70 */ 58 */
71static int 59static int
72calc_pattern (char ch, char **layout, int i, int j, int xsize, int ysize) 60calc_pattern (char ch, Layout &layout, int i, int j)
73{ 61{
74 int pattern = 0; 62 int pattern = 0;
75 63
76 if (i + 1 < xsize && layout[i + 1][j] == ch) 64 if (i + 1 < layout.w && layout[i + 1][j] == ch)
77 pattern |= 1; 65 pattern |= 1;
78 66
79 if (j + 1 < ysize) 67 if (j + 1 < layout.h)
80 { 68 {
81 if (layout[i][j + 1] == ch) 69 if (layout[i][j + 1] == ch)
82 pattern |= 2; 70 pattern |= 2;
71
83 if (i + 1 < xsize && layout[i + 1][j + 1] == ch) 72 if (i + 1 < layout.w && layout[i + 1][j + 1] == ch)
84 pattern |= 4; 73 pattern |= 4;
85 } 74 }
86 75
87 return pattern; 76 return pattern;
88} 77}
90/* Expand a wall. This function will try to sensibly connect the resulting 79/* 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 80 * wall to adjacent wall squares, so that the result won't have disconnected
92 * walls. 81 * walls.
93 */ 82 */
94static void 83static void
95expand_wall (char **newlayout, int i, int j, char **layout, int xsize, int ysize) 84expand_wall (Layout &newlayout, int i, int j, Layout &layout)
96{ 85{
97 int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); 86 int wall_pattern = calc_pattern ('#', layout, i, j);
98 int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); 87 int door_pattern = calc_pattern ('D', layout, i, j);
99 int both_pattern = wall_pattern | door_pattern; 88 int both_pattern = wall_pattern | door_pattern;
100 89
101 newlayout[i * 2][j * 2] = '#'; 90 newlayout[i * 2][j * 2] = '#';
102 if (i + 1 < xsize) 91
92 if (i + 1 < layout.w)
103 { 93 {
104 if (both_pattern & 1) 94 if (both_pattern & 1)
105 { /* join walls/doors to the right */ 95 { /* join walls/doors to the right */
106
107/* newlayout[i*2+1][j*2] = '#'; */ 96/* newlayout[i*2+1][j*2] = '#'; */
108 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; 97 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j];
109 } 98 }
110 } 99 }
111 100
112 if (j + 1 < ysize) 101 if (j + 1 < layout.h)
113 { 102 {
114 if (both_pattern & 2) 103 if (both_pattern & 2)
115 { /* join walls/doors to the bottom */ 104 { /* join walls/doors to the bottom */
116
117/* newlayout[i*2][j*2+1] = '#'; */ 105/* newlayout[i*2][j*2+1] = '#'; */
118 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; 106 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1];
119 } 107 }
120 108
121 if (wall_pattern == 7) 109 if (wall_pattern == 7)
129/* This function will try to sensibly connect doors so that they meet up with 117/* 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 118 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
131 * that it doesn't know how to correctly expand. 119 * that it doesn't know how to correctly expand.
132 */ 120 */
133static void 121static void
134expand_door (char **newlayout, int i, int j, char **layout, int xsize, int ysize) 122expand_door (Layout &newlayout, int i, int j, Layout &layout)
135{ 123{
136 int wall_pattern = calc_pattern ('#', layout, i, j, xsize, ysize); 124 int wall_pattern = calc_pattern ('#', layout, i, j);
137 int door_pattern = calc_pattern ('D', layout, i, j, xsize, ysize); 125 int door_pattern = calc_pattern ('D', layout, i, j);
138 int join_pattern; 126 int join_pattern;
139 127
140 /* Doors "like" to connect to walls more than other doors. If there is 128 /* 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 129 * a wall and another door, this door will connect to the wall and
142 * disconnect from the other door. */ 130 * disconnect from the other door. */
145 else 133 else
146 join_pattern = door_pattern; 134 join_pattern = door_pattern;
147 135
148 newlayout[i * 2][j * 2] = 'D'; 136 newlayout[i * 2][j * 2] = 'D';
149 137
150 if (i + 1 < xsize) 138 if (i + 1 < layout.w)
151 if (join_pattern & 1) 139 if (join_pattern & 1)
152 /* there is a door/wall to the right */ 140 /* there is a door/wall to the right */
153 newlayout[i * 2 + 1][j * 2] = 'D'; 141 newlayout[i * 2 + 1][j * 2] = 'D';
154 142
155 if (j + 1 < ysize) 143 if (j + 1 < layout.h)
156 if (join_pattern & 2) 144 if (join_pattern & 2)
157 /* there is a door/wall below */ 145 /* there is a door/wall below */
158 newlayout[i * 2][j * 2 + 1] = 'D'; 146 newlayout[i * 2][j * 2 + 1] = 'D';
159} 147}
148
149void
150expand2x (Layout &layout)
151{
152 Layout new_layout (layout.w * 2 - 1, layout.h * 2 - 1);
153
154 new_layout.clear ();
155
156 for (int i = 0; i < layout.w; i++)
157 for (int j = 0; j < layout.h; j++)
158 switch (layout[i][j])
159 {
160 case '#': expand_wall (new_layout, i, j, layout); break;
161 case 'D': expand_door (new_layout, i, j, layout); break;
162 default: expand_misc (new_layout, i, j, layout); break;
163 }
164
165 layout.swap (new_layout);
166}
167

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines