| 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 | |
|---|
| 44 | namespace orxonox |
|---|
| 45 | { |
|---|
| 46 | MazeGenerator::MazeGenerator() |
|---|
| 47 | { |
|---|
| 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 ); |
|---|
| 55 | |
|---|
| 56 | this->maze_ = new unsigned char[ NUM_CELLS*NUM_CELLS ]; |
|---|
| 57 | std::fill( maze_, maze_ + NUM_CELLS*NUM_CELLS, 0 ); |
|---|
| 58 | |
|---|
| 59 | // current traversing position |
|---|
| 60 | this->ptX_ = 0; |
|---|
| 61 | this->ptY_ = 0; |
|---|
| 62 | |
|---|
| 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 | }; |
|---|
| 78 | |
|---|
| 79 | std::copy(headingX, headingX + 9, this->headingX_); |
|---|
| 80 | std::copy(headingY, headingY + 9, this->headingY_); |
|---|
| 81 | std::copy(mask, mask + 9, this->mask_); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | @brief |
|---|
| 86 | Checks if Direction is valid (for Maze-Generator) |
|---|
| 87 | */ |
|---|
| 88 | bool MazeGenerator::isDirValid( eDirection Dir ) |
|---|
| 89 | { |
|---|
| 90 | int NewX = ptX_ + headingX_[ Dir ]; |
|---|
| 91 | int NewY = ptY_ + headingY_[ Dir ]; |
|---|
| 92 | |
|---|
| 93 | if ( !Dir || NewX < 0 || NewY < 0 || NewX >= NUM_CELLS || NewY >= NUM_CELLS ) return false; |
|---|
| 94 | |
|---|
| 95 | return !maze_[ NewX + NUM_CELLS * NewY ]; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | @brief |
|---|
| 100 | Generates new Direction (for Maze-Generator) |
|---|
| 101 | */ |
|---|
| 102 | eDirection MazeGenerator::getDirection() |
|---|
| 103 | { |
|---|
| 104 | eDirection Dir = eDirection( 1 << randomInt4() ); |
|---|
| 105 | |
|---|
| 106 | while ( true ) |
|---|
| 107 | { |
|---|
| 108 | for ( int x = 0; x < 4; x++ ) |
|---|
| 109 | { |
|---|
| 110 | if ( isDirValid( Dir ) ) { return eDirection( Dir ); } |
|---|
| 111 | |
|---|
| 112 | Dir = eDirection( Dir << 1 ); |
|---|
| 113 | |
|---|
| 114 | if ( Dir > eDirection_Left ) { Dir = eDirection_Up; } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | Dir = eDirection( ( maze_[ cellIdx() ] & 0xf0 ) >> 4 ); |
|---|
| 118 | |
|---|
| 119 | // nowhere to go |
|---|
| 120 | if ( !Dir ) return eDirection_Invalid; |
|---|
| 121 | |
|---|
| 122 | ptX_ += headingX_[ Dir ]; |
|---|
| 123 | ptY_ += headingY_[ Dir ]; |
|---|
| 124 | |
|---|
| 125 | Dir = eDirection( 1 << randomInt4() ); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | @brief |
|---|
| 131 | Generates a Maze (for Maze-Generator) |
|---|
| 132 | */ |
|---|
| 133 | void MazeGenerator::generateMaze() |
|---|
| 134 | { |
|---|
| 135 | |
|---|
| 136 | for ( eDirection Dir = getDirection(); Dir != eDirection_Invalid; Dir = getDirection() ) |
|---|
| 137 | { |
|---|
| 138 | maze_[ cellIdx() ] |= Dir; |
|---|
| 139 | |
|---|
| 140 | ptX_ += headingX_[ Dir ]; |
|---|
| 141 | ptY_ += headingY_[ Dir ]; |
|---|
| 142 | |
|---|
| 143 | maze_[ cellIdx() ] = mask_[ Dir ]; |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | @brief |
|---|
| 149 | Print Maze (for Debugging only) |
|---|
| 150 | */ |
|---|
| 151 | void MazeGenerator::mazeOut(){ |
|---|
| 152 | for ( int y = 0; y < NUM_CELLS; y++ ) |
|---|
| 153 | { |
|---|
| 154 | for ( int x = 0; x < NUM_CELLS; x++ ) |
|---|
| 155 | { |
|---|
| 156 | char v = maze_[ y * NUM_CELLS + x ]; |
|---|
| 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 | |
|---|
| 173 | /** |
|---|
| 174 | @brief |
|---|
| 175 | Print levelcode_ (for Debugging only) |
|---|
| 176 | */ |
|---|
| 177 | void MazeGenerator::levelOut(){ |
|---|
| 178 | for ( int y = 0; y < NUM_CELLS; y++ ) |
|---|
| 179 | { |
|---|
| 180 | for ( int x = 0; x < NUM_CELLS; x++ ) |
|---|
| 181 | { |
|---|
| 182 | orxout()<<"["; |
|---|
| 183 | if ( levelcode_[ y * NUM_CELLS + x ] < 2) orxout()<<"U"; |
|---|
| 184 | else orxout()<<" "; |
|---|
| 185 | if ( levelcode_[ y * NUM_CELLS + x ] % 2 == 0) orxout()<<"R"; |
|---|
| 186 | else orxout()<<" "; |
|---|
| 187 | |
|---|
| 188 | orxout()<<" "; |
|---|
| 189 | orxout()<<" "; |
|---|
| 190 | orxout()<<"]"; |
|---|
| 191 | } |
|---|
| 192 | orxout()<<endl; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | @brief |
|---|
| 198 | Generate levelcode_ from Maze |
|---|
| 199 | */ |
|---|
| 200 | void MazeGenerator::renderMaze() |
|---|
| 201 | { |
|---|
| 202 | for ( int y = 0; y < NUM_CELLS; y++ ) |
|---|
| 203 | { |
|---|
| 204 | for ( int x = 0; x < NUM_CELLS; x++ ) |
|---|
| 205 | { |
|---|
| 206 | char v = maze_[ y * NUM_CELLS + x ]; |
|---|
| 207 | |
|---|
| 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; |
|---|
| 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) |
|---|
| 218 | levelcode_[ y * NUM_CELLS + x ] &= 2; |
|---|
| 219 | else if (x == 7 && y != 3) |
|---|
| 220 | levelcode_[ y * NUM_CELLS + x ] &= 1; |
|---|
| 221 | else if(x != 7) |
|---|
| 222 | levelcode_[ y * NUM_CELLS + x ] = 0; |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | // return the current index in maze_ |
|---|
| 229 | int MazeGenerator::cellIdx() |
|---|
| 230 | { |
|---|
| 231 | return ptX_ + NUM_CELLS * ptY_; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | int MazeGenerator::randomInt() |
|---|
| 235 | { |
|---|
| 236 | return (rand() % NUM_CELLS); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | int MazeGenerator::randomInt4() |
|---|
| 240 | { |
|---|
| 241 | return (rand() % 4); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|