
:-object wasp5 : [bcilib].

var url = './soccer/soccer1b.wrl'.
var sleeptime=100.
var distance=5.
var timelimit = 500.
var interpolation=5.


main :-
	text_area(Browser),
	set_output(Browser),

	format('Load the game ...~n'),
	loadURL(url),

	format('the game will start in 5 seconds,~n'),
	format('note that the total playing time period is ~w seconds,~n', [timelimit]),
	delay(5000),
	format('the game startup,~n'),
	play_ball(me, ball).


play_ball(Agent, Ball) :-
	-- timelimit,
	timelimit > 0, !,
	format('time left: ~w seconds~n', [timelimit]),
	delay(800),
	near_ball_then_kick(Agent, Ball),
	play_ball(Agent, Ball).

play_ball(_, _).


near_ball_then_kick(Agent, Ball):-
	getViewpointPositionEx(Agent,X,_Y,Z),
	getPosition(Ball,X1,Y1,Z1),
	Xdif is X1-X,
	Zdif is Z1-Z,
	Dist is sqrt(Xdif*Xdif + Zdif*Zdif),
	Dist < 5, !,
      X2 is Xdif*distance,
	Z2 is Zdif*distance,
	X3 is X2 + X1,
	Z3 is Z2 + Z1,
      move_to_position(Ball, position(X1,Y1,Z1),position(X3,Y1,Z3),interpolation).


near_ball_then_kick(_, _).


move_to_position(Ball,_,position(X,Y,Z),0):-
            setPosition(Ball,X,Y,Z).


move_to_position(Ball, position(X1,Y1,Z1),position(X2,Y2,Z2),C):-
		C1 is C-1,
		Xdif is X2 -X1,
		Zdif is Z2 -Z1,
		X is X1 + Xdif/C,
		Z is Z1 + Zdif/C,
		setPosition(Ball,X,Y1,Z),
            sleep(100),
		move_to_position(Ball,position(X,Y1,Z),position(X2,Y2,Z2),C1).






getViewpointPositionEx(_,X,Y,Z) :- getSFVec3f(proxSensor,position,X,Y,Z).
getViewpointOrientationEx(_,X,Y,Z,R):- getSFRotation(proxSensor,orientation,X,Y,Z,R).



:-end_object wasp5.

