Go で書き直した Ikemen
修訂 | 55383ba195aa1b57a7a0c075af5d0340e9bde634 (tree) |
---|---|
時間 | 2016-12-01 23:59:31 |
作者 | SUEHIRO <supersuehiro@user...> |
Commiter | SUEHIRO |
作ってないところにでunimplementedを呼ぶようにした
@@ -327,7 +327,7 @@ function p1SelSub() | ||
327 | 327 | p1SelEnd = true |
328 | 328 | if p2In == 1 then |
329 | 329 | p2Task = p2TmSub |
330 | - commandBufReset(p2Cmd, p2In) | |
330 | + commandBufReset(p2Cmd) | |
331 | 331 | end |
332 | 332 | end |
333 | 333 | end |
@@ -404,7 +404,7 @@ function p2SelSub() | ||
404 | 404 | p2SelEnd = true |
405 | 405 | if p1In == 2 then |
406 | 406 | p1Task = p1TmSub |
407 | - commandBufReset(p1Cmd, p1In) | |
407 | + commandBufReset(p1Cmd) | |
408 | 408 | end |
409 | 409 | end |
410 | 410 | end |
@@ -519,8 +519,8 @@ function main() | ||
519 | 519 | |
520 | 520 | refresh() |
521 | 521 | |
522 | - commandBufReset(p1Cmd, p1In) | |
523 | - commandBufReset(p2Cmd, p2In) | |
522 | + commandBufReset(p1Cmd) | |
523 | + commandBufReset(p2Cmd) | |
524 | 524 | |
525 | 525 | selMode = true |
526 | 526 | selectStart() |
@@ -587,7 +587,7 @@ function modeSel() | ||
587 | 587 | textImgSetText(portChange, 'Port Change(' .. getListenPort() .. ')') |
588 | 588 | |
589 | 589 | refresh() |
590 | - commandBufReset(p1Cmd, 1) | |
590 | + commandBufReset(p1Cmd) | |
591 | 591 | |
592 | 592 | while btnPalNo(p1Cmd) <= 0 do |
593 | 593 | if commandGetState(p1Cmd, 'u') then |
@@ -0,0 +1,4 @@ | ||
1 | +package main | |
2 | + | |
3 | +type CharGlobalInfo struct{ drawpalno int32 } | |
4 | +type Char struct{} |
@@ -0,0 +1,3 @@ | ||
1 | +package main | |
2 | + | |
3 | +type Compiler struct{} |
@@ -32,18 +32,27 @@ type PalFX struct { | ||
32 | 32 | Remap []int |
33 | 33 | } |
34 | 34 | |
35 | -func NewPalFX() *PalFX { return &PalFX{} } | |
36 | -func (pfx *PalFX) GetFxPal(pal []uint32, neg bool) []uint32 { return pal } | |
35 | +func NewPalFX() *PalFX { return &PalFX{} } | |
36 | +func (pfx *PalFX) GetFxPal(pal []uint32, neg bool) []uint32 { | |
37 | + if pfx == nil || pfx.Time == 0 { | |
38 | + return pal | |
39 | + } | |
40 | + unimplemented() | |
41 | + return nil | |
42 | +} | |
37 | 43 | func (pfx *PalFX) GetFcPalFx(trans int32) (neg bool, color float32, |
38 | 44 | add, mul [3]float32) { |
39 | - neg = false | |
40 | - color = 1 | |
41 | - for i := range add { | |
42 | - add[i] = 0 | |
43 | - } | |
44 | - for i := range mul { | |
45 | - mul[i] = 1 | |
45 | + if pfx == nil || pfx.Time == 0 { | |
46 | + neg = false | |
47 | + color = 1 | |
48 | + for i := range add { | |
49 | + add[i] = 0 | |
50 | + } | |
51 | + for i := range mul { | |
52 | + mul[i] = 1 | |
53 | + } | |
46 | 54 | } |
55 | + unimplemented() | |
47 | 56 | return |
48 | 57 | } |
49 | 58 |
@@ -422,11 +422,17 @@ type NetBuffer struct { | ||
422 | 422 | } |
423 | 423 | type NetInput struct{ buf []NetBuffer } |
424 | 424 | |
425 | -func (ni *NetInput) Close() {} | |
425 | +func (ni *NetInput) Close() { unimplemented() } | |
426 | +func (ni *NetInput) Input(cb *CommandBuffer, i, facing int32) { | |
427 | + unimplemented() | |
428 | +} | |
426 | 429 | |
427 | 430 | type FileInput struct{ ib []InputBits } |
428 | 431 | |
429 | -func (ni *FileInput) Close() {} | |
432 | +func (ni *FileInput) Close() { unimplemented() } | |
433 | +func (ni *FileInput) Input(cb *CommandBuffer, i, facing int32) { | |
434 | + unimplemented() | |
435 | +} | |
430 | 436 | |
431 | 437 | type AiInput struct { |
432 | 438 | dir, dt, at, bt, ct, xt, yt, zt, st int32 |
@@ -902,7 +908,7 @@ func (c *Command) bufTest(cbuf *CommandBuffer, ai bool, | ||
902 | 908 | } |
903 | 909 | } |
904 | 910 | foo := false |
905 | - for _, k := range c.cmd[c.cmdi-1].key { | |
911 | + for _, k := range c.cmd[c.cmdi].key { | |
906 | 912 | n := cbuf.State2(k) |
907 | 913 | if c.cmd[c.cmdi].slash { |
908 | 914 | foo = foo || n > 0 |
@@ -966,8 +972,81 @@ type CommandList struct { | ||
966 | 972 | DefaultBufferTime int32 |
967 | 973 | } |
968 | 974 | |
969 | -func NewCommandList() *CommandList { | |
970 | - return &CommandList{Names: make(map[string]int)} | |
975 | +func NewCommandList(cb *CommandBuffer) *CommandList { | |
976 | + return &CommandList{Buffer: cb, Names: make(map[string]int)} | |
977 | +} | |
978 | +func (cl *CommandList) Input(i, facing int32) bool { | |
979 | + if cl.Buffer == nil { | |
980 | + return false | |
981 | + } | |
982 | + step := cl.Buffer.Bb != 0 | |
983 | + if i < 0 && int(^i) < len(sys.aiInput) { | |
984 | + sys.aiInput[^i].Update() // 乱数を使うので同期がずれないようここで | |
985 | + } | |
986 | + _else := i < 0 | |
987 | + if _else { | |
988 | + } else if sys.fileInput != nil { | |
989 | + sys.fileInput.Input(cl.Buffer, i, facing) | |
990 | + } else if sys.netInput != nil { | |
991 | + sys.netInput.Input(cl.Buffer, i, facing) | |
992 | + } else { | |
993 | + _else = true | |
994 | + } | |
995 | + if _else { | |
996 | + var l, r, u, d, a, b, c, x, y, z, s bool | |
997 | + if i < 0 { | |
998 | + i = ^i | |
999 | + if int(i) < len(sys.aiInput) { | |
1000 | + l = sys.aiInput[i].L() | |
1001 | + r = sys.aiInput[i].R() | |
1002 | + u = sys.aiInput[i].U() | |
1003 | + d = sys.aiInput[i].D() | |
1004 | + a = sys.aiInput[i].A() | |
1005 | + b = sys.aiInput[i].B() | |
1006 | + c = sys.aiInput[i].C() | |
1007 | + x = sys.aiInput[i].X() | |
1008 | + y = sys.aiInput[i].Y() | |
1009 | + z = sys.aiInput[i].Z() | |
1010 | + s = sys.aiInput[i].S() | |
1011 | + } | |
1012 | + } else if int(i) < len(sys.inputRemap) { | |
1013 | + in := sys.inputRemap[i] | |
1014 | + if in < len(sys.keyConfig) { | |
1015 | + joy := sys.keyConfig[in].Joy | |
1016 | + if joy >= -1 { | |
1017 | + l = JoystickState(joy, sys.keyConfig[in].L) | |
1018 | + r = JoystickState(joy, sys.keyConfig[in].R) | |
1019 | + u = JoystickState(joy, sys.keyConfig[in].U) | |
1020 | + d = JoystickState(joy, sys.keyConfig[in].D) | |
1021 | + a = JoystickState(joy, sys.keyConfig[in].A) | |
1022 | + b = JoystickState(joy, sys.keyConfig[in].B) | |
1023 | + c = JoystickState(joy, sys.keyConfig[in].C) | |
1024 | + x = JoystickState(joy, sys.keyConfig[in].X) | |
1025 | + y = JoystickState(joy, sys.keyConfig[in].Y) | |
1026 | + z = JoystickState(joy, sys.keyConfig[in].Z) | |
1027 | + s = JoystickState(joy, sys.keyConfig[in].S) | |
1028 | + } | |
1029 | + } | |
1030 | + } | |
1031 | + var B, F bool | |
1032 | + if facing < 0 { | |
1033 | + B, F = r, l | |
1034 | + } else { | |
1035 | + B, F = l, r | |
1036 | + } | |
1037 | + cl.Buffer.Input(B, d, F, u, a, b, c, x, y, z, s) | |
1038 | + } | |
1039 | + return step | |
1040 | +} | |
1041 | +func (cl *CommandList) Step(facing int32, ai, hitpause bool, | |
1042 | + buftime int32) { | |
1043 | + if cl.Buffer != nil { | |
1044 | + for i := range cl.Commands { | |
1045 | + for j := range cl.Commands[i] { | |
1046 | + cl.Commands[i][j].Step(cl.Buffer, ai, hitpause, buftime) | |
1047 | + } | |
1048 | + } | |
1049 | + } | |
971 | 1050 | } |
972 | 1051 | func (cl *CommandList) BufReset() { |
973 | 1052 | if cl.Buffer != nil { |
@@ -988,3 +1067,24 @@ func (cl *CommandList) Add(c Command) { | ||
988 | 1067 | cl.Commands[i] = append(cl.Commands[i], c) |
989 | 1068 | cl.Names[c.name] = i |
990 | 1069 | } |
1070 | +func (cl *CommandList) At(i int) []Command { | |
1071 | + if i < 0 || i >= len(cl.Commands) { | |
1072 | + return []Command{} | |
1073 | + } | |
1074 | + return cl.Commands[i] | |
1075 | +} | |
1076 | +func (cl *CommandList) Get(name string) []Command { | |
1077 | + i, ok := cl.Names[name] | |
1078 | + if !ok { | |
1079 | + return []Command{} | |
1080 | + } | |
1081 | + return cl.At(i) | |
1082 | +} | |
1083 | +func (cl *CommandList) GetState(name string) bool { | |
1084 | + for _, c := range cl.Get(name) { | |
1085 | + if c.curbuftime > 0 { | |
1086 | + return true | |
1087 | + } | |
1088 | + } | |
1089 | + return false | |
1090 | +} |
@@ -1,6 +1,7 @@ | ||
1 | 1 | package main |
2 | 2 | |
3 | 3 | import ( |
4 | + "fmt" | |
4 | 5 | "github.com/go-gl/glfw/v3.2/glfw" |
5 | 6 | "github.com/yuin/gopher-lua" |
6 | 7 | "runtime" |
@@ -15,6 +16,10 @@ func chk(err error) { | ||
15 | 16 | panic(err) |
16 | 17 | } |
17 | 18 | } |
19 | +func unimplemented() { | |
20 | + _, fn, line, _ := runtime.Caller(1) | |
21 | + panic(Error(fmt.Sprintf("%v:%v: unimplemented", fn, line))) | |
22 | +} | |
18 | 23 | func main() { |
19 | 24 | chk(glfw.Init()) |
20 | 25 | defer glfw.Terminate() |
@@ -73,7 +73,7 @@ func scriptCommonInit(l *lua.LState) { | ||
73 | 73 | return 1 |
74 | 74 | }) |
75 | 75 | luaRegister(l, "commandNew", func(l *lua.LState) int { |
76 | - l.Push(newUserData(l, NewCommandList())) | |
76 | + l.Push(newUserData(l, NewCommandList(&CommandBuffer{}))) | |
77 | 77 | return 1 |
78 | 78 | }) |
79 | 79 | luaRegister(l, "commandAdd", func(l *lua.LState) int { |
@@ -88,6 +88,24 @@ func scriptCommonInit(l *lua.LState) { | ||
88 | 88 | cl.Add(*c) |
89 | 89 | return 0 |
90 | 90 | }) |
91 | + luaRegister(l, "commandGetState", func(l *lua.LState) int { | |
92 | + cl, ok := toUserData(l, 1).(*CommandList) | |
93 | + if !ok { | |
94 | + userDataError(l, 1, cl) | |
95 | + } | |
96 | + l.Push(lua.LBool(cl.GetState(strArg(l, 2)))) | |
97 | + return 1 | |
98 | + }) | |
99 | + luaRegister(l, "commandInput", func(l *lua.LState) int { | |
100 | + cl, ok := toUserData(l, 1).(*CommandList) | |
101 | + if !ok { | |
102 | + userDataError(l, 1, cl) | |
103 | + } | |
104 | + if cl.Input(int32(numArg(l, 2))-1, 1) { | |
105 | + cl.Step(1, false, false, 0) | |
106 | + } | |
107 | + return 0 | |
108 | + }) | |
91 | 109 | luaRegister(l, "commandBufReset", func(l *lua.LState) int { |
92 | 110 | cl, ok := toUserData(l, 1).(*CommandList) |
93 | 111 | if !ok { |
@@ -356,7 +374,7 @@ func systemScriptInit(l *lua.LState) { | ||
356 | 374 | return 0 |
357 | 375 | }) |
358 | 376 | luaRegister(l, "setStage", func(*lua.LState) int { |
359 | - l.Push(lua.LNumber(sys.sel.setStageNo(int(numArg(l, 1))))) | |
377 | + l.Push(lua.LNumber(sys.sel.SetStageNo(int(numArg(l, 1))))) | |
360 | 378 | return 1 |
361 | 379 | }) |
362 | 380 | luaRegister(l, "refresh", func(*lua.LState) int { |
@@ -418,4 +436,13 @@ func systemScriptInit(l *lua.LState) { | ||
418 | 436 | sys.resetRemapInput() |
419 | 437 | return 0 |
420 | 438 | }) |
439 | + luaRegister(l, "loadStart", func(l *lua.LState) int { | |
440 | + sys.loadStart() | |
441 | + return 0 | |
442 | + }) | |
443 | + luaRegister(l, "selectStart", func(l *lua.LState) int { | |
444 | + sys.sel.ClearSelected() | |
445 | + sys.loadStart() | |
446 | + return 0 | |
447 | + }) | |
421 | 448 | } |
@@ -17,8 +17,8 @@ var sys = System{ | ||
17 | 17 | gameWidth: 320, gameHeight: 240, |
18 | 18 | widthScale: 1, heightScale: 1, |
19 | 19 | brightness: 256, |
20 | - introTime: 0, roundTime: -1, | |
21 | - lifeMul: 1, team1VS2Life: 1, | |
20 | + roundTime: -1, | |
21 | + lifeMul: 1, team1VS2Life: 1, | |
22 | 22 | turnsRecoveryRate: 1.0 / 300, |
23 | 23 | zoomMin: 1, zoomMax: 1, zoomSpeed: 1, |
24 | 24 | lifebarFontScale: 1, |
@@ -29,7 +29,8 @@ var sys = System{ | ||
29 | 29 | sel: *newSelect(), |
30 | 30 | match: 1, |
31 | 31 | inputRemap: [...]int{0, 1, 2, 3, 4, 5, 6, 7}, |
32 | - listenPort: "7500"} | |
32 | + listenPort: "7500", | |
33 | + loader: *newLoader()} | |
33 | 34 | |
34 | 35 | type System struct { |
35 | 36 | randseed int32 |
@@ -58,7 +59,7 @@ type System struct { | ||
58 | 59 | sel Select |
59 | 60 | netInput *NetInput |
60 | 61 | fileInput *FileInput |
61 | - aiInput []AiInput | |
62 | + aiInput [MaxSimul * 2]AiInput | |
62 | 63 | keyConfig []*KeyConfig |
63 | 64 | com [MaxSimul * 2]int32 |
64 | 65 | autolevel bool |
@@ -66,6 +67,12 @@ type System struct { | ||
66 | 67 | match int32 |
67 | 68 | inputRemap [MaxSimul * 2]int |
68 | 69 | listenPort string |
70 | + round int32 | |
71 | + wins [2]int32 | |
72 | + rexisted [2]int32 | |
73 | + loader Loader | |
74 | + chars [MaxSimul * 2][]*Char | |
75 | + cgi [MaxSimul * 2]CharGlobalInfo | |
69 | 76 | } |
70 | 77 | |
71 | 78 | func (s *System) init(w, h int32) *lua.LState { |
@@ -138,6 +145,14 @@ func (s *System) resetRemapInput() { | ||
138 | 145 | s.inputRemap[i] = i |
139 | 146 | } |
140 | 147 | } |
148 | +func (s *System) loaderReset() { | |
149 | + s.round, s.wins, s.rexisted = 1, [2]int32{0, 0}, [2]int32{0, 0} | |
150 | + s.loader.reset() | |
151 | +} | |
152 | +func (s *System) loadStart() { | |
153 | + s.loaderReset() | |
154 | + s.loader.runTread() | |
155 | +} | |
141 | 156 | |
142 | 157 | type SelectChar struct { |
143 | 158 | def, name string |
@@ -147,27 +162,31 @@ type SelectStage struct { | ||
147 | 162 | def, name string |
148 | 163 | } |
149 | 164 | type Select struct { |
150 | - columns, rows int32 | |
151 | - cellsize [2]float32 | |
152 | - cellscale [2]float32 | |
153 | - randomspr *Sprite | |
154 | - randomscl [2]float32 | |
155 | - charlist []SelectChar | |
156 | - stagelist []SelectStage | |
157 | - curStageNo int | |
165 | + columns, rows int32 | |
166 | + cellsize [2]float32 | |
167 | + cellscale [2]float32 | |
168 | + randomspr *Sprite | |
169 | + randomscl [2]float32 | |
170 | + charlist []SelectChar | |
171 | + stagelist []SelectStage | |
172 | + curStageNo int | |
173 | + selected [2][][2]int | |
174 | + selectedStageNo int | |
158 | 175 | } |
159 | 176 | |
160 | 177 | func newSelect() *Select { |
161 | 178 | return &Select{columns: 5, rows: 2, randomscl: [2]float32{1, 1}, |
162 | - cellsize: [2]float32{29, 29}, cellscale: [2]float32{1, 1}} | |
179 | + cellsize: [2]float32{29, 29}, cellscale: [2]float32{1, 1}, | |
180 | + selectedStageNo: -1} | |
163 | 181 | } |
164 | -func (s *Select) setStageNo(n int) int { | |
182 | +func (s *Select) SetStageNo(n int) int { | |
165 | 183 | s.curStageNo = n % (len(s.stagelist) + 1) |
166 | 184 | if s.curStageNo < 0 { |
167 | 185 | s.curStageNo += len(s.stagelist) + 1 |
168 | 186 | } |
169 | 187 | return s.curStageNo |
170 | 188 | } |
189 | +func (s *Select) SelectStage(n int) { s.selectedStageNo = n } | |
171 | 190 | func (s *Select) AddCahr(def string) { |
172 | 191 | s.charlist = append(s.charlist, SelectChar{}) |
173 | 192 | sc := &s.charlist[len(s.charlist)-1] |
@@ -260,3 +279,71 @@ func (s *Select) AddStage(def string) error { | ||
260 | 279 | } |
261 | 280 | return nil |
262 | 281 | } |
282 | +func (s *Select) ClearSelected() { | |
283 | + s.selected = [2][][2]int{[][2]int{}, [][2]int{}} | |
284 | + s.selectedStageNo = -1 | |
285 | +} | |
286 | + | |
287 | +type LoaderState int32 | |
288 | + | |
289 | +const ( | |
290 | + LS_NotYet LoaderState = iota | |
291 | + LS_Loading | |
292 | + LS_Complete | |
293 | + LS_Error | |
294 | + LS_Cancel | |
295 | +) | |
296 | + | |
297 | +type Loader struct { | |
298 | + state LoaderState | |
299 | + loadExit chan LoaderState | |
300 | + compiler *Compiler | |
301 | + err error | |
302 | +} | |
303 | + | |
304 | +func newLoader() *Loader { | |
305 | + return &Loader{state: LS_NotYet, loadExit: make(chan LoaderState, 1)} | |
306 | +} | |
307 | +func (l *Loader) load() { | |
308 | + defer func() { l.loadExit <- l.state }() | |
309 | + charDone, codeDone, stageDone := make([]bool, len(sys.chars)), false, false | |
310 | + allCharDone := func() bool { | |
311 | + for _, b := range charDone { | |
312 | + if !b { | |
313 | + return false | |
314 | + } | |
315 | + } | |
316 | + return true | |
317 | + } | |
318 | + for !codeDone || !stageDone || !allCharDone() { | |
319 | + unimplemented() | |
320 | + if sys.gameEnd { | |
321 | + l.state = LS_Cancel | |
322 | + } | |
323 | + if l.state == LS_Cancel { | |
324 | + return | |
325 | + } | |
326 | + } | |
327 | + l.state = LS_Complete | |
328 | +} | |
329 | +func (l *Loader) reset() { | |
330 | + if l.state != LS_NotYet { | |
331 | + l.state = LS_Cancel | |
332 | + <-l.loadExit | |
333 | + l.state = LS_NotYet | |
334 | + } | |
335 | + l.compiler, l.err = nil, nil | |
336 | + for i := range sys.cgi { | |
337 | + if sys.rexisted[i&1] == 0 { | |
338 | + sys.cgi[i].drawpalno = -1 | |
339 | + } | |
340 | + } | |
341 | +} | |
342 | +func (l *Loader) runTread() bool { | |
343 | + if l.state != LS_NotYet { | |
344 | + return false | |
345 | + } | |
346 | + l.state = LS_Loading | |
347 | + go l.load() | |
348 | + return true | |
349 | +} |