|
| 1 | +package idlerpg |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +// Monster encounters are the PvE heart of the D&D layer — and, unlike the level-up |
| 9 | +// PvP battle, they work with a single player. On some ticks a wandering idler runs |
| 10 | +// into a level-scaled monster and a quick d20 fight resolves: attacker rolls |
| 11 | +// d20 + attack vs the defender's AC, hits deal damage, repeat until one drops. |
| 12 | +// Win → time toward the next level, gold, a kill, maybe loot. Lose → bloodied or |
| 13 | +// downed. |
| 14 | + |
| 15 | +const monsterOdds = 5 // ~1-in-N chance a monster appears for a random idler each tick |
| 16 | + |
| 17 | +// monster is one bestiary entry. |
| 18 | +type monster struct { |
| 19 | + Name string |
| 20 | + MinLvl int64 // first level at which it can appear |
| 21 | + AC int64 // armor class (target number to hit it) |
| 22 | + Atk int64 // its attack bonus |
| 23 | + DmgDie int64 // its damage die (dN) |
| 24 | + HP int64 |
| 25 | + Gold int64 // reward on a kill |
| 26 | +} |
| 27 | + |
| 28 | +// bestiary, weakest to nastiest. |
| 29 | +var bestiary = []monster{ |
| 30 | + {"a giant rat", 0, 10, 0, 4, 4, 1}, |
| 31 | + {"a goblin", 1, 12, 2, 6, 7, 3}, |
| 32 | + {"a kobold warren-scout", 2, 12, 3, 4, 6, 4}, |
| 33 | + {"an orc", 4, 13, 4, 8, 16, 8}, |
| 34 | + {"a gnoll pack-hunter", 6, 14, 4, 8, 24, 12}, |
| 35 | + {"an ogre", 9, 11, 5, 10, 38, 22}, |
| 36 | + {"a wyvern", 13, 15, 6, 8, 55, 45}, |
| 37 | + {"a young dragon", 18, 17, 7, 12, 85, 110}, |
| 38 | +} |
| 39 | + |
| 40 | +// pickMonster chooses a level-appropriate foe. |
| 41 | +func (m *Manager) pickMonster(level int64) monster { |
| 42 | + var eligible []monster |
| 43 | + for _, mon := range bestiary { |
| 44 | + if mon.MinLvl <= level { |
| 45 | + eligible = append(eligible, mon) |
| 46 | + } |
| 47 | + } |
| 48 | + if len(eligible) == 0 { |
| 49 | + return bestiary[0] |
| 50 | + } |
| 51 | + return eligible[m.roll(len(eligible))] |
| 52 | +} |
| 53 | + |
| 54 | +// maybeMonster occasionally throws a monster at a random online idler. |
| 55 | +func (m *Manager) maybeMonster(ctx context.Context) { |
| 56 | + if m.roll(monsterOdds) != 0 { |
| 57 | + return |
| 58 | + } |
| 59 | + if p, ok := m.randomOnline(); ok { |
| 60 | + m.fightMonster(ctx, p) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// fightMonster picks a scaled foe and resolves an encounter for p. |
| 65 | +func (m *Manager) fightMonster(ctx context.Context, p player) { |
| 66 | + sheet, _ := m.store.HGetAll(ctx, sheetKey(p.key)) |
| 67 | + class, _ := m.store.GetStr(ctx, classKey(p.key)) |
| 68 | + if isDowned(sheet, class) { |
| 69 | + return // already down — can't fight |
| 70 | + } |
| 71 | + m.resolveFight(ctx, p, sheet, class, m.pickMonster(sheet["level"])) |
| 72 | +} |
| 73 | + |
| 74 | +// resolveFight runs the round-by-round combat and applies the outcome. |
| 75 | +func (m *Manager) resolveFight(ctx context.Context, p player, sheet map[string]int64, class string, mon monster) { |
| 76 | + startHP := curHP(sheet, class) |
| 77 | + pHP := startHP |
| 78 | + pAC := 10 + abilityMod(sheet["dex"]) |
| 79 | + pAtk := 2 + sheet["level"]/4 + classAttackMod(sheet, class) |
| 80 | + pDmgBonus := classAttackMod(sheet, class) |
| 81 | + if pDmgBonus < 0 { |
| 82 | + pDmgBonus = 0 |
| 83 | + } |
| 84 | + monHP := mon.HP |
| 85 | + |
| 86 | + for round := 0; round < 30 && pHP > 0 && monHP > 0; round++ { |
| 87 | + if int64(m.roll(20)+1)+pAtk >= mon.AC { // player swings (weapon d8) |
| 88 | + monHP -= int64(m.roll(8)+1) + pDmgBonus |
| 89 | + } |
| 90 | + if monHP <= 0 { |
| 91 | + break |
| 92 | + } |
| 93 | + if int64(m.roll(20)+1)+mon.Atk >= pAC { // monster swings |
| 94 | + pHP -= int64(m.roll(int(mon.DmgDie)) + 1) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if taken := startHP - pHP; taken > 0 { |
| 99 | + m.damage(ctx, p.key, taken) |
| 100 | + } |
| 101 | + |
| 102 | + if monHP <= 0 { |
| 103 | + reward := m.pctOfTTL(ctx, p.key, 8, 14) |
| 104 | + _, _ = m.store.HIncr(ctx, sheetKey(p.key), "ttl", -reward) |
| 105 | + _, _ = m.store.HIncr(ctx, sheetKey(p.key), "gold", mon.Gold) |
| 106 | + _, _ = m.store.HIncr(ctx, sheetKey(p.key), "kills", 1) |
| 107 | + m.out.Say(p.network, p.channel, fmt.Sprintf( |
| 108 | + "⚔️ %s slew %s! +%dg, %ds closer to the next level.", p.nick, mon.Name, mon.Gold, reward)) |
| 109 | + if m.roll(3) == 0 { |
| 110 | + m.findItem(ctx, p, sheet["level"]) |
| 111 | + } |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + if pHP <= 0 { |
| 116 | + m.out.Say(p.network, p.channel, fmt.Sprintf( |
| 117 | + "💀 %s was felled by %s and left for dead — they must recover before pressing on.", p.nick, mon.Name)) |
| 118 | + return |
| 119 | + } |
| 120 | + m.out.Say(p.network, p.channel, fmt.Sprintf( |
| 121 | + "🩸 %s fled %s, bloodied but alive.", p.nick, mon.Name)) |
| 122 | +} |
0 commit comments