home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / expect / expect-4.7 / example / robohunt < prev    next >
Encoding:
Text File  |  1993-04-12  |  2.0 KB  |  74 lines

  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.  
  9.  
  10. trap exit SIGINT
  11.  
  12. set timeout 1
  13.  
  14. # given a direction and number, moves that many spaces in that direction
  15. proc mv {dir num} {
  16.     # first try firing a bullet (what the hell...open some walls to move!)
  17.     send "f"
  18.     for {set i 0} {$i<$num} {incr i} {
  19.         send $dir
  20.     }
  21. }
  22.  
  23. # given a random character, generates/moves a distance/direction
  24.  
  25. # 31 is arbitrarily used as a max distance to move in any one direction
  26. # this is a compromise between long horizontal and vertical moves
  27. # but since excess movement is good for stabbing, this is reasonable
  28. proc move {str} {
  29.     scan $str "%c" num
  30.     set mask [expr $num&3]
  31.     set num [expr $num&31]
  32.     if $mask==0 {send "H"; mv "h" $num; return}
  33.     if $mask==1 {send "L"; mv "l" $num; return}
  34.     if $mask==2 {send "K"; mv "k" $num; return}
  35.              send "J"; mv "j" $num; return
  36. }
  37.  
  38. log_user 0
  39. # output is turned off so that we can first strip out ^Gs before they
  40. # are sent to the tty.  It seems to drive xterms crazy - because our
  41. # rather stupid algorithm off not checking after every move can cause
  42. # the game to send a lot of them.
  43.  
  44. if 3==[llength $argv] { set output 0 } {set output 1}
  45. if 2>[llength $argv] { send_user "usage: robohunt name \[-nodisplay\]\n"; exit}
  46. spawn hunt -b -c -n [lindex $argv 1]
  47. expect "team"
  48. send "\r"
  49.  
  50. set several_moves 15
  51.  
  52. set i -1;        # index into fortune buffer
  53.  
  54. for {} 1 {} {
  55.     if $i==-1 {
  56.         # get a new fortune to use for move data
  57.         set fortune [split [exec /usr/games/fortune] ""]
  58.         set i [expr [llength $fortune]-1]
  59.     }
  60.  
  61.     # make several moves at a time, before checking to see if we are dead
  62.     # this is a compromise between just ignoring our status after each move
  63.     # and looking at our status after each move
  64.     for {set j $several_moves} {$j>=0 && $i>=0} {incr i -1; incr j -1} {
  65.         move [lindex $fortune $i]
  66.     }
  67.     expect {
  68.         -re ^\007+ {continue -expect}
  69.         -re "? " {send y}
  70.         -re \[^\007\]+
  71.     }    
  72.     if $output {send_user $expect_out(buffer)}
  73. }
  74.