xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / internal / server / play_test.go 451 lines12.5 kB Raw
1package server
2
3import (
4	"slices"
5	"testing"
6	"time"
7
8	"tinyadventure/internal/model"
9	"tinyadventure/internal/sim"
10	"tinyadventure/internal/world"
11)
12
13func openGame(w, h int) *game {
14	hexes := make([]byte, 0, w*h*model.HexBytes)
15	for range w * h {
16		hexes = append(hexes, model.ShapePlain, 3, 1, 0)
17	}
18	return &game{state: model.GameState{Map: model.Map{Width: w, Height: h, Hexes: hexes}}}
19}
20
21func TestMovePicksUpLootOnTheWay(t *testing.T) {
22	g := openGame(5, 1)
23	g.state.Map.Loot = []model.LootNode{{ID: "l1", X: 2, Y: 0, Item: model.Item{ID: "i1", Name: "Sword"}}}
24	if err := g.move(4, 0, 0); err != nil {
25		t.Fatal(err)
26	}
27	if len(g.state.Party.Inventory) != 1 || len(g.state.Found) != 1 || !g.state.Map.Loot[0].Collected || g.state.Summary.Loot != 1 {
28		t.Fatalf("loot not picked up: %+v", g.state)
29	}
30	g.begin()
31	if g.state.Found != nil {
32		t.Fatal("found must reset on the next action")
33	}
34}
35
36func TestRouteAvoidsOtherUncleared(t *testing.T) {
37	g := openGame(5, 3)
38	g.state.Party.Y = 1
39	g.state.Map.POIs = []model.POI{{ID: "p1", X: 2, Y: 1}}
40	steps, err := g.path(4, 1)
41	if err != nil {
42		t.Fatal(err)
43	}
44	for _, s := range steps {
45		if s == [2]int{2, 1} {
46			t.Fatal("route crosses an uncleared POI")
47		}
48	}
49	steps, _ = g.path(2, 1)
50	if steps[len(steps)-1] != [2]int{2, 1} {
51		t.Fatal("route must still reach the POI itself")
52	}
53}
54
55func TestRouteFallsBackThroughPOIInCorridor(t *testing.T) {
56	g := openGame(5, 1)
57	g.state.Map.POIs = []model.POI{{ID: "p1", X: 2, Y: 0}}
58	if _, err := g.path(4, 0); err != nil {
59		t.Fatal(err)
60	}
61}
62
63func TestDefeatEndsTheCampaign(t *testing.T) {
64	g := openGame(5, 1)
65	g.state.Party = party("mage")
66	g.state.Party.Units[0].Stats = model.Stats{HP: 1, MaxHP: 1, Attack: 1}
67	g.state.Map.POIs = []model.POI{{ID: "p1", X: 1, Y: 0}}
68	g.encs = map[string]model.Encounter{"p1": {ID: "e1", POI: "p1", Enemies: []model.Unit{{ID: "x", Name: "Ogre", Level: 5, Stats: model.Stats{HP: 500, MaxHP: 500, Attack: 50}, Abilities: sim.Abilities("strike")}}}}
69	g.state.Clock = newClock()
70	g.state.Campaign.Main.Steps = []model.MissionStep{{ID: "s1", POI: "p0"}}
71	if err := g.interact("p1"); err != nil {
72		t.Fatal(err)
73	}
74	if g.state.Phase != "defeat" || !g.finished() {
75		t.Fatalf("phase %s finished %v", g.state.Phase, g.finished())
76	}
77}
78
79func TestCampOnlyAtNightOncePerDay(t *testing.T) {
80	g := openGame(200, 1)
81	g.state.Party = party("fighter")
82	g.state.Party.Units[0].Stats.HP = 10
83	g.state.Clock = newClock()
84	if g.rest() == nil {
85		t.Fatal("made camp in daylight")
86	}
87	if err := g.move(75, 0, 0); err != nil || !g.state.Clock.Night() || len(g.state.Notes) == 0 {
88		t.Fatalf("clock %+v notes %v err %v", g.state.Clock, g.state.Notes, err)
89	}
90	if err := g.rest(); err != nil {
91		t.Fatal(err)
92	}
93	c := g.state.Clock
94	if c.Day != 2 || c.Time != 0 || g.state.Party.Units[0].Stats.HP != 100 {
95		t.Fatalf("after camp %+v hp %d", c, g.state.Party.Units[0].Stats.HP)
96	}
97	if g.rest() == nil {
98		t.Fatal("made camp twice")
99	}
100	g.move(199, 0, 0)
101	if g.state.Clock.Day != 3 {
102		t.Fatalf("a night without camp must still end the day: %+v", g.state.Clock)
103	}
104}
105
106func equipAll(g *game) {
107	score := func(it model.Item) int {
108		b := it.Bonus
109		return b.MaxHP/3 + b.Attack + b.Defense + b.Magic + b.Speed/2
110	}
111	for _, u := range g.state.Party.Units {
112		for _, it := range g.state.Party.Inventory {
113			if it.Slot == "" {
114				continue
115			}
116			if old := u.Gear[it.Slot]; old == nil || score(*old) < score(it) {
117				g.equip(model.Action{Unit: u.ID, Item: it.ID})
118				break
119			}
120		}
121	}
122}
123
124func TestCarefulPlayWinsMostCampaigns(t *testing.T) {
125	wins, total := 0, 30
126	for seed := model.Seed(1); seed <= model.Seed(total); seed++ {
127		if careful(newGame("g", seed, time.Unix(0, 0))) {
128			wins++
129		}
130	}
131	if wins*100/total < 70 {
132		t.Fatalf("careful play won %d of %d", wins, total)
133	}
134}
135
136const safeOdds = 85
137
138func careful(g *game) bool {
139	for moves := 0; moves < 300 && !g.finished(); moves++ {
140		equipAll(g)
141		g.recover()
142		id, x, y := g.target()
143		if id == "" {
144			g.waitForCamp()
145			if id, x, y = g.target(); id == "" {
146				return false
147			}
148		}
149		g.move(x, y, 0)
150		if g.threat == "" {
151			continue
152		}
153		if g.threatView().Odds < safeOdds {
154			g.waitForCamp()
155			continue
156		}
157		g.interact(g.threat)
158	}
159	return g.state.Phase == "victory"
160}
161
162func (g *game) nextMain() *model.POI {
163	for _, s := range g.state.Campaign.Main.Steps {
164		if !s.Done {
165			return g.poi(s.POI)
166		}
167	}
168	return nil
169}
170
171func (g *game) odds(id string) int {
172	keep := g.threat
173	g.threat = id
174	odds := g.threatView().Odds
175	g.threat = keep
176	return odds
177}
178
179func (g *game) target() (string, int, int) {
180	if p := g.nextMain(); g.bossGate(p) && g.odds(p.ID) >= safeOdds {
181		return p.ID, p.X, p.Y
182	}
183	best, bx, by, bo := "", 0, 0, safeOdds-1
184	for _, p := range g.state.Map.POIs {
185		if !p.Cleared && g.bossGate(&p) {
186			if o := g.odds(p.ID); o > bo {
187				best, bx, by, bo = p.ID, p.X, p.Y, o
188			}
189		}
190	}
191	for _, b := range g.state.Map.Bands {
192		if !b.Cleared {
193			if o := g.odds(b.ID); o > bo {
194				best, bx, by, bo = b.ID, b.X, b.Y, o
195			}
196		}
197	}
198	return best, bx, by
199}
200
201func (g *game) waitForCamp() error {
202	if g.blockerNear() || g.contact() != "" {
203		px, py := g.state.Party.X, g.state.Party.Y
204		for y := py - 3; y <= py+3; y++ {
205			for x := px - 3; x <= px+3; x++ {
206				if d := world.Distance(x, y, px, py); d >= 2 && d <= 3 && g.state.Map.In(x, y) && g.state.Map.Walkable(x, y) && g.blocker(x, y) == "" && g.calm(x, y) {
207					g.move(x, y, 0)
208					y = py + 4
209					break
210				}
211			}
212		}
213	}
214	if !g.state.Clock.Night() {
215		g.pass(g.state.Clock.DayLength - g.state.Clock.Time)
216	}
217	return g.rest()
218}
219
220func (g *game) calm(x, y int) bool {
221	for _, n := range [][2]int{{x, y}, {x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}, {x + 1, y + 1}, {x - 1, y - 1}, {x + 1, y - 1}, {x - 1, y + 1}} {
222		if g.blocker(n[0], n[1]) != "" {
223			return false
224		}
225	}
226	return true
227}
228
229func TestPOILevelsGrowTowardTheBoss(t *testing.T) {
230	g := newGame("g", 5, time.Unix(0, 0))
231	steps := g.state.Campaign.Main.Steps
232	first, last := g.poi(steps[0].POI), g.poi(steps[len(steps)-1].POI)
233	if first.Level < 1 || last.Level <= first.Level {
234		t.Fatalf("levels %d -> %d", first.Level, last.Level)
235	}
236}
237
238func (g *game) engage(p *model.POI) error {
239	for range 12 {
240		if err := g.move(p.X, p.Y, 0); err != nil {
241			return err
242		}
243		id := g.threat
244		if id == "" {
245			return nil
246		}
247		if err := g.interact(id); err != nil {
248			return err
249		}
250		if id == p.ID || g.state.Phase == "defeat" {
251			return nil
252		}
253		g.recover()
254	}
255	return nil
256}
257
258func (g *game) hurt() bool {
259	for _, u := range g.state.Party.Units {
260		if u.Stats.HP*2 < sim.Effective(u).MaxHP {
261			return true
262		}
263	}
264	return false
265}
266
267func (g *game) recover() {
268	if g.finished() || !g.hurt() {
269		return
270	}
271	if g.state.Clock.Night() && g.rest() == nil {
272		return
273	}
274	for _, u := range g.state.Party.Units {
275		if u.Stats.HP*2 < sim.Effective(u).MaxHP {
276			g.drink(u.ID)
277		}
278	}
279}
280
281func TestThreatStopsBeforePOIAndOffersOdds(t *testing.T) {
282	g := newGame("g", 7, time.Unix(0, 0))
283	p := g.poi(g.state.Campaign.Main.Steps[0].POI)
284	if err := g.move(p.X, p.Y, 0); err != nil {
285		t.Fatal(err)
286	}
287	st := g.snapshot()
288	if st.Threat == nil || st.Threat.POI != p.ID || len(st.Threat.Enemies) == 0 {
289		t.Fatalf("threat %+v", st.Threat)
290	}
291	if world.Distance(st.Party.X, st.Party.Y, p.X, p.Y) != 1 || st.Summary.Battles != 0 {
292		t.Fatalf("party at %d,%d battles %d", st.Party.X, st.Party.Y, st.Summary.Battles)
293	}
294	if st.Threat.Odds < 50 || st.Threat.Odds > 100 || st.Threat.Wounds <= 0 || st.Threat.Wounds > 100 {
295		t.Fatalf("first fight odds %d wounds %d", st.Threat.Odds, st.Threat.Wounds)
296	}
297	if err := g.interact(p.ID); err != nil {
298		t.Fatal(err)
299	}
300	if st := g.snapshot(); st.Threat != nil || st.Summary.Battles != 1 {
301		t.Fatalf("after engage threat=%v battles=%d", st.Threat, st.Summary.Battles)
302	}
303}
304
305func TestBarredBossShowsAsBarred(t *testing.T) {
306	g := newGame("g", 7, time.Unix(0, 0))
307	g.state.Map.Bands = nil
308	steps := g.state.Campaign.Main.Steps
309	boss := g.poi(steps[len(steps)-1].POI)
310	if err := g.move(boss.X, boss.Y, 0); err != nil {
311		t.Fatal(err)
312	}
313	if st := g.snapshot(); st.Threat == nil || !st.Threat.Barred {
314		t.Fatalf("threat %+v", st.Threat)
315	}
316	if err := g.interact(boss.ID); err == nil {
317		t.Fatal("barred boss accepted a fight")
318	}
319}
320
321func TestHPCarriesBetweenFights(t *testing.T) {
322	g := newGame("g", 7, time.Unix(0, 0))
323	full := g.state.Party.Units[0].Stats.HP
324	if full != sim.Effective(g.state.Party.Units[0]).MaxHP {
325		t.Fatalf("not at full health at start: %d", full)
326	}
327	p := g.poi(g.state.Campaign.Main.Steps[0].POI)
328	if err := g.move(p.X, p.Y, 0); err != nil {
329		t.Fatal(err)
330	}
331	before := g.state.Clock.Time
332	if err := g.interact(p.ID); err != nil {
333		t.Fatal(err)
334	}
335	if g.state.Clock.Time != before+fightTime {
336		t.Fatalf("a fight must spend time: %d -> %d", before, g.state.Clock.Time)
337	}
338	total := 0
339	for _, u := range g.state.Party.Units {
340		total += sim.Effective(u).MaxHP - u.Stats.HP
341	}
342	if total == 0 {
343		t.Skip("party took no damage")
344	}
345	g.move(p.X+1, p.Y, 0)
346	for _, u := range g.state.Party.Units {
347		if u.Stats.HP > sim.Effective(u).MaxHP {
348			t.Fatal("hp above max")
349		}
350	}
351}
352
353func TestShopTradeAndPotions(t *testing.T) {
354	g := newGame("g", 7, time.Unix(0, 0))
355	g.state.Party.Gold = 1000
356	st := g.snapshot()
357	if st.Shop == nil || len(st.Shop.Stock) != stockSize {
358		t.Fatalf("shop %+v", st.Shop)
359	}
360	it := st.Shop.Stock[0]
361	if it.Value <= 0 {
362		t.Fatalf("stock item has no value: %+v", it)
363	}
364	if err := g.buy(it.ID); err != nil {
365		t.Fatal(err)
366	}
367	if g.state.Party.Gold != 1000-it.Value || len(g.shopView().Stock) != stockSize-1 {
368		t.Fatalf("gold %d stock %d", g.state.Party.Gold, len(g.shopView().Stock))
369	}
370	if err := g.sell(it.ID); err != nil {
371		t.Fatal(err)
372	}
373	if g.state.Party.Gold != 1000-it.Value+SellPrice(it) {
374		t.Fatalf("gold after sell %d", g.state.Party.Gold)
375	}
376	if err := g.buy("potion"); err != nil || g.state.Party.Potions != 1 {
377		t.Fatalf("potion %v %d", err, g.state.Party.Potions)
378	}
379	u := &g.state.Party.Units[0]
380	if err := g.drink(u.ID); err == nil {
381		t.Fatal("drank at full health")
382	}
383	u.Stats.HP = 1
384	if err := g.drink(u.ID); err != nil || u.Stats.HP <= 1 || g.state.Party.Potions != 0 {
385		t.Fatalf("drink %v hp %d potions %d", err, u.Stats.HP, g.state.Party.Potions)
386	}
387	g.state.Party.Gold = 0
388	if err := g.buy("potion"); err == nil {
389		t.Fatal("bought a potion with no gold")
390	}
391	g.state.Party.X++
392	if g.snapshot().Shop != nil || g.buy("potion") == nil {
393		t.Fatal("trade away from camp")
394	}
395}
396
397func TestMoveStepsLimitsTheWalk(t *testing.T) {
398	g := openGame(5, 1)
399	if err := g.move(4, 0, 1); err != nil || g.state.Party.X != 1 {
400		t.Fatalf("one step should reach x=1, at %d err %v", g.state.Party.X, err)
401	}
402	if err := g.move(4, 0, 0); err != nil || g.state.Party.X != 4 {
403		t.Fatalf("full walk should reach x=4, at %d", g.state.Party.X)
404	}
405}
406
407func TestKeysGoToTheKeyRingAndOpenTheBoss(t *testing.T) {
408	g := openGame(6, 1)
409	g.state.Party = party("fighter")
410	g.state.Map.POIs = []model.POI{{ID: "p1", X: 2, Y: 0, Cleared: true}, {ID: "boss", X: 5, Y: 0}}
411	g.state.Map.Loot = []model.LootNode{{ID: "loot-key1", X: 2, Y: 0, Item: model.Item{ID: "key1", Name: "Bone Key", Key: true}}}
412	g.state.Campaign.Main.Steps = []model.MissionStep{{ID: "m1", POI: "p1", Slots: model.Slots{Item: "key1"}}, {ID: "m2", POI: "boss"}}
413	g.encs = map[string]model.Encounter{"boss": {ID: "e", POI: "boss"}}
414	g.threat = "boss"
415	if v := g.threatView(); !v.Barred || !slices.Equal(v.Needs, []string{"Bone Key"}) {
416		t.Fatalf("barred %v needs %v", v.Barred, v.Needs)
417	}
418	g.move(3, 0, 0)
419	g.advance()
420	if len(g.state.Party.Keys) != 1 || len(g.state.Party.Inventory) != 0 || !g.state.Campaign.Main.Steps[0].Done {
421		t.Fatalf("keys %v inventory %v", g.state.Party.Keys, g.state.Party.Inventory)
422	}
423	g.threat = "boss"
424	if v := g.threatView(); v.Barred || len(v.Needs) != 0 {
425		t.Fatalf("boss still barred: %+v", v)
426	}
427}
428
429func TestFallenHeroesWaitForAPotionOrCamp(t *testing.T) {
430	g := newGame("g", 7, time.Unix(0, 0))
431	down := &g.state.Party.Units[1]
432	down.Stats.HP = 0
433	xp := down.XP
434	p := g.poi(g.state.Campaign.Main.Steps[0].POI)
435	g.move(p.X, p.Y, 0)
436	if err := g.interact(p.ID); err != nil || g.state.Phase != "explore" {
437		t.Fatalf("fight: %v %s", err, g.state.Phase)
438	}
439	if down.Stats.HP != 0 || down.XP != xp || slices.ContainsFunc(g.state.Log, func(e model.CombatEvent) bool { return e.Actor == down.ID }) {
440		t.Fatalf("fallen hero fought or rose: hp %d xp %d", down.Stats.HP, down.XP)
441	}
442	g.state.Party.Potions = 1
443	if err := g.drink(down.ID); err != nil || down.Stats.HP != sim.Effective(*down).MaxHP/2 {
444		t.Fatalf("potion revive: %v hp %d", err, down.Stats.HP)
445	}
446	down.Stats.HP = 0
447	g.pass(g.state.Clock.DayLength - g.state.Clock.Time)
448	if err := g.rest(); err != nil || down.Fallen() {
449		t.Fatalf("camp revive: %v hp %d", err, down.Stats.HP)
450	}
451}