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