1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Manuel Meier |
---|
24 | * Co-authors: |
---|
25 | * Cyrill Muskelprotz |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file MazeGenerator.cc |
---|
31 | @brief Implementation of the MazeGenerator class. Generates the maze. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "MazeGenerator.h" |
---|
35 | |
---|
36 | #include <vector> |
---|
37 | |
---|
38 | #include "util/Output.h" |
---|
39 | #include "util/Math.h" |
---|
40 | |
---|
41 | namespace orxonox |
---|
42 | { |
---|
43 | MazeGenerator::MazeGenerator() |
---|
44 | { |
---|
45 | //levelcode_ represents the pitch: It's a 10x10 field. |
---|
46 | // 1 represents a Wall on the right side of this square |
---|
47 | // 2 represents a Wall on the top of this square |
---|
48 | // 3 represents 2 and 1 at the same time |
---|
49 | // Note: the levelcode_ is generated from the Maze-Generator functions at the beginning of the game |
---|
50 | this->levelcode_ = new int[ NUM_CELLS*NUM_CELLS ];; |
---|
51 | std::fill( levelcode_, levelcode_ + NUM_CELLS*NUM_CELLS, 0 ); |
---|
52 | |
---|
53 | this->maze_ = new unsigned char[ NUM_CELLS*NUM_CELLS ]; |
---|
54 | std::fill( maze_, maze_ + NUM_CELLS*NUM_CELLS, 0 ); |
---|
55 | |
---|
56 | // current traversing position |
---|
57 | this->ptX_ = 0; |
---|
58 | this->ptY_ = 0; |
---|
59 | |
---|
60 | // 0 1 2 3 4 5 6 7 8 |
---|
61 | // U R D L |
---|
62 | int headingX[9] = { 0, 0,+1, 0, 0, 0, 0, 0,-1 }; |
---|
63 | int headingY[9] = { 0,-1, 0, 0,+1, 0, 0, 0, 0 }; |
---|
64 | int mask[9] = { |
---|
65 | 0, |
---|
66 | eDirection_Down | eDirection_Down << 4, |
---|
67 | eDirection_Left | eDirection_Left << 4, |
---|
68 | 0, |
---|
69 | eDirection_Up | eDirection_Up << 4, |
---|
70 | 0, |
---|
71 | 0, |
---|
72 | 0, |
---|
73 | eDirection_Right | eDirection_Right << 4 |
---|
74 | }; |
---|
75 | |
---|
76 | std::copy(headingX, headingX + 9, this->headingX_); |
---|
77 | std::copy(headingY, headingY + 9, this->headingY_); |
---|
78 | std::copy(mask, mask + 9, this->mask_); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | @brief |
---|
83 | Checks if Direction is valid (for Maze-Generator) |
---|
84 | */ |
---|
85 | bool MazeGenerator::isDirValid( eDirection Dir ) |
---|
86 | { |
---|
87 | int NewX = ptX_ + headingX_[ Dir ]; |
---|
88 | int NewY = ptY_ + headingY_[ Dir ]; |
---|
89 | |
---|
90 | if ( !Dir || NewX < 0 || NewY < 0 || NewX >= NUM_CELLS || NewY >= NUM_CELLS ) return false; |
---|
91 | |
---|
92 | return !maze_[ NewX + NUM_CELLS * NewY ]; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | @brief |
---|
97 | Generates new Direction (for Maze-Generator) |
---|
98 | */ |
---|
99 | eDirection MazeGenerator::getDirection() |
---|
100 | { |
---|
101 | eDirection Dir = eDirection( 1 << randomInt4() ); |
---|
102 | |
---|
103 | while ( true ) |
---|
104 | { |
---|
105 | for ( int x = 0; x < 4; x++ ) |
---|
106 | { |
---|
107 | if ( isDirValid( Dir ) ) { return eDirection( Dir ); } |
---|
108 | |
---|
109 | Dir = eDirection( Dir << 1 ); |
---|
110 | |
---|
111 | if ( Dir > eDirection_Left ) { Dir = eDirection_Up; } |
---|
112 | } |
---|
113 | |
---|
114 | Dir = eDirection( ( maze_[ cellIdx() ] & 0xf0 ) >> 4 ); |
---|
115 | |
---|
116 | // nowhere to go |
---|
117 | if ( !Dir ) return eDirection_Invalid; |
---|
118 | |
---|
119 | ptX_ += headingX_[ Dir ]; |
---|
120 | ptY_ += headingY_[ Dir ]; |
---|
121 | |
---|
122 | Dir = eDirection( 1 << randomInt4() ); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | @brief |
---|
128 | Generates a Maze (for Maze-Generator) |
---|
129 | */ |
---|
130 | void MazeGenerator::generateMaze() |
---|
131 | { |
---|
132 | |
---|
133 | for ( eDirection Dir = getDirection(); Dir != eDirection_Invalid; Dir = getDirection() ) |
---|
134 | { |
---|
135 | maze_[ cellIdx() ] |= Dir; |
---|
136 | |
---|
137 | ptX_ += headingX_[ Dir ]; |
---|
138 | ptY_ += headingY_[ Dir ]; |
---|
139 | |
---|
140 | maze_[ cellIdx() ] = mask_[ Dir ]; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | @brief |
---|
146 | Print Maze (for Debugging only) |
---|
147 | */ |
---|
148 | void MazeGenerator::mazeOut(){ |
---|
149 | for ( int y = 0; y < NUM_CELLS; y++ ) |
---|
150 | { |
---|
151 | for ( int x = 0; x < NUM_CELLS; x++ ) |
---|
152 | { |
---|
153 | char v = maze_[ y * NUM_CELLS + x ]; |
---|
154 | orxout()<<"["; |
---|
155 | if ( ( v & eDirection_Up ) ) orxout()<<"U"; |
---|
156 | else orxout()<<" "; |
---|
157 | if ( ( v & eDirection_Right ) ) orxout()<<"R"; |
---|
158 | else orxout()<<" "; |
---|
159 | if ( ( v & eDirection_Down ) ) orxout()<<" "; |
---|
160 | else orxout()<<" "; |
---|
161 | if ( ( v & eDirection_Left ) ) orxout()<<" "; |
---|
162 | else orxout()<<" "; |
---|
163 | orxout()<<"]"; |
---|
164 | } |
---|
165 | orxout()<<endl; |
---|
166 | } |
---|
167 | |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | @brief |
---|
172 | Print levelcode_ (for Debugging only) |
---|
173 | */ |
---|
174 | void MazeGenerator::levelOut(){ |
---|
175 | for ( int y = 0; y < NUM_CELLS; y++ ) |
---|
176 | { |
---|
177 | for ( int x = 0; x < NUM_CELLS; x++ ) |
---|
178 | { |
---|
179 | orxout()<<"["; |
---|
180 | if ( levelcode_[ y * NUM_CELLS + x ] < 2) orxout()<<"U"; |
---|
181 | else orxout()<<" "; |
---|
182 | if ( levelcode_[ y * NUM_CELLS + x ] % 2 == 0) orxout()<<"R"; |
---|
183 | else orxout()<<" "; |
---|
184 | |
---|
185 | orxout()<<" "; |
---|
186 | orxout()<<" "; |
---|
187 | orxout()<<"]"; |
---|
188 | } |
---|
189 | orxout()<<endl; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | @brief |
---|
195 | Generate levelcode_ from Maze |
---|
196 | */ |
---|
197 | void MazeGenerator::renderMaze() |
---|
198 | { |
---|
199 | for ( int y = 0; y < NUM_CELLS; y++ ) |
---|
200 | { |
---|
201 | for ( int x = 0; x < NUM_CELLS; x++ ) |
---|
202 | { |
---|
203 | char v = maze_[ y * NUM_CELLS + x ]; |
---|
204 | |
---|
205 | if ( !( v & eDirection_Up ) && y >0) levelcode_[ y * NUM_CELLS + x ] |= 2; |
---|
206 | if ( !( v & eDirection_Right ) && x <9) levelcode_[ y * NUM_CELLS + x ] |= 1; |
---|
207 | } |
---|
208 | } |
---|
209 | for ( int y = 3; y < 7; y++ ) |
---|
210 | { |
---|
211 | for ( int x = 3; x < 7; x++ ) |
---|
212 | { |
---|
213 | |
---|
214 | if(y == 3 && x != 7) |
---|
215 | levelcode_[ y * NUM_CELLS + x ] &= 2; |
---|
216 | else if (x == 7 && y != 3) |
---|
217 | levelcode_[ y * NUM_CELLS + x ] &= 1; |
---|
218 | else if(x != 7) |
---|
219 | levelcode_[ y * NUM_CELLS + x ] = 0; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | } |
---|
224 | |
---|
225 | // return the current index in maze_ |
---|
226 | int MazeGenerator::cellIdx() |
---|
227 | { |
---|
228 | return ptX_ + NUM_CELLS * ptY_; |
---|
229 | } |
---|
230 | |
---|
231 | int MazeGenerator::randomInt() |
---|
232 | { |
---|
233 | return (rand() % NUM_CELLS); |
---|
234 | } |
---|
235 | |
---|
236 | int MazeGenerator::randomInt4() |
---|
237 | { |
---|
238 | return (rand() % 4); |
---|
239 | } |
---|
240 | } |
---|