1#!../expect -f
2# Synopsis
3#    robohunt player-name [-nodisplay]
4
5# Plays hunt automatically.  Optional "-nodisplay" argument disables output.
6
7# by Don Libes
8
9expect_version -exit 5.0
10
11set timeout 1
12
13proc random {} {
14    global ia ic im jran
15    
16    set jran [expr ($jran*$ia + $ic) % $im]
17    return $jran
18}
19
20set ia 7141
21set ic 54773
22set im 259200
23set jran [pid]
24
25# given a direction and number, moves that many spaces in that direction
26proc mv {dir num} {
27    # first try firing a bullet (what the hell...open some walls to move!)
28    send "f"
29    for {set i 0} {$i<$num} {incr i} {
30	send $dir
31    }
32}
33
34# move a random distance/direction
35
36# 31 is arbitrarily used as a max distance to move in any one direction
37# this is a compromise between long horizontal and vertical moves
38# but since excess movement is good for stabbing, this is reasonable
39proc move {} {
40    set num [random]
41    set mask [expr $num&3]
42    set num [expr $num&31]
43    if $mask==0 {send "H"; mv "h" $num; return}
44    if $mask==1 {send "L"; mv "l" $num; return}
45    if $mask==2 {send "K"; mv "k" $num; return}
46                 send "J"; mv "j" $num; return
47}
48
49if {2==$argc} { set output 0 } {set output 1}
50if {1>$argc}  { send_user "usage: robohunt name \[-nodisplay\]\n"; exit}
51spawn hunt -b -c -n [lindex $argv 0]
52expect "team"
53send "\r"
54
55set several_moves 5
56
57expect "Monitor:"
58after 1000
59expect ;# flush output
60log_user 0
61# output is turned off so that we can first strip out ^Gs before they
62# are sent to the tty.  It seems to drive xterms crazy - because our
63# rather stupid algorithm off not checking after every move can cause
64# the game to send a lot of them.
65
66for {} {1} {} {
67    # make several moves at a time, before checking to see if we are dead
68    # this is a compromise between just ignoring our status after each move
69    # and looking at our status after each move
70    for {set j $several_moves} {$j} {incr j -1} {
71	move
72    }
73
74    expect {
75	-re ^\007+ {exp_continue}
76	-re "\\? " {send y}
77	-re .+
78    }	
79    if $output {send_user -raw $expect_out(buffer)}
80}
81