Thread: Pacman ghost AI and pills

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    Pacman ghost AI and pills

    Whats a way to write AI for the pacman ghosts? In my game there's three ghosts. Also, how would I get the ghosts to react to the super pill?

    Here's my attemp at ai:

    Code:
     struct ghost {
    //	BITMAP* sprites[4];
    	int sprNum;
    	unsigned int points;
    	int oldx;
    	int oldy;
    	int x;
    	int y;
    };
    
    struct ghost ghost1, ghost2, ghost3;
    
    void movecomputer(void)
    {
    	ghost1.oldy = ghost1.y;
    	ghost1.oldx = ghost1.x;
    	if(ghost1.y >0)
    		ghost1.y--;
    	else if(ghost1.y < 14)
    		ghost1.y++;
    
    	level[ghost1.oldy][ghost1.oldx] = 0;
    	level[ghost1.y][ghost1.x] = 5;
    }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, is the ghost trying to follow the player yet? If you've got that, then all you have to do is get it to move in the opposite direction it would go normally, unless you want the ghosts to be really smart... I mean, ghosts want to go to player, ghosts want to run away from player... opposite directions
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    lambs, if you want to have AI comparable to the original you have to consider that each ghost had it's own unique AI:

    1- tried to follow you keeping the shortest path between you and himself (basic chase algo)
    1 - tried to cut you off at the next junction
    1 - tried to cut you off from using the side tunnels
    1 - wandered around aimlessly

    The aggressor would use a basic chase algo; ie: if your.x > my.x then my.x++, etc. *if your left/right position is further right than mine then move me to the right, etc*

    To implement the interceptors' algo's you'd want to add a member var that would represent pac-man's current direction of movement (n, s , e, w). Then (in case #1) if you're moving north in column 0 and the next junction you'll hit is column 0: row 2, then I need to test my position relative to column0: row 2 and make the neccessary movements to close the gap between me and it, (in case #2) I figure the direction your heading in reference to the position of the nearest side tunnel, then I calculate the difference between my position and the tunnel and move that way to cut you off.

    The last one is the easiest, start moving in a random direction and whenever I hit a wall pick a random direction (n,s,e,w) and move that way until I hit another wall, then rather, rinse and repeat.

    Hope this helps.

    **EDIT** forgot to answer the super pill question. For that have a member var (let's say a bool) that is called Flee and set it to true when pac-man has eaten a super pill (once he's eaten a super pill run a time check and after the allotted time has passed reset Flee to false. When he's Flee is true implement a basic flee algo for the ghosts, ie: if your.x > my.x then my.x-- (same with the y position).
    Last edited by jdinger; 11-19-2002 at 02:20 PM.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Thank you for your help so far.

    I'm trying to write the ai for the Aggressor and the code works somewhat except it doesn't seem to react properly. I not sure what the problem could be. Perhaps you see my error?
    Code:
    	if(pacman.x > ghost1.x)
    		 {	ghost1.x++;
    			ghost1.sprNum = BLUEGHOST2_BMP;
    		 }
    	  else if(pacman.x < ghost1.x)
    		 { ghost1.x--;
    			ghost1.sprNum = BLUEGHOST3_BMP;
    		 }
    
    	  if(pacman.y > ghost1.y)
    		{ ghost1.y++;
    		  ghost1.sprNum = BLUEGHOST1_BMP;
    		}
    	  else if(pacman.y < ghost1.y)
    	  { ghost1.y--;
    		 ghost1.sprNum = BLUEGHOST4_BMP;
    	  }
    	}
    
    	if(level[ghost1.y][ghost1.x] == 1 || (ghost1.y < 0) || (ghost1.y > 14) || (ghost1.x < 0) || (ghost1.x > 19))
    	{
    		ghost1.y = ghost1.oldy;
    		ghost1.x = ghost1.oldx;
    	}
    
    	level[ghost1.oldy][ghost1.oldx] = 0;
    	level[ghost1.y][ghost1.x] = 5;
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

Popular pages Recent additions subscribe to a feed