Cave spawning

From Pikmin Technical Knowledge Base
Revision as of 20:56, 21 January 2022 by Espyo (talk | contribs) (Completely reworked this page. This is still a WIP.)
Jump to navigation Jump to search

This page explains the game's process behind spawning enemies in a cave. This is obtained through code analysis, both of the game code and the CaveGen project.

Credits: JHawk

Process

Some time after the cave is built out of its available units, the game will run this "main algorithm" in order to spawn objects. Note that this outline will only include information about which objects spawn, and how many. It does not have information on where they spawn, using the "score" mechanic.

Main algorithm

To do: Document the algorithms that are linked here, but don't exist in the page.

  1. Run the main category allocation algorithm to figure out how many "main" category entries of each spawn type will be spawned.
  2. Run the spawn type 5, 8, 1 algorithm to spawn all "main" category objects of type 5 (seams).
  3. Run the same algorithm to spawn all "main" category objects of type 8 ("special" enemies).
  4. Run the same algorithm to spawn all "main" category objects of type 1 ("hard" enemies).
  5. Run the spawn type 0 algorithm to spawn all "main" category objects of type 0 ("easy" enemies).
  6. Run the spawn type 6 algorithm to spawn all "decorative" category objects.
  7. Run the spawn treasure algorithm to spawn all "treasure" category objects.
  8. Run the spawn dead end algorithm to spawn all "dead end" category objects.

Main category allocation algorithm

This algorithm saves how many objects of type 0 we want to spawn, how many of type 1, of type 5, and of type 8.

  1. Save the number of allocations of spawn type 0 ("easy" enemies) as 0. Repeat for types 1 ("hard" enemies), 5 (seams), and 8 ("special" enemies).
  2. Save the weight of each spawn type as 0.
  3. For each "main" category entry in the sublevel's configuration...
    1. Add the minimum amount of that entry to the number of allocations of the relevant spawn type.
    2. Add the weight of that entry to the weight of the relevant spawn type.
  4. Repeat until the number of allocations reaches the "main" category limit in {f002}:
    1. With weighted randomness, pick a spawn type, using the spawn type weights.
    2. Add +1 allocation to the chosen spawn type.

Spawn type 5, 8, 1 algorithm

This algorithm spawns objects of types 5, 8, or 1.

  1. Repeat n times, n being the number of allocations for the spawn type.
    1. Pick a free spawn point of the given type.
      1. For type 5, this means picking a free seam. Note that not all seams have the same chance of being picked.
      2. For types 8 or 1, this means a standard spawn point. Note that some points cannot be picked due to being too close to important gameplay features.
    2. Run the entry picking algorithm with all entries of this type, and the number of spawned objects of this type so far.
    3. If there is no spawn point or entry that can be picked, finish now.
    4. Spawn one object of that entry on that spawn point.
    5. Mark that spawn point as used.

Spawn type 0 algorithm

This algorithm spawns objects of type 0.

  1. Repeat n times, n being the number of allocations for spawn type 0.
    1. Pick a free spawn point of type 0. Note that some points cannot be picked due to being too close to important gameplay features.
    2. Run the entry picking algorithm with all entries of type 0, and the number of spawned objects type 0 so far.
    3. If we picked an entry because we still have minimum amounts to fill, then:
      1. Save the number of candidates as the number of objects we still have left to spawn of the given entry to meet its minimum.
    4. Otherwise:
      1. Save the number of candidates as the number of "main" category objects we can still spawn before meeting the "main" category limit in {f002}.
    5. Save the maximum number to spawn as the spawn point's maximum amount, or the number of candidates, whichever is smaller.
    6. If the maximum number to spawn is greater than the spawn point's minimum amount, then:
      1. Save the number to spawn as a random number between the the spawn point's minimum amount, and our maximum number to spawn.
    7. Otherwise:
      1. Save the number to spawn as the maximum number to spawn.
    8. If there is no spawn point or no entry that can be picked, finish now.
    9. If the number to spawn is 0, finish now.
    10. Spawn however many objects we wanted to spawn on that spawn point.
    11. Randomly move the new objects around the spawn point, using the spawn point's "radius" property.
    12. Push the new objects around out of the way of each other.
    13. Mark that spawn point as used.

Entry picking algorithm

This algorithm picks an object entry from a given set, and chooses an object to spawn. It checks how many of this type have been spawned so far to decide which one to pick next.

  1. If we haven't spawned all necessary minimum amounts for objects of the given type, then:
    1. Pick an entry whose minimum amounts haven't been met yet. Prioritize the order in the sublevel configuration file.
  2. Otherwise, if there are entries of that type with any weight, then:
    1. Pick an entry of that type using weighted randomness, using the weights of the entries of that type.
  3. Otherwise:
    1. Pick none.

Notes

  • 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.
  • 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 far enough away from the ship's pod (300 units for Enemy Group A, B, and C).
    6. It is far enough away from the hole (200 units for Enemy Group A, 150 units for Enemy Group C, not applicable for Enemy Group B).
    7. It is far enough away from the geyser (200 units for Enemy Group A, 150 units for Enemy Group C, not applicable for Enemy Group B).
Credits: Espyo, Jimble

Weighted distribution

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).