ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/expand2x.C
Revision: 1.8
Committed: Tue Apr 15 03:00:24 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81, rel-2_80, rel-2_6, rel-2_7, rel-2_5, rel-2_72, rel-2_73, rel-2_71, rel-2_76, rel-2_77, rel-2_74, rel-2_75, rel-2_54, rel-2_55, rel-2_56, rel-2_79, rel-2_52, rel-2_53, rel-2_90, rel-2_78, rel-2_61
Changes since 1.7: +9 -7 lines
Log Message:
new logging thread, fix bugs in random map generator

File Contents

# User Rev Content
1 root 1.5
2 elmex 1.1 /*
3     * Expands a layout by 2x in each dimension.
4     * H. S. Teoh
5     * --------------------------------------------------------------------------
6 root 1.8 * $Id: expand2x.C,v 1.7 2008-04-14 22:41:17 root Exp $
7 elmex 1.1 *
8     * ALGORITHM
9     *
10     * ... (TBW)
11     */
12    
13 root 1.6 #include "global.h"
14     #include "random_map.h"
15     #include "rproto.h"
16 elmex 1.1
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 root 1.3 static void
23 root 1.8 expand_misc (Layout newlayout, int i, int j, Layout layout)
24 root 1.3 {
25     newlayout[i * 2][j * 2] = layout[i][j];
26 elmex 1.1 /* (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 root 1.3 static int
38 root 1.8 calc_pattern (char ch, Layout layout, int i, int j)
39 root 1.3 {
40 elmex 1.1 int pattern = 0;
41    
42 root 1.7 if (i + 1 < layout->w && layout[i + 1][j] == ch)
43 elmex 1.1 pattern |= 1;
44    
45 root 1.7 if (j + 1 < layout->h)
46 root 1.3 {
47     if (layout[i][j + 1] == ch)
48     pattern |= 2;
49 root 1.8
50 root 1.7 if (i + 1 < layout->w && layout[i + 1][j + 1] == ch)
51 root 1.3 pattern |= 4;
52     }
53 elmex 1.1
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 root 1.3 static void
62 root 1.8 expand_wall (Layout newlayout, int i, int j, Layout layout)
63 root 1.3 {
64 root 1.7 int wall_pattern = calc_pattern ('#', layout, i, j);
65     int door_pattern = calc_pattern ('D', layout, i, j);
66 elmex 1.1 int both_pattern = wall_pattern | door_pattern;
67    
68 root 1.3 newlayout[i * 2][j * 2] = '#';
69 root 1.7
70     if (i + 1 < layout->w)
71 root 1.3 {
72     if (both_pattern & 1)
73     { /* join walls/doors to the right */
74 elmex 1.1 /* newlayout[i*2+1][j*2] = '#'; */
75 root 1.3 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j];
76     }
77 elmex 1.1 }
78    
79 root 1.7 if (j + 1 < layout->h)
80 root 1.3 {
81     if (both_pattern & 2)
82     { /* join walls/doors to the bottom */
83 elmex 1.1 /* newlayout[i*2][j*2+1] = '#'; */
84 root 1.3 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1];
85     }
86 elmex 1.1
87 root 1.3 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 elmex 1.1 }
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 root 1.3 static void
100 root 1.8 expand_door (Layout newlayout, int i, int j, Layout layout)
101 root 1.3 {
102 root 1.7 int wall_pattern = calc_pattern ('#', layout, i, j);
103     int door_pattern = calc_pattern ('D', layout, i, j);
104 elmex 1.1 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 root 1.3 if (wall_pattern & 3)
110 root 1.6 join_pattern = wall_pattern;
111 root 1.3 else
112 root 1.6 join_pattern = door_pattern;
113 root 1.3
114     newlayout[i * 2][j * 2] = 'D';
115 root 1.6
116 root 1.7 if (i + 1 < layout->w)
117 root 1.6 if (join_pattern & 1)
118     /* there is a door/wall to the right */
119     newlayout[i * 2 + 1][j * 2] = 'D';
120 root 1.3
121 root 1.7 if (j + 1 < layout->h)
122 root 1.6 if (join_pattern & 2)
123     /* there is a door/wall below */
124     newlayout[i * 2][j * 2 + 1] = 'D';
125 elmex 1.1 }
126 root 1.7
127     void
128 root 1.8 expand2x (Layout layout)
129 root 1.7 {
130 root 1.8 Layout newlayout (layout->w * 2 - 1, layout->h * 2 - 1);
131 root 1.7 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 root 1.8 newlayout.free ();
144 root 1.7 }
145