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
|
package game
import (
"log"
)
type UnitMark int
const (
paralysis UnitMark = iota + 1
poison
panic_
rage
ward
faith
)
func (mark UnitMark) String() string {
switch mark {
case paralysis:
return "paralysis"
case poison:
return "poison"
case panic_:
return "panic"
case rage:
return "rage"
case ward:
return "ward"
case faith:
return "faith"
default:
log.Fatalf("Unhandled unit mark %d", mark)
return ""
}
}
var UnitMarks = struct {
Paralysis UnitMark
Poison UnitMark
Panic UnitMark
Rage UnitMark
Ward UnitMark
Faith UnitMark
}{
Paralysis: paralysis,
Poison: poison,
Panic: panic_,
Rage: rage,
Ward: ward,
Faith: faith,
}
const (
DEFAULT_AVAIL_MOVE_ACTIONS = 1
DEFAULT_AVAIL_ATTACK_ACTIONS = 1
)
type Unit struct {
PermanentBase
Health int
Movement Movement
Attack Attack
Upkeep int
FullActions []*FullAction
FreeActions []*FreeAction
States map[UnitMark]int
// Effects []Effects
AvailMoveActions int
additionalMoveActions int
AvailAttackActions int
additionalAttackActions int
}
func NewUnit(card *Card, tile *Tile, owner *Player) *Unit {
var movement Movement
movementDesc, found := card.Values["movement"]
if !found {
movement = INVALID_MOVEMENT()
} else {
movement = parseMovement(movementDesc)
}
upkeep, ok := card.Values["upkeep"].(int)
if !ok {
upkeep = 0
}
var attack Attack
if attackDesc, found := card.Values["attack"]; found {
attack = parseAttack(attackDesc)
}
u := &Unit{
PermanentBase: NewPermanentBase(card, tile, owner),
Health: card.Values["health"].(int),
Movement: movement,
Attack: attack,
Upkeep: upkeep,
States: make(map[UnitMark]int),
AvailMoveActions: DEFAULT_AVAIL_MOVE_ACTIONS,
AvailAttackActions: DEFAULT_AVAIL_ATTACK_ACTIONS,
}
// TODO: parse effect for additional actions
u.FullActions = card.Impl.fullActions(u)
u.FreeActions = card.Impl.freeActions(u)
tile.Permanent = u
return u
}
func NewUnitFromPath(cardPath string, tile *Tile, owner *Player) *Unit {
return NewUnit(NewCard(cardPath), tile, owner)
}
func (u *Unit) resetBaseActions() {
u.AvailMoveActions = DEFAULT_AVAIL_MOVE_ACTIONS + u.additionalMoveActions
u.AvailAttackActions = DEFAULT_AVAIL_ATTACK_ACTIONS + u.additionalAttackActions
}
func (u *Unit) onUpkeep() {
u.resetBaseActions()
}
func (u *Unit) move(s *State, tile *Tile) {
if !u.IsAvailableTile(tile) {
log.Fatalf("moveing to not available tile %v", tile)
}
if u.tile != nil {
u.tile.Permanent = nil
} else if u.containingPerm != nil {
RemovePermanentFromPile(u.containingPerm, u)
} else {
log.Fatalf("unit has no current location")
}
if tile.Permanent == nil {
tile.Permanent = u
u.tile = tile
} else { // crew permanent at tile
AddPermanentToPile(tile.Permanent, u)
u.tile = nil
}
}
func (u *Unit) MoveRangeTiles(m *Map) []*Tile {
tiles := []*Tile{}
// TODO: build graph only containing possible tile in movement range
graph := m.generateMapGraphFor(u)
for _, pos := range PositionsInRange(u.tile.Position, u.Movement.Range, false) {
tile := m.TileAt(pos)
if tile == nil || !u.IsAvailableTile(tile) {
continue
}
path, err := findPathTo(graph, u, pos)
if err != nil {
continue
}
if path.Distance/2 > int64(u.Movement.Range) {
continue
}
tiles = append(tiles, tile)
}
return tiles
}
func (u *Unit) AttackableTiles(m *Map) []*Tile {
return TilesInRange(m, u, u.Attack.MaxRange())
}
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) AttackableEnemyPermanents(m *Map) []Permanent {
controller := u.Controller()
permanents := []Permanent{}
for _, t := range u.AttackableTiles(m) {
if t.Permanent != nil && t.Permanent.Attackable() &&
t.Permanent.Controller() != controller {
permanents = append(permanents, t.Permanent)
}
}
return permanents
}
func (u *Unit) Fight(p Permanent) {
r := DistanceBetweenPermanents(p, u)
DealDamage(u, p, u.Attack.DamageInRange(r))
}
func (u *Unit) IsDestroyed() bool {
return u.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) AvailActions() (actions []Action) {
if u.AvailMoveActions > 0 && u.Movement != INVALID_MOVEMENT() {
actions = append(actions, NewMoveAction(u))
}
m := u.Controller().gameState.Map
if u.AvailAttackActions > 0 && !INVALID_ATTACK(u.Attack) &&
len(u.AttackableEnemyPermanents(m)) > 0 {
actions = append(actions, NewAttackAction(u))
}
if u.AvailAttackActions > 0 && u.AvailMoveActions > 0 {
for _, p := range u.FullActions {
actions = append(actions, p)
}
}
for _, p := range u.FreeActions {
actions = append(actions, p)
}
return actions
}
func (u *Unit) onPile(containing Permanent) {
u.PermanentBase.onPile(u)
if baseUnit, ok := containing.(*Unit); ok {
baseUnitAttackRange := baseUnit.Attack.MaxRange()
for r, a := range u.Attack.attacks {
if r > baseUnitAttackRange-1 {
baseUnit.Attack.attacks = append(baseUnit.Attack.attacks, a)
} else {
baseUnit.Attack.attacks[r] += a
}
}
}
}
func (u *Unit) onUnpile(containing Permanent) {
u.PermanentBase.onUnpile(u)
if baseUnit, ok := containing.(*Unit); ok {
for r, a := range baseUnit.Attack.attacks {
if a == u.Attack.attacks[r] {
// Remove all further attacks since they must all be from the
// dropped unit
baseUnit.Attack.attacks = baseUnit.Attack.attacks[:r]
break
} else {
baseUnit.Attack.attacks[r] -= u.Attack.attacks[r]
}
}
}
}
func (u *Unit) onDrop(containing Permanent) {
u.onUnpile(containing)
}
|