xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / internal / server / shop.go 169 lines3.7 kB Raw
1package server
2
3import (
4	"errors"
5	"hash/fnv"
6	"slices"
7
8	"tinyadventure/internal/model"
9	"tinyadventure/internal/rng"
10	"tinyadventure/internal/sim"
11)
12
13const (
14	PotionPrice = 15
15	stockSize   = 3
16)
17
18type site struct {
19	id, name string
20	x, y     int
21	level    int
22}
23
24func (g *game) shops() []site {
25	var out []site
26	for _, s := range g.state.Map.Spawns {
27		out = append(out, site{s.ID, s.Kind, s.X, s.Y, 1})
28	}
29	for _, p := range g.state.Map.POIs {
30		if p.Cleared && (p.Kind == "camp" || p.Kind == "village") {
31			out = append(out, site{p.ID, p.Name, p.X, p.Y, p.Level})
32		}
33	}
34	return out
35}
36
37func (g *game) shopAt(x, y int) *site {
38	for _, s := range g.shops() {
39		if s.x == x && s.y == y {
40			return &s
41		}
42	}
43	return nil
44}
45
46func (g *game) stockAt(s *site) []model.Item {
47	if g.stock == nil {
48		g.stock = map[string][]model.Item{}
49	}
50	if items, ok := g.stock[s.id]; ok {
51		return items
52	}
53	h := fnv.New64a()
54	h.Write([]byte(s.id))
55	r := rng.New(g.state.Seed ^ model.Seed(h.Sum64()))
56	items := make([]model.Item, stockSize)
57	for i := range items {
58		items[i] = sim.RollItem(r, s.level+1)
59	}
60	g.stock[s.id] = items
61	return items
62}
63
64func (g *game) shopView() *model.Shop {
65	s := g.shopAt(g.state.Party.X, g.state.Party.Y)
66	if s == nil {
67		return nil
68	}
69	return &model.Shop{ID: s.id, Name: s.name, Stock: slices.Clone(g.stockAt(s)), PotionPrice: PotionPrice}
70}
71
72func (g *game) atShop() (*site, error) {
73	s := g.shopAt(g.state.Party.X, g.state.Party.Y)
74	if s == nil {
75		return nil, errors.New("nobody trades here: find a spawn, or a camp or village you have cleared")
76	}
77	g.begin()
78	return s, nil
79}
80
81func (g *game) heal(revive bool) {
82	for i := range g.state.Party.Units {
83		if u := &g.state.Party.Units[i]; revive || !u.Fallen() {
84			u.Stats.HP = sim.Effective(*u).MaxHP
85		}
86	}
87}
88
89func (g *game) buy(id string) error {
90	s, err := g.atShop()
91	if err != nil {
92		return err
93	}
94	p := &g.state.Party
95	if id == "potion" {
96		if p.Gold < PotionPrice {
97			return errors.New("not enough gold for a potion")
98		}
99		p.Gold -= PotionPrice
100		p.Potions++
101		return nil
102	}
103	stock := g.stockAt(s)
104	i := slices.IndexFunc(stock, func(it model.Item) bool { return it.ID == id })
105	if i < 0 {
106		return errors.New("item is not for sale here")
107	}
108	if p.Gold < stock[i].Value {
109		return errors.New("not enough gold for " + stock[i].Name)
110	}
111	p.Gold -= stock[i].Value
112	p.Inventory = append(p.Inventory, stock[i])
113	g.stock[s.id] = slices.Delete(stock, i, i+1)
114	return nil
115}
116
117func (g *game) sell(id string) error {
118	if _, err := g.atShop(); err != nil {
119		return err
120	}
121	p := &g.state.Party
122	i := slices.IndexFunc(p.Inventory, func(it model.Item) bool { return it.ID == id })
123	if i < 0 {
124		return errors.New("item not in inventory")
125	}
126	p.Gold += SellPrice(p.Inventory[i])
127	p.Inventory = slices.Delete(p.Inventory, i, i+1)
128	return nil
129}
130
131func SellPrice(it model.Item) int { return max(1, it.Value/2) }
132
133func (g *game) drink(unit string) error {
134	u := g.unit(unit)
135	if u == nil {
136		return errors.New("unknown unit")
137	}
138	if g.state.Party.Potions == 0 {
139		return errors.New("no potions left: buy them at a spawn, camp or village")
140	}
141	full := sim.Effective(*u).MaxHP
142	if u.Stats.HP >= full {
143		return errors.New(u.Name + " is already at full health")
144	}
145	if u.Fallen() {
146		g.note(u.Name + " is back on their feet")
147	}
148	g.state.Party.Potions--
149	g.state.Summary.Potions++
150	u.Stats.HP = min(full, u.Stats.HP+full/2)
151	return nil
152}
153
154func (g *game) pickup(x, y int) {
155	if p := g.poiAt(x, y); (p != nil && !p.Cleared) || g.bandAt(x, y) != nil {
156		return
157	}
158	for i := range g.state.Map.Loot {
159		l := &g.state.Map.Loot[i]
160		if l.X == x && l.Y == y && !l.Collected {
161			if l.Trap {
162				g.spring()
163			}
164			l.Collected = true
165			g.take(l.Item)
166			g.state.Found = append(g.state.Found, l.Item)
167		}
168	}
169}