Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10835


Ignore:
Timestamp:
Nov 23, 2015, 3:32:48 PM (8 years ago)
Author:
meierman
Message:

Maze Generator works

Location:
code/branches/hoverHS15
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/hoverHS15/cmake/CompilerConfigGCC.cmake

    r9686 r10835  
    7272ADD_COMPILER_FLAGS("-fno-omit-frame-pointer" CACHE)
    7373
     74
    7475# Enable non standard floating point optimisations
    7576ADD_COMPILER_FLAGS("-ffast-math" CACHE)
  • code/branches/hoverHS15/src/modules/hover/Hover.cc

    r10787 r10835  
    3838#include "core/CoreIncludes.h"
    3939
     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
    4049namespace orxonox
    4150{
     
    4352    int levelcode[10][10] =
    4453        {
    45         { 0,0,0,0,0,0,0,0,0,0 }, // row 0
    46         { 1,0,0,0,0,0,0,0,0,0 }, // row 0
    47         { 1,0,0,0,0,0,0,0,0,0 }, // row 0
    48         { 1,1,3,0,1,3,0,0,0,0 }, // row 0
    49         { 1,0,3,2,3,2,0,0,0,0 }, // row 0
    50         { 1,0,1,0,1,0,0,0,0,0 }, // row 0
    51         { 1,2,2,0,0,0,0,0,0,0 }, // row 0
    52         { 1,0,0,0,0,0,0,0,0,0 },  // row 0
    53         { 1,0,0,0,0,0,0,0,1,0 },// row 1
    54         { 1,0,0,0,0,0,0,1,2,0 } // row 2
     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 }
    5564        };
    5665
     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
    5790
    5891    RegisterUnloadableClass(Hover);
     92
     93
     94
     95
     96
     97
    5998
    6099    Hover::Hover(Context* context) : Gametype(context)
     
    76115        if(firstTick)
    77116        {
     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();
    78124            firstTick = false;
    79125
    80126            for(int y=0; y<10; y++){
    81127                for(int x=0; x<10; x++){
    82 
    83128                    switch(levelcode[y][x]){
    84129                        case 1: new HoverWall(origin_->getContext(), x+1, 10-y, 1);
     
    96141
    97142           
    98             //new HoverWall(origin_->getContext(), 1, 1, 1);
    99             //new HoverWall(origin_->getContext(), 1, 1, 0);
     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
    100146        }     
    101147
     
    122168        GSLevel::startMainMenu();
    123169    }
     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
    124332}
  • code/branches/hoverHS15/src/modules/hover/Hover.h

    r10751 r10835  
    5858#include "tools/Timer.h"
    5959
     60
     61
    6062namespace orxonox
    6163{
     64
     65    enum eDirection
     66    {
     67        eDirection_Invalid = 0,
     68        eDirection_Up      = 1,
     69        eDirection_Right   = 2,
     70        eDirection_Down    = 4,
     71        eDirection_Left    = 8
     72    };
    6273
    6374    class _HoverExport Hover : public Gametype
     
    8091            WeakPtr<HoverOrigin> origin_;
    8192
     93        private:
     94            int CellIdx();
     95            int RandomInt();
     96            int RandomInt4();
     97
     98
     99            bool IsDirValid( eDirection Dir );
     100            eDirection GetDirection();
     101            void GenerateMaze();
     102            void RenderMaze();
     103            void MazeOut();
     104            void LevelOut();
     105
     106
    82107    };
    83108}
Note: See TracChangeset for help on using the changeset viewer.