608 lines
22 KiB
GDScript
608 lines
22 KiB
GDScript
class_name SituationSystem
|
|
extends RefCounted
|
|
|
|
const MAX_CONCURRENT_SITUATIONS := 3
|
|
const DEFINITION_PANTRY_SHORTAGE := &"situation_pantry_shortage"
|
|
const PREDICATE_EVENT_TYPE := &"event_type"
|
|
const PREDICATE_EVENT_PAYLOAD_EQUALS := &"event_payload_equals"
|
|
const PREDICATE_EVENT_PARTICIPANT_ID := &"event_participant_id"
|
|
const PREDICATE_EVENT_PANTRY_SUPPLIED := &"event_pantry_supplied"
|
|
const PREDICATE_STATE_NUMBER_LTE := &"state_number_lte"
|
|
const PREDICATE_STATE_NUMBER_GTE := &"state_number_gte"
|
|
const EXPIRY_STATE_NUMBER_GT := &"state_number_gt"
|
|
const EXPIRY_AGE_REACHED := &"age_reached"
|
|
const ALTERNATIVE_RESTOCK := &"restock_pantry"
|
|
const ALTERNATIVE_ENDURE := &"endure_shortage"
|
|
const DIALOGUE_TOPIC_PANTRY_SHORTAGE := &"topic_pantry_shortage"
|
|
const CLOSE_RESOLVED := &"predicate_resolved"
|
|
const CLOSE_EXPIRED := &"predicate_expired"
|
|
const CLOSE_CAPACITY := &"capacity_superseded"
|
|
const DEFAULT_WORLD_ID := &"jajce"
|
|
|
|
var _definitions_by_id: Dictionary = {}
|
|
var _situations_by_id: Dictionary = {}
|
|
var _next_situation_id := 0
|
|
|
|
|
|
static func create_default() -> SituationSystem:
|
|
var system := SituationSystem.new()
|
|
var definitions: Array[SituationDefinition] = [create_pantry_shortage_definition()]
|
|
system.configure(definitions)
|
|
return system
|
|
|
|
|
|
static func create_pantry_shortage_definition() -> SituationDefinition:
|
|
var definition := SituationDefinition.new()
|
|
definition.situation_definition_id = DEFINITION_PANTRY_SHORTAGE
|
|
definition.display_name = "The Pantry Is Empty"
|
|
definition.description = "The village pantry has run out of food."
|
|
definition.severity = 70
|
|
definition.priority = 80
|
|
definition.trigger_predicates = [
|
|
_event_predicate(
|
|
&"pantry_withdrawal", PREDICATE_EVENT_TYPE, {"event_type": "storage_withdrawn"}
|
|
),
|
|
_event_predicate(
|
|
&"withdrawal_from_pantry",
|
|
PREDICATE_EVENT_PARTICIPANT_ID,
|
|
{"role": "source", "entity_id": "village_pantry"}
|
|
),
|
|
_event_predicate(
|
|
&"withdrawal_of_food",
|
|
PREDICATE_EVENT_PAYLOAD_EQUALS,
|
|
{"key": "item_id", "value": "food"}
|
|
),
|
|
_state_predicate(
|
|
&"pantry_empty", PREDICATE_STATE_NUMBER_LTE, {"fact_key": "pantry.food", "value": 0.0}
|
|
),
|
|
]
|
|
definition.resolution_predicates = [
|
|
_event_predicate(
|
|
&"pantry_restocked",
|
|
PREDICATE_EVENT_PANTRY_SUPPLIED,
|
|
{"target_id": "village_pantry", "item_id": "food", "amount": 1.0}
|
|
)
|
|
]
|
|
definition.expiry_predicate_ids = [EXPIRY_AGE_REACHED]
|
|
definition.dedupe_key_fields = [&"world_id", &"source_id"]
|
|
definition.expiry_ticks = 200
|
|
definition.cooldown_ticks = 50
|
|
definition.dialogue_topic_ids = [DIALOGUE_TOPIC_PANTRY_SHORTAGE]
|
|
var restock := SituationAlternativeDefinition.new()
|
|
restock.alternative_id = ALTERNATIVE_RESTOCK
|
|
restock.display_name = "Restock the pantry"
|
|
restock.description = "Bring at least one food item to the village pantry."
|
|
restock.resolution_predicate_ids = [&"pantry_restocked"]
|
|
restock.dialogue_topic_ids = [DIALOGUE_TOPIC_PANTRY_SHORTAGE]
|
|
restock.commitment_terms = {
|
|
"action_id": "deposit_food",
|
|
"target_id": "village_pantry",
|
|
"item_id": "food",
|
|
"amount": 1.0,
|
|
}
|
|
var endure := SituationAlternativeDefinition.new()
|
|
endure.alternative_id = ALTERNATIVE_ENDURE
|
|
endure.display_name = "Endure the shortage"
|
|
endure.description = "Make no promise and let the immediate concern pass."
|
|
endure.resolution_predicate_ids = [&"pantry_restocked"]
|
|
endure.dialogue_topic_ids = [DIALOGUE_TOPIC_PANTRY_SHORTAGE]
|
|
definition.alternatives = [restock, endure]
|
|
return definition
|
|
|
|
|
|
func configure(definitions: Array[SituationDefinition]) -> Array[String]:
|
|
var errors: Array[String] = []
|
|
var definitions_by_id: Dictionary = {}
|
|
for definition in definitions:
|
|
if definition == null:
|
|
errors.append("Situation definitions contain null")
|
|
continue
|
|
for error in (
|
|
definition
|
|
. validate(
|
|
[
|
|
PREDICATE_EVENT_TYPE,
|
|
PREDICATE_EVENT_PAYLOAD_EQUALS,
|
|
PREDICATE_EVENT_PARTICIPANT_ID,
|
|
PREDICATE_EVENT_PANTRY_SUPPLIED,
|
|
],
|
|
[PREDICATE_STATE_NUMBER_LTE, PREDICATE_STATE_NUMBER_GTE],
|
|
[EXPIRY_STATE_NUMBER_GT, EXPIRY_AGE_REACHED]
|
|
)
|
|
):
|
|
errors.append("Situation '%s': %s" % [definition.situation_definition_id, error])
|
|
if definitions_by_id.has(definition.situation_definition_id):
|
|
errors.append(
|
|
"Duplicate situation definition '%s'" % definition.situation_definition_id
|
|
)
|
|
else:
|
|
definitions_by_id[definition.situation_definition_id] = definition
|
|
errors.sort()
|
|
if not errors.is_empty():
|
|
return errors
|
|
_definitions_by_id = definitions_by_id
|
|
return []
|
|
|
|
|
|
func consider_event(
|
|
event: WorldEventRecord, current_tick: int, state_facts: Dictionary
|
|
) -> Array[SituationEvaluationResult]:
|
|
var results: Array[SituationEvaluationResult] = []
|
|
if event == null or current_tick < event.get_tick():
|
|
return results
|
|
for definition in _get_definitions_sorted():
|
|
var context := _build_context(event, state_facts)
|
|
var dedupe_key := _build_dedupe_key(definition, context)
|
|
var result := SituationEvaluationResult.new(definition.situation_definition_id, dedupe_key)
|
|
var trigger_matched := true
|
|
for predicate in definition.trigger_predicates:
|
|
var evaluation := _evaluate_predicate(predicate, event, state_facts)
|
|
result.add_reason(evaluation["reason"])
|
|
if not bool(evaluation["matched"]):
|
|
trigger_matched = false
|
|
result.matched = trigger_matched
|
|
if trigger_matched:
|
|
var blocked_reason := _opening_block_reason(definition, dedupe_key, current_tick)
|
|
if blocked_reason.is_empty():
|
|
var situation := SituationStateRecord.create(
|
|
_next_situation_id,
|
|
definition.situation_definition_id,
|
|
dedupe_key,
|
|
current_tick,
|
|
event.get_event_id(),
|
|
context
|
|
)
|
|
_situations_by_id[_next_situation_id] = situation
|
|
result.opened_situation_id = _next_situation_id
|
|
result.add_reason("opened situation %d" % _next_situation_id)
|
|
_next_situation_id += 1
|
|
else:
|
|
result.add_reason(blocked_reason)
|
|
results.append(result)
|
|
return results
|
|
|
|
|
|
func maintain(
|
|
current_tick: int, world_events: WorldEventStore, state_facts: Dictionary
|
|
) -> Array[SituationStateRecord]:
|
|
var closed: Array[SituationStateRecord] = []
|
|
for situation in get_active_sorted():
|
|
var definition := get_definition(situation.get_definition_id())
|
|
if definition == null:
|
|
continue
|
|
var resolution := derive_progress(situation, world_events, state_facts)
|
|
if resolution.resolved:
|
|
if situation.close(
|
|
SituationStateRecord.STATUS_RESOLVED,
|
|
current_tick,
|
|
CLOSE_RESOLVED,
|
|
resolution.matched_event_id
|
|
):
|
|
closed.append(situation)
|
|
continue
|
|
if _is_expired(situation, definition, current_tick, state_facts):
|
|
if situation.close(SituationStateRecord.STATUS_EXPIRED, current_tick, CLOSE_EXPIRED):
|
|
closed.append(situation)
|
|
return closed
|
|
|
|
|
|
func derive_progress(
|
|
situation: SituationStateRecord,
|
|
world_events: WorldEventStore,
|
|
state_facts: Dictionary,
|
|
alternative_id: StringName = &""
|
|
) -> SituationProgressSnapshot:
|
|
var snapshot := SituationProgressSnapshot.new()
|
|
if situation == null:
|
|
snapshot.reason_traces.append("situation is null")
|
|
return snapshot
|
|
snapshot.situation_id = situation.get_situation_id()
|
|
var definition := get_definition(situation.get_definition_id())
|
|
if definition == null:
|
|
snapshot.reason_traces.append("definition is unavailable")
|
|
return snapshot
|
|
var selected_alternative := alternative_id
|
|
if selected_alternative.is_empty():
|
|
selected_alternative = situation.get_selected_alternative_id()
|
|
snapshot.alternative_id = selected_alternative
|
|
var predicate_ids: Array[StringName] = []
|
|
if not selected_alternative.is_empty():
|
|
var alternative := definition.get_alternative(selected_alternative)
|
|
if alternative == null:
|
|
snapshot.reason_traces.append("selected alternative is unavailable")
|
|
return snapshot
|
|
predicate_ids = alternative.resolution_predicate_ids
|
|
else:
|
|
for predicate in definition.resolution_predicates:
|
|
predicate_ids.append(predicate.predicate_id)
|
|
for predicate_id in predicate_ids:
|
|
var predicate := definition.get_predicate(predicate_id)
|
|
if predicate == null:
|
|
snapshot.reason_traces.append("predicate '%s' is unavailable" % predicate_id)
|
|
continue
|
|
var evaluation := _evaluate_resolution_predicate(
|
|
predicate, situation, world_events, state_facts
|
|
)
|
|
snapshot.reason_traces.append(evaluation["reason"])
|
|
if bool(evaluation["matched"]):
|
|
snapshot.resolved = true
|
|
snapshot.current_value = float(evaluation.get("current_value", 1.0))
|
|
snapshot.target_value = float(evaluation.get("target_value", 1.0))
|
|
snapshot.matched_event_id = int(evaluation.get("event_id", -1))
|
|
return snapshot
|
|
snapshot.current_value = float(evaluation.get("current_value", snapshot.current_value))
|
|
snapshot.target_value = float(evaluation.get("target_value", snapshot.target_value))
|
|
return snapshot
|
|
|
|
|
|
func import_opportunities(opportunities: Array[OpportunityStateRecord]) -> Array[String]:
|
|
var errors: Array[String] = []
|
|
for opportunity in opportunities:
|
|
var mapped_definition_id := opportunity.get_opportunity_type()
|
|
if opportunity.get_opportunity_type() == SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY:
|
|
mapped_definition_id = DEFINITION_PANTRY_SHORTAGE
|
|
if not _definitions_by_id.has(mapped_definition_id):
|
|
errors.append(
|
|
(
|
|
"No situation definition maps opportunity type '%s'"
|
|
% opportunity.get_opportunity_type()
|
|
)
|
|
)
|
|
continue
|
|
var situation := SituationStateRecord.from_opportunity(opportunity, mapped_definition_id)
|
|
if situation == null:
|
|
errors.append("Could not convert an opportunity")
|
|
continue
|
|
var situation_id := situation.get_situation_id()
|
|
if _situations_by_id.has(situation_id):
|
|
errors.append("Duplicate imported situation ID %d" % situation_id)
|
|
continue
|
|
_situations_by_id[situation_id] = situation
|
|
_next_situation_id = maxi(_next_situation_id, situation_id + 1)
|
|
return errors
|
|
|
|
|
|
func restore(records: Array[SituationStateRecord], restored_next_id: int) -> bool:
|
|
var by_id: Dictionary = {}
|
|
var active_keys: Dictionary = {}
|
|
var active_count := 0
|
|
for record in records:
|
|
if (
|
|
record == null
|
|
or by_id.has(record.get_situation_id())
|
|
or not _definitions_by_id.has(record.get_definition_id())
|
|
):
|
|
return false
|
|
if record.is_active():
|
|
var active_key := "%s|%s" % [record.get_definition_id(), record.get_dedupe_key()]
|
|
if active_keys.has(active_key):
|
|
return false
|
|
active_keys[active_key] = true
|
|
active_count += 1
|
|
if active_count > MAX_CONCURRENT_SITUATIONS:
|
|
return false
|
|
by_id[record.get_situation_id()] = record
|
|
_situations_by_id = by_id
|
|
_next_situation_id = maxi(restored_next_id, 0)
|
|
for situation_id in by_id:
|
|
_next_situation_id = maxi(_next_situation_id, int(situation_id) + 1)
|
|
return true
|
|
|
|
|
|
func get_definition(definition_id: StringName) -> SituationDefinition:
|
|
return _definitions_by_id.get(definition_id) as SituationDefinition
|
|
|
|
|
|
func get_by_id(situation_id: int) -> SituationStateRecord:
|
|
return _situations_by_id.get(situation_id) as SituationStateRecord
|
|
|
|
|
|
func get_all_sorted() -> Array[SituationStateRecord]:
|
|
var situations: Array[SituationStateRecord] = []
|
|
for situation in _situations_by_id.values():
|
|
situations.append(situation)
|
|
situations.sort_custom(_sort_situations)
|
|
return situations
|
|
|
|
|
|
func get_active_sorted() -> Array[SituationStateRecord]:
|
|
var active: Array[SituationStateRecord] = []
|
|
for situation in _situations_by_id.values():
|
|
if situation.is_active():
|
|
active.append(situation)
|
|
active.sort_custom(_sort_situations)
|
|
return active
|
|
|
|
|
|
func get_next_situation_id() -> int:
|
|
return _next_situation_id
|
|
|
|
|
|
func _get_definitions_sorted() -> Array[SituationDefinition]:
|
|
var definitions: Array[SituationDefinition] = []
|
|
for definition in _definitions_by_id.values():
|
|
definitions.append(definition)
|
|
definitions.sort_custom(_sort_definitions)
|
|
return definitions
|
|
|
|
|
|
func _opening_block_reason(
|
|
definition: SituationDefinition, dedupe_key: String, current_tick: int
|
|
) -> String:
|
|
for situation in get_all_sorted():
|
|
if (
|
|
situation.get_definition_id() != definition.situation_definition_id
|
|
or situation.get_dedupe_key() != dedupe_key
|
|
):
|
|
continue
|
|
if situation.is_active():
|
|
return "dedupe blocked by active situation %d" % situation.get_situation_id()
|
|
if current_tick - situation.get_closed_tick() < definition.cooldown_ticks:
|
|
return (
|
|
"cooldown active until tick %d"
|
|
% (situation.get_closed_tick() + definition.cooldown_ticks)
|
|
)
|
|
if get_active_sorted().size() >= MAX_CONCURRENT_SITUATIONS:
|
|
return "concurrency cap %d reached" % MAX_CONCURRENT_SITUATIONS
|
|
return ""
|
|
|
|
|
|
func _build_context(event: WorldEventRecord, state_facts: Dictionary) -> Dictionary:
|
|
var context := {
|
|
"world_id": String(DEFAULT_WORLD_ID),
|
|
"event_id": event.get_event_id(),
|
|
"event_type": String(event.get_event_type()),
|
|
}
|
|
if event.has_location() and not event.get_location().get_world_id().is_empty():
|
|
context["world_id"] = String(event.get_location().get_world_id())
|
|
var destination := event.get_participant(WorldEventRecord.ROLE_DESTINATION)
|
|
var source := event.get_participant(WorldEventRecord.ROLE_SOURCE)
|
|
var actor := event.get_participant(WorldEventRecord.ROLE_ACTOR)
|
|
if actor != null:
|
|
context["actor_id"] = String(actor.get_entity_id())
|
|
context["actor_type"] = String(actor.get_entity_type())
|
|
if state_facts.has("interested_npc_id"):
|
|
context["interested_npc_id"] = str(state_facts["interested_npc_id"])
|
|
elif actor != null and actor.get_entity_type() == WorldEventRecord.ENTITY_TYPE_NPC:
|
|
context["interested_npc_id"] = String(actor.get_entity_id())
|
|
if source != null:
|
|
context["source_id"] = String(source.get_entity_id())
|
|
if destination != null:
|
|
context["destination_id"] = String(destination.get_entity_id())
|
|
context["target_id"] = String(destination.get_entity_id())
|
|
elif state_facts.has("target_id"):
|
|
context["target_id"] = String(state_facts["target_id"])
|
|
return context
|
|
|
|
|
|
func _build_dedupe_key(definition: SituationDefinition, context: Dictionary) -> String:
|
|
var parts: Array[String] = [String(definition.situation_definition_id)]
|
|
for field in definition.dedupe_key_fields:
|
|
parts.append("%s=%s" % [field, str(context.get(String(field), ""))])
|
|
return "|".join(parts)
|
|
|
|
|
|
func _evaluate_predicate(
|
|
predicate: SituationPredicate, event: WorldEventRecord, state_facts: Dictionary
|
|
) -> Dictionary:
|
|
if StringName(predicate.source) == SituationPredicate.SOURCE_EVENT:
|
|
return _evaluate_event_predicate(predicate, event)
|
|
return _evaluate_state_predicate(predicate, state_facts)
|
|
|
|
|
|
func _evaluate_event_predicate(
|
|
predicate: SituationPredicate, event: WorldEventRecord
|
|
) -> Dictionary:
|
|
if predicate.evaluator_id == PREDICATE_EVENT_TYPE:
|
|
var expected := StringName(predicate.parameters.get("event_type", ""))
|
|
var matched := event.get_event_type() == expected
|
|
return {
|
|
"matched": matched,
|
|
"reason":
|
|
(
|
|
"event type %s %s %s"
|
|
% [event.get_event_type(), "matched" if matched else "did not match", expected]
|
|
),
|
|
"event_id": event.get_event_id(),
|
|
}
|
|
if predicate.evaluator_id == PREDICATE_EVENT_PAYLOAD_EQUALS:
|
|
var key := String(predicate.parameters.get("key", ""))
|
|
var expected: Variant = predicate.parameters.get("value")
|
|
var actual: Variant = event.get_payload().get(key)
|
|
var matched: bool = actual == expected
|
|
return {
|
|
"matched": matched,
|
|
"reason":
|
|
(
|
|
"event payload %s=%s %s expected %s"
|
|
% [key, actual, "matched" if matched else "did not match", expected]
|
|
),
|
|
"event_id": event.get_event_id(),
|
|
}
|
|
if predicate.evaluator_id == PREDICATE_EVENT_PARTICIPANT_ID:
|
|
var role := StringName(predicate.parameters.get("role", ""))
|
|
var expected := StringName(predicate.parameters.get("entity_id", ""))
|
|
var participant := event.get_participant(role)
|
|
var actual: StringName = participant.get_entity_id() if participant != null else &""
|
|
var matched: bool = actual == expected
|
|
return {
|
|
"matched": matched,
|
|
"reason":
|
|
(
|
|
"event participant %s=%s %s %s"
|
|
% [role, actual, "matched" if matched else "did not match", expected]
|
|
),
|
|
"event_id": event.get_event_id(),
|
|
}
|
|
if predicate.evaluator_id == PREDICATE_EVENT_PANTRY_SUPPLIED:
|
|
return _evaluate_pantry_supply_event(predicate, event)
|
|
return {
|
|
"matched": false,
|
|
"reason": "unsupported event predicate '%s'" % predicate.evaluator_id,
|
|
}
|
|
|
|
|
|
func _evaluate_state_predicate(
|
|
predicate: SituationPredicate, state_facts: Dictionary
|
|
) -> Dictionary:
|
|
var fact_key := String(predicate.parameters.get("fact_key", ""))
|
|
if fact_key.is_empty() or not state_facts.has(fact_key):
|
|
return {"matched": false, "reason": "state fact '%s' is unavailable" % fact_key}
|
|
var actual := float(state_facts[fact_key])
|
|
var expected := float(predicate.parameters.get("value", 0.0))
|
|
var matched := false
|
|
match predicate.evaluator_id:
|
|
PREDICATE_STATE_NUMBER_LTE:
|
|
matched = actual <= expected
|
|
PREDICATE_STATE_NUMBER_GTE:
|
|
matched = actual >= expected
|
|
_:
|
|
return {
|
|
"matched": false,
|
|
"reason": "unsupported state predicate '%s'" % predicate.evaluator_id
|
|
}
|
|
return {
|
|
"matched": matched,
|
|
"reason":
|
|
(
|
|
"state %s=%.2f %s %.2f"
|
|
% [fact_key, actual, "matched" if matched else "did not match", expected]
|
|
),
|
|
"current_value": actual,
|
|
"target_value": expected,
|
|
}
|
|
|
|
|
|
func _evaluate_resolution_predicate(
|
|
predicate: SituationPredicate,
|
|
situation: SituationStateRecord,
|
|
world_events: WorldEventStore,
|
|
state_facts: Dictionary
|
|
) -> Dictionary:
|
|
if StringName(predicate.source) == SituationPredicate.SOURCE_STATE:
|
|
return _evaluate_state_predicate(predicate, state_facts)
|
|
if world_events == null:
|
|
return {"matched": false, "reason": "world event store is unavailable"}
|
|
for event in world_events.get_between_ticks(situation.get_created_tick(), 2147483647):
|
|
if event.get_event_id() <= situation.get_trigger_event_id():
|
|
continue
|
|
var evaluation := _evaluate_event_predicate(predicate, event)
|
|
if bool(evaluation["matched"]):
|
|
return evaluation
|
|
return {"matched": false, "reason": "no later event matched '%s'" % predicate.predicate_id}
|
|
|
|
|
|
func _evaluate_pantry_supply_event(
|
|
predicate: SituationPredicate, event: WorldEventRecord
|
|
) -> Dictionary:
|
|
var expected_target := StringName(predicate.parameters.get("target_id", ""))
|
|
var expected_item := StringName(predicate.parameters.get("item_id", ""))
|
|
var expected_amount := float(predicate.parameters.get("amount", 0.0))
|
|
var payload := event.get_payload()
|
|
var destination := event.get_participant(WorldEventRecord.ROLE_DESTINATION)
|
|
var source := event.get_participant(WorldEventRecord.ROLE_SOURCE)
|
|
var actor := event.get_participant(WorldEventRecord.ROLE_ACTOR)
|
|
var destination_id := destination.get_entity_id() if destination != null else &""
|
|
var source_id := source.get_entity_id() if source != null else &""
|
|
var actor_id := actor.get_entity_id() if actor != null else &""
|
|
var event_type := event.get_event_type()
|
|
var item_matches := StringName(payload.get("item_id", "")) == expected_item
|
|
var amount := float(payload.get("amount", 0.0))
|
|
var route_matches := false
|
|
if event_type == &"storage_deposited":
|
|
var expected_source := (
|
|
&"player_inventory" if actor_id == &"-1" else StringName("npc_inventory_%s" % actor_id)
|
|
)
|
|
route_matches = not actor_id.is_empty() and source_id == expected_source
|
|
elif event_type == &"resource_extracted":
|
|
route_matches = actor_id == &"-1" and not source_id.is_empty()
|
|
var matched := (
|
|
destination_id == expected_target
|
|
and item_matches
|
|
and amount >= expected_amount
|
|
and route_matches
|
|
)
|
|
return {
|
|
"matched": matched,
|
|
"reason":
|
|
(
|
|
"supply event %d %s target=%s item=%s amount=%.2f"
|
|
% [
|
|
event.get_event_id(),
|
|
"matched" if matched else "did not match",
|
|
destination_id,
|
|
payload.get("item_id", ""),
|
|
amount
|
|
]
|
|
),
|
|
"event_id": event.get_event_id(),
|
|
"current_value": amount if matched else 0.0,
|
|
"target_value": expected_amount,
|
|
}
|
|
|
|
|
|
func _is_expired(
|
|
situation: SituationStateRecord,
|
|
definition: SituationDefinition,
|
|
current_tick: int,
|
|
state_facts: Dictionary
|
|
) -> bool:
|
|
for expiry_id in definition.expiry_predicate_ids:
|
|
if expiry_id == EXPIRY_AGE_REACHED and definition.expiry_ticks > 0:
|
|
if current_tick - situation.get_created_tick() >= definition.expiry_ticks:
|
|
return true
|
|
elif expiry_id == EXPIRY_STATE_NUMBER_GT:
|
|
var fact_key := String(situation.get_context().get("expiry_fact_key", ""))
|
|
var threshold := float(situation.get_context().get("expiry_value", 0.0))
|
|
if state_facts.has(fact_key) and float(state_facts[fact_key]) > threshold:
|
|
return true
|
|
return false
|
|
|
|
|
|
static func _event_predicate(
|
|
label: StringName, predicate_id: StringName, parameters: Dictionary
|
|
) -> SituationPredicate:
|
|
var predicate := SituationPredicate.new()
|
|
predicate.resource_name = String(label)
|
|
predicate.predicate_id = label
|
|
predicate.evaluator_id = predicate_id
|
|
predicate.source = String(SituationPredicate.SOURCE_EVENT)
|
|
predicate.parameters = parameters
|
|
return predicate
|
|
|
|
|
|
static func _state_predicate(
|
|
label: StringName, predicate_id: StringName, parameters: Dictionary
|
|
) -> SituationPredicate:
|
|
var predicate := SituationPredicate.new()
|
|
predicate.resource_name = String(label)
|
|
predicate.predicate_id = label
|
|
predicate.evaluator_id = predicate_id
|
|
predicate.source = String(SituationPredicate.SOURCE_STATE)
|
|
predicate.parameters = parameters
|
|
return predicate
|
|
|
|
|
|
static func _sort_definitions(first: SituationDefinition, second: SituationDefinition) -> bool:
|
|
if first.priority != second.priority:
|
|
return first.priority > second.priority
|
|
if first.severity != second.severity:
|
|
return first.severity > second.severity
|
|
return String(first.situation_definition_id) < String(second.situation_definition_id)
|
|
|
|
|
|
func _sort_situations(first: SituationStateRecord, second: SituationStateRecord) -> bool:
|
|
var first_definition := get_definition(first.get_definition_id())
|
|
var second_definition := get_definition(second.get_definition_id())
|
|
var first_priority := first_definition.priority if first_definition != null else -101
|
|
var second_priority := second_definition.priority if second_definition != null else -101
|
|
if first_priority != second_priority:
|
|
return first_priority > second_priority
|
|
var first_severity := first_definition.severity if first_definition != null else -1
|
|
var second_severity := second_definition.severity if second_definition != null else -1
|
|
if first_severity != second_severity:
|
|
return first_severity > second_severity
|
|
if first.get_created_tick() != second.get_created_tick():
|
|
return first.get_created_tick() < second.get_created_tick()
|
|
return first.get_situation_id() < second.get_situation_id()
|