• they\them

I like games: retro, fighting, doom etc and may occasionally rechost 🔞
lesbian donkey kong


rgmechex
@rgmechex

Pac-Man uses a software-based random number generator. I made a video all about it, which you can check out below. The game uses it only to determine what directions frightened ghosts move throughout the maze.

Thumbnail for the video 'How Frightened Ghosts Decide Where to Go'I feel like I've referenced this video once already...

Ms. Pac-Man reuses that same random number generator for the same purpose. However, it also has another source of randomness that it uses for a few other purposes. Today we'll look at one of them! This slight modification changed how top-level players play the game, and why you don't see any "perfect games" of Ms. Pac-Man.


The reason why you can even play a perfect game of Pac-Man is because the game is almost entirely deterministic. There is no randomness, besides the frightened ghosts' movement patterns. However, once you pass level 20, ghosts no longer become frightened after a power pellet is eaten, and the game becomes purely deterministic, meaning you can make the same movements each time and the ghosts will move exactly the same way each time. This makes it easy to plan a route that will allow you to eat all the pellets without losing a single life, all the way to the level limit.

Ms. Pac-Man shakes that up by starting each level with a short moment of completely random ghost movement. This puts the ghosts in a jumbled state, and there is no way to reliably find a single path you can take advantage of. You would have to memorize a movement pattern for every single combination of ghost movement patterns at the start, which quickly becomes way too many for any normal human being to memorize.

A bunch of instances of the start of Ms. Pac-Man overlaid on top of each otherEven though Ms. Pac-Man doesn't move at all, the ghosts choose many different paths at the start of the game.

In Pac-Man, the ghosts initially start in scatter mode. They each move into their respective corners. They do this by using a target tile, that is, they try to move closer to a single tile. (For more info on that, see my video on the original Pac-Man Ghost AI.) During scatter mode, each ghost has their own target tile they are trying to reach in each of the four corners of the maze.

The Pac-Man maze with target tiles in the four corners markedSince the ghosts can't reach their targets (well, even if they could), they continue to make circles in each of the four corners.

For the original Pac-Man, the ghosts alternate between these targets (scatter mode), and targets based off of Pac-Man's position (chase mode). In Ms. Pac-Man, scatter mode is a bit different. There are still four target tiles of interest per maze.

Ms. Pac-Man's third maze with target tiles in the four corners and bottom center markedDue to what I presume is a typo, the third maze type (pictured here), uses the bottom-center target instead of the bottom-left target. Otherwise, these are identical to the scatter mode targets in Pac-Man.

However, instead of one target belonging to each ghost, all ghosts use all four targets, and the tile they target in this particular instance is picked randomly. Inky and Sue call the RNG function to pick one of these four corners, but then immediately discard it and instead target their own tile in their respective corners from the original game. So effectively, only Blinky and Pinky are random at the start of each level.

Let's look at the RNG function used in Ms. Pac-Man.

; $9561 - get a random target tile
randomTarget:
  PUSH AF
  PUSH BC
  PUSH HL
  LD   HL,mazeTargets ; \ HL gets one of the
  CALL selectMaze     ; | pointers down there
  LD   L,C            ; | based on the current
  LD   H,B            ; / maze
  LD   A,R            ; \ get a 'random' number
  AND  #%00000110     ; / limit to 0,2,4,6
  RST  $10            ; \ DE gets the word
  LD   E,A            ; | at that pointer
  INC  HL             ; | plus the offset
  LD   D,(HL)         ; / in A
  POP  HL
  POP  BC
  POP  AF
  RET 

; $9578 - pointers to list of target
;         tile coordinate pairs
mazeTargets:
  dw maze1Targets
  dw maze2Targets
  dw maze3Targets
  dw maze4Targets

A lot of code just to say "pick one of four numbers from a list of four lists."

Really the only important part to look at here is the single instruction LD A,R. Pac-Man and Ms. Pac-Man run on a Z80 processor. This processor has an R register, which holds a very peculiar value. This is used as an internal counter in the CPU to help refresh dynamic RAM during instructions that don't access external memory. It gets incremented on every instruction executed, and rolls over at $7F back to $00. This value changes independently from whatever code the CPU is running, so it was commonly used as a source of randomness, since you can never really be sure what number this register holds at any given time unless you are counting cycles.

This results in a pretty good RNG function for Ms. Pac-Man, since it means there's no way for the player to manipulate it and the ghosts into moving in a certain way every time.

And yes, the original Pac-Man also runs on a Z80 and has access to this R register, but it did not use it at all.


You can support Retro Game Mechanics Explained on Patreon here! Any and all support is greatly appreciated!


You must log in to comment.

in reply to @rgmechex's post:

Yes, Ms. Pac also uses the R register for fruit selection, calculating (R % 32) % 7. There is a 5/32 chance of getting a cherry, strawberry, orange, or pretzel, and a 4/32 chance of getting an apple, pear, or banana.