feat: add missing wood opportunities
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user