Penguinspawner
Overview
The Penguinspawner component is responsible for autonomously generating penguin colonies (rookeries) during winter when conditions are met (e.g., sufficient distance from players and structures, proximity to water, and seasonal timing). It tracks active colonies, manages flock spawning logic, handles penguin lifecycle events (birth, death, mutation), and persists colony data across sessions. It operates exclusively on the master simulation and enforces world-wide constraints such as maximum penguin count and colony spacing.
Dependencies & Tags
- Requires
TheWorld.ismastersim(asserted in constructor). - Uses components
knownlocations(on spawned penguins) andMiniMapEntity(onpenguin_iceobjects). - Spawns prefabs:
penguin,mutated_penguin,penguin_ice,rock_ice. - No components are added to its owner entity (
self.inst) beyond internal logic; it is attached to a dedicated world-level entity (e.g., the world spawner or manager). - No tags are added to the spawner itself.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | — | The owning entity (world spawner instance). |
_colonies | table | {} | Array of colony tables, each containing rookery (position), members (set of penguins), ice (prefab instance), is_mutated (bool), and numspawned (count). |
_maxColonySize | number | 12 | Maximum penguins allowed per colony. |
_totalBirds | number | 0 | Current total number of live penguins (global). |
_flockSize | number | TUNING.PENGUINS_FLOCK_SIZE | Base flock size for new spawns. |
_spacing | number | 60 | Minimum squared distance between rookeries (in tiles²). |
_checktime | number | 5 | Interval (in seconds) between colony spawn checks. |
_lastSpawnTime | number | 0 | Timestamp of the last successful flock spawn. |
_maxColonies | number | TUNING.PENGUINS_MAX_COLONIES | Maximum number of colonies allowed in the world. |
_maxPenguins | number | _flockSize × (TUNING.PENGUINS_MAX_COLONIES + TUNING.PENGUINS_MAX_COLONIES_BUFFER) | Hard cap on total penguins in the world. |
_spawnInterval | number | TUNING.PENGUINS_SPAWN_INTERVAL | Minimum time (in seconds) between consecutive spawns. |
_numBoulders | number | TUNING.PENGUINS_DEFAULT_NUM_BOULDERS | Number of rock_ice boulders to generate per rookery. |
_activeplayers | table | {} | List of {player = PlayerEntity, lastSpawnLoc = Vector3 or nil} objects tracking player proximity for spawn avoidance. |
Main Functions
AddToColony(colonyNum, pengu)
- Description: Assigns a newly spawned penguin to an existing colony. Updates colony membership, increments counters, registers death callbacks, and stores colony location as
rookeryandhomeon the penguin’sknownlocationscomponent. - Parameters:
colonyNum(number): Index of the target colony in_colonies. Ifnil, the penguin is not assigned.pengu(Entity): The penguin entity being added.
EstablishColony(loc)
- Description: Attempts to find or create a new rookery at or near the given world position (
loc), ensuring compliance with spacing, terrain, LOS, structure, and water proximity rules. If successful, creates apenguin_iceprefab, populates it with boulders, marks it as mutated if in a Lunacy Area, and registers the colony in_colonies. - Parameters:
loc(Vector3): Approximate spawn center (typically land near water).
- Returns:
number(colony index) orfalseon failure.
SpawnFlock(colonyNum, loc, check_angle)
- Description: Spawns a randomized flock of penguins (size =
_flockSize ± variance) aroundlocfor the specified colony. Delays each spawn slightly usingDoTaskInTimeand respects the colony’s currentnumspawned. - Parameters:
colonyNum(number): Index of the colony to assign spawned penguins.loc(Vector3): Center position of the spawn area.check_angle(number): Orientation angle (in degrees) for the initial penguin placement.
SpawnPenguin(inst, spawner, colonyNum, pos, angle)
- Description: Spawns a single penguin (
penguinormutated_penguindepending on colony status) atposwith the givenangle. Registers it with the colony. - Parameters:
inst,spawner: Context arguments (unused; retained for callback compatibility).colonyNum(number): Colony index.pos(Vector3): Spawn position.angle(number): Heading (in degrees).
OnSave()
- Description: Serializes active colonies for world save. Stores rookery coordinates, mutation status, and
numspawnedcount per colony. - Returns:
tablewith keycolonies, an array of{x, y, z, is_mutated, numspawned}tables.
OnLoad(data)
- Description: Loads colony data from a saved state, restoring
penguin_iceprefabs and their mutation status. - Parameters:
data(table): Save data containingcolonies.
GetDebugString()
- Description: Returns a formatted string summarizing current penguin/spawner state for debugging (e.g., counts, limits, next spawn time).
- Returns:
string(e.g.,"10/120 Penguins, 3/5 Colonies, next spawn in :2.5").
Events & Listeners
- Listens for
ms_playerjoined→ triggersOnPlayerJoined - Listens for
ms_playerleft→ triggersOnPlayerLeft - Listens for
ms_setpenguinnumboulders→ triggersOnSetNumBoulders(val)(note: functionOnSetNumBouldersis referenced but not defined in the source; likely a no-op or stub) - Listens for
seasontick→ triggersOnSeasonTick - Triggers
deathandonremoveevents on each penguin (handled viaLostPenguin) - Uses
DoTaskInTimeto schedule recurringTryToSpawnFlockchecks and per-flock penguin spawns.