aboutsummaryrefslogtreecommitdiff
path: root/go/game/cardParsing.go
blob: 58dea59400edd7f3af1c60b87e7fa755ed39b9f6 (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
package game

import (
	"log"
	"regexp"
	"strconv"
	"strings"
)

type dynamicCardImplementation struct {
	card           *Card
	spawnTilesFunc func(*State, *Player) []*Tile
	genFullActions func(*Unit) []*FullAction
	genFreeActions func(Permanent) []*FreeAction

	targetDesc TargetDesc

	_stateBasedActions func(*State, Permanent)

	_additionalSpawnsFor func(Permanent, CardType) []*Tile

	_onEntering func(*Tile)
	_onLeaving  func(*Tile)
	_onPlay     func(*PlayAction)
	_onETB      func(*State, Permanent)
	_onPile     func(Permanent)
	_onUnpile   func(Permanent)
	_onDrop     func(Permanent)
}

func (impl *dynamicCardImplementation) spawnTiles(s *State, p *Player) []*Tile {
	if impl.spawnTilesFunc != nil {
		return impl.spawnTilesFunc(s, p)
	}
	return nil
}

func (impl *dynamicCardImplementation) fullActions(u *Unit) []*FullAction {
	if impl.genFullActions != nil {
		return impl.genFullActions(u)
	}
	return nil
}

func (impl *dynamicCardImplementation) freeActions(p Permanent) []*FreeAction {
	if impl.genFreeActions != nil {
		return impl.genFreeActions(p)
	}
	return nil
}

func (impl *dynamicCardImplementation) playTargets() TargetDesc {
	return impl.targetDesc
}

func (impl *dynamicCardImplementation) stateBasedActions(s *State, p Permanent) {
	if impl._stateBasedActions != nil {
		impl._stateBasedActions(s, p)
	}
}

func (impl *dynamicCardImplementation) additionalSpawnsFor(p Permanent, ct CardType) []*Tile {
	if impl._additionalSpawnsFor != nil {
		return impl._additionalSpawnsFor(p, ct)
	}
	return nil
}

func (impl *dynamicCardImplementation) onEntering(t *Tile) {
	if impl._onEntering != nil {
		impl._onEntering(t)
	}
}

func (impl *dynamicCardImplementation) onLeaving(t *Tile) {
	if impl._onLeaving != nil {
		impl._onLeaving(t)
	}
}

func (impl *dynamicCardImplementation) onPlay(a *PlayAction) {
	if impl._onPlay != nil {
		impl._onPlay(a)
	}
}

func (impl *dynamicCardImplementation) onETB(s *State, p Permanent) {
	if impl._onETB != nil {
		impl._onETB(s, p)
	}
}

func (impl *dynamicCardImplementation) onPile(p Permanent) {
	if impl._onPile != nil {
		impl._onPile(p)
	}
}

func (impl *dynamicCardImplementation) onUnpile(p Permanent) {
	if impl._onUnpile != nil {
		impl._onUnpile(p)
	}
}

func (impl *dynamicCardImplementation) onDrop(p Permanent) {
	if impl._onDrop != nil {
		impl._onDrop(p)
	}
}

func newParsedCardImplementation(c *Card) *dynamicCardImplementation {
	impl := &dynamicCardImplementation{card: c}
	for _, effect := range c.getEffects() {
		impl.parseEffect(effect)
	}

	for _, fullAction := range c.getCanonicalValues("full_action") {
		impl.parseFullAction(fullAction)
	}

	return impl
}

func (impl *dynamicCardImplementation) parseEffect(effect string) {
	if strings.Contains(effect, "can be played") {
		impl.parsePlayModification(effect)
	}
}

func (impl *dynamicCardImplementation) parseFullAction(fullAction string) {
	var resolveProtos []ActionFuncPrototype
	gainRessourceRegex := regexp.MustCompile(`gain [1-9][0-9]* resource`)

	if find := gainRessourceRegex.Find([]byte(fullAction)); find != nil {
		tokens := strings.SplitN(string(find), " ", 3)
		gain, err := strconv.Atoi(tokens[1])
		if err != nil {
			log.Panicf("Failed to parse resource gain full action: %s", err)
		}

		resolveProto := func(a Action) ActionResolveFunc {
			u := a.Source().(*Unit)
			return func(*State) {
				u.controller.gainResource(gain)
			}
		}
		resolveProtos = append(resolveProtos, resolveProto)
	}

	if resolveProtos != nil {
		impl.genFullActions = func(u *Unit) []*FullAction {
			fullActions := make([]*FullAction, 0, len(resolveProtos))

			for _, proto := range resolveProtos {
				fullActions = append(fullActions, newFullAction(u, proto, ""))
			}

			return fullActions
		}
	}
}

func parseUnitConstrain(constrain string) func(ctx interface{}, u *Unit) bool {
	enemy := strings.Contains(constrain, "enemy")
	if enemy {
		return func(ctx interface{}, e *Unit) bool {
			p := ctx.(*Player)
			return p.IsEnemy(e.Controller())
		}
	} else {
		log.Println("Only enemy unit constrains are implemented yet")
	}

	return nil
}

func parseLocation(location string) func(interface{}, *State) []*Tile {
	r := -1
	var what string
	if strings.Contains(location, "on") {
		r = 0
		tokens := strings.SplitN(location, "on ", 2)
		what = tokens[1]

	} else if strings.Contains(location, "next to") {
		r = 1
		tokens := strings.SplitN(location, "next to ", 2)
		what = tokens[1]

	} else if strings.Contains(location, "in range") {
		tokens := strings.SplitN(location, "in range ", 2)
		afterRangePattern := tokens[1]

		var rangeStr string
		tokens = strings.SplitN(afterRangePattern, " ", 2)
		rangeStr, what = tokens[0], tokens[1]

		var err error
		r, err = strconv.Atoi(rangeStr)
		if err != nil {
			log.Panicf("Unsupported range pattern \"%s\" in \"%s\"", "range "+rangeStr, location)
		}
	}

	if r == -1 || what == "" {
		return nil
	}

	if strings.Contains(what, "unit") {
		constrainFunc := parseUnitConstrain(what)
		if constrainFunc != nil {
			return func(ctx interface{}, s *State) []*Tile {
				origins := s.Map.FilterTiles(func(t *Tile) bool {
					if t.Permanent != nil &&
						t.Permanent.Card().Type == CardTypes.Unit &&
						constrainFunc(ctx, t.Permanent.(*Unit)) {
						return true
					}
					return false
				})

				if r == 0 {
					return origins
				} else {
					tiles := []*Tile{}
					for _, origin := range origins {
						tiles = append(tiles, TilesInRangeFromOrigin(s.Map, origin.Position, r)...)
					}
					return tiles
				}
			}
		}
	}
	log.Println("Only unit location constrains are implemented yet")
	return nil
}

func (impl *dynamicCardImplementation) parsePlayModification(effect string) {
	tokens := strings.SplitN(effect, " can be played ", 2)
	log.Println(tokens)
	what, how := tokens[0], tokens[1]
	if what == strings.ToLower(impl.card.Name) {
		if locationFunc := parseLocation(how); locationFunc != nil {
			impl.spawnTilesFunc = func(p *State, s *Player) []*Tile { return locationFunc(s, p) }
		} else {
			log.Println("Play modification other than locations are not implemented ")
		}
	} else {
		log.Println(effect)
		log.Println("Play modification for other cards are not implemented")
	}
}