Files
windroost/p2_godot_sample/scripts/effects.gd
T
wushenghua 16d3cbe911 feat: effect-primitive engine foundation (stage A of gameplay-depth goal)
Continued PRODUCTION/51. Built the ADR-008 effect-primitive system that turns
skills from damage numbers into real distinct combat effects:
- tools/fullgame/effects.py + GDScript effects.gd mirror: 16 primitives
  (damage/heal/shield/buff/debuff/stun/slow/poison/taunt/summon/cleanse/
  execute/chain/lifesteal/energy/reflect) with real battle semantics.
- test_effects.py PASS 9 checks (each primitive distinct + identity).
- check_effects_parity.py PASS 17 cases (python==gd).
- skill_runtime.py bridge: folds skill effects into per-tick battle state;
  test_skill_runtime.py PASS 7 checks proving >=4 skills give distinct outcomes.
- Identity invariant: absent skills -> zero change (all 64 gates green).

Next: wire skill_runtime into both battle sims (with parity + identity guard).
P2_CHECKS_PASS. prototype/ untracked.
2026-11-19 13:07:43 +08:00

159 lines
6.4 KiB
GDScript

class_name Effects
extends RefCounted
## GDScript mirror of tools/fullgame/effects.py (ADR-008 effect primitives).
## Must produce identical apply/tick output to the Python engine (parity tested).
## Identity invariant: no effect_primitive -> no change in battle.
const PRIMITIVES := ["damage", "heal", "shield", "buff", "debuff", "stun",
"slow", "poison", "taunt", "summon", "cleanse", "execute", "chain",
"lifesteal", "energy", "reflect"]
static func new_skill_state() -> Dictionary:
return {
"ally_shield": 0.0, "ally_buff_atk": 0.0, "ally_buff_def": 0.0,
"debuffs": {}, "stunned": {}, "taunt_target": "",
"summons_dps": 0.0, "lifesteal_pct": 0.0, "energy_bonus": 0.0,
"reflect_pct": 0.0, "event_counts": {},
}
static func _bump(state: Dictionary, prim: String) -> void:
var c: Dictionary = state["event_counts"]
c[prim] = int(c.get(prim, 0)) + 1
static func _find_enemy(enemies: Array, target_id: String) -> Dictionary:
for e in enemies:
var en: Dictionary = e
if bool(en.get("dead", false)):
continue
if str(en.get("id", "")) == target_id or str(en.get("content_id", "")) == target_id:
return en
return {}
static func apply_effect(state: Dictionary, effect: Dictionary, ctx: Dictionary) -> Dictionary:
var t := str(effect.get("type", "damage"))
var v := float(effect.get("value", 0.0))
var dur := float(effect.get("duration", 0.0))
var ctx_enemies: Array = ctx.get("enemies", [])
var target_id := str(effect.get("target_id", ctx.get("target_id", "")))
var out := {"primitive": t, "damage": 0.0, "healed": 0.0, "shield_gain": 0.0,
"killed_ids": [], "skipped": [], "meta": {}}
_bump(state, t)
var tick_s := float(ctx.get("tick_seconds", 0.1))
match t:
"damage":
out["damage"] = v
"execute":
var threshold := float(effect.get("threshold", 0.25))
var e := _find_enemy(ctx_enemies, target_id)
if not e.is_empty() and float(e.get("hp", 0.0)) <= float(e.get("max_hp", 1.0)) * threshold:
(out["killed_ids"] as Array).append(str(e["id"]))
out["damage"] = float(out["damage"]) + float(e["hp"])
(out["meta"] as Dictionary)["executed"] = true
"chain":
var jumps := int(effect.get("jumps", 2))
var hit := 0
for e0 in ctx_enemies:
var en: Dictionary = e0
if bool(en.get("dead", false)) or str(en.get("id", "")) == target_id:
continue
out["damage"] = float(out["damage"]) + v
var arr: Array = (out["meta"] as Dictionary).get("chain_targets", [])
arr.append(str(en["id"]))
(out["meta"] as Dictionary)["chain_targets"] = arr
hit += 1
if hit >= jumps:
break
"poison", "debuff", "slow":
var e1 := _find_enemy(ctx_enemies, target_id)
if not e1.is_empty():
var dbs: Dictionary = state["debuffs"]
var d: Dictionary = dbs.get(str(e1["id"]), {"atk":0.0,"slow":0.0,"poison_dps":0.0,"ticks":0})
if t == "poison":
d["poison_dps"] = float(d.get("poison_dps", 0.0)) + v
elif t == "debuff":
var stat := str(effect.get("stat", "atk"))
d[stat] = float(d.get(stat, 0.0)) + v
else:
d["slow"] = minf(0.9, float(d.get("slow", 0.0)) + v)
d["ticks"] = maxi(int(d.get("ticks", 0)), int(ceil(dur / maxf(0.01, tick_s))))
dbs[str(e1["id"])] = d
state["debuffs"] = dbs
"stun":
var e2 := _find_enemy(ctx_enemies, target_id)
if not e2.is_empty():
var st: Dictionary = state["stunned"]
st[str(e2["id"])] = maxf(float(st.get(str(e2["id"]), 0.0)), dur)
state["stunned"] = st
(out["skipped"] as Array).append(str(e2["id"]))
"shield":
out["shield_gain"] = v
"heal":
out["healed"] = v
"lifesteal":
state["lifesteal_pct"] = minf(1.0, float(state["lifesteal_pct"]) + v)
"buff":
var stat2 := str(effect.get("stat", "atk"))
if stat2 == "def":
state["ally_buff_def"] = minf(0.8, float(state["ally_buff_def"]) + v)
else:
state["ally_buff_atk"] = float(state["ally_buff_atk"]) + v
"taunt":
var e3 := _find_enemy(ctx_enemies, target_id)
if not e3.is_empty():
state["taunt_target"] = str(e3["id"])
state["ally_buff_def"] = minf(0.8, float(state["ally_buff_def"]) + v * 0.5)
"summon":
state["summons_dps"] = float(state["summons_dps"]) + v
"cleanse":
state["debuffs"] = {}
(out["meta"] as Dictionary)["cleansed"] = true
"energy":
state["energy_bonus"] = float(state["energy_bonus"]) + v
"reflect":
state["reflect_pct"] = minf(1.0, float(state["reflect_pct"]) + v)
_:
push_error("unknown primitive: " + t)
return out
static func tick_effects(state: Dictionary, tick_seconds: float) -> Dictionary:
var poison := {}
var expired: Array = []
var dbs: Dictionary = state["debuffs"]
for eid in dbs.keys():
var d: Dictionary = dbs[eid]
if float(d.get("poison_dps", 0.0)) > 0.0:
poison[eid] = float(d["poison_dps"]) * tick_seconds
if int(d.get("ticks", 0)) > 0:
d["ticks"] = int(d["ticks"]) - 1
if int(d["ticks"]) <= 0:
expired.append(eid)
for eid in expired:
dbs.erase(eid)
state["debuffs"] = dbs
var st: Dictionary = state["stunned"]
for eid in st.keys():
st[eid] = maxf(0.0, float(st[eid]) - tick_seconds)
if float(st[eid]) <= 0.0:
st.erase(eid)
state["stunned"] = st
return {"poison_damage": poison, "expired": expired}
static func ally_damage_mult(state: Dictionary) -> float:
return 1.0 + float(state["ally_buff_atk"]) + float(state["energy_bonus"]) * 0.25
static func ally_incoming_mult(state: Dictionary) -> float:
return maxf(0.1, 1.0 - float(state["ally_buff_def"]))
static func extra_dps(state: Dictionary) -> float:
return float(state["summons_dps"])