%
% 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).


n_reqMoves(N) :- step(N), N{ requiredMove(I,D) : index(I) : dir(D) }N.

%just_count.
%stop_smart_decisions.

%
% If at all possible, use smart decisions
%
%
% NOTE: "smart decisions" in this encoding means that we:
%        1) use all the requiredMoves in reverse order, but
%        2) we allow delaying ONE required move to the end
%        3) AND we allow generating useless moves, if needed,
%           by performing pivot(.,1,clock/anticlock) actions.
%
%
1{ -stop_smart_decisions, stop_smart_decisions }1.
#minimize[ -stop_smart_decisions=1, stop_smart_decisions=2 ].

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.


1{ delayed(I) : requiredMove(I,D) }1 :- smart_decisions.

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

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

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

-last_i(I2) :-
	next(I1,I2).
last_i(I1) :-
	next(I1,I2),
	not -last_i(I1).

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

%%%%%%%%%%%%% useless actions (if needed)
uselesspivotneeded :- smart_decisions, time(T), n_reqMoves(N), T>N.

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

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


%
% Select *only* among the required moves.
%
%1{ pivot(S,I,D) : requiredMove(I,D) }1 :- step(S), time(T), S<=T.
1{ pivot(S,I,D) : step(S) : S<=T }1 :- requiredMove(I,D), time(T), not block_choice_decisions.

%
% Select among *all* the moves.
%
dir(clock).
dir(anticlock).
%1{ pivot(S,I,D) : index(I) : dir(D) }1 :- step(S), time(T), S <= T.
0{ pivot(S,I,D) : step(S) : S<=T }1 :- index(I), dir(D), not requiredMove(I,D), time(T), n_reqMoves(RM), RM < T, not block_choice_decisions.

%
% The required moves *must* be used.
%
%move_used(I,D) :- pivot(S,I,D), step(S), time(T), S<=T.
%:- requiredMove(I,D), not move_used(I,D).

%:- step(S), pivot(S,I1,D1), pivot(S,I2,D2), I1 != I2.
%:- step(S), pivot(S,I1,D1), pivot(S,I2,D2), D1 != D2.
% Exactly one move per step
:- step(S), not 1{ pivot(S,I,D) : index(I): dir(D) }1, time(T), S<=T.


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

index(I) :- fold(I,X,Y).

cspvar(tfoldx(S,I),0,2*N) :-
	step(S),
	index(I),
	length(N).

label_order(tfoldx(S,I),1) :-
	step(S),
	index(I),
	length(N).

cspvar(tfoldy(S,I),0,2*N) :-
	step(S),
	index(I),
	length(N).

label_order(tfoldy(S,I),1) :-
	step(S),
	index(I),
	length(N).

% initial state
required(tfoldx(1,I)==N) :-
	index(I),
	length(N).
required(tfoldy(1,I)==Y) :-
	index(I),
	length(N),
	Y=N+I-1.

%
% Effect of pivot(t,i,d)
%
required(tfoldy(S2,I)==tfoldx(S1,P)-tfoldx(S1,I)+tfoldy(S1,P)) :-
	step(S1), step(S2), S2=S1+1,
	pivot(S1,P,clock),
	index(I),
	I >= P.

required(tfoldy(S2,I)==tfoldx(S1,I)-tfoldx(S1,P)+tfoldy(S1,P)) :-
	step(S1), step(S2), S2=S1+1,
	pivot(S1,P,anticlock),
	index(I),
	I >= P.

required(tfoldx(S2,I)==tfoldy(S1,I)-tfoldy(S1,P)+tfoldx(S1,P)) :-
	step(S1), step(S2), S2=S1+1,
	pivot(S1,P,clock),
	index(I),
	I >= P.

required(tfoldx(S2,I)==tfoldy(S1,P)-tfoldy(S1,I)+tfoldx(S1,P)) :-
	step(S1), step(S2), S2=S1+1,
	pivot(S1,P,anticlock),
	index(I),
	I >= P.

required(tfoldx(S2,I)==tfoldx(S1,I)) :-
	step(S1), step(S2), S2=S1+1,
	pivot(S1,P,D),
	index(I),
	I < P.

required(tfoldy(S2,I)==tfoldy(S1,I)) :-
	step(S1), step(S2), S2=S1+1,
	pivot(S1,P,D),
	index(I),
	I < 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.


%
% DEBUG: show detailed info about overlaps
%
% Define show_overlaps to enable.
%
%show_overlaps.

cspvar(overlap(S,I1,I2),0,1) :-
	show_overlaps,
	step(S),
	index(I1), index(I2),
	I1<I2.
required((tfoldx(S,I1)==tfoldx(S,I2) /\ tfoldy(S,I1)==tfoldy(S,I2)) -> overlap(S,I1,I2)==1) :-
	show_overlaps,
	step(S),
	index(I1), index(I2),
	I1<I2.


% Goal constraint
required(tfoldx(S,I)==X) :-
not just_count,
	time(T), S=T+1,
	fold(I,X,Y).

required(tfoldy(S,I)==Y) :-
not just_count,
	time(T), S=T+1,
	fold(I,X,Y).
