void GameParticle::moveGhosts() { std::vector< int >::reverse_iterator white_it = white_ghosts_time_.rbegin(); std::vector< geometry_msgs::Pose >::reverse_iterator spawn_pose_it = spawn_ghosts_poses_.rbegin(); for(std::vector< geometry_msgs::Pose >::reverse_iterator it = ghosts_poses_.rbegin(); it != ghosts_poses_.rend(); ++it, ++white_it, ++spawn_pose_it) { double random_number_of_moves = std::rand() / (double) RAND_MAX; if(*white_it) // if white, change probabilities of moves { if( random_number_of_moves > util::CHANCE_OF_WHITE_GHOST_STOP ) { random_number_of_moves -= util::CHANCE_OF_WHITE_GHOST_STOP; moveGhost(it); // if eaten, go to initial position if( ( it->position.x == pacman_pose_.position.x ) && ( it->position.y == pacman_pose_.position.y ) ) { *it = *spawn_pose_it; *white_it = 0; score_ += 500; } if(random_number_of_moves > util::CHANCE_OF_WHITE_GHOST_ONE_MOVE) { moveGhost(it); // if eaten, go to initial position if( ( it->position.x == pacman_pose_.position.x ) && ( it->position.y == pacman_pose_.position.y ) ) { *it = *spawn_pose_it; *white_it = 0; score_ += 500; } } } } else // if not white, move normally { if( random_number_of_moves > util::CHANCE_OF_GHOST_STOP ) { random_number_of_moves -= util::CHANCE_OF_GHOST_STOP; moveGhost(it); // if kileed, drop score if( ( it->position.x == pacman_pose_.position.x ) && ( it->position.y == pacman_pose_.position.y ) ) { score_ -= 1000; } if(random_number_of_moves > util::CHANCE_OF_GHOST_ONE_MOVE) { moveGhost(it); // if kileed, drop score if( ( it->position.x == pacman_pose_.position.x ) && ( it->position.y == pacman_pose_.position.y ) ) { score_ -= 1000; } } } } } }
void targetGhosts(Location ghosts[],Location *pacman,int xtl,int ytl,int boardHeight,int boardWidth,int active,int state[]){ int i,j,k=blinky,move,a; static int loop=0; double dist[4]; /* This loop will update the locations of each individual ghost */ /* Assumptions: ghosts cannot turn around * Each ghost tries to get to its specified target that turn */ int target[2];//target[0]=ytarget target[1]=xtarget for(k=blinky;k<=active;k++){ if(state[k]==frighten && loop%2){//if a ghost is frightened, they slow down to one space every two loops target[0]=ghosts[k].y; target[1]=ghosts[k].x; continue; } switch(state[k]){ case chase: //this is the chase state, all ghosts go for pacman switch(k){ /* Blinky's movement targets pacman directly. His target is pacman's current position */ case blinky: target[0]=pacman->y; target[1]=pacman->x; break; /* Pinky's target is 3 spaces in front of pacman's current position, toward his orientation */ case pinky: switch(pacman->orientation){ case left: target[0]=pacman->y; target[1]=pacman->x - 3; break; case right: target[0]=pacman->y; target[1]=pacman->x + 3; break; case up: target[0]=pacman->y - 3; target[1]=pacman->x; break; case down: target[0]=pacman->y + 3; target[1]=pacman->x; break; } break; /* Inky's movement uses blinky's location and pacman's location. His target is blinky's target +2 in direction of pacman's orientation times two */ case inky: switch(pacman->orientation){ case left: target[0]=2*(pacman->y - ghosts[blinky].y)+ghosts[blinky].y; target[1]=2*(pacman->x - 2 - ghosts[blinky].x)+ghosts[blinky].x; break; case right: target[0]=2*(pacman->y - ghosts[blinky].y)+ghosts[blinky].y; target[1]=2*(pacman->x + 2 - ghosts[blinky].x)+ghosts[blinky].x; break; case up: target[0]=2*(pacman->y - 2 - ghosts[blinky].y)+ghosts[blinky].y; target[1]=2*(pacman->x - ghosts[blinky].x)+ghosts[blinky].x; break; case down: target[0]=2*(pacman->y + 2 - ghosts[blinky].y)+ghosts[blinky].y; target[1]=2*(pacman->x - ghosts[blinky].x)+ghosts[blinky].x; break; } break; /* If distance greater than 5, target pacman, otherwise, target random target on board */ case clyde: if(targetDistance(ghosts[clyde].x,ghosts[clyde].y,pacman->x,pacman->y)>=5){ target[0]=pacman->y; target[1]=pacman->x; } else { target[0]=rows-1; target[1]=0; } break; } break; case scatter: //This is when the ghosts go to their respective target corners switch(k){ //each has its own designated corner case blinky://top right target[0]=0; target[1]=columns-1; break; case pinky://top left target[0]=0; target[1]=0; break; case inky://bottom right target[0]=rows-1; target[1]=columns-1; break; case clyde://bottom left target[0]=rows-1; target[1]=0; break; } break; case frighten: //This is randomized chaotic movement, usually slower target[0]=rand()%rows; target[1]=rand()%columns; break; case dead: target[0]=6; target[1]=7; break; } /* This next block makes sure the target is on the board */ if(target[0]>=rows) target[0]=rows-1; else if(target[0]<0) target[0]=0; if(target[1]>=columns) target[1]=columns-1; else if(target[1]<0) target[1]=0; /* This block of code decides where to move the ghost in this loop */ if(state[k]==dead && ghosts[k].x==target[1] && ghosts[k].y==target[0]){ //This means they are at their designated target /* Put them into the ghost house */ ghosts[k].y++; //at top of house, move into house and change state state[k]=housed; } else if(state[k]==housed){ ghosts[k].y--; state[k]=chase; } else { moveGhost(&ghosts[k],xtl,ytl,boardHeight,boardWidth,target); } } loop++; }