xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / internal / server / hazards.go 56 lines1.2 kB Raw
1package server
2
3import (
4	"fmt"
5	"maps"
6	"slices"
7
8	"tinyadventure/internal/model"
9	"tinyadventure/internal/sim"
10)
11
12const hazardPC = 2
13
14var hazardBiomes = map[byte]string{2: sim.Frost, 5: sim.Poison, 6: sim.Fire}
15
16func hazardOf(m *model.Map, x, y int) (string, string) {
17	h := m.Hex(x, y)
18	switch {
19	case h[3]&model.FlagRoad != 0:
20		return "", ""
21	case h[0] == model.ShapeShallow:
22		return "Wading", sim.Frost
23	case h[0] == model.ShapeMountain:
24		return "The climb", sim.Physical
25	}
26	if typ, ok := hazardBiomes[h[1]]; ok {
27		return model.Biomes[h[1]], typ
28	}
29	return "", ""
30}
31
32func endure(m *model.Map, units []model.Unit, x, y int, lost map[string]int) {
33	name, typ := hazardOf(m, x, y)
34	if name == "" {
35		return
36	}
37	for i := range units {
38		u := &units[i]
39		if typ != sim.Physical && sim.Guards(*u, typ) {
40			continue
41		}
42		hit := min(u.Stats.HP-1, max(1, sim.Effective(*u).MaxHP*hazardPC/100))
43		u.Stats.HP -= max(0, hit)
44		lost[name] += max(0, hit)
45	}
46}
47
48func (g *game) noteHazards(lost map[string]int) {
49	for _, name := range slices.Sorted(maps.Keys(lost)) {
50		if lost[name] > 0 {
51			g.note(fmt.Sprintf("%s costs the party %d hp", name, lost[name]))
52		}
53	}
54}
55
56func (g *game) note(text string) { g.state.Notes = append(g.state.Notes, text) }