;;; This holds debugging functions for the null model

(in-package 'USER)

(defun print-snode (node)
  (format t "[~a] age: ~a  depth: ~a  tag: ~a~%"
	  (snode-id node)
	  (snode-age node)
	  (snode-depth node)
	  (snode-tag node))
  (format t "genotype: ~29,'0B~29,'0B~29,'0B~%"
	  (snode-phenotype node)
	  (snode-prey node)
	  (snode-generalism node))
  (format t "muts: ~a~%" (snode-mutations node))
  (format t "dominant clade membership: ~a~%" (snode-dominant node)))


(defun list-tree (node)
  (if (null node) ()
    (list (snode-id node)
	  (list-tree (snode-left node))
	  (list-tree (snode-right node)))))

(defun print-tree (proc node)
  (if (null node) ()
    (if (and (null (snode-left node))
	     (null (snode-right node)))
	(list (funcall proc node))
      (list (funcall proc node)
	    (print-tree proc (snode-left node))
	    (print-tree proc (snode-right node))))))

(defun print-list (proc node-list)
  (if (null node-list)
      (format t "~%")
    (progn
      (format t "~a " (funcall proc (first node-list)))
      (print-list proc (rest node-list)))))

	  

(defun list-extant ()
  (list-extants (iter-get-list)))

(defun print-epoch (epoch)
  (format t "[~a] ~a~%" (first epoch) (list-epochrophals (rest epoch))))

(defun list-epochrophals (elist)
  (if (null elist) ()
    (cons (snode-id (first elist))
	  (list-epochrophals (rest elist)))))

(defun list-extants (node-list)
  (if (null node-list) ()
    (cons (snode-id (first node-list))
	  (list-extants (rest node-list)))))
	   

(defun clear-tags (node)
  (unless (null node)
	  (setf (snode-tag node) ())
	  (clear-tags (snode-left node))
	  (clear-tags (snode-right node))))

(defun clear-depths (node)
  (unless (null node)
	  (setf (snode-depth node) 0)
	  (clear-depths (snode-left node))
	  (clear-depths (snode-right node))))

(defun test-rand (percent trials &optional (base PROBSCALE))
  (let ((chance (round (* percent base) 100))
	(count 0))
    (dotimes (n trials)
      (when (< (random base) chance) (incf count)))
    (format t "~a out of ~a :  ~a percent~%" count trials (round (* 100 count) trials))))


(defun array-to-list (arr l)
  (let ((result ()))
    (dotimes (i l result)
      (setf result (cons (aref arr (- l (1+ i)))
			 result)))))


(defun sum-list (l)
  (eval (cons '+ l)))
