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
|
package game
import (
"log"
"strconv"
"strings"
)
type parsedCardImplementation struct {
card *Card
spawnTilesFunc func(*State, *Player) []*Tile
genFullActionPrototypes func(*Unit) []*FullActionPrototype
genFreeActionPrototypes func(Permanent) []*FreeActionPrototype
}
func (impl *parsedCardImplementation) spawnTiles(s *State, p *Player) []*Tile {
if impl.spawnTilesFunc != nil {
return impl.spawnTilesFunc(s, p)
}
return nil
}
func (impl *parsedCardImplementation) fullActionPrototypes(u *Unit) []*FullActionPrototype {
if impl.genFullActionPrototypes != nil {
return impl.genFullActionPrototypes(u)
}
return nil
}
func (impl *parsedCardImplementation) freeActionPrototypes(p Permanent) []*FreeActionPrototype {
if impl.genFreeActionPrototypes != nil {
return impl.genFreeActionPrototypes(p)
}
return nil
}
func newParsedCardImplementation(c *Card) *parsedCardImplementation {
impl := &parsedCardImplementation{card: c}
for _, effect := range c.getEffects() {
impl.parseEffect(effect)
}
return impl
}
func (impl *parsedCardImplementation) parseEffect(effect string) {
if strings.Contains(effect, "can be played") {
impl.parsePlayModification(effect)
}
}
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.Fatalf("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 *parsedCardImplementation) 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")
}
}
|