Cave spawning

From Pikmin Technical Knowledge Base
Jump to: navigation, search

This page explains the game's process behind spawning enemies in a cave. This is obtained through observation, and is subject to have some parts incorrect.

Editor's note: Until further notice, this only applies to spawning enemies.


Some time after the cave is built out of its available units, the game will run this "main algorithm" in order to spawn objects.

Main algorithm[edit]

  1. For each TekiInfo entry in the sublevel's configuration, in order...
    1. Run the spawn algorithm using this entry's class, minimum spawn amount, and spawn coordinate type.
  2. If the number of enemies hasn't reached {f002} yet...
    1. For each TekiInfo entry in the sublevel's configuration...
      1. Grab its random distribution weight and add it to a list.
    2. While the number of enemies hasn't reached {f002}...
      1. Randomly decide what enemy to spawn next, using the weighted list built in the previous step.
      2. Store the enemy's type.
      3. Store the spawn coordinate type.
      4. Run the spawn algorithm using this entry's class, amount of 1, and this entry's spawn coordinate type.
  3. Remove every spawned object that is not meant to be there (collected treasures, Candypop Buds that disappear when you have 20+, etc.)

Spawn algorithm[edit]

This algorithm takes as parameters an object class, a spawn amount, and a spawn coordinate type.

  1. While the intended amount of enemies hasn't been spawned yet...
    1. Pick a valid spawn coordinate randomly.
    2. If there is no valid spawn coordinate remaining, leave this algorithm.
    3. Mark this spawn coordinate as used.
    4. Decide how many objects to spawn here, picking randomly between the spawn coordinate's amount limits.
    5. For each one in that amount...
      1. If the number of objects spawned here surpasses the intended amount, leave this algorithm.
      2. Spawn that object in these coordinates (randomly translated inside the circle decided by the spawn coordinate's radius).

Coordinate check[edit]

A spawn coordinate is valid if all of the following are true:

  1. It is not in a dead end cave unit.
  2. It is of the intended spawn coordinate type (Enemy Group A, Plant, etc.)
  3. It is in-bounds.
  4. It has not been used already.
  5. It is farther than 300 units from the ship's pod.


  • If a spawn coordinate has a radius of 0, all objects to spawn will be placed in the same spot, and will physically push each other apart. Otherwise, the objects will be forced to maintain a fixed minimum distance from one another, even if that distance will push them beyond the spawn coordinate's radius.
Credits: Espyo, Jimble

Weighted distribution[edit]

Some types of object may come up more often than others. For instance, a developer could want a cave where the enemy slots are filled randomly, with each one having a 90% chance of being a Red Bulborb, and 10% chance of being a Wollywog. This is known as weighted random distribution. The way Pikmin 2 does this is that every object definition has a weight number that goes from 0 to 9. Any object definition with a weight of 0 will not be picked, though.

To know what the chances are of the game picking a given object, all of the weight numbers are summed up, and the chance of that object is <object's weight> / <sum>. So for example, if you have the following list:

Object Weight
Red Bulborb 4
Wollywog 7
Water Dumple 3

The chances of spawning a Red Bulborb are 28.5% (4 in 14), the chances of a Wollywog are 50% (7 in 14), and the chances of a Water Dumple are 21.4% (3 in 14).