xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / internal / gen / mission.go 87 lines2.6 kB Raw
1package gen
2
3import (
4	"fmt"
5	"math/rand/v2"
6
7	"tinyadventure/internal/model"
8	"tinyadventure/internal/rng"
9)
10
11var mainNames = []string{"The Fall of the %s", "Ashes Under the %s", "Bane of the %s"}
12var mainTemplates = []string{"hunt", "relic", "siege"}
13var sideNames = []string{"A Favor for the Hamlet", "Lost and Found", "Bounty Posted", "The Quiet Errand"}
14
15func kindsFor(sys Systems) []string {
16	if sys.Factions {
17		return []string{"reach", "defeat"}
18	}
19	return []string{"reach"}
20}
21
22type questBuilder struct {
23	r        *rand.Rand
24	sys      Systems
25	factions map[string]model.Faction
26	names    []string
27	keys     []model.Item
28	n        int
29}
30
31func (q *questBuilder) step(id string, kind string, poi model.POI) model.MissionStep {
32	f := q.factions[poi.Faction]
33	s := model.MissionStep{ID: id, Kind: kind, POI: poi.ID, Slots: model.Slots{POIKind: poi.Kind, Faction: f.ID}}
34	switch kind {
35	case "defeat":
36		s.Text = fmt.Sprintf("Drive the %s out of %s", f.Name, poi.Name)
37	case "retrieve":
38		q.n++
39		it := makeKeyItem(q.n, q.names[q.n%len(q.names)])
40		q.keys = append(q.keys, it)
41		s.Slots.Item = it.ID
42		s.Text = fmt.Sprintf("Recover the %s from %s", it.Name, poi.Name)
43	default:
44		s.Text = fmt.Sprintf("Travel to %s", poi.Name)
45	}
46	return s
47}
48
49func (q *questBuilder) main(pois []model.POI, boss string) model.Mission {
50	last := pois[len(pois)-1]
51	steps := make([]model.MissionStep, 0, len(pois))
52	for i, p := range pois[:len(pois)-1] {
53		kind := "reach"
54		if i > 0 {
55			kind = "retrieve"
56		}
57		steps = append(steps, q.step(fmt.Sprintf("m%d", i+1), kind, p))
58	}
59	bs := q.step(fmt.Sprintf("m%d", len(pois)), "boss", last)
60	bs.Text = fmt.Sprintf("Slay %s in %s", boss, last.Name)
61	steps = append(steps, bs)
62	ti := q.r.IntN(len(mainTemplates))
63	return model.Mission{
64		Template: mainTemplates[ti],
65		Name:     fmt.Sprintf(mainNames[ti], q.factions[last.Faction].Name),
66		Steps:    steps,
67	}
68}
69
70func (q *questBuilder) side(i int, pois []model.POI) model.Mission {
71	kinds := kindsFor(q.sys)
72	steps := make([]model.MissionStep, len(pois))
73	for j, p := range pois {
74		steps[j] = q.step(fmt.Sprintf("s%d_%d", i+1, j+1), rng.Pick(q.r, kinds), p)
75	}
76	return model.Mission{Template: "side", Name: sideNames[i%len(sideNames)], Steps: steps}
77}
78
79var keyNames = []string{"Sunken Crown", "Bone Key", "Moonstone Sigil", "Ember Seal", "Gilded Chalice", "Black Lantern", "Serpent Idol", "Iron Reliquary", "Glass Eye", "Thorn Circlet"}
80
81func makeKeyItem(n int, name string) model.Item {
82	return model.Item{ID: fmt.Sprintf("key%d", n), Name: name, Rarity: "rare", Key: true}
83}
84
85func rollKeyNames(r *rand.Rand) []string {
86	return rng.Shuffled(r, keyNames)
87}