-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
393 lines (363 loc) · 8.6 KB
/
main.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//lightriders visualizer with termbox console drawing
package main
import (
"bufio"
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path"
"time"
"github.com/jessevdk/go-flags"
"github.com/nsf/termbox-go"
"github.com/vendelin8/lightriders-starterbot-golang/utils"
log "gopkg.in/inconshreveable/log15.v2"
)
var (
opts Options
w, h int //width and height of the termbox
index int //the current step
bg, fg termbox.Attribute
replayReder *bufio.Reader
ticker *time.Ticker
tickerC <-chan time.Time
top, left int //left and top of the centered field
p0, p1 *Player
running bool
rf utils.ReplayFormat
maxDebugLens []int
debugValues [][]string
debugMiddle int
)
type Options struct {
File string `short:"f" long:"file" description:"file to replay, last one will used if empty"`
Delay int `short:"d" long:"delay" description:"delay in millisec between turns" default:"600"`
InvCol bool `short:"i" long:"invert-colors" description:"invert colors for dark terminal backgrounds"`
}
type Player struct {
utils.Player
Moves []utils.Direction
Fg termbox.Attribute
}
func (p *Player) Fill() {
//fills the player's position with it's id
termbox.SetCell(left+p.X, top+p.Y, rune(p.IdStr), p.Fg, bg)
}
func (p *Player) Move2() {
//moves, fills and updates last move
p.Move()
p.Fill()
p.LastMove = p.Moves[index]
}
func (p *Player) MoveFill(indexDiff int) {
//draws "x" to current position, player's id to the next one
var toDraw rune
if index > 1 {
lastLast := p.Moves[index+indexDiff-2]
switch p.LastMove {
case utils.Up:
if lastLast == utils.Right {
toDraw = '┘'
} else if lastLast == utils.Left {
toDraw = '└'
} else {
toDraw = '│'
}
case utils.Left:
if lastLast == utils.Up {
toDraw = '┐'
} else if lastLast == utils.Down {
toDraw = '┘'
} else {
toDraw = '─'
}
case utils.Down:
if lastLast == utils.Right {
toDraw = '┐'
} else if lastLast == utils.Left {
toDraw = '┌'
} else {
toDraw = '│'
}
case utils.Right:
if lastLast == utils.Up {
toDraw = '┌'
} else if lastLast == utils.Down {
toDraw = '└'
} else {
toDraw = '─'
}
}
} else {
toDraw = 'x'
}
termbox.SetCell(left+p.X, top+p.Y, toDraw, p.Fg, bg)
p.Move2()
}
func (p *Player) UnMove() {
//undos one move
termbox.SetCell(left+p.X, top+p.Y, '.', fg, bg)
p.LastMove = p.Moves[index].Reverse()
p.Move2()
}
func main() {
var err error
if _, err = flags.ParseArgs(&opts, os.Args); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
log.New() //initializing logger
handler := log.Must.FileHandler("log.txt", log.LogfmtFormat())
log.Root().SetHandler(log.MultiHandler(
log.LvlFilterHandler(log.LvlWarn, log.CallerStackHandler("%+v", handler)),
log.MatchFilterHandler("lvl", log.LvlInfo, handler)))
defer func() { //startup error check
if r := recover(); r != nil {
log.Error("main", "error", r)
panic(r)
}
}()
if len(opts.File) == 0 { //searching for last replay
var fileInfos []os.FileInfo
if fileInfos, err = ioutil.ReadDir(utils.REPLAY_DIR); err != nil {
panic(err)
}
latest := time.Now().AddDate(-100, 0, 0)
for _, info := range fileInfos { //loading languages
name := path.Join(utils.REPLAY_DIR, info.Name())
s, _ := os.Stat(name)
if s.IsDir() {
continue
}
if s.ModTime().After(latest) {
opts.File = name
latest = s.ModTime()
}
}
}
var b []byte //parsing replay file
if b, err = ioutil.ReadFile(opts.File); err != nil {
panic(err)
}
bs := bytes.Split(b, []byte{utils.REPLAY_SEPARATOR})
if err = json.Unmarshal(bs[0], &rf); err != nil {
panic(err)
}
if err = termbox.Init(); err != nil { //termbox console drawing init
panic(err)
}
defer termbox.Close()
bg = termbox.ColorDefault
if opts.InvCol {
fg = termbox.ColorWhite
} else {
fg = termbox.ColorBlack
}
w, h = termbox.Size()
//key info to the screen
keys := []string{"quit", "Ctrl+C, Esc",
"back 1 turn", "Left",
"back 10 turns", "Down",
"forward 1 turn", "Right",
"forward 10 turns", "Up",
"play/pause", "Space",
"-10% speed", "[",
"+10% speed", "]",
"half speed", "{",
"double speed", "}"}
var maxLeft, maxRight, i, j int
for i = 0; i < len(keys); i += 2 { //getting max lengths for padding
if len(keys[i]) > maxLeft {
maxLeft = len(keys[i])
}
if len(keys[i+1]) > maxRight {
maxRight = len(keys[i+1])
}
}
left = w - maxLeft - maxRight - 2
middle := w - maxRight
for i = 0; i < len(keys); i += 2 {
printStr(left, i/2, keys[i])
termbox.SetCell(middle-2, i/2, ':', fg, bg)
printStr(middle, i/2, keys[i+1])
}
var key string
for _, key = range utils.DEBUG_VARS { //getting padding for debug vars
if len(key) > debugMiddle {
debugMiddle = len(key)
}
}
for i, key = range utils.DEBUG_VARS { //printing debug vars' label
printStr(i, 0, key)
termbox.SetCell(debugMiddle, i, ':', fg, bg)
printStr(middle, i/2, keys[i+1])
}
debugMiddle += 2
rounds := len(bs) - 1
p0 = new(Player) //init players
p0.X = rf.OppX
p0.Y = rf.OppY
p0.IdStr = '0'
p0.Moves = make([]utils.Direction, rounds)
p0.Fg = termbox.ColorGreen
p1 = new(Player)
p1.X = rf.OwnX
p1.Y = rf.OwnY
p1.IdStr = '1'
p1.Moves = make([]utils.Direction, rounds)
p1.Fg = termbox.ColorRed
var rm utils.ReplayMove
maxDebugLens = make([]int, len(utils.DEBUG_VARS))
debugValues = make([][]string, rounds)
for i = 0; i < rounds; i++ { //load moves
if err = json.Unmarshal(bs[i+1], &rm); err != nil {
panic(err)
}
p0.Moves[i] = rm.OppMove
p1.Moves[i] = rm.OwnMove
debugValues[i] = make([]string, len(utils.DEBUG_VARS))
for j, key = range rm.Others {
if len(key) > maxDebugLens[j] {
maxDebugLens[j] = len(key)
}
debugValues[i][j] = key
}
}
p0.LastMove = p0.Moves[0]
p1.LastMove = p1.Moves[0]
left = 0 //init map with dots to the middle
for _, i = range maxDebugLens {
if i > left {
left = i
}
}
left = left + debugMiddle + (w-rf.FieldWidth-left-maxLeft-maxRight-debugMiddle)/2
top = (h - rf.FieldHeight) / 2
for i := 0; i < rf.FieldHeight; i++ {
for j := 0; j < rf.FieldWidth; j++ {
termbox.SetCell(left+j, top+i, '.', fg, bg)
}
}
running = false
index = 0
p0.Fill()
p1.Fill()
writeDebugValues()
defer func() { //init main loop
if running {
stopCounter()
}
}()
eventQueue := make(chan termbox.Event)
go func() {
for {
eventQueue <- termbox.PollEvent()
}
}()
startCounter() //start main loop
for {
select {
case <-tickerC:
if forward(1) {
return
}
case e := <-eventQueue:
if e.Type == termbox.EventKey {
switch e.Key {
case termbox.KeyCtrlC, termbox.KeyEsc:
return
case termbox.KeyArrowDown: //-10 turns
backward(10)
case termbox.KeyArrowUp: //+10 turns
if forward(10) {
return
}
case termbox.KeyArrowLeft: //-1 turn
backward(1)
case termbox.KeyArrowRight: //+1 turn
if forward(1) {
return
}
case termbox.KeySpace:
if running {
stopCounter()
} else {
startCounter()
}
default:
switch e.Ch {
case 91: //[, -10%
changeSpeed(1.1)
case 93: //], +10%
changeSpeed(0.9)
case 123: //{, half speed
changeSpeed(2)
case 125: //}, double speed
changeSpeed(0.5)
}
}
}
}
}
}
func printStr(x, y int, text string) {
for i, value := range text {
termbox.SetCell(x+i, y, rune(value), fg, bg)
}
}
func printCenter(y int, text string) {
printStr((w-len(text))/2, y, text)
termbox.Flush()
}
func writeDebugValues() {
for i, key := range debugValues[index] {
printStr(debugMiddle, i, key)
for j := len(key); j < maxDebugLens[i]; j++ {
termbox.SetCell(debugMiddle+j, i, ' ', fg, bg)
}
}
termbox.Flush()
}
func forward(turnCount int) bool {
//go forward @turnCount turns, returns true if ended
for ; turnCount > 0; turnCount-- {
if index+1 >= len(p0.Moves) {
p0.MoveFill(1)
p1.MoveFill(1)
printCenter(top+rf.FieldHeight+1, "Press any key to quit...")
termbox.PollEvent()
return true
}
index++
p0.MoveFill(0)
p1.MoveFill(0)
writeDebugValues()
}
return false
}
func backward(turnCount int) {
//go back @turnCount turns
for ; turnCount > 0 && index > 0; turnCount-- {
index--
p0.UnMove()
p1.UnMove()
writeDebugValues()
}
}
func startCounter() {
ticker = time.NewTicker(time.Millisecond * time.Duration(opts.Delay))
tickerC = ticker.C
running = true
}
func stopCounter() {
ticker.Stop()
running = false
}
func changeSpeed(delayMult float32) {
stopCounter()
opts.Delay = int(float32(opts.Delay) * delayMult)
startCounter()
}