zbytek:-
 free(Typ,J,D,S,V,Par),
 kam(Typ,D,S,V,X,Y,A,B).
 
kam(se,D,S,V,X,Y,A,B):-
 steny(Sez),
 member([M,Px,St],Sez), % vybereme si nejakou stenu
 D<M,	%a bylo by dobre, aby se to tam vlezlo
 potreba(St,M,Px,D,S,ND,NS),
 A is X+ND,B is Y+NS,
 volne(X,Y,A,B),
 retract(free(se,J,D,S,V,Par)),
 asserta(ook(se,J,D,S,V,Par,[X,Y,0],St)).

potreba(St,M,Px,D,S,M,S):-
 hor(St), not D>M.

potreba(St,M,Px,D,S,S,D):-
 vert(St),not S>M. 

y_steny(0,n).
y_steny(Y,s):-
 mistnost(_,Y,_).
x_steny(0,w).
x_steny(X,e):-
 mistnost(X,_,_).
volne(X,Y,A,B):-
 not obsazeno(X,Y,A,B,_).

obsazeno(X1,Y1,X2,Y2,J):-
 ook(_,J,D,S,_,_,[X,Y,_],St),
 ctverec(X,Y,XM,YM,D,S,St),
 prunik(X1,Y1,X2,Y2,X,Y,XM,YM).
 
ctverec(X,Y,XM,YM,PX,PY,St):-
 hor(St),
 XM is X+PX,YM is Y+PY.
 
ctverec(X,Y,XM,YM,PX,PY,_):-
 XM is X+PY,YM is Y+PX.

prunik(X1,Y1,X2,Y2,A1,B1,A2,B2):-
 prunik_i(X1,X2,A1,A2),
 prunik_i(Y1,Y2,B1,B2).
 
prunik_i(X,Y,A,_):-
 X<A,A<Y.

prunik_i(X,Y,_,B):-
 X<B,B<Y.

prunik_i(X,Y,A,B):-
 A<X,Y<B.

