aboutsummaryrefslogtreecommitdiff
path: root/go/game/unit.go
blob: 921be3f00060affb675a6485a6261356f6dbbe71 (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
package game

import (
	"log"
)

const (
	DEFAULT_AVAIL_STREET_ACTIONS = 1
	DEFAULT_AVAIL_MOVE_ACTIONS   = 1
	DEFAULT_AVAIL_ATTACK_ACTIONS = 1
)

type Unit struct {
	permanentBase
	Health      int
	Movement    Movement
	Attack      Attack
	upkeep      *ResourceCosts
	FullActions []*FullAction
	FreeActions []*FreeAction
	// Effects []Effects
	AvailStreetActions      int
	AvailMoveActions        int
	additionalMoveActions   int
	AvailAttackActions      int
	additionalAttackActions int
}

func NewUnit(card *Card, tile *Tile, owner *Player) *Unit {
	if !card.Type.IsUnit() {
		log.Panic(card.Name, " is not a unit")
	}
	var movement Movement
	movementDesc, found := card.Values["movement"]
	if !found {
		movement = INVALID_MOVEMENT()
	} else {
		movement = parseMovement(movementDesc)
	}

	upkeep := card.PlayCosts
	if upkeep == nil {
		upkeep = &ResourceCosts{fixed: 0}
	}

	var attack Attack
	if attackDesc, found := card.Values["attack"]; found {
		attack = parseAttack(attackDesc)
	}

	u := &Unit{
		permanentBase:      newPermanentBase(card, tile, owner),
		Health:             int(card.Values["health"].(uint64)),
		Movement:           movement,
		Attack:             attack,
		upkeep:             upkeep,
		AvailMoveActions:   DEFAULT_AVAIL_MOVE_ACTIONS,
		AvailStreetActions: DEFAULT_AVAIL_STREET_ACTIONS,
		AvailAttackActions: DEFAULT_AVAIL_ATTACK_ACTIONS,
	}

	// TODO: parse effect for additional actions

	u.FullActions = card.Impl.fullActions(u)
	u.FreeActions = card.Impl.freeActions(u)

	return u
}

func (u *Unit) String() string {
	return FmtPermanent(u)
}

func NewUnitFromPath(cardPath string, tile *Tile, owner *Player) *Unit {
	return NewUnit(NewCard(cardPath), tile, owner)
}

func (u *Unit) UpkeepCost() int {
	return u.upkeep.Costs(u.controller.gameState)
}

func (u *Unit) resetBaseActions() {
	u.AvailStreetActions = DEFAULT_AVAIL_STREET_ACTIONS + u.additionalMoveActions
	u.AvailMoveActions = DEFAULT_AVAIL_MOVE_ACTIONS + u.additionalMoveActions
	u.AvailAttackActions = DEFAULT_AVAIL_ATTACK_ACTIONS + u.additionalAttackActions
}

func (u *Unit) onUpkeep() {
	u.resetBaseActions()
	getCardImplementation(u.Card()).onUpkeep(u)

	if u.Marks(UnitStates.Poison) > 0 {
		u.adjustMarks(UnitStates.Poison, 1)
	}

	if u.Marks(UnitStates.Paralysis) > 0 {
		u.adjustMarks(UnitStates.Paralysis, -1)
		u.tap()
	}
}

func (u *Unit) MoveRangeTiles() []*Tile {
	tiles := []*Tile{}
	m := u.controller.gameState.Map()
	graph := m.generateMovementRangeGraphFor(u)
	origin := TileOrContainingPermTile(u).Position
	for _, pos := range PositionsInRange(origin, 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) AttackableTile(t *Tile) (bool, Attack) {
	if flexAttack := u.Attack.flexAttack; flexAttack != nil {
		if _, ok := flexAttack(u, t); ok {
			return true, u.Attack
		}
	}

	return IsPositionInRange(u.Tile().Position, t.Position, u.Attack.MaxRange()), u.Attack
}

func (u *Unit) AttackableTiles() []*Tile {
	// Units not on the map can not attack
	if u.tile == nil {
		return nil
	}

	m := u.controller.gameState.Map()
	var tilesInRange []*Tile
	if flexAttack := u.Attack.flexAttack; flexAttack != nil {
		tilesInRange = m.FilterTiles(func(t *Tile) bool { _, ok := flexAttack(u, t); return ok })
	} else {
		tilesInRange = TilesInRange(m, u, u.Attack.MaxRange())
		if u.Attack.MaxRange() == 1 {
			return tilesInRange
		}
	}

	// House tiles are not attackable from ranges > 1
	aTiles := []*Tile{}
	for _, t := range tilesInRange {
		if DistanceBetweenPositions(t.Position, u.tile.Position) > 1 && t.Type == TileTypes.House {
			continue
		}
		aTiles = append(aTiles, t)
	}
	return aTiles
}

func (u *Unit) AttackablePermanents() []Permanent {
	permanents := []Permanent{}
	for _, t := range u.AttackableTiles() {
		if t.Permanent != nil && t.Permanent.Attackable() {
			permanents = append(permanents, t.Permanent)
		}
	}
	return permanents
}

func (u *Unit) AttackableEnemyPermanents() []Permanent {
	controller := u.Controller()
	permanents := []Permanent{}
	for _, t := range u.AttackableTiles() {
		if t.Permanent != nil && t.Permanent.Attackable() &&
			t.Permanent.Controller() != controller {

			permanents = append(permanents, t.Permanent)
		}
	}
	return permanents
}

func (u *Unit) fight(p Permanent) {
	d, reachable := u.Attack.DamageForTile(u, p.Tile())
	if reachable {
		DealDamage(u, p, d)
	}
}

func (u *Unit) IsDestroyed() bool {
	return u.damage >= u.Health || u.Marks(UnitStates.Poison) >= u.Health*2
}

func (u *Unit) HasFullAction() bool {
	return len(u.FullActions) > 0
}

func (u *Unit) tap() {
	u.AvailMoveActions = 0
	u.AvailAttackActions = 0
}

func (u *Unit) AvailSlowActions() (actions []Action) {
	// TODO: implement panic and rage marks
	if u.Marks(UnitStates.Panic) > 0 {
	}
	if u.Marks(UnitStates.Rage) > 0 {
	}

	if u.AvailMoveActions > 0 && u.Movement != INVALID_MOVEMENT() {
		actions = append(actions, NewMoveAction(u))
	}

	if u.AvailStreetActions > 0 && u.Movement != INVALID_MOVEMENT() && u.Tile() != nil && u.Tile().Type == TileTypes.Street {
		actions = append(actions, NewStreetAction(u))
	}

	m := u.Controller().gameState.Map()
	if u.AvailAttackActions > 0 && u.Attack.Valid() &&
		len(u.AttackableEnemyPermanents()) > 0 {

		actions = append(actions, NewAttackAction(u))
	}

	if u.AvailAttackActions > 0 && u.AvailMoveActions > 0 {
		for _, p := range u.FullActions {
			actions = append(actions, p)
		}
	}

	if u.AvailAttackActions > 0 || u.AvailMoveActions > 0 {
		for _, t := range TilesInRange(m, u, 1) {
			if t.Permanent == nil {
				continue
			}

			// Equipment Actions
			if equipment, ok := t.Permanent.(*Equipment); ok {
				actions = append(actions, NewEquipAction(u, equipment))
			}

			for _, perm := range t.Permanent.Pile() {
				if equipment, ok := perm.(*Equipment); ok {
					actions = append(actions, NewEquipAction(u, equipment))
				}
			}

			// Artifact Actions
			if t.Permanent.Card().Type.IsArtifact() {
				// ArtifactMoveActions are FullActions
				if u.AvailAttackActions > 0 && u.AvailMoveActions > 0 {
					actions = append(actions, newArtifactMoveAction(u, t.Permanent))
				}

				// ArtifactSwitchActions replace the MoveAction
				if u.AvailMoveActions > 0 {
					// Only generate an ArtifactSwitchAction if the current
					// tiles are suitable for both permanents
					if t.Permanent != nil && t.Permanent.Card().Type.IsArtifact() &&
						t.IsSuitableForCard(u.Card()) && u.Tile().IsSuitableForCard(t.Permanent.Card()) {
						actions = append(actions, newArtifactSwitchAction(u, t.Permanent))
					}
				}
			}
		}
	}

	return actions
}

func (u *Unit) AvailFreeActions() (actions []Action) {
	for _, p := range u.FreeActions {
		actions = append(actions, p)
	}
	return
}

func (u *Unit) CurrentlyAvailActions() (actions []Action) {
	s := u.Controller().gameState
	if s.IsActivePlayer(u.Controller()) && s.ActivePhase() == Phases.ActionPhase {
		actions = append(actions, u.AvailSlowActions()...)
	}
	actions = append(actions, u.AvailFreeActions()...)
	return
}

func (u *Unit) AvailActions() (actions []Action) {
	actions = append(actions, u.AvailSlowActions()...)
	actions = append(actions, u.AvailFreeActions()...)
	return
}

func (u *Unit) onPile(containing Permanent) {
	u.permanentBase.onPile(containing)
	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)
}

func (u *Unit) removeFullAction(tag string) {
	for i, fa := range u.FullActions {
		if fa.tag != tag {
			continue
		}
		u.FullActions[i] = u.FullActions[len(u.FullActions)-1]
		u.FullActions = u.FullActions[:len(u.FullActions)-1]
		break
	}
}

func (u *Unit) adjustDamage(damage int) {
	if damage > 0 && u.Marks(UnitMarks.Ward) > 0 {
		u.adjustMarks(UnitMarks.Ward, -1)
		return
	}

	u.permanentBase.adjustDamage(damage)
}

func (u *Unit) equip(e *Equipment) {
	if e.containingPerm != nil {
		removePermanentFromPile(e.containingPerm, e)
	} else {
		leaveTileOrPile(e)
	}
	addPermanentToPile(u, e)
}