Files
Paradise/code/game/objects/effects/spawners/random/pool/spawn_pool.dm
warriorstar-orion c4e4487452 API: Spawn pools, a distributed loot manager. (#27199)
* api: Spawn pools, a distributed loot manager.

* meh

* documentation and cleanups

* how do numbers work

* word wrapping

* fixes found from prototyping
2024-11-22 19:00:23 +00:00

60 lines
2.0 KiB
Plaintext

/// Keeps track of the available points for a given pool, as well as any
/// spawners that need to keep track globally of the number of any specific item
/// that they spawn.
/datum/spawn_pool
/// The ID of the spawn pool. All spawners registered to this pool must use this ID.
var/id
/// The number of points left for the spawner to use. Starts at its initial value.
var/available_points = 0
/// A list of all spawners registered to this pool.
var/list/known_spawners = list()
/// A key-value list of spawners with TRUE `unique_picks` to a shared copy of their
/// loot pool. When items from one of these spawners are spawned, it is removed
/// from the shared loot pool so it never spawns again.
var/list/unique_spawners = list()
/// A list of spawners whose `guaranteed` is `TRUE`. These spawners will
/// always spawn, and always before anything else,
var/list/guaranteed_spawners = list()
/datum/spawn_pool/proc/can_afford(points)
if(available_points >= points)
return TRUE
return FALSE
/datum/spawn_pool/proc/consume(points)
available_points -= points
/datum/spawn_pool/proc/process_guaranteed_spawners()
while(length(guaranteed_spawners))
var/obj/effect/spawner/random/pool/spawner = guaranteed_spawners[length(guaranteed_spawners)]
guaranteed_spawners.len--
spawner.spawn_loot()
qdel(spawner)
QDEL_LIST_CONTENTS(guaranteed_spawners)
/datum/spawn_pool/proc/process_spawners()
process_guaranteed_spawners()
shuffle_inplace(known_spawners)
while(length(known_spawners))
if(available_points <= 0)
break
var/obj/effect/spawner/random/pool/spawner = known_spawners[length(known_spawners)]
known_spawners.len--
if(spawner.point_value != INFINITY && available_points < spawner.point_value)
qdel(spawner)
continue
spawner.spawn_loot()
if(length(guaranteed_spawners))
WARNING("non-guaranteed spawner [spawner.type] spawned a guaranteed spawner, this should be avoided")
process_guaranteed_spawners()
qdel(spawner)
QDEL_LIST_CONTENTS(known_spawners)