diff --git a/packages/engine/src/game.ts b/packages/engine/src/game.ts index 6f60a60..b789632 100644 --- a/packages/engine/src/game.ts +++ b/packages/engine/src/game.ts @@ -667,7 +667,8 @@ export type GameEvent = | { type: "extraTurnStarted"; player: PlayerId } | { type: "moved"; player: PlayerId; from: Cell; to: Cell; direction: Side; via: "step" | "warp" | "passWall" } | { type: "numberPlayedForMovement"; player: PlayerId; card: CardInstance; value: number; newAllowance: number } - | { type: "punched"; attacker: PlayerId; target: PlayerId; at: Cell } + /** `kind` is set when the target is a creature, which the chronicle names by it. */ + | { type: "punched"; attacker: PlayerId; target: PlayerId | string; at: Cell; kind?: CreatureState["kind"] } | { type: "spellCast"; caster: PlayerId; card: CardInstance; cardId: string; numberCards: CardInstance[]; numberValue: number | null; from: Cell; target: PlayerId | null; targetCell: Cell | null; /** AMPLIFY cards attached (each doubles). */ amplifies?: number } | { type: "counteractionPlayed"; player: PlayerId; card: CardInstance; cardId: string; against: string } @@ -847,7 +848,8 @@ export type Command = * ahead is closed — the ledge runs around the whole rim (rev 14). */ | { type: "move"; direction: Side; over?: boolean; exit?: Side } | { type: "playNumberForMovement"; instanceId: string; addInstanceId?: string } - | { type: "punch"; targetId: PlayerId } + /** A wizard, or a creature, sharing your square. */ + | { type: "punch"; targetId: PlayerId | string } | { type: "tearTreasure"; targetId: PlayerId } | { type: "punchWall"; cell: Cell; side: Side } | { type: "testIllusion"; cell: Cell; side: Side } @@ -5184,7 +5186,11 @@ function doPunchWall(prev: GameState, cell: Cell, side: Side): CommandResult { return { ok: true, state, events }; } -function doPunch(prev: GameState, targetId: PlayerId): CommandResult { +/** "When all else fails, you may PUNCH your opponent": one point, bare-handed, + * in the same square, and your attack for the turn. The FAQ has a wraith + * "attacked physically, with a DAGGER, punch, etc.", so a creature sharing the + * square takes the punch as a wizard would, with no hand to counter it. */ +function doPunch(prev: GameState, targetId: PlayerId | string): CommandResult { const pre = attackPreconditions(prev) ?? idiotBlocked(prev, activePlayer(prev).id); if (pre) return err(pre); @@ -5192,7 +5198,10 @@ function doPunch(prev: GameState, targetId: PlayerId): CommandResult { const attacker = activePlayer(state); if (targetId === attacker.id) return err("you cannot attack yourself"); const target = state.players.find((p) => p.id === targetId); - if (!target || !target.alive) return err("no such living player"); + if (!target || !target.alive) { + const creature = creatureById(state, targetId); + return creature ? punchCreature(state, attacker, creature) : err("no such living player"); + } if (cellKey(target.position) !== cellKey(attacker.position)) { return err("you must be in the same square to punch"); } @@ -5202,23 +5211,9 @@ function doPunch(prev: GameState, targetId: PlayerId): CommandResult { (s) => !(s.cardId === "buddy" && s.casterId === attacker.id && s.targetId === target.id), ); - // BLIND: "...engage in combat..." — a blinded brawler flails on a die - // roll; the swing connects only on a 1 (same convention as INVISIBLE). const preRoll: GameEvent[] = []; - if (isBlinded(state, attacker)) { - const roll = rollD4(state, preRoll, attacker.id, "a blind swing — only a 1 connects"); - if (roll !== 1) { - state.turn.attackUsed = true; - return { - ok: true, - state, - events: [...preRoll, { - type: "attackMisdirected", attacker: attacker.id, intended: target.id, - rolledDirection: SIDES[roll - 1]!, newTarget: null, - }], - }; - } - } + const missed = blindSwing(state, attacker, target.id, preRoll); + if (missed) return missed; if (state.turn.attackUsed) state.turn.secondAttackUsed = true; state.turn.attackUsed = true; @@ -5242,6 +5237,51 @@ function doPunch(prev: GameState, targetId: PlayerId): CommandResult { }; } +function punchCreature(state: GameState, attacker: PlayerState, creature: CreatureState): CommandResult { + if (cellKey(creature.position) !== cellKey(attacker.position)) { + return err("you must be in the same square to punch"); + } + // "Players may not attack a creature that they control." The democratic + // monster is everyone's to move and claws its creator like anyone else. + if (creature.controllerId === attacker.id && creature.kind !== "democratic-monster") { + return err("you cannot attack a creature you control"); + } + if (inThornbush(state, attacker)) return err("you cannot attack from inside a thornbush"); + if (isMisted(state, attacker.id)) return err("as mist, you may not attack"); + if (isMisted(state, creature.id)) return err("your target is mist and cannot be attacked"); + + const preRoll: GameEvent[] = []; + const missed = blindSwing(state, attacker, creature.id, preRoll); + if (missed) return missed; + + if (state.turn.attackUsed) state.turn.secondAttackUsed = true; + state.turn.attackUsed = true; + const events: GameEvent[] = [ + ...preRoll, + { type: "punched", attacker: attacker.id, target: creature.id, at: attacker.position, kind: creature.kind }, + ]; + damageCreature(state, events, creature, 1, `punch from ${attacker.id}`); + return { ok: true, state, events }; +} + +/** BLIND: "...engage in combat..." — a blinded brawler flails on a die roll; + * the swing connects only on a 1 (same convention as INVISIBLE). A miss + * spends the attack and is returned whole; null means the swing lands. */ +function blindSwing(state: GameState, attacker: PlayerState, intended: PlayerId | string, preRoll: GameEvent[]): CommandResult | null { + if (!isBlinded(state, attacker)) return null; + const roll = rollD4(state, preRoll, attacker.id, "a blind swing — only a 1 connects"); + if (roll === 1) return null; + state.turn.attackUsed = true; + return { + ok: true, + state, + events: [...preRoll, { + type: "attackMisdirected", attacker: attacker.id, intended, + rolledDirection: SIDES[roll - 1]!, newTarget: null, + }], + }; +} + interface CastConsumables { numbers: CardInstance[]; amplifies: CardInstance[]; diff --git a/packages/engine/test/creatures.test.ts b/packages/engine/test/creatures.test.ts index 5e0fde7..ebde770 100644 --- a/packages/engine/test/creatures.test.ts +++ b/packages/engine/test/creatures.test.ts @@ -1105,3 +1105,55 @@ describe("creatures cannot strike the mist (rev 28)", () => { expect(r.ok).toBe(true); }); }); + +describe("punching a creature", () => { + /** A wraith of `controller`'s standing on `puncher`'s square, `damage` points along. */ + function wraithUnderfoot(state: GameState, controller: PlayerId, puncher: PlayerId, damage = 0) { + const me = state.players.find((p) => p.id === puncher)!; + state.creatures.push({ + id: "w1", kind: "wraith", controllerId: controller, position: { ...me.position }, + damage, maxDamage: 4, movesPerTurn: 3, movementUsed: 0, + wallPassesPerTurn: 1, wallPassUsed: 0, attackUsed: false, justCreated: false, scorchedThisTurn: [], + }); + } + function rig(damage = 0) { + const state = toRound2(newGame().state); + const me = activePlayer(state).id; + const other = state.players.find((p) => p.id !== me)!.id; + wraithUnderfoot(state, other, me, damage); + return { state, me, other }; + } + + it("a wraith in your square takes one point from a punch, which is your attack and opens no window", () => { + const { state, me } = rig(); + const r = applyCommand(state, me, { type: "punch", targetId: "w1" }); + expect(r.ok).toBe(true); + if (!r.ok) return; + expect(r.state.creatures[0]!.damage).toBe(1); + expect(r.state.turn.attackUsed).toBe(true); + expect(r.state.stack).toBeFalsy(); + expect(r.events.some((e) => e.type === "punched" && e.target === "w1" && e.kind === "wraith")).toBe(true); + expect(applyCommand(r.state, me, { type: "punch", targetId: "w1" }).ok).toBe(false); + }); + + it("the fourth point destroys it", () => { + const { state, me } = rig(3); + const r = applyCommand(state, me, { type: "punch", targetId: "w1" }); + expect(r.ok).toBe(true); + if (!r.ok) return; + expect(r.state.creatures).toHaveLength(0); + expect(r.events.some((e) => e.type === "creatureDestroyed" && e.creatureId === "w1")).toBe(true); + }); + + it("your own creature is safe from your fists, and one in another square is out of reach", () => { + const { state, me } = rig(); + state.creatures[0]!.controllerId = me; + const own = applyCommand(state, me, { type: "punch", targetId: "w1" }); + expect(own.ok).toBe(false); + if (!own.ok) expect(own.error).toMatch(/control/); + const { state: far, me: puncher } = rig(); + far.creatures[0]!.position = emptyNeighborCell(far, far.players.find((p) => p.id === puncher)!.position).cell; + expect(applyCommand(far, puncher, { type: "punch", targetId: "w1" }).ok).toBe(false); + expect(applyCommand(far, puncher, { type: "punch", targetId: "nobody" }).ok).toBe(false); + }); +}); diff --git a/packages/web/src/App.svelte b/packages/web/src/App.svelte index 6fa3f67..b8c60b0 100644 --- a/packages/web/src/App.svelte +++ b/packages/web/src/App.svelte @@ -1356,6 +1356,15 @@ return; } const mine = creature.controllerId === view.you || creature.kind === "democratic-monster"; + if (!mine && !selectedCard && !selectedCreature) { + // No card selected: a creature sharing your square gets the punch a + // wizard there would. Its card is still read from the board's peek. + const me = view.players.find((p) => p.id === view.you); + if (me && cellKey(me.position) === cellKey(creature.position)) { + dispatch({ type: "punch", targetId: creatureId }); + return; + } + } if (mine) { // Commanding your own creature docks its card in the rail instead of // opening the centered peek — a modal would block the very board diff --git a/packages/web/src/net.svelte.ts b/packages/web/src/net.svelte.ts index d984744..044f5ae 100644 --- a/packages/web/src/net.svelte.ts +++ b/packages/web/src/net.svelte.ts @@ -46,7 +46,7 @@ export function humanize(e: GameEvent): string | null { case "extraTurnStarted": return `${e.player} takes an extra turn!`; case "moved": return null; // too chatty for the log case "numberPlayedForMovement": return `${e.player} plays a ${e.value} for movement (${e.newAllowance} total).`; - case "punched": return `${e.attacker} punches ${e.target}!`; + case "punched": return e.kind ? `${e.attacker} punches the ${spellName(e.kind)}!` : `${e.attacker} punches ${e.target}!`; case "spellCast": { const num = e.numberValue ? ` with a ${e.numberValue}` : ""; const amp = e.amplifies ? `, AMPLIFIED${e.amplifies > 1 ? ` ×${2 ** e.amplifies}` : ""},` : "";