2008-04-28 07:18:59 +00:00
|
|
|
(in-package :gtp-handler)
|
|
|
|
|
2008-05-10 09:02:08 +00:00
|
|
|
|
2008-05-05 15:39:04 +00:00
|
|
|
(defparameter *quit?* nil)
|
2008-05-26 23:30:59 +00:00
|
|
|
;(defparameter *cputime* 0)
|
2008-05-05 15:39:04 +00:00
|
|
|
|
2008-05-29 02:40:25 +00:00
|
|
|
(defmacro inc-cpu-timer (body)
|
|
|
|
`(let ((start (get-internal-run-time))
|
|
|
|
(val ,body)
|
|
|
|
(end (get-internal-run-time)))
|
|
|
|
(setf go-bot:*cputime* (+ go-bot:*cputime* (float (/ (- end start) 1000))))
|
|
|
|
val))
|
|
|
|
|
2008-05-07 09:07:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun gtp-net-client (server port)
|
|
|
|
(go-bot:init)
|
|
|
|
(setf *quit?* nil)
|
2008-05-26 23:30:59 +00:00
|
|
|
(let ((socket (netpipe:tcp-connect server port)))
|
2008-05-08 01:36:06 +00:00
|
|
|
(if (eql socket nil)
|
|
|
|
()
|
|
|
|
(progn
|
2008-05-10 09:02:08 +00:00
|
|
|
; (format t "Connection establish, playing...~%")
|
2008-05-08 01:36:06 +00:00
|
|
|
(do ()
|
|
|
|
((or (eql socket nil) (eql *quit?* t)))
|
2008-05-26 23:30:59 +00:00
|
|
|
(let ((cmd (netpipe:tcp-read socket)))
|
2008-05-29 02:40:25 +00:00
|
|
|
;(format t "cmd: '~a'~%" cmd)
|
2008-05-26 23:30:59 +00:00
|
|
|
(let ((resp (inc-cpu-timer (dispatch-gtp-command cmd))))
|
2008-05-29 02:40:25 +00:00
|
|
|
;(format t "resp: '~a'~%" resp)
|
2008-05-26 23:30:59 +00:00
|
|
|
(netpipe:tcp-print socket (concatenate 'string "= " resp (string #\newline) (string #\newline))))))))))
|
2008-05-08 01:36:06 +00:00
|
|
|
|
2008-04-28 07:18:59 +00:00
|
|
|
(defun gtp-client ()
|
2008-05-07 09:07:47 +00:00
|
|
|
(go-bot:init)
|
2008-05-05 15:39:04 +00:00
|
|
|
(setf *quit?* nil)
|
|
|
|
(do ()
|
|
|
|
((eql *quit?* t))
|
2008-05-26 23:30:59 +00:00
|
|
|
(format t "= ~a~%~%" (inc-cpu-timer (dispatch-gtp-command (read-line t))))))
|
2008-05-05 15:00:06 +00:00
|
|
|
|
|
|
|
(defun split-string (string pivot-str)
|
|
|
|
(do ((pivot (char pivot-str 0))
|
|
|
|
(i 0 (+ i 1))
|
|
|
|
(beg 0)
|
|
|
|
(strings '()))
|
|
|
|
((> i (length string)) (reverse strings))
|
|
|
|
(if (or (eql (length string) i) (eql (aref string i) pivot))
|
|
|
|
(progn (push (subseq string beg i) strings) (setf beg (+ i 1))))))
|
|
|
|
|
|
|
|
|
2008-05-29 02:40:25 +00:00
|
|
|
(defparameter *supported_commands* '("name" "version" "protocol_version" "komi" "boardsize" "clear_board" "play" "genmove" "cputime" "quit" "game_score" "list_commands" "known_command" "gogui-analyze_commands" ))
|
|
|
|
|
2008-08-23 17:19:41 +00:00
|
|
|
(defparameter *analyze_commands* '("gfx/Stones/stones" "gfx/Liberties/liberties" "gfx/Shapes/shapes" "gfx/Shape-Liberties/shape-liberties" "gfx/Shape-Stone-Liberties/shape-stone-liberties"))
|
2008-05-29 02:40:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-26 23:30:59 +00:00
|
|
|
|
|
|
|
(defun match-string (str)
|
|
|
|
(lambda (elem) (string-equal str elem)))
|
2008-04-28 07:18:59 +00:00
|
|
|
|
|
|
|
(defun dispatch-gtp-command (command-string)
|
2008-08-23 17:19:41 +00:00
|
|
|
(pdebug "dispatch-gtp-command ~a~%" command-string)
|
2008-05-29 02:40:25 +00:00
|
|
|
(let* ((commands (split-string (string-trim #(#\newline #\space) (string-upcase command-string)) " "))
|
|
|
|
;(cl-ppcre:split "[\\s\\n]+" (string-upcase command-string)))
|
2008-05-07 09:07:47 +00:00
|
|
|
(command (intern (first commands) :gtp-handler)))
|
2008-05-29 02:40:25 +00:00
|
|
|
;(format t "~a~%" commands)
|
2008-05-05 15:00:06 +00:00
|
|
|
(case command
|
2008-05-05 15:39:04 +00:00
|
|
|
(name go-bot:*name*)
|
|
|
|
(version go-bot:*version*)
|
2008-05-07 09:07:47 +00:00
|
|
|
(protocol_version "gtp2ip-0.1")
|
2008-05-06 07:53:22 +00:00
|
|
|
(boardsize (go-bot:set-boardsize (parse-integer (second commands)))
|
|
|
|
(go-bot:init-board)
|
|
|
|
"")
|
|
|
|
; warning: read-from-string pulls full reader. not safe
|
|
|
|
(komi (go-bot:set-komi (read-from-string (second commands)))
|
|
|
|
"")
|
2008-05-07 09:07:47 +00:00
|
|
|
(clear_board (go-bot:init) "")
|
2008-05-19 04:00:04 +00:00
|
|
|
(play (go-bot:do-play (char (second commands) 0) (third commands)) "")
|
|
|
|
(genmove (go-bot:do-genmove (char (second commands) 0)))
|
|
|
|
(genmove_black (go-bot:do-genmove #\b))
|
|
|
|
(genmove_white (go-bot:do-genmove #\w))
|
2008-05-26 23:30:59 +00:00
|
|
|
(cputime (write-to-string go-bot:*cputime*))
|
2008-05-07 09:07:47 +00:00
|
|
|
;(get_random_seed "0")
|
2008-05-26 23:30:59 +00:00
|
|
|
(known_command (write-to-string (count-if (match-string (second commands)) *supported_commands*)))
|
|
|
|
(list_commands (let ((str ""))
|
|
|
|
(loop for command in *supported_commands* do (setf str (concatenate 'string str command " ")))
|
|
|
|
str))
|
2008-05-29 02:40:25 +00:00
|
|
|
(gogui-analyze_commands (let ((str ""))
|
|
|
|
(loop for command in *analyze_commands* do (setf str (concatenate 'string str command (string #\newline))))
|
|
|
|
(string-trim #(#\newline) str)))
|
2008-05-07 09:07:47 +00:00
|
|
|
(game_score (format t "Score for ~c: ~s~%" go-bot:*player* (string-trim (string #\newline) (second commands))) "")
|
2008-08-23 17:19:41 +00:00
|
|
|
(stones (string-trim #(#\newline) (analyze-stones)))
|
2008-05-29 02:40:25 +00:00
|
|
|
(liberties (string-trim #(#\newline) (analyze-liberty)))
|
2008-06-19 06:16:51 +00:00
|
|
|
(shapes (string-trim #(#\newline) (analyze-shapes)))
|
2008-06-21 06:03:06 +00:00
|
|
|
(shape-liberties (string-trim #(#\newline) (analyze-shape-liberties)))
|
2008-06-24 17:45:26 +00:00
|
|
|
(shape-stone-liberties (string-trim #(#\newline) (analyze-shape-stone-liberties)))
|
2008-06-21 06:03:06 +00:00
|
|
|
;(scores (string-trim #(#\newline)(analyze-score)))
|
2008-05-06 07:53:22 +00:00
|
|
|
(quit (setf *quit?* t) "")
|
2008-05-07 09:07:47 +00:00
|
|
|
(otherwise (concatenate 'string "? unknown command: " (string-downcase (first commands)))))))
|
2008-04-28 07:18:59 +00:00
|
|
|
|