Difference between revisions of "Cave spawning"
m (This needs to be expanded further...) |
(Completely reworked this page. This is still a WIP.) |
||
Line 1: | Line 1: | ||
− | This page explains the game's process behind spawning enemies in a cave. This is obtained through | + | 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 [https://github.com/JHaack4/CaveGen CaveGen] project. |
− | {{ | + | {{credits|JHawk}} |
==Process== | ==Process== | ||
− | Some time after the cave is built out of its [[Cave unit list file|available units]], the game will run this "main algorithm" in order to spawn objects. | + | Some time after the cave is built out of its [[Cave unit list file|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=== | ===Main algorithm=== | ||
− | # | + | {{todo|Document the algorithms that are linked here, but don't exist in the page.}} |
− | + | # Run the [[#Main category allocation algorithm|main category allocation algorithm]] to figure out how many "main" category entries of each spawn type will be spawned. | |
− | # | + | # Run the [[#Spawn type 5, 8, 1 algorithm|spawn type 5, 8, 1 algorithm]] to spawn all "main" category objects of type 5 (seams). |
− | # | + | # Run the [[#Spawn type 5, 8, 1 algorithm|same algorithm]] to spawn all "main" category objects of type 8 ("special" enemies). |
− | # | + | # Run the [[#Spawn type 5, 8, 1 algorithm|same algorithm]] to spawn all "main" category objects of type 1 ("hard" enemies). |
− | + | # Run the [[#Spawn type 0 algorithm|spawn type 0 algorithm]] to spawn all "main" category objects of type 0 ("easy" enemies). | |
− | ## | + | # Run the [[#Spawn type 6 algorithm|spawn type 6 algorithm]] to spawn all "decorative" category objects. |
− | ## | + | # Run the [[#Spawn treasure algorithm|spawn treasure algorithm]] to spawn all "treasure" category objects. |
− | + | # Run the [[#Spawn dead end algorithm|spawn dead end algorithm]] to spawn all "dead end" category objects. | |
− | |||
− | # | ||
− | === | + | === Main category allocation algorithm === |
− | This 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. |
− | # | + | # 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). |
− | # | + | # Save the weight of each spawn type as 0. |
− | + | # For each "main" category entry in the [[Cave definition file|sublevel's configuration]]... | |
− | ## | + | ## Add the minimum amount of that entry to the number of allocations of the relevant spawn type. |
− | ## | + | ## Add the weight of that entry to the weight of the relevant spawn type. |
− | # | + | # Repeat until the number of allocations reaches the "main" category limit in <code>{f002}</code>: |
− | ### | + | ## With [[#Weighted distribution|weighted randomness]], pick a spawn type, using the spawn type weights. |
− | ## | + | ## Add +1 allocation to the chosen spawn type. |
− | === | + | === Spawn type 5, 8, 1 algorithm === |
− | + | This algorithm spawns objects of types 5, 8, or 1. | |
− | # | + | |
− | # | + | # Repeat n times, n being the number of allocations for the spawn type. |
− | # | + | ## Pick a free spawn point of the given type. |
− | # | + | ### For type 5, this means picking a free seam. Note that not all seams have the same chance of being picked. |
− | # | + | ### 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. |
− | # | + | ## Run the [[#Entry picking algorithm|entry picking algorithm]] with all entries of this type, and the number of spawned objects of this type so far. |
− | # It | + | ## If there is no spawn point or entry that can be picked, finish now. |
+ | ## Spawn one object of that entry on that spawn point. | ||
+ | ## Mark that spawn point as used. | ||
+ | |||
+ | === Spawn type 0 algorithm === | ||
+ | This algorithm spawns objects of type 0. | ||
+ | |||
+ | # Repeat n times, n being the number of allocations for spawn type 0. | ||
+ | ## Pick a free spawn point of type 0. Note that some points cannot be picked due to being too close to important gameplay features. | ||
+ | ## Run the [[#Entry picking algorithm|entry picking algorithm]] with all entries of type 0, and the number of spawned objects type 0 so far. | ||
+ | ## If we picked an entry because we still have minimum amounts to fill, then: | ||
+ | ### Save the number of candidates as the number of objects we still have left to spawn of the given entry to meet its minimum. | ||
+ | ## Otherwise: | ||
+ | ### Save the number of candidates as the number of "main" category objects we can still spawn before meeting the "main" category limit in <code>{f002}</code>. | ||
+ | ## Save the maximum number to spawn as the spawn point's maximum amount, or the number of candidates, whichever is smaller. | ||
+ | ## If the maximum number to spawn is greater than the spawn point's minimum amount, then: | ||
+ | ### Save the number to spawn as a random number between the the spawn point's minimum amount, and our maximum number to spawn. | ||
+ | ## Otherwise: | ||
+ | ### Save the number to spawn as the maximum number to spawn. | ||
+ | ## If there is no spawn point or no entry that can be picked, finish now. | ||
+ | ## If the number to spawn is 0, finish now. | ||
+ | ## Spawn however many objects we wanted to spawn on that spawn point. | ||
+ | ## Randomly move the new objects around the spawn point, using the spawn point's "radius" property. | ||
+ | ## Push the new objects around out of the way of each other. | ||
+ | ## 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. | ||
+ | |||
+ | # If we haven't spawned all necessary minimum amounts for objects of the given type, then: | ||
+ | ## Pick an entry whose minimum amounts haven't been met yet. Prioritize the order in the sublevel configuration file. | ||
+ | # Otherwise, if there are entries of that type with any weight, then: | ||
+ | ## Pick an entry of that type using [[#Weighted distribution|weighted randomness]], using the weights of the entries of that type. | ||
+ | # Otherwise: | ||
+ | ## Pick none. | ||
===Notes=== | ===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. | * 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: | ||
+ | *# It is not in a dead end cave unit. | ||
+ | *# It is of the intended spawn coordinate type (Enemy Group A, Plant, etc.) | ||
+ | *# It is in-bounds. | ||
+ | *# It has not been used already. | ||
+ | *# It is far enough away from the ship's pod (300 units for Enemy Group A, B, and C). | ||
+ | *# 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). | ||
+ | *# 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|[[User:Espyo|Espyo]], [[User:Jimble|Jimble]]}} | {{credits|[[User:Espyo|Espyo]], [[User:Jimble|Jimble]]}} |
Revision as of 19:56, 21 January 2022
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.
Contents
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.
- Run the main category allocation algorithm to figure out how many "main" category entries of each spawn type will be spawned.
- Run the spawn type 5, 8, 1 algorithm to spawn all "main" category objects of type 5 (seams).
- Run the same algorithm to spawn all "main" category objects of type 8 ("special" enemies).
- Run the same algorithm to spawn all "main" category objects of type 1 ("hard" enemies).
- Run the spawn type 0 algorithm to spawn all "main" category objects of type 0 ("easy" enemies).
- Run the spawn type 6 algorithm to spawn all "decorative" category objects.
- Run the spawn treasure algorithm to spawn all "treasure" category objects.
- 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.
- 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).
- Save the weight of each spawn type as 0.
- For each "main" category entry in the sublevel's configuration...
- Add the minimum amount of that entry to the number of allocations of the relevant spawn type.
- Add the weight of that entry to the weight of the relevant spawn type.
- Repeat until the number of allocations reaches the "main" category limit in
{f002}
:- With weighted randomness, pick a spawn type, using the spawn type weights.
- Add +1 allocation to the chosen spawn type.
Spawn type 5, 8, 1 algorithm
This algorithm spawns objects of types 5, 8, or 1.
- Repeat n times, n being the number of allocations for the spawn type.
- Pick a free spawn point of the given type.
- For type 5, this means picking a free seam. Note that not all seams have the same chance of being picked.
- 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.
- Run the entry picking algorithm with all entries of this type, and the number of spawned objects of this type so far.
- If there is no spawn point or entry that can be picked, finish now.
- Spawn one object of that entry on that spawn point.
- Mark that spawn point as used.
- Pick a free spawn point of the given type.
Spawn type 0 algorithm
This algorithm spawns objects of type 0.
- Repeat n times, n being the number of allocations for spawn type 0.
- Pick a free spawn point of type 0. Note that some points cannot be picked due to being too close to important gameplay features.
- Run the entry picking algorithm with all entries of type 0, and the number of spawned objects type 0 so far.
- If we picked an entry because we still have minimum amounts to fill, then:
- Save the number of candidates as the number of objects we still have left to spawn of the given entry to meet its minimum.
- Otherwise:
- Save the number of candidates as the number of "main" category objects we can still spawn before meeting the "main" category limit in
{f002}
.
- Save the number of candidates as the number of "main" category objects we can still spawn before meeting the "main" category limit in
- Save the maximum number to spawn as the spawn point's maximum amount, or the number of candidates, whichever is smaller.
- If the maximum number to spawn is greater than the spawn point's minimum amount, then:
- Save the number to spawn as a random number between the the spawn point's minimum amount, and our maximum number to spawn.
- Otherwise:
- Save the number to spawn as the maximum number to spawn.
- If there is no spawn point or no entry that can be picked, finish now.
- If the number to spawn is 0, finish now.
- Spawn however many objects we wanted to spawn on that spawn point.
- Randomly move the new objects around the spawn point, using the spawn point's "radius" property.
- Push the new objects around out of the way of each other.
- 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.
- If we haven't spawned all necessary minimum amounts for objects of the given type, then:
- Pick an entry whose minimum amounts haven't been met yet. Prioritize the order in the sublevel configuration file.
- Otherwise, if there are entries of that type with any weight, then:
- Pick an entry of that type using weighted randomness, using the weights of the entries of that type.
- Otherwise:
- 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:
- It is not in a dead end cave unit.
- It is of the intended spawn coordinate type (Enemy Group A, Plant, etc.)
- It is in-bounds.
- It has not been used already.
- It is far enough away from the ship's pod (300 units for Enemy Group A, B, and C).
- 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).
- 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).
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).