feat: add missing wood opportunities

This commit is contained in:
Rijad Zuzo
2026-07-16 01:50:19 +02:00
parent f82d9683de
commit 79dd503343
26 changed files with 1054 additions and 175 deletions
+38 -11
View File
@@ -4,9 +4,6 @@ const SimulationEventLogScript := preload("res://simulation/events/SimulationEve
const VillageEconomyScript := preload("res://simulation/economy/VillageEconomy.gd")
const RelationshipSystemScript := preload("res://simulation/relationships/RelationshipSystem.gd")
const EventKnowledgeSystemScript := preload("res://simulation/knowledge/EventKnowledgeSystem.gd")
const FoodShortageOpportunitySystemScript := preload(
"res://simulation/opportunities/FoodShortageOpportunitySystem.gd"
)
signal npc_task_changed(npc: SimNPC, old_task: StringName, new_task: StringName)
signal village_changed(village: SimVillage)
@@ -23,6 +20,7 @@ signal event_knowledge_transferred(speaker_id: int, listener_id: int, event: Eco
signal event_knowledge_forgotten(knower_id: int, event: EconomicEventRecord)
signal opportunity_opened(opportunity: OpportunityStateRecord)
signal opportunity_resolved(opportunity: OpportunityStateRecord, cause_event: EconomicEventRecord)
signal opportunity_invalidated(opportunity: OpportunityStateRecord)
var village := SimVillage.new()
@@ -42,7 +40,7 @@ var event_log := SimulationEventLogScript.new()
var economy := VillageEconomyScript.new()
var relationship_system := RelationshipSystemScript.new()
var event_knowledge_system := EventKnowledgeSystemScript.new()
var opportunity_system := FoodShortageOpportunitySystemScript.new()
var opportunity_system := VillageOpportunitySystem.new()
var storage_states: Dictionary:
get:
return economy.storage_states
@@ -162,12 +160,15 @@ func simulate_tick() -> void:
var village_was_changed := false
for npc in npcs:
village_was_changed = _simulate_npc_tick(npc) or village_was_changed
if village_was_changed:
village_changed.emit(village)
var invalidated: OpportunityStateRecord = opportunity_system.maintain_open_opportunity(
tick_count, get_knowledge_review_interval(), npcs, get_pantry(), get_woodpile()
)
if invalidated != null:
opportunity_invalidated.emit(invalidated)
if tick_count % get_knowledge_review_interval() == 0:
_maintain_event_knowledge(true)
if debug_logs:
print(village.get_summary())
@@ -552,14 +553,23 @@ func _record_economic_event_at(
func record_narrative_event(
event_type: StringName, actor_id: int, source_id: StringName = &"", action_display: String = ""
event_type: StringName,
actor_id: int,
source_id: StringName = &"",
action_display: String = "",
action_id: StringName = &"",
item_id: StringName = &"",
required_amount: float = 0.0
) -> void:
_record_narrative_event_at(
event_type,
actor_id,
source_id,
action_display,
_get_event_world_position(actor_id, source_id, &"")
_get_event_world_position(actor_id, source_id, &""),
action_id,
item_id,
required_amount
)
@@ -568,10 +578,21 @@ func _record_narrative_event_at(
actor_id: int,
source_id: StringName,
action_display: String,
world_position: Vector3
world_position: Vector3,
action_id: StringName = &"",
item_id: StringName = &"",
required_amount: float = 0.0
) -> void:
event_log.record_narrative(
tick_count, event_type, actor_id, source_id, action_display, world_position
tick_count,
event_type,
actor_id,
source_id,
action_display,
world_position,
action_id,
item_id,
required_amount
)
@@ -673,7 +694,13 @@ func _apply_new_event_knowledge(
func _update_opportunity_from_event(event: EconomicEventRecord) -> void:
var changed: OpportunityStateRecord = opportunity_system.consider_event(
event, tick_count, get_pantry(), npcs, event_knowledge_system, resource_states
event,
tick_count,
get_pantry(),
npcs,
event_knowledge_system,
resource_states,
get_woodpile()
)
if changed == null:
return
+4
View File
@@ -42,8 +42,12 @@ const EVENT_TASK_BLOCKED := &"task_blocked"
const EVENT_RESOURCE_DEPLETED := &"resource_depleted"
const OPPORTUNITY_RESTOCK_EMPTY_PANTRY := &"restock_empty_pantry"
const OPPORTUNITY_SUPPLY_MISSING_WOOD := &"supply_missing_wood"
const OPPORTUNITY_STATUS_OPEN := &"open"
const OPPORTUNITY_STATUS_RESOLVED := &"resolved"
const OPPORTUNITY_STATUS_INVALIDATED := &"invalidated"
const OPPORTUNITY_INVALIDATED_INTERESTED_DIED := &"interested_died"
const OPPORTUNITY_INVALIDATED_EVIDENCE_STALE := &"evidence_stale"
const KNOWLEDGE_ACQUISITION_PERFORMED := &"performed"
const KNOWLEDGE_ACQUISITION_WITNESSED := &"witnessed"
+11 -2
View File
@@ -10,7 +10,13 @@ signal economic_event_requested(
amount: float
)
signal narrative_event_requested(
event_type: StringName, actor_id: int, source_id: StringName, action_display: String
event_type: StringName,
actor_id: int,
source_id: StringName,
action_display: String,
action_id: StringName,
item_id: StringName,
required_amount: float
)
var village: SimVillage
@@ -178,7 +184,10 @@ func consume_completion_cost(npc: SimNPC, definition: ActionDefinition) -> bool:
SimulationIds.EVENT_TASK_BLOCKED,
npc.id,
storage.get_storage_id() if storage != null else &"",
reason
reason,
definition.action_id,
resource_id,
required_amount
)
return false
var consumed := storage.withdraw(resource_id, required_amount)
+14 -2
View File
@@ -42,10 +42,22 @@ func record_narrative(
actor_id: int,
source_id: StringName = &"",
action_display: String = "",
world_position: Vector3 = Vector3.ZERO
world_position: Vector3 = Vector3.ZERO,
action_id: StringName = &"",
item_id: StringName = &"",
required_amount: float = 0.0
) -> EconomicEventRecord:
var event := EconomicEventRecord.create_narrative(
next_event_id, event_type, tick, actor_id, source_id, action_display, world_position
next_event_id,
event_type,
tick,
actor_id,
source_id,
action_display,
world_position,
action_id,
item_id,
required_amount
)
_append(event)
return event
+14 -1
View File
@@ -222,10 +222,23 @@ func _remember(
static func is_knowable_event(event: EconomicEventRecord) -> bool:
if event == null or float(event.data["amount"]) <= 0.0:
if event == null:
return false
var event_type := StringName(event.data["event_type"])
var item_id := StringName(event.data["item_id"])
if event_type == SimulationIds.EVENT_TASK_BLOCKED:
return (
int(event.data["actor_id"]) >= 0
and StringName(event.data["source_id"]) == SimulationIds.STORAGE_VILLAGE_WOODPILE
and item_id == SimulationIds.RESOURCE_WOOD
and (
StringName(event.data.get("action_id", &""))
in [SimulationIds.ACTION_PATROL, SimulationIds.ACTION_STUDY]
)
and float(event.data.get("required_amount", 0.0)) > 0.0
)
if float(event.data["amount"]) <= 0.0:
return false
if event_type == SimulationIds.EVENT_STORAGE_DEPOSITED:
return item_id == SimulationIds.RESOURCE_FOOD
if event_type != SimulationIds.EVENT_STORAGE_WITHDRAWN:
@@ -1,4 +1,4 @@
class_name FoodShortageOpportunitySystem
class_name VillageOpportunitySystem
extends RefCounted
const CARE_HUNGER_THRESHOLD := 80.0
@@ -13,17 +13,64 @@ func consider_event(
pantry: StorageStateRecord,
npcs: Array[SimNPC],
knowledge_system: EventKnowledgeSystem,
resource_states: Dictionary
resource_states: Dictionary,
woodpile: StorageStateRecord = null
) -> OpportunityStateRecord:
if event == null or pantry == null or current_tick < 0:
return null
var event_tick := int(event.data["tick"])
if event_tick < 0 or event_tick > current_tick:
return null
var resolved := _try_resolve(event, pantry, npcs, resource_states)
var resolved := _try_resolve(event, pantry, woodpile, npcs, resource_states)
if resolved != null:
return resolved
return _try_open(event, current_tick, pantry, npcs, knowledge_system)
var opened := _try_open_food(event, current_tick, pantry, npcs, knowledge_system)
if opened != null:
return opened
if woodpile == null:
return null
return _try_open_wood(event, current_tick, woodpile, npcs, knowledge_system)
func maintain_open_opportunity(
current_tick: int,
max_age: int,
npcs: Array[SimNPC],
pantry: StorageStateRecord,
woodpile: StorageStateRecord
) -> OpportunityStateRecord:
var opportunity := get_open_opportunity()
if opportunity == null or current_tick < opportunity.get_created_tick():
return null
if opportunity.get_opportunity_type() != SimulationIds.OPPORTUNITY_SUPPLY_MISSING_WOOD:
return null
var interested := _find_npc(opportunity.get_interested_npc_id(), npcs)
if interested == null or interested.is_dead:
return (
opportunity
if opportunity.invalidate(
SimulationIds.OPPORTUNITY_INVALIDATED_INTERESTED_DIED, current_tick
)
else null
)
var target_storage := _get_target_storage(opportunity, pantry, woodpile)
var condition_is_stale := (
target_storage == null
or (
target_storage.get_amount(opportunity.get_resource_id())
>= opportunity.get_target_amount()
)
or current_tick - opportunity.get_created_tick() >= maxi(max_age, 1)
)
if not condition_is_stale:
return null
return (
opportunity
if opportunity.invalidate(
SimulationIds.OPPORTUNITY_INVALIDATED_EVIDENCE_STALE, current_tick
)
else null
)
func restore(records: Array[OpportunityStateRecord], restored_next_id: int) -> void:
@@ -65,7 +112,7 @@ func get_latest_for_npc(npc_id: int) -> OpportunityStateRecord:
return null
func _try_open(
func _try_open_food(
event: EconomicEventRecord,
current_tick: int,
pantry: StorageStateRecord,
@@ -91,18 +138,62 @@ func _try_open(
return opportunity
func _try_open_wood(
event: EconomicEventRecord,
current_tick: int,
woodpile: StorageStateRecord,
npcs: Array[SimNPC],
knowledge_system: EventKnowledgeSystem
) -> OpportunityStateRecord:
if knowledge_system == null or get_open_opportunity() != null:
return null
if not _is_missing_wood_block(event):
return null
var target_amount := float(event.data.get("required_amount", 0.0))
if woodpile.get_amount(SimulationIds.RESOURCE_WOOD) >= target_amount:
return null
var actor := _find_npc(int(event.data["actor_id"]), npcs)
if (
actor == null
or actor.is_dead
or not knowledge_system.knows_event(actor.id, int(event.data["event_id"]))
):
return null
var opportunity := OpportunityStateRecord.create(
next_opportunity_id,
current_tick,
int(event.data["event_id"]),
actor.id,
target_amount,
SimulationIds.OPPORTUNITY_SUPPLY_MISSING_WOOD,
SimulationIds.STORAGE_VILLAGE_WOODPILE,
SimulationIds.RESOURCE_WOOD
)
next_opportunity_id += 1
opportunities.append(opportunity)
return opportunity
func _try_resolve(
event: EconomicEventRecord,
pantry: StorageStateRecord,
woodpile: StorageStateRecord,
npcs: Array[SimNPC],
resource_states: Dictionary
) -> OpportunityStateRecord:
var opportunity := get_open_opportunity()
if opportunity == null:
return null
if pantry.get_amount(SimulationIds.RESOURCE_FOOD) < opportunity.get_target_amount():
var target_storage := _get_target_storage(opportunity, pantry, woodpile)
if (
target_storage == null
or (
target_storage.get_amount(opportunity.get_resource_id())
< opportunity.get_target_amount()
)
):
return null
if not _is_valid_supply(event, npcs, resource_states):
if not _is_valid_supply(event, opportunity, npcs, resource_states):
return null
if int(event.data["tick"]) < opportunity.get_created_tick():
return null
@@ -139,12 +230,29 @@ static func _is_food_withdrawal(event: EconomicEventRecord) -> bool:
)
static func _is_missing_wood_block(event: EconomicEventRecord) -> bool:
return (
StringName(event.data["event_type"]) == SimulationIds.EVENT_TASK_BLOCKED
and int(event.data["actor_id"]) >= 0
and StringName(event.data["source_id"]) == SimulationIds.STORAGE_VILLAGE_WOODPILE
and StringName(event.data["item_id"]) == SimulationIds.RESOURCE_WOOD
and (
StringName(event.data.get("action_id", &""))
in [SimulationIds.ACTION_PATROL, SimulationIds.ACTION_STUDY]
)
and float(event.data.get("required_amount", 0.0)) > 0.0
)
static func _is_valid_supply(
event: EconomicEventRecord, npcs: Array[SimNPC], resource_states: Dictionary
event: EconomicEventRecord,
opportunity: OpportunityStateRecord,
npcs: Array[SimNPC],
resource_states: Dictionary
) -> bool:
if (
StringName(event.data["item_id"]) != SimulationIds.RESOURCE_FOOD
or StringName(event.data["destination_id"]) != SimulationIds.STORAGE_VILLAGE_PANTRY
StringName(event.data["item_id"]) != opportunity.get_resource_id()
or StringName(event.data["destination_id"]) != opportunity.get_target_id()
or float(event.data["amount"]) <= 0.0
):
return false
@@ -162,11 +270,22 @@ static func _is_valid_supply(
)
return (
resource_state != null
and resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD
and resource_state.get_resource_id() == opportunity.get_resource_id()
and resource_state.can_player_use_resource()
)
static func _get_target_storage(
opportunity: OpportunityStateRecord, pantry: StorageStateRecord, woodpile: StorageStateRecord
) -> StorageStateRecord:
match opportunity.get_target_id():
SimulationIds.STORAGE_VILLAGE_PANTRY:
return pantry
SimulationIds.STORAGE_VILLAGE_WOODPILE:
return woodpile
return null
static func _sort_by_care(first: SimNPC, second: SimNPC) -> bool:
if first.hunger != second.hunger:
return first.hunger > second.hunger
+22 -4
View File
@@ -1,7 +1,8 @@
class_name EconomicEventRecord
extends RefCounted
const SCHEMA_VERSION := 2
const SCHEMA_VERSION := 3
const POSITION_LEGACY_SCHEMA_VERSION := 2
const LEGACY_SCHEMA_VERSION := 1
var data: Dictionary
@@ -33,6 +34,8 @@ static func create(
"destination_id": String(destination_id),
"item_id": String(item_id),
"amount": amount,
"action_id": "",
"required_amount": 0.0,
"world_position": [world_position.x, world_position.y, world_position.z]
}
)
@@ -45,7 +48,10 @@ static func create_narrative(
actor_id: int,
source_id: StringName,
action_display: String = "",
world_position: Vector3 = Vector3.ZERO
world_position: Vector3 = Vector3.ZERO,
action_id: StringName = &"",
item_id: StringName = &"",
required_amount: float = 0.0
) -> EconomicEventRecord:
return EconomicEventRecord.new(
{
@@ -56,9 +62,11 @@ static func create_narrative(
"actor_id": actor_id,
"source_id": String(source_id),
"destination_id": "",
"item_id": "",
"item_id": String(item_id),
"amount": 0.0,
"action_name": action_display,
"action_id": String(action_id),
"required_amount": required_amount,
"world_position": [world_position.x, world_position.y, world_position.z]
}
)
@@ -66,7 +74,7 @@ static func create_narrative(
static func from_dictionary(record_data: Dictionary) -> EconomicEventRecord:
var version := int(record_data.get("schema_version", -1))
if version not in [LEGACY_SCHEMA_VERSION, SCHEMA_VERSION]:
if version not in [LEGACY_SCHEMA_VERSION, POSITION_LEGACY_SCHEMA_VERSION, SCHEMA_VERSION]:
return null
if not record_data.has_all(
[
@@ -93,6 +101,8 @@ static func from_dictionary(record_data: Dictionary) -> EconomicEventRecord:
normalized["destination_id"] = String(record_data["destination_id"])
normalized["item_id"] = String(record_data["item_id"])
normalized["amount"] = float(record_data["amount"])
normalized["action_id"] = String(record_data.get("action_id", ""))
normalized["required_amount"] = float(record_data.get("required_amount", 0.0))
var saved_position = record_data.get("world_position", [0.0, 0.0, 0.0])
if not saved_position is Array or saved_position.size() != 3:
return null
@@ -101,6 +111,14 @@ static func from_dictionary(record_data: Dictionary) -> EconomicEventRecord:
]
if normalized["event_id"] < 0 or normalized["tick"] < 0 or normalized["event_type"].is_empty():
return null
if StringName(normalized["event_type"]) == SimulationIds.EVENT_TASK_BLOCKED:
var has_stable_contract: bool = (
not normalized["action_id"].is_empty()
and not normalized["item_id"].is_empty()
and normalized["required_amount"] > 0.0
)
if version == SCHEMA_VERSION and not has_stable_contract:
return null
return EconomicEventRecord.new(normalized)
+114 -27
View File
@@ -1,11 +1,13 @@
class_name OpportunityStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
const SCHEMA_VERSION := 2
const LEGACY_SCHEMA_VERSION := 1
const NO_EVENT_ID := -1
const DEFAULT_TARGET_AMOUNT := 1.0
const STATUS_OPEN := SimulationIds.OPPORTUNITY_STATUS_OPEN
const STATUS_RESOLVED := SimulationIds.OPPORTUNITY_STATUS_RESOLVED
const STATUS_INVALIDATED := SimulationIds.OPPORTUNITY_STATUS_INVALIDATED
var data: Dictionary
@@ -19,7 +21,10 @@ static func create(
created_tick: int,
trigger_event_id: int,
interested_npc_id: int,
target_amount: float = DEFAULT_TARGET_AMOUNT
target_amount: float = DEFAULT_TARGET_AMOUNT,
opportunity_type: StringName = SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY,
target_id: StringName = SimulationIds.STORAGE_VILLAGE_PANTRY,
resource_id: StringName = SimulationIds.RESOURCE_FOOD
) -> OpportunityStateRecord:
return (
OpportunityStateRecord
@@ -27,26 +32,34 @@ static func create(
{
"schema_version": SCHEMA_VERSION,
"opportunity_id": opportunity_id,
"opportunity_type": String(SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY),
"opportunity_type": String(opportunity_type),
"status": String(STATUS_OPEN),
"created_tick": created_tick,
"trigger_event_id": trigger_event_id,
"interested_npc_id": interested_npc_id,
"target_id": String(SimulationIds.STORAGE_VILLAGE_PANTRY),
"resource_id": String(SimulationIds.RESOURCE_FOOD),
"target_id": String(target_id),
"resource_id": String(resource_id),
"target_amount": target_amount,
"resolution_event_id": NO_EVENT_ID,
"resolved_tick": -1,
"closed_tick": -1,
"invalidation_reason": "",
}
)
)
static func from_dictionary(record_data: Dictionary) -> OpportunityStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
var version := int(record_data.get("schema_version", -1))
if version not in [LEGACY_SCHEMA_VERSION, SCHEMA_VERSION]:
return null
var normalized := record_data.duplicate(true)
if version == LEGACY_SCHEMA_VERSION:
normalized["schema_version"] = SCHEMA_VERSION
normalized["closed_tick"] = int(record_data.get("resolved_tick", -1))
normalized["invalidation_reason"] = ""
if not (
record_data
normalized
. has_all(
[
"opportunity_id",
@@ -60,45 +73,91 @@ static func from_dictionary(record_data: Dictionary) -> OpportunityStateRecord:
"target_amount",
"resolution_event_id",
"resolved_tick",
"closed_tick",
"invalidation_reason",
]
)
):
return null
var opportunity_id := int(record_data["opportunity_id"])
var opportunity_type := StringName(record_data["opportunity_type"])
var status := StringName(record_data["status"])
var created_tick := int(record_data["created_tick"])
var trigger_event_id := int(record_data["trigger_event_id"])
var interested_npc_id := int(record_data["interested_npc_id"])
var target_id := StringName(record_data["target_id"])
var resource_id := StringName(record_data["resource_id"])
var target_amount := float(record_data["target_amount"])
var resolution_event_id := int(record_data["resolution_event_id"])
var resolved_tick := int(record_data["resolved_tick"])
var opportunity_id := int(normalized["opportunity_id"])
var opportunity_type := StringName(normalized["opportunity_type"])
var status := StringName(normalized["status"])
var created_tick := int(normalized["created_tick"])
var trigger_event_id := int(normalized["trigger_event_id"])
var interested_npc_id := int(normalized["interested_npc_id"])
var target_id := StringName(normalized["target_id"])
var resource_id := StringName(normalized["resource_id"])
var target_amount := float(normalized["target_amount"])
var resolution_event_id := int(normalized["resolution_event_id"])
var resolved_tick := int(normalized["resolved_tick"])
var closed_tick := int(normalized["closed_tick"])
var invalidation_reason := StringName(normalized["invalidation_reason"])
if opportunity_id < 0 or created_tick < 0 or trigger_event_id < 0 or interested_npc_id < 0:
return null
if opportunity_type != SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY:
return null
if target_id != SimulationIds.STORAGE_VILLAGE_PANTRY:
return null
if resource_id != SimulationIds.RESOURCE_FOOD:
return null
match opportunity_type:
SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY:
if (
target_id != SimulationIds.STORAGE_VILLAGE_PANTRY
or resource_id != SimulationIds.RESOURCE_FOOD
):
return null
SimulationIds.OPPORTUNITY_SUPPLY_MISSING_WOOD:
if (
target_id != SimulationIds.STORAGE_VILLAGE_WOODPILE
or resource_id != SimulationIds.RESOURCE_WOOD
):
return null
_:
return null
if not is_finite(target_amount) or not is_equal_approx(target_amount, DEFAULT_TARGET_AMOUNT):
return null
match status:
STATUS_OPEN:
if resolution_event_id != NO_EVENT_ID or resolved_tick != -1:
if (
resolution_event_id != NO_EVENT_ID
or resolved_tick != -1
or closed_tick != -1
or not invalidation_reason.is_empty()
):
return null
STATUS_RESOLVED:
if resolution_event_id <= trigger_event_id or resolved_tick < created_tick:
if (
resolution_event_id <= trigger_event_id
or resolved_tick < created_tick
or closed_tick != resolved_tick
or not invalidation_reason.is_empty()
):
return null
STATUS_INVALIDATED:
if (
resolution_event_id != NO_EVENT_ID
or resolved_tick != -1
or closed_tick < created_tick
or (
invalidation_reason
not in [
SimulationIds.OPPORTUNITY_INVALIDATED_INTERESTED_DIED,
SimulationIds.OPPORTUNITY_INVALIDATED_EVIDENCE_STALE,
]
)
):
return null
_:
return null
var record := create(
opportunity_id, created_tick, trigger_event_id, interested_npc_id, target_amount
opportunity_id,
created_tick,
trigger_event_id,
interested_npc_id,
target_amount,
opportunity_type,
target_id,
resource_id
)
if status == STATUS_RESOLVED:
record.resolve(resolution_event_id, resolved_tick)
elif status == STATUS_INVALIDATED:
record.invalidate(invalidation_reason, closed_tick)
return record
@@ -112,6 +171,26 @@ func resolve(resolution_event_id: int, resolved_tick: int) -> bool:
data["status"] = String(STATUS_RESOLVED)
data["resolution_event_id"] = resolution_event_id
data["resolved_tick"] = resolved_tick
data["closed_tick"] = resolved_tick
return true
func invalidate(reason: StringName, closed_tick: int) -> bool:
if (
not is_open()
or closed_tick < get_created_tick()
or (
reason
not in [
SimulationIds.OPPORTUNITY_INVALIDATED_INTERESTED_DIED,
SimulationIds.OPPORTUNITY_INVALIDATED_EVIDENCE_STALE,
]
)
):
return false
data["status"] = String(STATUS_INVALIDATED)
data["closed_tick"] = closed_tick
data["invalidation_reason"] = String(reason)
return true
@@ -159,6 +238,14 @@ func get_resolved_tick() -> int:
return int(data["resolved_tick"])
func get_closed_tick() -> int:
return int(data["closed_tick"])
func get_invalidation_reason() -> StringName:
return StringName(data["invalidation_reason"])
func is_open() -> bool:
return get_status() == STATUS_OPEN
+79 -16
View File
@@ -2,13 +2,14 @@ class_name SimulationStateRecord
extends RefCounted
const SCHEMA_NAME := "the_steward.simulation"
const SCHEMA_VERSION := 8
const SCHEMA_VERSION := 9
const LEGACY_SCHEMA_VERSION := 1
const EVENT_LEGACY_SCHEMA_VERSION := 2
const RELATIONSHIP_LEGACY_SCHEMA_VERSION := 3
const KNOWLEDGE_LEGACY_SCHEMA_VERSION := 4
const PROVENANCE_LEGACY_SCHEMA_VERSION := 5
const RETENTION_LEGACY_SCHEMA_VERSION := 6
const OPPORTUNITY_LEGACY_SCHEMA_VERSION := 8
const PREVIOUS_SCHEMA_VERSION := 7
var simulation: Dictionary
@@ -86,6 +87,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
PROVENANCE_LEGACY_SCHEMA_VERSION,
RETENTION_LEGACY_SCHEMA_VERSION,
PREVIOUS_SCHEMA_VERSION,
OPPORTUNITY_LEGACY_SCHEMA_VERSION,
]
):
record_data = _migrate_legacy(record_data, version)
@@ -185,6 +187,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
record.simulation = simulation_data.duplicate(true)
record.village = village_record
var npc_ids := {}
var npc_records_by_id := {}
for item in npc_data:
if not item is Dictionary:
@@ -196,6 +199,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
if npc_ids.has(npc_id):
return null
npc_ids[npc_id] = true
npc_records_by_id[npc_id] = npc_record
record.npcs.append(npc_record)
var resource_ids := {}
@@ -337,6 +341,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
if not _is_valid_opportunity(
opportunity_record,
npc_ids,
npc_records_by_id,
storage_records_by_id,
event_records_by_id,
knowledge_records_by_key,
@@ -398,19 +403,16 @@ static func _is_valid_knowledge_provenance(
static func _is_valid_opportunity(
opportunity: OpportunityStateRecord,
npc_ids: Dictionary,
npc_records_by_id: Dictionary,
storage_records_by_id: Dictionary,
event_records_by_id: Dictionary,
knowledge_records_by_key: Dictionary,
resource_records_by_id: Dictionary,
current_tick: int
) -> bool:
if opportunity.get_opportunity_type() != SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY:
return false
if (
not npc_ids.has(opportunity.get_interested_npc_id())
or opportunity.get_target_id() != SimulationIds.STORAGE_VILLAGE_PANTRY
or not storage_records_by_id.has(opportunity.get_target_id())
or opportunity.get_resource_id() != SimulationIds.RESOURCE_FOOD
or not is_finite(opportunity.get_target_amount())
or opportunity.get_target_amount() <= 0.0
):
@@ -418,8 +420,17 @@ static func _is_valid_opportunity(
var trigger_event := (
event_records_by_id.get(opportunity.get_trigger_event_id()) as EconomicEventRecord
)
if trigger_event == null or not _is_valid_pantry_empty_trigger(trigger_event, npc_ids):
if trigger_event == null:
return false
match opportunity.get_opportunity_type():
SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY:
if not _is_valid_pantry_empty_trigger(trigger_event, npc_ids):
return false
SimulationIds.OPPORTUNITY_SUPPLY_MISSING_WOOD:
if not _is_valid_missing_wood_trigger(trigger_event, opportunity, npc_ids):
return false
_:
return false
var trigger_tick := int(trigger_event.data["tick"])
var created_tick := opportunity.get_created_tick()
if created_tick < trigger_tick or created_tick > current_tick:
@@ -428,6 +439,9 @@ static func _is_valid_opportunity(
var status := opportunity.get_status()
var resolution_event_id := opportunity.get_resolution_event_id()
var resolved_tick := opportunity.get_resolved_tick()
var interested_record := (
npc_records_by_id.get(opportunity.get_interested_npc_id()) as NPCStateRecord
)
if status == OpportunityStateRecord.STATUS_OPEN:
var target_storage := (
storage_records_by_id.get(opportunity.get_target_id()) as StorageStateRecord
@@ -439,6 +453,11 @@ static func _is_valid_opportunity(
return (
resolution_event_id == OpportunityStateRecord.NO_EVENT_ID
and resolved_tick == -1
and interested_record != null
and (
opportunity.get_opportunity_type() != SimulationIds.OPPORTUNITY_SUPPLY_MISSING_WOOD
or not bool(interested_record.data["is_dead"])
)
and target_storage != null
and (
target_storage.get_amount(opportunity.get_resource_id())
@@ -447,6 +466,24 @@ static func _is_valid_opportunity(
and evidence != null
and evidence.get_acquired_tick() <= created_tick
)
if status == OpportunityStateRecord.STATUS_INVALIDATED:
if (
opportunity.get_opportunity_type() != SimulationIds.OPPORTUNITY_SUPPLY_MISSING_WOOD
or resolution_event_id != OpportunityStateRecord.NO_EVENT_ID
or resolved_tick != -1
or opportunity.get_closed_tick() < created_tick
or opportunity.get_closed_tick() > current_tick
):
return false
if (
opportunity.get_invalidation_reason()
== SimulationIds.OPPORTUNITY_INVALIDATED_INTERESTED_DIED
):
return interested_record != null and bool(interested_record.data["is_dead"])
return (
opportunity.get_invalidation_reason()
== SimulationIds.OPPORTUNITY_INVALIDATED_EVIDENCE_STALE
)
if status != OpportunityStateRecord.STATUS_RESOLVED:
return false
if resolution_event_id == OpportunityStateRecord.NO_EVENT_ID:
@@ -454,7 +491,9 @@ static func _is_valid_opportunity(
var resolution_event := event_records_by_id.get(resolution_event_id) as EconomicEventRecord
if (
resolution_event == null
or not _is_valid_pantry_supply_resolution(resolution_event, npc_ids, resource_records_by_id)
or not _is_valid_supply_resolution(
resolution_event, opportunity, npc_ids, resource_records_by_id
)
):
return false
var resolution_tick := int(resolution_event.data["tick"])
@@ -466,6 +505,26 @@ static func _is_valid_opportunity(
)
static func _is_valid_missing_wood_trigger(
event: EconomicEventRecord, opportunity: OpportunityStateRecord, npc_ids: Dictionary
) -> bool:
var actor_id := int(event.data["actor_id"])
return (
npc_ids.has(actor_id)
and actor_id == opportunity.get_interested_npc_id()
and StringName(event.data["event_type"]) == SimulationIds.EVENT_TASK_BLOCKED
and StringName(event.data["source_id"]) == SimulationIds.STORAGE_VILLAGE_WOODPILE
and StringName(event.data["item_id"]) == SimulationIds.RESOURCE_WOOD
and (
StringName(event.data.get("action_id", &""))
in [SimulationIds.ACTION_PATROL, SimulationIds.ACTION_STUDY]
)
and is_equal_approx(
float(event.data.get("required_amount", 0.0)), opportunity.get_target_amount()
)
)
static func _is_valid_pantry_empty_trigger(event: EconomicEventRecord, npc_ids: Dictionary) -> bool:
var actor_id := int(event.data["actor_id"])
return (
@@ -478,14 +537,17 @@ static func _is_valid_pantry_empty_trigger(event: EconomicEventRecord, npc_ids:
)
static func _is_valid_pantry_supply_resolution(
event: EconomicEventRecord, npc_ids: Dictionary, resource_records_by_id: Dictionary
static func _is_valid_supply_resolution(
event: EconomicEventRecord,
opportunity: OpportunityStateRecord,
npc_ids: Dictionary,
resource_records_by_id: Dictionary
) -> bool:
var actor_id := int(event.data["actor_id"])
var event_type := StringName(event.data["event_type"])
if (
StringName(event.data["destination_id"]) != SimulationIds.STORAGE_VILLAGE_PANTRY
or StringName(event.data["item_id"]) != SimulationIds.RESOURCE_FOOD
StringName(event.data["destination_id"]) != opportunity.get_target_id()
or StringName(event.data["item_id"]) != opportunity.get_resource_id()
or float(event.data["amount"]) <= 0.0
):
return false
@@ -500,7 +562,7 @@ static func _is_valid_pantry_supply_resolution(
)
return (
resource_state != null
and resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD
and resource_state.get_resource_id() == opportunity.get_resource_id()
and resource_state.can_player_use_resource()
)
return false
@@ -559,10 +621,11 @@ static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary
migrated.get("economic_events", []),
int((migrated.get("simulation", {}) as Dictionary).get("tick_count", 0))
)
migrated["opportunities"] = []
var opportunity_simulation_data: Dictionary = migrated.get("simulation", {})
opportunity_simulation_data["next_opportunity_id"] = 0
migrated["simulation"] = opportunity_simulation_data
if version != OPPORTUNITY_LEGACY_SCHEMA_VERSION:
migrated["opportunities"] = []
var opportunity_simulation_data: Dictionary = migrated.get("simulation", {})
opportunity_simulation_data["next_opportunity_id"] = 0
migrated["simulation"] = opportunity_simulation_data
return migrated