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
+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