[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: |
---|
[10930] | 25 | * Cyrill Muskelprotz |
---|
[10658] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file Hover.cc |
---|
[10930] | 31 | @brief Implementation of the Hover class. Sets up the whole Minigame |
---|
[10658] | 32 | */ |
---|
| 33 | |
---|
[10660] | 34 | //#include "orxonox/worldentities/pawns/SpaceShip.h" |
---|
[10658] | 35 | #include "Hover.h" |
---|
[10708] | 36 | |
---|
| 37 | #include "HoverWall.h" |
---|
[10894] | 38 | #include "HoverFlag.h" |
---|
[10658] | 39 | #include "core/CoreIncludes.h" |
---|
| 40 | |
---|
[10835] | 41 | #include <iostream> |
---|
| 42 | #include <string> |
---|
| 43 | #include <time.h> |
---|
| 44 | #include <stdlib.h> |
---|
| 45 | #include <memory.h> |
---|
| 46 | #include <stdint.h> |
---|
| 47 | #include <fstream> |
---|
[10894] | 48 | #include <vector> |
---|
[10835] | 49 | |
---|
[10658] | 50 | namespace orxonox |
---|
| 51 | { |
---|
[10751] | 52 | bool firstTick = true; |
---|
[10930] | 53 | |
---|
| 54 | //Levelcode represents the pitch: It's a 10x10 field. |
---|
| 55 | // 1 represents a Wall on the right side of this square |
---|
| 56 | // 2 represents a Wall on the top of this square |
---|
| 57 | // 3 represents 2 and 1 at the same time |
---|
| 58 | // Note: the levelcode is generated from the Maze-Generator functions at the beginning of the game |
---|
[10787] | 59 | int levelcode[10][10] = |
---|
| 60 | { |
---|
[10835] | 61 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 62 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 63 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 64 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 65 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 66 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 67 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 68 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 69 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
| 70 | { 0,0,0,0,0,0,0,0,0,0 } |
---|
[10787] | 71 | }; |
---|
| 72 | |
---|
[10835] | 73 | const int NumCells = 10; |
---|
| 74 | unsigned char* g_Maze = new unsigned char[ NumCells* NumCells ]; |
---|
[10787] | 75 | |
---|
[10835] | 76 | // current traversing position |
---|
| 77 | int g_PtX; |
---|
| 78 | int g_PtY; |
---|
| 79 | |
---|
| 80 | // return the current index in g_Maze |
---|
| 81 | int Hover::CellIdx() |
---|
| 82 | { |
---|
| 83 | return g_PtX + NumCells * g_PtY; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | int Hover::RandomInt() |
---|
| 88 | { |
---|
| 89 | return (rand() % NumCells); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | int Hover::RandomInt4() |
---|
| 93 | { |
---|
| 94 | return (rand() % 4); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
[10658] | 98 | RegisterUnloadableClass(Hover); |
---|
| 99 | |
---|
[10835] | 100 | |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
[10664] | 106 | Hover::Hover(Context* context) : Gametype(context) |
---|
[10658] | 107 | { |
---|
[10708] | 108 | |
---|
[10658] | 109 | RegisterObject(Hover); |
---|
[10895] | 110 | this->setHUDTemplate("HoverHUD"); |
---|
[10658] | 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | void Hover::tick(float dt) |
---|
| 116 | { |
---|
[10928] | 117 | |
---|
[10658] | 118 | SUPER(Hover, tick, dt); |
---|
[10751] | 119 | |
---|
[10787] | 120 | |
---|
| 121 | |
---|
| 122 | |
---|
[10760] | 123 | if(firstTick) |
---|
| 124 | { |
---|
[10900] | 125 | |
---|
[10835] | 126 | std::fill( g_Maze, g_Maze + NumCells * NumCells, 0 ); |
---|
| 127 | g_PtX=0; |
---|
| 128 | g_PtY=0; |
---|
| 129 | GenerateMaze(); |
---|
| 130 | RenderMaze(); |
---|
[10751] | 131 | firstTick = false; |
---|
[10787] | 132 | |
---|
[10928] | 133 | //Outer Walls |
---|
| 134 | for(int i = 0; i<10; i++){ |
---|
| 135 | new HoverWall(origin_->getContext(), 0, i+1, 1); |
---|
| 136 | new HoverWall(origin_->getContext(), 10, i+1, 1); |
---|
| 137 | new HoverWall(origin_->getContext(), i+1, 0, 2); |
---|
| 138 | new HoverWall(origin_->getContext(), i+1, 10, 2); |
---|
| 139 | } |
---|
| 140 | |
---|
[10930] | 141 | //Generate inner Walls according to levelcode |
---|
[10787] | 142 | for(int y=0; y<10; y++){ |
---|
| 143 | for(int x=0; x<10; x++){ |
---|
| 144 | switch(levelcode[y][x]){ |
---|
| 145 | case 1: new HoverWall(origin_->getContext(), x+1, 10-y, 1); |
---|
| 146 | break; |
---|
| 147 | case 3: new HoverWall(origin_->getContext(), x+1, 10-y, 1); |
---|
| 148 | case 2: new HoverWall(origin_->getContext(), x+1, 10-y, 0); |
---|
| 149 | default: break; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
[10930] | 157 | //Generate 5 flags randomly |
---|
[10894] | 158 | for ( int i = 0; i < 5; i++ ) |
---|
| 159 | flagVector.push_back(new HoverFlag(origin_->getContext(), rand()%10, rand()%10)); |
---|
[10900] | 160 | |
---|
| 161 | Flags_ = flagVector.size(); |
---|
[10930] | 162 | |
---|
| 163 | }//firsttick end |
---|
| 164 | |
---|
| 165 | // Check if ship collided with one of the flags |
---|
[10938] | 166 | for ( unsigned int i = 0; i < flagVector.size(); i++ ){ |
---|
[10894] | 167 | if(flagVector[i]->getCollided()){ |
---|
| 168 | flagVector[i]->destroyLater(); |
---|
| 169 | flagVector.erase (flagVector.begin()+i); |
---|
| 170 | } |
---|
| 171 | } |
---|
[10900] | 172 | Flags_ = flagVector.size(); |
---|
[10751] | 173 | |
---|
| 174 | |
---|
[10894] | 175 | |
---|
[10658] | 176 | } |
---|
| 177 | |
---|
[10900] | 178 | int Hover::getFlags() |
---|
| 179 | { |
---|
[10658] | 180 | |
---|
[10900] | 181 | // Call start for the parent class. |
---|
| 182 | return Flags_; |
---|
| 183 | } |
---|
| 184 | |
---|
[10658] | 185 | void Hover::start() |
---|
| 186 | { |
---|
| 187 | |
---|
| 188 | // Call start for the parent class. |
---|
[10664] | 189 | Gametype::start(); |
---|
[10708] | 190 | |
---|
[10658] | 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | void Hover::end() |
---|
| 195 | { |
---|
| 196 | // DON'T CALL THIS! |
---|
| 197 | // Deathmatch::end(); |
---|
| 198 | // It will misteriously crash the game! |
---|
| 199 | // Instead startMainMenu, this won't crash. |
---|
| 200 | GSLevel::startMainMenu(); |
---|
| 201 | } |
---|
[10835] | 202 | |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | |
---|
[10930] | 206 | // Some definitions for the Maze-Generator |
---|
[10835] | 207 | |
---|
| 208 | // 0 1 2 3 4 5 6 7 8 |
---|
| 209 | // U R D L |
---|
| 210 | int Heading_X[9] = { 0, 0,+1, 0, 0, 0, 0, 0,-1 }; |
---|
| 211 | int Heading_Y[9] = { 0,-1, 0, 0,+1, 0, 0, 0, 0 }; |
---|
| 212 | int Mask[9] = { |
---|
| 213 | 0, |
---|
| 214 | eDirection_Down | eDirection_Down << 4, |
---|
| 215 | eDirection_Left | eDirection_Left << 4, |
---|
| 216 | 0, |
---|
| 217 | eDirection_Up | eDirection_Up << 4, |
---|
| 218 | 0, |
---|
| 219 | 0, |
---|
| 220 | 0, |
---|
| 221 | eDirection_Right | eDirection_Right << 4 |
---|
| 222 | }; |
---|
| 223 | |
---|
| 224 | |
---|
[10930] | 225 | /** |
---|
| 226 | @brief |
---|
| 227 | Checks if Direction is valid (for Maze-Generator) |
---|
| 228 | */ |
---|
[10835] | 229 | bool Hover::IsDirValid( eDirection Dir ) |
---|
| 230 | { |
---|
| 231 | int NewX = g_PtX + Heading_X[ Dir ]; |
---|
| 232 | int NewY = g_PtY + Heading_Y[ Dir ]; |
---|
| 233 | |
---|
| 234 | if ( !Dir || NewX < 0 || NewY < 0 || NewX >= NumCells || NewY >= NumCells ) return false; |
---|
| 235 | |
---|
| 236 | return !g_Maze[ NewX + NumCells * NewY ]; |
---|
| 237 | } |
---|
| 238 | |
---|
[10930] | 239 | /** |
---|
| 240 | @brief |
---|
| 241 | Generates new Direction (for Maze-Generator) |
---|
| 242 | */ |
---|
[10835] | 243 | eDirection Hover::GetDirection() |
---|
| 244 | { |
---|
| 245 | eDirection Dir = eDirection( 1 << RandomInt4() ); |
---|
| 246 | |
---|
| 247 | while ( true ) |
---|
| 248 | { |
---|
| 249 | for ( int x = 0; x < 4; x++ ) |
---|
| 250 | { |
---|
| 251 | if ( IsDirValid( Dir ) ) { return eDirection( Dir ); } |
---|
| 252 | |
---|
| 253 | Dir = eDirection( Dir << 1 ); |
---|
| 254 | |
---|
| 255 | if ( Dir > eDirection_Left ) { Dir = eDirection_Up; } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | Dir = eDirection( ( g_Maze[ CellIdx() ] & 0xf0 ) >> 4 ); |
---|
| 259 | |
---|
| 260 | // nowhere to go |
---|
| 261 | if ( !Dir ) return eDirection_Invalid; |
---|
| 262 | |
---|
| 263 | g_PtX += Heading_X[ Dir ]; |
---|
| 264 | g_PtY += Heading_Y[ Dir ]; |
---|
| 265 | |
---|
| 266 | Dir = eDirection( 1 << RandomInt4() ); |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
[10930] | 270 | /** |
---|
| 271 | @brief |
---|
| 272 | Generates a Maze (for Maze-Generator) |
---|
| 273 | */ |
---|
[10835] | 274 | void Hover::GenerateMaze() |
---|
| 275 | { |
---|
| 276 | |
---|
| 277 | for ( eDirection Dir = GetDirection(); Dir != eDirection_Invalid; Dir = GetDirection() ) |
---|
| 278 | { |
---|
| 279 | g_Maze[ CellIdx() ] |= Dir; |
---|
| 280 | |
---|
| 281 | g_PtX += Heading_X[ Dir ]; |
---|
| 282 | g_PtY += Heading_Y[ Dir ]; |
---|
| 283 | |
---|
| 284 | g_Maze[ CellIdx() ] = Mask[ Dir ]; |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
[10930] | 288 | /** |
---|
| 289 | @brief |
---|
| 290 | Print Maze (for Debugging only) |
---|
| 291 | */ |
---|
[10835] | 292 | void Hover::MazeOut(){ |
---|
| 293 | for ( int y = 0; y < NumCells; y++ ) |
---|
| 294 | { |
---|
| 295 | for ( int x = 0; x < NumCells; x++ ) |
---|
| 296 | { |
---|
| 297 | char v = g_Maze[ y * NumCells + x ]; |
---|
| 298 | orxout()<<"["; |
---|
| 299 | if ( ( v & eDirection_Up ) ) orxout()<<"U"; |
---|
| 300 | else orxout()<<" "; |
---|
| 301 | if ( ( v & eDirection_Right ) ) orxout()<<"R"; |
---|
| 302 | else orxout()<<" "; |
---|
| 303 | if ( ( v & eDirection_Down ) ) orxout()<<" "; |
---|
| 304 | else orxout()<<" "; |
---|
| 305 | if ( ( v & eDirection_Left ) ) orxout()<<" "; |
---|
| 306 | else orxout()<<" "; |
---|
| 307 | orxout()<<"]"; |
---|
| 308 | } |
---|
| 309 | orxout()<<endl; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | } |
---|
| 313 | |
---|
[10930] | 314 | /** |
---|
| 315 | @brief |
---|
| 316 | Print Levelcode (for Debugging only) |
---|
| 317 | */ |
---|
[10835] | 318 | void Hover::LevelOut(){ |
---|
| 319 | for ( int y = 0; y < NumCells; y++ ) |
---|
| 320 | { |
---|
| 321 | for ( int x = 0; x < NumCells; x++ ) |
---|
| 322 | { |
---|
[10930] | 323 | orxout()<<"["; |
---|
[10835] | 324 | if ( levelcode[x][y] < 2) orxout()<<"U"; |
---|
| 325 | else orxout()<<" "; |
---|
| 326 | if ( levelcode[x][y] % 2 == 0) orxout()<<"R"; |
---|
| 327 | else orxout()<<" "; |
---|
| 328 | |
---|
| 329 | orxout()<<" "; |
---|
| 330 | orxout()<<" "; |
---|
[10930] | 331 | orxout()<<"]"; |
---|
[10835] | 332 | } |
---|
| 333 | orxout()<<endl; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
[10930] | 337 | /** |
---|
| 338 | @brief |
---|
| 339 | Generate Levelcode from Maze |
---|
| 340 | */ |
---|
[10835] | 341 | void Hover::RenderMaze() |
---|
| 342 | { |
---|
| 343 | for ( int y = 0; y < NumCells; y++ ) |
---|
| 344 | { |
---|
| 345 | for ( int x = 0; x < NumCells; x++ ) |
---|
| 346 | { |
---|
| 347 | char v = g_Maze[ y * NumCells + x ]; |
---|
| 348 | |
---|
| 349 | if ( !( v & eDirection_Up ) && y >0) levelcode[y][x] |= 2; |
---|
| 350 | if ( !( v & eDirection_Right ) && x <9) levelcode[y][x] |= 1; |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | for ( int y = 3; y < 7; y++ ) |
---|
| 354 | { |
---|
| 355 | for ( int x = 3; x < 7; x++ ) |
---|
| 356 | { |
---|
| 357 | |
---|
| 358 | if(y == 3 && x != 7) |
---|
| 359 | levelcode[y][x] &= 2; |
---|
| 360 | else if (x == 7 && y != 3) |
---|
| 361 | levelcode[y][x] &= 1; |
---|
| 362 | else if(x != 7) |
---|
| 363 | levelcode[y][x] = 0; |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | |
---|
| 370 | |
---|
[10658] | 371 | } |
---|