| 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 | * Florian Zinggeler |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | @file Hover.cc |
|---|
| 31 | @brief Implementation of the Hover class. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | //#include "orxonox/worldentities/pawns/SpaceShip.h" |
|---|
| 35 | #include "Hover.h" |
|---|
| 36 | |
|---|
| 37 | #include "HoverWall.h" |
|---|
| 38 | #include "core/CoreIncludes.h" |
|---|
| 39 | |
|---|
| 40 | #include <iostream> |
|---|
| 41 | #include <string> |
|---|
| 42 | #include <time.h> |
|---|
| 43 | #include <stdlib.h> |
|---|
| 44 | #include <memory.h> |
|---|
| 45 | #include <stdint.h> |
|---|
| 46 | #include <fstream> |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | namespace orxonox |
|---|
| 50 | { |
|---|
| 51 | bool firstTick = true; |
|---|
| 52 | int levelcode[10][10] = |
|---|
| 53 | { |
|---|
| 54 | { 0,0,0,0,0,0,0,0,0,0 }, |
|---|
| 55 | { 0,0,0,0,0,0,0,0,0,0 }, |
|---|
| 56 | { 0,0,0,0,0,0,0,0,0,0 }, |
|---|
| 57 | { 0,0,0,0,0,0,0,0,0,0 }, |
|---|
| 58 | { 0,0,0,0,0,0,0,0,0,0 }, |
|---|
| 59 | { 0,0,0,0,0,0,0,0,0,0 }, |
|---|
| 60 | { 0,0,0,0,0,0,0,0,0,0 }, |
|---|
| 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 | }; |
|---|
| 65 | |
|---|
| 66 | const int NumCells = 10; |
|---|
| 67 | unsigned char* g_Maze = new unsigned char[ NumCells* NumCells ]; |
|---|
| 68 | |
|---|
| 69 | // current traversing position |
|---|
| 70 | int g_PtX; |
|---|
| 71 | int g_PtY; |
|---|
| 72 | |
|---|
| 73 | // return the current index in g_Maze |
|---|
| 74 | int Hover::CellIdx() |
|---|
| 75 | { |
|---|
| 76 | return g_PtX + NumCells * g_PtY; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | int Hover::RandomInt() |
|---|
| 81 | { |
|---|
| 82 | return (rand() % NumCells); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | int Hover::RandomInt4() |
|---|
| 86 | { |
|---|
| 87 | return (rand() % 4); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | RegisterUnloadableClass(Hover); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | Hover::Hover(Context* context) : Gametype(context) |
|---|
| 100 | { |
|---|
| 101 | |
|---|
| 102 | RegisterObject(Hover); |
|---|
| 103 | //this->setHUDTemplate("HoverHUD"); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | void Hover::tick(float dt) |
|---|
| 109 | { |
|---|
| 110 | SUPER(Hover, tick, dt); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | if(firstTick) |
|---|
| 116 | { |
|---|
| 117 | std::fill( g_Maze, g_Maze + NumCells * NumCells, 0 ); |
|---|
| 118 | g_PtX=0; |
|---|
| 119 | g_PtY=0; |
|---|
| 120 | GenerateMaze(); |
|---|
| 121 | MazeOut(); |
|---|
| 122 | RenderMaze(); |
|---|
| 123 | LevelOut(); |
|---|
| 124 | firstTick = false; |
|---|
| 125 | |
|---|
| 126 | for(int y=0; y<10; y++){ |
|---|
| 127 | for(int x=0; x<10; x++){ |
|---|
| 128 | switch(levelcode[y][x]){ |
|---|
| 129 | case 1: new HoverWall(origin_->getContext(), x+1, 10-y, 1); |
|---|
| 130 | break; |
|---|
| 131 | case 3: new HoverWall(origin_->getContext(), x+1, 10-y, 1); |
|---|
| 132 | case 2: new HoverWall(origin_->getContext(), x+1, 10-y, 0); |
|---|
| 133 | default: break; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | //new HoverWall(origin_->getContext(), 1, 1, 1); //Rechts in Y Richtung |
|---|
| 144 | //new HoverWall(origin_->getContext(), 5, 6, 0); //Ueber in x richtung |
|---|
| 145 | //new HoverWall(origin_->getContext(), 5, 5, 0); //Ueber in x richtung |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | void Hover::start() |
|---|
| 154 | { |
|---|
| 155 | |
|---|
| 156 | // Call start for the parent class. |
|---|
| 157 | Gametype::start(); |
|---|
| 158 | |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | void Hover::end() |
|---|
| 163 | { |
|---|
| 164 | // DON'T CALL THIS! |
|---|
| 165 | // Deathmatch::end(); |
|---|
| 166 | // It will misteriously crash the game! |
|---|
| 167 | // Instead startMainMenu, this won't crash. |
|---|
| 168 | GSLevel::startMainMenu(); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | // 0 1 2 3 4 5 6 7 8 |
|---|
| 178 | // U R D L |
|---|
| 179 | int Heading_X[9] = { 0, 0,+1, 0, 0, 0, 0, 0,-1 }; |
|---|
| 180 | int Heading_Y[9] = { 0,-1, 0, 0,+1, 0, 0, 0, 0 }; |
|---|
| 181 | int Mask[9] = { |
|---|
| 182 | 0, |
|---|
| 183 | eDirection_Down | eDirection_Down << 4, |
|---|
| 184 | eDirection_Left | eDirection_Left << 4, |
|---|
| 185 | 0, |
|---|
| 186 | eDirection_Up | eDirection_Up << 4, |
|---|
| 187 | 0, |
|---|
| 188 | 0, |
|---|
| 189 | 0, |
|---|
| 190 | eDirection_Right | eDirection_Right << 4 |
|---|
| 191 | }; |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 195 | |
|---|
| 196 | bool Hover::IsDirValid( eDirection Dir ) |
|---|
| 197 | { |
|---|
| 198 | int NewX = g_PtX + Heading_X[ Dir ]; |
|---|
| 199 | int NewY = g_PtY + Heading_Y[ Dir ]; |
|---|
| 200 | |
|---|
| 201 | if ( !Dir || NewX < 0 || NewY < 0 || NewX >= NumCells || NewY >= NumCells ) return false; |
|---|
| 202 | |
|---|
| 203 | return !g_Maze[ NewX + NumCells * NewY ]; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | eDirection Hover::GetDirection() |
|---|
| 207 | { |
|---|
| 208 | eDirection Dir = eDirection( 1 << RandomInt4() ); |
|---|
| 209 | |
|---|
| 210 | while ( true ) |
|---|
| 211 | { |
|---|
| 212 | for ( int x = 0; x < 4; x++ ) |
|---|
| 213 | { |
|---|
| 214 | if ( IsDirValid( Dir ) ) { return eDirection( Dir ); } |
|---|
| 215 | |
|---|
| 216 | Dir = eDirection( Dir << 1 ); |
|---|
| 217 | |
|---|
| 218 | if ( Dir > eDirection_Left ) { Dir = eDirection_Up; } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | Dir = eDirection( ( g_Maze[ CellIdx() ] & 0xf0 ) >> 4 ); |
|---|
| 222 | |
|---|
| 223 | // nowhere to go |
|---|
| 224 | if ( !Dir ) return eDirection_Invalid; |
|---|
| 225 | |
|---|
| 226 | g_PtX += Heading_X[ Dir ]; |
|---|
| 227 | g_PtY += Heading_Y[ Dir ]; |
|---|
| 228 | |
|---|
| 229 | Dir = eDirection( 1 << RandomInt4() ); |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | void Hover::GenerateMaze() |
|---|
| 234 | { |
|---|
| 235 | int Cells = 0; |
|---|
| 236 | |
|---|
| 237 | for ( eDirection Dir = GetDirection(); Dir != eDirection_Invalid; Dir = GetDirection() ) |
|---|
| 238 | { |
|---|
| 239 | // a progress indicator, kind of |
|---|
| 240 | // if ( ++Cells % 1000 == 0 ) std::cout << "."; |
|---|
| 241 | |
|---|
| 242 | g_Maze[ CellIdx() ] |= Dir; |
|---|
| 243 | |
|---|
| 244 | g_PtX += Heading_X[ Dir ]; |
|---|
| 245 | g_PtY += Heading_Y[ Dir ]; |
|---|
| 246 | |
|---|
| 247 | g_Maze[ CellIdx() ] = Mask[ Dir ]; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | std::cout << std::endl; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | void Hover::MazeOut(){ |
|---|
| 255 | for ( int y = 0; y < NumCells; y++ ) |
|---|
| 256 | { |
|---|
| 257 | for ( int x = 0; x < NumCells; x++ ) |
|---|
| 258 | { |
|---|
| 259 | char v = g_Maze[ y * NumCells + x ]; |
|---|
| 260 | orxout()<<"["; |
|---|
| 261 | if ( ( v & eDirection_Up ) ) orxout()<<"U"; |
|---|
| 262 | else orxout()<<" "; |
|---|
| 263 | if ( ( v & eDirection_Right ) ) orxout()<<"R"; |
|---|
| 264 | else orxout()<<" "; |
|---|
| 265 | if ( ( v & eDirection_Down ) ) orxout()<<" "; |
|---|
| 266 | else orxout()<<" "; |
|---|
| 267 | if ( ( v & eDirection_Left ) ) orxout()<<" "; |
|---|
| 268 | else orxout()<<" "; |
|---|
| 269 | orxout()<<"]"; |
|---|
| 270 | } |
|---|
| 271 | orxout()<<endl; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | void Hover::LevelOut(){ |
|---|
| 277 | for ( int y = 0; y < NumCells; y++ ) |
|---|
| 278 | { |
|---|
| 279 | for ( int x = 0; x < NumCells; x++ ) |
|---|
| 280 | { |
|---|
| 281 | /*orxout()<<"["; |
|---|
| 282 | if ( levelcode[x][y] < 2) orxout()<<"U"; |
|---|
| 283 | else orxout()<<" "; |
|---|
| 284 | if ( levelcode[x][y] % 2 == 0) orxout()<<"R"; |
|---|
| 285 | else orxout()<<" "; |
|---|
| 286 | |
|---|
| 287 | orxout()<<" "; |
|---|
| 288 | orxout()<<" "; |
|---|
| 289 | orxout()<<"]";*/ |
|---|
| 290 | |
|---|
| 291 | orxout()<<levelcode[x][y]; |
|---|
| 292 | } |
|---|
| 293 | orxout()<<endl; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | void Hover::RenderMaze() |
|---|
| 301 | { |
|---|
| 302 | for ( int y = 0; y < NumCells; y++ ) |
|---|
| 303 | { |
|---|
| 304 | for ( int x = 0; x < NumCells; x++ ) |
|---|
| 305 | { |
|---|
| 306 | char v = g_Maze[ y * NumCells + x ]; |
|---|
| 307 | |
|---|
| 308 | if ( !( v & eDirection_Up ) && y >0) levelcode[y][x] |= 2; |
|---|
| 309 | if ( !( v & eDirection_Right ) && x <9) levelcode[y][x] |= 1; |
|---|
| 310 | //if ( !( v & eDirection_Down ) && y>0) levelcode[x][y-1] += 2; |
|---|
| 311 | //if ( !( v & eDirection_Left ) && x>0) levelcode[x-1][y] += 1; |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | for ( int y = 3; y < 7; y++ ) |
|---|
| 315 | { |
|---|
| 316 | for ( int x = 3; x < 7; x++ ) |
|---|
| 317 | { |
|---|
| 318 | |
|---|
| 319 | if(y == 3 && x != 7) |
|---|
| 320 | levelcode[y][x] &= 2; |
|---|
| 321 | else if (x == 7 && y != 3) |
|---|
| 322 | levelcode[y][x] &= 1; |
|---|
| 323 | else if(x != 7) |
|---|
| 324 | levelcode[y][x] = 0; |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | } |
|---|