feat: add structured economic events
This commit is contained in:
@@ -9,6 +9,8 @@ signal village_changed(village: SimVillage)
|
||||
signal npc_died(npc: SimNPC)
|
||||
signal npc_target_requested(npc: SimNPC)
|
||||
signal npc_travel_requested(npc: SimNPC, target_position: Vector3)
|
||||
signal npc_inventory_changed(npc: SimNPC, item_id: StringName, amount: float)
|
||||
signal economic_event_recorded(event: EconomicEventRecord)
|
||||
|
||||
var village := SimVillage.new()
|
||||
|
||||
@@ -23,6 +25,8 @@ var tick_count := 0
|
||||
var wander_random_sources := {}
|
||||
var resource_states: Dictionary = {}
|
||||
var storage_states: Dictionary = {}
|
||||
var economic_events: Array[EconomicEventRecord] = []
|
||||
var next_event_id := 0
|
||||
var action_selector := ActionSelectionSystem.new()
|
||||
var action_executor := ActionExecutionSystem.new()
|
||||
var target_resolver := ActionTargetResolver.new()
|
||||
@@ -162,11 +166,28 @@ func simulate_tick() -> void:
|
||||
SimulationIds.RESOURCE_FOOD,
|
||||
extracted
|
||||
)
|
||||
npc_inventory_changed.emit(
|
||||
npc,
|
||||
SimulationIds.RESOURCE_FOOD,
|
||||
npc.get_inventory_amount(
|
||||
SimulationIds.RESOURCE_FOOD
|
||||
)
|
||||
)
|
||||
else:
|
||||
village.apply_resource_delta(
|
||||
resource_state.get_resource_id(),
|
||||
extracted
|
||||
)
|
||||
_record_economic_event(
|
||||
SimulationIds.EVENT_RESOURCE_EXTRACTED,
|
||||
npc.id,
|
||||
resource_state.get_node_id(),
|
||||
_npc_inventory_id(npc.id)
|
||||
if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD
|
||||
else &"village",
|
||||
resource_state.get_resource_id(),
|
||||
extracted
|
||||
)
|
||||
if debug_logs:
|
||||
print(
|
||||
"[SimulationManager] ",
|
||||
@@ -355,6 +376,33 @@ func synchronize_npc_position(npc_id: int, active_position: Vector3) -> bool:
|
||||
return true
|
||||
return false
|
||||
|
||||
func _npc_inventory_id(npc_id: int) -> StringName:
|
||||
return StringName("npc_%d_inventory" % npc_id)
|
||||
|
||||
func _record_economic_event(
|
||||
event_type: StringName,
|
||||
actor_id: int,
|
||||
source_id: StringName,
|
||||
destination_id: StringName,
|
||||
item_id: StringName,
|
||||
amount: float
|
||||
) -> void:
|
||||
if amount <= 0.0:
|
||||
return
|
||||
var event := EconomicEventRecord.create(
|
||||
next_event_id,
|
||||
event_type,
|
||||
tick_count,
|
||||
actor_id,
|
||||
source_id,
|
||||
destination_id,
|
||||
item_id,
|
||||
amount
|
||||
)
|
||||
next_event_id += 1
|
||||
economic_events.append(event)
|
||||
economic_event_recorded.emit(event)
|
||||
|
||||
func _initialize_storage() -> void:
|
||||
if storage_states.has(SimulationIds.STORAGE_VILLAGE_PANTRY):
|
||||
_sync_village_food()
|
||||
@@ -384,6 +432,16 @@ func deposit_npc_inventory(npc: SimNPC, item_id: StringName) -> float:
|
||||
var deposited := get_pantry().deposit(item_id, available)
|
||||
npc.remove_inventory(item_id, deposited)
|
||||
_sync_village_food()
|
||||
if deposited > 0.0:
|
||||
npc_inventory_changed.emit(npc, item_id, npc.get_inventory_amount(item_id))
|
||||
_record_economic_event(
|
||||
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
||||
npc.id,
|
||||
_npc_inventory_id(npc.id),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
item_id,
|
||||
deposited
|
||||
)
|
||||
return deposited
|
||||
|
||||
func withdraw_to_npc(
|
||||
@@ -394,6 +452,16 @@ func withdraw_to_npc(
|
||||
var withdrawn := get_pantry().withdraw(item_id, amount)
|
||||
npc.add_inventory(item_id, withdrawn)
|
||||
_sync_village_food()
|
||||
if withdrawn > 0.0:
|
||||
npc_inventory_changed.emit(npc, item_id, npc.get_inventory_amount(item_id))
|
||||
_record_economic_event(
|
||||
SimulationIds.EVENT_STORAGE_WITHDRAWN,
|
||||
npc.id,
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
_npc_inventory_id(npc.id),
|
||||
item_id,
|
||||
withdrawn
|
||||
)
|
||||
return withdrawn
|
||||
|
||||
func consume_npc_food(npc: SimNPC) -> bool:
|
||||
@@ -404,6 +472,19 @@ func consume_npc_food(npc: SimNPC) -> bool:
|
||||
npc.hunger = maxf(npc.hunger - 55.0, 0.0)
|
||||
npc.starvation_ticks = 0
|
||||
npc.is_starving = npc.hunger >= 90.0
|
||||
npc_inventory_changed.emit(
|
||||
npc,
|
||||
SimulationIds.RESOURCE_FOOD,
|
||||
npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD)
|
||||
)
|
||||
_record_economic_event(
|
||||
SimulationIds.EVENT_ITEM_CONSUMED,
|
||||
npc.id,
|
||||
_npc_inventory_id(npc.id),
|
||||
&"consumed",
|
||||
SimulationIds.RESOURCE_FOOD,
|
||||
1.0
|
||||
)
|
||||
return true
|
||||
|
||||
func register_loaded_resource_nodes() -> void:
|
||||
@@ -484,6 +565,16 @@ func harvest_resource_node(node: ResourceNode) -> float:
|
||||
_sync_village_food()
|
||||
else:
|
||||
village.apply_resource_delta(resource_state.get_resource_id(), extracted)
|
||||
_record_economic_event(
|
||||
SimulationIds.EVENT_RESOURCE_EXTRACTED,
|
||||
-1,
|
||||
resource_state.get_node_id(),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY
|
||||
if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD
|
||||
else &"village",
|
||||
resource_state.get_resource_id(),
|
||||
extracted
|
||||
)
|
||||
village_changed.emit(village)
|
||||
|
||||
if debug_logs:
|
||||
@@ -591,7 +682,8 @@ func create_state_record() -> SimulationStateRecord:
|
||||
"tick_count": tick_count,
|
||||
"clock_accumulator": clock.accumulator,
|
||||
"clock_elapsed_ticks": clock.elapsed_ticks,
|
||||
"wander_random_streams": wander_streams
|
||||
"wander_random_streams": wander_streams,
|
||||
"next_event_id": next_event_id
|
||||
}
|
||||
record.village = VillageStateRecord.capture(village)
|
||||
for npc in npcs:
|
||||
@@ -607,6 +699,8 @@ func create_state_record() -> SimulationStateRecord:
|
||||
for storage_id in sorted_storage_ids:
|
||||
var storage_state: StorageStateRecord = storage_states[storage_id]
|
||||
record.storages.append(storage_state)
|
||||
for event in economic_events:
|
||||
record.economic_events.append(event)
|
||||
return record
|
||||
|
||||
func serialize_state() -> String:
|
||||
@@ -626,6 +720,7 @@ func restore_state(record: SimulationStateRecord) -> bool:
|
||||
simulation_seed = int(record.simulation["seed"])
|
||||
tick_interval = float(record.simulation["tick_interval"])
|
||||
tick_count = int(record.simulation["tick_count"])
|
||||
next_event_id = int(record.simulation["next_event_id"])
|
||||
clock = SimulationClock.new(tick_interval)
|
||||
clock.accumulator = float(record.simulation["clock_accumulator"])
|
||||
clock.elapsed_ticks = int(record.simulation["clock_elapsed_ticks"])
|
||||
@@ -634,6 +729,7 @@ func restore_state(record: SimulationStateRecord) -> bool:
|
||||
for storage_record in record.storages:
|
||||
storage_states[storage_record.get_storage_id()] = storage_record
|
||||
_sync_village_food()
|
||||
economic_events = record.economic_events.duplicate()
|
||||
|
||||
npcs.clear()
|
||||
for npc_record in record.npcs:
|
||||
|
||||
Reference in New Issue
Block a user