33 lines
1.1 KiB
GDScript
33 lines
1.1 KiB
GDScript
extends Resource
|
|
class_name ItemAffixDatabase
|
|
|
|
@export var affixes: Array[ItemAffixDefinition] = []
|
|
|
|
func roll_affixes(item: ItemDefinition, level: int, requested_count: int) -> Array[ItemStatModifier]:
|
|
var pool: Array[ItemAffixDefinition] = []
|
|
for affix in affixes:
|
|
if affix != null and affix.can_roll(item, level):
|
|
pool.append(affix)
|
|
var modifiers: Array[ItemStatModifier] = []
|
|
while modifiers.size() < requested_count and not pool.is_empty():
|
|
var total_weight := 0.0
|
|
for candidate in pool:
|
|
total_weight += candidate.selection_weight
|
|
var roll := randf() * total_weight
|
|
var selected := pool.back() as ItemAffixDefinition
|
|
for candidate in pool:
|
|
roll -= candidate.selection_weight
|
|
if roll <= 0.0:
|
|
selected = candidate
|
|
break
|
|
var modifier := ItemStatModifier.new()
|
|
modifier.affix = selected
|
|
modifier.stat_key = selected.stat_key
|
|
modifier.value = selected.roll_value(level)
|
|
modifiers.append(modifier)
|
|
# Removing incompatible candidates also prevents duplicate affix IDs.
|
|
for index in range(pool.size() - 1, -1, -1):
|
|
if not selected.can_combine(pool[index]):
|
|
pool.remove_at(index)
|
|
return modifiers
|