feat: let the player live as a citizen with needs and carried inventory

This commit is contained in:
Rijad Zuzo
2026-08-11 00:18:39 +02:00
parent f72aa659d2
commit 37f262fb84
20 changed files with 746 additions and 90 deletions
+62 -27
View File
@@ -1,5 +1,9 @@
extends Node
# gdlint: disable=max-file-lines
# The manager is the deliberate scene-tree facade for the simulation slice and
# intentionally keeps its orchestration readable in one authoritative file.
const SimulationEventLogScript := preload("res://simulation/events/SimulationEventLog.gd")
const VillageEconomyScript := preload("res://simulation/economy/VillageEconomy.gd")
const AnimalCareSystemScript := preload("res://simulation/animals/animal_care_system.gd")
@@ -25,6 +29,7 @@ signal opportunity_resolved(opportunity: OpportunityStateRecord, cause_event: Ec
signal opportunity_invalidated(opportunity: OpportunityStateRecord)
var village := SimVillage.new()
var player_system := PlayerCitizenSystem.new()
var npcs: Array[SimNPC] = []
@export var tick_interval := 1.2
@@ -89,6 +94,7 @@ func _ready() -> void:
village.debug_logs = debug_logs
economy.configure(village, debug_logs)
animal_care.configure(economy, active_world_adapter)
player_system.configure(economy, _record_player_event_at)
economy.initialize_storage()
village.update_modifiers()
village.update_priorities()
@@ -164,6 +170,7 @@ func simulate_tick() -> void:
if debug_logs:
print("--- Tick ", tick_count, " ---")
animal_care.advance(tick_count)
advance_player_needs()
var village_was_changed := false
_population_view.rebuild(npcs)
for npc in npcs:
@@ -984,9 +991,6 @@ func find_resource_node_for_player(from_position: Vector3, max_distance: float)
or not resource_state.can_extract()
):
continue
var storage := economy.get_storage_for_resource(resource_state.get_resource_id())
if storage == null or storage.get_available_capacity() <= 0.0:
continue
var distance := from_position.distance_squared_to(node.interaction_point.global_position)
if distance <= best_distance:
best_distance = distance
@@ -1000,16 +1004,12 @@ func harvest_resource_node(node: ResourceNode) -> float:
var resource_state := get_resource_state(node.node_id)
if resource_state == null or not resource_state.can_player_use_resource():
return 0.0
var storage := economy.get_storage_for_resource(resource_state.get_resource_id())
if storage == null:
return 0.0
var extracted := resource_state.extract(
minf(resource_state.get_yield_per_action(), storage.get_available_capacity())
)
var extracted := resource_state.extract(resource_state.get_yield_per_action())
if extracted <= 0.0:
return 0.0
var resource_id := resource_state.get_resource_id()
player_system.add_carried(resource_id, extracted)
var deposited := economy.deposit_resource(resource_state.get_resource_id(), extracted)
var event_position := (
node.interaction_point.global_position
if node.interaction_point != null
@@ -1017,25 +1017,17 @@ func harvest_resource_node(node: ResourceNode) -> float:
)
_record_economic_event_at(
SimulationIds.EVENT_RESOURCE_EXTRACTED,
-1,
SimulationIds.PLAYER_ACTOR_ID,
resource_state.get_node_id(),
(
SimulationIds.STORAGE_VILLAGE_PANTRY
if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD
else (
SimulationIds.STORAGE_VILLAGE_WOODPILE
if resource_state.get_resource_id() == SimulationIds.RESOURCE_WOOD
else &"village"
)
),
resource_state.get_resource_id(),
deposited,
SimulationIds.PLAYER_INVENTORY_ID,
resource_id,
extracted,
event_position
)
if resource_state.get_amount_remaining() <= 0.0:
_record_narrative_event_at(
SimulationIds.EVENT_RESOURCE_DEPLETED,
-1,
SimulationIds.PLAYER_ACTOR_ID,
resource_state.get_node_id(),
"",
event_position
@@ -1044,20 +1036,60 @@ func harvest_resource_node(node: ResourceNode) -> float:
if debug_logs:
print(
"[SimulationManager] Player extracted ",
"[SimulationManager] Player carried ",
extracted,
" ",
resource_state.get_resource_id(),
resource_id,
" from ",
resource_state.get_node_id()
)
return extracted
func advance_player_needs() -> void:
player_system.advance_needs(village.food_modifier)
func get_player_state() -> PlayerStateRecord:
return player_system.player_state
func deposit_player_inventory(item_id: StringName) -> float:
var storage := economy.get_storage_for_resource(item_id)
var storage_node := (
StorageNode.get_by_id(storage.get_storage_id()) as StorageNode if storage != null else null
)
var deposited := player_system.deposit(
item_id, storage_node.get_interaction_position() if storage_node != null else Vector3.ZERO
)
village_changed.emit(village)
return deposited
func eat_food(amount: float) -> void:
economy.withdraw_resource(SimulationIds.RESOURCE_FOOD, amount)
func eat_player_food() -> bool:
var succeeded := player_system.eat(Vector3.ZERO)
village_changed.emit(village)
return succeeded
func _record_player_event_at(
event_type: StringName,
source_id: StringName,
destination_id: StringName,
item_id: StringName,
amount: float,
world_position: Vector3
) -> void:
_record_economic_event_at(
event_type,
SimulationIds.PLAYER_ACTOR_ID,
source_id,
destination_id,
item_id,
amount,
world_position
)
func add_safety(amount: float) -> void:
@@ -1125,6 +1157,7 @@ func create_state_record() -> SimulationStateRecord:
record.event_knowledge.append(known_event)
for opportunity in opportunity_system.get_all_sorted():
record.opportunities.append(opportunity)
record.player = player_system.player_state
return record
@@ -1175,6 +1208,8 @@ func restore_state(record: SimulationStateRecord) -> bool:
resource_states[resource_record.get_node_id()] = resource_record
register_loaded_resource_nodes()
animal_care.restore_state_records(record.animals, tick_count)
if record.player != null:
player_system.player_state = record.player
village_changed.emit(village)
state_restored.emit()
return true
+1
View File
@@ -2,6 +2,7 @@ class_name SimulationIds
extends RefCounted
const PLAYER_ACTOR_ID := -1
const PLAYER_INVENTORY_ID := &"player_inventory"
const ACTION_IDLE := &"idle"
const ACTION_DEAD := &"dead"
@@ -537,6 +537,8 @@ static func _is_valid_supply(
var event_type := StringName(event.data["event_type"])
var actor_id := int(event.data["actor_id"])
if event_type == SimulationIds.EVENT_STORAGE_DEPOSITED:
if actor_id == SimulationIds.PLAYER_ACTOR_ID:
return StringName(event.data["source_id"]) == SimulationIds.PLAYER_INVENTORY_ID
return (
_find_npc(actor_id, npcs) != null
and StringName(event.data["source_id"]) == SimulationIds.npc_inventory_id(actor_id)
@@ -0,0 +1,80 @@
class_name PlayerCitizenSystem
extends RefCounted
var player_state := PlayerStateRecord.create_default()
var economy: RefCounted
var _record_event: Callable
func configure(economy_state: RefCounted, record_event: Callable) -> void:
economy = economy_state
_record_event = record_event
func advance_needs(food_modifier: float) -> void:
player_state.set_hunger(player_state.get_hunger() + 0.125 * food_modifier)
player_state.set_energy(maxf(player_state.get_energy() - 0.0625, 0.0))
func add_carried(item_id: StringName, amount: float) -> void:
player_state.add_inventory(item_id, amount)
func get_carried(item_id: StringName) -> float:
return player_state.get_inventory_amount(item_id)
func deposit(item_id: StringName, event_position: Vector3) -> float:
if item_id.is_empty():
return 0.0
var storage := _storage_for(item_id)
if storage == null:
return 0.0
var deposited := storage.deposit(item_id, player_state.get_inventory_amount(item_id))
player_state.remove_inventory(item_id, deposited)
economy.sync_resource(item_id)
if deposited > 0.0:
_record_event.call(
SimulationIds.EVENT_STORAGE_DEPOSITED,
SimulationIds.PLAYER_INVENTORY_ID,
storage.get_storage_id(),
item_id,
deposited,
event_position
)
return deposited
func eat(event_position: Vector3) -> bool:
if player_state.get_inventory_amount(SimulationIds.RESOURCE_FOOD) >= 1.0:
player_state.remove_inventory(SimulationIds.RESOURCE_FOOD, 1.0)
_apply_meal(event_position, SimulationIds.PLAYER_INVENTORY_ID)
return true
var pantry := _storage_for(SimulationIds.RESOURCE_FOOD)
if pantry != null and pantry.get_amount(SimulationIds.RESOURCE_FOOD) > 0.0:
var withdrawn := pantry.withdraw(SimulationIds.RESOURCE_FOOD, 1.0)
economy.sync_resource(SimulationIds.RESOURCE_FOOD)
if withdrawn <= 0.0:
return false
_apply_meal(event_position, pantry.get_storage_id())
return true
return false
func _apply_meal(event_position: Vector3, source_id: StringName) -> void:
player_state.set_hunger(maxf(player_state.get_hunger() - 55.0, 0.0))
player_state.set_energy(minf(player_state.get_energy() + 10.0, 100.0))
_record_event.call(
SimulationIds.EVENT_ITEM_CONSUMED,
source_id,
&"consumed",
SimulationIds.RESOURCE_FOOD,
1.0,
event_position
)
func _storage_for(item_id: StringName) -> StorageStateRecord:
if economy == null or not economy.has_method("get_storage_for_resource"):
return null
return economy.get_storage_for_resource(item_id) as StorageStateRecord
@@ -0,0 +1 @@
uid://vqve206mikpt
+35 -7
View File
@@ -2,7 +2,7 @@ class_name SimulationStateRecord
extends RefCounted
const SCHEMA_NAME := "the_steward.simulation"
const SCHEMA_VERSION := 12
const SCHEMA_VERSION := 13
const LEGACY_SCHEMA_VERSION := 1
const EVENT_LEGACY_SCHEMA_VERSION := 2
const RELATIONSHIP_LEGACY_SCHEMA_VERSION := 3
@@ -13,6 +13,7 @@ const OPPORTUNITY_LEGACY_SCHEMA_VERSION := 8
const ANIMAL_LEGACY_SCHEMA_VERSION := 9
const ROUTINE_LEGACY_SCHEMA_VERSION := 10
const PLAYER_RELATIONSHIP_SCHEMA_VERSION := 11
const PLAYER_NEEDS_SCHEMA_VERSION := 12
const PREVIOUS_SCHEMA_VERSION := 7
var simulation: Dictionary
@@ -25,6 +26,7 @@ var economic_events: Array[EconomicEventRecord] = []
var relationships: Array[RelationshipStateRecord] = []
var event_knowledge: Array[KnownEventStateRecord] = []
var opportunities: Array[OpportunityStateRecord] = []
var player: PlayerStateRecord
func to_dictionary() -> Dictionary:
@@ -66,7 +68,8 @@ func to_dictionary() -> Dictionary:
"economic_events": event_data,
"relationships": relationship_data,
"event_knowledge": knowledge_data,
"opportunities": opportunity_data
"opportunities": opportunity_data,
"player": player.to_dictionary()
}
@@ -99,6 +102,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
ANIMAL_LEGACY_SCHEMA_VERSION,
ROUTINE_LEGACY_SCHEMA_VERSION,
PLAYER_RELATIONSHIP_SCHEMA_VERSION,
PLAYER_NEEDS_SCHEMA_VERSION,
]
):
record_data = _migrate_legacy(record_data, version)
@@ -118,6 +122,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
"relationships",
"event_knowledge",
"opportunities",
"player",
]
)
):
@@ -200,6 +205,13 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
var record := SimulationStateRecord.new()
record.simulation = simulation_data.duplicate(true)
record.village = village_record
var player_data = record_data["player"]
if not player_data is Dictionary:
return null
var player_record := PlayerStateRecord.from_dictionary(player_data)
if player_record == null:
return null
record.player = player_record
var npc_ids := {}
var npc_records_by_id := {}
@@ -681,12 +693,23 @@ static func _is_valid_relationship_cause(
and StringName(cause_event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
and float(cause_event.data["amount"]) > 0.0
)
if int(cause_event.data["actor_id"]) != SimulationIds.PLAYER_ACTOR_ID:
return false
if StringName(cause_event.data["item_id"]) != SimulationIds.RESOURCE_FOOD:
return false
if float(cause_event.data["amount"]) <= 0.0:
return false
if StringName(cause_event.data["event_type"]) == SimulationIds.EVENT_STORAGE_DEPOSITED:
return (
StringName(cause_event.data["source_id"]) == SimulationIds.PLAYER_INVENTORY_ID
and (
StringName(cause_event.data["destination_id"])
== SimulationIds.STORAGE_VILLAGE_PANTRY
)
)
return (
int(cause_event.data["actor_id"]) == SimulationIds.PLAYER_ACTOR_ID
and StringName(cause_event.data["event_type"]) == SimulationIds.EVENT_RESOURCE_EXTRACTED
and (StringName(cause_event.data["destination_id"]) == SimulationIds.STORAGE_VILLAGE_PANTRY)
and StringName(cause_event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
and float(cause_event.data["amount"]) > 0.0
StringName(cause_event.data["event_type"]) == SimulationIds.EVENT_RESOURCE_EXTRACTED
and StringName(cause_event.data["destination_id"]) == SimulationIds.STORAGE_VILLAGE_PANTRY
)
@@ -705,6 +728,8 @@ static func _is_valid_supply_resolution(
):
return false
if event_type == SimulationIds.EVENT_STORAGE_DEPOSITED:
if actor_id == SimulationIds.PLAYER_ACTOR_ID:
return StringName(event.data["source_id"]) == SimulationIds.PLAYER_INVENTORY_ID
return (
npc_ids.has(actor_id)
and StringName(event.data["source_id"]) == SimulationIds.npc_inventory_id(actor_id)
@@ -724,6 +749,8 @@ static func _is_valid_supply_resolution(
static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary:
var migrated := legacy_data.duplicate(true)
migrated["schema_version"] = SCHEMA_VERSION
if not migrated.has("player"):
migrated["player"] = PlayerStateRecord.create_default().to_dictionary()
if version <= ANIMAL_LEGACY_SCHEMA_VERSION:
migrated["animals"] = []
if version == LEGACY_SCHEMA_VERSION:
@@ -785,6 +812,7 @@ static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary
ANIMAL_LEGACY_SCHEMA_VERSION,
ROUTINE_LEGACY_SCHEMA_VERSION,
PLAYER_RELATIONSHIP_SCHEMA_VERSION,
PLAYER_NEEDS_SCHEMA_VERSION,
]
):
migrated["opportunities"] = []
+107
View File
@@ -0,0 +1,107 @@
class_name PlayerStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create_default() -> PlayerStateRecord:
return (
PlayerStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"hunger": 40.0,
"energy": 100.0,
"inventory": {},
}
)
)
static func from_dictionary(record_data: Dictionary) -> PlayerStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not record_data.has_all(["hunger", "energy", "inventory"]):
return null
if not record_data["inventory"] is Dictionary:
return null
var hunger := float(record_data["hunger"])
var energy := float(record_data["energy"])
if not is_finite(hunger) or not is_finite(energy):
return null
var normalized_inventory := {}
for raw_item_id in record_data["inventory"]:
var item_id := String(raw_item_id)
var amount := float(record_data["inventory"][raw_item_id])
if (
item_id.is_empty()
or normalized_inventory.has(item_id)
or not is_finite(amount)
or amount < 0.0
):
return null
normalized_inventory[item_id] = amount
return (
PlayerStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"hunger": clampf(hunger, 0.0, 100.0),
"energy": clampf(energy, 0.0, 100.0),
"inventory": normalized_inventory,
}
)
)
func get_hunger() -> float:
return clampf(float(data["hunger"]), 0.0, 100.0)
func get_energy() -> float:
return clampf(float(data["energy"]), 0.0, 100.0)
func set_hunger(amount: float) -> void:
data["hunger"] = clampf(amount, 0.0, 100.0)
func set_energy(amount: float) -> void:
data["energy"] = clampf(amount, 0.0, 100.0)
func get_inventory_amount(item_id: StringName) -> float:
return float(data["inventory"].get(String(item_id), 0.0))
func get_carried_total() -> float:
var total := 0.0
for amount in data["inventory"].values():
total += float(amount)
return total
func add_inventory(item_id: StringName, amount: float) -> void:
if item_id.is_empty() or not is_finite(amount) or amount <= 0.0:
return
data["inventory"][String(item_id)] = get_inventory_amount(item_id) + amount
func remove_inventory(item_id: StringName, requested_amount: float) -> float:
if item_id.is_empty() or not is_finite(requested_amount) or requested_amount <= 0.0:
return 0.0
var removed := minf(requested_amount, get_inventory_amount(item_id))
if removed <= 0.0:
return 0.0
data["inventory"][String(item_id)] = get_inventory_amount(item_id) - removed
return removed
func to_dictionary() -> Dictionary:
return data.duplicate(true)
@@ -0,0 +1 @@
uid://dynd78rglkeji