
(in-package 'USER)

;; -*- Mode: LISP; Syntax: Common-Lisp; Package: (USER); -*-
;-----------------------------------------------------------
;
;  act-defs.lisp
;  -contains Actions
;
; 'Ymir Agent System, 1996
;
; Action definitions for Gandalf 1.0
; 3/3/1996
;
;-----------------------------------------------------------
;

;INCLUDES
(unless (fboundp 'make-instance) (load "/mas/lib/ds/lisp/clos.mbin"))
(load "foreign2.mbin")
(load "my-net-io.mbin")
(load "demo.lisp")
; Motor2 loaded at end of file

(defparameter act-list '())

; RULES FOR ACTS: 
;  1. Only one option allowed at mot-level
;  2. There has to be one mot-lev action for any act made.
;  3. Delay is from beginning of whole act
;  4. For spatial actions, there are always two motors for the head, four motors for the eyes.
;  5. You cannot mix spatial motors and other motors in higher-level actions.
; Hint: when developing actions, make them with only one option; then, when ready, combine.
; To try out a new act, call (make-all-objects), then call (manual-execute act-object)
; All time-related numbers are in milliseconds; all angles are degrees; ranges are 0-100.

(setf act-list 
  ;GENERAL LAYOUT: (<list-of-acts> (<first-act>(<first-act-element>)(<second-act-element>))
  ;                                (<second-act (<...>))
  ;                                )
  ;ACT TEMPLATE:  (name class (((act-name-of-option-1 delay exec-time)(act-name delay exec-time) etc*) 
  ;                            (etc*)))
  ;MOTORS:  (motor-name class delay exec-time pos/data)

      '(
      ; MORPHOLOGICAL DEFINITIONS
        ;Features
	  ;neutral
	(face-neutral act (((mouth-neutral 100 400)
			    (eyes-neutral 0 300)
			    (brows-neutral 0 500))
			   ))
        (brows-neutral act (((left-brow-neutral 0 400)(right-brow-neutral 0 400))
			    ))
	(left-brow-neutral mot-lev (((Bll 0 400 30)
				     (Blc 0 400 30) ;Brow, left, central
				     (Blm 0 400 30)) ;Brow, left, medial
				    ))
	(right-brow-neutral mot-lev (((Brm 0 400 30)
				      (Brc 0 400 30)
				      (Brl 0 400 30))
				     ))
	(eyes-neutral act (((upper-lids-neutral 0 100)(lower-lids-neutral 0 100))))
	(upper-lids-neutral mot-lev (((Eru 0 100 75)(Elu 0 100 80))))
	(upper-lids-open-wide mot-lev (((Eru 0 100 89)(Elu 0 100 94))))
	(lids-neutral act (((upper-lids-neutral 0 300)(lower-lids-neutral 0 200))))
	(mouth-neutral mot-lev (((Mb  0 200 15) ;Mouth, bottom
				 (Mlv 0 200 60) ;Mouth, left, vertical
				 (Mlh 0 200 40)
				 (Mrv 0 200 60)
				 (Mrh 0 200 40))
				))
	(mouth-in-n-shape   mot-lev (((Mb 0 200 15)(Mlv 0 40)(Mrv 0 40)(Mlh 0 50)(Mrh 0 50))))
	(head-at-zero-zero  mot-lev (((Hh 0 800 0)(Hv 0 150 0))))
	(head-diag-up-left  mot-lev (((Hh 0 1000 20)(Hv 0 1000 20)))) ;for debugging
	(gaze-at-zero-zero  mot-lev (((Plv 0 50 0)(Plh 0 50 0)(Prv 0 50 0)(Prh 0 50 0))))

	;actions
	(raise-brows         mot-lev (((Bll 0 400 90)(Blc 0 300 100)(Blm 0 400 90)
				       (Brl 0 400 90)(Brc 0 300 100)(Brm 0 400 90))))
	(lower-brows         mot-lev (((Bll 0 400 5)(Blc 0 400 5)(Blm 0 400 5)
				       (Brl 0 400 5)(Brc 0 400 5)(Brm 0 400 5))))
	(brows-in-v-shape    mot-lev (((Bll 0 400 90)(Blc 0 300 40)(Blm 0 400 10)
				       (Brl 0 400 90)(Brc 0 300 40)(Brm 0 400 10))))
        (brows-in-roof-shape mot-lev (((Bll 0 400 10)(Blc 0 300 50)(Blm 0 400 90)
				       (Brl 0 400 10)(Brc 0 300 50)(Brm 0 400 70))))
	(brows-in-n-shape    mot-lev (((Bll 0 400 50)(Blc 0 300 90)(Blm 0 400 50)
				       (Brl 0 400 50)(Brc 0 300 90)(Brm 0 400 50))))
	(brows-in-pensive-shape mot-lev (((Bll 0 400 95)(Blc 0 300 40)(Blm 0 400 40)
					  (Brl 0 400 50)(Brc 0 300 10)(Brm 0 400 5))))
	(squint mot-lev (((Elu 0 300 60)(Eru 0 300 60)(Ell 0 300 20)(Erl 0 300 20))))
	(half-closed-eyes mot-lev (((Elu 0 500 50)(Eru 0 500 60))))
	(lower-lids-neutral mot-lev (((Erl 0 300 30)(Ell 0 300 50))))
	(lower-lids-up mot-lev (((Ell 0 300 0)(Erl 0 400 0))))
	(pull-l-mouth-corner mot-lev (((Mlh 0 500 90))))
	(quickly-glance-sideways-and-back act (((gaze-right 0 100)(gaze-at-zero-zero 100 100))))
	
	;symbolic
	(shake-head act (((turn-head-left      0  50)
			  (turn-head-right    50 100)
			  (turn-head-left    100 100)
			  (head-at-zero-zero 150 50))
			 ))
	(nod       mot-lev (((Hv 0 105 -15)(Hv 105 100 0)))) 
	(wink      mot-lev (((Elu 0 100 0)(Elu 300 100 90))))
	(say-ahh   mot-lev (((Sp 0 250 "[_<,110>aa<550,100>]"))))
	(gaze-up   mot-lev (((Plv 0 100 30)(Plh 0 100 40)(Prv 0 100 30)(Prh 0 100 40))))
	(gaze-away mot-lev (((Plv 0 100 20)(Plh 0 100 -30)(Prv 0 100 20)(Prh 0 100 -30))))
	(gaze-right mot-lev (((Plv 0 100 20)(Plh 0 100 40)(Prv 0 100 20)(Prh 0 100 40))))

	;emotional symbols (emblems)
	(smile mot-lev (((Mlh 0 400 99)(Mrh 0 400 99)(Mlv 0 200 99)(Mrv 0 200 99))))
	(smile-a-little mot-lev (((Mlh 0 400 99)(Mrh 0 400 99)(Mlv 0 200 88)(Mrv 0 200 88))))
	(grin-broadly   mot-lev (((Mrh 0 1000 80)(Mrv 0 500 60)(Mlh 0 1000 80)(Mlv 0 500 60))))
	(grin-a-little  mot-lev (((Mrh 0 1000 75)(Mrv 0 500 55)(Mlh 0 200 40)(Mlv 0 200 50))))

  	;self adjustors
	(blink act (((close-eyes 0 50)(open-eyes 50 50))))
	(blink-slowly act (((close-eyes 0 300)(open-eyes 300 200))))
	
      ; FUNCTIONAL DEFINITIONS
	;Back channel feedback / turn control
	(say-aha   mot-lev (((Sp 0 250 "[_<,110>aahxaa<250,130>]"))))
	(look-pensive act (((gaze-away 0 100)(pull-l-mouth-corner 300 500)) ;option 1
			   ))
	(look-aloof   act (((gaze-away 0 50)(turn-head-left 200 1000)(raise-brows 800 800))))
	(look-puzzled act (((squint 200 200)(brows-in-roof-shape 0 400))))
	(look-drowsy  act (((half-closed-eyes 0 800)(lower-lids-neutral 0 400))))
	(show-give-turn act (((face-neutral    0  200) (gaze-at-zero-zero 0 100)
			      (head-at-zero-zero 0 600)(raise-brows     0  200))))
        (show-take-turn act (((open-mouth-wide 0  100)
			      (quickly-glance-sideways-and-back 0 300)(blink-slowly 300 400))   ;option 1
			     ((eyebrow-greet   0  500)(quickly-glance-sideways-and-back 0 200)) ;option 2
			     ))
	(hesitate act (((say-ahh 0 400)) ;option 1
		       ((gaze-up 0 200)) ;option 2
		       ((look-pensive 0 600)))) ;option 3

; Notice that show-give-turn is controlled from the DKB, but should be composed completely here.
; Below action for illustrative purposes only - it is incorrect, 2/19/96
;	(show-give-turn act (((look-at user))))
 
	(show-listening act (((blink-slowly  0 500))))
	(back-ch-feedb-normal act (((say-aha 0 100)) ;option 1
				   ((nod     0 200)) ;option 2
				   ))
	;other
	(happy act (((raise-brows 0 400)(brows-in-n-shape 400 200)(lower-lids-up 0 300)
		     (open-eyes-wide 0 300)(smile 0 200))))
	(greet act (((eyebrow-greet 0 1500))))
	(eyebrow-greet act (((raise-brows 0 200)(upper-lids-open-wide 0 200)
			     (brows-neutral 900 200)(eyes-neutral 1000 300))))
	;acknowledge (ack)  
	(ack-normal    act (((say-ok-normal 0 250))))
	(say-ok-normal mot-lev (((Sp 0 250 "[_<,120>ow<,130>kehiy<250,95>]"))))
	(say-ok-bored  mot-lev (((Sp 0 250 "[ow<,130>k<100,100>ehiy]"))))
	(say-all-right-normal mot-lev (((Sp 0 250 "[<,120>ow<,130>lraet<250,95>]"))))

	;other
	(close-eyes         mot-lev (((Eru 0 300 10)(Elu 0 300 10))))
	(open-eyes          mot-lev (((Eru 0 300 75)(Elu 0 300 80)))) 
	(open-mouth-wide    mot-lev (((Mb 0 400 60))))
	(close-mouth-tight  mot-lev (((Mb 0 300 15))))
	(open-eyes-wide     mot-lev (((Elu 0 100 99)(Eru 0 100 95)(Ell 0 100 95)(Erl 0 100 95))))

      ; WILDCARD SPEECH
	;Star is replaced by a value from the spatial knowledge base
	(deliver-speech     mot-lev (((Sp 0 250 *))))

      ; SPATIAL DEFINITIONS
	;gaze ;Star is replaced by a value from the spatial knowledge base
	(gaze-at-user spatial-mot-lev (((Plh 0 250 *)(Plv 0 250 *)(Prh 0 250 *)(Prv 0 250 *))))
	(look-at spatial-mot-lev (((Plh 0 250 *)(Plv 0 250 *)(Prh 0 250 *)(Prv 0 250 *))))
	(turn-to spatial-mot-lev (((Hh  0 1000 *)(Hv  0 800 *))))

	;head (mostly for debugging)
        (turn-head-toward spatial-mot-lev (((Hh 0 900 *)(Hv 0 900 *))))
	(turn-head-left  mot-lev (((Hh 0 150 -20))))
        (turn-head-right mot-lev (((Hh 0 150  20))))
	(turn-head-up    mot-lev (((Hv 0 300  20))))
	(turn-head-down  mot-lev (((Hv 0 300    -20))))
	(head-at-zero-horiz mot-lev (((Hh 0 500   0))))
	(head-at-zero-vert  mot-lev (((Hv 0 500   0))))
	(turn-head-90-left  mot-lev (((Hh 0 150 -90))))
	(turn-head-90-right mot-lev (((Hh 0 150  90))))
	(turn-head-45-right mot-lev (((Hh 0 500  45))))
	(turn-head-45-left  mot-lev (((Hh 0 500 -45))))

      ; MANUAL GESTURE

	;motor level
	(gest-very-slow-beat mot-lev (((Gb 0 8000 0))))
	(gest-slow-beat  mot-lev     (((Gb 0 1500 0))))
        (gest-fast-beat  mot-lev     (((Gb 0 600  0))))
        (gest-rest       mot-lev     (((Gr 0 1000 0))))
	(hand-raise-palm-fwd mot-lev (((Gw 0 600  0))))
	(drum-with-fingers   mot-lev (((Gd 0 600  0))))
	(point-to-big-screen mot-lev (((Gp 0 1000 0))))

	;act level [functional descriptions]
	(gest-greet      act (((hand-raise-palm-fwd 0 600))
			      ((hand-raise-palm-fwd 0 1000))))
	(manual-hold-it-signal act (((hand-raise-palm-fwd 0 900))))
	(restless act (((drum-with-fingers 0 400))))

	;bilbo demo gestures
	(drive   mot-lev (((Gc 0 0 0))))
	(stumble mot-lev (((Gf 0 0 0))))
	(walk    mot-lev (((Gp 0 0 0))))
	
	)) ;End all


;------------------------------------------
; MOTOR SPECIFICATIONS
;------------------------------------------

; Notice that "right" and "left" refer 
; to the *agent's* right and left.

(defparameter *motor-list* nil)

(setf  *motor-list*                                  ;Control numbers 
   ;TEMPLATE: (name class number default-time)       ; on animation side
  '((Brl motor   3  700) ;Brow, right, lateral       ; 
    (Brc motor   2  700) ;Brow, right, central       ; 
    (Brm motor   1  700) ;Brow, right, medial        ; 
    (Blm motor   4  700) ;Brow, left,  medial        ;
    (Blc motor   5  700)                             ;
    (Bll motor   6  700)                             ;
    (Elu motor   8  300) ;Eye, right, upper          ;
    (Ell motor  10  700) ;Eye, left,  upper          ;
    (Plh motor 151    0) ;Pupil, right, horizontal   ;
    (Plv motor 150    0) ;Pupil, right, vertical     ;
    (Eru motor   7  300) ;Eye, right, upper          ;
    (Erl motor   9  700) ;Eye, left,  lower          ;
    (Prh motor 161    0) ;Pupil, left, horizontal    ;
    (Prv motor 160    0)                             ;
    (Mb  motor  12  800) ;Mouth, bottom              ;
    (Mrv motor 140  800) ;Mouth, left, vertical      ;
    (Mrh motor 141  800)                             ;
    (Mlv motor 130  800)                             ;     
    (Mlh motor 131  800)                             ;
    (Hh  motor 171 1000) ;Head                       ;
    (Hv  motor 170 1000)                             ;
    (Gr  motor  30 1000) ;Gesture  "rest"            ;
    (Gw  motor  31 1000) ;         "waive"           ;
    (Gb  motor  32 1000) ;         "beat"            ;
    (Gd  motor  33 1000) ;         "drum"            ;
    (Gp  motor  34 1000) ;         "pace"/"point"    ;
    (Gc  motor  35 1000) ;         "car"             ;
    (Gf  motor  36 1000) ;         "finger"          ;
    (Sp  motor   0    0) ;Speak                      ;
    ))


(defvar Brl-m   3)     ;             
(defvar Brc-m   2)     ;                All ranges are 0 - 100
(defvar Brm-m   1)     ;
(defvar Blm-m   4)     ;                   __________________
(defvar Blc-m   5)     ;                 /                   \
(defvar Bll-m   6)     ;               /                       \
(defvar Eru-m   7)     ;             /                           \        170 [0 - 90]
(defvar Erl-m   9)     ;           /     3--2--1     4--5--6       \        ^
(defvar Prh-m 151)     ;         /                                   \      |
                       ;        |           7            8            |     |
(defvar Prv-m 150)     ;        |        /     \      /     \         |     |
(defvar Elu-m   8)     ;        |       + 150/1 +    + 160/1 +        |-----------
(defvar Ell-m  10)     ;        |        \     /      \     /         |     |
(defvar Plh-m 161)     ;        |           9            10           |     |
(defvar Plv-m 160)     ;         \                (11)               /      |
(defvar Mb-m   12)     ;           \            ---+---            /       \/
(defvar Mlv-m 140)     ;             \        /          \       /        170 [0 - -90]
(defvar Mlh-m 141)     ;               \  130/1        140/1   /
(defvar Mrv-m 130)     ;                 \   \           /   / 
(defvar Mrh-m 131)     ;                   \   ----12---   /
(defvar Hh-m  171)     ;                     \           /
(defvar Hv-m  170)     ;                       ---------
(defvar G0-m   20)     ;                           |
(defvar Ga-m   21)     ;                           |
(defvar Gd-m   22)     ;            171 [90]<----- 0------> 171 [-90]
(defvar Sp-m    0)     ;                           |
                       ;                           |
                       ;                        
                       ;                        SPEAK: 0
                       ;

(defclass act ()
  ((name   :accessor name   :initarg :name   :initform nil)
   (acts   :accessor acts   :initarg :acts   :initform nil)
   (delay  :accessor delay  :initarg :delay  :initform 0)
   (execution-time :accessor exec-time  :initarg :exec-time  :initform nil)
   ))

(defclass motor (act)
  ((ctrlpt :accessor ctrlpt :initarg :ctrlpt :initform nil)
   (pos    :accessor pos    :initarg :pos    :initform nil) ;only used in making out-obj.
   (start-time :accessor start-time  :initarg :start-time :initform nil) ;stamped when initiated.
   ))

(defclass mot-lev (act) ())

(defclass spatial ()  ; Spatial class is EXECUTEd differently from other acts
  ((coords :accessor coords :initarg :coords :initform nil)
   ))

(defclass spatial-mot-lev (spatial mot-lev)
  ()) ;treated as mot-lev when TRACEd-DOWN

(defclass spatial-act (spatial act)
  ()) ;treated as act when TRACEd-DOWN


;----------- VOICES -----------

(defun init-voice ()
  (load "my-net-io")
  (speak "[:nr :dv ap 130]"))

(defun gandalf-voice ()
  (load "my-net-io")
  (say "[:nr :dv ap 130]"))

(defun roland-voice ()
  (load "my-net-io")
  (say "[:nr :dv ap 180 hs 136 gn 65 as 20 ri 100 sm 50]"))

(defun bilbo-voice ()
  (load "my-net-io")
  (say "[:nr :dv ap 280 hs 76 gn 65 as 20]"))

;(gandalf-voice) ;defaults to Gandalf's voice...

;----------- LOAD -----------

(defvar act-in-socket-# 1998)
(setf   act-in-socket-# 1999)

(defun compile-all ()
  (compile-file "act-defs.lisp")
  (compile-file "Motor2.lisp"))

(defun compile-net ()
  (compile-file "my-net-io.lisp")
  (compile-file "foreign2.lisp"))

(defun load-all ()
  (load "act-defs")
  (load "Motor2"))

(load "Motor2")
(make-all-objects)





