; AisleRiot - seahaven.scm
; Copyright (C) 2001 Rosanna Yuen <zana@webwynk.net>
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(use-modules (aisleriot interface) (aisleriot api))

(def-save-var free-reserves 0)

(define foundation '(0 1 6 7))
(define freecells '(2 3 4 5))
(define tableau '(8 9 10 11 12 13 14 15 16 17))

(define (new-game)
  (initialize-playing-area)
  (set-ace-low)
  (make-standard-deck)
  (shuffle-deck)

  (add-normal-slot DECK 'foundation)
  (add-normal-slot '() 'foundation)

  (add-blank-slot)

  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)

  (add-blank-slot)

  (add-normal-slot '() 'foundation)
  (add-normal-slot '() 'foundation)

  (add-carriage-return-slot)

  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)

  (deal-cards-face-up 0 '(8 9 10 11 12 13 14 15 16 17 8 9 10 11 12 13
			    14 15 16 17 8 9 10 11 12 13 14 15 16 17 8
			    9 10 11 12 13 14 15 16 17 8 9 10 11 12 13 
			    14 15 16 17 3 4))

  (set! free-reserves 2)

  (list 10 4))

(define (sequence-matches-slot? card-list slot)
  (if (empty-slot? slot)
      (= (get-value (car card-list)) ace)
      (and (= (get-suit (car card-list))
	      (get-suit (get-top-card slot)))
           (= (get-value (car card-list))
	      (+ 1 (get-value (get-top-card slot)))))))

(define (button-pressed slot-id card-list)
  (and (not (empty-slot? slot-id))
       (or (= (length card-list) 1)
	   (and (> slot-id 7)
		(< (length card-list) (+ 2 free-reserves))
		(check-same-suit-list card-list)
		(check-straight-descending-list card-list))
	   (and (> slot-id 7) ; Move an entire sequence to the foundation
		(check-same-suit-list card-list)
		(check-straight-descending-list card-list)
		(or (sequence-matches-slot? card-list 0)
		    (sequence-matches-slot? card-list 1)
		    (sequence-matches-slot? card-list 6)
		    (sequence-matches-slot? card-list 7))))))

(define (droppable? start-slot card-list end-slot)
  (cond ((= start-slot end-slot)
	 #f)
	((> end-slot 7) ; The tableau
	 (and (or (and (empty-slot? end-slot)
		       (= (get-value (car (reverse card-list)))
			  king))
		  (and (not (empty-slot? end-slot))
		       (= (get-suit (get-top-card end-slot))
			  (get-suit (car card-list)))
		       (= (get-value (get-top-card end-slot))
			  (+ 1 (get-value (car (reverse card-list)))))))
              (< (length card-list) (+ 2 free-reserves))))
	((and (> end-slot 1) ; The free reserves
	      (< end-slot 6))
	 (and (= (length card-list) 1)
	      (empty-slot? end-slot)))
	(else ; otherwise it is the foundations
	 (cond ((empty-slot? end-slot) 
		(= (get-value (car card-list)) ace))
	       (else
		(and (= (get-suit (get-top-card end-slot))
			(get-suit (car card-list)))
		     (= (+ 1 (get-value (get-top-card end-slot)))
			(get-value (car card-list)))))))))

(define (button-released start-slot card-list end-slot)
  (cond ((= start-slot end-slot)
	 #f)
	((> end-slot 7)
	 (and (< (length card-list) (+ 2 free-reserves))
	      (or (and (empty-slot? end-slot)
		       (= (get-value (car (reverse card-list)))
			  king)
		       (move-n-cards! start-slot end-slot card-list))
		  (and (not (empty-slot? end-slot))
		       (= (get-suit (get-top-card end-slot))
			  (get-suit (car card-list)))
		       (= (get-value (get-top-card end-slot))
			  (+ 1 (get-value (car (reverse card-list)))))
		       (move-n-cards! start-slot end-slot card-list)))
	      (or (> start-slot 7)
		  (and (> start-slot 1)
		       (< start-slot 6)
		       (set! free-reserves (+ 1 free-reserves)))
		  (add-to-score! -1))))
	((and (= (length card-list) 1)
	      (empty-slot? end-slot)
	      (> end-slot 1)
	      (< end-slot 6))
	 (and (move-n-cards! start-slot end-slot card-list)
	      (or (and (> start-slot 1)
		       (< start-slot 6))
		  (set! free-reserves (- free-reserves 1)))))
	((and (= (length card-list) 1)
	      (or (< end-slot 2)
		  (and (> end-slot 5)
		       (< end-slot 8))))
	 (and (or (and (empty-slot? end-slot)
		       (= (get-value (car card-list)) ace))
		  (and (not (empty-slot? end-slot))
		       (= (get-suit (get-top-card end-slot))
			  (get-suit (car card-list)))
		       (= (+ 1 (get-value (get-top-card end-slot)))
			  (get-value (car card-list)))))
	      (move-n-cards! start-slot end-slot card-list)
	      (or (and (> start-slot 7)
		       (add-to-score! 1))
		  (and (> start-slot 1)
		       (< start-slot 6)
		       (set! free-reserves (+ free-reserves 1))
		       (add-to-score! 1))
		  #t)))
	((and (or (< end-slot 2) ; Move an entire sequence to the foundation
		  (= end-slot 6)
		  (= end-slot 7))
	      (check-same-suit-list card-list)
	      (check-straight-descending-list card-list)
	      (sequence-matches-slot? card-list end-slot)) 
	 (and (move-n-cards! start-slot end-slot (reverse card-list))
	      (add-to-score! (length card-list))
	      #t))
	(#t #f)))

(define (button-clicked slot-id)
  #f)

(define (move-to-empty-foundation slot f-slot)
  (cond ((= f-slot 2)
	 (move-to-empty-foundation slot 6))
	((= f-slot 8)
	 #f)
	((empty-slot? f-slot)
	 (deal-cards slot (list f-slot)))
	(#t (move-to-empty-foundation slot (+ 1 f-slot)))))

(define (move-to-foundation slot f-slot)
  (cond ((= f-slot 2)
	 (move-to-foundation slot 6))
	((= f-slot 8)
	 #f)
	((and (not (empty-slot? f-slot))
	      (= (get-suit (get-top-card slot))
		 (get-suit (get-top-card f-slot))))
	 (and (= (get-value (get-top-card slot))
		 (+ 1 (get-value (get-top-card f-slot))))
	      (deal-cards slot (list f-slot))))
	(#t (move-to-foundation slot (+ 1 f-slot)))))

(define (autoplay-foundations)
  (define (autoplay-foundations-tail)
    (if (or-map button-double-clicked (append freecells tableau))
        (delayed-call autoplay-foundations-tail)
        #t))
  (if (or-map button-double-clicked (append freecells tableau))
      (autoplay-foundations-tail)
      #f))

(define (button-double-clicked slot-id)
  (or (and (member slot-id foundation)
	   (autoplay-foundations))
      (and (not (empty-slot? slot-id))
	   (> slot-id 1)
	   (or (< slot-id 6)
	       (> slot-id 7))
	   (or (and (= (get-value (get-top-card slot-id))
		       ace)
		    (move-to-empty-foundation slot-id 0))
	       (move-to-foundation slot-id 0))
	   (add-to-score! 1)
	   (or (> slot-id 7)
	       (set! free-reserves (+ 1 free-reserves))))))

(define (game-continuable)
  (and (not (game-won))
       (get-hint)))

(define (game-won)
  (and (= (length (get-cards 0)) 13)
       (= (length (get-cards 1)) 13)
       (= (length (get-cards 6)) 13)
       (= (length (get-cards 7)) 13)))

(define (check-to-foundations? slot f-slot)
  (cond ((= slot 18)
	 #f)
	((= slot 6)
	 (check-to-foundations? 8 0))
	((= f-slot 2)
	 (check-to-foundations? slot 6))
	((or (empty-slot? slot)
	     (= f-slot 8))
	 (check-to-foundations? (+ 1 slot) 0))
	((= (get-value (get-top-card slot)) ace)
	 (hint-move slot 1 (find-empty-slot foundation)))
	((and (not (empty-slot? f-slot))
	      (= (get-suit (get-top-card slot))
		 (get-suit (get-top-card f-slot)))
	      (= (get-value (get-top-card slot))
		 (+ 1 (get-value (get-top-card f-slot)))))
	 (hint-move slot 1 f-slot))
	(#t (check-to-foundations? slot (+ 1 f-slot)))))

(define (check-for-king card-list iter slot to-slot)
  (cond ((= (length card-list) 0)
	 #f)
	((and (= (length card-list) 1)
	      (> slot 7))
	 #f)
	((= (get-value (car card-list)) king)
	 (hint-move slot (- (+ 1 (length (get-cards slot))) (length card-list)) to-slot))
	((= iter 0)
	 #f)
	((and (> (length card-list)1)
	      (= (get-suit (car card-list))
		 (get-suit (cadr card-list)))
	      (= (+ 1 (get-value (car card-list)))
		 (get-value (cadr card-list))))
	 (check-for-king (cdr card-list) (- iter 1) slot to-slot))
	(#t #f)))

(define (check-for-spec-card card-list iter value slot cards to-slot)
  (cond ((= (length card-list) 0)
	 #f)
	((= (get-value (car card-list)) value)
	 (hint-move slot cards to-slot))
	((= iter 0)
	 #f)
	((and (> (length card-list) 1)
	      (= (get-suit (car card-list))
		 (get-suit (cadr card-list)))
	      (= (+ 1 (get-value (car card-list)))
		 (get-value (cadr card-list))))
	 (check-for-spec-card (cdr card-list) (- iter 1) value slot (+ 1 cards) to-slot))
	(#t #f)))

(define (check-to-tableau? slot t-slot)
  (cond ((= slot 18)
	 #f)
	((= slot 6)
	 (check-to-tableau? 8 9))
	((or (empty-slot? slot)
	     (= t-slot 18))
	 (check-to-tableau? (+ 1 slot) 8))
	((and (not (= slot t-slot))
	      (empty-slot? t-slot)
	      (check-for-king (get-cards slot) free-reserves slot t-slot))
	 (check-for-king (get-cards slot) free-reserves slot t-slot))
	((and (not (= slot t-slot))
	      (not (empty-slot? t-slot))
	      (= (get-suit (get-top-card slot))
		 (get-suit (get-top-card t-slot)))
	      (check-for-spec-card (get-cards slot) 
				   free-reserves 
				   (- (get-value (get-top-card t-slot)) 1)
				   slot 1 t-slot))
	 (check-for-spec-card (get-cards slot) 
			      free-reserves 
			      (- (get-value (get-top-card t-slot)) 1)
			      slot 1 t-slot))
	(#t (check-to-tableau? slot (+ 1 t-slot)))))

(define (check-for-empty-reserve)
  (and (> free-reserves 0)
       (list 0 (G_"Move something on to an empty reserve"))))

(define (get-hint)
  (or (check-to-foundations? 2 0)
      (check-to-tableau? 2 8)
      (check-for-empty-reserve)))

(define (get-options) 
  #f)

(define (apply-options options) 
  #f)

(define (timeout) 
  #f)

(set-features droppable-feature)

(set-lambda new-game button-pressed button-released button-clicked
button-double-clicked game-continuable game-won get-hint get-options
apply-options timeout droppable?)
