Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int getDirection( int targetX, int targetY, int ghostX, int ghostY, int incX, int incY, Maze& maze )
- {
- // Initialize Values
- std::vector<int> pref( 4 );
- std::vector<int> directions( 4 );
- std::vector<int> distancesSorted( 4 );
- int distances[] = { 0,0,0,0 };
- // Get distances
- distances[UP] = (ghostX-targetX)*(ghostX-targetX)+(ghostY-1-targetY)*(ghostY-1-targetY);
- distances[DOWN] = (ghostX-targetX)*(ghostX-targetX)+(ghostY+1-targetY)*(ghostY+1-targetY);
- distances[LEFT] = (ghostX-1-targetX)*(ghostX-1-targetX)+(ghostY-targetY)*(ghostY-targetY);
- distances[RIGHT] = (ghostX+1-targetX)*(ghostX+1-targetX)+(ghostY-targetY)*(ghostY-targetY);
- for( int i = 0; i < 4; i++ )
- {
- // pref: Directions in order of desirability
- pref[i] = 0;
- // directions: Valid ways to move (To populate pref)
- directions[i] = i;
- // distancesSorted: Distances, to be sorted
- distancesSorted[i] = distances[i];
- }
- std::sort( distancesSorted.begin(), distancesSorted.end() );
- for( int i = 3; i >= 0; i-- )
- {
- // poss: Containing ties (Ties are broken randomly)
- std::vector<int> poss( 0 );
- // dist: smallest distance
- int dist = distancesSorted[i];
- // Populate poss
- for( int j : directions )
- if( distances[j] == dist )
- poss.push_back( j );
- // Randomly shuffle to break the tie
- std::random_shuffle( poss.begin(), poss.end() );
- // Get the winning direction
- int dir = poss.back();
- // Remove from list of directions to populate pref with
- for( auto ind = directions.begin(); ind != directions.end(); ind++ )
- if( *ind == dir )
- {
- directions.erase( ind );
- break;
- }
- // Populate pref with direction
- pref[i] = dir;
- }
- // Direction to be returned
- int dir = -1;
- for( int i = 0; i < 4; i++ )
- {
- // Get most preferable direction
- dir = pref.at( i );
- // If not running into a wall, and not flipping around, return
- if( (dir == UP && !maze.getTile( ghostX, ghostY - 1 ).getWall() && !(incX == 0 && incY == +1))
- || (dir == DOWN && !maze.getTile( ghostX, ghostY + 1 ).getWall() && !(incX == 0 && incY == -1))
- || (dir == LEFT && !maze.getTile( ghostX - 1, ghostY ).getWall() && !(incX == +1 && incY == 0))
- || (dir == RIGHT && !maze.getTile( ghostX + 1, ghostY ).getWall() && !(incX == -1 && incY == 0)) )
- break;
- }
- return dir;
- }
- int flick( Ghost& c )
- {
- // Get direction info
- int incX = c.getXInc();
- int incY = c.getYInc();
- int dir;
- // Set direction to oppose current direction
- if( incX == 0 && incY == -1 )
- dir = DOWN;
- if( incX == 0 && incY == 1 )
- dir = UP;
- if( incX == -1 && incY == 0 )
- dir = RIGHT;
- if( incX == 1 && incY == 0 )
- dir = LEFT;
- // Change and return direction
- c.changeDirection( Direction( dir ) );
- return dir;
- }
- // TODO: Proper include location
- #include <time.h>
- void GameElements::moveGhosts()
- {
- // TODO: Remove static variables
- static int lasttime = time( nullptr );
- static int fright[] = { false, false, false, false };
- static std::vector<int> scatter = { 7,7,5,5 };
- static std::vector<int> chase = { 20,20,20,-1 };
- static bool chasing = false;
- // Initialize
- Tiles pacman = this->getPacmanPos();
- int pacX = pacman.getX();
- int pacY = pacman.getY();
- Tiles pos;
- // ??why??
- // TODO Fix .getX() and .getY()
- std::swap( pacX, pacY );
- { // BLINKY
- // Move if possible
- pos = maze.getTile( red.getX() + red.getXInc(), red.getY() + red.getYInc() );
- if( !pos.getWall() )
- {
- red.move();
- }
- // Default Location
- int tarX = 10000;
- int tarY = -10000;
- // Save Variables
- int gX = red.getX();
- int gY = red.getY();
- int incX = red.getXInc();
- int incY = red.getYInc();
- int dir;
- if( red.getFrightened() == true )
- {
- // Flick back immediately
- if( !fright[0] )
- {
- dir = flick( red );
- // Prepare for random movement
- fright[0] = true;
- } else
- {
- // Go randomly
- tarX = 10000 * (rand() % 3 - 1);
- tarY = 10000 * (rand() % 3 - 1);
- dir = getDirection( tarX, tarY, gX, gY, incX, incY, maze );
- }
- } else
- {
- // Reset for next time
- fright[0] = false;
- // Blinky goes straight
- if( red.getChase() == true )
- {
- tarX = pacX;
- tarY = pacY;
- }
- dir = getDirection( tarX, tarY, gX, gY, incX, incY, maze );
- }
- // Update direction
- red.changeDirection( Direction(dir) );
- }
- { // PINKY
- pos = maze.getTile( pink.getX() + pink.getXInc(), pink.getY() + pink.getYInc() );
- if( !pos.getWall() )
- {
- pink.move();
- }
- int tarX = -10000;
- int tarY = -10000;
- int gX = pink.getX();
- int gY = pink.getY();
- int incX = pink.getXInc();
- int incY = pink.getYInc();
- int dir;
- if( pink.getFrightened() == true )
- {
- if( !fright[1] )
- {
- dir = flick( pink );
- fright[1] = true;
- } else
- {
- tarX = 10000 * (rand() % 3 - 1);
- tarY = 10000 * (rand() % 3 - 1);
- dir = getDirection( tarX, tarY, gX, gY, incX, incY, maze );
- }
- } else
- {
- fright[1] = false;
- if( pink.getChase() == true )
- {
- tarX = pacX + 4*this->pacman.getXInc();
- tarY = pacY + 4*this->pacman.getYInc();
- }
- dir = getDirection( tarX, tarY, gX, gY, incX, incY, maze );
- }
- pink.changeDirection( Direction( dir ) );
- }
- { // INKY
- pos = maze.getTile( blue.getX() + blue.getXInc(), blue.getY() + blue.getYInc() );
- if( !pos.getWall() )
- {
- blue.move();
- }
- int tarX = 10000;
- int tarY = 10000;
- int gX = blue.getX();
- int gY = blue.getY();
- int incX = blue.getXInc();
- int incY = blue.getYInc();
- int dir;
- if( blue.getFrightened() == true )
- {
- if( !fright[2] )
- {
- dir = flick( blue );
- fright[2] = true;
- } else
- {
- tarX = 10000 * (rand() % 3 - 1);
- tarY = 10000 * (rand() % 3 - 1);
- dir = getDirection( tarX, tarY, gX, gY, incX, incY, maze );
- }
- } else
- {
- fright[2] = false;
- if( blue.getChase() == true )
- {
- tarX = red.getX()+2*(pacX-red.getX());
- tarY = red.getY()+2*(pacY-red.getY());
- }
- dir = getDirection( tarX, tarY, gX, gY, incX, incY, maze );
- }
- blue.changeDirection( Direction( dir ) );
- }
- { // CLYDE
- pos = maze.getTile( orange.getX() + orange.getXInc(), orange.getY() + orange.getYInc() );
- if( !pos.getWall() )
- {
- orange.move();
- }
- int tarX = -10000;
- int tarY = 10000;
- int gX = orange.getX();
- int gY = orange.getY();
- int incX = orange.getXInc();
- int incY = orange.getYInc();
- int dir;
- if( orange.getFrightened() == true )
- {
- if( !fright[3] )
- {
- dir = flick( orange );
- fright[3] = true;
- } else
- {
- tarX = 10000 * (rand() % 3 - 1);
- tarY = 10000 * (rand() % 3 - 1);
- dir = getDirection( tarX, tarY, gX, gY, incX, incY, maze );
- }
- } else
- {
- fright[3] = false;
- if( orange.getChase() == true )
- {
- if( (pacX-orange.getX())*(pacX-orange.getX()) + (pacY-orange.getY())*(pacY-orange.getY()) > 64 )
- {
- tarX = pacX;
- tarY = pacY;
- }
- }
- dir = getDirection( tarX, tarY, gX, gY, incX, incY, maze );
- }
- orange.changeDirection( Direction( dir ) );
- }
- // Control chase modes
- if( chasing == true )
- {
- // If scatter time eached
- if( time( nullptr ) >= lasttime + chase.at( 0 ) && chase.at( 0 ) != -1 )
- {
- // Remove used data
- chase.erase(chase.begin());
- // Reset timer
- lasttime = time( nullptr );
- // Set chase mode
- chasing = false;
- red.setChase( chasing );
- pink.setChase( chasing );
- blue.setChase( chasing );
- orange.setChase( chasing );
- // Ghosts must swap direction ("flick back")
- flick( red );
- flick( pink );
- flick( blue );
- flick( orange );
- }
- } else
- {
- // If chase time reached
- if( time( nullptr ) >= lasttime + scatter.at( 0 ) )
- {
- // Remove used data
- scatter.erase(scatter.begin());
- // Reset timer
- lasttime = time( nullptr );
- // Set chase mode
- chasing = true;
- red.setChase( chasing );
- pink.setChase( chasing );
- blue.setChase( chasing );
- orange.setChase( chasing );
- // Ghosts must swap direction ("flick back")
- flick( red );
- flick( pink );
- flick( blue );
- flick( orange );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement