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
|
package game
import (
"fmt"
"log"
"strconv"
"strings"
"golang.org/x/exp/slices"
)
type UnitState int
const (
Paralysis UnitState = iota + 1
Poison
Panic
Rage
Ward
)
type Attack struct {
Damage int
Range int
}
func (a *Attack) String() string {
if a.Range == 1 {
return fmt.Sprintf("%d", a.Damage)
}
return fmt.Sprintf("%d range %d", a.Damage, a.Range)
}
func parseAttack(attack string) Attack {
tokens := strings.Split(attack, " ")
a, err := strconv.Atoi(tokens[0])
if err != nil {
log.Fatalf("Attack %s has no valid Damage value\n", attack)
}
r := 1
if len(tokens) == 3 {
r, err = strconv.Atoi(tokens[2])
if err != nil {
log.Fatalf("Attack %s has no valid Range value\n", attack)
}
}
return Attack{a, r}
}
type Unit struct {
PermanentBase
Health int
Movement int
Attack Attack
Upkeep int
FullActions []*FullActionPrototype
FreeActions []*FreeActionPrototype
States []UnitState
// Effects []Effects
AvailMoveActions int
AvailAttackActions int
}
func NewUnit(cardPath string, tile *Tile, owner *Player) *Unit {
card := NewCard(cardPath)
u := &Unit{
NewPermanentBase(card, tile, owner),
card.Values["health"].(int),
card.Values["movement"].(int),
parseAttack(card.Values["attack"].(string)),
card.Values["upkeep"].(int),
[]*FullActionPrototype{},
[]*FreeActionPrototype{},
[]UnitState{},
1,
1,
}
tile.Permanent = u
return u
}
func (u *Unit) move(s *State, tile *Tile) {
if !slices.Contains(u.MoveRangeTiles(s.Map), tile) {
log.Panicf("Unit %s at %v can not move to %v", u.Card.Name, u.Tile.Position, tile.Position)
}
if tile.Permanent != nil {
log.Panicf("Unit %s at %v can not move to occupied %v", u.Card.Name, u.Tile.Position, tile.Position)
}
u.Tile.Permanent = nil
u.Tile = tile
}
func (u *Unit) MoveRangeTiles(m *Map) []*Tile {
tiles := []*Tile{}
for _, pos := range PositionsInRange(u.Tile.Position, u.Movement, false) {
tile := m.TileAt(pos)
if u.IsAvailableTile(tile) {
tiles = append(tiles, tile)
}
}
return tiles
}
func (u *Unit) AttackableTiles(m *Map) []*Tile {
return TilesInRange(m, u, u.Attack.Range)
}
func (u *Unit) AttackablePermanents(m *Map) []Permanent {
permanents := []Permanent{}
for _, t := range u.AttackableTiles(m) {
if t.Permanent != nil && t.Permanent.Attackable() {
permanents = append(permanents, t.Permanent)
}
}
return permanents
}
func (u *Unit) IsAvailableTile(tile *Tile) bool {
return tile.IsFree()
}
func (u *Unit) Fight(p Permanent) {
p.AddDamage(u.Attack.Damage)
}
func (u *Unit) IsDestroyed() bool {
return u.PermanentBase.Damage >= u.Health
}
func (u *Unit) Attackable() bool {
return true
}
func (u *Unit) HasFullAction() bool {
return len(u.FullActions) > 0
}
func (u *Unit) FullAction() {
}
func (u *Unit) GetAvailActionPrototypes() []ActionPrototype {
prototypes := []ActionPrototype{}
if u.AvailMoveActions > 0 {
prototypes = append(prototypes, NewMoveActionPrototype(u))
}
if u.AvailAttackActions > 0 {
prototypes = append(prototypes, NewAttackActionPrototype(u))
}
for _, p := range u.FullActions {
prototypes = append(prototypes, p)
}
for _, p := range u.FreeActions {
prototypes = append(prototypes, p)
}
return prototypes
}
|