aboutsummaryrefslogtreecommitdiff
path: root/go/game/targets.go
blob: 1a8c7ab3667f14aceac28187ed65459ba04ddc19 (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
package game

import (
	"fmt"
	"log"
	"strconv"
	"strings"

	"golang.org/x/exp/slices"
	"muhq.space/muhqs-game/go/utils"
)

type (
	TargetConstrainFunc func(interface{}) error
	TargetConstrain     struct {
		desc      string
		constrain TargetConstrainFunc
	}
)

type Targets struct {
	s          *State
	action     Action
	constrains []*TargetConstrain
	sel        []interface{}
}

func fmtSel(sel interface{}) string {
	switch t := sel.(type) {
	case *Tile:
		return t.Position.String()
	default:
		return fmt.Sprintf("%d", t)
	}
}

func (t *Targets) String() string {
	if len(t.sel) == 0 {
		return "undecided targets"
	}

	if len(t.sel) == 1 {
		return fmt.Sprintf("%s", fmtSel(t.sel[0]))
	}

	s := "["
	for _, sel := range t.sel {
		s += fmt.Sprintf("%s ", fmtSel(sel))
	}
	return s[:len(s)-1] + "]"
}

func newTargetsWithSel(s *State, targets []string, action Action, sel []interface{}) *Targets {
	t := &Targets{
		s:   s,
		sel: sel,
	}

	t.buildConstrains(targets, action)
	return t
}

func newTargets(s *State, targets []string, action Action) *Targets {
	return newTargetsWithSel(s, targets, action, make([]interface{}, 0, len(targets)))
}

func (t *Targets) buildConstrains(targets []string, action Action) {
	for _, desc := range targets {
		c := &TargetConstrain{desc, targetConstrain(desc, t.s, action)}
		t.constrains = append(t.constrains, c)
	}
}

func (t *Targets) RequireTargets() bool {
	return len(t.constrains) != len(t.sel)
}

func (t *Targets) Options() [][]interface{} {
	options := make([][]interface{}, 0, len(t.constrains))
	for _, c := range t.constrains {
		options = append(options, c.options(t.s))
	}
	return options
}

func (t *Targets) NextOptions() []interface{} {
	next := len(t.sel)
	return t.constrains[next].options(t.s)
}

func (t *Targets) AddTarget(target interface{}) {
	t.sel = append(t.sel, target)
}

func (t *Targets) CheckTargets(s *State) error {
	if t.RequireTargets() {
		return fmt.Errorf("Number of selected targets and required ones does not match")
	}

	for i, c := range t.constrains {
		sel := t.sel[i]
		if err := c.constrain(sel); err != nil {
			return fmt.Errorf("selected target %s does not fit its desciption %s: %s",
				sel, c.desc, err)
		}
	}

	return nil
}

func targetConstrain(desc string, s *State, action Action) TargetConstrainFunc {
	constrains := []TargetConstrainFunc{}
	if strings.Contains(desc, "unit") {
		constrains = append(constrains, parseUnitTargetConstrain(desc, s, action)...)
	} else if strings.Contains(desc, "artifact") {
		constrains = append(constrains, parseArtifactTargetConstrain(desc, s, action)...)
	} else if strings.Contains(desc, "tile") {
		constrains = append(constrains, parseTileTargetConstrain(desc, s, action)...)
		// } else if strings.Contains("action") {
		// contrains = append(contrains, parseActionTargetConstrain(desc, s, action)...)
		// } else if strings.Contains("spell") {
		// contrains = append(contrains, parseSpellTargetConstrain(desc, s, action)...)
	}

	return func(t interface{}) error {
		for _, constrain := range constrains {
			if err := constrain(t); err != nil {
				return err
			}
		}

		return nil
	}
}

func enemyPermanentTargetConstrain(action Action) TargetConstrainFunc {
	var sourceController *Player
	switch source := action.Source().(type) {
	case *Player:
		sourceController = source
	case Permanent:
		sourceController = source.Controller()
	default:
		log.Fatalf("Unhandled source type %T in enemyPermanentTargetConstrain", source)
	}

	return func(t interface{}) (err error) {
		p, _ := t.(Permanent)
		if !p.Controller().IsEnemy(sourceController) {
			err = fmt.Errorf("Controller %s of target %t is not an enemy of %s",
				p.Controller(), t, sourceController)
		}
		return
	}
}

func rangeTargetConstrain(source interface{}, r int) TargetConstrainFunc {
	var sourcePos Position
	switch source := source.(type) {
	case *Tile:
		sourcePos = source.Position
	case Permanent:
		sourcePos = source.Tile().Position
	default:
		log.Fatalf("Unhandled source type %T in rangeTargetConstrain", source)
	}

	return func(t interface{}) (err error) {
		p, _ := t.(Permanent)
		if !IsPositionInRange(sourcePos, p.Tile().Position, r) {
			err = fmt.Errorf("Position %s of target %s not in range %d of source's position %s",
				p.Tile().Position, t, r, sourcePos)
		}
		return
	}
}

var unitTargetConstrain TargetConstrainFunc = func(t interface{}) (err error) {
	_, ok := t.(*Unit)
	if !ok {
		err = fmt.Errorf("target is %T not *Unit", t)
	}
	return
}

func parseUnitTargetConstrain(desc string, s *State, action Action) []TargetConstrainFunc {
	constrains := []TargetConstrainFunc{unitTargetConstrain}

	if strings.Contains(desc, "enemy unit") {
		constrains = append(constrains, enemyPermanentTargetConstrain(action))
	}

	if strings.Contains(desc, " in range ") {
		tokens := strings.SplitN(desc, " in range ", 2)
		r, err := strconv.Atoi(tokens[1])
		if err != nil {
			log.Fatalf("Invalid range %s in target constrain %s", tokens[1], desc)
		}
		constrains = append(constrains, rangeTargetConstrain(action, r))
	}

	return constrains
}

var artifactTargetConstrain TargetConstrainFunc = func(t interface{}) (err error) {
	_, ok := t.(*Artifact)
	if !ok {
		err = fmt.Errorf("target is %T not *Artifact", t)
	}
	return
}

func parseArtifactTargetConstrain(constrain string, s *State, action Action) []TargetConstrainFunc {
	constrains := []TargetConstrainFunc{artifactTargetConstrain}
	return constrains
}

var tileTargetConstrain TargetConstrainFunc = func(t interface{}) (err error) {
	_, ok := t.(*Tile)
	if !ok {
		err = fmt.Errorf("target is %T not *Tile", t)
	}
	return
}

func parseTileTargetConstrain(desc string, s *State, action Action) []TargetConstrainFunc {
	log.Println("parsing", desc)
	constrains := []TargetConstrainFunc{tileTargetConstrain}

	if moveAction, ok := action.(*MoveAction); ok {
		u := moveAction.Source().(*Unit)
		constrains = append(constrains, func(t interface{}) (err error) {
			tile := t.(*Tile)
			if slices.Contains(u.MoveRangeTiles(s.Map), tile) {
				return nil
			}
			return fmt.Errorf("tile %s is not in %d movement range", tile)
		})
	}

	var player *Player
	switch a := action.(type) {
	case *PlayAction:
		player = a.Source().(*Player)
	}

	var card *Card
	switch a := action.(type) {
	case *PlayAction:
		card = a.Card
	case *MoveAction:
		card = a.Card
	}

	if strings.Contains(desc, "spawn") {
		if player == nil {
			log.Fatalf("Not implemented spawn tile constrain for %T", action)
		}

		constrains = append(constrains, func(t interface{}) (err error) {
			tile := t.(*Tile)
			if slices.Contains(s.AvailableSpawnTiles(player, card), tile) {
				return nil
			}
			return fmt.Errorf("tile %s is no spawn tile", tile)
		})
	}

	if strings.Contains(desc, "available") {
		constrains = append(constrains, func(t interface{}) (err error) {
			tile := t.(*Tile)

			if card == nil {
				log.Fatalf("Not implemented availability tile constrain for %T", action)
			}

			if tile.IsAvailableForCard(card) {
				return nil
			}
			return fmt.Errorf("tile %s is not available for %s", tile, card.Name)
		})
	}

	return constrains
}

func (c *TargetConstrain) options(s *State) (options []interface{}) {
	candidates := targetCandidates(c.desc, s)

	for _, candidate := range candidates {
		if err := c.constrain(candidate); err == nil {
			options = append(options, candidate)
		}
	}
	return
}

func targetCandidates(desc string, s *State) []interface{} {
	if strings.Contains(desc, "unit") || strings.Contains(desc, "artifact") {
		return utils.TypedSliceToInterfaceSlice(s.Permanents)
	} else if strings.Contains(desc, "tile") {
		return utils.TypedSliceToInterfaceSlice(s.Map.AllTiles())
	}

	log.Fatalf("Unimplemented target options for %s", desc)
	return nil
}