aboutsummaryrefslogtreecommitdiff
path: root/go/game/action.go
blob: 08e42f646daa55ad62f64334811f3ca07744cf51 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package game

import (
	"fmt"
)

type (
	ActionCostFunc      func(*State) bool
	ActionResolveFunc   func(*State)
	ActionFuncPrototype func(Action) ActionResolveFunc
)

type Action interface {
	Source() interface{}
	Targets() *Targets

	CheckTargets(*State) error
	PayCosts(s *State) bool
	Resolve(s *State)
	String() string
}

type PassPriority struct{ source *Player }

func (a *PassPriority) Source() interface{}     { return a.source }
func (*PassPriority) Targets() *Targets         { return nil }
func (*PassPriority) CheckTargets(*State) error { return nil }
func (*PassPriority) PayCosts(*State) bool      { return true }
func (*PassPriority) Resolve(*State)            {}
func (*PassPriority) String() string            { return "pass" }
func NewPassPriority(p *Player) Action          { return &PassPriority{p} }

type HandCardSelection struct{ cards []*Card }

func (*HandCardSelection) Source() interface{}       { return nil }
func (*HandCardSelection) Targets() *Targets         { return nil }
func (*HandCardSelection) CheckTargets(*State) error { return nil }
func (*HandCardSelection) PayCosts(*State) bool      { return true }
func (*HandCardSelection) Resolve(*State)            {}
func (*HandCardSelection) String() string            { return "hand card selection" }
func NewHandCardSelection(cards []*Card) Action      { return &HandCardSelection{cards} }

type ActionBase struct {
	source      interface{}
	Card        *Card
	targets     *Targets
	resolveFunc ActionResolveFunc
	costFunc    ActionCostFunc
}

func (a *ActionBase) Source() interface{} {
	return a.source
}

func (a *ActionBase) Resolve(s *State) {
	a.resolveFunc(s)
}

func (a *ActionBase) PayCosts(s *State) bool {
	return a.costFunc(s)
}

func (a *ActionBase) Targets() *Targets {
	return a.targets
}

func (a *ActionBase) CheckTargets(s *State) error {
	return a.targets.CheckTargets(s)
}

type PlayAction struct {
	ActionBase
}

const permanentPlayActionTarget = "available spawn tile"

func NewPlayAction(p *Player, c *Card, args ...interface{}) *PlayAction {
	a := &PlayAction{ActionBase{
		source: p,
		Card:   c,
	}}

	if len(args) > 0 {
		if costFunc, ok := args[0].(ActionCostFunc); ok {
			a.costFunc = costFunc
		}
	}

	if a.costFunc == nil {
		a.costFunc = func(*State) bool {
			cost := c.PlayCosts()

			if p.Resource < cost {
				return false
			}
			p.Resource -= cost
			return true
		}
	}

	s := p.gameState
	if c.IsPermanent() {
		a.targets = newTargets(s, []string{permanentPlayActionTarget}, a)
	} else {
		a.targets = newTargets(s, []string{}, a)
	}

	p.Hand.RemoveCard(c)

	a.resolveFunc = func(s *State) {
		s.resolvePlay(p, c, a.targets)
	}

	return a
}

func (a *PlayAction) String() string {
	p := a.source.(*Player)
	if a.targets.RequireTargets() {
		return fmt.Sprintf(" %s play %s", p.Name, a.Card.Name)
	}

	return fmt.Sprintf("%s play %s@%v", p.Name, a.Card.Name, a.targets)
}

type MoveAction struct {
	ActionBase
}

const moveActionTargetDesc = "available tile"

func NewMoveAction(u *Unit) *MoveAction {
	a := &MoveAction{
		ActionBase{
			source: u,
			Card:   u.Card(),
		},
	}

	a.targets = newTargets(u.Controller().gameState, []string{moveActionTargetDesc}, a)

	a.resolveFunc = func(s *State) {
		tile := a.targets.sel[0].(*Tile)
		u.move(s, tile)
	}

	a.costFunc = func(*State) bool {
		if u.AvailMoveActions < 1 {
			return false
		}
		u.AvailMoveActions--
		return true
	}

	return a
}

func (a *MoveAction) String() string {
	u := a.source.(*Unit)
	if a.targets.RequireTargets() {
		return fmt.Sprintf("move %s", u.Movement.String())
	}

	t := a.targets.sel[0].(*Tile)
	return fmt.Sprintf("%v: %v -> %v", u.Controller().Name, u.Card().Name, t.Position)
}

type AttackAction struct {
	ActionBase
}

const attackActionTargetDesc = "attackable enemy permanent"

func NewAttackAction(u *Unit) *AttackAction {
	a := &AttackAction{ActionBase{
		source: u,
		Card:   u.Card(),
	}}

	a.targets = newTargets(u.Controller().gameState, []string{attackActionTargetDesc}, a)

	a.resolveFunc = func(s *State) {
		s.Fight(u, a.targets.sel[0].(Permanent))
	}

	a.costFunc = func(*State) bool {
		if u.AvailAttackActions < 1 {
			return false
		}
		u.AvailAttackActions--
		return true
	}

	return a
}

func (a *AttackAction) String() string {
	u := a.source.(*Unit)
	if a.targets.RequireTargets() {
		return fmt.Sprintf("attack %v", u.Attack)
	}

	target := a.targets.sel[0].(Permanent)
	return fmt.Sprintf("%v: %v x %v",
		u.Controller().Name, u.Card().Name, target.Tile().Position)
}

type FullAction struct {
	ActionBase
	desc string
}

func NewFullAction(u *Unit, proto ActionFuncPrototype, desc string) *FullAction {
	a := &FullAction{
		ActionBase{
			source: u,
			Card:   u.Card(),
		},

		desc,
	}

	a.resolveFunc = func(s *State) {
		rf := proto(a)
		rf(s)
	}

	a.costFunc = func(*State) bool {
		if u.AvailAttackActions < 1 && u.AvailMoveActions < 1 {
			return false
		}
		u.AvailAttackActions--
		u.AvailMoveActions--
		return true
	}
	return a
}

func (a *FullAction) String() string {
	u := a.source.(*Unit)
	if a.targets.RequireTargets() {
		return fmt.Sprintf("↻ %s", a.desc)
	}

	return fmt.Sprintf("%v: %s ↻@%v", u.Controller(), u.Card().Name, a.targets)
}

type FreeAction struct {
	ActionBase
	desc string
}

func NewFreeAction(p Permanent, resolveProto ActionFuncPrototype, costFunc ActionCostFunc,
	desc string,
) *FreeAction {
	a := &FreeAction{
		ActionBase{
			source:   p,
			Card:     p.Card(),
			costFunc: costFunc,
		},
		desc,
	}

	a.resolveFunc = func(s *State) {
		rf := resolveProto(a)
		rf(s)
	}

	return a
}

func (a *FreeAction) String() string {
	p := a.source.(Permanent)
	if a.targets.RequireTargets() {
		return fmt.Sprintf("free_action %s", a.desc)
	}

	return fmt.Sprintf("%v: %s free_action@%v", p.Controller(), p.Card().Name, a.targets)
}

type BuyAction struct {
	player *Player
	card   *Card
}

func (a *BuyAction) Source() interface{} {
	return a.player
}

func (a *BuyAction) Targets() *Targets {
	return newTargetsWithSel(a.player.gameState, []string{"store cards"}, a, []interface{}{a.card})
}

func (a *BuyAction) CheckTargets(s *State) error {
	return s.isValidBuy(a)
}

func (a *BuyAction) Resolve(s *State) {
	a.player.Store.MoveCard(a.card, a.player.DiscardPile)
}

func (a *BuyAction) PayCosts(*State) bool {
	if a.card.BuyCost < 0 || a.card.BuyCost > a.player.Resource {
		return false
	}

	a.player.Resource -= a.card.BuyCost
	return true
}

func (a *BuyAction) String() string { return fmt.Sprintf("%s buy %s", a.player.Name, a.card.Name) }

func NewBuyAction(p *Player, c *Card) *BuyAction {
	return &BuyAction{p, c}
}

type UpkeepAction struct {
	player *Player
	unit   *Unit
	pay    bool
}

func (a *UpkeepAction) Resolve(s *State) {
	if a.pay {
		a.player.Resource -= a.unit.Upkeep
	} else {
		s.DestroyPermanent(a.unit)
	}
}

func (a *UpkeepAction) PayCosts(*State) bool {
	if a.unit.Upkeep > a.player.Resource {
		return false
	}

	a.player.Resource -= a.unit.Upkeep
	return true
}

func (a *UpkeepAction) String() string {
	if a.pay {
		return fmt.Sprintf("%s keep %v", a.player.Name, a.unit.Tile().Position)
	}

	return fmt.Sprintf("%s disband %v", a.player.Name, a.unit.Tile().Position)
}

func NewUpkeepAction(p *Player, u *Unit, pay bool) *UpkeepAction {
	return &UpkeepAction{p, u, pay}
}