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
|
package game
import (
"log"
"muhq.space/muhqs-game/go/utils"
)
const (
MAX_DRAW int = 3
)
type Player struct {
Id int
Name string
Turn int
Resource int
DrawPerTurn int
Hand *Hand
DiscardPile *DiscardPile
Deck *Deck
Store *Store
gameState *LocalState
Ctrl PlayerControl
knownStores map[Position]bool
}
// NewPlayerWithStore creates a new player.
func NewPlayerWithDeckAndStore(id int, name string, deck *Deck, store *Store, gameState *LocalState) *Player {
p := Player{
Id: id,
Name: name,
Turn: 0,
Resource: 0,
DrawPerTurn: MAX_DRAW,
Hand: NewHand(),
DiscardPile: NewDiscardPile(),
Deck: deck,
Store: store,
gameState: gameState,
Ctrl: nil,
knownStores: make(map[Position]bool),
}
return &p
}
// NewPlayer creates a new player moving its deck to the store.
// It initializes the players starting deck from the map's deck list if available.
func NewPlayer(id int, name string, deck *Deck, gameState *LocalState) *Player {
store := NewStore()
deck.MoveInto(store)
p := NewPlayerWithDeckAndStore(id, name, nil, store, gameState)
// Prepopulate the Deck
if gameState != nil {
if m := gameState.Map(); m != nil {
p.Deck = NewDeckFromDeckList(m.StartDeckList)
}
}
if p.Deck == nil {
p.Deck = NewDeck()
}
return p
}
// NewDraftPlayer returns a player struct only initialized with a name and an empty deck.
func NewDraftPlayer(name string) *Player {
p := Player{
Name: name,
Deck: NewDeck(),
}
return &p
}
func (p *Player) ResourceGain() int {
if p.Name == "The kraken" {
return p.gameState.Map().ResourceGain * (len(p.gameState.Players()) - 1)
} else {
return p.gameState.Map().ResourceGain
}
}
func (p *Player) UpkeepGain() int {
return p.ResourceGain() - p.UpkeepCost()
}
func (p *Player) UpkeepCost() int {
cost := 0
for _, u := range p.gameState.units {
if u.Controller() != p {
continue
}
cost += u.UpkeepCost()
}
return cost
}
func (p *Player) gainResource(gain int) {
p.Resource += gain
}
func (p *Player) reduceResource(amount int) {
p.Resource -= amount
if p.Resource < 0 {
p.Resource = 0
}
}
func (p *Player) upkeep() []*Player {
p.gainResource(p.ResourceGain())
// TODO: handle upkeep triggers
// Skip upkeep prompt if player does not controll any units
controllsUnits := false
for _, u := range p.gameState.units {
if u.Controller() == p {
controllsUnits = true
break
}
}
if !controllsUnits {
return nil
}
a, err := prompt(p.Ctrl, newUpkeepPrompt(p))
if err != nil {
// FIXME: handle error
}
if _, ok := a.(*PassPriority); ok {
a = newUpkeepAction(p)
}
// FIXME: upkeep action is special paying is part of its effect
p.gameState.declareAction(a)
return p.gameState.stack.resolve()
}
func (p *Player) actionPhase() []*Player {
for {
a, err := promptAction(p.Ctrl)
if err != nil {
// FIXME
}
if _, ok := a.(*PassPriority); ok {
return nil
}
if a == nil {
log.Fatal("Received nil action from ", p.Name)
}
p.gameState.declareAction(a)
w := p.gameState.stack.resolve()
if len(w) > 0 {
return w
}
}
}
func (p *Player) buyPhase() []*Player {
s := p.gameState
if p.Store.Size() == 0 && s.Map().HasStores() && len(p.knownStores) == 0 {
return nil
}
a, err := promptBuy(p.Ctrl)
if err != nil {
// FIXME
}
if _, ok := a.(*PassPriority); ok {
return nil
}
if a.Targets().HasSelections() {
s.declareAction(a)
return s.stack.resolve()
}
return nil
}
func (p *Player) discardHand() {
for _, c := range p.Hand.Cards() {
p.DiscardCard(c)
}
}
func (p *Player) DiscardCard(c *Card) {
p.Hand.MoveCard(c, p.DiscardPile)
}
func (p *Player) discardStep() {
if p.Hand.Size() == 0 {
return
}
cards := p.PromptHandCardSelection(0, p.Hand.Size())
for _, c := range cards {
p.DiscardCard(c)
}
}
func (p *Player) PromptHandCardSelection(min, max int) []*Card {
p.Ctrl.SendNotification(newHandCardSelectionPrompt(p, min, max))
_a, err := p.Ctrl.RecvAction()
if err != nil {
// FIXME
}
switch a := _a.(type) {
case *TargetSelection:
err := a.CheckTargets(p.gameState)
if err != nil {
log.Panicf("Invalid hand card selection: %v", err)
}
return utils.InterfaceSliceToTypedSlice[*Card](a.Target().sel)
case *PassPriority:
return nil
default:
log.Panicf("Unexpected response type %T for hand card selection", a)
}
return nil
}
func (p *Player) Draw() {
toDraw := p.DrawPerTurn - p.Hand.Size()
if toDraw < 1 {
return
}
p.DrawN(toDraw)
}
func (p *Player) DrawN(n int) {
drawnCards := p.Deck.Draw(n)
p.Hand.AddCards(drawnCards)
}
func (p *Player) IsEnemy(other *Player) bool {
// TODO: support coop / teams
return p != other
}
func (p *Player) AvailableStores() (stores []*Store) {
m := p.gameState.Map()
for pos, store := range m.Stores {
t := m.TileAt(pos)
if t.Permanent != nil && t.Permanent.Controller() == p {
stores = append(stores, store)
}
}
return
}
func (p *Player) KnowsStore(pos Position) bool {
return p.knownStores[pos]
}
func (p *Player) addKnownStore(pos Position) {
p.knownStores[pos] = true
}
func (p *Player) clearKnownStore() {
p.knownStores = make(map[Position]bool)
}
|