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

import (
	"fmt"
	"log"
)

type ActionPrototype interface {
	Specialize(...interface{}) Action
	String() string
}

type Action interface {
	Resolve(s *State)
	PayCosts(s *State) bool
	String() string
}

type PassPriorityAction struct{ Source *Player }

func (*PassPriorityAction) Resolve(*State)       {}
func (*PassPriorityAction) PayCosts(*State) bool { return true }
func (*PassPriorityAction) String() string       { return "pass" }
func NewPassPriorityAction(p *Player) Action     { return &PassPriorityAction{p} }

type ActionBase struct {
	Source      interface{}
	Card        *Card
	Targets     interface{}
	resolveFunc func(s *State)
	costFunc    func(s *State) bool
	desc        string
}

func (a *ActionBase) Resolve(s *State) {
	a.resolveFunc(s)
}

func (a *ActionBase) PayCosts(s *State) bool {
	return a.costFunc(s)
}

func (a *ActionBase) String() string {
	switch source := a.Source.(type) {
	case Permanent:
		p := source
		return fmt.Sprintf("%s: %s %s", p.GetController().Name, p.GetTile().Position.String(), a.desc)
	case *Player:
		return fmt.Sprintf("%s %s", source.Name, a.desc)
	default:
		return fmt.Sprintf("%v %s", source, a.desc)
	}
}

type PlayAction struct {
	ActionBase
}

func NewPlayAction(p *Player, c *Card, targets interface{}) Action {
	costFunc := func(*State) bool {
		cost := c.GetPlayCosts()

		if p.Resource < cost {
			return false
		}
		p.Resource -= cost
		return true
	}
	return NewPlayActionWithSpecialCosts(p, c, targets, costFunc)
}

func NewPlayActionWithSpecialCosts(p *Player, c *Card, targets interface{},
	costFunc func(*State) bool,
) Action {
	p.Hand.RemoveCard(c)

	resolveFunc := func(s *State) {
		s.ResolvePlay(p, c, targets)
	}

	desc := fmt.Sprintf("play %s@%v", c.Name, targets)
	return &PlayAction{ActionBase{p, c, targets, resolveFunc, costFunc, desc}}
}

type MoveAction struct {
	ActionBase
}

type MoveActionPrototype struct {
	Unit *Unit
}

func NewMoveActionPrototype(u *Unit) *MoveActionPrototype {
	return &MoveActionPrototype{u}
}

func (proto *MoveActionPrototype) String() string {
	return fmt.Sprintf("move %s", proto.Unit.Movement.String())
}

func (proto *MoveActionPrototype) Specialize(args ...interface{}) Action {
	target := args[0].(*Tile)
	u := proto.Unit

	resolveFunc := func(s *State) {
		u.move(s, target)
	}

	costFunc := func(*State) bool {
		if u.AvailMoveActions < 1 {
			return false
		}
		u.AvailMoveActions--
		return true
	}

	desc := fmt.Sprintf("-> %s", target.Position.String())
	return &MoveAction{ActionBase{u, u.GetCard(), target, resolveFunc, costFunc, desc}}
}

type AttackAction struct {
	ActionBase
}

type AttackActionPrototype struct {
	Unit *Unit
}

func NewAttackActionPrototype(u *Unit) *AttackActionPrototype {
	return &AttackActionPrototype{u}
}

func (proto *AttackActionPrototype) String() string {
	return fmt.Sprintf("attack %s", proto.Unit.Attack.String())
}

func (proto *AttackActionPrototype) Specialize(args ...interface{}) Action {
	target := args[0].(Permanent)
	u := proto.Unit

	resolveFunc := func(s *State) {
		s.Fight(u, target)
	}

	costFunc := func(*State) bool {
		if u.AvailAttackActions < 1 {
			return false
		}
		u.AvailAttackActions--
		return true
	}

	desc := fmt.Sprintf("x %s", target.GetTile().Position.String())
	return &AttackAction{ActionBase{u, u.GetCard(), target, resolveFunc, costFunc, desc}}
}

type FullActionPrototype struct {
	Unit *Unit
}

func NewFullActionPrototype(u *Unit) *FullActionPrototype {
	return &FullActionPrototype{u}
}

func (proto *FullActionPrototype) String() string {
	return "full_action"
}

func (proto *FullActionPrototype) Specialize(args ...interface{}) Action {
	u := proto.Unit
	resolveFunc := args[0].(func(s *State))
	costFunc := func(*State) bool {
		if u.AvailAttackActions < 1 && u.AvailMoveActions < 1 {
			return false
		}
		u.AvailAttackActions--
		u.AvailMoveActions--
		return true
	}
	desc := "full_action"
	return &ActionBase{u, u.GetCard(), nil, resolveFunc, costFunc, desc}
}

type FreeActionPrototype struct {
	Permanent  Permanent
	canPayFunc func(s *State) bool
}

func (proto *FreeActionPrototype) String() string {
	return "free_action"
}

func (proto *FreeActionPrototype) Specialize(args ...interface{}) Action {
	p := proto.Permanent
	resolveFunc := args[0].(func(s *State))
	costFunc := args[1].(func(s *State) bool)
	desc := "free_action"
	return &FreeAction{ActionBase{p, p.GetCard(), nil, resolveFunc, costFunc, desc}}
}

type FreeAction struct {
	ActionBase
}

type BuyAction struct {
	player *Player
	card   *Card
}

func (a *BuyAction) Resolve(s *State) {
	log.Println("Move", a.card.Name, "into", a.player.Name, "discard pile")
	a.player.Store.MoveCard(a.card, a.player.DiscardPile)
}

func (a *BuyAction) PayCosts(*State) bool {
	if a.card.BuyCost < 0 || a.card.BuyCost > a.player.Resource {
		return false
	}

	a.player.Resource -= a.card.BuyCost
	return true
}

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

func NewBuyAction(p *Player, c *Card) *BuyAction {
	return &BuyAction{p, c}
}

type UpkeepAction struct {
	player *Player
	unit   *Unit
	pay    bool
}

func (a *UpkeepAction) Resolve(s *State) {
	if a.pay {
		a.player.Resource -= a.unit.Upkeep
	} else {
		s.DestroyPermanent(a.unit)
	}
}

func (a *UpkeepAction) PayCosts(*State) bool {
	if a.unit.Upkeep > a.player.Resource {
		return false
	}

	a.player.Resource -= a.unit.Upkeep
	return true
}

func (a *UpkeepAction) String() string {
	if a.pay {
		return fmt.Sprintf("%s keep %s", a.player.Name, a.unit.Tile.Position.String())
	}

	return fmt.Sprintf("%s disband %s", a.player.Name, a.unit.Tile.Position.String())
}

func NewUpkeepAction(p *Player, u *Unit, pay bool) *UpkeepAction {
	return &UpkeepAction{p, u, pay}
}