Pikmin 2 cave generation

Caves in are mostly generated randomly, a process with many moving parts.

Generation
When a sublevel is entered, the game generates it randomly; it starts by building random terrain, and then populates it with random objects.

Layout
A sublevel's terrain is composed of several cave units stitched together. Each unit can be a dead end, a corridor, or a room, and has some spots where objects can spawn; each spot contains data about the amount and type of object that can spawn there. In each sublevel's configuration, there is data about which cave units it is allowed to use, and data about how the dead ends, corridors, and rooms should work in terms of quantities. The game then uses this information to randomly create and connect cave units.

Objects
Each sublevel's configuration also has information about what objects to spawn. Entries listed in this information specify what type they are, what object to spawn, a minimum amount, a random filler weight, spawn location, and some other properties. Depending on these properties, different entries are processed differently by the game. With this in mind, entries can be thought of as belonging to one of five different "categories". As a general rule, the "main" category contains enemies, the "decorative" category contains plants, the "treasure" category contains loose treasures, the "gate" category contains gates, and the "dead end" category contains objects specifically meant to appear in dead ends.

On the wiki, each sublevel section in a cave's article contains a basic list of objects that can spawn, as well as their amounts. It also contains a more detailed list that explains what objects the game wants to spawn, in what order, and under what details. These lists reflect the sublevel's data in the game files. Though the basic list can be changed to better document what really happens in-game, the detailed list always reflects what the sublevel's data says. In the detailed list, the ID of each entry is not something recognized by the game, but exists to make the list easier to understand.

Procedure
After generating the sublevel's layout, the game follows a complex process to spawn objects in it. The following is an outline of this process:


 * 1) Decide how many objects to spawn of the "main" category.
 * 2) * This process is split into four sub-categories, which reflect the entry's spawn type.
 * 3) * For each sub-category, respect the minimum amounts first. Then, without going beyond the "main" category object limit defined in the sublevel, add some random filler amounts, if there is any entry with random filler weight.
 * 4) Spawn the "main" objects in the amounts decided.
 * 5) * For each sub-category, spawn the minimum amounts first. Then spawn filler objects (if any) picked at random.
 * 6) Spawn the minimum amount of objects of the "decorative" category.
 * 7) Spawn the minimum amount of objects of the "treasure" category.
 * 8) Possibly spawn more picked at random from the "treasure" category.
 * 9) * Cannot go beyond the "treasure" category object limit defined in the sublevel.
 * 10) * Caves in Story Mode don't make use of this since every treasure is fixed.
 * 11) Spawn the minimum amount of objects of the "dead end" category.
 * 12) Possibly spawn more picked at random from the "dead end" category.
 * 13) * This happens until there are no more dead ends where objects can be placed in.
 * 14) Spawn gates picked at random from the "gate" category.
 * 15) * Cannot go beyond the "gate" category object limit defined in the sublevel.

Missing objects
Because the sublevel generation doesn't use many fixed numbers, and instead opts to use statistics and randomness to generate diverse sublevels, the game can create a sublevel where some objects in its generation data do not spawn. Sometimes an object has a minimum amount of 0, and as luck would have it, it was never chosen when filling out objects at random. At times however, a sublevel may physically be unable to accommodate some of the intended cave units or objects. For example: Frontier Cavern sublevel 4 wants to spawn a Violet Candypop Bud (in a dead end). In one attempt, the generated sublevel has 4 dead ends, so both treasures, the hole, and the Violet Candypop Bud appear in dead ends. But in a different attempt, the game generates a sublevel with 3 dead ends – only the treasures and hole will have a spot, and the Violet Candypop Bud will not show up. This happens often with decorative objects, but can, in rare cases, happen with important objects. Players should be wary about this possibility, and if they find an important item is missing, they can reset the console and try for another sublevel layout.

Scenarios that can make an object not appear include:
 * There aren't enough cave units of a given type to fit all objects that want to go in these units.
 * There aren't enough spawn points of a given type in the entire sublevel to fit all objects that want to use them.
 * The object spawns out of bounds (spawn points randomly offset the spawned objects by an amount).

Note that when the sublevel has finished generating, the game may purposely delete some objects for gameplay reasons. This happens to treasures in Story Mode that have already been collected, and Candypop Buds that do not meet the maximum Pikmin requirement.

Note also that the algorithm has no logic to figure out if the player can logistically reach and recover a treasure in the cave layout it generates. This is usually no problem, but in Hole of Heroes sublevel 6, it can result in one of the treasures being unobtainable, as explained here.

Floor designs
In Pikmin 2, even though most sublevels are randomly generated, there are consistent themes shared between them. The following is a list of themes used in the game, based on what the units are called in the game's files. The screenshots are from Challenge Mode stages, not story mode caves.


 * Concrete
 * Concrete Maze.jpg.]] This design features plants, sandy floors, and concrete walls, with concrete floors sometimes as well, alongside blue rusty pipes at times. This design resembles a basement house foundation and is most likely meant to be one. Eggs and bomb-rocks will occasionally fall from the cave ceiling.


 * Garden
 * Three Color Training.jpg.]] This type of design appears as if the party is outside, in a garden, walled in by small rocks and wood chips. These types of sublevels always have harmful enemies in them, save Mitites, and are popular in caves such as the Snagret Hole and in Challenge Mode. The second to last level in Challenge mode, the Sniper Room, is the only nighttime garden level.


 * Metal
 * Brawny Abyss.png.]] This design has metal floors and low walls, making it possible to throw Pikmin, and ward some enemies, out of bounds. It frequently contains Careening Dirigibugs, Gatling Groinks, or Lithopods, with bomb-rocks appearing on occasion. The first area with a floor like this is the first sublevel of the White Flower Garden.


 * Snow
 * Cave of Pain.jpg.]] A common sublevel design in the Valley of Repose, these sublevels have floors blanketed with snow, and have stone – and occasionally brick – walls. The floors are commonly littered with dying Figworts. Hairy Bulborbs and Snow Bulborbs are often present here.


 * Soil
 * Emperor's Realm.jpg.]] This theme represents a very basic type of cave, with soil floors and stone walls.


 * Toybox
 * P2 Lost Toy Box Sublevel 1.png.]] These sublevel designs are in caves such as Glutton's Kitchen and the Dream Den. These levels are often filled with giant toys and treasures that are used for food, entertainment, and office supplies. The borders of the sublevels are smaller toys that Pikmin and enemies can cross, but not leaders, similar to the garden theme. They sometimes can be covered with a large carpet or wood flooring that stretches for the entirety of the floor.


 * Tiles
 * The Giant's Bath.jpg.]] Bath tiles cover nearly the whole sub-level, and many hazards are found on these types of design, mostly water. Wogpoles will sometimes fall from the cave ceiling.

Backgrounds
Some caves have a background (also called a "skybox", or "VRBOX", as they're known internally), as a wall and/or bottom way off in the distance. Because they're so far away, it may be hard to see them over the fog and lighting. Designs that are meant to give off the idea that the leaders and Pikmin are enclosed in a burrow do not have a background, but other sublevels normally do. There can be any sort of sublevel theme and background combination.

Technical discrepancies
Even though the cave and sublevel information is present in human-readable format within the game's files, some of their settings appear to make no sense, as the values in the files don't match up to what happens in-game. The following assumed discrepancies have been noted:


 * Property {f013} (隠し床, meaning "Hidden floor") of a sublevel indicates whether there's ground under the cave units or if it should be simulated. This is due to the fact that toybox and garden levels' units do not contain ground. However, sublevel 3 of Snagret Hole has this value set to 1 as well, despite the fact that soil-type units always have a ground.
 * Sublevel 8 of the Dream Den, sublevels 1 and 2 of the Hot House and sublevels 1, 2, 4, 5, 6 and 7 of the Collector's Room all have their property {f00A} (VRBOX, the "skybox" model) set to "vrbox". Both sublevels of the Green Hole have that property set to "f010". Both of these values are invalid, and the game defaults to having no "skybox".
 * In addition, the folder that contains the models for the skyboxes,  also contains a file for a "meck" skybox,  . This skybox is not used in the game, and it contains no model or any other file, just an empty folder.
 * The final sublevels of the Subterranean Complex, the Hole of Beasts and the Bulblax Kingdom all have property {f010} (階段を壊す岩で隠す(0=オフ 1=オン)) set to 1. This property decides whether the hole to the next sublevel should be clogged or not. The oddity here is that the final sublevel does not have a hole.
 * The final sublevels in the Cave of Pain, Red Chasm, Collector's Room and the Breeding Ground all have property {f007} (帰還噴水(1=あり)) set to 0. This property decides whether a level contains an escape geyser or not. Naturally, all final sublevels contain one in-game, so the fact that these sublevels have that property set to 0 is a mystery.