aboutsummaryrefslogtreecommitdiff
path: root/go/game/targets_test.go
blob: e4105e8bc7ec499d59ee38949648b73968cc8907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package game

import (
	"strings"
	"testing"
)

func TestTileTargets(t *testing.T) {
	mapDef := `map: |1-
 HSTS
 HSFS
 TSWD

symbols:
  T: tower
  H: house
  F: farm
  S: street
  W: deep water
  D: docks
`
	s := NewLocalState()
	m, _ := readMap(strings.NewReader(mapDef))
	s.SetMap(m)

	p := s.AddNewPlayer("player", NewDeck())
	u := s.addNewUnit(NewCard("base/archer"), Position{1, 1}, p)
	s.addNewUnit(NewCard("base/cavalry"), Position{1, 2}, p)

	a := newFullAction(u,
		func(Action) ActionResolveFunc { var f ActionResolveFunc; return f },
		"mock full action")

	tDesc := newTargetDesc("tile")
	trgt := newTarget(s, tDesc, a)
	opts := trgt.Options()
	if len(opts) != 12 {
		t.Fatal("expexted 12 candidates not:", len(opts))
	}

	tDesc = newTargetDesc("water tile")
	trgt = newTarget(s, tDesc, a)
	opts = trgt.Options()
	if len(opts) != 1 {
		t.Fatal("expected 1 water candidates not:", len(opts))
	}

	tDesc = newTargetDesc("free tile")
	trgt = newTarget(s, tDesc, a)
	opts = trgt.Options()
	if len(opts) != 10 {
		t.Fatal("expected 10 free candidates not:", len(opts))
	}

	tDesc = newTargetDesc("available tile")
	trgt = newTarget(s, tDesc, a)
	opts = trgt.Options()
	if len(opts) != 9 {
		t.Fatal("expected 9 available candidates not:", len(opts))
	}

	tDesc = newTargetDesc("adjacent tile")
	trgt = newTarget(s, tDesc, a)
	opts = trgt.Options()
	if len(opts) != 9 {
		t.Fatal("expected 9 available candidates not:", len(opts))
	}

	tDesc = newTargetDesc("adjacent available tile")
	trgt = newTarget(s, tDesc, a)
	opts = trgt.Options()
	if len(opts) != 6 {
		t.Fatal("expected 6 available candidates not:", len(opts))
	}

	fisher := NewCard("nautics/fisher")
	p.Hand.AddCard(fisher)
	pa := NewPlayAction(p, fisher)
	opts = pa.Target().Options()
	if len(opts) != 1 {
		t.Fatal("expected 1 available candidates not:", len(opts))
	}
}

func TestSpawnTargets(t *testing.T) {
	s := NewLocalState()
	m, _ := GetMap("the-kraken")
	s.SetMap(m)

	s.activePhase = actionPhase

	p := s.AddNewPlayer("player", NewDeck())
	ctrl := newMockPlayerControl(p)
	ctrl.actionToSend = NewPassPriority(p)
	p.Ctrl = ctrl

	p.gainResource(2)

	farmer := NewCard("misc/farmer")
	p.Hand.AddCard(farmer)
	fisher := NewCard("nautics/fisher")
	p.Hand.AddCard(fisher)

	pa := NewPlayAction(p, farmer)
	if !pa.Target().RequireSelection() {
		t.Fatal("playing farmer has not require selection")
	}
	if !pa.Target().AllowSelection() {
		t.Fatal("playing farmer has no valid targets")
	}
	farmerPos := Position{3, 13}
	err := pa.Target().AddSelection(m.TileAt(farmerPos))
	if err != nil {
		t.Fatal("Not allowed to play farmer at:", farmerPos)
	}
	s.declareAction(pa)
	if s.stack.IsEmpty() {
		t.Fatal("farmer play action not declared")
	}
	s.stack.resolve()

	pa2 := NewPlayAction(p, fisher)
	if !pa2.Target().RequireSelection() {
		t.Fatal("playing fisher has not require selection")
	}
	if !pa2.Target().AllowSelection() {
		t.Fatal("playing fisher has no valid targets")
	}
	fisherPos := Position{3, 10}
	pa2.Target().AddSelection(m.TileAt(fisherPos))
	if err != nil {
		t.Fatal("Not allowed to play fisher at:", fisherPos)
	}
	s.declareAction(pa2)
	if s.stack.IsEmpty() {
		t.Fatal("fisher play action not declared")
	}
	s.stack.resolve()
}

func TestFlexAttackTargets(t *testing.T) {
	mapDef := `map: |1-
 HSTS
 HSFS
 TSWS

symbols:
  T: tower
  H: house
  F: farm
  S: street
  W: deep water
`
	s := NewLocalState()
	m, _ := readMap(strings.NewReader(mapDef))
	s.SetMap(m)

	p := s.AddNewPlayer("player", NewDeck())
	o := s.AddNewPlayer("oponent", NewDeck())

	u := s.addNewUnit(NewCard("base/pikeman"), Position{1, 1}, p)
	s.addNewUnit(NewCard("base/pikeman"), Position{0, 0}, o)
	s.addNewUnit(NewCard("base/pikeman"), Position{3, 1}, o)
	s.addNewUnit(NewCard("base/cavalry"), Position{3, 2}, o)

	a := NewAttackAction(u)
	opts := a.Target().Options()
	if len(opts) != 2 {
		t.Fatal("expected 2 attackable options")
	}
}

func TestDisjunction(t *testing.T) {
	mapDef := `map: |1-
 HST
 HSF
 TSW
symbols:
  T: tower
  H: house
  F: farm
  S: street
  W: deep water
`
	s := NewLocalState()
	m, _ := readMap(strings.NewReader(mapDef))
	s.SetMap(m)

	p := s.AddNewPlayer("player", NewDeck())

	pioneer := NewUnit(NewCard("base/pioneer"), s.Map().TileAt(Position{1, 1}), p)
	s.addPermanent(pioneer)

	sword := newEquipmentFromPath("base/sword", s.Map().TileAt(Position{0, 1}), p)
	s.addPermanent(sword)

	fa := pioneer.FullActions[0]
	targets := fa.Targets()
	if err := targets.AddSelection(s.Map().TileAt(Position{1, 1})); err != nil {
		t.Fatalf("TileAt(1,1) not a valid target for %v", fa)
	}

	if err := targets.CheckTargets(s); err != nil {
		t.Fatalf("TileAt(1,1) not a valid target for %v", fa)
	}

	targets.ClearSelections()

	if err := targets.AddSelection(sword); err != nil {
		t.Fatalf("%v not a valid target for %v", sword, fa)
	}

	if err := targets.CheckTargets(s); err != nil {
		t.Fatalf("%v not a valid target for %v", sword, fa)
	}
}