;****************************************************
;*
;*  PROTOTYPE 5  (P5.lisp) "Gandalf"
;*
;*  PROTOTYPE INTERNAL AND EXTERNAL BEHAVIORS
;* 
;*  May 1, 1995 - begin
;*  Aug 1, 1995 - customized for the cube
;*  Feb 16,1996 - changed to Gandalf
;*
;****************************************************

(in-package 'USER)


;; SKETCH-TYPE MESSAGES

;contains a list of all message types that can be posted on BB

(defvar *turn-sensors* nil)
(defvar *turn-descriptors* nil)
(defvar *turn-state-behaviors* nil)
(defvar *ext-rea-behaviors* nil)
(defvar *int-rea-behaviors* nil)
(defvar *int-rea-beh* nil)
(defvar *CL-beh* nil)
(defvar *PCL-state-beh* nil)
(defvar *grand-generic-list* nil) ;I'm not sure this is used anymore....
(defvar *ext-cont-rea-behaviors* nil)
(defvar *ext-CL-behaviors* nil)
(defvar *ext-PCL-behaviors* nil)

(defvar *BB-sensory-msgs-types* nil)  
;Eval *BB-msgs-types* below when *BB-sensory-msgs-types*!!
(setf *BB-msgs-types* *BB-sensory-msgs-types*)
(setf *BB-sensory-msgs-types* 
    '(facing-domain
      looking-at-me
      facing-me
      turned-to-me
      gesturing
      speaking
      addressing-me
      rhand-in-gest-space
      lhand-in-gest-space
      hand-in-gest-space
      looking-at-r-hand
      looking-at-l-hand
      looking-at-hands
      is-active
      giving-turn
      taking-turn
      wanting-turn
      take-turn
      give-turn
      see-user
      greeting
      saying-goodbye
      saying-my-name
      complete-utter
      complete-synt
      complete-gram
      complete-pragm
      want-back-ch-feedb
      concluding
      dial-on
      dial-off
      referencing-domain
      l-deictic-morph
      r-deictic-morph
      )
  )



(defmethod BB-msgs-type ((msgs symbol))
  "Receives a message and returns T if part of BB message set."
  (member msgs *BB-msgs-types*))

(defmethod BB-msgs-type ((msgs list))
  "In this case we have a function call in the form of a list
   and need to determine if the message of the funcall is part of BB."
  (member (second msgs) *BB-msgs-types*))

(if *BB-sensory-msgs-types* (setf *BB-msgs-types* *BB-sensory-msgs-types*))


;; KNOWLEDGE BASE MESSAGE TYPES

(defvar *KB-msgs-types* nil)   ;stores all message types that can be posted to *KB-msgs*
(setf *KB-msgs-types* 
      '(RCV-SPCH ;given when something is received from HARK
        SPCH-data-avail
        KB-parsing
        KB-succ-parse
        CL-act-avail
        KB-exe-act
        DKB-act-avail
        DKB-exe-act 
        TKB-act-avail
        TKB-exe-speech-act 
        TKB-exe-world-act
        )
      )
      
(defmethod KB-msgs-type ((item symbol))
  (member item *KB-msgs-types*))


; Indices into the *input-buffer* datarecord
; Constants for positions and orientations for turn-taking system.
;Indexes are really functions

;************ Virtual Sensors **************

;NOTICE: these are not *just* turn sensors anymore! (5/26/96)

(defparameter *turn-sensors*  
  ;there are not *only* turn sensors here anymore 3/10/96
  ;Sensors receive raw data and do simple computations.
  ; TEMPLATE: (name object-type 
  ;                       data1 data2 
  ;                       (index1) (index2)  ;indexes are in prens if functions
  ;                       func state) 
    '((facing-domain bs-fix-ref
		     nil work-screen
		     (get-head-dir) nil
                     facing nil)
      (looking-at-me bs-fix-ref 
                     nil agent-screen
                     (get-gaze-dir) nil
                     looking-me nil)
      (facing-me bs-fix-ref 
                 nil agent-screen 
                 (get-head-dir) nil 
                 facing nil)
      (turned-to-me bs-fix-ref 
                    nil agent-screen
                    (get-trunk-dir) nil 
                    turned-to nil)
      (looking-at-r-hand bs-var-ref 
                         nil nil 
		        (get-gaze-dir) (get-r-wrist-pos) 
                        looking-hands nil)
      (looking-at-l-hand bs-var-ref 
                         nil nil 
		        (get-gaze-dir) (get-l-wrist-pos) 
                        looking-hands nil)
      (l-deictic-morph bs-var-ref
                      nil nil
                                    ;LA-index defined in Sockread2.lisp
                      (get-body-part LA-index 0)(get-trunk-dir)
                      deictic-sketch nil)   ; 0 is foo
      (r-deictic-morph bs-var-ref
                      nil nil
                      (get-body-part RA-index 0)(get-trunk-dir)
                      deictic-sketch nil)
;      (l-hand-above-trunk-midline bs-var-ref  ;used for deictic sketch (5/24/96)
;                                  nil nil
;                                  (get-l-wrist-pos)(get-trunk-dir)
;                                  hand-above-trunk-midplane nil)
;      (r-hand-above-trunk-midline bs-var-ref  ;used for deictic sketch
;                                  nil nil
;                                  (get-r-wrist-pos)(get-trunk-dir)
;                                  hand-above-trunk-midplane nil)
;      (l-arm-straight bs-fix-ref
;                      nil nil
;                      (get-l-elbow-angle) nil
;                      arm-straight nil)
;      (r-arm-straight bs-fix-ref
;                      nil nil
;                      (get-r-elbow-angle) nil
;                      arm-straight nil)
;;      (looking-at-gest-space bs-var-ref 
;;                         nil nil
;;                         (get-gaze-dir) (get-trunk-dir) 
;;                         looking-gest-space nil)
      (rhand-in-gest-space bs-var-ref 
                           nil nil 
			  (get-trunk-dir) (get-r-wrist-pos) 
                          hand-in-gesture-space nil) 
      (lhand-in-gest-space bs-var-ref 
                           nil nil 
			  (get-trunk-dir) (get-l-wrist-pos) 
                          hand-in-gesture-space nil)
;      (complete-inton inton-sensor nil nil ind1 nil nil nil) 
;      (filler inton-sensor nil nil ind1 nil nil nil)           
      (speaking inton-sensor 
                nil nil 
                speech-on-idx nil 
                speaking? nil)
      (greeting speech-sensor
                9 nil   ;9 stands for region of speech
                nil nil
                region-equal nil)
      (saying-my-name speech-sensor
                      8 nil
                      nil nil
                      region-equal nil)
      (see-user socket-sensor
	        *socket-obj1* nil
		nil nil
		body-status  nil) ;need something more powerful later...
; very simplified for now... 4/12/96
      (complete-pragm speech-sensor 
                      nil nil 
                      ind1 nil 
                      pragmatically-complete nil)
      (complete-gram speech-sensor 
                     nil nil 
                     ind1 ind2 
                     grammar-complete nil)
      (complete-synt speech-sensor 
                     nil nil 
                     ind1 nil 
                     syntax-complete nil)
;      (cl-act-avail speech-sensor ;notice that this should not be done! DKB & TKB
;                    nil nil       ;communicate directly through their own *KB-msgs*.
;                    nil nil
;                    cl-act-available? nil)
  ; TEMPLATE: (name object-type 
  ;                       data1 data2 
  ;                       (index1) (index2)  ;indexes are in prens if functions
  ;                       func state)                     
  ))

; Note: "filler" is really an analysis question and 
; shouldn't be in the descriptor list.
; Or should it? It is crucial for the reactive 
; behavior of the system, so perhaps it should.

; Combining hand-in-gest-space and looking-at-hands 
; could be a reactive predictor's conditions
; to predict iconic gesture.  Or, it's really an 
; "assumption's" conditions because it's not
; anticipating anything; it's assuming after the 
; fact but with very meager evidence.


;************ Multimodal Descriptors **************

;notice: if added to, change list in function descr-type in file mmrecorder!
(defparameter *turn-descriptors*  ;Descriptors dealing with turn analysis.
  ; TEMPLATE: (name pos-cond neg-cond thresh state) 
  '((giving-turn (;(complete-utter 0.5)            ;pos-cond
                  (looking-at-me 0.3)
		  (facing-me 0.3))                
                 ((gesturing 0.3) (speaking 0.4)) ;neg-cond
                 1  nil)                          ;thresh ;state
    (taking-turn ((gesturing 1.0)      ;pos
                  (speaking 1.0))
                 ((looking-at-me 0.5)) ;neg
                 1 nil) ;for neg-cond we could also have (not self-adjustor)
    (want-back-ch-feedb ((looking-at-me 0.5) 
                         (speaking 0.5))
                        ()
                        1 nil)
    (looking-at-hands ((looking-at-r-hand 1.0) 
                       (looking-at-l-hand 1.0))
		       () 1 nil)
    (hand-in-gest-space ((lhand-in-gest-space 1.0)
                         (rhand-in-gest-space 1.0))
                        () 1 nil)
;    (l-deictic-gest ((l-hand-above-trunk-midline 0.5) ;now in sensors 6/3/96
;                     (l-arm-straight 0.6))
;		    () 1 nil)
;    (r-deictic-gest ((r-hand-above-trunk-midline 0.5)
;                     (r-arm-straight 0.6))
;		    () 1 nil)
    (referencing-domain ((l-deictic-morph 0.7)(r-deictic-morph 0.7)
                         (facing-domain 0.5)(speaking 0.5)(facing-me 0.4))
                        () 1 nil)
    (is-active ((hand-in-gest-space 1.0)(speaking 1.0)) 
               () 1 nil)
    (gesturing ((hand-in-gest-space 0.5)(speaking 0.6))
	       () 1 nil)
    (complete-utter ((complete-inton 0.5) 
		     (complete-gram 0.5) 
                     (complete-pragm 0.5))
                    () 1 nil)
    (addressing-me ((turned-to-me 1.0)(facing-me 1.0)(facing-domain 1.0))
                   () 1 nil)
    (wanting-turn ((speaking 0.5)
                   (hand-in-gest-space 0.6))
                  () 1 nil)
    (concluding () 
                () 1 nil)
    (saying-goodbye () 
                    () 1 nil)
;    (saying-my-name ()
;                    () 1 nil)
;not sure we need greeting here....
;    (greeting ()
;              () 1 nil)
    ))


; Prototype files contain all the necessary commands to make
; objects, initialize states and run the proper main routine.

;************ define the internal turn behaviors **************

; To create, call (make-turn-state-beh *turn-state-behaviors*)

; language use: agent has "take/give", user is "taking/giving":

(defparameter *turn-state-behaviors*    
  ;These behaviors change the turn state and are thus internal.
  ;TEMPLATE: nodename msgs next-state pos-cond neg-cond active-descr thresh state
    '(
      (take-turn1 take-turn       ; name of node and what to post in turn-history.
                  (give-turn dial-on)     ; next states that should be active-the ones that should follow.
		  (addressing-me  wanting-turn)   ; pos cond that make next-state active/t
                                  ; (=transition conds to cancel curr state)
		  (KB-exe-act spch-data-avail)    ; neg-cond that make next-state active
                ; active-descr...should include giving-back-ch-feedb.
                   (taking-turn   ; descriptors that will enable transition to next state
                    wanting-turn  ; and should therefore be active *now*.
		    gesturing 
                    addressing-me
                    saying-goodbye
                    concluding
		    hand-in-gest-space 
;;		   is-active 
                    looking-at-hands)
                    1  nil)       ; thresh       ; state
      (take-turn2 take-turn       ; name of node and what to post in turn-history.
                  (give-turn dial-on)     ; next states that should be active-the one that should follow.
		  (addressing-me  taking-turn)   ; pos cond that make next-state active/t
                                  ; (=transition conds to cancel curr state)
		  (KB-exe-act)    ; neg-cond that make next-state active
                ; active-descr...should include giving-back-ch-feedb.
                   (taking-turn   ; descriptors that will enable transition to next state
                    wanting-turn  ; and should therefore be active *now*.
		    gesturing 
                    addressing-me
                    saying-goodbye
                    concluding
		    hand-in-gest-space 
;;		   is-active 
                    looking-at-hands)
                    1  nil)       ; thresh       ; state
 ;     (take-turn3 take-turn       ; name of node and what to post in turn-history.
 ;                 (give-turn dial-on)     ; next states that should be active-the one that should follow.
;		  (facing-me (time-since facing-me 300)) ; pos cond that make next-state active/t
 ;;                                 ; (=transition conds to cancel curr state)
;		  (KB-exe-act spch-data-avail)         ; neg-cond that make next-state active
 ;;               ; active-descr...
;		  (taking-turn   ; descriptors that will enable transition to *next* state(s)
;		   wanting-turn  ; and should therefore be active *now*.
;		   gesturing 
 ;                  addressing-me
;		   saying-goodbye
;		   concluding
;		   hand-in-gest-space 
;;		   is-active 
;		   looking-at-hands)
;		  1  nil)       ; thresh       ; state
;      (give-turn1 give-turn       ; what to post.
;		  (take-turn dial-on)            ; next states that should be active.
;                  (giving-turn (time-since 'give-turn 200))  ; pos-conds  (=transition rule)
;                  (speaking)                     ; neg-conds 
;                  (gesturing                     ; active descriptors
;                   taking-turn 
;      ;             giving-turn 
;;                filler 
;                   looking-at-hands
;                   complete-utter 
;                   want-back-ch-feedb 
;                   saying-goodbye
;                   concluding
;                   is-active) ;active-descr
;                   1 nil)       ;thresh  ;state        

;    (take-turn3 take-turn                   ; what to post.
;                ((time-since speaking 400)) ; pos-conds
;                (speaking)                  ; neg-conds
;                (taking-turn wanting-turn gesturing) ; These updated during this state.
;                all  nil)
      
    ;Perhaps there should be a give-turn-by-free-will 
    ;and then give-turn-bec-user-interrupt;the 
    ;latter would not have goal-list-empty as a condition. In fact, when give-turn is set to
    ;true when goal-list is not empty, there is a different method for 
    ;giving the turn, like "I'm sorry, what was that?" 
      
      (give-turn2 give-turn      ; node name and posting (state) name.
		  (take-turn dial-on)         ; next states that should be active.
		  ((time-since 'speaking 50) giving-turn)  ; pos-cond that make next-state active
                  (taking-turn)    ;neg-cond
                  (gesturing 
                   referencing-domain
                   addressing-me
                   taking-turn 
                   giving-turn 
;                filler 
                   saying-goodbye
                   concluding
                   looking-at-hands
                   complete-utter 
                   want-back-ch-feedb 
                   is-active) ;active-descr
                   1 nil)       ;thresh  ;state         
    ;TEMPLATE: nodename what-to-post next-state pos-cond neg-cond active-descr thresh state
      ))


;************ External Reactive Behaviors **************

; To create, call (make-ext-rea-beh *ext-rea-behaviors*)
; ALL TIME VALUES IN CENTISECONDS

(defparameter *ext-rea-behaviors*
  ;These behaviors control the external behavior of the agent.
  ;TEMPLATE: name extent  [=centiseconds]
  ;               type 
  ;               posconds 
  ;               negconds 
  ;               pos-restore-conds - conds that will make the beh on the outlook for its conds
  ;               neg-restore-conds 
  ;               actions [slot name = msgs]
  ;               initial state [=T or NIL]
    '((show-take-turn1 500                   ;name & extent
                       ext-one-shot-rea-beh  ;type
                       (take-turn)           ;positive trigger conditions.
                       nil                   ;negative trigger conditions.
                       nil                   ;positive restore conditions [not used for cont beh]
                       (take-turn)           ;negative restore conditions [not used for cont beh]
                       (Show-Take-Turn)      ;actions
                       nil)                  ;initial state = is the beh allowed to be executed?
;    (be-impatient  200
;		   ext-one-shot-rea-beh  ;type
;		   (give-turn (time-since speaking 300))
;		   (speaking)
;		   nil               ;positive restore conditions
;		   (take-turn)       ;negative restore conditions
;		   (slow-cube1)
;		   nil)
;    (show-idle 200
;	       ext-one-shot-rea-beh  ;type
;	       (give-turn (time-since speaking 500))  ;pos cond
;	       (speaking)                             ;neg cond
;	       nil               ;positive restore conditions
;	       (give-turn)       ;negative restore conditions
;	       (slow-cube2)
;	       nil)
;    (surprised 500
;	       ext-one-shot-rea-beh  ;type
;               (take-turn hand-in-gest-space) ;pos conds
;               (speaking)                                      ;neg conds
;	       nil               ;positive restore conditions
;	       (take-turn)       ;negative restore conditions
;               (speed-cube)
;               nil)
;    (pay-attention 10000
;		   ext-one-shot-rea-beh  ;type
;		   (give-turn)
;		   nil
;		   nil              ;positive restore conditions
;		   (give-turn)      ;negative restore conditions
;		   (speed-cube)  ;specifies the constituents of action (+ options?).
;		   nil)             ; (or, the actions' constituents 
                                    ; could be specified somewhere else.)
;    (displ-speaking forever     ;this beh is just to show delay of user-speaking->motor act
;		    ext-one-shot-rea-beh  ;type
;                    (speaking)
;                    nil
;		    (t)               ;positive restore conditions
;		    nil               ;negative restore conditions
;                    (displ-speaking)
;                    nil)
;    (displ-silent forever
;                  ext-one-shot-rea-beh
;                  nil
;                  (speaking)
;                  (t)
;                  nil
;                  (displ-silent)
;                  nil)
;      (show-eager 1000                    
;		  ext-one-shot-rea-beh
;		  (turned-to-me facing-me)
;		  nil
;		  nil
;		  (turned-to-me)
;		  (head-at-zero-zero) ;second element indicates which object to reference.
;		  nil)
;      (show-eager2 1000                   
;		   ext-one-shot-rea-beh
;		   (turned-to-me facing-me)
;		   nil
;		   nil
;		   (turned-to-me)
;		   (gaze-at-zero-zero) ;second element indicates which object to reference.
;		   nil)
;      (show-addressing-user1 1000
;                             ext-one-shot-rea-beh
;                             (DKB-exe-act)
;                             nil
;                             (take-turn)
;                             nil
;                             (head-at-zero-zero)
;                             Nil)
      (show-addr-me3 20 ;centiseconds
                     ext-one-shot-rea-beh
		     (turned-to-me facing-me)
                     nil
                     nil
                     (turned-to-me) ;if not turned to me, restore
		     (smile)
                     nil)
  ;    (show-addr-me4 20 ;centiseconds
  ;                   ext-one-shot-rea-beh
;		     (turned-to-me facing-me speaking)
 ;                    nil
  ;                   nil
   ;                  (facing-me) ;if not facing me, restore
;		     (turn-to user)
 ;                    nil)
      (show-addr-me5 20 ;lifetime in centiseconds
                     ext-one-shot-rea-beh
		     (saying-my-name turned-to-me facing-me)
                     nil
                     nil
                     (turned-to-me)  ;if not turned to me, restore
		     (eyebrow-greet)
                     nil)
      (show-listen 20 ;lifetime in centiseconds
		   ext-one-shot-rea-beh
		   (saying-my-name turned-to-me facing-me)
		   nil
		   nil
		   (turned-to-me)
		   (brows-in-pensive-shape)
		   nil)
      (initialize   20 ;lifetime in centiseconds
		    ext-one-shot-rea-beh
		    (dial-off)
		    nil
                    (dial-on)
		    nil
                    (face-neutral)
		    nil)
      (look-puzzled 100 ; 1 sec
		    ext-one-shot-rea-beh
		    (turned-to-me facing-me (time-since 'facing-me 400))
		    nil
		    nil
		    (turned-to-me)
		    (look-puzzled)
		    nil)
      (look-aloof 100 ;centiseconds
		  ext-one-shot-rea-beh
		  (turned-to-me facing-me (time-since 'facing-me 800) dial-off)
		  (speaking kb-parsing)
		  nil
		  (turned-to-me)
		  (look-aloof)
		  nil)
;    (show-addr-me5 200
;		   ext-one-shot-rea-beh
;		   (turned-to-me facing-me)
;                  nil
;                   (turned-to-me)
;                   nil
;		   (turn-to user)
;                   nil)
      (show-not-addr-me 100
                        spatial-rea-beh
                        (dial-off)
                        (turned-to-me)
                        (taking-turn) ;pos restr cond
                        nil
                        (turn-to big-screen)
                        nil)
      (show-give-turn1 200                   ;name & extent
                       ext-one-shot-rea-beh  ;type
                       (give-turn)
                       nil
                       nil               ;positive reset conditions
                       (give-turn)       ;negative reset conditions
                       (show-give-turn)
                       nil)
      (show-give-turn2 200               ;name & extent
                       ext-one-shot-rea-beh  ;type
                       (give-turn)
                       nil
                       nil               ;positive reset conditions
                       (give-turn)       ;negative reset conditions
                       (gaze-at-zero-zero)
                       nil)
      (look-where-pointing 200 
                           spatial-rea-beh                           
                           (referencing-domain)
                           ()
                           ()
                           (referencing-domain)
                           (look-at big-screen)
                           nil)
      (turn-to-where-pointing 200 
                              spatial-rea-beh
                              (referencing-domain (time-since 'referencing-domain 100))
                              ()
                              ()
                              (referencing-domain)
                              (turn-to big-screen)
                              nil)
   ;   (show-give-turn3 200               ;name & extent
   ;                    ext-one-shot-rea-beh  ;type
   ;                    (give-turn)
   ;                    nil
   ;                    nil               ;positive reset conditions
   ;                    (give-turn)       ;negative reset conditions
   ;                    (turn-to user)
   ;                    nil)
      )
  )


;*********** EXTERNAL CONTINUOUS REACTIVE BEHAVIORS ***********

(defparameter *ext-cont-rea-behaviors*  
  ;These behaviors control the external behavior of the agent.
  ; All values in centiseconds.
  ;TEMPLATE: name extent 
  ;               type 
  ;               posconds  ;if t = forever on
  ;               negconds 
  ;               actions (slot name = msgs)
  ;               initial state (T or NIL)
  ;               period
    '((blink-slowly 10000
                    ext-cont-rea-beh
                    (dial-off)  
                    nil
                    (blink-slowly)
                    t
                    300)
      (blink 10000
             ext-cont-rea-beh
             (dial-on)   ;if t = forever on
             nil
             (blink)
             t
             300)
;      (show-addr-me2 10000                     ;2/19/96 new action!
;		 spatial-rea-beh          ;If the motor sys had direct access to spatial data,
;		 (turned-to-me facing-me)  ;we wouldn't need to duplicate show-addr-me in this way.
;		 nil
;		 (look-at user)
;		 nil
;		 600) ;look at user every 6 seconds while talking.
;      (cont-look-at-user 10000
;                         spatial-rea-beh
;                         (DKB-exe-beh)
;                         nil
;                         (turn-to user)
;                         nil
;                         500)
      ))




(defparameter *int-rea-behaviors*
      ;TEMPLATE: name extent type posconds negconds pos-res neg-res msgs
    '(
;      (one-shot-listen forever
;                       int-one-shot-rea-beh  ;type
;                       (give-turn)           ;pos firing conds
;                       nil                   ;neg firing conds
;                       nil                   ;pos reset
;                       (give-turn)           ;neg reset
;                       (one-shot-spch)       ;msgs
;                       )
;      (cont-listen forever
;                   int-one-shot-rea-beh
;                   (see-user)
;                   ()
;                   nil
;                   nil
;                   (cont-speech)
;                   )
      (clear-buffs 10000
		   int-one-shot-rea-beh
		   (give-turn)
		   nil
		   (take-turn)
		   nil
		   (clear-spch-buffs)
		   )
      )
  )
      

;******************************************
;*   PROCESS CONTROL LAYER Behaviors
;******************************************

; These are actually communicating with the TKB, or part
; of it, because they control the sensory apparatus and
; semantic functions.

; These should perhaps all be moved to a file named
; after the character they belong to.

; A neat way to make actions that won't occur for a long
; time after they have been done is to include a long
; time-delay, plus some event, in their restore conditions.

;NOTICE: only DESCRIPTORS may appear in conditions (neg/pos)
;why?

(defparameter *PCL-state-behaviors* 
    ;TEMPLATE 
      ;name type message pos-c neg-c init-state
    '((dial-on1 PCL-state-beh         ;to start dialogue with user
                dial-on               ;what to post (=what state is it now)
	        (dial-off)            ;next states that should be active
	        (saying-goodbye concluding) ;pos-conds that make transition possible
                (dial-off)            ;neg-conds that make transition possible
                (saying-goodbye       ;desciptors that make transition from this state possible
                 concluding)
                nil)
      (dial-off1 PCL-state-beh   ;this one requires the user to say the agent's name
                 dial-off
                 (dial-on give-turn) ;probably HAVE to have give-turn in both ...
                 (saying-my-name)
                 (dial-off)
                 (addressing-me gesturing)
                 nil)
      (dial-off2 PCL-state-beh
                 dial-off                 ;what to post (=what state is it now)
                 (dial-on give-turn)      ;next state(s) that should be active
                 (addressing-me)          ;pos-conds that make transition posible
                 ()                       ;neg-conds that make this state true
                 (addressing-me)          ;descr that enable transition to next
                 nil)
      )
  )


; Here we would update speech through a node...
(defparameter *int-PCL-behaviors* 
    ;Here are actions like excusing delay; asking about missing info, etc.
    ;TEMPLATE 
      ;name type extent msgs pos-c neg-c pos-restr neg-restr init-state
    '((parse-speech int-PCL-beh 200
                    (parse-speech)
		    (giving-turn spch-data-avail)              ;pos-cond
		    ()  ;neg-cond
		    (give-turn)                                ;pos-restr conds
		    ()                                         ;neg-restr-conds
		    nil)
  ;    (rem-spch-addr-to-others int-PCL-beh 200
;			       (rem-spch-to-others)
;			       ((time-since 'speaking 50))
;			       (addressing-me KB-exe-act)
;			       (speaking)
;			       ()
;			       nil)
;      (remove-partial-parses int-PCL-beh 100
 ;                            (rem-part-parses)
 ;                            (KB-succ-parse KB-exe-act)
 ;                            (addressing-me)
 ;                            ()
 ;                            (KB-exe-act)
 ;                            nil)
  ;    (report-no-words int-PCL-beh 900
  ;                     (compose-DKB-trouble-report 'no-words)
  ;                     ((time-since 'speaking 400) addressing-me take-turn giving-turn dial-on)
  ;                     (RCV-spch CL-act-avail taking-turn
  ;                      KB-succ-parse KB-exe-act speaking)
  ;                     (take-turn)
  ;                     ()
  ;                     nil)
    ;  (report-no-words int-PCL-beh 400
     ;                  (compose-DKB-trouble-report 'no-words)
      ;                 ((time-since    'speaking   100)
       ;                 (KB-time-since 'RCV-spch   200)
        ;               (KB-time-since 'KB-succ-parse 800)
         ;               (KB-time-since 'KB-exe-act 800))
          ;             (KB-succ-parse (KB-time-since 'KB-succ-parse 800)
           ;            (time-since 'speaking 300)
            ;            spch-data-avail CL-act-avail RCV-spch taking-turn 
             ;                          give-turn KB-exe-act speaking)
              ;        (speaking)
               ;        ((time-since speaking 50)
                ;       CL-act-avail KB-exe-act spch-data-avail) ;when worked: kb-exe-act cl-act-avail
                 ;      nil)
      )
  )

(defparameter *ext-PCL-behaviors* 
    ;Here are actions like excusing delay; asking about missing info, etc.
    ;TEMPLATE 
      ;name type extent msgs pos-c neg-c pos-restr neg-restr init-state
        ;2/27/96 ? NOTICE: if msgs is a list, first element should be a 
        ;function that takes the rest of msgs in the form of a list as parameters
    '(
      (hesitate1 ext-PCL-beh 100
                 hesitate                                       ;action
		 (dial-on take-turn SPCH-data-avail (time-since speaking 70))   ;pos-cond
		 (CL-act-avail speaking)                        ;neg-cond
		 (give-turn)                                    ;pos-restr conds
		 ()                                             ;neg-restr-cond
                 nil)
      (turn-to-user ext-spatial-PCL-beh 2000
                    (turn-to user)
                    (TKB-exe-speech-act)
		    ()
                    ()
                    (TKB-exe-speech-act)
                    nil)
      (show-content-delivery ext-spatial-PCL-beh 2000
                             (look-at user)
                             (DKB-exe-act looking-at-me)
		             ()
                             (take-turn)
                             ()
                             nil)
      (pay-attention-to-act ext-spatial-PCL-beh 2000
                            (turn-to big-screen)      ;msgs
                            (TKB-exe-world-act)       ;pos-cond
                            ()                        ;neg-cond
                            nil
                            (TKB-exe-world-act)       ;neg-restr-cond
                            nil)
 ;     (show-busy ext-PCL-beh 200
 ;                manual-hold-it-signal
 ;                (speaking facing-me TKB-exe-world-act)
 ;                ()
 ;                ()
 ;                (TKB-exe-world-act)
 ;                nil)
      (show-done-exe ext-spatial-PCL-beh 2000
		     (look-at user)      ;msgs
		     ()                        ;pos-cond
		     (TKB-exe-world-act)       ;neg-cond
		     (TKB-exe-world-act)       ;pos-restr-cond
		     nil
		     nil)
      (show-done-exe2 ext-spatial-PCL-beh 2000
		      (turn-to user)      ;msgs
		      ()                        ;pos-cond
		      (TKB-exe-world-act)       ;neg-cond
		      (TKB-exe-world-act)       ;pos-restr-cond
		      nil
		      nil)
      (show-know-addressing ext-spatial-PCL-beh 2000
                            (look-at 'user)
                            (TKB-exe-world-act looking-at-me facing-me speaking)
                            ()
                            ()
                            (TKB-exe-world-act)
                            nil)
      (show-idle ext-PCL-beh 2000
                 restless
                 ()
                 (facing-me)
                 (facing-me)
                 ()
                 nil)
      (look-at-domain ext-spatial-PCL-beh 2000
                      (turn-to big-screen)
                      (facing-domain take-turn)
                      (speaking)
                      (taking-turn)
                      ()
                      nil)
      (look-relaxed ext-PCL-beh 2000
                    face-neutral
                    (TKB-exe-world-act)
                    ()
                    ()
                    (TKB-exe-world-act)
                    nil)
      (show-listening ext-spatial-PCL-beh 1000
                      (turn-to user)
                      (speaking addressing-me)
                      ()
                      (take-turn)
                      ()
                      nil)
      (show-listening2 ext-spatial-PCL-beh 1000
                       (look-at user)
                       (speaking addressing-me)
                       ()
                       (take-turn)
                       ()
                       nil)
      (look-where-pointing ext-spatial-PCL-beh 2000
                           (look-at big-screen)
                           (referencing-domain)
                           ()
                           ()
                           (referencing-domain)
                           nil)
      (turn-to-where-pointing ext-spatial-PCL-beh 2000
                              (turn-to big-screen)
                              (referencing-domain 
			       (time-since 'referencing-domain 100))
                              ()
                              ()
                              (referencing-domain)
                              nil)
;      (look-at-big-screen2 ext-spatial-PCL-beh 2000
;                           (look-at big-screen)     ;msgs
;                           (TKB-exe-world-act)      ;pos-cond
;                           ()                       ;neg-cond
;                           (give-turn)
;                           ()
;                           nil)
;      (deliver-CL-problem ext-PCL-beh
;                          
      )
  )


;************* CONTENT LAYER BEHAVIORS **************

; When a 'problem report' is received from TKB (reported on KB-msgs)
; an ext-PCL-beh is created with the correct slots filled.

(defparameter *ext-CL-behaviors* 
    ;TEMPLATE
      ;name type func pos-c neg-c pos-restr neg-restr init-state
    '((do-TKB-act ext-CL-beh 2000
	          (post-TKB-act)
	          (dial-on TKB-act-avail take-turn) ;pos-cond
	          ()                                ;neg-cond
	          (CL-act-avail) ;pos-restr conds
	          () ;neg-restr-conds
	          nil
	          )
      (do-DKB-act ext-CL-beh 2000
	          (post-DKB-act)
	          (dial-on DKB-act-avail take-turn) ;pos-cond
	          ()                                ;neg-cond
	          (CL-act-avail) ;pos-restr conds
	          () ;neg-restr-conds
	          nil
	          )
 ;     (turn-to-big-screen3 ext-spatial-PCL-beh 2000    ;perhaps allow this later...????
 ;                          (turn-to big-screen)        ;msgs
 ;                          (TKB-exe-world-act)         ;pos-cond
 ;                          ()                          ;neg-cond
 ;                          (give-turn)
 ;                          ()
 ;                         nil)
      )
  )


(defun compile-all ()
  (proclaim '(optimize (speed 3)))
  (compile-file "/home/kris/sol/ymir/obj-defs.lisp")
  (compile-file "/home/kris/sol/ymir/3DGeo.lisp")       ;3-d calcs.
  (compile-file "/home/kris/sol/ymir/3Danaly.lisp")    ;Specialized 3-d dialogue functions-
                                                         ;(needs to be loaded before T-T!)
  
  (compile-file  "/home/kris/sol/ymir/Sockread2.lisp")  ;Needs to be loaded before all objects.
  
  (compile-file  "/home/kris/sol/ymir/T-T2.lisp")       ;Sensors and integrators.
  (compile-file  "/home/kris/sol/ymir/t-t-funcs")       ;Input processing.
  
  (compile-file  "/home/kris/sol/ymir/Beh3.lisp")       ;Functions related to behaviors.
  (compile-file  "/home/kris/sol/ymir/Dia1.lisp")       ;PCL behaviors
  (compile-file  "/home/kris/sol/ymir/motorMV2.lisp")   ;Motor commands.
  
  (compile-file  "/home/kris/sol/ymir/Timers")          ;Functions returning time stamps.
  
  (compile-file  "/home/kris/sol/ymir/simu")            ;Simulation for debugging.
  
;  (compile-file "/home/kris/sol/ymir/display/Xbasics2.lisp")
;  (compile-file "/home/kris/sol/ymir/display/Xdrawingtools.lisp")

  (compile-file  "/home/kris/sol/ymir/xvisLW.lisp")  ;Display functions
  
  (compile-file "/home/kris/sol/ymir/Assemble.lisp") ;inits & more
  (compile-file "/home/kris/sol/ymir/speech.lisp")   ;parsing, TKB, DKB
  (compile-file "/home/kris/sol/ymir/World") ;Mverse solar sys
  (compile-file "/home/kris/sol/ymir/display/mmrecorder")     ;for recording/plotting data
  )

(defun load-all ()
  (load "/home/kris/sol/ymir/obj-defs.lisp")
  (load "/home/kris/sol/ymir/P5.lisp") ;object defs and load cmnds		
  (load "/home/kris/sol/ymir/defsystem")       ;drawingstuff
;  (load "/home/kris/sol/ymir/display/Xbasics2") ;these loaded in xvisLW
;  (load "/home/kris/sol/ymir/display/Xdrawingtools")

  (load "/home/kris/sol/ymir/3DGeo")        ;3-d calcs.
  (load "/home/kris/sol/ymir/3Danaly")      ;Specialized 3-d dialogue functions-
                                               ;(needs to be loaded before T-T!)
  
  (load "/home/kris/sol/ymir/Sockread2")    ;Needs to be loaded before all objects.

  (load "/home/kris/sol/ymir/T-T2")         ;Functions related to sensors and integrators.  
  (load "/home/kris/sol/ymir/Beh3")         ;RL behaviors; Functions related to behaviors.
  (load "/home/kris/sol/ymir/t-t-funcs")    ;Functions related to input processing.
  
  (load "/home/kris/sol/ymir/Dia1")         ;PCL behaviors; Functions related to behaviors.
  (load "/home/kris/sol/ymir/speech")       ;parsing, TKB, DKB
  (load "/home/kris/sol/ymir/World")        ;Mverse solar sys

  (load "/home/kris/sol/ymir/motorMV2")     ;motor commands - output from Sparta to Spot.

  (load "/home/kris/sol/ymir/Timers")       ;Functions returning time stamps.
  
;  (set-display "karl") ;this may be needed...don't know...
  (load "/home/kris/sol/ymir/display/xvisLW")  ;Display functions [=XvisualsLispWorks]

  (load "/home/kris/sol/ymir/Assemble.lisp")
  (load "/home/kris/sol/ymir/abbrev.lisp")              ;abbreviations
  (load "/home/kris/sol/ymir/display/mmrecorder")       ;for recording/plotting data
  )

;-----------------------------------------------
; SETUPS
;-----------------------------------------------

#|
(defun spike-setup ()
  (setf *hostname* "spike")
  (set-display *hostname*)
  (setf *read-file* gest-space1)
  (finish-setup)
  )

(defun leon-setup ()
  (setf *hostname* "leon")
  (if (boundp '*win1*) (setf font (xlib:open-font *win1* "fixed")))
  (set-display *hostname*)
  (setf Mverse-port# 15006) ;opens mverse on leon's leon:1
  (finish-setup)
  )

(defun spud-setup ()
  (setf *hostname* "spud")
;  (if (boundp '*win1*)(setf font (xlib:open-font *win1* "fixed")))
  (setf *font-name* "type13") ;other options: rock12, vtsingle, fixed
  (set-display *hostname*)
  (setf Mverse-port# 15006)
  (finish-setup)
  )

(defun splotch-setup ()
  (setf *hostname* "splotch")
;  (if (boundp '*win1*)(setf font (xlib:open-font *win1* "6x12")))
  (setf *font-name* "6x12") ;options: rock12, vtsingle, fixed, 6x12, type13
  (set-display *hostname*)
  (get-colors)
  (if (y-or-n-p "Open graphic display? [y/n]")
      (progn
        (open-window)
        (d-on)))
  (setf Mverse-port# 15006)
  (finish-setup)
  )


;(compile-all)
;(load-all-compiled)

(defun finish-setup ()
  (if *display-on* (update-graph))
  (make-all)
  (initialize-all)
;(init-all-sockets *read-file*)
;  (init-all-sockets)
  (setf (num-vals *socket-obj1*) NUM-vals-list)
;(main1)
  (generate-report)
;(print "All set.")
  (print "To start, type: (main1)")
  )

|#