Files
gogobee/internal/plugin/expedition_autorun.go
prosolis 41adfce9f1 Expedition autopilot Phase 4 + rogue sneak attack + TwinBee voice sweep
Tedium-removal pass driven by live play feedback. Three big threads:

Expedition autopilot Phase 4 — background auto-run + harvest-until-dry:
- New expeditionAutoRunTicker walks active expeditions every 15min
  (5min tick, per-expedition CAS on new last_autorun_at column). Skips
  combat sessions, briefing/recap quiet windows, expeditions <30min old.
- Walks up to 3 rooms/tick with compact narration; suppresses DMs when
  0 rooms walked or just hitting the per-tick cap (no "stretch complete"
  filler). Player only hears from the bot when a real decision is needed.
- Compact mode: one-line combat narration for trash/elite, auto-resolves
  elite doorways via the forward-sim engine (boss still pauses). Threaded
  via new advanceOnceWithOpts → resolveRoom → resolveCombatRoom.
- Auto-harvest now grinds each Common/Uncommon node until dry (cap at 8
  attempts/visit), mirroring manual !scavenge retries. Rare+ still pauses.
- Ambient ticker: anti-repeat by Kind via new last_ambient_kind column
  (avoids two pack_rat DMs in a row when the pool only has 6 lines).
- !expedition go <n> now routes to the fork-choice handler when active +
  numeric; fork footer rewritten to suggest !expedition go instead of
  !zone go. Boss/Elite doorway: formatNextRoomMessage routes the action
  hint by next room type and autopilot loop breaks via new nextRoomType
  field so "Room X/Y — Boss" doesn't double-print with contradictory
  hints (!zone advance vs !fight).
- runAutopilotWalk extracted from expeditionCmdRun so foreground and
  background share the loop body.

Rogue Sneak Attack actually exists now:
- Audit found the rogue's "Sneak Attack" passive was AutoCritFirst +
  5% damage rider. No Nd6, ever. Phase 2 Monte Carlo masked this
  with a small flat buff; the class's defining mechanic never matched
  its tooltip or 5e identity.
- Added SneakAttackDie int to CombatModifiers, per-hit Nd6 in
  combat_primitives.go (same lane as DivineStrikePerHit). Scales with
  level: 1d6 at L1-2 ... 4d6 at L7-8 ... capped at 10d6 at L19-20.
- AutoCritFirst + 5% rider retained as bonuses on top of working sneak
  attack — preserves the opener-burst feel.
- L7 rogue expected per-hit ~8 → ~22 damage. Valdris fight math goes
  from "16 rounds to kill, die in 8" to winnable.

TwinBee voice sweep (Phase B3):
- ~530 line changes across 24 files replacing third-person "TwinBee
  notes/files/tracks/respects/..." constructions with first-person
  inside flavor string literals. Code identifiers (TwinBeeLine,
  twinBeeLine, etc.), chat-prefix labels (🎭 **TwinBee:**), item names
  (TwinBee's Bell), achievements, and game-title references in
  fun.go (Konami TwinBee ship) left intact.
- Catches a real bug: Valdris fight rendered "TwinBee marks the
  Legendary Resistance" mid-combat in third person, contradicting
  the Phase B2 convention.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 12:40:17 -07:00

198 lines
6.2 KiB
Go

package plugin
import (
"log/slog"
"strings"
"time"
"gogobee/internal/db"
"maunium.net/go/mautrix/id"
)
// Phase 4 of expedition autopilot — background auto-run ticker. Walks
// active expeditions on real time so the player only engages when a real
// decision is needed (fork, elite, boss, low HP/SU, rare node, death).
//
// Design (settled 2026-05-16):
// • Tick every autoRunTickInterval; per-expedition rate-limited by
// autoRunCooldown via a last_autorun_at CAS.
// • Skip when the player is in active combat, inside the briefing/recap
// quiet window, or on a very-fresh expedition (give them a beat).
// • Walk up to autoRunRoomCap rooms per tick. Smaller than foreground's
// cap so background DMs stay digestible.
// • DM-suppression rules:
// - 0 rooms walked → silent (still stuck at a fork / blocked / etc.;
// the player already knows, no point spamming).
// - rooms > 0 with stopOK (hit room cap) → silent; we'll keep walking
// on the next tick. Cadence handles pacing; no "stretch complete"
// footer churn.
// - Any other reason with rooms > 0 → DM the bundled walk.
// • Idempotency: CAS-claim last_autorun_at before doing any work. A
// double-fire on the same expedition is a no-op.
const (
// autoRunTickInterval — how often the ticker wakes. The per-expedition
// cooldown is what actually paces; the tick just has to be frequent
// enough that the cooldown is enforced cleanly.
autoRunTickInterval = 5 * time.Minute
// autoRunCooldown — minimum gap between background walks for one
// expedition. 15 min keeps the dungeon moving while leaving room for
// the player to step in and steer if they want.
autoRunCooldown = 15 * time.Minute
// autoRunMinExpeditionAge — don't auto-walk a brand-new expedition;
// let the player walk the first beat manually so the opening reads
// as deliberate.
autoRunMinExpeditionAge = 30 * time.Minute
// autoRunRoomCap — rooms per background tick. Smaller than the
// foreground cap so a single background DM doesn't dump an
// overwhelming wall of narration.
autoRunRoomCap = 3
)
// expeditionAutoRunTicker — fires the auto-run pass every tick interval.
func (p *AdventurePlugin) expeditionAutoRunTicker() {
ticker := time.NewTicker(autoRunTickInterval)
defer ticker.Stop()
for {
select {
case <-p.stopCh:
return
case <-ticker.C:
p.fireExpeditionAutoRuns(time.Now().UTC())
}
}
}
// fireExpeditionAutoRuns walks active expeditions due for a background
// step and dispatches one walk per. Cheap per-expedition gating happens
// in tryAutoRun; this layer just selects candidates.
func (p *AdventurePlugin) fireExpeditionAutoRuns(now time.Time) {
// Reuse the ambient quiet window — same intent: don't talk over the
// scheduled 06:00 briefing or 21:00 recap.
if inAmbientQuietWindow(now) {
return
}
due, err := loadExpeditionsForAutoRun(
now.Add(-autoRunCooldown), now.Add(-autoRunMinExpeditionAge))
if err != nil {
slog.Error("expedition: load autorun candidates", "err", err)
return
}
for _, expID := range due {
e, err := getExpedition(expID)
if err != nil || e == nil {
continue
}
if e.Status != ExpeditionStatusActive {
continue
}
uid := id.UserID(e.UserID)
if hasActiveCombatSession(uid) {
continue
}
if err := p.tryAutoRun(e, now); err != nil {
slog.Warn("expedition: autorun step", "expedition", e.ID, "err", err)
}
}
}
// loadExpeditionsForAutoRun returns active expeditions whose
// last_autorun_at is NULL or older than runCutoff, AND whose start_date
// is older than ageCutoff so a freshly-started expedition isn't yanked
// out from under the player on tick 1.
func loadExpeditionsForAutoRun(runCutoff, ageCutoff time.Time) ([]string, error) {
rows, err := db.Get().Query(`
SELECT expedition_id
FROM dnd_expedition
WHERE status = 'active'
AND start_date < ?
AND (last_autorun_at IS NULL OR last_autorun_at < ?)`,
ageCutoff, runCutoff)
if err != nil {
return nil, err
}
defer rows.Close()
var ids []string
for rows.Next() {
var s string
if err := rows.Scan(&s); err != nil {
return nil, err
}
ids = append(ids, s)
}
return ids, rows.Err()
}
// tryAutoRun claims the slot for this expedition, runs one background
// walk, and DMs the result if the suppression rules say to. The CAS-
// update is the only persistent side effect on the autorun column —
// supplies/threat/run-graph state are mutated by the walk itself, just
// as they would be in a foreground !expedition run.
func (p *AdventurePlugin) tryAutoRun(e *Expedition, now time.Time) error {
cutoff := now.Add(-autoRunCooldown)
res, err := db.Get().Exec(`
UPDATE dnd_expedition
SET last_autorun_at = ?,
last_activity = ?
WHERE expedition_id = ?
AND status = 'active'
AND (last_autorun_at IS NULL OR last_autorun_at < ?)`,
now, now, e.ID, cutoff)
if err != nil {
return err
}
if n, _ := res.RowsAffected(); n == 0 {
return nil // someone else got there first
}
uid := id.UserID(e.UserID)
r := p.runAutopilotWalk(MessageContext{Sender: uid}, autoRunRoomCap, true)
if r.initErr != "" {
// "no expedition" / "no run" — race with abandon/extract. Silent.
return nil
}
if !shouldDMAutoRun(r) {
return nil
}
body := renderAutoRunDM(r)
if err := p.SendDM(uid, body); err != nil {
slog.Warn("expedition: autorun DM", "user", uid, "err", err)
}
return nil
}
// shouldDMAutoRun applies the background suppression rules. See the
// file-top design block for the rationale.
func shouldDMAutoRun(r autopilotWalkResult) bool {
if r.rooms == 0 {
return false
}
if r.reason == stopOK {
// Hit the per-tick room cap. Next tick will continue; no need to
// post a "stretch complete" filler DM.
return false
}
return true
}
// renderAutoRunDM bundles the staged walk narration into a single DM.
// Background can't pace via streamFlow, so we concatenate phases with a
// blank line between each beat and tack on the final message.
func renderAutoRunDM(r autopilotWalkResult) string {
var b strings.Builder
b.WriteString("🚶 *Auto-walk*\n\n")
for _, s := range r.stream {
if s == "" {
continue
}
b.WriteString(s)
b.WriteString("\n\n")
}
b.WriteString(r.finalMsg)
return b.String()
}