%%%===================================================%%%
%%% 		STEP Action Library				%%%
%%%   http://wasp.cs.vu.nl/step				%%%
%%%									%%%
%%%	Version 0.35 May 28, 2002				%%%
%%%									%%%
%%% 	Vrije University Amsterdam				%%%
%%%	Intelligent Multimedia Group				%%%
%%% (C) Zhisheng Huang						%%%
%%%===================================================%%%

:-object step_scriptlib : [ bcilib ].	

script(walk(Agent), Action) :-
		Action= seq([
				par([turn(Agent,r_shoulder,back_down2,fast),
				turn(Agent,r_hip,front_down2,fast),
		turn(Agent,l_shoulder,front_down2,fast),
		turn(Agent,l_hip,back_down2,fast)]),
		par([turn(Agent,l_shoulder,back_down2,fast),
		turn(Agent,l_hip,front_down2,fast),
		turn(Agent,r_shoulder,front_down2,fast),
		turn(Agent,r_hip,back_down2,fast)])
		]),
		!.


script(walk0(Agent), ActionList) :-
		ActionList = [
				parallel([turn(Agent,r_shoulder,back_down2,fast),
				turn(Agent,r_hip,front_down2,fast),
		turn(Agent,l_shoulder,front_down2,fast),
		turn(Agent,l_hip,back_down2,fast)]),
		parallel([turn(Agent,l_shoulder,back_down2,fast),
		turn(Agent,l_hip,front_down2,fast),
		turn(Agent,r_shoulder,front_down2,fast),
		turn(Agent,r_hip,back_down2,fast)])
		],
		!.

script(walk(Agent,StepLength), ActionList) :-
		ActionList = [parallel([script(walk(Agent)),
						move(Agent,increment(0.0,0.0,StepLength),fast)
						])
				
				],
		!.

script(get_adjustY(Agent, Y), ActionList) :-
		ActionList = [ do(getBodyObjectName(Agent, l_ankle, ObjectName)),
				   do(getSFVec3f(ObjectName,translation,_X1,Y1,_Z1)),
				   turn(Agent,l_hip,back_down2,fast),
				   do(getSFVec3f(ObjectName,translation,_X2,Y2,_Z2)),
				   do(Y is Y2 - Y1),
				   turn(Agent,l_hip,down,fast)
				   ],
				   !.


script(walk_adjustY(Agent,Y,StepLength), ActionList) :-
		ActionList = [do(Y1 is -Y),
				  parallel([script(walk(Agent)),
				 [move(Agent,increment(0.0,Y,StepLength),very_fast),
				  move(Agent,increment(0.0,Y1,StepLength),very_fast)]
						])
				
				],
		!.




	script(fast_walk(Agent), ActionList) :-
		ActionList = [
			set_tempo(300),
			parallel([turn(Agent,r_shoulder,back_down2,fast),
		turn(Agent,r_hip,front_down2,fast),
		turn(Agent,l_shoulder,front_down2,fast),
		turn(Agent,l_hip,back_down2,fast)]),
		parallel([turn(Agent,l_shoulder,back_down2,fast),
		turn(Agent,l_hip,front_down2,fast),
		turn(Agent,r_shoulder,front_down2,fast),
		turn(Agent,r_hip,back_down2,fast)])
		],
		!.

	script(walk_front(Agent), ActionList) :-
		ActionList = [
		parallel([turn(Agent,r_shoulder,back_down2,fast),
				turn(Agent,r_hip,front_down2,fast),
				move(Agent,front,fast),
				turn(Agent,l_shoulder,front_down2,fast),
				turn(Agent,l_hip,back_down2,fast)]),
		parallel([turn(Agent,l_shoulder,back_down2,fast),
				turn(Agent,l_hip,front_down2,fast),
				move(Agent,front,fast),
				turn(Agent,r_shoulder,front_down2,fast),
				turn(Agent,r_hip,back_down2,fast)])
		],
		!.
	script(jump0(Agent), ActionList) :-
		ActionList = [
		parallel([turn(Agent,r_knee,back,fast),
				turn(Agent,r_hip,front_down,fast),
				move(Agent,humanoidroot,down,fast),
				turn(Agent,r_shoulder,up,fast),
				turn(Agent,l_shoulder,up,fast),
				turn(Agent,l_knee,back,fast),
				turn(Agent,l_hip,front_down,fast)]),
		parallel([turn(Agent,l_knee,down,fast),
				turn(Agent,l_hip,down,fast),
				move(Agent,humanoidroot,up,fast),
				turn(Agent,r_shoulder,right,fast),
			 	turn(Agent,l_shoulder,left,fast),
				turn(Agent,r_knee,down,fast),
				turn(Agent,r_hip,down,fast)]),
		parallel([move(Agent,humanoidroot,up,fast),
			   turn(Agent,r_shoulder,down_from_front,fast),
			   turn(Agent,l_shoulder,down_from_front,fast)]),
		
		move(Agent,down,fast)
		],
		!.

script(jump(Agent), Action) :-
		Action = seq([
		par([turn(Agent,r_knee,back,fast),
				turn(Agent,r_hip,front_down,fast),
				move(Agent,humanoidroot,down,fast),
				turn(Agent,r_shoulder,up,fast),
				turn(Agent,l_shoulder,up,fast),
				turn(Agent,l_knee,back,fast),
				turn(Agent,l_hip,front_down,fast)]),
		par([turn(Agent,l_knee,down,fast),
				turn(Agent,l_hip,down,fast),
				move(Agent,humanoidroot,up,fast),
				turn(Agent,r_shoulder,right,fast),
			 	turn(Agent,l_shoulder,left,fast),
				turn(Agent,r_knee,down,fast),
				turn(Agent,r_hip,down,fast)]),
		par([move(Agent,humanoidroot,up,fast),
			   turn(Agent,r_shoulder,down_from_front,fast),
			   turn(Agent,l_shoulder,down_from_front,fast)]),
		
		move(Agent,humanoidroot,down,fast)
		]),
		!.

	
script(stand1(Agent), Action) :-
		Action = seq([par([turn(Agent,l_shoulder,down,fast),
		turn(Agent,skullbase,front,fast),
		turn(Agent,r_shoulder,down,fast),
		turn(Agent,l_hip,down,fast),
		turn(Agent,r_hip,down,fast),
		turn(Agent,l_knee,back_down2,fast),
		turn(Agent,r_knee,back_down2,fast),
		turn(Agent,r_wrist,down,fast),
		turn(Agent,l_wrist,down,fast),
		turn(Agent,r_elbow,down,fast),
		turn(Agent,l_elbow,down,fast)
		])
		]),
		!.

script(pose_reset(Agent), Action) :-
		Action = seq([par([turn(Agent,l_shoulder,down,fast),
		turn(Agent,skullbase,front,fast),
		turn(Agent,r_shoulder,down,fast),
		turn(Agent,l_hip,down,fast),
		turn(Agent,r_hip,down,fast),
		turn(Agent,l_knee,down,fast),
		turn(Agent,r_knee,down,fast),
		turn(Agent,r_wrist,down,fast),
		turn(Agent,l_wrist,down,fast),
		turn(Agent,r_elbow,down,fast),
		turn(Agent,l_elbow,down,fast)
		]),
			turn(Agent,humanoidroot,front,fast),
			par([turn(Agent,vl5,front,fast),
			turn(Agent,sacroiliac,front,fast)
			])
		]),
		!.


	script(stand2(Agent), Action) :-
		Action = seq([
		turn_init(Agent,r_shoulder,down),
		par([turn(Agent,l_shoulder,down,fast),
		turn(Agent,r_shoulder,down,fast),
		turn(Agent,skullbase,front,fast),
		turn(Agent,l_hip,down,fast),
		turn(Agent,r_hip,down,fast)]),
		par([turn(Agent,l_knee,down,fast),
		turn(Agent,r_knee,down,fast)])
		]),
		!.
	script(kicking(Agent), Action) :-
		Action = seq([
			par([turn(Agent,r_hip,back_down,fast),
					turn(Agent,r_knee,back,fast),
					turn(Agent,r_shoulder,front_down2,fast),
					turn(Agent,l_shoulder,back_down2,fast)
					]),
			par([turn(Agent,r_hip,front,fast),
					turn(Agent,r_knee,front_down2,fast),
					turn(Agent,l_shoulder,front_down2,fast),
					turn(Agent,r_shoulder,back_down2,fast)
					])
		]),
		!.
	
	
				
	script(kneeing(Agent), Action) :-
		Action = seq([par([turn(Agent, r_elbow, front, fast), 
				turn(Agent, l_elbow, front, fast)]),
				par([turn(Agent, l_hip, front_down2, fast),
				turn(Agent, r_hip, front_down2, fast),
				turn(Agent, l_knee, back, fast),
				turn(Agent, r_knee, back, fast)])
		]),
		!.
	script(start_run(Agent), Action) :-
		Action = seq([par([turn(Agent, r_elbow, front, fast), 
				turn(Agent, l_elbow, front, fast)]),
				par([turn(Agent, l_hip, front_down2, fast),
				turn(Agent, r_hip, front_down2, fast),
				turn(Agent, l_knee, back_down, fast),
				turn(Agent, r_knee, back_down, fast)])
		]),
		!.

	script(hold(Agent,DirectionL,DirectionR,Speed), Action) :-
		Action = par([turn(Agent, l_shoulder, DirectionL, Speed), 
				turn(Agent, r_shoulder, DirectionR, Speed)]),
		!.

	script(hold_reset(Agent), Action) :-
		Action = par([turn(Agent, l_shoulder, down, fast), 
				turn(Agent, r_shoulder, down, fast)]),
		!.


	script(run1(Agent), Action) :-
		Action = seq([par([turn(Agent,r_shoulder,back_down2,fast),
		turn(Agent,r_hip,front,fast),
		turn(Agent,l_shoulder,front_down2,fast),
		turn(Agent,l_hip,back_down2,fast)]),
		par([turn(Agent,l_shoulder,back_down2,fast),
		turn(Agent,l_hip,front,fast),
		turn(Agent,r_shoulder,front_down2,fast),
		turn(Agent,r_hip,back_down2,fast)])
		]),
		!.




	script(run(Agent), Action) :-
		Action = seq([par([turn(Agent,r_shoulder,back_down2,fast),
		turn(Agent,r_hip,front_down,fast),
		turn(Agent,l_shoulder,front_down2,fast),
		turn(Agent,l_hip,back_down2,fast)]),
		par([turn(Agent,l_shoulder,back_down2,fast),
		turn(Agent,l_hip,front_down,fast),
		turn(Agent,r_shoulder,front_down2,fast),
		turn(Agent,r_hip,back_down2,fast)])
		]),
		!.



	script(run_step(Agent,position(Xmid,Ymid,Zmid),position(X,Y,Z),Field), Action) :-
		Action = seq([par([do(setSFVec3f(Agent,Field, Xmid, Ymid, Zmid)),
		turn(Agent,r_shoulder,back_down2,fast),
		turn(Agent,r_hip,front_down,fast),
		turn(Agent,l_shoulder,front_down2,fast),
		turn(Agent, r_elbow, front, fast), 
		turn(Agent, l_elbow, front, fast),
		turn(Agent, l_knee, back_down, fast),
		turn(Agent, r_knee, back_down, fast),
		turn(Agent,l_hip,back_down2,fast)]),
		par([do(setSFVec3f(Agent,Field, X, Y, Z)),
		turn(Agent,l_shoulder,back_down2,fast),
		turn(Agent,l_hip,front_down,fast),
		turn(Agent,r_shoulder,front_down2,fast),
		turn(Agent,r_hip,back_down2,fast)])
		]),
		!.

	script(l_wave(Agent), Action) :-
		Action = seq([turn(Agent,l_shoulder,left_front_up,fast),
		turn(Agent,l_shoulder,left_front2_up,fast)]),
		!.
	script(l_wave2(Agent), Action) :-
		Action = seq([turn(Agent,l_shoulder,left_front_up,fast),
		turn(Agent,l_shoulder,up_turn_90,fast)]),
		!.
	script(r_wave(Agent), Action) :-
		Action = seq([turn(Agent,r_shoulder,right_front_up,fast),
		turn(Agent,r_shoulder,right_front2_up,fast)]),
		!.
	script(r_wave2(Agent), Action) :-
		Action = seq([turn(Agent,r_shoulder,right_front_up,fast),
		turn(Agent,r_shoulder,up_turn_90,fast)]),
		!.
	script(greeting(Agent), Action) :-
		Action = seq([par([
				turn_init(Agent,r_shoulder,right_front_up),
				repeat(l_wave2(Agent),8),
				repeat(r_wave2(Agent),8)]),
				  script(stand2(Agent)),
				  wait(1.0)
		]),
		!.
	script(open_leg1(Agent), Action) :-
		Action = seq([par([turn(Agent,l_hip,side1_down,fast),
				turn(Agent,r_hip,side1_down,fast)])]),
		!.

	script(squat(Agent,Y), Action) :-
		Action = 
				seq([	
			par([
				turn(Agent,l_hip,front,fast),
				turn(Agent,r_hip,front,fast),
				turn(Agent,l_knee,back_up,fast),
				turn(Agent,r_knee,back_up,fast),
				turn(Agent,vl5,front_up,fast),
				turn(Agent,skullbase,back_up,fast)]),
                  move(Agent,humanoidroot,increment(0.0,Y,0.0),fast)
				]),
		!.

	
	
	script(test(Agent), Action) :-
		Action = seq([
		do(format('Agent action: walking~n')),
		repeat(walk(Agent), 6),
		do(format('Agent action: kicking~n')),
		repeat(kicking(Agent), 6),
		script(stand1(Agent)),
		do(format('Agent action: running~n')),
		script(start_run(Agent)),
		repeat(run(Agent), 6),
		script(stand1(Agent)),
		do(format('Agent action: jumping~n')),
		repeat(jump(Agent), 6),
		script(stand1(Agent)),
		do(format('Agent action: greeting~n')),
		repeat(greeting(Agent), 3),
		script(stand1(Agent))
		]),
		!.

	script(run0(Agent,Times), Action) :-
		Action = seq([
		script(start_run(Agent)),
		repeat(run(Agent), Times),
		script(stand1(Agent))
		]),
		!.

	script(walk0(Agent,Times), Action) :-
		Action = seq([
		repeat(walk(Agent), Times),
		script(stand1(Agent))
		]),
		!.

	script(kick0(Agent,Times), Action) :-
		Action = seq([
		repeat(kicking(Agent), Times),
		script(stand1(Agent))
		]),
		!.

	script(jump0(Agent,Times), Action) :-
		Action = seq([
		repeat(jump(Agent), Times),
		script(stand1(Agent))
		]),
		!.

	script(greeting0(Agent,Times), Action) :-
		Action = seq([
		repeat(greeting(Agent), Times),
		script(stand1(Agent))
		]),
		!.



%%@ AE included stage 0 (with phrases)

	script(taichi(Agent), Action) :-
		Action = seq([
		do(format('Agent action: taichi~n')),
		script(taichi(Agent, stage0)),
	%	script(taichi(Agent, stage2)),
		script(stand1(Agent))
		]),
		!.

script(taichi(Agent, stage0), Action) :-
		Action = seq([
		speak(Agent, 'turn the hips'),
		turn(Agent,l_hip,side1_down,fast),
		turn(Agent,r_hip,side1_down,fast),
		style(Agent,text=(0,0,1)),
		speak(Agent,'slowly'),
		style(Agent,color=(1,0,0)),
		speak(Agent,'raise your arms~n'),
		par([turn(Agent,l_shoulder,front,slow),
			    turn(Agent,r_shoulder,front,slow)]),
		par([turn(Agent,l_shoulder,front_down,slow),
			    turn(Agent,r_shoulder,front_down,slow),
			    turn(Agent,l_elbow,front_right2,slow),
			    turn(Agent,r_elbow,front_left2,slow)]),
		speak(Agent,'now, just watch~n'),
		par([
			    move(Agent,humanoidroot, increment(0.0,-0.1,0.0),slow),
			    turn(Agent,l_hip,left_front_down,slow),
			    turn(Agent,r_hip,right_front_down,slow),
			    turn(Agent,l_elbow,right_front_down,slow),
			    turn(Agent,r_shoulder,front_down2,slow),
			    turn(Agent,l_knee,back2_down,slow),
			    turn(Agent,r_knee,back2_down,slow)]),
		speak(Agent,'complete the first stage~n'),
		par([turn(Agent,l_shoulder,left_front_up2,very_slow),
				turn(Agent,r_wrist,turn_n45,slow),
				turn(Agent,r_elbow,front,slow),
			    turn(Agent,r_shoulder,front_left,very_slow)]),
		speak(Agent,'and slowly turn the wrists~n'),
		par([turn(Agent,l_wrist,turn_90,slow),
			    turn(Agent,r_wrist,turn_90,slow)])
		]),
		!.

script(taichi(Agent, stage1), Action) :-
		Action =seq([
		turn(Agent,l_hip,side1_down,fast),
		turn(Agent,r_hip,side1_down,fast),
		par([turn(Agent,l_shoulder,front,slow),
			    turn(Agent,r_shoulder,front,slow)]),
		par([turn(Agent,l_shoulder,front_down,slow),
			    turn(Agent,r_shoulder,front_down,slow),
			    turn(Agent,l_elbow,front_right2,slow),
			    turn(Agent,r_elbow,front_left2,slow)]),
		par([
			    move(Agent,humanoidroot,increment(0.0,-0.1,0.0),slow),
			    turn(Agent,l_hip,left_front_down,slow),
			    turn(Agent,r_hip,right_front_down,slow),
			    turn(Agent,l_elbow,right_front_down,slow),
			    turn(Agent,r_shoulder,front_down2,slow),
			    turn(Agent,l_knee,back2_down,slow),
			    turn(Agent,r_knee,back2_down,slow)]),
		par([turn(Agent,l_shoulder,left_front_up2,very_slow),
				turn(Agent,r_wrist,turn_n45,slow),
				turn(Agent,r_elbow,front,slow),
			    turn(Agent,r_shoulder,front_left,very_slow)]),
		par([turn(Agent,l_wrist,turn_90,slow),
			    turn(Agent,r_wrist,turn_90,slow)])
		]),
		!.

script(taichi(Agent, stage2), Action) :-
		Action = seq([
		par([turn(Agent,humanoidroot,turn_n45,very_slow),
		turn(Agent,r_shoulder,right_front_down,very_slow),
		turn(Agent,r_knee,back_down2,very_slow)]),
		par([turn(Agent,l_shoulder,right_front_down,fast),
		turn(Agent,l_elbow,front,fast)]),
		par([turn(Agent,r_knee,back,slow),
		turn(Agent,l_knee,back,slow),
		turn(Agent,l_hip,front_down1,slow)]),
		par([turn(Agent,l_hip,left_down,slow),
		turn(Agent,l_shoulder,front,slow),
		turn(Agent,r_shoulder,front,slow),
		turn(Agent,l_elbow,down,slow),
		turn(Agent,r_elbow,down,slow),
		turn(Agent,l_knee,down,slow)]),
		par([turn(Agent,l_hip,front_down,slow),
		turn(Agent,l_shoulder,front_down,slow),
		turn(Agent,r_shoulder,front_down,slow),
		turn(Agent,l_elbow,front,slow),
		turn(Agent,r_elbow,front,slow),
		turn(Agent,l_knee,back,slow)])
		]),
		!.


:-end_object step_scriptlib.

