aboutsummaryrefslogtreecommitdiff
path: root/go/game/action.go
blob: 5a8fc35b17137947303b5a7022eb7ad81e94630a (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
package game

import (
	"fmt"
	"log"
)

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

type Action interface {
	Source() interface{}
	Controller() *Player
	GameState() *State
	Targets() *Targets
	Target() *Target // Shortcut for to select the first target

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

type PassPriority struct{ player *Player }

func (a *PassPriority) Source() interface{}       { return a.player }
func (a *PassPriority) Controller() *Player       { return a.player }
func (a *PassPriority) GameState() *State         { return a.player.gameState }
func (*PassPriority) Targets() *Targets           { return nil }
func (*PassPriority) Target() *Target             { return nil }
func (*PassPriority) NeedsActionCostChoice() bool { return false }
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 TargetSelection struct {
	player  *Player
	targets *Targets
}

func (sel *TargetSelection) Source() interface{}         { return sel.player }
func (sel *TargetSelection) Controller() *Player         { return sel.player }
func (sel *TargetSelection) GameState() *State           { return sel.player.gameState }
func (sel *TargetSelection) Targets() *Targets           { return sel.targets }
func (sel *TargetSelection) Target() *Target             { return sel.targets.ts[0] }
func (*TargetSelection) NeedsActionCostChoice() bool     { return false }
func (sel *TargetSelection) CheckTargets(s *State) error { return sel.targets.CheckTargets(s) }
func (*TargetSelection) PayCosts(*State) bool            { return true }
func (*TargetSelection) resolve(*State)                  {}
func (*TargetSelection) String() string                  { return "target selection" }

func newTargetSelection(player *Player, targets *Targets) Action {
	return &TargetSelection{player, targets}
}

func newHandCardSelection(p *Player, min, max int) Action {
	a := &TargetSelection{player: p}
	targetDesc := TargetDesc{"hand card", fmt.Sprintf("%d-%d", min, max)}
	a.targets = newTargets(newTarget(p.gameState, targetDesc, a))
	return a
}

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

func (a *ActionBase) Source() interface{} { return a.source }
func (a *ActionBase) Controller() *Player {
	switch t := a.source.(type) {
	case *Player:
		return t
	case Permanent:
		return t.Controller()
	default:
		log.Panicf("Unhandled source type %v", t)
		return nil
	}
}
func (a *ActionBase) GameState() *State { return a.Controller().gameState }
func (a *ActionBase) resolve(s *State)  { a.resolveFunc(s) }
func (a *ActionBase) PayCosts(s *State) bool {
	if a.costFunc != nil {
		return a.costFunc(s)
	}
	return true
}
func (a *ActionBase) Targets() *Targets { return a.targets }
func (a *ActionBase) Target() *Target   { return a.targets.ts[0] }
func (a *ActionBase) CheckTargets(s *State) error {
	if a.targets == nil {
		return nil
	}
	return a.targets.CheckTargets(s)
}

type TriggeredAction struct {
	ActionBase
	event Event
}

func newTriggeredAction(event Event, trigger Trigger, resolveFunc ActionResolveFunc,
	costFunc ActionCostFunc,
) *TriggeredAction {
	return &TriggeredAction{
		ActionBase: ActionBase{
			source:      trigger.Source(),
			Card:        trigger.Card(),
			resolveFunc: resolveFunc,
			costFunc:    costFunc,
		},
		event: event,
	}
}

func (a *TriggeredAction) String() string {
	return fmt.Sprintf("%v ! %v", a.event, a.source)
}

// Wrapper action to pass all triggered actions through the PlayerControl
type DeclareTriggeredActionsAction struct {
	actions []*TriggeredAction
}

func (sel *DeclareTriggeredActionsAction) Source() interface{}         { return nil }
func (sel *DeclareTriggeredActionsAction) Controller() *Player         { return nil }
func (sel *DeclareTriggeredActionsAction) GameState() *State           { return nil }
func (sel *DeclareTriggeredActionsAction) Targets() *Targets           { return nil }
func (sel *DeclareTriggeredActionsAction) Target() *Target             { return nil }
func (*DeclareTriggeredActionsAction) NeedsActionCostChoice() bool     { return false }
func (sel *DeclareTriggeredActionsAction) CheckTargets(s *State) error { return nil }
func (*DeclareTriggeredActionsAction) PayCosts(*State) bool            { return true }
func (*DeclareTriggeredActionsAction) resolve(*State)                  {}
func (*DeclareTriggeredActionsAction) String() string                  { return "declared triggered actions" }

func NewDeclareTriggeredActionsAction(t []*TriggeredAction) *DeclareTriggeredActionsAction {
	return &DeclareTriggeredActionsAction{t}
}

type PileDropAction struct {
	ActionBase
	pile []Permanent
}

func (a *PileDropAction) String() string {
	perm := a.source.(Permanent)
	controller := perm.Controller()
	if a.targets.RequireSelection() {
		return fmt.Sprintf("%s drop %v", controller.Name, perm.Tile().Position)
	}

	s := fmt.Sprintf("%s drop [", controller.Name)
	pile := perm.Pile()
	for i, t := range a.targets.ts {
		s = fmt.Sprintf("%s %v@%v,", s, pile[i], t.sel[0])
	}
	return s[:len(s)-1] + " ]"
}

func (*PileDropAction) PayCosts(*State) bool { return true }

func newPileDropAction(perm Permanent, tile *Tile, pile []Permanent) *PileDropAction {
	a := &PileDropAction{
		ActionBase{
			source: perm,
		},
		pile,
	}

	a.resolveFunc = func(s *State) {
		for i, p := range pile {
			p.onDrop(p)
			p.setContainingPerm(nil)
			enterTileOrPile(p, a.targets.ts[i].sel[0].(*Tile))
		}

		perm.clearPile()
	}

	a.targets = newPileDropTargets(perm.Controller().gameState, a)
	return a
}

type PlayAction struct {
	ActionBase
	ChoosenVariadicCost int
}

var (
	permanentPlayActionTarget = TargetDesc{"available spawn tile", "1"}
	equipmentPlayActionTarget = TargetDesc{"available spawn tile", "?"}
)

func NewPlayActionCostFunc(player *Player, cost int) ActionCostFunc {
	return func(*State) bool {
		if player.Resource < cost {
			return false
		}
		player.Resource -= cost
		return true
	}
}

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

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

	s := p.gameState
	if a.costFunc == nil {
		a.costFunc = NewPlayActionCostFunc(p, c.PlayCosts.Costs(s))
	}

	if c.IsPermanent() {
		if c.Type == CardTypes.Equipment {
			a.targets = newTargets(newTarget(s, equipmentPlayActionTarget, a))
		} else {
			a.targets = newTargets(newTarget(s, permanentPlayActionTarget, a))
		}
	} else if targetDesc := c.Impl.playTargets(); targetDesc != INVALID_TARGET_DESC {
		a.targets = newTargets(newTarget(s, targetDesc, a))
	} else {
		a.targets = newTargets()
	}

	a.resolveFunc = func(s *State) {
		s.resolvePlay(a)
	}

	return a
}

func NewPlayActionVariadicCosts(p *Player, c *Card, variadicCosts int) *PlayAction {
	a := NewPlayAction(p, c, NewPlayActionCostFunc(p, c.PlayCosts.Costs(p.gameState, variadicCosts)))
	a.ChoosenVariadicCost = variadicCosts
	return a
}

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

	if a.Card.Type == CardTypes.Equipment && len(a.Target().sel) == 0 {
		return fmt.Sprintf("%s play %s@next unit", p.Name, a.Card.Name)
	}

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

type MoveAction struct {
	ActionBase
}

var moveActionTargetDesc = TargetDesc{"available tile", "1"}

func targetTile(t *Target) *Tile {
	if tile, ok := t.sel[0].(*Tile); ok {
		return tile
	}

	return t.sel[0].(*Unit).Tile()
}

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

	a.targets = newTargets(newTarget(u.Controller().gameState, moveActionTargetDesc, a))

	a.resolveFunc = func(s *State) {
		t := targetTile(a.Target())
		movePermanent(u, t)
	}

	a.costFunc = genMoveActionCost(u)

	return a
}

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

	t := targetTile(a.Target())
	return fmt.Sprintf("%v -> %v", u, t.Position)
}

func genBaseActionCost(u *Unit, attack, move int) ActionCostFunc {
	return func(*State) bool {
		if u.AvailAttackActions < attack || u.AvailMoveActions < move {
			return false
		}
		u.AvailAttackActions -= attack
		u.AvailMoveActions -= move
		return true
	}
}

func genAttackActionCost(u *Unit) ActionCostFunc {
	return genBaseActionCost(u, 1, 0)
}

func genMoveActionCost(u *Unit) ActionCostFunc {
	return genBaseActionCost(u, 0, 1)
}

func genFullActionCost(u *Unit) ActionCostFunc {
	return func(*State) bool {
		if u.AvailAttackActions < 1 || u.AvailMoveActions < 1 {
			return false
		}
		u.AvailAttackActions = 0
		u.AvailMoveActions = 0
		return true
	}
}

type EquipAction struct {
	ActionBase
	equipment *Equipment
}

func NewEquipAction(u *Unit, e *Equipment) *EquipAction {
	a := &EquipAction{
		ActionBase{
			source: u,
			Card:   u.Card(),
		},
		e,
	}

	a.resolveFunc = func(s *State) {
		u.equip(e)
	}

	return a
}

func (a *EquipAction) String() string {
	u := a.source.(*Unit)
	return fmt.Sprintf("%v equip %v", u, a.equipment)
}

type AttackAction struct {
	ActionBase
}

var attackActionTargetDesc = TargetDesc{"attackable enemy permanent", "1"}

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

	a.targets = newTargets(newTarget(u.Controller().gameState, attackActionTargetDesc, a))

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

	a.costFunc = genAttackActionCost(u)

	return a
}

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

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

type ArtifactSwitchAction struct {
	ActionBase
	Artifact Permanent
}

func newArtifactSwitchAction(u *Unit, artifact Permanent) *ArtifactSwitchAction {
	a := &ArtifactSwitchAction{
		ActionBase{
			source: u,
			Card:   u.Card(),
		},
		artifact,
	}

	a.resolveFunc = func(s *State) {
		s.switchPermanents(a.Source().(*Unit), a.Artifact)
	}

	a.costFunc = genMoveActionCost(u)

	return a
}

func (a *ArtifactSwitchAction) String() string {
	return fmt.Sprintf("%v <-> %v", a.Source().(*Unit), a.Artifact)
}

type ArtifactMoveAction struct {
	ActionBase
	Artifact Permanent
}

func newArtifactMoveAction(u *Unit, artifact Permanent) *ArtifactMoveAction {
	a := &ArtifactMoveAction{
		ActionBase{
			source: u,
			Card:   u.Card(),
		},
		artifact,
	}

	a.targets = newArtifactMoveTargets(u.Controller().gameState, a)

	a.resolveFunc = func(s *State) {
		tile := targetTile(a.Targets().ts[0])
		movePermanent(u, tile)

		tile = targetTile(a.Targets().ts[1])
		movePermanent(artifact, tile)
	}

	a.costFunc = genFullActionCost(u)

	return a
}

func (a *ArtifactMoveAction) String() string {
	if !a.Targets().HasSelections() {
		return fmt.Sprintf("%v move %v", a.Source().(*Unit), a.Artifact)
	}

	t1 := targetTile(a.Targets().ts[0])
	t2 := targetTile(a.Targets().ts[1])
	return fmt.Sprintf("%v,%v -> %v,%v", a.Source().(*Unit), a.Artifact, t1, t2)
}

type FullAction struct {
	ActionBase
	desc string
	tag  string
}

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

		desc: desc,
	}

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

	a.costFunc = genFullActionCost(u)
	return a
}

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

	return fmt.Sprintf("%v↻@%v", u, 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.RequireSelection() {
		return fmt.Sprintf("free_action %s", a.desc)
	}

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

var buyActionTargetDesc = TargetDesc{"store card", "?"}

type BuyAction struct{ TargetSelection }

func (a *BuyAction) card() *Card {
	return a.Target().sel[0].(*Card)
}

func (a *BuyAction) CheckTargets(s *State) error {
	err := a.targets.CheckTargets(s)
	if err != nil {
		return err
	}
	return s.isValidBuy(a)
}

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

func (a *BuyAction) PayCosts(s *State) bool {
	cost := a.card().BuyCosts.Costs(s)
	if cost < 0 || cost > a.player.Resource {
		return false
	}

	a.player.Resource -= cost
	return true
}

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

	return fmt.Sprintf("%s buy %s", a.player.Name, a.card().Name)
}

func newBuyAction(p *Player) *BuyAction {
	a := &BuyAction{TargetSelection{player: p}}
	a.targets = newTargets(newTarget(p.gameState, buyActionTargetDesc, a))
	return a
}

var upkeepActionTargetDesc = TargetDesc{"unit you controll", "*"}

type UpkeepAction struct {
	TargetSelection
}

func (a *UpkeepAction) PayCosts(*State) bool {
	costs := 0
	for _, t := range a.Target().sel {
		u := t.(*Unit)
		costs += u.UpkeepCost()
	}

	if a.player.Resource < costs {
		return false
	}

	a.player.Resource -= costs
	return true
}

func (a *UpkeepAction) resolve(*State) {
	p := a.player
	s := p.gameState
	for _, i := range a.Target().sel {
		u := i.(*Unit)
		s.destroyPermanent(u)
	}

	// Keep and pay for the rest
	for _, u := range s.Units {
		if u.Controller() != p {
			continue
		}

		p.Resource -= u.UpkeepCost()
		u.onUpkeep()
	}
}

func (a *UpkeepAction) String() string {
	return fmt.Sprintf("upkeep disbanding: %v", a.Target().sel)
}

func newUpkeepAction(p *Player) *UpkeepAction {
	a := &UpkeepAction{TargetSelection{player: p}}
	a.targets = newTargets(newTarget(p.gameState, upkeepActionTargetDesc, a))

	return a
}

func (a *ActionBase) NeedsActionCostChoice() bool {
	return a.costFunc == nil
}

func UseMoveAction(a Action) {
	switch a := a.(type) {
	case *EquipAction:
		a.costFunc = genMoveActionCost(a.Source().(*Unit))
	}
}

func UseAttackAction(a Action) {
	switch a := a.(type) {
	case *EquipAction:
		a.costFunc = genAttackActionCost(a.Source().(*Unit))
	}
}