diff -ur v4/kon.py v5/kon.py --- v4/kon.py 2017-10-17 23:00:00.000000000 +0900 +++ v5/kon.py 2017-10-19 23:00:00.000000000 +0900 @@ -6,13 +6,49 @@ import termios import select import threading +import socket def dbg(s, *arg): sys.stderr.write(s.format(*arg) + os.linesep) sys.stdout.flush() -def out(s): - sys.stdout.write(s) +def get_arg(k): + # -k -k=xxx --k --k=xxx + for ds in ('-', '--'): + t = ds + k + if t in sys.argv: + return '' + t += '=' + r = next( ( a[ len(t): ] for a in sys.argv if a.startwith(t) ), None ) + if r != None: + return r + return None + +def get_host_port(s, host='localhost', port=12345): + if not s: + return (host, port) + if ':' in s: + (h, p) = s.split(':') + if h: + host = h + if p: + port = p + else: + if s.isdigit(): + port = int(s) + else: + host = s + return (host, port) + +def out_new(): + sock = None + s = get_arg('o') + if s != None: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect( get_host_port(s, port=55765) ) + return lambda s: ( sock.sendall if sock else sys.stdout.write )(s) + +out = out_new() def flush(): sys.stdout.flush() diff -ur v4/pac.py v5/pac.py --- v4/pac.py 2017-10-17 23:00:00.000000000 +0900 +++ v5/pac.py 2017-10-19 23:00:00.000000000 +0900 @@ -158,17 +158,20 @@ break elif mode != 'ret' and not NO_DIE: mov.en = False - v = 'over' if spare(-1) == 'over' else 'die' + v = 'over' if spare(-1) == 'over' or is_remote else 'die' game_ev.set(v) break -def get_hz(c, nx, ny): +def get_hz(c, nx, ny, nd): hz = stat_get(c).hz if c in MONS: mode = mon_mode(c) hz *= mon_hz_rate.get(mode, 1.0) if mode != 'ret': hz *= warp_rate.get( warp_buf.get(nx, ny), 1.0 ) + if c == 'P': + if nd != arr.get_dir(c): + hz *= 1.5 return hz def move_new(): @@ -288,20 +291,36 @@ if d in DIRS and d != kdir(): kdir(d) +is_remote = '-r' in sys.argv + +def pac_remote(pd, ds): + ds = filter( lambda d: d != kon.bak_dir(pd), ds ) + if len(ds) == 1: + return ds[0] + + d = sys.stdin.read(1) + if d not in ds: + sys.stdout.write('?') + sys.stdout.write(d) + flush() + def th_pac(c): - stat = stat_get(c) (px, py) = arr.pos(c) pd = arr.get_dir(c) - ds = next_ds(px, py, c) - kd = kdir() + d = '' + if is_remote: + d = pac_remote(pd, ds) + else: + kd = kdir() + d = kd if kd and kd in ds else pd if pd in ds else '' - d = kd if kd and kd in ds else pd if pd in ds else '' (nx, ny) = step_to(px, py, d) if d: - hz = get_hz(c, nx, ny) + hz = get_hz(c, nx, ny, d) move(c, d, hz) try_eat(nx, ny) + stat_get(c).th.set_hz(hz) def rand(n): v = sum( sum( map( lambda c: list( arr.pos(c) ), MOVES ), [] ) ) @@ -365,7 +384,7 @@ d = sel_mon_way(c, px, py, ds) (nx, ny) = step_to(px, py, d) - hz = get_hz(c, nx, ny) + hz = get_hz(c, nx, ny, d) move(c, d, hz) mode = mon_mode(c) @@ -547,5 +566,5 @@ game_ev.clear() if __name__ == "__main__": - kon.main( work, '[-bw] [-m]' ) + kon.main( work, '[-bw] [-m] [-o[=host:port]] [-r]' ) # EOF