@@ -57,22 +57,46 @@ func TestRouteFallsBackThroughPOIInCorridor(t *testing.T) {
5757 }
5858 }
5959
60-func TestDefeatFallsBackToNearestCamp(t *testing.T) {
61- g := openGame(30, 3)
62- g.spawn = model.SpawnPoint{X: 0, Y: 0}
63- g.state.Map.Spawns = []model.SpawnPoint{g.spawn}
64- g.state.Map.POIs = []model.POI{{ID: "p1", X: 20, Y: 0, Cleared: true}, {ID: "p2", X: 25, Y: 0}}
65- if x, _ := g.camp(g.state.Map.POIs[1].X, g.state.Map.POIs[1].Y); x != 20 {
66- t.Fatalf("cleared place is a camp, got %d", x)
67- }
68- g.state.Map.POIs[0].Cleared = false
69- if x, _ := g.camp(g.state.Map.POIs[1].X, g.state.Map.POIs[1].Y); x != 0 {
70- t.Fatalf("uncleared place is no camp, got %d", x)
71- }
72- g.state.Party.X, g.state.Party.Y = 20, 0
73- g.state.Map.POIs[0].Cleared = true
74- if c := g.campView(); c == nil || c.Trade || g.buy("potion") == nil {
75- t.Fatalf("a cleared ruin offers rest but no trade: %+v", c)
60+func TestDefeatEndsTheCampaign(t *testing.T) {
61+ g := openGame(5, 1)
62+ g.state.Party = party("mage")
63+ g.state.Party.Units[0].Stats = model.Stats{HP: 1, MaxHP: 1, Attack: 1}
64+ g.state.Map.POIs = []model.POI{{ID: "p1", X: 1, Y: 0}}
65+ 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")}}}}
66+ g.state.Clock = newClock()
67+ g.state.Campaign.Main.Steps = []model.MissionStep{{ID: "s1", POI: "p0"}}
68+ if err := g.interact("p1"); err != nil {
69+ t.Fatal(err)
70+ }
71+ if g.state.Phase != "defeat" || !g.finished() || g.move(4, 0) == nil {
72+ t.Fatalf("phase %s finished %v", g.state.Phase, g.finished())
73+ }
74+}
75+
76+func TestCampOnlyAtNightOncePerDay(t *testing.T) {
77+ g := openGame(80, 1)
78+ g.state.Party = party("fighter")
79+ g.state.Party.Units[0].Stats.HP = 10
80+ g.state.Clock = newClock()
81+ if g.rest() == nil {
82+ t.Fatal("made camp in daylight")
83+ }
84+ if err := g.move(31, 0); err != nil || !g.state.Clock.Night() || len(g.state.Notes) == 0 {
85+ t.Fatalf("clock %+v notes %v err %v", g.state.Clock, g.state.Notes, err)
86+ }
87+ if err := g.rest(); err != nil {
88+ t.Fatal(err)
89+ }
90+ c := g.state.Clock
91+ if c.Day != 2 || c.Time != 0 || g.state.Party.Units[0].Stats.HP != 100 {
92+ t.Fatalf("after camp %+v hp %d", c, g.state.Party.Units[0].Stats.HP)
93+ }
94+ if g.rest() == nil {
95+ t.Fatal("made camp twice")
96+ }
97+ g.move(79, 0)
98+ if g.state.Clock.Day != 3 {
99+ t.Fatalf("a night without camp must still end the day: %+v", g.state.Clock)
76100 }
77101 }
78102
@@ -94,31 +118,109 @@ func equipAll(g *game) {
94118 }
95119 }
96120
97-func TestMainRouteIsWinnableWithoutGrinding(t *testing.T) {
98- for seed := model.Seed(1); seed <= 40; seed++ {
99- g := newGame("local", seed)
100- defeats := 0
101- for moves := 0; moves < 60 && !g.finished(); moves++ {
102- equipAll(g)
103- var poi *model.POI
104- for _, s := range g.state.Campaign.Main.Steps {
105- if !s.Done {
106- poi = g.poi(s.POI)
107- break
108- }
121+func TestCarefulPlayWinsMostCampaigns(t *testing.T) {
122+ wins, total := 0, 30
123+ for seed := model.Seed(1); seed <= model.Seed(total); seed++ {
124+ if careful(newGame("local", seed)) {
125+ wins++
126+ }
127+ }
128+ if wins*100/total < 90 {
129+ t.Fatalf("careful play won %d of %d", wins, total)
130+ }
131+}
132+
133+const safeOdds = 85
134+
135+func careful(g *game) bool {
136+ for moves := 0; moves < 300 && !g.finished(); moves++ {
137+ equipAll(g)
138+ g.recover()
139+ id, x, y := g.target()
140+ if id == "" {
141+ g.waitForCamp()
142+ if id, x, y = g.target(); id == "" {
143+ return false
109144 }
110- if err := g.engage(poi); err != nil {
111- t.Fatalf("seed %d: %v", seed, err)
145+ }
146+ g.move(x, y)
147+ if g.threat == "" {
148+ continue
149+ }
150+ if g.threatView().Odds < safeOdds {
151+ g.waitForCamp()
152+ continue
153+ }
154+ g.interact(g.threat)
155+ }
156+ return g.state.Phase == "victory"
157+}
158+
159+func (g *game) nextMain() *model.POI {
160+ for _, s := range g.state.Campaign.Main.Steps {
161+ if !s.Done {
162+ return g.poi(s.POI)
163+ }
164+ }
165+ return nil
166+}
167+
168+func (g *game) odds(id string) int {
169+ keep := g.threat
170+ g.threat = id
171+ odds := g.threatView().Odds
172+ g.threat = keep
173+ return odds
174+}
175+
176+func (g *game) target() (string, int, int) {
177+ if p := g.nextMain(); g.bossGate(p) && g.odds(p.ID) >= safeOdds {
178+ return p.ID, p.X, p.Y
179+ }
180+ best, bx, by, bo := "", 0, 0, safeOdds-1
181+ for _, p := range g.state.Map.POIs {
182+ if !p.Cleared && g.bossGate(&p) {
183+ if o := g.odds(p.ID); o > bo {
184+ best, bx, by, bo = p.ID, p.X, p.Y, o
112185 }
113- if g.state.Phase == "defeat" {
114- defeats++
186+ }
187+ }
188+ for _, b := range g.state.Map.Bands {
189+ if !b.Cleared {
190+ if o := g.odds(b.ID); o > bo {
191+ best, bx, by, bo = b.ID, b.X, b.Y, o
115192 }
116- g.recover()
117193 }
118- if !g.finished() || defeats > 12 {
119- t.Fatalf("seed %d: finished=%v defeats=%d", seed, g.finished(), defeats)
194+ }
195+ return best, bx, by
196+}
197+
198+func (g *game) waitForCamp() error {
199+ if g.blockerNear() || g.contact() != "" {
200+ px, py := g.state.Party.X, g.state.Party.Y
201+ for y := py - 3; y <= py+3; y++ {
202+ for x := px - 3; x <= px+3; x++ {
203+ if d := dist(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) {
204+ g.move(x, y)
205+ y = py + 4
206+ break
207+ }
208+ }
120209 }
121210 }
211+ if !g.state.Clock.Night() {
212+ g.pass(g.state.Clock.DayLength - g.state.Clock.Time)
213+ }
214+ return g.rest()
215+}
216+
217+func (g *game) calm(x, y int) bool {
218+ 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}} {
219+ if g.blocker(n[0], n[1]) != "" {
220+ return false
221+ }
222+ }
223+ return true
122224 }
123225
124226 func TestPOILevelsGrowTowardTheBoss(t *testing.T) {
@@ -163,14 +265,13 @@ func (g *game) recover() {
163265 if g.finished() || !g.hurt() {
164266 return
165267 }
166- near := g.sites()[0]
167- for _, s := range g.sites() {
168- if dist(s.x, s.y, g.state.Party.X, g.state.Party.Y) < dist(near.x, near.y, g.state.Party.X, g.state.Party.Y) {
169- near = s
170- }
268+ if g.state.Clock.Night() && g.rest() == nil {
269+ return
171270 }
172- if err := g.move(near.x, near.y); err == nil {
173- g.rest()
271+ for _, u := range g.state.Party.Units {
272+ if u.Stats.HP*2 < sim.Effective(u).MaxHP {
273+ g.drink(u.ID)
274+ }
174275 }
175276 }
176277
@@ -214,7 +315,7 @@ func TestBarredBossShowsAsBarred(t *testing.T) {
214315 }
215316 }
216317
217-func TestHPCarriesBetweenFightsAndRestHeals(t *testing.T) {
318+func TestHPCarriesBetweenFights(t *testing.T) {
218319 g := newGame("local", 7)
219320 full := g.state.Party.Units[0].Stats.HP
220321 if full != sim.Effective(g.state.Party.Units[0]).MaxHP {
@@ -224,49 +325,44 @@ func TestHPCarriesBetweenFightsAndRestHeals(t *testing.T) {
224325 if err := g.move(p.X, p.Y); err != nil {
225326 t.Fatal(err)
226327 }
227- sx, sy := g.state.Party.X, g.state.Party.Y
328+ before := g.state.Clock.Time
228329 if err := g.interact(p.ID); err != nil {
229330 t.Fatal(err)
230331 }
231- if err := g.move(sx, sy); err != nil {
232- t.Fatal(err)
332+ if g.state.Clock.Time != before+fightTime {
333+ t.Fatalf("a fight must spend time: %d -> %d", before, g.state.Clock.Time)
233334 }
234- if !g.hurt() && g.state.Party.Units[0].Stats.HP == full {
235- t.Skip("party took no damage")
236- }
237- if err := g.rest(); err == nil {
238- t.Fatal("rested away from a camp")
239- }
240- sp := g.state.Map.Spawns[0]
241- if err := g.move(sp.X, sp.Y); err != nil {
242- t.Fatal(err)
335+ total := 0
336+ for _, u := range g.state.Party.Units {
337+ total += sim.Effective(u).MaxHP - u.Stats.HP
243338 }
244- if err := g.rest(); err != nil {
245- t.Fatal(err)
339+ if total == 0 {
340+ t.Skip("party took no damage")
246341 }
342+ g.move(p.X+1, p.Y)
247343 for _, u := range g.state.Party.Units {
248- if u.Stats.HP != sim.Effective(u).MaxHP {
249- t.Fatalf("%s at %d after rest", u.Name, u.Stats.HP)
344+ if u.Stats.HP > sim.Effective(u).MaxHP {
345+ t.Fatal("hp above max")
250346 }
251347 }
252348 }
253349
254-func TestCampTradeAndPotions(t *testing.T) {
350+func TestShopTradeAndPotions(t *testing.T) {
255351 g := newGame("local", 7)
256352 g.state.Party.Gold = 1000
257353 st := g.snapshot()
258- if st.Camp == nil || len(st.Camp.Stock) != stockSize {
259- t.Fatalf("camp %+v", st.Camp)
354+ if st.Shop == nil || len(st.Shop.Stock) != stockSize {
355+ t.Fatalf("shop %+v", st.Shop)
260356 }
261- it := st.Camp.Stock[0]
357+ it := st.Shop.Stock[0]
262358 if it.Value <= 0 {
263359 t.Fatalf("stock item has no value: %+v", it)
264360 }
265361 if err := g.buy(it.ID); err != nil {
266362 t.Fatal(err)
267363 }
268- if g.state.Party.Gold != 1000-it.Value || len(g.campView().Stock) != stockSize-1 {
269- t.Fatalf("gold %d stock %d", g.state.Party.Gold, len(g.campView().Stock))
364+ if g.state.Party.Gold != 1000-it.Value || len(g.shopView().Stock) != stockSize-1 {
365+ t.Fatalf("gold %d stock %d", g.state.Party.Gold, len(g.shopView().Stock))
270366 }
271367 if err := g.sell(it.ID); err != nil {
272368 t.Fatal(err)
@@ -290,7 +386,7 @@ func TestCampTradeAndPotions(t *testing.T) {
290386 t.Fatal("bought a potion with no gold")
291387 }
292388 g.state.Party.X++
293- if g.snapshot().Camp != nil || g.buy("potion") == nil {
389+ if g.snapshot().Shop != nil || g.buy("potion") == nil {
294390 t.Fatal("trade away from camp")
295391 }
296392 }