Code: Select all
(defun encode-tagpair (my-stream my-pgntn my-value)
"Encode a PGN tag pair on a stream."
(fmt-brack-l my-stream)
(if my-pgntn
(put-string my-stream (svref as-pgntn-vec my-pgntn))
(put-nil my-stream))
(blank my-stream)
(fmt-quote my-stream)
(put-string my-stream my-value)
(fmt-quote my-stream)
(fmt-brack-r my-stream)
(values))
(defun encode-tp-vec (my-stream my-tp-vec)
"Encode a vector of PGN tag pair values on a stream."
(dopgntns (pgntn)
(when (svref my-tp-vec pgntn)
(encode-tagpair my-stream pgntn (svref my-tp-vec pgntn))
(newline my-stream)))
(values))
(defconstant movetext-column-limit 80)
(defun encode-movetext-item (my-stream my-item-string my-column)
"Encode a movetext string starting at the given column; return the new column."
(let ((result my-column) (item-length (length my-item-string)))
(when (>= (+ result 1 item-length) movetext-column-limit)
(newline my-stream)
(setf result 0))
(when (positive? result)
(blank my-stream)
(incf result))
(put-string my-stream my-item-string)
(incf result item-length)
result))
(defun encode-movetext-afc (my-stream my-afc my-column)
"Encode a movetext AFC starting at the given column; return the new column."
(encode-movetext-item my-stream (afc-string my-afc) my-column))
(defun encode-movetext-move (my-stream my-move my-column)
"Encode a movetext move starting at the given column; return the new column."
(encode-movetext-item my-stream (san-string my-move) my-column))
(defun encode-movetext-gsr (my-stream my-gsr my-column)
"Encode a movetext game status result starting at the given column; return the new column."
(encode-movetext-item my-stream (svref as-gsr-vec my-gsr) my-column))
(defun encode-movetext (my-stream my-game)
"Encode a game's movetext and game status indicator."
(let*
(
(column 0)
(pos (clone-pos (game-pos my-game)))
(moves nil)
(move nil)
(afc (calc-afc-from-fenpos (game-base-fenpos my-game)))
)
(dowhile (positive? (pos-push-count pos))
(push (first (pos-move-stack pos)) moves)
(retract-move pos))
(when (and moves (= (afc-act-color afc) color-black))
(setf column (encode-movetext-afc my-stream afc column))
(setf move (pop moves))
(setf column (encode-movetext-move my-stream move column))
(incf-afc afc))
(dowhile moves
(if (= (afc-act-color afc) color-white)
(setf column (encode-movetext-afc my-stream afc column)))
(setf move (pop moves))
(setf column (encode-movetext-move my-stream move column))
(incf-afc afc))
(setf column (encode-movetext-gsr my-stream (game-gsr my-game) column))
(when (positive? column)
(newline my-stream)))
(values))
(defun encode-pgn (my-stream my-pgn)
"Encode a PGN game on a stream."
(encode-tp-vec my-stream (pgn-tp-vec my-pgn))
(newline my-stream)
(encode-movetext my-stream (pgn-game my-pgn))
(values))