-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-compiler.go
227 lines (216 loc) · 6.09 KB
/
game-compiler.go
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
package compiler
import (
"fmt"
"math/rand"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"time"
rp "github.com/steveoc64/republique5/proto"
)
// NewAccessCode generates a random access code and returns it as a string
func NewAccessCode() string {
a := make([]byte, 4, 4)
for i := 0; i < 4; i++ {
a[i] = byte('0' + rand.Intn(9))
}
return string(a)
}
// CompileGame reads a game file and returns a compiled game
func (c *Compiler) CompileGame(filename string) (*rp.Game, error) {
rand.Seed(time.Now().UnixNano())
lines, err := c.load(filename)
if err != nil {
return nil, fmt.Errorf("Error Loading %s: %s", filename, err.Error())
}
game := &rp.Game{
AdminAccess: NewAccessCode(),
TableX: 6,
TableY: 4,
}
tableMode := false
tableLine := int32(0)
println("Compiling Game", filename, "AdminAccess =", game.AdminAccess)
var currentTeam *rp.Team
indents := 1
var k int
var v string
// catch panics
defer func() {
if r := recover(); r != nil {
fmt.Println("ERROR:", r, "line:", k+1, "file:", filename, "\n->", v)
debug.PrintStack()
}
}()
// scan for !commands
for k, v = range lines {
if tableMode {
if tableLine == 0 && v[0] != '-' {
tableMode = false
} else {
if tableLine > 0 && tableLine <= game.TableY {
// v contains a row to add to the tableLayout
for int32(len(v)) < game.TableX {
v = v + " "
}
game.TableLayout = game.TableLayout + v
}
tableLine++
if tableLine > game.TableY+1 {
tableMode = false
}
continue
}
}
words := strings.Split(v, " ")
ww := len(words)
w := strings.ToLower(words[0])
if len(v) == 0 {
// empty lines are OK
continue
}
if strings.HasPrefix(v, "#") {
// is a comment
continue
}
if strings.HasPrefix(v, "!") {
w = w[1:]
switch w {
case "indent":
if ww != 2 {
return nil, Error{k + 1, filename, "!Indent Command - missing size"}
}
i, err := strconv.Atoi(words[1])
if err != nil || i < 1 {
return nil, Error{k + 1, filename, fmt.Sprintf("!Indent Command - invalid size '%v'", words[1])}
}
indents = i
continue
}
}
// work out the indent and base it off that
ii := 0
ioffset := 0
if strings.HasPrefix(v, " ") {
ioffset = countLeadingRune(v, ' ')
ii = ioffset / indents
}
if strings.HasPrefix(v, "\t") {
ioffset = countLeadingRune(v, '\t') / indents
ii = ioffset
}
switch ii {
case 0: // Directive
switch w {
case "name":
game.Name = strings.TrimSpace(v[4:])
case "scenario":
if len(words) != 2 {
return nil, Error{k + 1, filename, "Invalid Scenario Name"}
}
filename := filepath.Join(filepath.Dir(filename), "..", "scenarios", words[1]+".scenario")
scn, err := c.CompileScenario(filename)
if err != nil {
return nil, err
}
game.Scenario = scn
game.GameTime = scn.StartTime
for _, v := range scn.Teams {
v.AccessCode = NewAccessCode()
println("Team", v.Name, "AccessCode =", v.AccessCode)
}
case "table":
if len(words) != 2 {
return nil, Error{k + 1, filename, "expecting 'Table XxY' in feet"}
}
xpos := strings.Index(words[1], "x")
if xpos < 1 {
return nil, Error{k + 1, filename, "expecting 'Table XxY' in feet"}
}
x, err := strconv.Atoi(words[1][:xpos])
y, err2 := strconv.Atoi(words[1][xpos+1:])
if err != nil || err2 != nil {
return nil, Error{k + 1, filename, "expecting 'Table X Y' in feet"}
}
game.TableX = int32(x)
game.TableY = int32(y)
tableMode = true
// following this is a map
// line of dashes + Y lines of X chars + line of dashes
default: // is a team name
currentTeam = nil
teamname := words[0]
teamwords := strings.Split(v, " - ")
teamgamename := game.Name
teamside := rp.MapSide_FRONT
if len(teamwords) != 2 {
return nil, Error{k + 1,
filename,
fmt.Sprintf("invalid team name '%v' : expecting 'Team Name - Game Descriptnion (MapSide)' one of (Front, Top, Left, Right)", v)}
}
teamname = teamwords[0]
teamgamename = teamwords[1]
// extract the mapside
l1 := strings.Index(teamgamename, "(")
l2 := strings.Index(teamgamename, ")")
if l1 == -1 || l2 == -1 {
return nil, Error{k + 1,
filename,
fmt.Sprintf("invalid team name '%v' : expecting 'Team Name - Game Descriptnion (MapSide)' one of (Front, Top, Left, Right)", v)}
}
sidename := strings.ToLower(teamgamename[l1+1 : l2])
switch sidename {
case "front":
teamside = rp.MapSide_FRONT
case "top":
teamside = rp.MapSide_TOP
case "left":
teamside = rp.MapSide_LEFT_FLANK
case "right":
teamside = rp.MapSide_RIGHT_FLANK
default:
return nil, Error{k + 1,
filename,
fmt.Sprintf("invalid team name '%v' : expecting 'Team Name - Game Descriptnion (MapSide)' one of (Front, Top, Left, Right)", v)}
}
teamgamename = teamgamename[:l1-1]
for _, team := range game.Scenario.Teams {
if team.Name == teamname {
currentTeam = team
team.GameName = teamgamename
team.Side = teamside
break
}
}
if currentTeam == nil {
return nil, Error{k + 1, filename, fmt.Sprintf("Unknown Team '%v'", words[0])}
}
println("processing team", currentTeam.Name)
}
case 1: // Player command
if currentTeam == nil {
return nil, Error{k + 1, filename, "No Team Defined"}
}
player := &rp.Player{AccessCode: NewAccessCode()}
v = strings.TrimSpace(v)
names := strings.Split(v, ",")
for _, name := range names {
name = strings.TrimSpace(name)
cc := currentTeam.GetCommandByName(name)
if cc == nil {
cc = currentTeam.GetCommandByCommanderName(name)
if cc == nil {
return nil, Error{k + 1, filename, fmt.Sprintf("%v is not a valid unit or commander in team %v", name, currentTeam.Name)}
}
}
player.Commanders = append(player.Commanders, cc.CommanderName)
}
currentTeam.Players = append(currentTeam.Players, player)
default:
return nil, Error{k + 1, filename, fmt.Sprintf("Dont know what to do with a line at indent level %d '%v", ii, v)}
}
}
game.GenerateIDs()
return game, nil
}