加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
nc_ui.go 24.91 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
package ftbt
import (
"log"
"fmt"
"gitee.com/tfcolin/goncurses"
)
const (
NUM_WTECH int = 10
)
type NCursesUI struct {
is_first_msg bool
opt_name [2]string
w_all * goncurses.Window
w_map * goncurses.Window
p_map * goncurses.Pad
w_base * goncurses.Window
w_aux * goncurses.Window
w_atech * goncurses.Window
w_scroll * goncurses.Window
w_msg * goncurses.Window
p_intro * goncurses.Pad
w_intro * goncurses.Window
w_dx * goncurses.Window
w_weight * goncurses.Window
w_people * goncurses.Window
w_opt * goncurses.Window
w_hp * goncurses.Window
w_is_finish * goncurses.Window
w_abi * goncurses.Window
w_def * goncurses.Window
w_speed * goncurses.Window
w_buf_v [2][2]* goncurses.Window
w_buf_d [2][2]* goncurses.Window
w_fly_v [2] * goncurses.Window
w_fly_d [2] * goncurses.Window
w_dl_v [2] * goncurses.Window
w_dl_d [2] * goncurses.Window
w_tech [NUM_WTECH] * goncurses.Window
w_sb [2] * goncurses.Window
w_msg_in * goncurses.Window
wl_dx * goncurses.Window
wl_weight * goncurses.Window
wl_people * goncurses.Window
wl_opt * goncurses.Window
wl_hp * goncurses.Window
wl_is_finish * goncurses.Window
wl_abi * goncurses.Window
wl_def * goncurses.Window
wl_speed * goncurses.Window
wl_aux_opt [2] * goncurses.Window
wl_aux_val [2] * goncurses.Window
wl_aux_dur [2] * goncurses.Window
wl_buf [2] * goncurses.Window
wl_fly * goncurses.Window
wl_dl * goncurses.Window
wl_tech * goncurses.Window
wl_sb [2] * goncurses.Window
sc_base[2] int
sc_win [2] int
sc_len [2] int
cx, cy int
}
type NCursesUISelect struct {
* NCursesUI
opt int
}
type NCursesUIAISelect struct {
* NCursesUISelect
/* current people id */
cp int
}
func InitNCursesUISelect (opt int, nui * NCursesUI) * NCursesUISelect {
var nuis NCursesUISelect
nuis.NCursesUI = nui
nuis.opt = opt
return &nuis
}
func InitNCursesUIAISelect (opt int, nui * NCursesUI) * NCursesUIAISelect {
var nuis NCursesUIAISelect
nuis.NCursesUISelect = InitNCursesUISelect (opt, nui)
return &nuis
}
func (nui * NCursesUI) End () {
goncurses.End ()
}
func InitNCursesUI () * NCursesUI {
var nui NCursesUI
nui.opt_name = [2]string{"OptA", "OptB"}
nui.is_first_msg = true
var err error
/* init ncurses */
nui.w_all, err = goncurses.Init("zh_CN.UTF-8")
// nui.w_all, err = goncurses.Init()
if err != nil {
goncurses.End()
log.Fatal ("NCUI Init error.\n")
return nil
}
goncurses.Cursor (0)
goncurses.Echo (false)
goncurses.MouseMask (goncurses.M_ALL, nil)
has_color := goncurses.HasColors()
cc_color := goncurses.CanChangeColor ()
goncurses.StartColor ()
n_color := goncurses.Colors ()
if !has_color || !cc_color || n_color < 16 {
goncurses.End()
log.Fatal ("This terminal do not have enough color support.")
return nil
}
/* init colors */
goncurses.InitColor (0, 0, 0, 0)
goncurses.InitColor (1, 1000, 0, 0)
goncurses.InitColor (2, 0, 1000, 0)
goncurses.InitColor (3, 0, 0, 1000)
goncurses.InitColor (4, 1000, 1000, 0)
goncurses.InitColor (5, 1000, 0, 1000)
goncurses.InitColor (6, 0, 1000, 1000)
goncurses.InitColor (7, 1000, 1000, 1000)
goncurses.InitColor (8, 1000, 500, 0)
goncurses.InitColor (9, 400, 400, 400)
goncurses.InitColor (10, 0, 500, 1000)
goncurses.InitColor (11, 200, 300, 400)
goncurses.InitColor (12, 1000, 600, 600)
goncurses.InitColor (13, 600, 600, 1000)
/* white/black */
goncurses.InitPair (0, 7, 0)
/* show people and aux */
goncurses.InitPair (1, 12, 0)
goncurses.InitPair (2, 13, 0)
goncurses.InitPair (3, 12, 9)
goncurses.InitPair (4, 13, 9)
goncurses.InitPair (5, 12, 11)
goncurses.InitPair (6, 13, 11)
/* show scroll bar */
goncurses.InitPair (7, 0, 8)
goncurses.InitPair (8, 0, 10)
/* show dx */
goncurses.InitPair (9, 3, 4) // PL
goncurses.InitPair (10, 1, 6) // GR
goncurses.InitPair (11, 5, 2) // FO
goncurses.InitPair (12, 2, 5) // MO
goncurses.InitPair (13, 4, 3) // CI
goncurses.InitPair (14, 6, 1) // WA
/* set background */
nui.w_all.Color (0)
/* enable special keys */
nui.w_all.Keypad (true)
/* Refresh */
nui.w_all.Refresh ()
ny, nx := nui.w_all.MaxYX ()
if ny < 32 || nx < 164 {
goncurses.End()
log.Fatal ("Not enough space. Please use a larger terminal.")
return nil
}
/* create all sub windows */
nui.w_map, _ = goncurses.NewWindow (32, 122, 0, 0)
nui.w_base, _ = goncurses.NewWindow (7, 42, 0, 122)
nui.w_aux, _ = goncurses.NewWindow (8, 42, 7, 122)
nui.w_atech, _ = goncurses.NewWindow (7, 42, 15, 122)
nui.w_scroll, _ = goncurses.NewWindow (4, 42, 22, 122)
nui.w_msg, _ = goncurses.NewWindow (6, 42, 26, 122)
nui.w_msg_in, _ = goncurses.NewWindow (4, 40, 27, 123)
nui.w_msg_in.ScrollOk (true)
nui.p_intro, _ = goncurses.NewPad (500, 70)
nui.p_intro.Window.ScrollOk (true)
nui.w_intro, _ = goncurses.NewWindow (28, 72, 2, 25)
nui.wl_dx, _ = goncurses.NewWindow (1, 12, 1, 123)
nui.w_dx, _ = goncurses.NewWindow (1, 8, 1, 135)
nui.wl_weight, _ = goncurses.NewWindow (1, 12, 1, 143)
nui.w_weight, _ = goncurses.NewWindow (1, 8, 1, 155)
nui.wl_people, _ = goncurses.NewWindow (1, 12, 2, 123)
nui.w_people, _ = goncurses.NewWindow (1, 8, 2, 135)
nui.wl_opt, _ = goncurses.NewWindow (1, 12, 2, 143)
nui.w_opt, _ = goncurses.NewWindow (1, 8, 2, 155)
nui.wl_hp, _ = goncurses.NewWindow (1, 12, 3, 123)
nui.w_hp, _ = goncurses.NewWindow (1, 8, 3, 135)
nui.wl_is_finish, _ = goncurses.NewWindow (1, 12, 3, 143)
nui.w_is_finish, _ = goncurses.NewWindow (1, 8, 3, 155)
nui.wl_abi, _ = goncurses.NewWindow (1, 12, 4, 123)
nui.w_abi, _ = goncurses.NewWindow (1, 8, 4, 135)
nui.wl_def, _ = goncurses.NewWindow (1, 12, 4, 143)
nui.w_def, _ = goncurses.NewWindow (1, 8, 4, 155)
nui.wl_speed, _ = goncurses.NewWindow (1, 12, 5, 123)
nui.w_speed, _ = goncurses.NewWindow (1, 8, 5, 135)
nui.wl_aux_opt[0], _ = goncurses.NewWindow (1, 4, 8, 123 + 8 + 6)
nui.wl_aux_opt[1], _ = goncurses.NewWindow (1, 4, 8, 123 + 8 * 3 + 6)
nui.wl_aux_val[0], _ = goncurses.NewWindow (1, 8, 9, 123 + 8)
nui.wl_aux_dur[0], _ = goncurses.NewWindow (1, 8, 9, 123 + 8 * 2)
nui.wl_aux_val[1], _ = goncurses.NewWindow (1, 8, 9, 123 + 8 * 3)
nui.wl_aux_dur[1], _ = goncurses.NewWindow (1, 8, 9, 123 + 8 * 4)
nui.wl_buf[0], _ = goncurses.NewWindow (1, 8, 10, 123)
nui.wl_buf[1], _ = goncurses.NewWindow (1, 8, 11, 123)
nui.wl_fly, _ = goncurses.NewWindow (1, 8, 12, 123)
nui.wl_dl, _ = goncurses.NewWindow (1, 8, 13, 123)
for i := 0; i < 2; i ++ {
for j := 0; j < 2; j ++ {
nui.w_buf_v[i][j], _ = goncurses.NewWindow (1, 8, 10 + j, 123 + 8 + 16 * i)
nui.w_buf_d[i][j], _ = goncurses.NewWindow (1, 8, 10 + j, 123 + 16 + 16 * i)
}
nui.w_fly_v[i], _ = goncurses.NewWindow (1, 8, 12, 123 + 8 + 16 * i)
nui.w_fly_d[i], _ = goncurses.NewWindow (1, 8, 12, 123 + 16 + 16 * i)
nui.w_dl_v[i], _ = goncurses.NewWindow (1, 8, 13, 123 + 8 + 16 * i)
nui.w_dl_d[i], _ = goncurses.NewWindow (1, 8, 13, 123 + 16 + 16 * i)
}
nui.wl_tech, _ = goncurses.NewWindow (1, 10, 16, 123)
nsh := (NUM_WTECH - 1) / 3 + 1
for i := 0; i < nsh; i ++ {
for j := 0; j < 3; j ++ {
ind := i * 3 + j
if ind < NUM_WTECH {
nui.w_tech[ind], _ = goncurses.NewWindow (1, 10, 16 + i, 123 + 10 + 10 * j)
}
}
}
for i := 0; i < 2; i ++ {
nui.wl_sb[i], _ = goncurses.NewWindow (1, 10, 23 + i, 123)
nui.w_sb[i], _ = goncurses.NewWindow (1, 30, 23 + i, 123 + 10)
}
/* draw window borders */
nui.w_map.Border ('|', '|', '-', '-', '*', '*', '*', '*')
nui.w_base.Border ('|', '|', '-', '-', '*', '*', '*', '*')
nui.w_aux.Border ('|', '|', '-', '-', '*', '*', '*', '*')
nui.w_atech.Border ('|', '|', '-', '-', '*', '*', '*', '*')
nui.w_scroll.Border ('|', '|', '-', '-', '*', '*', '*', '*')
nui.w_msg.Border ('|', '|', '-', '-', '*', '*', '*', '*')
nui.w_intro.Border ('|', '|', '-', '-', '*', '*', '*', '*')
nui.w_map.Refresh()
nui.w_base.Refresh()
nui.w_aux.Refresh()
nui.w_atech.Refresh()
nui.w_scroll.Refresh()
nui.w_msg.Refresh()
nui.w_intro.Refresh()
/* write labels */
nui.wl_dx.Printf ("%-12s", "Ground")
nui.wl_weight.Printf ("%-12s", "Weight")
nui.wl_people.Printf ("%-12s", "Role")
nui.wl_opt.Printf ("%-12s", "Control")
nui.wl_hp.Printf ("%-12s", "HP")
nui.wl_is_finish.Printf ("%-12s", "Finished")
nui.wl_abi.Printf ("%-12s", "Ability")
nui.wl_def.Printf ("%-12s", "Defence")
nui.wl_speed.Printf ("%-12s", "Speed")
nui.wl_aux_opt[0].Printf ("%4s", "OptA")
nui.wl_aux_opt[1].Printf ("%4s", "OptB")
for i := 0; i < 2; i ++ {
nui.wl_aux_val[i].Printf ("%-8s", " Value")
nui.wl_aux_dur[i].Printf ("%-8s", " Dur.")
}
nui.wl_buf[0].Printf ("%-8s", "Abi.")
nui.wl_buf[1].Printf ("%-8s", "Def.")
nui.wl_fly.Printf ("%-8s", "Fly")
nui.wl_dl.Printf ("%-8s", "Dis.L")
nui.wl_tech.Printf ("%-10s", " Tech")
nui.wl_sb[0].Printf ("%-10s", "XScroll")
nui.wl_sb[1].Printf ("%-10s", "YScroll")
nui.wl_dx.Refresh()
nui.wl_weight.Refresh()
nui.wl_people.Refresh()
nui.wl_opt.Refresh()
nui.wl_hp.Refresh()
nui.wl_is_finish.Refresh()
nui.wl_abi.Refresh()
nui.wl_def.Refresh()
nui.wl_speed.Refresh()
for i := 0; i < 2; i ++ {
nui.wl_aux_opt[i].Refresh()
nui.wl_aux_val[i].Refresh()
nui.wl_aux_dur[i].Refresh()
nui.wl_buf[i].Refresh()
nui.wl_sb[i].Refresh()
}
nui.wl_fly.Refresh()
nui.wl_dl.Refresh()
nui.wl_tech.Refresh()
return &nui
}
func (nui * NCursesUI) ShowMsg (msg string) {
if nui.is_first_msg {
nui.is_first_msg = false
} else {
nui.w_msg_in.Printf ("\n")
}
nui.w_msg_in.Printf ("%s", msg)
nui.w_msg_in.Refresh ()
}
func (nui * NCursesUI) UpdateScroll () {
/* adjust sc_base */
for i := 0; i < 2; i ++ {
if nui.sc_base[i] < 0 {
nui.sc_base[i] = 0
}
if nui.sc_base[i] + nui.sc_win[i] > nui.sc_len[i] {
nui.sc_base[i] = nui.sc_len[i] - nui.sc_win[i]
}
}
/* draw scroll bar */
var sb_len int = 30
var sb_s, sb_e int
for i := 0; i < 2; i ++ {
sb_s = sb_len * nui.sc_base[i] / nui.sc_len[i]
sb_e = sb_len * (nui.sc_base[i] + nui.sc_win[i]) / nui.sc_len[i]
nui.w_sb[i].ColorOn (7)
for j := 0; j < sb_s; j ++ {
nui.w_sb[i].MoveAddChar (0, j, ' ')
}
nui.w_sb[i].ColorOff (7)
nui.w_sb[i].ColorOn (8)
for j := sb_s; j < sb_e; j ++ {
nui.w_sb[i].MoveAddChar (0, j, ' ')
}
nui.w_sb[i].ColorOff (8)
nui.w_sb[i].ColorOn (7)
for j := sb_e; j < sb_len; j ++ {
nui.w_sb[i].MoveAddChar (0, j, ' ')
}
nui.w_sb[i].ColorOff (7)
nui.w_sb[i].Refresh ()
}
nui.p_map.Refresh (nui.sc_base[1] * 2, nui.sc_base[0] * 6, 1, 1, nui.sc_win[1] * 2, nui.sc_win[0] * 6)
}
func (nui * NCursesUI) DrawInfo () {
iloc := nui.cy * MAP_SIZE[0] + nui.cx
loc := gmap[iloc]
/* show dx and weight */
nui.w_dx.MovePrintf (0, 0, "%-8s", dx_name[loc.dx])
if loc.weight >= 0 {
nui.w_weight.MovePrintf (0, 0, "%-8d", loc.weight)
} else {
nui.w_weight.Erase ()
}
/* show aux */
for i := 0; i < 2; i ++ {
for j := 0; j < 2; j ++ {
nui.w_buf_v[i][j].MovePrintf (0, 0, "%-8d", loc.buf_value[i][j])
nui.w_buf_d[i][j].MovePrintf (0, 0, "%-8d", loc.buf_dur[i][j])
nui.w_buf_v[i][j].Refresh ()
nui.w_buf_d[i][j].Refresh ()
}
nui.w_fly_v[i].MovePrintf (0, 0, "%-8d", loc.fly_weight[i])
nui.w_fly_d[i].MovePrintf (0, 0, "%-8d", loc.fly_weight_dur[i])
nui.w_dl_v[i].MovePrintf (0, 0, "%-8v", loc.disable_long[i])
nui.w_dl_d[i].MovePrintf (0, 0, "%-8d", loc.disable_long_dur[i])
nui.w_fly_v[i].Refresh ()
nui.w_fly_d[i].Refresh ()
nui.w_dl_v[i].Refresh ()
nui.w_dl_d[i].Refresh ()
}
/* show people */
if loc.people >= 0 {
p := people[loc.people]
nui.w_people.MovePrintf (0, 0, "%-8s", p.name)
nui.w_opt.MovePrintf (0, 0, "%-8s", nui.opt_name[p.opt])
nui.w_hp.MovePrintf (0, 0, "%-8d", p.hp)
nui.w_is_finish.MovePrintf (0, 0, "%-8t", p.is_finish)
nui.w_abi.MovePrintf (0, 0, "%-8d", p.abi)
nui.w_def.MovePrintf (0, 0, "%-8d", p.def)
nui.w_speed.MovePrintf (0, 0, "%-8d", p.speed)
for i, it := range p.tech {
if i < NUM_WTECH {
nui.w_tech[i].MovePrintf (0, 0, "%-10s", gtech[it].name)
}
}
for i := len(p.tech) ; i < NUM_WTECH; i ++ {
nui.w_tech[i].Erase ()
}
} else {
nui.w_people.Erase ()
nui.w_opt.Erase ()
nui.w_hp.Erase ()
nui.w_is_finish.Erase ()
nui.w_abi.Erase ()
nui.w_def.Erase ()
nui.w_speed.Erase ()
for i := 0; i < NUM_WTECH; i ++ {
nui.w_tech[i].Erase ()
}
}
nui.w_dx.Refresh ()
nui.w_weight.Refresh ()
nui.w_people.Refresh ()
nui.w_opt.Refresh ()
nui.w_hp.Refresh ()
nui.w_is_finish.Refresh ()
nui.w_abi.Refresh ()
nui.w_def.Refresh ()
nui.w_speed.Refresh ()
for i := 0; i < NUM_WTECH; i ++ {
nui.w_tech[i].Refresh ()
}
}
func (nui * NCursesUI) DrawMapStatic () {
for i := 0; i < MAP_SIZE[1]; i ++ {
by := i * 2
for j := 0; j < MAP_SIZE[0]; j ++ {
iloc := i * MAP_SIZE[0] + j
loc := gmap[iloc]
bx := j * 6
nui.p_map.ColorOn (int16(9 + loc.dx))
nui.p_map.MovePrint (by, bx, dx_name[loc.dx])
nui.p_map.ColorOff (int16(9 + loc.dx))
}
}
}
func (nui * NCursesUI) DrawGrid (x, y int, hp_down int) {
loc := gmap[y * MAP_SIZE[0] + x]
by := y * 2
bx := x * 6
var ic int
if x == nui.cx && y == nui.cy {
ic = 5
} else {
if loc.path_last_loc == -2 {
ic = 3
} else {
ic = 1
}
}
for opt := 0; opt < 2; opt ++ {
nui.p_map.ColorOn (int16(ic + opt))
if hp_down >= 0 {
hp_down = hp_down % 100
nui.p_map.MovePrintf (by, bx +2, "%2d", hp_down)
} else {
if loc.people != -1 && people[loc.people].opt == opt {
nui.p_map.MoveAddChar (by, bx + 2 + opt, 'P')
} else {
nui.p_map.MoveAddChar (by, bx + 2 + opt, ' ')
}
}
if loc.buf_dur[opt][0] != 0 {
nui.p_map.MoveAddChar (by + 1, bx + opt, 'A')
} else {
nui.p_map.MoveAddChar (by + 1, bx + opt, ' ')
}
if loc.buf_dur[opt][1] != 0 {
nui.p_map.MoveAddChar (by + 1, bx + 2 + opt, 'D')
} else {
nui.p_map.MoveAddChar (by + 1, bx + 2 + opt, ' ')
}
if loc.fly_weight_dur[opt] != 0 {
nui.p_map.MoveAddChar (by, bx + 4 + opt, 'F')
} else {
nui.p_map.MoveAddChar (by, bx + 4 + opt, ' ')
}
if loc.disable_long_dur[opt] != 0 {
nui.p_map.MoveAddChar (by + 1, bx + 4 + opt, 'S')
} else {
nui.p_map.MoveAddChar (by + 1, bx + 4 + opt, ' ')
}
nui.p_map.ColorOff (int16(ic + opt))
}
}
func (nui * NCursesUI) Update () {
for i := 0; i < MAP_SIZE[1]; i ++ {
for j := 0; j < MAP_SIZE[0]; j ++ {
nui.DrawGrid (j, i, -1)
}
}
nui.DrawInfo ()
nui.UpdateScroll ()
}
func (nui * NCursesUI) ShowGrid (x, y int) {
if x >= nui.sc_base[0] && x < nui.sc_base[0] + nui.sc_win[0] &&
y >= nui.sc_base[1] && y < nui.sc_base[1] + nui.sc_win[1] {
bx := (x - nui.sc_base[0]) * 6
by := (y - nui.sc_base[1]) * 2
nui.p_map.Refresh (y * 2, x * 6, 1 + by, 1 + bx, by + 2, bx + 6)
}
}
func (nui * NCursesUI) UpdateCursor (cx, cy int, hp_down int) {
ocx, ocy := nui.cx, nui.cy
nui.cx, nui.cy = cx, cy
nui.DrawGrid (cx, cy, hp_down)
nui.ShowGrid (cx, cy)
if ocx != cx || ocy != cy {
nui.DrawGrid (ocx, ocy, -1)
nui.ShowGrid (ocx, ocy)
}
nui.DrawInfo ()
}
func (nui * NCursesUI) LoadMapDone () {
nui.w_all.GetChar ()
nui.p_map, _ = goncurses.NewPad (MAP_SIZE[1] * 2, MAP_SIZE[0] * 6)
nui.p_intro.Printf ("%s", intro)
tl, _ := nui.p_intro.CursorYX ()
tl ++
wx, wy := 70, 26
bx, by := 26, 3
for cl := 0; cl < tl; cl += wy {
nui.p_intro.Refresh (cl, 0, by, bx, by + wy - 1, bx + wx - 1)
for ;nui.w_all.GetChar () == 0; {}
}
nui.w_intro.Erase ()
nui.w_intro.Refresh ()
nui.sc_win[0] = 20
nui.sc_win[1] = 15
for i := 0; i < 2; i ++ {
nui.sc_len[i] = MAP_SIZE[i]
nui.sc_base[i] = 0
nui.cx, nui.cy = 0, 0
if nui.sc_len[i] < nui.sc_win[i] {
nui.sc_win[i] = nui.sc_len[i]
}
}
nui.DrawMapStatic ()
nui.Update ()
}
func (nui * NCursesUI) GameStart () {
nui.ShowMsg ("游戏开始")
}
func (nui * NCursesUI) TurnStart (opt int) {
msg := fmt.Sprintf ("%s turn start. There are total %d active roles in battle field.",
nui.opt_name[opt], np_in[opt])
nui.ShowMsg (msg)
}
func (nuis * NCursesUISelect) TurnStart (opt int) {
nuis.NCursesUI.TurnStart (opt)
}
func (nui * NCursesUI) GameOver (win_opt int) {
var msg string = "Game over."
if win_opt >= 0 {
msg += fmt.Sprintf (" %s win.", nui.opt_name[win_opt])
}
nui.ShowMsg (msg)
nui.ShowMsg ("Press any key to exit")
nui.w_all.GetChar ()
}
func (nui * NCursesUI) DoTech (itech, p_src, p_obj, l_obj int) {
msg := fmt.Sprintf ("%s use tech %s", people[p_src].name, gtech[itech].name)
if p_obj != -1 {
msg += fmt.Sprintf (" on %s", people[p_obj].name)
}
msg += ". Press any key to continue"
nui.ShowMsg (msg)
x, y := Loc2XY (l_obj)
nui.UpdateCursor (x, y, -1)
nui.w_all.GetChar ()
}
func (nui * NCursesUI) HPDown (ip, loc, hp, dhp int) {
x, y := Loc2XY (loc)
msg := fmt.Sprintf ("%s at location (%d, %d) drop hp %d from %d, press any key to continue",
people[ip].name, x, y, dhp, hp)
nui.ShowMsg (msg)
nui.UpdateCursor (x, y, dhp)
nui.w_all.GetChar ()
}
func (nui * NCursesUI) PeopleOut (ip, loc int) {
x, y := Loc2XY (loc)
msg := fmt.Sprintf ("%s evacuate from location (%d, %d)", people[ip].name, x, y)
nui.ShowMsg (msg)
}
func (nui * NCursesUI) PeopleIn (ip, loc int) {
}
func (nui * NCursesUI) PeopleMove (ip, loc_start, loc_end int) {
}
func (nui * NCursesUI) PeopleStep (ip, loc, dir int) {
}
func (nui * NCursesUI) LocBuf (loc, att_def, val, dur int) {
}
func (nui * NCursesUI) LocFly (loc, weight, dur int) {
}
func (nui * NCursesUI) LocDisableLong (loc, dur int) {
}
func next_p (ip int) int {
ip = ip + 1
if ip >= len(people) {
ip = 0
}
return ip
}
func (nuis * NCursesUISelect) BrowseOperation (c goncurses.Key, cur_p * int) {
switch {
case c == 'l' || c == goncurses.KEY_RIGHT:
if nuis.cx < MAP_SIZE[0] - 1 {
nuis.UpdateCursor (nuis.cx + 1, nuis.cy, -1)
}
case c == 'h' || c == goncurses.KEY_LEFT:
if nuis.cx > 0 {
nuis.UpdateCursor (nuis.cx - 1, nuis.cy, -1)
}
case c == 'k' || c == goncurses.KEY_UP:
if nuis.cy > 0 {
nuis.UpdateCursor (nuis.cx, nuis.cy - 1, -1)
}
case c == 'j' || c == goncurses.KEY_DOWN:
if nuis.cy < MAP_SIZE[1] - 1 {
nuis.UpdateCursor (nuis.cx, nuis.cy + 1, -1)
}
case c == 'w':
nuis.sc_base[0] += nuis.sc_win[0] / 2
nuis.UpdateScroll ()
if nuis.cx < nuis.sc_base[0] {
nuis.UpdateCursor(nuis.sc_base[0], nuis.cy, -1)
}
case c == 'b':
nuis.sc_base[0] -= nuis.sc_win[0] / 2
nuis.UpdateScroll ()
if nuis.cx >= nuis.sc_base[0] + nuis.sc_win[0] {
nuis.UpdateCursor(nuis.sc_base[0] + nuis.sc_win[0] - 1, nuis.cy, -1)
}
case c == 'd' || c == 4: /* 4 == Ctrl-D */
nuis.sc_base[1] += nuis.sc_win[1] / 2
nuis.UpdateScroll ()
if nuis.cy < nuis.sc_base[1] {
nuis.UpdateCursor(nuis.cx, nuis.sc_base[1], -1)
}
case c == 'u' || c == 21: /* 21 == Ctrl-U */
nuis.sc_base[1] -= nuis.sc_win[1] / 2
nuis.UpdateScroll ()
if nuis.cy >= nuis.sc_base[1] + nuis.sc_win[1] {
nuis.UpdateCursor(nuis.cx, nuis.sc_base[1] + nuis.sc_win[1] - 1, -1)
}
case c == '\t':
if np_in[nuis.opt] == 0 {
return
}
var p * People
for {
*cur_p = next_p (*cur_p)
p = &people[*cur_p]
if p.loc != -1 && p.opt == nuis.opt && !p.is_finish {
break
}
}
x, y := Loc2XY (p.loc)
nuis.UpdateCursor (x, y, -1)
nuis.CenterCursor ()
nuis.UpdateScroll()
}
if nuis.cx < nuis.sc_base[0] {
nuis.sc_base[0] -= nuis.sc_win[0] / 2
nuis.UpdateScroll ()
}
if nuis.cx >= nuis.sc_base[0] + nuis.sc_win[0] {
nuis.sc_base[0] += nuis.sc_win[0] / 2
nuis.UpdateScroll ()
}
if nuis.cy < nuis.sc_base[1] {
nuis.sc_base[1] -= nuis.sc_win[1] / 2
nuis.UpdateScroll ()
}
if nuis.cy >= nuis.sc_base[1] + nuis.sc_win[1] {
nuis.sc_base[1] += nuis.sc_win[1] / 2
nuis.UpdateScroll ()
}
}
func (nuis * NCursesUISelect) SelObj (status SelObjStatus, itech int, valid_loc []int) int {
nuis.Update ()
var msg string
switch status {
case SOS_PEOPLE:
msg = "select people to operate"
case SOS_MOVE_OBJ:
msg = "select moving object location"
case SOS_TECH_OBJ:
msg = fmt.Sprintf ("select object for tech %s", gtech[itech].name)
}
nuis.ShowMsg (msg)
var cur_p int = len(people) - 1
gc: for {
c := nuis.w_all.GetChar ()
switch c {
case 'q':
return -1
case 't':
return -2
case '\n':
break gc
}
nuis.BrowseOperation (c, &cur_p)
}
return XY2Loc (nuis.cx, nuis.cy)
}
func (nui * NCursesUI) CenterCursor () {
nui.sc_base[0] = nui.cx - nui.sc_win[0] / 2
nui.sc_base[1] = nui.cy - nui.sc_win[1] / 2
}
func (nuis * NCursesUISelect) SelTech (ip int) int {
p := people[ip]
if p.loc == -1 {
return -1
}
msg := fmt.Sprintf ("Select tech (0 - %d)", len(p.tech) - 1)
nuis.ShowMsg (msg)
nuis.CenterCursor ()
nuis.Update ()
var cur_p int = len(people) - 1
var it int
for {
c := nuis.w_all.GetChar ()
if c == 'q' {
return -1
}
it = int (c - '0')
if it >= 0 && it < len(p.tech) {
break
}
nuis.BrowseOperation (c, &cur_p)
}
return it
}
func (nuis * NCursesUISelect) Confirm (msg string) bool {
nuis.ShowMsg (msg)
for {
c := nuis.w_all.GetChar ()
if c == 'y' || c == 'Y' {
return true
}
if c == 'n' || c == 'N' {
return false
}
}
}
func (nuis * NCursesUIAISelect) TurnStart (opt int) {
nuis.NCursesUI.TurnStart (opt)
nuis.cp = len(people) - 1
AICalAllDist ()
}
/* return -1 to quit, -2 to end turn (only for SOS_PEOPLE) */
func (nuis * NCursesUIAISelect) SelObj (status SelObjStatus, itech int, valid_loc []int) int {
var msg string
var res int = -1
var cur_p int
cur_p = nuis.cp
switch status {
case SOS_PEOPLE:
var p * People
for {
nuis.cp = next_p (nuis.cp)
p = &people[nuis.cp]
if p.opt == nuis.opt && p.loc != -1 {
break
}
}
cur_p = nuis.cp
res = p.loc
msg = fmt.Sprintf ("people %s action by AI. press c to continue.", p.name)
nuis.cx, nuis.cy = Loc2XY (p.loc)
nuis.CenterCursor ()
case SOS_MOVE_OBJ:
AIMakeDecision (nuis.cp, valid_loc)
msg = fmt.Sprintf ("people %s move by AI. press c to continue.", people[nuis.cp].name)
res = ai_people[nuis.cp].opt_move_loc
if res != -1 {
nuis.cx, nuis.cy = Loc2XY (res)
}
case SOS_TECH_OBJ:
p := people[nuis.cp]
msg = fmt.Sprintf ("people %s do tech %s by AI. press c to continue.", p.name, gtech[itech].name)
res = ai_people[nuis.cp].opt_tech_obj
nuis.cx, nuis.cy = Loc2XY (p.loc)
}
nuis.Update ()
nuis.ShowMsg (msg)
for {
c := nuis.w_all.GetChar ()
if c == 'c' {
break
}
nuis.BrowseOperation (c, &cur_p)
}
return res
}
/* return -1 to give up tech use */
func (nuis * NCursesUIAISelect) SelTech (ip int) int {
return ai_people[ip].opt_itech
}
/* return true to confirm quit */
func (nuis * NCursesUIAISelect) Confirm (msg string) bool {
return true
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化