137 lines
6.1 KiB
GDScript
137 lines
6.1 KiB
GDScript
class_name ConditionEngine
|
|
extends RefCounted
|
|
|
|
const StatusEffectType = preload("res://scripts/domain/combat/status_effect.gd")
|
|
|
|
var diagnostics := PackedStringArray()
|
|
|
|
func evaluate(condition: Dictionary, context: Dictionary) -> bool:
|
|
if condition.is_empty():
|
|
return true
|
|
var kind := StringName(condition.get("type", ""))
|
|
var player: PlayerState = context.get("player")
|
|
if player == null:
|
|
_diagnose("Condition requires a player: %s" % kind)
|
|
return false
|
|
match kind:
|
|
&"skill":
|
|
return player.effective_skill(StringName(condition.get("skill", ""))) >= int(condition.get("min", 0))
|
|
&"attr":
|
|
return player.effective_attribute(StringName(condition.get("attr", ""))) >= int(condition.get("min", 0))
|
|
&"level":
|
|
return player.level >= int(condition.get("min", 1))
|
|
&"flag":
|
|
return player.flags.get(StringName(condition.get("flag", "")), false) == condition.get("equals", true)
|
|
&"reputation":
|
|
return player.reputation.get(StringName(condition.get("faction", "")), 0) >= int(condition.get("min", 0))
|
|
&"gold":
|
|
return int(_payload(context).get("gold", 0)) >= int(condition.get("min", 0))
|
|
&"archetype":
|
|
if condition.has("is_set"):
|
|
return (not player.archetype.is_empty()) == bool(condition["is_set"])
|
|
return player.archetype == StringName(condition.get("value", ""))
|
|
&"has_race":
|
|
if condition.has("is_set"):
|
|
return (not player.race.is_empty()) == bool(condition["is_set"])
|
|
return player.race == StringName(condition.get("value", ""))
|
|
&"payload_flag":
|
|
return _payload(context).get(StringName(condition.get("key", "")), false) == condition.get("equals", true)
|
|
&"payload_equals":
|
|
return _payload(context).get(StringName(condition.get("key", ""))) == condition.get("value")
|
|
&"payload_min":
|
|
return float(_payload(context).get(StringName(condition.get("key", "")), 0)) >= float(condition.get("min", 0))
|
|
&"payload_max":
|
|
return float(_payload(context).get(StringName(condition.get("key", "")), 0)) <= float(condition.get("max", 0))
|
|
&"payload_contains":
|
|
var value: Variant = _payload(context).get(StringName(condition.get("key", "")))
|
|
return value is Array and value.has(condition.get("value")) or value is String and String(value).contains(String(condition.get("value", "")))
|
|
&"quest_state":
|
|
var quest_manager: QuestManager = context.get("quest_manager")
|
|
return quest_manager != null and quest_manager.condition_met(condition)
|
|
&"has_item":
|
|
var inventory: InventoryState = context.get("inventory")
|
|
if inventory == null:
|
|
return false
|
|
return inventory.items.any(func(item: ItemInstance) -> bool: return item.definition.id == StringName(condition.get("item", "")))
|
|
&"not":
|
|
|
|
return not evaluate(condition.get("cond", {}), context)
|
|
&"all":
|
|
for nested: Dictionary in condition.get("conds", []):
|
|
if not evaluate(nested, context):
|
|
return false
|
|
return true
|
|
&"any":
|
|
for nested: Dictionary in condition.get("conds", []):
|
|
if evaluate(nested, context):
|
|
return true
|
|
return false
|
|
_:
|
|
_diagnose("Unknown condition type: %s" % kind)
|
|
return false
|
|
|
|
func evaluate_all(conditions: Array, context: Dictionary) -> bool:
|
|
for condition: Dictionary in conditions:
|
|
if not evaluate(condition, context):
|
|
return false
|
|
return true
|
|
|
|
func apply_effects(effects: Array, context: Dictionary) -> Array[Dictionary]:
|
|
var applications: Array[Dictionary] = []
|
|
for effect: Dictionary in effects:
|
|
applications.append(apply_effect(effect, context))
|
|
return applications
|
|
|
|
func apply_effect(effect: Dictionary, context: Dictionary) -> Dictionary:
|
|
var player: PlayerState = context.get("player")
|
|
var kind := StringName(effect.get("type", ""))
|
|
if player == null:
|
|
_diagnose("Effect requires a player: %s" % kind)
|
|
return {"applied": false, "type": kind}
|
|
var quest_manager: QuestManager = context.get("quest_manager")
|
|
if quest_manager != null and kind in [&"start_quest", &"complete_objective", &"remove_item"]:
|
|
return quest_manager.apply_effect(effect, context)
|
|
match kind:
|
|
&"set_flag":
|
|
player.flags[StringName(effect.get("flag", ""))] = effect.get("value", true)
|
|
&"add_attr":
|
|
player.modify_attribute(StringName(effect.get("attr", "")), int(effect.get("amount", 1)))
|
|
&"add_skill":
|
|
player.modify_skill(StringName(effect.get("skill", "")), int(effect.get("amount", 1)))
|
|
&"apply_status", &"inflict_status":
|
|
player.apply_status(StatusEffectType.new(StringName(effect.get("status", "")), int(effect.get("duration", -1)), int(effect.get("magnitude", 0))))
|
|
&"cure_status":
|
|
player.clear_status(StringName(effect.get("status", "")))
|
|
&"heal":
|
|
player.hit_points = mini(player.max_hp(), player.hit_points + int(effect.get("amount", 0)))
|
|
&"heal_pct":
|
|
player.hit_points = mini(player.max_hp(), player.hit_points + int(player.max_hp() * float(effect.get("pct", 0.0))))
|
|
&"damage":
|
|
player.hit_points = maxi(0, player.hit_points - int(effect.get("amount", 0)))
|
|
&"add_rep":
|
|
var faction := StringName(effect.get("faction", ""))
|
|
player.reputation[faction] = player.reputation.get(faction, 0) + int(effect.get("amount", 0))
|
|
&"join_party":
|
|
var party: PartyState = context.get("party")
|
|
var recruit: ActorState = context.get("recruit")
|
|
var map: GridMapState = context.get("map")
|
|
if party == null or recruit == null or map == null:
|
|
return {"applied": false, "type": kind, "reason": &"missing_party_context"}
|
|
var result := party.recruit(recruit, player.cell, map, context.get("blocked_cells", {}))
|
|
return {"applied": bool(result.get("accepted", false)), "type": kind, "result": result}
|
|
&"message", &"give_item", &"remove_item", &"learn_spell", &"learn_random_spell", &"complete_objective", &"endanger_tourist", &"augment_companion", &"tourist_selfie":
|
|
# Later content slices own the corresponding inventory, quest, NPC, and spell mutations.
|
|
pass
|
|
_:
|
|
_diagnose("Unknown effect type: %s" % kind)
|
|
return {"applied": false, "type": kind}
|
|
return {"applied": true, "type": kind}
|
|
|
|
func _payload(context: Dictionary) -> Dictionary:
|
|
var payload: Variant = context.get("payload", {})
|
|
return payload if payload is Dictionary else {}
|
|
|
|
func _diagnose(message: String) -> void:
|
|
# Keep unsupported content visible to callers without silently granting access.
|
|
diagnostics.append(message)
|