ludic/examples/events/cancel.ludic
Orkuncakilkaya bf36bc8a8f refactor(runtime,packages,examples): named constants, package enums, idiom sweep
- runtime: HEADLESS_FRAME_PATH, STICK_DEADZONE / STICK_LEFT_X/Y, key and
  byte codes as char literals throughout (`k == 'w'`, `fill(rt_map, ' ', …)`)
- ludic.gameplay/stats: drop the duplicate `stat_field` (it answered "atk"
  for every build stat); Stats.base uses stats_field_name
- ludic.shooter: compare aim modes and fire patterns with AimMode.* and
  WeaponPattern.* instead of raw ints; STICK_RIGHT_X/Y
- ludic.npcai: DecisionMade / brain_set_state use AiState.*
- examples/games/menu.ludic uses Font.load / Ui.* with FONT_PATH and
  BACKDROP named; strings.ludic header says what it prints
- whole tree: `x = x + 1` → `x += 1` (single-term right-hand sides only),
  `0 - x` → `-x`, ASCII codes → char literals; every .ludic and every
  ```ludic fence reformatted with the fixed formatter (whitespace only)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-05 01:12:26 +03:00

27 lines
1.2 KiB
Text

# cancel.ludic — EV3: cancellable (decision) events.
#
# A `cancellable` event is fired BEFORE an action so a listener can veto it. The
# listener calls `cancel`; the caller reads the verdict back — `emit E(…)` used as
# an expression yields the cancelled flag (1 = vetoed, 0 = allowed). A foreign mod
# vetoes the same way, by setting the payload's trailing `cancelled` field over
# the ABI. This is the modding headline: observation becomes control.
#
# Running it prints: 0 1 92
program Cancel {
event cancellable BeforeHurt { amount: int = 0 }
@On(BeforeHurt) handler Armor { if amount > 10 { cancel } } # veto any hit over 10
entry {
let v1 = emit BeforeHurt(amount: 5) # 5 <= 10 -> allowed
print(v1) # 0
let v2 = emit BeforeHurt(amount: 15) # 15 > 10 -> vetoed
print(v2) # 1
# the realistic shape: only apply the effect when the decision isn't vetoed
var hp = 100
if emit BeforeHurt(amount: 8) == 0 { hp -= 8 } # allowed -> 92
if emit BeforeHurt(amount: 50) == 0 { hp -= 50 } # vetoed -> unchanged
print(hp) # 92
}
}