feat: regrow finite berry sources toward their authored cap
This commit is contained in:
@@ -171,6 +171,7 @@ func simulate_tick() -> void:
|
||||
print("--- Tick ", tick_count, " ---")
|
||||
animal_care.advance(tick_count)
|
||||
advance_player_needs()
|
||||
_advance_resource_regrowth()
|
||||
var village_was_changed := false
|
||||
_population_view.rebuild(npcs)
|
||||
for npc in npcs:
|
||||
@@ -1065,6 +1066,12 @@ func advance_player_needs() -> void:
|
||||
player_system.advance_needs(village.food_modifier)
|
||||
|
||||
|
||||
func _advance_resource_regrowth() -> void:
|
||||
for resource_id in resource_states:
|
||||
var resource_state := resource_states[resource_id] as ResourceStateRecord
|
||||
resource_state.regrow()
|
||||
|
||||
|
||||
func get_player_state() -> PlayerStateRecord:
|
||||
return player_system.player_state
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@ extends RefCounted
|
||||
signal changed(state: ResourceStateRecord)
|
||||
signal depleted(state: ResourceStateRecord)
|
||||
|
||||
const SCHEMA_VERSION := 3
|
||||
const SCHEMA_VERSION := 4
|
||||
const LEGACY_SCHEMA_VERSION := 1
|
||||
const DISCOVERY_LEGACY_SCHEMA_VERSION := 2
|
||||
const REGROW_LEGACY_SCHEMA_VERSION := 3
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
@@ -24,6 +25,8 @@ static func create_from_node(node: ResourceNode) -> ResourceStateRecord:
|
||||
"resource_id": String(node.resource_id),
|
||||
"amount_remaining": node.initial_amount,
|
||||
"yield_per_action": node.yield_per_action,
|
||||
"max_amount": node.max_amount if node.max_amount > 0.0 else node.initial_amount,
|
||||
"regrow_rate": node.regrow_rate,
|
||||
"reserved_by": -1,
|
||||
"enabled": node.initial_enabled,
|
||||
"can_npcs_use": node.can_npcs_use,
|
||||
@@ -41,6 +44,8 @@ static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord:
|
||||
record_data = _migrate_v1(record_data)
|
||||
elif version == DISCOVERY_LEGACY_SCHEMA_VERSION:
|
||||
record_data = _migrate_v2(record_data)
|
||||
elif version == REGROW_LEGACY_SCHEMA_VERSION:
|
||||
record_data = _migrate_v3(record_data)
|
||||
elif version != SCHEMA_VERSION:
|
||||
return null
|
||||
|
||||
@@ -51,6 +56,8 @@ static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord:
|
||||
"resource_id",
|
||||
"amount_remaining",
|
||||
"yield_per_action",
|
||||
"max_amount",
|
||||
"regrow_rate",
|
||||
"reserved_by",
|
||||
"enabled",
|
||||
"can_npcs_use",
|
||||
@@ -73,6 +80,8 @@ static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord:
|
||||
normalized["resource_id"] = String(record_data["resource_id"])
|
||||
normalized["amount_remaining"] = float(record_data["amount_remaining"])
|
||||
normalized["yield_per_action"] = float(record_data["yield_per_action"])
|
||||
normalized["max_amount"] = float(record_data["max_amount"])
|
||||
normalized["regrow_rate"] = float(record_data["regrow_rate"])
|
||||
normalized["reserved_by"] = int(record_data["reserved_by"])
|
||||
normalized["enabled"] = bool(record_data["enabled"])
|
||||
normalized["can_npcs_use"] = bool(record_data["can_npcs_use"])
|
||||
@@ -80,6 +89,13 @@ static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord:
|
||||
normalized["safety_risk"] = float(record_data["safety_risk"])
|
||||
normalized["comfort_distance"] = float(record_data["comfort_distance"])
|
||||
normalized["discovery_priority"] = float(record_data["discovery_priority"])
|
||||
if (
|
||||
not is_finite(normalized["max_amount"])
|
||||
or normalized["max_amount"] < 0.0
|
||||
or not is_finite(normalized["regrow_rate"])
|
||||
or normalized["regrow_rate"] < 0.0
|
||||
):
|
||||
return null
|
||||
return ResourceStateRecord.new(normalized)
|
||||
|
||||
|
||||
@@ -102,6 +118,15 @@ static func _migrate_v2(legacy_data: Dictionary) -> Dictionary:
|
||||
migrated["safety_risk"] = clampf(float(migrated.get("safety_risk", 0.0)), 0.0, 1.0)
|
||||
migrated["comfort_distance"] = maxf(float(migrated.get("comfort_distance", 18.0)), 0.1)
|
||||
migrated["discovery_priority"] = float(migrated.get("discovery_priority", 0.0))
|
||||
return _migrate_v3(migrated)
|
||||
|
||||
|
||||
static func _migrate_v3(legacy_data: Dictionary) -> Dictionary:
|
||||
var migrated := legacy_data.duplicate(true)
|
||||
migrated["schema_version"] = SCHEMA_VERSION
|
||||
var remaining := float(migrated.get("amount_remaining", 0.0))
|
||||
migrated["max_amount"] = maxf(float(migrated.get("max_amount", remaining)), 0.0)
|
||||
migrated["regrow_rate"] = maxf(float(migrated.get("regrow_rate", 0.0)), 0.0)
|
||||
return migrated
|
||||
|
||||
|
||||
@@ -114,6 +139,8 @@ func apply_definition(node: ResourceNode) -> bool:
|
||||
data["action_id"] = String(node.action_id)
|
||||
data["resource_id"] = String(node.resource_id)
|
||||
data["yield_per_action"] = node.yield_per_action
|
||||
data["max_amount"] = node.max_amount if node.max_amount > 0.0 else node.initial_amount
|
||||
data["regrow_rate"] = node.regrow_rate
|
||||
data["can_npcs_use"] = node.can_npcs_use
|
||||
data["can_player_use"] = node.can_player_use
|
||||
data["safety_risk"] = node.safety_risk
|
||||
@@ -151,6 +178,27 @@ func get_yield_per_action() -> float:
|
||||
return float(data["yield_per_action"])
|
||||
|
||||
|
||||
func get_max_amount() -> float:
|
||||
return maxf(float(data["max_amount"]), 0.0)
|
||||
|
||||
|
||||
func get_regrow_rate() -> float:
|
||||
return maxf(float(data["regrow_rate"]), 0.0)
|
||||
|
||||
|
||||
func regrow() -> float:
|
||||
var rate := get_regrow_rate()
|
||||
if rate <= 0.0 or is_equal_approx(get_amount_remaining(), get_max_amount()):
|
||||
return 0.0
|
||||
var previous := get_amount_remaining()
|
||||
var next := minf(previous + rate, get_max_amount())
|
||||
data["amount_remaining"] = next
|
||||
if is_depleted():
|
||||
data["reserved_by"] = -1
|
||||
changed.emit(self)
|
||||
return next - previous
|
||||
|
||||
|
||||
func get_reserved_by() -> int:
|
||||
return int(data["reserved_by"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user