Ransom Conant / Mbed 2 deprecated MbedPacman

Dependencies:   4DGL-uLCD-SE mbed wave_player

Fork of PacMan_Skeleton_unlock by ECE 2035 TA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ghost.cpp Source File

ghost.cpp

00001 /* Gatech ECE2035 2015 SPRING PAC MAN
00002  * Copyright (c) 2015 Gatech ECE2035
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 
00023 #include "ghost.h "
00024 
00025 //================================================================//
00026 // Private functions
00027 void clean_blk(unsigned int blk_x, unsigned int blk_y)
00028 {
00029     GRID grid_info = map_get_grid_status(blk_x,blk_y);
00030     uLCD.filled_rectangle(grid_info.x, grid_info.y, grid_info.x+GRID_SIZE-1, grid_info.y+GRID_SIZE-1, BACKGROUND_COLOR);
00031 }
00032 
00033 bool check_blk_occupied(unsigned int blk_x, unsigned int blk_y)
00034 {
00035     if (map_get_grid_status(blk_x,blk_y).status==GRID_WALL)
00036         return true;
00037     else
00038         return false;
00039 }
00040 
00041 void draw_ghost(unsigned int blk_x, unsigned int blk_y, unsigned int ghost_color)
00042 {
00043     GRID grid = map_get_grid_status(blk_x, blk_y);
00044     unsigned pos_x = grid.x + GRID_RADIUS;
00045     unsigned pos_y = grid.y + GRID_RADIUS;
00046     uLCD.filled_circle(pos_x,pos_y,GRID_RADIUS,ghost_color);
00047     uLCD.filled_rectangle(pos_x-GRID_RADIUS,pos_y,pos_x+GRID_RADIUS,pos_y+GRID_RADIUS,ghost_color);
00048     uLCD.filled_circle(pos_x+1,pos_y-1,1,BLACK);
00049     uLCD.filled_circle(pos_x-1,pos_y-1,1,BLACK);
00050 }
00051 
00052 void ghost_move(GHOST * g, unsigned int new_blk_x, unsigned int new_blk_y)
00053 {
00054     // clean up ghost at old position
00055     clean_blk(g->x, g->y);
00056     // clean the block at new position
00057     clean_blk(new_blk_x, new_blk_y);
00058     // draw the ghost at new position
00059     draw_ghost(new_blk_x, new_blk_y, g->color);
00060 
00061     // recover map component
00062     map_draw_grid(g->x, g->y);
00063 
00064     g->x = new_blk_x;
00065     g->y = new_blk_y;
00066 }
00067 
00068 // move ghost up/down/left/right
00069 // return false if failed. true if success
00070 bool ghost_up(GHOST * g)
00071 {
00072     if (check_blk_occupied(g->x, g->y-1)) return false;
00073     if (g->y==0)
00074         ghost_move(g, g->x, (NUM_GRID_Y-1));
00075     else
00076         ghost_move(g, g->x, g->y-1);
00077     return true;
00078 }
00079 bool ghost_down(GHOST * g)
00080 {
00081     if (check_blk_occupied(g->x, g->y+1)) return false;
00082 
00083     if (g->y==(NUM_GRID_Y-1))
00084         ghost_move(g, g->x, 0);
00085     else
00086         ghost_move(g, g->x, g->y+1);
00087     return true;
00088 }
00089 
00090 bool ghost_left(GHOST * g)
00091 {
00092     if (check_blk_occupied(g->x-1, g->y)) return false;
00093     if (g->x==0) 
00094         ghost_move(g, (NUM_GRID_X-1), g->y);
00095     else
00096         ghost_move(g, g->x-1, g->y);
00097     return true;
00098 }
00099 bool ghost_right(GHOST * g)
00100 {
00101     if (check_blk_occupied(g->x+1, g->y)) return false;
00102     if (g->x==(NUM_GRID_X-1)) 
00103         ghost_move(g, 0, g->y);
00104     else
00105         ghost_move(g, g->x+1, g->y);
00106     return true;
00107 }
00108 
00109 void gen_random_direction(GHOST * g)
00110 {
00111     bool blocked[4];
00112     blocked[0] = check_blk_occupied(g->x, g->y-1);    //up
00113     blocked[1] = check_blk_occupied(g->x, g->y+1);    //down
00114     blocked[2] = check_blk_occupied(g->x-1, g->y);    //left
00115     blocked[3] = check_blk_occupied(g->x+1, g->y);    //right
00116     
00117     unsigned int npath=0;
00118     unsigned int outcome=0;
00119     if (blocked[0]==false) npath++;
00120     if (blocked[1]==false) npath++;
00121     if (blocked[2]==false) npath++;
00122     if (blocked[3]==false) npath++;
00123     
00124     unsigned curr=(unsigned int) g->ghost_motion;
00125     unsigned reverse;
00126     if ((curr%2)==0)
00127         reverse = curr+1;
00128     else 
00129         reverse = curr-1;
00130     
00131     unsigned off=rand();
00132     for (int i=0;i<4;i++)
00133     {
00134         outcome=(off+i)%4;
00135         // skip the reverse path if possible
00136         if (npath>1 && reverse==outcome) continue;
00137         if (blocked[outcome]==false) break;
00138     }
00139     g->ghost_motion = (GHOST_MOTION)outcome;
00140     
00141     return;
00142 }
00143 //======================================================================================//
00144 //All the function descriptions are in the ghost.h file
00145 
00146 //Create a DLL for ghosts
00147 DLinkedList* ghostDLL = NULL;
00148 
00149 
00150 void ghost_init(void){
00151     ghostDLL = create_dlinkedlist();
00152 }
00153 
00154 // Public functions
00155 void ghost_create(unsigned int blk_x, unsigned int blk_y, unsigned int color)
00156 {
00157     GHOST* newGhost = (GHOST*)malloc(sizeof(GHOST));
00158     newGhost->x = blk_x;
00159     newGhost->y = blk_y;
00160     newGhost->color = color;
00161     insertHead(ghostDLL, (void*)newGhost);
00162 }
00163 
00164 DLinkedList* get_ghost_list(void) {
00165     //Your code here
00166     return ghostDLL;
00167 }
00168 
00169 void ghost_show(DLinkedList* list)
00170 {
00171     //Your code here
00172     //Functions like map_draw_grid, clean_blk, draw_ghost may be useful
00173     GHOST* newGhost = (GHOST*)getHead(list);
00174     while(newGhost){
00175         draw_ghost(newGhost->x,newGhost->y,newGhost->color);
00176         newGhost = (GHOST*)getNext(list);
00177     }
00178 }
00179 
00180 int ghost_collision(DLinkedList* list, int pac_x, int pac_y, int invuln){
00181     GHOST* newGhost = (GHOST*)getHead(list);
00182     while(newGhost){
00183         if(newGhost->x == pac_x && newGhost->y == pac_y){
00184             if(invuln > 0){
00185                 newGhost->ghost_motion = GHOST_DIED;
00186                 return 10;
00187             }
00188             else{
00189                 return 1;
00190             }
00191         
00192         }
00193         newGhost = (GHOST*)getNext(list);
00194     }
00195     return 0;
00196 }
00197 
00198 void ghost_random_walk(void)
00199 {
00200     GHOST* newGhost = (GHOST*)getHead(ghostDLL);
00201     while(newGhost){ 
00202         if(newGhost->ghost_motion == GHOST_DIED){ 
00203             //recover map grid
00204             map_draw_grid(newGhost->x, newGhost->y);
00205             //delete from the linked list
00206             newGhost = (GHOST*)deleteForward(ghostDLL);    
00207         }else{
00208              gen_random_direction(newGhost);   
00209              switch (newGhost->ghost_motion) {
00210                 case GHOST_UP:
00211                     ghost_up(newGhost);
00212                     break;
00213                 case GHOST_DOWN:
00214                     ghost_down(newGhost);
00215                     break;
00216                 case GHOST_LEFT:
00217                     ghost_left(newGhost);
00218                     break;
00219                 case GHOST_RIGHT:
00220                     ghost_right(newGhost);
00221                 break;
00222                 default:
00223                      break;
00224             }                            
00225         }
00226         //advance the loop 
00227         newGhost = (GHOST*)getNext(ghostDLL);
00228     } 
00229 }
00230 
00231 //=======================================================================//
00232