%
% segDirection(i,a,d)
%
% The direction of the segment from point i to point i+1
% is a (horiz,vert), d (plus,minus).
%

segDirection(I1,horiz,plus) :-
	I2=I1+1,
	fold(I1,X1,Y),
	fold(I2,X2,Y),
	X2>X1.

segDirection(I1,horiz,minus) :-
	I2=I1+1,
	fold(I1,X1,Y),
	fold(I2,X2,Y),
	X2<X1.

segDirection(I1,vert,plus) :-
	I2=I1+1,
	fold(I1,X,Y1),
	fold(I2,X,Y2),
	Y2>Y1.

segDirection(I1,vert,minus) :-
	I2=I1+1,
	fold(I1,X,Y1),
	fold(I2,X,Y2),
	Y2<Y1.


%
% foldDirection(i,d)
%
% The direction of the fold at point i is
% d (clock,anticlock,none).
%

foldDirection(I1,none) :-
	I0=I1-1,
	segDirection(I0,A,D),
	segDirection(I1,A,D).

foldDirection(I1,clock) :-
	I0=I1-1,
	segDirection(I0,A0,D0),
	segDirection(I1,A1,D1),
	clockFold(A0,D0,A1,D1).

foldDirection(I1,anticlock) :-
	I0=I1-1,
	segDirection(I0,A0,D0),
	segDirection(I1,A1,D1),
	anticlockFold(A0,D0,A1,D1).


clockFold(vert,plus,horiz,plus).
clockFold(horiz,plus,vert,minus).
clockFold(vert,minus,horiz,minus).
clockFold(horiz,minus,vert,plus).

anticlockFold(vert,plus,horiz,minus).
anticlockFold(horiz,minus,vert,minus).
anticlockFold(vert,minus,horiz,plus).
anticlockFold(horiz,plus,vert,plus).

%
% requiredMove(i,d)
%
% The pivot move <point i,turn direction> is required.
%
requiredMove(I,D) :-
	D != none,
	foldDirection(I,D).


%stop_smart_decisions.
smart_decisions :- not stop_smart_decisions.

block_choice_decisions :- just_count.
block_choice_decisions :- smart_decisions.


next(I1,I2) :-
smart_decisions,
	requiredMove(I1,D1),
	requiredMove(I2,D2),
	I1 < I2,
	not moveBetween(I1,I2).

-first(I1) :-
smart_decisions,
	requiredMove(I1,D1),
	requiredMove(I2,D2),
	next(I1,I2).

first(I) :-
smart_decisions,
	requiredMove(I,D),
	not -first(I).

moveBetween(I1,I2) :-
smart_decisions,
	requiredMove(I1,D1),
	requiredMove(I2,D2),
	I1 < I2,
	requiredMove(I3,D3),
	I1 < I3,
	I3 < I2.

pivot(1,I,D) :-
smart_decisions,
	first(I),
	requiredMove(I,D).

pivot(N1,I1,D1) :-
smart_decisions,
	pivot(N2,I2,D2),
	step(N1), step(N2), N1 = N2 + 1,
	requiredMove(I1,D1),
	requiredMove(I2,D2),
	next(I1,I2).


% If the Time given is more than the number of required moves,
% do useless pivot moves on element 1 to pass the time!

index(1..N) :- length(N).
dir(clock).
dir(anticlock).
numRequiredMoves(N) :- step(N), N{ requiredMove(I,D) : index(I) : dir(D) }N.

uselesspivotneeded :- smart_decisions, time(T), numRequiredMoves(N), T>N.

pivot(S,1,clock) :-
smart_decisions,
	uselesspivotneeded,
	step(S), numRequiredMoves(N),
	S > N,
	(S - N) #mod 2 == 1.

pivot(S,1,anticlock) :-
smart_decisions,
	uselesspivotneeded,
	step(S), numRequiredMoves(N),
	S > N,
	(S - N) #mod 2 == 0.

%
% Consider MORE moves
%
1{ pivot(S,I,D) : step(S) : S<=T }1 :- requiredMove(I,D), time(T), not block_choice_decisions.
0{ pivot(S,I,D) : step(S) : S<=T }1 :- index(I), dir(D), not requiredMove(I,D), time(T), numRequiredMoves(RM), RM < T, not block_choice_decisions.

% There's a solution only if 
% - the given time is equal to the number of required moves
% - or greater than it by an even number (time for useless pivots back 
%   and forth)

:- time(T), numRequiredMoves(N), T<N.
:- time(T), numRequiredMoves(N), T>N, (T-N) #mod 2 == 1.


:- step(S), pivot(S,I1,D1), pivot(S,I2,D2), I1 != I2.
:- step(S), pivot(S,I1,D1), pivot(S,I2,D2), D1 != D2.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tfoldx(1,I,N) :-
	fold(I,X,Y),
	length(N).
tfoldy(1,I,N+I-1) :-
	fold(I,X,Y),
	length(N).

step(1..T) :- time(T).

coord(0..2*N) :- length(N).
xcoord(-2*N..2*N) :- length(N).

%
% Effect of pivot(t,i,d)
%
tfoldy(S+1,I,Y2) :-
step(S),
	tfoldx(S,I,X1),
	pivot(S,P,D),
	I >= P,
	tfoldx(S,P,XP),
	tfoldy(S,P,YP),
	X0 = X1 - XP,
	rotatedx(D,X0,Y0r),
	Y2 = Y0r + YP.
tfoldx(S+1,I,X2) :-
step(S),
	tfoldy(S,I,Y1),
	pivot(S,P,D),
	I >= P,
	tfoldx(S,P,XP),
	tfoldy(S,P,YP),
	Y0 = Y1 - YP,
	rotatedy(D,Y0,X0r),
	X2 = X0r + XP.

tfoldx(S+1,I,X) :-
	step(S),
	tfoldx(S,I,X),
	pivot(S,P,D),
	I < P.
tfoldy(S+1,I,Y) :-
	step(S),
	tfoldy(S,I,Y),
	pivot(S,P,D),
	I < P.

rotatedx(clock,X,-X) :-
	xcoord(X).
rotatedy(clock,Y,Y) :-
	xcoord(Y).
rotatedx(anticlock,X,X) :-
	xcoord(X).
rotatedy(anticlock,Y,-Y) :-
	xcoord(Y).


%{ pivot(S,I,D) : requiredMove(I,D) }1 :- step(S).
%move_used(I,D) :- pivot(S,I,D), step(S).
%:- requiredMove(I,D), not move_used(I,D).


% No-overlapping constraint
%:- tfoldx(S,I1,X1), tfoldy(S,I1,Y1),
%   tfoldx(S,I2,X1), tfoldy(S,I2,Y1),
%   I1 != I2.

:- tfoldx(S,I1,X1), tfoldy(S,I1,Y1),
   tfoldx(S,I2,X1), tfoldy(S,I2,Y1),
   I1 < I2, 
   % Only need to check for I2 that have moved in the preceding step
   pivot(S-1,P,D),
   I2 > P.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% No-overlap constraint
%required(tfoldx(S,I1)!=tfoldx(S,I2) \/ tfoldy(S,I1)!=tfoldy(S,I2)) :-
%	step(S),
%	index(I1), index(I2),
%	I1<I2,
%	% simplification: we only need to check the parts that have moved
%	step(S-1),
%	pivot(S-1,P,D),
%	I2 > P.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- time(F),
   tfoldx(F+1,I,X1),
   fold(I,X2,Y2),
   X1 != X2.
:- time(F),
   tfoldy(F+1,I,Y1),
   fold(I,X2,Y2),
   Y1 != Y2.
