Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationHS15/src/modules/hover/MazeGenerator.cc @ 11040

Last change on this file since 11040 was 11040, checked in by landauf, 8 years ago

hover: maze size is now fully configurable in xml

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
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 Burgener
26 *
27 *   Based on random-maze-generator by Sergey Kosarevsky, 2014
28 *   https://github.com/corporateshark/random-maze-generator
29 *
30 */
31
32/**
33    @file MazeGenerator.cc
34    @brief Implementation of the MazeGenerator class. Generates the maze.
35*/
36
37#include "MazeGenerator.h"
38
39#include <vector>
40
41#include "util/Output.h"
42#include "util/Math.h"
43
44namespace orxonox
45{
46    MazeGenerator::MazeGenerator(int numCells)
47    {
48        this->numCells_ = numCells;
49
50        //levelcode_ represents the pitch: It's a 10x10 field.
51        // 1 represents a Wall on the right side of this square
52        // 2 represents a Wall on the top of this square
53        // 3 represents 2 and 1 at the same time
54        // Note: the levelcode_ is generated from the Maze-Generator functions at the beginning of the game
55        this->levelcode_ = new int[ numCells_*numCells_ ];;
56        std::fill( levelcode_, levelcode_ + numCells_*numCells_, 0 );
57
58        this->maze_ = new unsigned char[ numCells_*numCells_ ];
59        std::fill( maze_, maze_ + numCells_*numCells_, 0 );
60
61        // current traversing position
62        this->ptX_ = 0;
63        this->ptY_ = 0;
64
65        //                  0  1  2  3  4  5  6  7  8
66        //                     U  R     D           L
67        int headingX[9] = { 0, 0,+1, 0, 0, 0, 0, 0,-1 };
68        int headingY[9] = { 0,-1, 0, 0,+1, 0, 0, 0, 0 };
69        int mask[9]     = {
70                              0,
71                              eDirection_Down | eDirection_Down << 4,
72                              eDirection_Left | eDirection_Left << 4,
73                              0,
74                              eDirection_Up | eDirection_Up << 4,
75                              0,
76                              0,
77                              0,
78                              eDirection_Right | eDirection_Right << 4
79                          };
80
81        std::copy(headingX, headingX + 9, this->headingX_);
82        std::copy(headingY, headingY + 9, this->headingY_);
83        std::copy(mask,     mask + 9,     this->mask_);
84    }
85
86    /**
87    @brief
88        Checks if Direction is valid (for Maze-Generator)
89    */
90    bool MazeGenerator::isDirValid( eDirection Dir )
91    {
92        int NewX = ptX_ + headingX_[ Dir ];
93        int NewY = ptY_ + headingY_[ Dir ];
94
95        if ( !Dir || NewX < 0 || NewY < 0 || NewX >= numCells_ || NewY >= numCells_ ) return false;
96
97        return !maze_[ NewX + numCells_ * NewY ];
98    }
99
100    /**
101    @brief
102        Generates new Direction (for Maze-Generator)
103    */
104    eDirection MazeGenerator::getDirection()
105    {
106        eDirection Dir = eDirection( 1 << randomInt4() );
107
108        while ( true )
109        {
110            for ( int x = 0; x < 4; x++ )
111            {
112                if ( isDirValid( Dir ) ) { return eDirection( Dir ); }
113
114                Dir = eDirection( Dir << 1 );
115
116                if ( Dir > eDirection_Left ) { Dir = eDirection_Up; }
117            }
118
119            Dir = eDirection( ( maze_[ cellIdx() ] & 0xf0 ) >> 4 );
120
121            // nowhere to go
122            if ( !Dir ) return eDirection_Invalid;
123
124            ptX_ += headingX_[ Dir ];
125            ptY_ += headingY_[ Dir ];
126
127            Dir = eDirection( 1 << randomInt4() );
128        }
129    }
130
131    /**
132    @brief
133        Generates a Maze (for Maze-Generator)
134    */
135    void MazeGenerator::generateMaze()
136    {
137
138        for ( eDirection Dir = getDirection(); Dir != eDirection_Invalid; Dir = getDirection() )
139        {
140            maze_[ cellIdx() ] |= Dir;
141
142            ptX_ += headingX_[ Dir ];
143            ptY_ += headingY_[ Dir ];
144
145            maze_[ cellIdx() ] = mask_[ Dir ];
146        }
147    } 
148   
149    /**
150    @brief
151        Print Maze (for Debugging only)
152    */
153    void MazeGenerator::mazeOut(){
154        for ( int y = 0; y < numCells_; y++ )
155        {
156            for ( int x = 0; x < numCells_; x++ )
157            {
158                char v = maze_[ y * numCells_ + x ];
159                orxout()<<"[";
160                if ( ( v & eDirection_Up    ) ) orxout()<<"U";
161                else orxout()<<" ";
162                if ( ( v & eDirection_Right ) ) orxout()<<"R";
163                else orxout()<<" ";
164                if ( ( v & eDirection_Down  ) ) orxout()<<" ";
165                else orxout()<<" ";
166                if ( ( v & eDirection_Left  ) ) orxout()<<" ";
167                else orxout()<<" ";
168                orxout()<<"]";
169            }
170            orxout()<<endl;
171        }
172
173    }
174
175    /**
176    @brief
177        Print levelcode_ (for Debugging only)
178    */
179    void MazeGenerator::levelOut(){
180        for ( int y = 0; y < numCells_; y++ )
181        {
182            for ( int x = 0; x < numCells_; x++ )
183            {
184                orxout()<<"[";
185                if ( levelcode_[ y * numCells_ + x ] < 2) orxout()<<"U";
186                else orxout()<<" ";
187                if ( levelcode_[ y * numCells_ + x ] % 2 == 0) orxout()<<"R";
188                else orxout()<<" ";
189
190                orxout()<<" ";
191                orxout()<<" ";
192                orxout()<<"]";
193            }
194            orxout()<<endl;
195        }
196    }
197
198    /**
199    @brief
200        Generate levelcode_ from Maze
201    */
202    void MazeGenerator::renderMaze()
203    {
204        for ( int y = 0; y < numCells_; y++ )
205        {
206            for ( int x = 0; x < numCells_; x++ )
207            {
208                char v = maze_[ y * numCells_ + x ];
209
210                if ( !( v & eDirection_Up    ) && y >0) levelcode_[ y * numCells_ + x ] |= 2;
211                if ( !( v & eDirection_Right ) && x <(numCells_-1)) levelcode_[ y * numCells_ + x ] |= 1;
212            }
213        }
214
215        // leave an empty space in the middle of the maze
216        int lowerBound = numCells_ / 2 - 2;
217        int upperBound = numCells_ / 2 + 2;
218        for ( int y = lowerBound; y < upperBound; y++ )
219        {
220            for ( int x = lowerBound; x < upperBound; x++ )
221            {
222
223                if(y == lowerBound && x != upperBound)
224                    levelcode_[ y * numCells_ + x ] &= 2;
225                else if (x == upperBound && y != lowerBound)
226                    levelcode_[ y * numCells_ + x ] &= 1;
227                else if(x != upperBound)
228                    levelcode_[ y * numCells_ + x ] = 0;
229            }
230        }
231
232    }
233
234    // return the current index in maze_
235    int MazeGenerator::cellIdx()
236    {
237        return ptX_ + numCells_ * ptY_;
238    }
239
240    int MazeGenerator::randomInt()
241    {
242        return (rand() % numCells_);
243    }
244
245    int MazeGenerator::randomInt4()
246    {
247        return (rand() % 4);
248    }
249}
Note: See TracBrowser for help on using the repository browser.