aboutsummaryrefslogtreecommitdiff
path: root/go/game/permanent.go
blob: b4090592aad9857a766b14f96ad37e7ac8f9fcc1 (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
package game

import (
	"fmt"
	"log"
	"strconv"
	"strings"

	"golang.org/x/exp/slices"
)

type Permanent interface {
	Card() *Card
	Tile() *Tile
	Damage() int
	Pile() []Permanent
	Controller() *Player
	Owner() *Player
	Effects() []string
	HasEffect(string) bool
	XEffect(string) (int, error)

	String() string
	Fight(p Permanent)
	AddDamage(damage int)
	IsDestroyed() bool
	Attackable() bool
	AvailActions() []Action
	IsAvailableTile(*Tile) bool
}

type PermanentBase struct {
	card       *Card
	tile       *Tile
	damage     int
	pile       []Permanent
	controller *Player
	owner      *Player
	tmpEffects []string
}

func NewPermanentBase(card *Card, tile *Tile, owner *Player) PermanentBase {
	if tile.Permanent != nil {
		log.Fatalf("Tile at %v already occupied by %v", tile.Position, tile.Permanent)
	}

	return PermanentBase{
		card:       card,
		tile:       tile,
		controller: owner,
		owner:      owner,
	}
}

func (p *PermanentBase) Card() *Card {
	return p.card
}

func (p *PermanentBase) Tile() *Tile {
	return p.tile
}

func (p *PermanentBase) Damage() int {
	return p.damage
}

func (p *PermanentBase) Pile() []Permanent {
	return p.pile
}

func (p *PermanentBase) Controller() *Player {
	return p.controller
}

func (p *PermanentBase) Owner() *Player {
	return p.owner
}

func (p *PermanentBase) String() string {
	return fmt.Sprintf("%s's %s@%s",
		p.Controller().Name, p.Card().Name, p.Tile().Position.String())
}

func DealDamage(src, dest Permanent, damage int) {
	actualDamage := damage
	shield, err := dest.XEffect("shield")
	if err != nil {
		// TODO: implement piercing

		actualDamage -= shield
		if actualDamage < 0 {
			actualDamage = 0
		}
	}
	dest.AddDamage(actualDamage)
}

func (p *PermanentBase) AddDamage(damage int) {
	p.damage += damage
}

func (p *PermanentBase) Effects() []string {
	return append(p.card.getEffects(), p.tmpEffects...)
}

func (p *PermanentBase) HasEffect(effect string) bool {
	return slices.Contains(p.tmpEffects, effect) || p.card.hasEffect(effect)
}

func (p *PermanentBase) XEffect(effect string) (int, error) {
	x, err := p.card.getXEffect(effect)

	for _, e := range p.tmpEffects {
		if !strings.Contains(e, effect) {
			continue
		}

		tokens := strings.SplitN(e, " ", 2)
		tmpX, err := strconv.Atoi(tokens[1])
		if err != nil {
			return 0, err
		}
		x += tmpX
	}

	return x, err
}

func (p *PermanentBase) IsAvailableTile(tile *Tile) bool {
	return tile.IsAvailableForCard(p.card)
}