Difference between revisions of "Cave spawning"

From Pikmin Technical Knowledge Base
Jump to navigation Jump to search
(Corrections.)
(Added a section explaining weighted distribution.)
Line 31: Line 31:
 
### Spawn that enemy in these coordinates (with slight variation).
 
### Spawn that enemy in these coordinates (with slight variation).
 
## Mark this spawn coordinate as used.
 
## Mark this spawn coordinate as used.
 +
 +
==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 with 90% Red Bulborbs and 10% Wollywogs, picked randomly. 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 <code>&lt;object's weight&gt; / &lt;sum&gt;</code>. So for example, if you have the following list:
 +
 +
{| class="wikitable"
 +
|-
 +
! 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).
  
 
[[Category:Pikmin 2]]
 
[[Category:Pikmin 2]]
 
[[Category:Algorithms]]
 
[[Category:Algorithms]]

Revision as of 01:05, 1 November 2016

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.

Process

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

Main algorithm

  1. For each TekiInfo entry in the sublevel's configuration...
    1. Store the minimum amount of enemies to spawn.
    2. Store the spawn coordinate type.
    3. Do the spawn algorithm with this enemy type, 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 last step.
      2. Store the enemy's type.
      3. Store the spawn coordinate type.
      4. Do the spawn algorithm with this enemy type, amount of 1, and spawn coordinate type.

Spawn algorithm

  1. While the intended amount of enemies hasn't been spawned yet...
    1. If all of the possible spawn positions have been used up, finish the whole enemy spawn process.
    2. Pick a random spawn coordinate (every unit in the cave has a set of spawn coordinates).
    3. If this coordinate is out of bounds, skip it.
    4. If this coordinate has already been used, skip it.
    5. Decide how many enemies to spawn here, picking randomly between the spawn coordinate's amount limits.
    6. For each one in that amount...
      1. If the number of enemies spawned here surpasses the intended amount, leave this algorithm.
      2. Spawn that enemy in these coordinates (with slight variation).
    7. Mark this spawn coordinate as used.

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 with 90% Red Bulborbs and 10% Wollywogs, picked randomly. 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).