%Ausführliche Anleitung als Zusatzdatei in den Spielordner legen.
%Auf Englisch übersetzen.
%VORSCHLAG: RECHTER MAUSKLICK BEI ZEITEINSTELLUNG +10 - HILFT MEHR
% In Level 2 (intern geführt als Level 1) und Level 3 (intern Level 2),
% Level 1 (intern Level 11), Level 4,5,6,7,8,9,10 funktioniert das.
%
%UMBAU: TOLERANZ VON +-2 FUER STARTZEITEN: ALLE PROBIEREN;
%FUNKTIONIERT EINER KONSISTENT UND ERFOLGREICH, ZEIGE DIESEN AN;
%FALLS NICHT, ZEIGE AN WIE EINGEGEBEN.
%
%BEI LEVEL 1,2,3,4,5,6,7,8,10,11 IST TOLERANZ ERFOLGREICH!
%In Level 7 dauert es allerdings ziemlich lange - prüfen...
%
%AN IOANNA SCHICKEN!
%LEVEL 10: schauen, ob test auf virtuelle objekte gestrichen werden kann
% bzw. ob es ohne (besser) läuft.

% LEVEL 11: winkelpositionen im durchlauf anpassen. (die erwünschten
% positionen stehen im eingabeteil - einfach übernehmen...
%
%hier soll durch die level geschaltet werden; man sollte also wählen
% können, welches man spielen möchte.

:- pce_global(@in_out_link, make_in_out_link).
:-	consult(zeitreise16c).
:-	consult(zeitreisegraphikv16a1).



time_ball:-
	free(@stani),
	%free(@time),
	%free(@balls),
	new(@stani,picture('TimeBall - Intro')),
%	new(@time,device),
%	send(@time,display,new(_,text('time')),point(100,100)),
%	new(@balls,device),
%	send(@balls,display,new(_,text('balls')),point(100,100)),
	send(@stani,width(1200)),
	send(@stani,height(600)),

	send(@stani,flush),

	send(@stani,open),
send(timer(1),delay),

	send(@stani,display,new(_,text('TIME')),point(550,275)),
	send(@stani,open),
send(timer(1),delay),
	send(@stani,display,new(_,text('BALLS')),point(600,275)),
	send(timer(1),delay),
	send(@stani,display,new(_,text('von MERLIN CARL')),point(535,350)),
	send(timer(3),delay),
	send(@stani,open),

	send(@stani,destroy),
	free(@menue),
	new(@menue,dialog('TimeBall')),
	send(@menue,width,1200),
	send(@menue,height,600),
	send(@menue,append,button(anleitung,message(@prolog,anleitung))),
	send(@menue,append,button(level0,message(@prolog,showtest13,level0,25)),below),
	send(@menue,append,button(level1,message(@prolog,showtest13,level11,25))),
	send(@menue,append,button(level2,message(@prolog,showtest13,level1,20))),
	send(@menue,append,button(level3,message(@prolog,showtest13,level2,20))),
	send(@menue,append,button(level4,message(@prolog,showtest13,level3,20))),
	send(@menue,append,button(level5,message(@prolog,showtest13,level8,25))),
	send(@menue,append,button(level6,message(@prolog,showtest13,level9,25))),
	send(@menue,append,button(level7,message(@prolog,showtest13,level10,25)),below),
	send(@menue,append,button(level8,message(@prolog,showtest13,level4,42))),
	send(@menue,append,button(level9,message(@prolog,showtest13,level5,40))),
        send(@menue,append,button(level10,message(@prolog,showtest13,level7,45))),
	send(@menue,append,button(level11,message(@prolog,showtest13,level6,55))),
	send(@menue,append,button(level12,message(@prolog,showtest13,level12,45))),
	send(@menue,append,button(quit,message(@menue,destroy)),below),

	send(@menue,open).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rescale(X,Y):-
	X1 is X mod 50,
	X2 is X-X1,
	Y is X2//50.

make_in_out_link(L) :-
 new(L, link(in, out, line(arrows := second))).


reiserichtung(0,unten). reiserichtung(1,oben). reiserichtung(6, links). reiserichtung(8,rechts).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



anleitung:-
	free(@anleitung),
	new(@anleitung,dialog('Anleitung')),
	send(@anleitung,width,1200),
	send(@anleitung,height,600),
	send(@anleitung,display,new(_,text('Das Ziel des Spieles ist es, alle auf dem Feld befindlichen Bälle (dargestellt durch farbige Kreise, die ein volles Kästchen füllen) in das mit dem Schriftzug "goal" markierte Zielfeld zu bewegen.')),point(50,125)),
	send(@anleitung,display,new(_,text('Die vorkommenden Spielelemente sind Bälle, Winkel, Wände, Zeitmaschinen, Schalter und das Zielfeld.')),point(50,150)),
	send(@anleitung,display,new(_,text('Von diesen Spielelementen sind stets einige fixiert, andere beweglich und können mit der Maus auf einem freien Feld platziert werden.')),point(50,175)),
	send(@anleitung,display,new(_,text('Ferner können an Bällen und Zeitmaschinen häufig noch weitere Einstellungen vorgenommen werden')),point(50,200)),
	send(@anleitung,display,new(_,text('Bälle mit einstellbarer Startrichtung haben vier gelb unterlegte Pfeile. Wähle aus diesen die gewünschte Startrichtung durch einen Linksklick.')),point(50,225)),
	send(@anleitung,display,new(_,text('Zeitmaschinen haben je nach Level einen oberen Regler für den Zeitpunkt der Abreise, einen unteren Regler für den Zeitpunkt der Ankunft sowie eine Auswahl für die Richtung, in die das transportierte Objekt sich bewegt')),point(50,250)),
	send(@anleitung,display,new(_,text('Zeitmaschinen sind dargestellt durch farbige Quadrate mit zwei + und - Reglern für Start- und Zielzeit sowie - je nach einstellbarkeit - vier Pfeilen')),point(50,275)),
		send(@anleitung,display,new(_,text('Gibt es mehrere Bälle, so enstpricht die Farbe einer Zeitmaschine der Farbe des Balles, der durch sie transportiert werden kann')),point(50,300)),
	send(@anleitung,display,new(_,text('Bälle werden durch Winkel umgelenkt und durch Wände reflektiert')),point(50,325)),
send(@anleitung,display,new(_,text('Prallen zwei Bälle aufeinanden, so nimmt jeder die Bewegungsrichtung des jeweils anderen an')),point(50,350)),
send(@anleitung,display,new(_,text('Ausser zur Start- und Zielzeit funktioniert eine Zeitmaschine wie ein leeres Feld')),point(50,375)),

send(@anleitung,display,new(_,text('Start- und Zielzeit einer arbeitenden Zeitmaschine müssen positiv sein. Ein Level startet im Zeitpunkt 1. Sind beide Zeiten auf 0, bleibt die Maschine inaktiv')),point(50,400)),

send(@anleitung,display,new(_,text('Sind alle Gegenstände verteilt, klicke auf den Start-Button, um das Level zu starten')),point(50,425)),

send(@anleitung,display,new(_,text('Links oben wird eine Uhr angezeigt, die zu jedem Levelzustand den entsprechenden Zeitpunkt anzeigt. Ein Ball benötigt einen Zeitschritt, um sich ein Feld fortzubewegen.')),point(50,450)),

send(@anleitung,display,new(_,text('Ist die Konstellation der Gegenstände so, dass eine eingestellte Zeitreise nicht angetreten wird, weil sich der betreffende Ball nicht zur Startzeit auf dem Feld der Zeitmaschine befindet')),point(50,475)),

	send(@anleitung,display,new(_,text('oder sich nicht in die richtige Richtung bewegt, ist der Verlauf inkonsistent')),point(50,490)),

send(@anleitung,display,new(_,text('Dies wird beim Ablauf angezeigt, zusammen mit dem Startzeitpunkt der Zeitreise, die nicht angetreten werden kann')),point(50,510)),

send(@anleitung,display,new(_,text('Schalter werden durch kleine rote Kreise dargestellt, die durch Kurven mit den durch sie aktivierten Objekten verbunden sind')),point(50,535)),
send(@anleitung,display,new(_,text('Bewegt sich ein Ball über ein Feld mit einem Schalter, wird er aktiviert, wenn er derzeit inaktiv ist und inaktiviert, wenn er derzeit aktiviert ist')),point(50,560)),
send(@anleitung,display,new(_,text('Aktivierte Schalter haben einen kleinen schwarzen Kreis im Inneren, inaktive nicht')),point(50,585)),
send(@anleitung,display,new(_,text('Zwischen dem Eintreffen zweier Bälle auf dem Zielfeld muss mindestens ein Zeitschritt liegen,
	sonst prallen sie aneinander ab')),point(50,610)),
 send(@anleitung,display,new(_,text('Alle Zeitmaschinen müssen verwendet werden')),point(50,640)),

send(@anleitung,display,new(_,text('Inaktive Objekte (Winkel oder Wände) werden schraffiert dargestellt. Sie funktionieren wie leere Felder')),point(50,660)),


send(@anleitung,display,new(_,text('Spielelemente und Spielweise')),point(100,200)),
	send(@anleitung,append,button('Zurück zum Spiel',message(@anleitung,destroy)),below),

	send(@anleitung,open).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Level 0 - Testfall 18
%Beweglich: Wand. Der Rest ist fix.
%[1,[[[ball,1,[8,8],links]],[],[],[[wa,[1,8]],[wi,2,[12,8],lu]],[[goal,3,[12,4],auf,[],[]]],[]]]

level(0,[1,[[[ball,1,[8,8],links]],[],[],[[wi,2,[12,8],lu]],[[goal,3,[12,4],auf,[],[]]],[]]]).

showtest13(level0,Max):-
	!,
	free(@ci),free(@ball),free(@larrowba0),
	free(@wa),
	free(@box1),
	free(@smile),
	free(@p),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 0')),
	new(@p,picture),
	level(0,[1,[Ba,Sw,Dyn,Stat,Lo,_]]),
	preprocess_state([1,[Ba,Sw,Dyn,Stat,Lo,[]]],[1,[_,_Sw1,_Dyn1,Stat1,Lo1,_Zm1]]),
	make_grid(1000),

	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),

	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),

	new(@ball,device),
	new(@larrowba0,device),
	send(@ball,display,new(@ci,circle(50)),point(0,50)),
	send(@ci,fill_pattern,colour(green)),
	%Anzeigepfeil nach links.
	send(@larrowba0,display,new(_,line(0,7,30,7))),
	send(@larrowba0,display,new(_,line(0,7,10,15))),
	send(@larrowba0,display,new(_,line(0,7,10,0))),
	send(@p,display,@ball,point(400,400)),
	send(@p,display,@larrowba0,point(375,415)),


	%Statische Barriere
	new(@wa,device),
	send(@wa,display,new(@box1,box(45,45)),point(30,50)),
	send(@box1,fill_pattern,colour(grey)),
	send(@wa,recogniser,new(move_gesture(left))),
	send(@p,display,@wa),

	send(@dialog,display,@p),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl0,Max,@wa))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level0,Max))),
	send(@dialog,open).

playl0(Max,Wa):-
	free(@larrowba0),
	get(Wa,x,Wax),get(Wa,y,Way),
	rescale(Wax,Wax1),rescale(Way,Way1),
	Wax12 is Wax1+1, Way12 is Way1+1,

	catch_virtual_objects([1,[[[ball,1,[8,8],links]],[],[],[[wa,[Wax12,Way12]],[wi,2,[12,8],lu]],[[goal,3,[12,4],auf,[],[]]],[]]],Max,P0),
	!,
	free(Wa),

	send(@p,display,new(_,text('Virtuelle Objekte gefunden',center,bold)),point(250,250)),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	misserfolgsmeldung,
	!,
	send(@p,open).

playl0(Max,Wa):-
	free(@larrowba0),
	get(Wa,x,Wax),get(Wa,y,Way),
	rescale(Wax,Wax1),rescale(Way,Way1),
	Wax12 is Wax1+1, Way12 is Way1+1,
	free(@ball),
	free(Wa),
	!,
	verlauf([1,[[[ball,1,[8,8],links]],[],[],[[wa,[Wax12,Way12]],[wi,2,[12,8],lu]],[[goal,3,[12,4],auf,[],[]]],[]]],Max,P0),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
showtest13(level1,Max):-
	!,
	free(@conn),
	free(@smile),
	free(@ci),
	free(@p),
	free(@ball),
	free(@zm),
	%free(@tm),
	free(@reisezeiten),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 2')),
	new(@p,picture),
	new(@reisezeiten,point),
	send(@reisezeiten,x,0),send(@reisezeiten,y,0),
	testfall(11,[1,[[],Sw,Dyn,Stat,Lo,_]]),
	preprocess_state([1,[[],Sw,Dyn,Stat,Lo,[]]],[1,[[],Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),
	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),
	new(@ball,device),
	send(@ball,display,new(@ci,circle(50)),point(50,50)),
	send(@ci,fill_pattern,colour(blue)),
	%Anzeigepfeil nach rechts, zum ball gehörig
	send(@ball,display,new(_,line(100,72,130,72))),
	send(@ball,display,new(_,line(130,72,120,80))),
	send(@ball,display,new(_,line(130,72,120,64))),

	%new(@zm,device),
	%send(@zm,display,new(@tm,box(50,50)),point(100,100)),
	%send(@tm,fill_pattern,colour(green)),
	%send(@zm,display,new(_,text('zm 7 1')),point(100,100)),
	%send(@p,display,@zm,point(100,100)),
	send(@p,display,@ball,point(50,50)),
	send(@p,display,new(@conn,bezier_curve(point(225,75),point(375,75),point(300,115)))),
	%ball sollte nicht beweglich sein, sonst wird es trivial...
	%send(@ball,recogniser,new(move_gesture(left))),
	%send(@zm,recogniser,new(move_gesture(left))),
	send(@dialog,display,@p),

	create_counting_box21(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,v91,Max,@ball,@cbox))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level1,Max))),
	send(@dialog,open).

v91(Max,A,B):-
	%A ist der Ball, B die Position der Zeitmaschine
	free(@conn),
	get(@reisezeiten,x,St),get(@reisezeiten,y,Zi),
	get(A,x,X),
	get(A,y,Y),
	get(B,x,B1),
	get(B,y,B2),
	get(@selected_arrow,y,DirCode),
	reiserichtung1(DirCode,Dir),
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,
	%free(A),
	%free(B),
	X1 is X mod 50,
	Y1 is Y mod 50,
	X2 is X-X1,
	Y2 is Y-Y1,
	X3 is X2//50,
	Y3 is Y2//50,
	X4 is X3+1,
	Y4 is Y3+1,
	testfall(11,[1,[[],Sw,Dyn,Stat,Lo,_]]),
	catch_virtual_objects([1,[[[ball,3,[X4,Y4],rechts]],Sw,Dyn,Stat,Lo,[[zm,2,St,Zi,[B3,B4],[ball,3,[B3,B4],Dir]]]]],Max,P0),
	!,
	%verlauf([1,[[[ball,3,[X4,Y4],rechts]],Sw,Dyn,Stat,Lo,[[zm,2,7,1,[B3,B4],[ball,3,[B3,B4],rechts]]]]],Max,P0),
	free(A),
	free(B),
	send(@p,display,new(_,text('Virtuelle Objekte gefunden',center,bold)),point(250,250)),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	misserfolgsmeldung,
	!,
	send(@p,open).

v91(Max,A,B):-
	free(@conn),
	get(A,x,X),
	get(A,y,Y),
	get(B,x,B1),
	get(B,y,B2),
	free(@cbox),
	get(@reisezeiten,x,St),get(@reisezeiten,y,Zi),
	get(@selected_arrow,y,DirCode),
	reiserichtung1(DirCode,Dir),
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,
	free(A),
	free(B),
	X1 is X mod 50,
	Y1 is Y mod 50,
	X2 is X-X1,
	Y2 is Y-Y1,
	X3 is X2//50,
	Y3 is Y2//50,
	X4 is X3+1,
	Y4 is Y3+1,
	testfall(11,[1,[[],Sw,Dyn,Stat,Lo,_]]),
	!,
	%HIER WURDE ERSTMALS DIE TOLERANZREGEL VERWENDET!
	bester_verlauf1([1,[[[ball,3,[X4,Y4],rechts]],Sw,Dyn,Stat,Lo,[[zm,2,St,Zi,[B3,B4],[ball,3,[B3,B4],Dir]]]]],P0,Max),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).

%zeigt eine box mit zwei einstellbaren zählern an.
% Jetzt noch: Balleinstellung. Man sollte die Richtung eines Balles
% wählen können.
%Ballwahl ist hier gelöscht - es gibt ja nur einen ball...
create_counting_box21(X,Y):-display_counting_box21(@p,@dialog,X,Y,60,60).

display_counting_box21(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox),
	free(@number),
	free(@number2),
	free(@cbox),
	free(@plus),
	free(@minus),
	free(@plus2),
	free(@minus2),
	free(@cball),
	free(@larrow),free(@rarrow),free(@darrow),free(@uparrow),
	free(@selected_arrow),
	free(@lbox),free(@rbox),free(@dbox),free(@upbox),
	new(@cbox,device),
	new(@plus,device),
	new(@minus,device),
	new(@plus2,device),
	new(@minus2,device),
	new(@larrow,device),
	new(@rarrow,device),
	new(@darrow,device),
	new(@uparrow,device),
	new(@selected_arrow,device),

	send(@cbox,display,new(@mainbox,box(50,50)),point(60,60)),
	send(@mainbox,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow,display,new(@lbox,box(32,16))),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@larrow,display,new(_,line(0,7,30,7))),
	send(@larrow,display,new(_,line(0,7,10,15))),
	send(@larrow,display,new(_,line(0,7,10,0))),
	send(@larrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl1,links)))),
	send(@cbox,display,@larrow,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow,display,new(@rbox,box(32,16))),
	send(@rbox,fill_pattern,colour(yellow)),
	send(@rarrow,display,new(_,line(0,7,30,7))),
	send(@rarrow,display,new(_,line(30,7,20,15))),
	send(@rarrow,display,new(_,line(30,7,20,0))),
	send(@rarrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl1,rechts)))),
	send(@cbox,display,@rarrow,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow,display,new(@upbox,box(16,32))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@uparrow,display,new(_,line(7,0,7,30))),
	send(@uparrow,display,new(_,line(7,30,15,20))),
	send(@uparrow,display,new(_,line(7,30,0,20))),
	send(@uparrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl1,oben)))),
	send(@cbox,display,@uparrow,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow,display,new(@dbox,box(16,32))),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@darrow,display,new(_,line(7,0,7,30))),
	send(@darrow,display,new(_,line(7,0,15,10))),
	send(@darrow,display,new(_,line(7,0,0,10))),
	send(@darrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl1,unten)))),
	send(@cbox,display,@darrow,point(75,40)),


	send(@plus,display,new(_,box(15,15)),point(0,-5)),
	send(@plus,display,new(_,text(+)),point(0,-5)),
	send(@minus,display,new(_,box(15,15)),point(15,-5)),
	send(@minus,display,new(_,text(-)),point(15,-5)),
	free(@number),
	send(@cbox,display,new(@number,text(X)),point(75,60)),
	send(@plus,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number21,@reisezeiten,@dialog,plus,0)))),
	send(@minus,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number21,@reisezeiten,@dialog,minus,0)))),

	%+10 HIER
	send(@plus,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number21,@reisezeiten,@dialog,plus10,0)))),
	send(@minus,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number21,@reisezeiten,@dialog,minus10,0)))),

	send(@cbox,display,@plus,point(50,50)),
	send(@cbox,display,@minus,point(100,50)),
	send(@cbox,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox,point(Ko1,Ko2)),

	send(@plus2,display,new(_,box(15,15)),point(0,5)),
	send(@plus2,display,new(_,text(+)),point(0,5)),
	send(@minus2,display,new(_,box(15,15)),point(15,5)),
	send(@minus2,display,new(_,text(-)),point(15,7)),
	free(@number2),
	send(@cbox,display,new(@number2,text(Y)),point(75,85)),
	send(@plus2,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number21,@reisezeiten,@dialog,0,plus)))),
	%Neu: Reisezeiten +10
	send(@plus2,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number21,@reisezeiten,@dialog,0,plus10)))),

	send(@minus2,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number21,@reisezeiten,@dialog,0,minus)))),
%Neu: Reisezeiten -10
	send(@minus2,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number21,@reisezeiten,@dialog,0,minus10)))),


	send(@cbox,display,@plus2,point(50,100)),
	send(@cbox,display,@minus2,point(100,100)),
	send(@cbox,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).

%für die wahl einer richtung.
richtungswahl1(rechts):-
	free(@selected_arrow),
	send(@rarrow,display,new(@selected_arrow,line(0,8,30,8))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(red)).

richtungswahl1(links):-
	free(@selected_arrow),
	send(@larrow,display,new(@selected_arrow,line(0,6,30,6))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(red)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungswahl1(oben):-
	free(@selected_arrow),
	send(@uparrow,display,new(@selected_arrow,line(7,1,7,31))),
	send(@upbox,fill_pattern,colour(red)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungswahl1(unten):-
	free(@selected_arrow),
	send(@darrow,display,new(@selected_arrow,line(7,0,7,30))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(red)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung1(0,unten). reiserichtung1(1,oben). reiserichtung1(6, links). reiserichtung1(8,rechts).

%zum aktualisieren der reisezeiten.

new_number21(@reisezeiten,@dialog,plus,_):-
	get(@reisezeiten,x,X),
	Y is X+1,
	send(@reisezeiten,x,Y),
	free(@number),
	send(@cbox,display,new(@number,text(Y)),point(75,60)).

new_number21(@reisezeiten,@dialog,minus,_):-
	get(@reisezeiten,x,X),
	Y is X-1,
        send(@reisezeiten,x,Y),
	free(@number),
	send(@cbox,display,new(@number,text(Y)),point(75,60)).

%Reisezeiten +-10
new_number21(@reisezeiten,@dialog,plus10,_):-
	get(@reisezeiten,x,X),
	Y is X+10,
	send(@reisezeiten,x,Y),
	free(@number),
	send(@cbox,display,new(@number,text(Y)),point(75,60)).

new_number21(@reisezeiten,@dialog,minus10,_):-
	get(@reisezeiten,x,X),
	Y is X-10,
        send(@reisezeiten,x,Y),
	free(@number),
	send(@cbox,display,new(@number,text(Y)),point(75,60)).



new_number21(@reisezeiten,@dialog,_,plus):-
	get(@reisezeiten,y,Y),
	Y1 is Y+1,
	send(@reisezeiten,y,Y1),
	free(@number2),
	send(@cbox,display,new(@number2,text(Y1)),point(75,85)).

new_number21(@reisezeiten,@dialog,_,minus):-
	get(@reisezeiten,y,Y),
	Y1 is Y-1,
	send(@reisezeiten,y,Y1),
	free(@number2),
	send(@cbox,display,new(@number2,text(Y1)),point(75,85)).

%Reisezeiten +-10

new_number21(@reisezeiten,@dialog,_,plus10):-
	get(@reisezeiten,y,Y),
	Y1 is Y+10,
	send(@reisezeiten,y,Y1),
	free(@number2),
	send(@cbox,display,new(@number2,text(Y1)),point(75,85)).

new_number21(@reisezeiten,@dialog,_,minus10):-
	get(@reisezeiten,y,Y),
	Y1 is Y-10,
	send(@reisezeiten,y,Y1),
	free(@number2),
	send(@cbox,display,new(@number2,text(Y1)),point(75,85)).




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


level(2,[[[ball,0,[1,1],rechts]],[[sw,1,[5,1],aus,[3]],[sw,2,[9,1],aus,[4]]],[[barr,3,[7,1],auf,[1]],[barr,4,[11,1],auf,[2]]],[],[[goal,7,[15,1],auf,[],[]]],[]]).

showtest13(level2,Max):-
	!,
	free(@ci),free(@ball),
	free(@conn1),
	free(@conn2),
	free(@smile),
	free(@p),
	free(@zm),
	%free(@tm),
	free(@reisezeiten1),
	free(@reisezeiten2),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 3')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	new(@reisezeiten2,point),
	send(@reisezeiten2,x,0),send(@reisezeiten2,y,0),
        level(2,[Ba,Sw,Dyn,Stat,Lo,_]),
	preprocess_state([1,[Ba,Sw,Dyn,Stat,Lo,[]]],[1,[_,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),
	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),

	insert_time(Zm1,1,Zm2),

	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),
	new(@ball,device),
	send(@ball,display,new(@ci,circle(50)),point(0,50)),
	send(@ci,fill_pattern,colour(yellow)),
	%Anzeigepfeil nach rechts, zum ball gehörig
	send(@ball,display,new(_,line(50,72,80,72))),
	send(@ball,display,new(_,line(80,72,70,80))),
	send(@ball,display,new(_,line(80,72,70,64))),

	send(@p,display,@ball,point(0,50)),

	send(@p,display,new(@conn1,bezier_curve(point(275,75),point(375,75),point(325,115)))),
	send(@p,display,new(@conn2,bezier_curve(point(475,75),point(575,75),point(525,115)))),

	send(@dialog,display,@p),

	create_counting_box212(0,0),
        create_counting_box222(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl2,Max,@cbox1,@cbox2))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level2,Max))),
	send(@dialog,open).






playl2(Max,Z1,Z2):-
	free(@ball),
	free(@conn1),
	free(@conn2),
	get(@reisezeiten1,x,St1),get(@reisezeiten1,y,Zi1),
	get(@reisezeiten2,x,St2),get(@reisezeiten2,y,Zi2),
        %Koordinaten erste Zeitmaschine
	get(Z1,x,B11),
	get(Z1,y,B21),
	get(@selected_arrow1,y,DirCode1),
	reiserichtung2(DirCode1,Dir1),
	R11 is B11 mod 50,
	R21 is B21 mod 50,
	B31 is ((B11-R11)//50)+2,
	B41 is ((B21-R21)//50)+2,

        %Koordinaten zweite Zeitmaschine
	get(Z2,x,B12),
	get(Z2,y,B22),
	get(@selected_arrow2,y,DirCode2),
	reiserichtung2(DirCode2,Dir2),
	R12 is B12 mod 50,
	R22 is B22 mod 50,
	B32 is ((B12-R12)//50)+2,
	B42 is ((B22-R22)//50)+2,


	level(2,[_,Sw,Dyn,Stat,Lo,_]),
	%[[zm,5,6,1,[6,1],[ball,0,[6,1],rechts]],[zm,6,6,1,[10,1],[ball,0,[10,1],rechts]]]
	catch_virtual_objects([1,[[[ball,0,[1,1],rechts]],Sw,Dyn,Stat,Lo,[[zm,5,St1,Zi1,[B31,B41],[ball,0,[B31,B41],Dir1]],[zm,6,St2,Zi2,[B32,B42],[ball,0,[B32,B42],Dir2]]]]],Max,P0),
	!,
	free(Z1),
	free(Z2),
	send(@p,display,new(_,text('Virtuelle Objekte gefunden',center,bold)),point(250,250)),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	misserfolgsmeldung,
	!,
	send(@p,open).

playl2(Max,Z1,Z2):-
	free(@ball),
	free(@conn1),
	free(@conn2),
	get(@reisezeiten1,x,St1),get(@reisezeiten1,y,Zi1),
	get(@reisezeiten2,x,St2),get(@reisezeiten2,y,Zi2),

	%Koordinaten erste Zeitmaschine
	get(Z1,x,B11),
	get(Z1,y,B21),
	get(@selected_arrow1,y,DirCode1),
	reiserichtung2(DirCode1,Dir1),
	R11 is B11 mod 50,
	R21 is B21 mod 50,
	B31 is ((B11-R11)//50)+2,
	B41 is ((B21-R21)//50)+2,

        %Koordinaten zweite Zeitmaschine
	get(Z2,x,B12),
	get(Z2,y,B22),
	get(@selected_arrow2,y,DirCode2),
	reiserichtung2(DirCode2,Dir2),
	R12 is B12 mod 50,
	R22 is B22 mod 50,
	B32 is ((B12-R12)//50)+2,
	B42 is ((B22-R22)//50)+2,


	level(2,[_,Sw,Dyn,Stat,Lo,_]),
	%[[zm,5,6,1,[6,1],[ball,0,[6,1],rechts]],[zm,6,6,1,[10,1],[ball,0,[10,1],rechts]]]
	!,
	free(Z1),
	free(Z2),
	!,
	%ÄNDERUNG 1
	bester_verlauf1([1,[[[ball,0,[1,1],rechts]],Sw,Dyn,Stat,Lo,[[zm,0,St2,Zi2,[B32,B42],[ball,0,[B32,B42],Dir2]],[zm,5,St1,Zi1,[B31,B41],[ball,0,[B31,B41],Dir1]]]]],P0,Max),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).




%Erste Zeitmaschine.
create_counting_box212(X,Y):-display_counting_box212(@p,@dialog,X,Y,60,60).

display_counting_box212(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl12,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl12,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl12,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl12,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number212,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number212,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number212,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number212,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number212,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number212,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number212,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number212,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl12(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl12(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl12(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl12(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung2(0,unten). reiserichtung2(1,oben). reiserichtung2(6, links). reiserichtung2(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number212(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number212(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number212(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number212(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%Zeiten +-10

new_number212(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number212(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number212(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number212(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).





%Zweite Zeitmaschine.

create_counting_box222(X,Y):-display_counting_box222(@p,@dialog,X,Y,0,0).

display_counting_box222(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox2),
	free(@number2),
	free(@number22),
	free(@cbox2),
	free(@plus2),
	free(@minus2),
	free(@plus22),
	free(@minus22),
	free(@cball2),
	free(@larrow2),free(@rarrow2),free(@darrow2),free(@uparrow2),
	free(@selected_arrow2),
	free(@lbox2),free(@rbox2),free(@dbox2),free(@upbox2),
	new(@cbox2,device),
	new(@plus2,device),
	new(@minus2,device),
	new(@plus22,device),
	new(@minus22,device),
	new(@larrow2,device),
	new(@rarrow2,device),
	new(@darrow2,device),
	new(@uparrow2,device),
	new(@selected_arrow2,device),

	send(@cbox2,display,new(@mainbox2,box(50,50)),point(60,60)),
	send(@mainbox2,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow2,display,new(@lbox2,box(32,16))),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@larrow2,display,new(_,line(0,7,30,7))),
	send(@larrow2,display,new(_,line(0,7,10,15))),
	send(@larrow2,display,new(_,line(0,7,10,0))),
	send(@larrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl22,links)))),
	send(@cbox2,display,@larrow2,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow2,display,new(@rbox2,box(32,16))),
	send(@rbox2,fill_pattern,colour(yellow)),
	send(@rarrow2,display,new(_,line(0,7,30,7))),
	send(@rarrow2,display,new(_,line(30,7,20,15))),
	send(@rarrow2,display,new(_,line(30,7,20,0))),
	send(@rarrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl22,rechts)))),
	send(@cbox2,display,@rarrow2,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow2,display,new(@upbox2,box(16,32))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@uparrow2,display,new(_,line(7,0,7,30))),
	send(@uparrow2,display,new(_,line(7,30,15,20))),
	send(@uparrow2,display,new(_,line(7,30,0,20))),
	send(@uparrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl22,oben)))),
	send(@cbox2,display,@uparrow2,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow2,display,new(@dbox2,box(16,32))),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@darrow2,display,new(_,line(7,0,7,30))),
	send(@darrow2,display,new(_,line(7,0,15,10))),
	send(@darrow2,display,new(_,line(7,0,0,10))),
	send(@darrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl22,unten)))),
	send(@cbox2,display,@darrow2,point(75,40)),


	send(@plus2,display,new(_,box(15,15)),point(0,-5)),
	send(@plus2,display,new(_,text(+)),point(0,-5)),
	send(@minus2,display,new(_,box(15,15)),point(15,-5)),
	send(@minus2,display,new(_,text(-)),point(15,-5)),
	free(@number2),
	send(@cbox2,display,new(@number2,text(X)),point(75,60)),
	send(@plus2,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number222,@reisezeiten2,@dialog,plus,0)))),
	send(@minus2,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number222,@reisezeiten2,@dialog,minus,0)))),
	%+-10
	send(@plus2,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number222,@reisezeiten2,@dialog,plus10,0)))),
	send(@minus2,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number222,@reisezeiten2,@dialog,minus10,0)))),

	send(@cbox2,display,@plus2,point(50,50)),
	send(@cbox2,display,@minus2,point(100,50)),
	send(@cbox2,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox2,point(Ko1,Ko2)),

	send(@plus22,display,new(_,box(15,15)),point(0,5)),
	send(@plus22,display,new(_,text(+)),point(0,5)),
	send(@minus22,display,new(_,box(15,15)),point(15,5)),
	send(@minus22,display,new(_,text(-)),point(15,7)),
	free(@number22),
	send(@cbox2,display,new(@number22,text(Y)),point(75,85)),
	%+-10
	send(@plus22,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number222,@reisezeiten2,@dialog,0,plus10)))),
	send(@minus22,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number222,@reisezeiten2,@dialog,0,minus10)))),

	send(@plus22,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number222,@reisezeiten2,@dialog,0,plus)))),
	send(@minus22,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number222,@reisezeiten2,@dialog,0,minus)))),
	send(@cbox2,display,@plus22,point(50,100)),
	send(@cbox2,display,@minus22,point(100,100)),
	send(@cbox2,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox2,point(Ko1,Ko2)).

%für die wahl einer richtung für maschine 1.
richtungswahl22(rechts):-
	free(@selected_arrow2),
	send(@rarrow2,display,new(@selected_arrow2,line(0,8,30,8))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(red)).

richtungswahl22(links):-
	free(@selected_arrow2),
	send(@larrow2,display,new(@selected_arrow2,line(0,6,30,6))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(red)),
	send(@rbox2,fill_pattern,colour(yellow)).

richtungswahl22(oben):-
	free(@selected_arrow2),
	send(@uparrow2,display,new(@selected_arrow2,line(7,1,7,31))),
	send(@upbox2,fill_pattern,colour(red)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(yellow)).

richtungswahl22(unten):-
	free(@selected_arrow2),
	send(@darrow2,display,new(@selected_arrow2,line(7,0,7,30))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@dbox2,fill_pattern,colour(red)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(yellow)).




%zum aktualisieren der reisezeiten von maschine 2.

new_number222(@reisezeiten2,@dialog,plus,_):-
	get(@reisezeiten2,x,X),
	Y is X+1,
	send(@reisezeiten2,x,Y),
	free(@number2),
	send(@cbox2,display,new(@number2,text(Y)),point(75,60)).

new_number222(@reisezeiten2,@dialog,minus,_):-
	get(@reisezeiten2,x,X),
	Y is X-1,
        send(@reisezeiten2,x,Y),
	free(@number2),
	send(@cbox2,display,new(@number2,text(Y)),point(75,60)).

new_number222(@reisezeiten2,@dialog,_,plus):-
	get(@reisezeiten2,y,Y),
	Y1 is Y+1,
	send(@reisezeiten2,y,Y1),
	free(@number22),
	send(@cbox2,display,new(@number22,text(Y1)),point(75,85)).

new_number222(@reisezeiten2,@dialog,_,minus):-
	get(@reisezeiten2,y,Y),
	Y1 is Y-1,
	send(@reisezeiten2,y,Y1),
	free(@number22),
	send(@cbox2,display,new(@number22,text(Y1)),point(75,85)).

%+-10
new_number222(@reisezeiten2,@dialog,plus10,_):-
	get(@reisezeiten2,x,X),
	Y is X+10,
	send(@reisezeiten2,x,Y),
	free(@number2),
	send(@cbox2,display,new(@number2,text(Y)),point(75,60)).

new_number222(@reisezeiten2,@dialog,minus10,_):-
	get(@reisezeiten2,x,X),
	Y is X-10,
        send(@reisezeiten2,x,Y),
	free(@number2),
	send(@cbox2,display,new(@number2,text(Y)),point(75,60)).

new_number222(@reisezeiten2,@dialog,_,plus10):-
	get(@reisezeiten2,y,Y),
	Y1 is Y+10,
	send(@reisezeiten2,y,Y1),
	free(@number22),
	send(@cbox2,display,new(@number22,text(Y1)),point(75,85)).

new_number222(@reisezeiten2,@dialog,_,minus10):-
	get(@reisezeiten2,y,Y),
	Y1 is Y-10,
	send(@reisezeiten2,y,Y1),
	free(@number22),
	send(@cbox2,display,new(@number22,text(Y1)),point(75,85)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%level 3, testfall 4
%variabel: zeitmaschine, winkel, ballrichtung
%Die Winkel sollten beim Editor als ausgeschaltete erkennbar sein.
%problem: variable bezierkurve. das lösen wir später.

level(3,[[[ball,0,[1,5],rechts]],[[sw,1,[3,1],aus,[2,3]]],[[dwi,2,[6,1],lo,auf,[1]],[dwi,3,[6,5],lu,auf,[1]]],[[wa,[1,3]]],[[goal,6,[1,1],auf,[],[]]],[[zm,7,12,1,[4,1],[ball,0,[4,1],links]]]]).

showtest13(level3,Max):-
	!,
	free(@ball),free(@larrow),free(@rarrow),free(@darrow),free(@uparrow),
	free(@ci),free(@lbox),free(@rbox),free(@dbox),free(@upbox),
	free(@conn),
	free(@buci),
	free(@button),
	free(@smile),
	free(@p),
	free(@zm),
	free(@wi1),
	free(@wi2),
	%free(@tm),
	free(@reisezeiten1),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 4')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	level(3,[_Ba,Sw,_Dyn,Stat,Lo,_]),
	preprocess_state([1,[[],Sw,[],Stat,Lo,[]]],[1,[_,Sw1,_,Stat1,Lo1,Zm1]]),
	make_grid(1000),


	new(@ball,device),
	new(@button,device),
	new(@larrow,device),
	new(@rarrow,device),
	new(@darrow,device),
	new(@uparrow,device),
	new(@ci,circle(50)),
	send(@ci,fill_pattern,colour(yellow)),
	send(@ball,display,@ci,point(60,60)),

	%der Knopf
	new(@buci,circle(1)),
	send(@buci,fill_pattern,colour(red)),
	send(@button,display,@buci,point(162,65)),
	send(@button,handle,handle(w,h/2,out)),

	%Pfeil nach links.
	send(@larrow,display,new(@lbox,box(32,16))),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@larrow,display,new(_,line(0,7,30,7))),
	send(@larrow,display,new(_,line(0,7,10,15))),
	send(@larrow,display,new(_,line(0,7,10,0))),
	send(@larrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,links)))),
	send(@ball,display,@larrow,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow,display,new(@rbox,box(32,16))),
	send(@rbox,fill_pattern,colour(yellow)),
	send(@rarrow,display,new(_,line(0,7,30,7))),
	send(@rarrow,display,new(_,line(30,7,20,15))),
	send(@rarrow,display,new(_,line(30,7,20,0))),
	send(@rarrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,rechts)))),
	send(@ball,display,@rarrow,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow,display,new(@upbox,box(16,32))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@uparrow,display,new(_,line(7,0,7,30))),
	send(@uparrow,display,new(_,line(7,30,15,20))),
	send(@uparrow,display,new(_,line(7,30,0,20))),
	send(@uparrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,oben)))),
	send(@ball,display,@uparrow,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow,display,new(@dbox,box(16,32))),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@darrow,display,new(_,line(7,0,7,30))),
	send(@darrow,display,new(_,line(7,0,15,10))),
	send(@darrow,display,new(_,line(7,0,0,10))),
	send(@darrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,unten)))),
	send(@ball,display,@darrow,point(75,40)),

	send(@p,display,@ball,point(30,230)),


	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),

	%Erster Winkel
	new(@wi1,device),
	draw_triangle_lu3([180,80],50),
	send(@wi1,handle,handle(w,h/2,in)),
	send(@wi1,recogniser,new(move_gesture(left))),
	send(@wi1,connect,@button,@in_out_link),
	send(@p,display,@wi1),

% send(B1, handle, handle(w, h/2, in)),
% send(B2, handle, handle(w/2, 0, out)),
% send(B1,recogniser,new(move_gesture(left))),
% send(B2,recogniser,new(move_gesture(left))),
% send(B1, connect, B2, @in_out_link).



	%Zweiter Winkel
	new(@wi2,device),
	draw_triangle_lo3([80,180],50),
	send(@wi2,recogniser,new(move_gesture(left))),
	send(@wi2,handle,handle(w,h/2,in)),
	send(@wi2,connect,@button,@in_out_link),
	send(@p,display,@wi2),
	send(@p,display,@button),


	%send(@p,display,new(@conn,bezier_curve(point(225,75),point(375,75),point(300,115)))),
	send(@dialog,display,@p),

	create_counting_box213(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl3,Max,@ball,@cbox1,@wi1,@wi2))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level3,Max))),
	send(@dialog,open).

% A ist der Ball, B ist die Zeitmaschine, C und D der erste und zweite
% Winkel
% Test auf virtuelle Objekte gelöscht. In früheren Versionen ist er noch
% da.



playl3(Max,A,B,C,D):-

free(@conn),free(@button),free(@buci),

	%Ballstartrichtung auslesen
	get(@selected_arrow,y,Ri),
	richtung(Ri,StaRi),

	%Erster Winkel
	get(C,x,W1x),
	get(C,y,W1y),

	%Zweiter Winkel
	get(D,x,W2x),
	get(D,y,W2y),


	%Reisezeiten auslesen
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	%Position der Zeitmaschine auslesen
	get(B,x,B1),
	get(B,y,B2),

	%Reiserichtung der Zeitmaschine auslesen
	get(@selected_arrow1,y,DirCode),
	reiserichtung13(DirCode,Dir),

	%Koordinaten der Zeitmaschine normieren.
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,

	%Koordinaten des ersten Winkels normieren

	round(W1x,W1x1),
	round(W1y,W1y1),
	W1x2 is W1x1+3,
	W1y2 is W1y1+1,

	%Koordinaten des zweiten Winkels normieren
	round(W2x,W2x1),
	round(W2y,W2y1),
	W2x2 is W2x1+1,
	W2y2 is W2y1+4,

%[[[ball,0,[1,5],rechts]],[[sw,1,[3,1],aus,[2,3]]],[[dwi,2,[6,1],lo,auf,[1]],[dwi,3,[6,5],lu,auf,[1]]],[[wa,[1,3]]],[[goal,6,[1,1],auf,[],[]]],[[zm,7,12,1,[4,1],[ball,0,[4,1],links]]]]

        level(3,[_,Sw,_Dyn,Stat,Lo,_]),
	!,
	%ÄNDERUNG TOLERANZ HIER EINFÜGEN!

bester_verlauf1([1,[[[ball,0,[1,5],StaRi]],Sw,[[dwi,2,[W2x2,W2y2],lo,auf,[1]],[dwi,3,[W1x2,W1y2],lu,auf,[1]]],Stat,Lo,[[zm,7,St,Zi,[B3,B4],[ball,0,[B3,B4],Dir]]]]],P0,Max),

%	verlauf([1,[[[ball,0,[1,5],StaRi]],Sw,[[dwi,2,[W2x2,W2y2],lo,auf,[1]],[dwi,3,[W1x2,W1y2],lu,auf,[1]]],Stat,Lo,[[zm,7,St,Zi,[B3,B4],[ball,0,[B3,B4],Dir]]]]],Max,P0),
%

	free(A),free(B),free(C),free(D),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).

%Erste Zeitmaschine.
create_counting_box213(X,Y):-display_counting_box213(@p,@dialog,X,Y,60,60).

display_counting_box213(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl13,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl13,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl13,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl13,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number23,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number23,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number23,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number23,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number23,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number23,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number23,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number23,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl13(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl13(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl13(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl13(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung13(0,unten). reiserichtung13(1,oben). reiserichtung13(6, links). reiserichtung13(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number23(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number23(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number23(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number23(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).
%+-10
new_number23(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number23(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number23(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number23(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).


%Ersten Winkel zeichnen.
draw_triangle_lu3(_,0).
draw_triangle_lu3([X,Y],A):-
	Endpoint is X-A,
	send(@wi1,display,line(X,Y,Endpoint,Y)),
	Yneu is Y-2,
	Aneu is A-2,
	draw_triangle_lu3([X,Yneu],Aneu).

%Zweiten Winkel zeichnen
draw_triangle_lo3(_,0).
draw_triangle_lo3([X,Y],A):-
	Endpoint is X-A,
	send(@wi2,display,line(X,Y,Endpoint,Y)),
	Yneu is Y+2,
	Aneu is A-2,
	draw_triangle_lo3([X,Yneu],Aneu).





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Level 4 - testfall 6
%fest: winkel rechts oben, winkel links unten, ziel, ballposition
% variabel: winkel links oben, winkel rechts unten, ballrichtung,
% zeitmaschine
% Erstmals haben wir damit variable und fixe elemente der gleichen
% kategorie (Stat), das sollte aber kein Problem sein.
% winkel stimmen noch nicht (koordinatennormierung+ausrichtung im
% editor)

%level geändert zwecks detrivialisierung - erstmals in version o.
%[barr,4,[5,1],zu,[5]]
level(4,[[[ball,7,[5,1],rechts]],[[sw,10,[14,2],an,[9]],[sw,5,[14,3],aus,[4]],[sw,6,[11,5],an,[4]]],[[dwi,4,[14,1],lo,zu,[5,6]],[barr,9,[16,1],zu,[10]]],[[wi,0,[1,1],ro],[wi,1,[1,5],ru],[wi,2,[14,5],lu]],[[goal,3,[17,1],auf,[],[]]],[[zm,8,42,13,[12,1],[ball,7,[12,1],rechts]]]]).

showtest13(level4,Max):-
	!,
	free(@ball),free(@larrow),free(@rarrow),free(@darrow),free(@uparrow),
	free(@ci),free(@lbox),free(@rbox),free(@dbox),free(@upbox),free(@conn1),
	free(@conn),free(@button1),free(@buci1),free(@button2),free(@buci2),
	free(@smile),
	free(@p),
	free(@zm),
	free(@wi1),
	free(@dwi2),
	%free(@tm),
	free(@reisezeiten1),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 8')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	level(4,[_Ba,Sw,_Dyn,_Stat,Lo,_]),
	preprocess_state([1,[[],Sw,[[barr,9,[16,1],zu,[5]]],[[wi,0,[1,1],ro],[wi,2,[14,5],lu]],Lo,[]]],[1,[_,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),


	new(@ball,device),
	new(@larrow,device),
	new(@rarrow,device),
	new(@darrow,device),
	new(@uparrow,device),
	new(@ci,circle(50)),
	send(@ci,fill_pattern,colour(purple)),
	send(@ball,display,@ci,point(60,60)),

	%Pfeil nach links.
	send(@larrow,display,new(@lbox,box(32,16))),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@larrow,display,new(_,line(0,7,30,7))),
	send(@larrow,display,new(_,line(0,7,10,15))),
	send(@larrow,display,new(_,line(0,7,10,0))),
	send(@larrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,links)))),
	send(@ball,display,@larrow,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow,display,new(@rbox,box(32,16))),
	send(@rbox,fill_pattern,colour(yellow)),
	send(@rarrow,display,new(_,line(0,7,30,7))),
	send(@rarrow,display,new(_,line(30,7,20,15))),
	send(@rarrow,display,new(_,line(30,7,20,0))),
	send(@rarrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,rechts)))),
	send(@ball,display,@rarrow,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow,display,new(@upbox,box(16,32))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@uparrow,display,new(_,line(7,0,7,30))),
	send(@uparrow,display,new(_,line(7,30,15,20))),
	send(@uparrow,display,new(_,line(7,30,0,20))),
	send(@uparrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,oben)))),
	send(@ball,display,@uparrow,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow,display,new(@dbox,box(16,32))),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@darrow,display,new(_,line(7,0,7,30))),
	send(@darrow,display,new(_,line(7,0,15,10))),
	send(@darrow,display,new(_,line(7,0,0,10))),
	send(@darrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,unten)))),
	send(@ball,display,@darrow,point(75,40)),

	send(@p,display,@ball,point(225,25)),


	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),
	send(@p,display,new(@conn1,bezier_curve(point(725,125),point(825,75),point(750,115)))),



	%Statischer Winkel
	new(@wi1,device),
	draw_triangle_ru4([180,80],50),
	send(@wi1,recogniser,new(move_gesture(left))),
	send(@p,display,@wi1),

%Die Knöpfe
	new(@button1,device),
	new(@buci1,circle(1)),
	send(@buci1,fill_pattern,colour(red)),
	send(@button1,display,@buci1,point(60,60)),
	send(@button1,handle,handle(w,h/2,out)),
	send(@p,display,@button1,point(725,175)),

	new(@button2,device),
	new(@buci2,circle(1)),
	send(@buci2,fill_pattern,colour(red)),
	send(@button2,display,@buci2,point(60,60)),
	send(@button2,handle,handle(w,h/2,out)),
	send(@p,display,@button2,point(575,270)),

%Geschalteter Winkel
	new(@dwi2,device),
	draw_triangle_lo4([80,180],50),
	send(@dwi2,recogniser,new(move_gesture(left))),
	send(@dwi2,handle,handle(w,h/2,in)),
	send(@dwi2,connect,@button1,@in_out_link),
	send(@dwi2,connect,@button2,@in_out_link),
	send(@p,display,@dwi2),

	%send(@p,display,new(@conn,bezier_curve(point(225,75),point(375,75),point(300,115)))),
	send(@dialog,display,@p),

	create_counting_box214(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl4,Max,@ball,@cbox1,@wi1,@dwi2))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level4,Max))),
	send(@dialog,open).

% A ist der Ball, B ist die Zeitmaschine, C der statische und D der
% dynamische Winkel

%playl4(Max,A,B,C,D):-
% Hier müssen später andere Bezierkurven hin. Evtl. lassen wir sie hier
% vorläufig weg.

% free(@conn),free(@conn1),free(@button1),free(@buci1),free(button2),free(@buci2),
%

%	%Ballstartrichtung auslesen
%	get(@selected_arrow,y,Ri),
%	richtung(Ri,StaRi),

%	%Erster Winkel
%	get(C,x,W1x),
%	get(C,y,W1y),

%	%Zweiter Winkel
%	get(D,x,W2x),
%	get(D,y,W2y),


%	%Reisezeiten auslesen
%	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
%	%Position der Zeitmaschine auslesen
%	get(B,x,B1),
%	get(B,y,B2),

%	%Reiserichtung der Zeitmaschine auslesen
%	get(@selected_arrow1,y,DirCode),
%	reiserichtung14(DirCode,Dir),

%	%Koordinaten der Zeitmaschine normieren.
%	R1 is B1 mod 50,
%	R2 is B2 mod 50,
%	B3 is ((B1-R1)//50)+2,
%	B4 is ((B2-R2)//50)+2,

%	%Koordinaten des ersten Winkels normieren

%	round(W1x,W1x1),
%	round(W1y,W1y1),
%	W1x2 is W1x1+4,
%	W1y2 is W1y1+1,

%	%Koordinaten des zweiten Winkels normieren
%	round(W2x,W2x1),
%	round(W2y,W2y1),
%	W2x2 is W2x1+1,
%	W2y2 is W2y1+4,

% [[[ball,7,[5,1],rechts]],[[sw,5,[14,3],aus,[4]],[sw,6,[11,5],an,[4]]],[[dwi,4,[14,1],lo,zu,[5,6]]],[[wi,0,[1,1],ro],[wi,1,[1,5],ru],[wi,2,[14,5],lu]],[[goal,3,[17,1],auf,[],[]]],[[zm,8,42,13,[12,1],[ball,7,[12,1],rechts]]]]


%	level(4,[_,Sw,_Dyn,_Stat,Lo,_]),
%catch_virtual_objects([1,[[[ball,7,[5,1],StaRi]],Sw,[[dwi,4,[W2x2,W2y2],lo,zu,[5,6]],[barr,9,[16,1],zu,[10]]],[[wi,0,[1,1],ro],[wi,1,[W1x2,W1y2],ru],[wi,2,[14,5],lu]],Lo,[[zm,8,St,Zi,[B3,B4],[ball,7,[B3,B4],Dir]]]]],Max,P0),
%	!,
%	free(A),
%	free(B),
%	free(C),
%	free(D),
%send(@p,display,new(_,text('Virtuelle Objekte gefunden',center,bold)),point(250,250)),
%	!,
%	preprocess_all(P0,P1),
%	!,
%	refine(P1,P2),
%	!,
%	verlauf_anzeigen(P2),
%	!,
%	send(timer(1.0),delay),
%	!,
%	misserfolgsmeldung,
%	!,
%	send(@p,open).

playl4(Max,A,B,C,D):-

free(@conn),free(@conn1),free(@button1),free(@buci1),free(button2),free(@buci2),

	%Ballstartrichtung auslesen
	get(@selected_arrow,y,Ri),
	richtung(Ri,StaRi),

	%Erster Winkel
	get(C,x,W1x),
	get(C,y,W1y),

	%Zweiter Winkel
	get(D,x,W2x),
	get(D,y,W2y),


	%Reisezeiten auslesen
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	%Position der Zeitmaschine auslesen
	get(B,x,B1),
	get(B,y,B2),

	%Reiserichtung der Zeitmaschine auslesen
	get(@selected_arrow1,y,DirCode),
	reiserichtung14(DirCode,Dir),

	%Koordinaten der Zeitmaschine normieren.
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,

	%Koordinaten des ersten Winkels normieren

	round(W1x,W1x1),
	round(W1y,W1y1),
	W1x2 is W1x1+4,
	W1y2 is W1y1+1,

	%Koordinaten des zweiten Winkels normieren
	round(W2x,W2x1),
	round(W2y,W2y1),
	W2x2 is W2x1+1,
	W2y2 is W2y1+4,


        level(4,[_,Sw,_Dyn,_Stat,Lo,_]),
	!,
	%Toleranzklausel Level 4 bzw. Level 8
%verlauf([1,[[[ball,7,[5,1],StaRi]],Sw,[[dwi,4,[W2x2,W2y2],lo,zu,[5,6]],[barr,9,[16,1],zu,[10]]],[[wi,0,[1,1],ro],[wi,1,[W1x2,W1y2],ru],[wi,2,[14,5],lu]],Lo,[[zm,8,St,Zi,[B3,B4],[ball,7,[B3,B4],Dir]]]]],Max,P0),

bester_verlauf1([1,[[[ball,7,[5,1],StaRi]],Sw,[[dwi,4,[W2x2,W2y2],lo,zu,[5,6]],[barr,9,[16,1],zu,[10]]],[[wi,0,[1,1],ro],[wi,1,[W1x2,W1y2],ru],[wi,2,[14,5],lu]],Lo,[[zm,8,St,Zi,[B3,B4],[ball,7,[B3,B4],Dir]]]]],P0,Max),

	free(A),free(B),free(C),free(D),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).

%Erste Zeitmaschine.
create_counting_box214(X,Y):-display_counting_box214(@p,@dialog,X,Y,60,60).

display_counting_box214(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl14,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl14,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl14,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl14,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number24,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number24,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number24,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number24,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number24,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number24,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number24,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number24,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl14(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl14(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl14(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl14(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung14(0,unten). reiserichtung14(1,oben). reiserichtung14(6, links). reiserichtung14(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number24(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number24(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number24(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number24(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number24(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number24(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number24(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number24(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%Ersten Winkel zeichnen.
draw_triangle_lo4(_,0).
draw_triangle_lo4([X,Y],A):-
	Endpoint is X-A,
	send(@dwi2,display,line(X,Y,Endpoint,Y)),
	Yneu is Y+1,
	Aneu is A-1,
	draw_triangle_lo4([X,Yneu],Aneu).

%Zweiten Winkel zeichnen
draw_triangle_ru4(_,0).
draw_triangle_ru4([X,Y],A):-
	Endpoint is X+A,
	send(@wi1,display,line(X,Y,Endpoint,Y)),
	Yneu is Y-1,
	Aneu is A-1,
	draw_triangle_ru4([X,Yneu],Aneu).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Level 5 - Testfall 8
% Variabel: Winkel links oben (geschaltet), Winkel rechts unten
% (statisch), beide Ballrichtungen, beide Zeitmaschinen
% PROBLEM: IRGENDWIE (z.B. durch Farbcodierung?) sollte klar gemacht
% werden, welche Zeitmaschine welchen Ball transportiert.
% ODER: Auch das sollte einstellbar sein. Dann muss aber noch einmal
% an den Zeitmaschinen gearbeitet werden.

level(5,[[[ball,7,[8,1],rechts],[ball,9,[1,4],unten]],[[sw,5,[14,3],aus,[4]],[sw,6,[11,5],an,[4]],[sw,12,[14,2],an,[11]]],[[dwi,4,[14,1],lo,zu,[5,6]],[barr,11,[16,1],zu,[12]]],[[wi,0,[1,1],ro],[wi,1,[1,5],ru],[wi,2,[14,5],lu]],[[goal,3,[17,1],auf,[],[]]],[[zm,10,41,11,[5,1],[ball,9,[5,1],rechts]],[zm,8,38,9,[11,1],[ball,7,[11,1],rechts]]]]).

showtest13(level5,Max):-
	!,
	free(@ball),free(@larrow),free(@rarrow),free(@darrow),free(@uparrow),
	free(@ci),free(@lbox),free(@rbox),free(@dbox),free(@upbox),

	free(@ball2),free(@larrow2),free(@rarrow2),free(@darrow2),free(@uparrow2),
	free(@ci2),free(@lbox2),free(@rbox2),free(@dbox2),free(@upbox2),
	free(@button1),free(@buci1),free(@button2),free(@buci2),
        free(@cbox1),
	free(@cbox3),
	free(@conn),
	free(@smile),
	free(@p),
	free(@zm1),
	free(@zm2),
	free(@wi1),
	free(@dwi2),
	%free(@tm),
	free(@reisezeiten1),
	free(@reisezeiten3),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 9')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	new(@reisezeiten3,point),
	send(@reisezeiten3,x,0),send(@reisezeiten3,y,0),
	level(5,[_Ba,Sw,_Dyn,_Stat,Lo,_]),
	preprocess_state([1,[[],Sw,[[barr,11,[16,1],zu,[12]]],[[wi,0,[1,1],ro],[wi,2,[14,5],lu]],Lo,[]]],[1,[_,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),



%Erster Ball
	new(@ball,device),
	new(@larrow,device),
	new(@rarrow,device),
	new(@darrow,device),
	new(@uparrow,device),
	new(@ci,circle(50)),
	send(@ci,fill_pattern,colour(purple)),
	send(@ball,display,@ci,point(60,60)),

	%Pfeil nach links.
	send(@larrow,display,new(@lbox,box(32,16))),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@larrow,display,new(_,line(0,7,30,7))),
	send(@larrow,display,new(_,line(0,7,10,15))),
	send(@larrow,display,new(_,line(0,7,10,0))),
	send(@larrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,links)))),
	send(@ball,display,@larrow,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow,display,new(@rbox,box(32,16))),
	send(@rbox,fill_pattern,colour(yellow)),
	send(@rarrow,display,new(_,line(0,7,30,7))),
	send(@rarrow,display,new(_,line(30,7,20,15))),
	send(@rarrow,display,new(_,line(30,7,20,0))),
	send(@rarrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,rechts)))),
	send(@ball,display,@rarrow,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow,display,new(@upbox,box(16,32))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@uparrow,display,new(_,line(7,0,7,30))),
	send(@uparrow,display,new(_,line(7,30,15,20))),
	send(@uparrow,display,new(_,line(7,30,0,20))),
	send(@uparrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,oben)))),
	send(@ball,display,@uparrow,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow,display,new(@dbox,box(16,32))),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@darrow,display,new(_,line(7,0,7,30))),
	send(@darrow,display,new(_,line(7,0,15,10))),
	send(@darrow,display,new(_,line(7,0,0,10))),
	send(@darrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl,unten)))),
	send(@ball,display,@darrow,point(75,40)),

	send(@p,display,@ball,point(375,25)),

%Zweiter Ball
	new(@ball2,device),
	new(@larrow2,device),
	new(@rarrow2,device),
	new(@darrow2,device),
	new(@uparrow2,device),
	new(@ci2,circle(50)),
	send(@ci2,fill_pattern,colour(green)),
	send(@ball2,display,@ci2,point(60,60)),


	%Pfeil nach links.
	send(@larrow2,display,new(@lbox2,box(32,16))),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@larrow2,display,new(_,line(0,7,30,7))),
	send(@larrow2,display,new(_,line(0,7,10,15))),
	send(@larrow2,display,new(_,line(0,7,10,0))),
	send(@larrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl25,links)))),
	send(@ball2,display,@larrow2,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow2,display,new(@rbox2,box(32,16))),
	send(@rbox2,fill_pattern,colour(yellow)),
	send(@rarrow2,display,new(_,line(0,7,30,7))),
	send(@rarrow2,display,new(_,line(30,7,20,15))),
	send(@rarrow2,display,new(_,line(30,7,20,0))),
	send(@rarrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl25,rechts)))),
	send(@ball2,display,@rarrow2,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow2,display,new(@upbox2,box(16,32))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@uparrow2,display,new(_,line(7,0,7,30))),
	send(@uparrow2,display,new(_,line(7,30,15,20))),
	send(@uparrow2,display,new(_,line(7,30,0,20))),
	send(@uparrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl25,oben)))),
	send(@ball2,display,@uparrow2,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow2,display,new(@dbox2,box(16,32))),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@darrow2,display,new(_,line(7,0,7,30))),
	send(@darrow2,display,new(_,line(7,0,15,10))),
	send(@darrow2,display,new(_,line(7,0,0,10))),
	send(@darrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl25,unten)))),
	send(@ball2,display,@darrow2,point(75,40)),

	send(@p,display,@ball2,point(25,175)),


	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),

	%Statischer Winkel
	new(@wi1,device),
	draw_triangle_ru5([180,80],50),
	send(@wi1,recogniser,new(move_gesture(left))),
	send(@p,display,@wi1),

%Die Knöpfe
	new(@button1,device),
	new(@buci1,circle(1)),
	send(@buci1,fill_pattern,colour(red)),
	send(@button1,display,@buci1,point(60,60)),
	send(@button1,handle,handle(w,h/2,out)),
	send(@p,display,@button1,point(725,175)),

	new(@button2,device),
	new(@buci2,circle(1)),
	send(@buci2,fill_pattern,colour(red)),
	send(@button2,display,@buci2,point(60,60)),
	send(@button2,handle,handle(w,h/2,out)),
	send(@p,display,@button2,point(575,270)),



	%Geschalteter Winkel
	new(@dwi2,device),
	draw_triangle_lo5([80,180],50),
	send(@dwi2,recogniser,new(move_gesture(left))),
	send(@dwi2,handle,handle(w,h/2,in)),
	send(@dwi2,connect,@button1,@in_out_link),
	send(@dwi2,connect,@button2,@in_out_link),
	send(@p,display,@dwi2),
%NEU
	send(@p,display,new(@conn,bezier_curve(point(725,125),point(825,75),point(775,135)))),
	send(@dialog,display,@p),

	create_counting_box215(0,0),
	create_counting_box225(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl5,Max,@ball,@ball2,@cbox1,@cbox3,@wi1,@dwi2))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level5,Max))),
	send(@dialog,open).

% A ist der erste Ball, B der zweite Ball, C ist die erste Zeitmaschine,
% D die zweite Zeitmaschine, E der statische und F der dynamische Winkel

% test auf virtuelle objekte gelöscht. (siehe frühere versionen playl5,
% klausel 1

playl5(Max,A,B,C,D,E,F):-

free(@conn),free(@button1),free(@buci1),free(@button2),free(@buci2),

	%Ballstartrichtung Ball 1 auslesen
	get(@selected_arrow,y,Ri),
	richtung(Ri,StaRi),

	%Ballstartrichtung Ball 2 auslesen
	get(@selected_arrow2,y,Ri2),
	richtung(Ri2,StaRi2),

	%Erster Winkel
	get(E,x,W1x),
	get(E,y,W1y),

	%Zweiter Winkel
	get(F,x,W2x),
	get(F,y,W2y),


	%Erste Zeitmaschine:
	%Reisezeiten Zm1 auslesen
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	%Position der Zeitmaschine 1 auslesen
	get(C,x,B1),
	get(C,y,B2),

	%Reiserichtung der Zeitmaschine 1 auslesen
	get(@selected_arrow1,y,DirCode),
	reiserichtung15(DirCode,Dir),

	%Koordinaten der Zeitmaschine 1 normieren.
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,

	%Zweite Zeitmaschine:
	%Reisezeiten Zm2 auslesen
	get(@reisezeiten3,x,St2),get(@reisezeiten3,y,Zi2),
	%Position der Zeitmaschine 2 auslesen
	get(D,x,C1),
	get(D,y,C2),

	%Reiserichtung der Zeitmaschine auslesen
	get(@selected_arrow3,y,DirCode2),
	reiserichtung(DirCode2,Dir2),

	%Koordinaten der Zeitmaschine normieren.
	S1 is C1 mod 50,
	S2 is C2 mod 50,
	Zm2x is ((C1-S1)//50)+2,
	Zm2y is ((C2-S2)//50)+2,


	%Koordinaten des ersten Winkels normieren

	rescale5(W1x,W1x1),
	rescale5(W1y,W1y1),
	W1x2 is W1x1+4,
	W1y2 is W1y1+1,

	%Koordinaten des zweiten Winkels normieren
	rescale5(W2x,W2x1),
	rescale5(W2y,W2y1),
	W2x2 is W2x1+1,
	W2y2 is W2y1+4,


        level(5,[_,Sw,_Dyn,_Stat,Lo,_]),
	!,
% bester_verlauf1([1,[[[ball,7,[9,1],StaRi],[ball,9,[1,3],StaRi2]],Sw,[[dwi,4,[W2x2,W2y2],lo,zu,[5,6]],[barr,11,[16,1],zu,[12]]],[[wi,0,[1,1],ro],[wi,1,[W1x2,W1y2],ru],[wi,2,[14,5],lu]],Lo,[[zm,10,St2,Zi2,[Zm2x,Zm2y],[ball,9,[Zm2x,Zm2y],Dir2]],[zm,8,St,Zi,[B3,B4],[ball,7,[B3,B4],Dir]]]]],P0,Max),
%

	verlauf([1,[[[ball,7,[8,1],StaRi],[ball,9,[1,4],StaRi2]],Sw,[[dwi,4,[W2x2,W2y2],lo,zu,[5,6]],[barr,11,[16,1],zu,[12]]],[[wi,0,[1,1],ro],[wi,1,[W1x2,W1y2],ru],[wi,2,[14,5],lu]],Lo,[[zm,10,St2,Zi2,[Zm2x,Zm2y],[ball,9,[Zm2x,Zm2y],Dir2]],[zm,8,St,Zi,[B3,B4],[ball,7,[B3,B4],Dir]]]]],Max,P0),
	free(A),free(B),free(C),free(D),free(E),free(F),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).

%Erste Zeitmaschine.
create_counting_box215(X,Y):-display_counting_box215(@p,@dialog,X,Y,60,60).

display_counting_box215(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(purple)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl15,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl15,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl15,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl15,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number25,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number25,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number25,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number25,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number25,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number25,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number25,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number25,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).

%für die wahl einer richtung für maschine 1.
richtungswahl15(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl15(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl15(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl15(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung15(0,unten). reiserichtung15(1,oben). reiserichtung15(6, links). reiserichtung15(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number25(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number25(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number25(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number25(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number25(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number25(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number25(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number25(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).





%Zweite Zeitmaschine.

create_counting_box225(X,Y):-display_counting_box225(@p,@dialog,X,Y,0,0).

display_counting_box225(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox3),
	free(@number3),
	free(@number23),
	free(@cbox3),
	free(@plus3),
	free(@minus3),
	free(@plus23),
	free(@minus23),
	free(@cball3),
	free(@larrow3),free(@rarrow3),free(@darrow3),free(@uparrow3),
	free(@selected_arrow3),
	free(@lbox3),free(@rbox3),free(@dbox3),free(@upbox3),
	new(@cbox3,device),
	new(@plus3,device),
	new(@minus3,device),
	new(@plus23,device),
	new(@minus23,device),
	new(@larrow3,device),
	new(@rarrow3,device),
	new(@darrow3,device),
	new(@uparrow3,device),
	new(@selected_arrow3,device),

	send(@cbox3,display,new(@mainbox3,box(50,50)),point(60,60)),
	send(@mainbox3,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow3,display,new(@lbox3,box(32,16))),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@larrow3,display,new(_,line(0,7,30,7))),
	send(@larrow3,display,new(_,line(0,7,10,15))),
	send(@larrow3,display,new(_,line(0,7,10,0))),
	send(@larrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl35,links)))),
	send(@cbox3,display,@larrow3,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow3,display,new(@rbox3,box(32,16))),
	send(@rbox3,fill_pattern,colour(yellow)),
	send(@rarrow3,display,new(_,line(0,7,30,7))),
	send(@rarrow3,display,new(_,line(30,7,20,15))),
	send(@rarrow3,display,new(_,line(30,7,20,0))),
	send(@rarrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl35,rechts)))),
	send(@cbox3,display,@rarrow3,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow3,display,new(@upbox3,box(16,32))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@uparrow3,display,new(_,line(7,0,7,30))),
	send(@uparrow3,display,new(_,line(7,30,15,20))),
	send(@uparrow3,display,new(_,line(7,30,0,20))),
	send(@uparrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl35,oben)))),
	send(@cbox3,display,@uparrow3,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow3,display,new(@dbox3,box(16,32))),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@darrow3,display,new(_,line(7,0,7,30))),
	send(@darrow3,display,new(_,line(7,0,15,10))),
	send(@darrow3,display,new(_,line(7,0,0,10))),
	send(@darrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl35,unten)))),
	send(@cbox3,display,@darrow3,point(75,40)),


	send(@plus3,display,new(_,box(15,15)),point(0,-5)),
	send(@plus3,display,new(_,text(+)),point(0,-5)),
	send(@minus3,display,new(_,box(15,15)),point(15,-5)),
	send(@minus3,display,new(_,text(-)),point(15,-5)),
	free(@number3),
	send(@cbox3,display,new(@number3,text(X)),point(75,60)),
	send(@plus3,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number235,@reisezeiten3,@dialog,plus,0)))),
	send(@minus3,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number235,@reisezeiten3,@dialog,minus,0)))),
	%+-10
	send(@plus3,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number235,@reisezeiten3,@dialog,plus10,0)))),
	send(@minus3,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number235,@reisezeiten3,@dialog,minus10,0)))),

	send(@cbox3,display,@plus3,point(50,50)),
	send(@cbox3,display,@minus3,point(100,50)),
	send(@cbox3,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox3,point(Ko1,Ko2)),

	send(@plus23,display,new(_,box(15,15)),point(0,5)),
	send(@plus23,display,new(_,text(+)),point(0,5)),
	send(@minus23,display,new(_,box(15,15)),point(15,5)),
	send(@minus23,display,new(_,text(-)),point(15,7)),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y)),point(75,85)),
	send(@plus23,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number235,@reisezeiten3,@dialog,0,plus)))),
	send(@minus23,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number235,@reisezeiten3,@dialog,0,minus)))),
	%+-10
	send(@plus23,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number235,@reisezeiten3,@dialog,0,plus10)))),
	send(@minus23,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number235,@reisezeiten3,@dialog,0,minus10)))),

	send(@cbox3,display,@plus23,point(50,100)),
	send(@cbox3,display,@minus23,point(100,100)),
	send(@cbox3,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox3,point(Ko1,Ko2)).

%für die wahl einer richtung für maschine 2.
richtungswahl35(rechts):-
	free(@selected_arrow3),
	send(@rarrow3,display,new(@selected_arrow3,line(0,8,30,8))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(red)).

richtungswahl35(links):-
	free(@selected_arrow3),
	send(@larrow3,display,new(@selected_arrow3,line(0,6,30,6))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(red)),
	send(@rbox3,fill_pattern,colour(yellow)).

richtungswahl35(oben):-
	free(@selected_arrow3),
	send(@uparrow3,display,new(@selected_arrow3,line(7,1,7,31))),
	send(@upbox3,fill_pattern,colour(red)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(yellow)).

richtungswahl35(unten):-
	free(@selected_arrow3),
	send(@darrow3,display,new(@selected_arrow3,line(7,0,7,30))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(red)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(yellow)).


%zum aktualisieren der reisezeiten von maschine 2.

new_number235(@reisezeiten3,@dialog,plus,_):-
	get(@reisezeiten3,x,X),
	Y is X+1,
	send(@reisezeiten3,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number235(@reisezeiten3,@dialog,minus,_):-
	get(@reisezeiten3,x,X),
	Y is X-1,
        send(@reisezeiten3,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number235(@reisezeiten3,@dialog,_,plus):-
	get(@reisezeiten3,y,Y),
	Y1 is Y+1,
	send(@reisezeiten3,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

new_number235(@reisezeiten3,@dialog,_,minus):-
	get(@reisezeiten3,y,Y),
	Y1 is Y-1,
	send(@reisezeiten3,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

%+-10
new_number235(@reisezeiten3,@dialog,plus10,_):-
	get(@reisezeiten3,x,X),
	Y is X+10,
	send(@reisezeiten3,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number235(@reisezeiten3,@dialog,minus10,_):-
	get(@reisezeiten3,x,X),
	Y is X-10,
        send(@reisezeiten3,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number235(@reisezeiten3,@dialog,_,plus10):-
	get(@reisezeiten3,y,Y),
	Y1 is Y+10,
	send(@reisezeiten3,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

new_number235(@reisezeiten3,@dialog,_,minus10):-
	get(@reisezeiten3,y,Y),
	Y1 is Y-10,
	send(@reisezeiten3,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).




%Richtungswahl für Ball 2:
richtungswahl25(rechts):-
	free(@selected_arrow2),
	send(@rarrow2,display,new(@selected_arrow2,line(0,8,30,8))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(red)).

richtungswahl25(links):-
	free(@selected_arrow2),
	send(@larrow2,display,new(@selected_arrow2,line(0,6,30,6))),
	send(@upbox2,fill_pattern,colour(yellow1)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(red)),
	send(@rbox2,fill_pattern,colour(yellow)).

richtungswahl25(oben):-
	free(@selected_arrow2),
	send(@uparrow2,display,new(@selected_arrow2,line(7,1,7,31))),
	send(@upbox2,fill_pattern,colour(red)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(yellow)).

richtungswahl25(unten):-
	free(@selected_arrow2),
	send(@darrow2,display,new(@selected_arrow2,line(7,0,7,30))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@dbox2,fill_pattern,colour(red)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(yellow)).


%Ersten Winkel zeichnen.
draw_triangle_lo5(_,0).
draw_triangle_lo5([X,Y],A):-
	Endpoint is X-A,
	send(@dwi2,display,line(X,Y,Endpoint,Y)),
	Yneu is Y+1,
	Aneu is A-1,
	draw_triangle_lo5([X,Yneu],Aneu).

%Zweiten Winkel zeichnen
draw_triangle_ru5(_,0).
draw_triangle_ru5([X,Y],A):-
	Endpoint is X+A,
	send(@wi1,display,line(X,Y,Endpoint,Y)),
	Yneu is Y-1,
	Aneu is A-1,
	draw_triangle_ru5([X,Yneu],Aneu).


rescale5(X,Y):-
	X1 is X mod 50,
	X2 is X-X1,
	Y is X2//50.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Level 6, Testfall 15, vorgesehene Laufzeit 34
%Fix: Ballposition, Ballrichtung, Schalter, Ziel, Barrieren am Ziel.
% Beweglich: Rest (sämtliche Winkel, Zeitmaschine, weitere Barriere auf
% 20.1)
% Initiale Ballrichtung sollte durch einen Pfeil angezeigt werden.
%[ ro-winkel fixiert, um trivialisierung zu vermeiden. -> reicht nicht]
%Idee zur Detrivialisierung: alle u-Winkel fixieren

level(6,[[[ball,1,[20,4],unten]],[[sw,5,[6,4],an,[4]]],[[barr,4,[5,1],zu,[5]]],[[wi,2,[5,8],ru],[wi,7,[12,4],lo],[wi,8,[12,10],lu],[wi,9,[3,10],ru],[wi,10,[3,4],ro],[wi,11,[20,8],lu],[wa,[20,1]],[wa,[4,0]],[wa,[6,0]]],[[goal,3,[5,0],auf,[],[]]],[[zm,6,34,3,[10,4],[ball,1,[10,4],rechts]]]]).

showtest13(level6,Max):-
	!,
	free(@ci),free(@ball),free(@box1),free(@darrowba6),
	free(@wi2),free(@wi7),free(@wi8),free(@wi9),free(@wi10),free(@wi11),
	free(@wa),
	free(@conn),
	free(@smile),
	free(@p),
	free(@zm),
	%free(@tm),
	free(@reisezeiten1),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 11')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	testfall(15,[1,[_Ba,Sw,Dyn,_Stat,Lo,_]]),
	preprocess_state([1,[[],Sw,Dyn,[[wa,[6,0]],[wa,[4,0]],[wi,2,[5,8],ru],[wi,8,[12,10],lu],[wi,9,[3,10],ru],[wi,11,[20,8],lu]],Lo,[]]],[1,[[],Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),
	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),
	new(@ball,device),
	send(@ball,display,new(@ci,circle(50)),point(0,50)),
	send(@ci,fill_pattern,colour(green)),
	send(@p,display,@ball,point(1000,200)),
	%Anzeigepfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
	new(@darrowba6,device),
	send(@darrowba6,display,new(_,line(7,0,7,30))),
	send(@darrowba6,display,new(_,line(7,0,15,10))),
	send(@darrowba6,display,new(_,line(7,0,0,10))),
	send(@p,display,@darrowba6,point(1018,170)),

%[650,200] sind die koordinaten der zielposition dieses winkels
        %Statischer Winkel
	new(@wi7,device),
	draw_triangle_lo76([180,80],50),
	send(@wi7,recogniser,new(move_gesture(left))),
	send(@p,display,@wi7),

	%Statischer Winkel
	new(@wi10,device),
	draw_triangle_ro106([180,80],50),
	send(@wi10,recogniser,new(move_gesture(left))),
	send(@p,display,@wi10),

	%Statische Barriere
	new(@wa,device),
	send(@wa,display,new(@box1,box(45,45)),point(30,50)),
	send(@box1,fill_pattern,colour(grey)),
	send(@wa,recogniser,new(move_gesture(left))),
	send(@p,display,@wa),

	send(@p,display,new(@conn,bezier_curve(point(325,225),point(275,60),point(250,125)))),


	create_counting_box216(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	%NEU
	send(@dialog,display,@p),
	!,
	send(@dialog,append,button(start,message(@prolog,playl6,Max,@cbox1,@wa,@wi7,@wi10))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level6,Max))),
	send(@dialog,open).

playl6(Max,Zm,Wa,Wi7,Wi10):-
	free(@conn),free(@darrowba6),
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	get(Zm,x,Zmx),
	get(Zm,y,Zmy),
	get(@selected_arrow1,y,DirCode),
	reiserichtung16(DirCode,Dir),
	get(Wa,x,Wax),
	get(Wa,y,Way),
	get(Wi7,x,Wi7x),
	get(Wi7,y,Wi7y),
	get(Wi10,x,Wi10x),
	get(Wi10,y,Wi10y),

	rescale6(Zmx,Zmx1),rescale6(Zmy,Zmy1),rescale6(Wax,Wax1),rescale6(Way,Way1),
	rescale6(Wi7x,Wi7x1),rescale6(Wi7y,Wi7y1),
	rescale6(Wi10x,Wi10x1),rescale6(Wi10y,Wi10y1),

	Wax12 is Wax1+1, Way12 is Way1+1,
	Zmx12 is Zmx1+2, Zmy12 is Zmy1+2,
	Wi10x12 is Wi10x1+4, Wi10y12 is Wi10y1+2,
	Wi7x12 is Wi7x1+3, Wi7y12 is Wi7y1+2,

	free(@wa),free(@cbox1),free(@wi2),free(@wi7),free(@wi8),free(@wi9),free(@wi10),free(@wi11),free(@ball),

        testfall(15,[1,[_,Sw,Dyn,_Stat,Lo,_]]),
	!,
bester_verlauf1([1,[[[ball,1,[20,4],unten]],Sw,Dyn,[[wi,2,[5,8],ru],[wi,7,[Wi7x12,Wi7y12],lo],[wi,8,[12,10],lu],[wi,9,[3,10],ru],[wi,10,[Wi10x12,Wi10y12],ro],[wi,11,[20,8],lu],[wa,[Wax12,Way12]],[wa,[4,0]],[wa,[6,0]]],Lo,[[zm,6,St,Zi,[Zmx12,Zmy12],[ball,1,[Zmx12,Zmy12],Dir]]]]],P0,Max),

%	verlauf([1,[[[ball,1,[20,4],unten]],Sw,Dyn,[[wi,2,[5,8],ru],[wi,7,[Wi7x12,Wi7y12],lo],[wi,8,[12,10],lu],[wi,9,[3,10],ru],[wi,10,[Wi10x12,Wi10y12],ro],[wi,11,[20,8],lu],[wa,[Wax12,Way12]],[wa,[4,0]],[wa,[6,0]]],Lo,[[zm,6,St,Zi,[Zmx12,Zmy12],[ball,1,[Zmx12,Zmy12],Dir]]]]],Max,P0),
	%[wi,2,[5,8],ru],[wi,8,[12,10],lu],[wi,9,[3,10],ru],[wi,11,[20,8],lu]
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).

%Erste Zeitmaschine.
create_counting_box216(X,Y):-display_counting_box216(@p,@dialog,X,Y,60,60).

display_counting_box216(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl16,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl16,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl16,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl16,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number26,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number26,@reisezeiten1,@dialog,minus,0)))),
	%+10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number26,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number26,@reisezeiten1,@dialog,minus10,0)))),
	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number26,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number26,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number26,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number26,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl16(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl16(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl16(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl16(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung16(0,unten). reiserichtung16(1,oben). reiserichtung16(6, links). reiserichtung16(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number26(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number26(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number26(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number26(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number26(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number26(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number26(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number26(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).


rescale6(X,Y):-
	X1 is X mod 50,
	X2 is X-X1,
	Y is X2//50.

%Winkel zeichnen

%Wi7
draw_triangle_lo76(_,0).
draw_triangle_lo76([X,Y],A):-
	Endpoint is X-A,
	send(@wi7,display,line(X,Y,Endpoint,Y)),
	Yneu is Y+1,
	Aneu is A-1,
	draw_triangle_lo76([X,Yneu],Aneu).

%Wi10 (ro)
draw_triangle_ro106(_,0).
draw_triangle_ro106([X,Y],B):-
	Endpoint is X+B,
	send(@wi10,display,line(X,Y,Endpoint,Y)),
	Yneu is Y+1,
	Bneu is B-1,
	draw_triangle_ro106([X,Yneu],Bneu).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Level 16 - Testfall 17
% Variabel: Einstellung der Zeitmaschine. Rest ist fix. (Evtl.
% Ballrichtung variable machen?)
% Sonst: Startrichtung anzeigen!

level(7,[[[ball,1,[2,5],links]],[[sw,2,[10,5],aus,[3]]],[[barr,3,[20,5],auf,[2]]],[[wa,[0,5]]],[[goal,5,[22,5],auf,[],[]]],[[zm,4,40,1,[9,5],[ball,1,[9,5],rechts]]]]).


showtest13(level7,Max):-
	!,
	free(@ci),free(@ball),free(@larrowba7),
	free(@conn),
	free(@smile),
	free(@p),
	free(@zm),
	%free(@tm),
	free(@reisezeiten1),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 10')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	testfall(17,[1,[Ba,Sw,Dyn,Stat,Lo,_]]),
	preprocess_state([1,[Ba,Sw,Dyn,Stat,Lo,[]]],[1,[_,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),
	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),
	new(@ball,device),
	send(@ball,display,new(@ci,circle(50)),point(0,50)),
	send(@ci,fill_pattern,colour(green)),
	send(@p,display,@ball,point(100,250)),
	%Anzeigepfeil nach links.
	new(@larrowba7,device),
	send(@larrowba7,display,new(_,line(0,7,30,7))),
	send(@larrowba7,display,new(_,line(0,7,10,15))),
	send(@larrowba7,display,new(_,line(0,7,10,0))),
	send(@p,display,@larrowba7,point(75,265)),


	send(@p,display,new(@conn,bezier_curve(point(525,275),point(1025,275),point(750,115)))),
	send(@dialog,display,@p),

	create_counting_box217(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl7,Max,@cbox1))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level7,Max))),
	send(@dialog,open).

%Prüfung auf virtuelle Objekte auskommentiert.
%playl7(Max,B):-
%	free(@conn),free(@larrowba7),
%	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
%	get(@selected_arrow1,y,DirCode),
%	reiserichtung17(DirCode,Dir),

%	testfall(17,[1,[_,Sw,Dyn,Stat,Lo,_]]),
%%[[[ball,1,[2,5],links]],[[sw,2,[10,5],aus,[3]]],[[barr,3,[20,5],auf,[2]]],[[wa,[0,5]]],[[goal,5,[22,5],auf,[],[]]],[[zm,4,40,1,[9,5],[ball,1,[9,5],rechts]]]]
%catch_virtual_objects([1,[[[ball,1,[2,5],links]],Sw,Dyn,Stat,Lo,[[zm,4,St,Zi,[9,5],[ball,1,[9,5],Dir]]]]],Max,P0),
%!,
%	free(B),
%send(@p,display,new(_,text('Virtuelle Objekte gefunden',center,bold)),point(250,250)),
%	!,
%	preprocess_all(P0,P1),
%	!,
%	refine(P1,P2),
%	!,
%	verlauf_anzeigen(P2),
%	!,
%	send(timer(1.0),delay),
%	!,
%	misserfolgsmeldung,
%	!,
%	send(@p,open).

playl7(Max,B):-
	free(@conn),free(@larrowba7),
	free(@cbox1),
	free(@ball),
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	get(@selected_arrow1,y,DirCode),
	reiserichtung17(DirCode,Dir),

	free(B),
	testfall(17,[1,[_,Sw,Dyn,Stat,Lo,_]]),
	!,
%Toleranz Level 10
        bester_verlauf1([1,[[[ball,1,[2,5],links]],Sw,Dyn,Stat,Lo,[[zm,4,St,Zi,[9,5],[ball,1,[9,5],Dir]]]]],P0,Max),
	%	verlauf([1,[[[ball,1,[2,5],links]],Sw,Dyn,Stat,Lo,[[zm,4,St,Zi,[9,5],[ball,1,[9,5],Dir]]]]],Max,P0),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).

%Erste Zeitmaschine.
create_counting_box217(X,Y):-display_counting_box217(@p,@dialog,X,Y,430,230).

display_counting_box217(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl17,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl17,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl17,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl17,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number27,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number27,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number27,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number27,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	%send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number27,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number27,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number27,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number27,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	%send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl17(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl17(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl17(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl17(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung17(0,unten). reiserichtung17(1,oben). reiserichtung17(6, links). reiserichtung17(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number27(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number27(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number27(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number27(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number27(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number27(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number27(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number27(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%Level 8 - Testfall 21
%[[[ball,1,[0,10],rechts],[ball,2,[15,0],oben]],[[sw,3,[15,8],an,[5]]],[[barr,5,[9,10],zu,[3]]],[[wi,4,[15,10],lu]],[[goal,6,[11,10],auf,[],[]]],[[zm,7,8,10,[7,10],[ball,1,[7,10],rechts]]]]
% variabel: reisezeiten maschine, rest fix: einführungslevel für reisen
% in die zukunft.
% virtuellen ball eingeführt

level(8,[[[ball,1,[0,10],rechts],[ball,2,[15,0],oben],[ball,8,[0,-5],rechts]],[[sw,3,[15,8],an,[5]]],[[barr,5,[9,10],zu,[3]]],[[wi,4,[15,10],lu]],[[goal,6,[11,10],auf,[],[]]],[[zm,7,8,10,[7,10],[ball,1,[7,10],rechts]]]]).

showtest13(level8,Max):-
	!,
	free(@ba1),free(@ba2),free(@ci1),free(@uparrowba28),
	free(@conn),
	free(@smile),
	free(@p),
	free(@zm),
	free(@reisezeiten1),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 5')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	level(8,[Ba,Sw,Dyn,Stat,Lo,_Zm]),
	preprocess_state([1,[Ba,Sw,Dyn,Stat,Lo,[]]],[1,[_Ba1,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),

%erster ball mit pfeil
	new(@ba1,device),
	send(@ba1,display,new(@ci1,circle(50)),point(0,50)),
	send(@ci1,fill_pattern,colour(green)),
	send(@ba1,display,new(_,line(50,72,80,72))),
	send(@ba1,display,new(_,line(80,72,70,80))),
	send(@ba1,display,new(_,line(80,72,70,64))),
        send(@p,display,@ba1,point(0,500)),
	%send(@p,display,new(@ba1,circle(50)),point(0,500)),
	%send(@ba1,fill_pattern,colour(green)),
	%%Anzeigepfeil nach rechts, zum ball gehörig

	send(@p,display,new(@ba2,circle(50)),point(750,0)),
	send(@ba2,fill_pattern,colour(purple)),
	%Anzeigepfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	new(@uparrowba28,device),
	send(@uparrowba28,display,new(_,line(7,0,7,30))),
	send(@uparrowba28,display,new(_,line(7,30,15,20))),
	send(@uparrowba28,display,new(_,line(7,30,0,20))),
	send(@p,display,@uparrowba28,point(768,50)),


	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),

	send(@p,display,new(@conn,bezier_curve(point(775,425),point(475,525),point(675,525)))),
	send(@dialog,display,@p),

	create_counting_box218(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl8,Max,@cbox1))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level8,Max))),
	send(@dialog,open).

%playl8(Max,A):-
%	free(@conn),free(@ba1),free(@ci1),free(@ba2),free(@uparrowba28),
%	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
%	get(@selected_arrow1,y,DirCode),
%	reiserichtung18(DirCode,Dir),
%
%level(8,[Ba,Sw,Stat,Dyn,Lo,_]),
%catch_virtual_objects([1,[Ba,Sw,Stat,Dyn,Lo,[[zm,7,St,Zi,[7,10],[ball,1,[7,10],Dir]]]]],Max,P0),
%	!,
%	free(A),
%send(@p,display,new(_,text('Virtuelle Objekte gefunden',center,bold)),point(250,250)),
%	!,
%	preprocess_all(P0,P1),
%	!,
%	refine(P1,P2),
%	!,
%	verlauf_anzeigen(P2),
%	!,
%	send(timer(1.0),delay),
%	!,
%	misserfolgsmeldung,
%	!,
%	send(@p,open).

playl8(Max,A):-
	free(@conn), free(@ba1),free(@ba2), free(@ci1),free(@uparrowba28),
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	get(@selected_arrow1,y,DirCode),
	reiserichtung18(DirCode,Dir),

level(8,[Ba,Sw,Stat,Dyn,Lo,_]),
	!,
	%Toleranz Level 8 bzw. Level 5
	bester_verlauf2([1,[Ba,Sw,Stat,Dyn,Lo,[[zm,7,St,Zi,[7,10],[ball,1,[7,10],Dir]]]]],P0,Max),

	%verlauf([1,[Ba,Sw,Stat,Dyn,Lo,[[zm,7,St,Zi,[7,10],[ball,1,[7,10],Dir]]]]],Max,P0),
	!,
	free(A),
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success8(P0,Max),
	!,
	send(@p,open).

% success8 erwartet als endzustand keine leere ballliste, sondern ein
% singleton. damit stellen wir sicher, dass der fiktive ball nicht als
% zeichen des misserfolgs betrachtet wird.

success8(X,Max):-
	member([Max,[[_]|_]],X),
	erfolgsmeldung,
	send(@p,open).

success8(_,_):-
	misserfolgsmeldung,
	send(@p,open).


%Zeitmaschine.
% MODIFIZIERT: DIESE HIER IST UNBEWEGLICH! DYNAMISCHE FINDET MAN Z.B. IN
% LEVEL 1

create_counting_box218(X,Y):-display_counting_box218(@p,@dialog,X,Y,330,480).

display_counting_box218(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl18,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl18,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl18,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl18,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number28,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number28,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number28,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number28,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	%send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number28,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number28,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number28,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number28,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl18(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl18(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl18(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl18(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung18(0,unten). reiserichtung18(1,oben). reiserichtung18(6, links). reiserichtung18(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number28(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number28(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number28(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number28(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number28(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number28(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number28(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number28(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Level 9 - Testfall 20
%[1,[[[ball,0,[0,-5],rechts],[ball,1,[2,10],rechts]],[[sw,2,[8,3],aus,[4]],[sw,3,[8,7],an,[5]]],[[dwi,4,[8,10],lu,auf,[2]],[barr,5,[12,1],zu,[3]]],[[wi,6,[8,1],ro]],[[goal,7,[17,1],auf,[],[]]],[[zm,9,8,13,[10,1],[ball,1,[10,1],rechts]],[zm,8,12,1,[8,5],[ball,1,[8,5],unten]]]]]
%variabel: beide zeitmaschinen, winkel dwi (4), ballrichtung
%fix: ballposition, ziel, schalter, barriere, winkel wi (6)
% Noch zu machen: Winkel hüpft wild herum, eine der zeitmaschinen
% ebenfalls; evtl. könnte auch der zweite winkel variabel werden.

level(9,[[[ball,0,[0,-5],rechts],[ball,1,[2,10],rechts]],[[sw,2,[8,3],aus,[4]],[sw,3,[8,7],an,[5]]],[[dwi,4,[8,10],lu,auf,[2]],[barr,5,[12,1],zu,[3]]],[[wi,6,[8,1],ro]],[[goal,7,[17,1],auf,[],[]]],[[zm,9,8,13,[10,1],[ball,1,[10,1],rechts]],[zm,8,12,1,[8,5],[ball,1,[8,5],unten]]]]).

showtest13(level9,Max):-
	!,
	free(@ball),free(@larrow),free(@rarrow),free(@darrow),free(@uparrow),
	free(@ci),free(@lbox),free(@rbox),free(@dbox),free(@upbox),free(@button1),free(@button2),free(@buci1),free(@buci2),free(@dywa),free(@dywabo),

	free(@ball2),free(@larrow2),free(@rarrow2),free(@darrow2),free(@uparrow2),
	free(@ci2),free(@lbox2),free(@rbox2),free(@dbox2),free(@upbox2),
        free(@cbox1),
	free(@cbox3),
	free(@conn),
	free(@smile),
	free(@p),
	free(@zm1),
	free(@zm2),
	free(@wi1),
	free(@dwi2),
	free(@reisezeiten1),
	free(@reisezeiten2),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 6')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	new(@reisezeiten2,point),
	send(@reisezeiten2,x,0),send(@reisezeiten2,y,0),
	level(9,[_Ba,Sw,_Dyn,_Stat,Lo,_]),
	preprocess_state([1,[[],Sw,[[barr,5,[12,1],zu,[3]]],[[wi,6,[8,1],ro]],Lo,[]]],[1,[_,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),

%Erster Ball
	new(@ball,device),
	new(@larrow,device),
	new(@rarrow,device),
	new(@darrow,device),
	new(@uparrow,device),
	new(@ci,circle(50)),
	send(@ci,fill_pattern,colour(green)),
	send(@ball,display,@ci,point(60,60)),

	%Pfeil nach links.
	send(@larrow,display,new(@lbox,box(32,16))),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@larrow,display,new(_,line(0,7,30,7))),
	send(@larrow,display,new(_,line(0,7,10,15))),
	send(@larrow,display,new(_,line(0,7,10,0))),
	send(@larrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl9,links)))),
	send(@ball,display,@larrow,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow,display,new(@rbox,box(32,16))),
	send(@rbox,fill_pattern,colour(yellow)),
	send(@rarrow,display,new(_,line(0,7,30,7))),
	send(@rarrow,display,new(_,line(30,7,20,15))),
	send(@rarrow,display,new(_,line(30,7,20,0))),
	send(@rarrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl9,rechts)))),
	send(@ball,display,@rarrow,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow,display,new(@upbox,box(16,32))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@uparrow,display,new(_,line(7,0,7,30))),
	send(@uparrow,display,new(_,line(7,30,15,20))),
	send(@uparrow,display,new(_,line(7,30,0,20))),
	send(@uparrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl9,oben)))),
	send(@ball,display,@uparrow,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow,display,new(@dbox,box(16,32))),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@darrow,display,new(_,line(7,0,7,30))),
	send(@darrow,display,new(_,line(7,0,15,10))),
	send(@darrow,display,new(_,line(7,0,0,10))),
	send(@darrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl9,unten)))),
	send(@ball,display,@darrow,point(75,40)),

	send(@p,display,@ball,point(125,475)),

%Die Knöpfe
	new(@button1,device),
	new(@buci1,circle(1)),
	send(@buci1,fill_pattern,colour(red)),
	send(@button1,display,@buci1,point(60,60)),
	send(@button1,handle,handle(w,h/2,out)),
	send(@p,display,@button1,point(425,175)),

	new(@button2,device),
	new(@buci2,circle(1)),
	send(@buci2,fill_pattern,colour(red)),
	send(@button2,display,@buci2,point(60,60)),
	send(@button2,handle,handle(w,h/2,out)),
	send(@p,display,@button2,point(420,370)),

%Die zu Verbindungszwecken eingeführte dynamische Wand
        new(@dywa,device),
	new(@dywabo,circle(10)),
	send(@dywabo,fill_pattern,colour(grey)),
	send(@dywa,display,@dywabo,point(60,60)),
	send(@dywa,handle,handle(w,h/2,in)),
	send(@dywa,connect,@button2,@in_out_link),
	send(@p,display,@dywa,point(600,75)),

	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),

	% send(B1, handle, handle(w, h/2, in)),
% send(B2, handle, handle(w/2, 0, out)),
% send(B1,recogniser,new(move_gesture(left))),
% send(B2,recogniser,new(move_gesture(left))),
% send(B1, connect, B2, @in_out_link).

%Dynamischer Winkel
	new(@wi1,device),
	draw_triangle_lu9([180,80],50),
	send(@wi1,handle,handle(w/2,h/2,in)),
	send(@wi1,recogniser,new(move_gesture(left))),
	send(@p,display,@wi1),
	send(@wi1,connect,@button1,@in_out_link),

	%send(@p,display,new(@conn,bezier_curve(point(225,75),point(375,75),point(300,115)))),
	send(@dialog,display,@p),

	create_counting_box219(0,0),
	create_counting_box229(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl9,Max,@ball,@wi1,@cbox1,@cbox3))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level9,Max))),
	send(@dialog,open).

% A ist der Ball, C ist die erste Zeitmaschine,
% D die zweite Zeitmaschine

% abfangroutine für virtuelle objekte hier gelöscht, da zum einen extrem
% laufzeitintensiv und zum anderen vermutlich unnötig - mit dem
% spielmaterial sollten sich keine virtuellen objekte konstruieren
% lassen. das prädikat findet sich sonst als erster teil der definition
% von playl9 in leveldurchlauf2.pl

playl9(Max,A,F,C,D):-

free(@conn),free(@button1),free(@button2),free(@buci1),free(@buci2),free(@dywa),free(@dywabo),

	%Ballstartrichtung auslesen
	get(@selected_arrow,y,Ri),
	richtung9(Ri,StaRi),

	%Winkel
	get(F,x,W2x),
	get(F,y,W2y),


	%Erste Zeitmaschine (zm8)
	%Reisezeiten Zm1 auslesen
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	%Position der Zeitmaschine 1 auslesen
	get(C,x,B1),
	get(C,y,B2),

	%Reiserichtung der Zeitmaschine 1 auslesen
	get(@selected_arrow1,y,DirCode),
	reiserichtung19(DirCode,Dir),

	%Koordinaten der Zeitmaschine 1 normieren.
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,

	%Zweite Zeitmaschine (zm 9)
	%Reisezeiten Zm2 auslesen
	get(@reisezeiten2,x,St2),get(@reisezeiten2,y,Zi2),
	%Position der Zeitmaschine 2 auslesen
	get(D,x,C1),
	get(D,y,C2),

	%Reiserichtung der Zeitmaschine 2 auslesen
	get(@selected_arrow3,y,DirCode2),
	%das war mal 'reiserichtung', als solches aber undefiniert.
	reiserichtung19(DirCode2,Dir2),

	%Koordinaten der Zeitmaschine normieren.
	S1 is C1 mod 50,
	S2 is C2 mod 50,
	Zm2x is ((C1-S1)//50)+2,
	Zm2y is ((C2-S2)//50)+2,

	%Koordinaten des Winkels normieren
	rescale9(W2x,W2x1),
	rescale9(W2y,W2y1),
	W2x2 is W2x1+3,
	W2y2 is W2y1+1,

	level(9,[_,Sw,_Dyn,_Stat,Lo,_]),
	!,
	%Toleranzklausel Level 9 FUNKTIONIERT NOCH NICHT!
%verlauf([1,[[[ball,0,[0,-5],rechts],[ball,1,[2,10],StaRi]],Sw,[[dwi,4,[W2x2,W2y2],lu,auf,[2]],[barr,5,[12,1],zu,[3]]],[[wi,6,[8,1],ro]],Lo,[[zm,9,St2,Zi2,[Zm2x,Zm2y],[ball,1,[Zm2x,Zm2y],Dir2]],[zm,8,St,Zi,[B3,B4],[ball,1,[B3,B4],Dir]]]]],Max,P0),
	bester_verlauf2([1,[[[ball,0,[0,-5],rechts],[ball,1,[3,10],StaRi]],Sw,[[dwi,4,[W2x2,W2y2],lu,auf,[2]],[barr,5,[12,1],zu,[3]]],[[wi,6,[8,1],ro]],Lo,[[zm,9,St2,Zi2,[Zm2x,Zm2y],[ball,1,[Zm2x,Zm2y],Dir2]],[zm,8,St,Zi,[B3,B4],[ball,1,[B3,B4],Dir]]]]],P0,Max),

	free(A),free(C),free(D),free(F),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success9(P0,Max),
	!,
	send(@p,open).

% success9 erwartet als endzustand keine leere ballliste, sondern ein
% singleton. damit stellen wir sicher, dass der fiktive ball nicht als
% zeichen des misserfolgs betrachtet wird.

success9(X,Max):-
	member([Max,[[_]|_]],X),
	erfolgsmeldung,
	send(@p,open).

success9(_,_):-
	misserfolgsmeldung,
	send(@p,open).



%Erste Zeitmaschine.
create_counting_box219(X,Y):-display_counting_box219(@p,@dialog,X,Y,60,60).

display_counting_box219(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl19,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl19,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl19,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl19,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number29,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number29,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number29,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number29,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number29,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number29,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number29,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number29,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl19(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl19(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl19(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl19(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung19(0,unten). reiserichtung19(1,oben). reiserichtung19(6, links). reiserichtung19(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number29(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number29(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number29(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number29(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number29(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number29(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number29(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number29(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).





%Zweite Zeitmaschine.

create_counting_box229(X,Y):-display_counting_box229(@p,@dialog,X,Y,0,0).

display_counting_box229(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox3),
	free(@number3),
	free(@number23),
	free(@cbox3),
	free(@plus3),
	free(@minus3),
	free(@plus23),
	free(@minus23),
	free(@cball3),
	free(@larrow3),free(@rarrow3),free(@darrow3),free(@uparrow3),
	free(@selected_arrow3),
	free(@lbox3),free(@rbox3),free(@dbox3),free(@upbox3),
	new(@cbox3,device),
	new(@plus3,device),
	new(@minus3,device),
	new(@plus23,device),
	new(@minus23,device),
	new(@larrow3,device),
	new(@rarrow3,device),
	new(@darrow3,device),
	new(@uparrow3,device),
	new(@selected_arrow3,device),

	send(@cbox3,display,new(@mainbox3,box(50,50)),point(60,60)),
	send(@mainbox3,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow3,display,new(@lbox3,box(32,16))),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@larrow3,display,new(_,line(0,7,30,7))),
	send(@larrow3,display,new(_,line(0,7,10,15))),
	send(@larrow3,display,new(_,line(0,7,10,0))),
	send(@larrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl39,links)))),
	send(@cbox3,display,@larrow3,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow3,display,new(@rbox3,box(32,16))),
	send(@rbox3,fill_pattern,colour(yellow)),
	send(@rarrow3,display,new(_,line(0,7,30,7))),
	send(@rarrow3,display,new(_,line(30,7,20,15))),
	send(@rarrow3,display,new(_,line(30,7,20,0))),
	send(@rarrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl39,rechts)))),
	send(@cbox3,display,@rarrow3,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow3,display,new(@upbox3,box(16,32))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@uparrow3,display,new(_,line(7,0,7,30))),
	send(@uparrow3,display,new(_,line(7,30,15,20))),
	send(@uparrow3,display,new(_,line(7,30,0,20))),
	send(@uparrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl39,oben)))),
	send(@cbox3,display,@uparrow3,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow3,display,new(@dbox3,box(16,32))),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@darrow3,display,new(_,line(7,0,7,30))),
	send(@darrow3,display,new(_,line(7,0,15,10))),
	send(@darrow3,display,new(_,line(7,0,0,10))),
	send(@darrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl39,unten)))),
	send(@cbox3,display,@darrow3,point(75,40)),


	send(@plus3,display,new(_,box(15,15)),point(0,-5)),
	send(@plus3,display,new(_,text(+)),point(0,-5)),
	send(@minus3,display,new(_,box(15,15)),point(15,-5)),
	send(@minus3,display,new(_,text(-)),point(15,-5)),
	free(@number3),
	send(@cbox3,display,new(@number3,text(X)),point(75,60)),
	send(@plus3,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number239,@reisezeiten2,@dialog,plus,0)))),
	send(@minus3,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number239,@reisezeiten2,@dialog,minus,0)))),
	%+-10
	send(@plus3,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number239,@reisezeiten2,@dialog,plus10,0)))),
	send(@minus3,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number239,@reisezeiten2,@dialog,minus10,0)))),

	send(@cbox3,display,@plus3,point(50,50)),
	send(@cbox3,display,@minus3,point(100,50)),
	send(@cbox3,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox3,point(Ko1,Ko2)),

	send(@plus23,display,new(_,box(15,15)),point(0,5)),
	send(@plus23,display,new(_,text(+)),point(0,5)),
	send(@minus23,display,new(_,box(15,15)),point(15,5)),
	send(@minus23,display,new(_,text(-)),point(15,7)),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y)),point(75,85)),
	send(@plus23,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number239,@reisezeiten2,@dialog,0,plus)))),
	send(@minus23,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number239,@reisezeiten2,@dialog,0,minus)))),
	%+-10
	send(@plus23,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number239,@reisezeiten2,@dialog,0,plus10)))),
	send(@minus23,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number239,@reisezeiten2,@dialog,0,minus10)))),

	send(@cbox3,display,@plus23,point(50,100)),
	send(@cbox3,display,@minus23,point(100,100)),
	send(@cbox3,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox3,point(Ko1,Ko2)).

%für die wahl einer richtung für maschine 2.
richtungswahl39(rechts):-
	free(@selected_arrow3),
	send(@rarrow3,display,new(@selected_arrow3,line(0,8,30,8))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(red)).

richtungswahl39(links):-
	free(@selected_arrow3),
	send(@larrow3,display,new(@selected_arrow3,line(0,6,30,6))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(red)),
	send(@rbox3,fill_pattern,colour(yellow)).

richtungswahl39(oben):-
	free(@selected_arrow3),
	send(@uparrow3,display,new(@selected_arrow3,line(7,1,7,31))),
	send(@upbox3,fill_pattern,colour(red)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(yellow)).

richtungswahl39(unten):-
	free(@selected_arrow3),
	send(@darrow3,display,new(@selected_arrow3,line(7,0,7,30))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(red)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(yellow)).


%zum aktualisieren der reisezeiten von maschine 2.

new_number239(@reisezeiten2,@dialog,plus,_):-
	get(@reisezeiten2,x,X),
	Y is X+1,
	send(@reisezeiten2,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number239(@reisezeiten2,@dialog,minus,_):-
	get(@reisezeiten2,x,X),
	Y is X-1,
        send(@reisezeiten2,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number239(@reisezeiten2,@dialog,_,plus):-
	get(@reisezeiten2,y,Y),
	Y1 is Y+1,
	send(@reisezeiten2,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

new_number239(@reisezeiten2,@dialog,_,minus):-
	get(@reisezeiten2,y,Y),
	Y1 is Y-1,
	send(@reisezeiten2,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

%+-10
new_number239(@reisezeiten2,@dialog,plus10,_):-
	get(@reisezeiten2,x,X),
	Y is X+10,
	send(@reisezeiten2,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number239(@reisezeiten2,@dialog,minus10,_):-
	get(@reisezeiten2,x,X),
	Y is X-10,
        send(@reisezeiten2,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number239(@reisezeiten2,@dialog,_,plus10):-
	get(@reisezeiten2,y,Y),
	Y1 is Y+10,
	send(@reisezeiten2,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

new_number239(@reisezeiten2,@dialog,_,minus10):-
	get(@reisezeiten2,y,Y),
	Y1 is Y-10,
	send(@reisezeiten2,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).


%Zweiten Winkel zeichnen
%Wi8 (lu)
draw_triangle_lu9(_,0).
draw_triangle_lu9([X,Y],A):-
	Endpoint is X-A,
	send(@wi1,display,line(X,Y,Endpoint,Y)),
	Yneu is Y-2,
	Aneu is A-2,
	draw_triangle_lu9([X,Yneu],Aneu).

rescale9(X,Y):-
	X1 is X mod 50,
	X2 is X-X1,
	Y is X2//50.


richtungswahl9(rechts):-
	free(@selected_arrow),
	send(@rarrow,display,new(@selected_arrow,line(0,8,30,8))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(red)).

richtungswahl9(links):-
	free(@selected_arrow),
	send(@larrow,display,new(@selected_arrow,line(0,6,30,6))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(red)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungswahl9(oben):-
	free(@selected_arrow),
	send(@uparrow,display,new(@selected_arrow,line(7,1,7,31))),
	send(@upbox,fill_pattern,colour(red)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungswahl9(unten):-
	free(@selected_arrow),
	send(@darrow,display,new(@selected_arrow,line(7,0,7,30))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(red)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(yellow)).

richtung9(0,unten). richtung9(1,oben). richtung9(6,links). richtung9(8,rechts).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LEVEL 10 hat derzeit zweckes rechenzeitersparnis keine überprüfung auf
% virtuelle objekte - vermutlich können sich ohnehin keine ergeben.
%ausserdem: virtueller ball im hintergrund eingefügt!

%Level 10 - Testfall 22
%[1,[[[ball,1,[0,2],rechts],[ball,2,[0,5],rechts]],[[sw,7,[12,3],an,[5]],[sw,8,[7,5],aus,[6]]],[[barr,5,[5,5],zu,[7]],[barr,6,[8,2],auf,[8]]],[[wi,4,[12,2],lo]],[[goal,3,[12,5],auf,[],[]]],[[zm,10,7,10,[6,5],[ball,2,[6,5],rechts]],[zm,9,12,1,[11,2],[ball,1,[11,2],rechts]]]]]
%variabel: ballrichtungen, zeitmaschinen, winkel

%WINKEL IN DER ANZEIGE ÄNDERN - ANSONSTEN SCHEINT ES ZU STIMMEN.

level(10,[[[ball,1,[0,2],rechts],[ball,2,[0,5],rechts]],[[sw,7,[12,3],an,[5]],[sw,8,[7,5],aus,[6]]],[[barr,5,[5,5],zu,[7]],[barr,6,[8,2],auf,[8]]],[[wi,4,[12,2],lo]],[[goal,3,[12,5],auf,[],[]]],[[zm,10,7,10,[6,5],[ball,2,[6,5],rechts]],[zm,9,12,1,[11,2],[ball,1,[11,2],rechts]]]]).

showtest13(level10,Max):-
	!,
	free(@ball),free(@larrow),free(@rarrow),free(@darrow),free(@uparrow),
	free(@ci),free(@lbox),free(@rbox),free(@dbox),free(@upbox),

	free(@ball2),free(@larrow2),free(@rarrow2),free(@darrow2),free(@uparrow2),
	free(@ci2),free(@lbox2),free(@rbox2),free(@dbox2),free(@upbox2),
        free(@cbox1),
	free(@cbox3),
	free(@conn),free(@conn110),free(@conn210),
	free(@smile),
	free(@p),
	free(@zm1),
	free(@zm2),
	free(@wi1),
	free(@dwi2),
	%free(@tm),
	free(@reisezeiten1),
	free(@reisezeiten3),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 7')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	new(@reisezeiten3,point),
	send(@reisezeiten3,x,0),send(@reisezeiten3,y,0),
	level(10,[_Ba,Sw,Dyn,_Stat,Lo,_]),
	preprocess_state([1,[[],Sw,Dyn,[],Lo,[]]],[1,[_,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),

%Erster Ball
	new(@ball,device),
	new(@larrow,device),
	new(@rarrow,device),
	new(@darrow,device),
	new(@uparrow,device),
	new(@ci,circle(50)),
	send(@ci,fill_pattern,colour(purple)),
	send(@ball,display,@ci,point(60,60)),

	%Pfeil nach links.
	send(@larrow,display,new(@lbox,box(32,16))),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@larrow,display,new(_,line(0,7,30,7))),
	send(@larrow,display,new(_,line(0,7,10,15))),
	send(@larrow,display,new(_,line(0,7,10,0))),
	send(@larrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl10,links)))),
	send(@ball,display,@larrow,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow,display,new(@rbox,box(32,16))),
	send(@rbox,fill_pattern,colour(yellow)),
	send(@rarrow,display,new(_,line(0,7,30,7))),
	send(@rarrow,display,new(_,line(30,7,20,15))),
	send(@rarrow,display,new(_,line(30,7,20,0))),
	send(@rarrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl10,rechts)))),
	send(@ball,display,@rarrow,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow,display,new(@upbox,box(16,32))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@uparrow,display,new(_,line(7,0,7,30))),
	send(@uparrow,display,new(_,line(7,30,15,20))),
	send(@uparrow,display,new(_,line(7,30,0,20))),
	send(@uparrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl10,oben)))),
	send(@ball,display,@uparrow,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow,display,new(@dbox,box(16,32))),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@darrow,display,new(_,line(7,0,7,30))),
	send(@darrow,display,new(_,line(7,0,15,10))),
	send(@darrow,display,new(_,line(7,0,0,10))),
	send(@darrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl10,unten)))),
	send(@ball,display,@darrow,point(75,40)),

	send(@p,display,@ball,point(-20,80)),

%Zweiter Ball
	new(@ball2,device),
	new(@larrow2,device),
	new(@rarrow2,device),
	new(@darrow2,device),
	new(@uparrow2,device),
	new(@ci2,circle(50)),
	send(@ci2,fill_pattern,colour(green)),
	send(@ball2,display,@ci2,point(60,60)),


	%Pfeil nach links.
	send(@larrow2,display,new(@lbox2,box(32,16))),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@larrow2,display,new(_,line(0,7,30,7))),
	send(@larrow2,display,new(_,line(0,7,10,15))),
	send(@larrow2,display,new(_,line(0,7,10,0))),
	send(@larrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl210,links)))),
	send(@ball2,display,@larrow2,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow2,display,new(@rbox2,box(32,16))),
	send(@rbox2,fill_pattern,colour(yellow)),
	send(@rarrow2,display,new(_,line(0,7,30,7))),
	send(@rarrow2,display,new(_,line(30,7,20,15))),
	send(@rarrow2,display,new(_,line(30,7,20,0))),
	send(@rarrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl210,rechts)))),
	send(@ball2,display,@rarrow2,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow2,display,new(@upbox2,box(16,32))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@uparrow2,display,new(_,line(7,0,7,30))),
	send(@uparrow2,display,new(_,line(7,30,15,20))),
	send(@uparrow2,display,new(_,line(7,30,0,20))),
	send(@uparrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl210,oben)))),
	send(@ball2,display,@uparrow2,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow2,display,new(@dbox2,box(16,32))),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@darrow2,display,new(_,line(7,0,7,30))),
	send(@darrow2,display,new(_,line(7,0,15,10))),
	send(@darrow2,display,new(_,line(7,0,0,10))),
	send(@darrow2,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl210,unten)))),
	send(@ball2,display,@darrow2,point(75,40)),

	send(@p,display,@ball2,point(-20,230)),


	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),

	%Statischer Winkel
	new(@wi1,device),
	draw_triangle_lo10([180,80],50),
	send(@wi1,recogniser,new(move_gesture(left))),
	send(@p,display,@wi1),


	%[12,3]-[5,5]; [7,5]-[8,2]
	send(@p,display,new(@conn110,bezier_curve(point(625,175),point(275,275),point(475,140)))),
	send(@p,display,new(@conn210,bezier_curve(point(375,275),point(425,125),point(525,200)))),
	send(@dialog,display,@p),

	create_counting_box2110(0,0),
	create_counting_box2210(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl10,Max,@ball,@ball2,@cbox1,@cbox3,@wi1))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level10,Max))),
	send(@dialog,open).

% A ist der erste Ball, B der zweite Ball, C ist die erste Zeitmaschine,
% D die zweite Zeitmaschine, E der statische Winkel


playl10(Max,A,B,C,D,E):-


free(@conn),free(@conn110),free(@conn210),

	%Ballstartrichtung Ball 1 auslesen
	get(@selected_arrow,y,Ri),
	richtung(Ri,StaRi),

	%Ballstartrichtung Ball 2 auslesen
	get(@selected_arrow2,y,Ri2),
	richtung(Ri2,StaRi2),

	%Erster Winkel
	get(E,x,W1x),
	get(E,y,W1y),

	%Erste Zeitmaschine:
	%Reisezeiten Zm1 auslesen
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	%Position der Zeitmaschine 1 auslesen
	get(C,x,B1),
	get(C,y,B2),

	%Reiserichtung der Zeitmaschine 1 auslesen
	get(@selected_arrow1,y,DirCode),
	reiserichtung110(DirCode,Dir),

	%Koordinaten der Zeitmaschine 1 normieren.
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,

	%Zweite Zeitmaschine:
	%Reisezeiten Zm2 auslesen
	get(@reisezeiten3,x,St2),get(@reisezeiten3,y,Zi2),
	%Position der Zeitmaschine 2 auslesen
	get(D,x,C1),
	get(D,y,C2),

	%Reiserichtung der Zeitmaschine auslesen
	get(@selected_arrow3,y,DirCode2),
	reiserichtung(DirCode2,Dir2),

	%Koordinaten der Zeitmaschine normieren.
	S1 is C1 mod 50,
	S2 is C2 mod 50,
	Zm2x is ((C1-S1)//50)+2,
	Zm2y is ((C2-S2)//50)+2,


	%Koordinaten des ersten Winkels normieren

	rescale10(W1x,W1x1),
	rescale10(W1y,W1y1),
	W1x2 is W1x1+3,
	W1y2 is W1y1+2,

% [[[ball,1,[0,2],rechts],[ball,2,[0,5],rechts]],[[sw,7,[12,3],an,[5]],[sw,8,[7,5],aus,[6]]],[[barr,5,[5,5],zu,[7]],[barr,6,[8,2],auf,[8]]],[[wi,4,[12,2],lo]],[[goal,3,[12,5],auf,[],[]]],[[zm,10,7,10,[6,5],[ball,2,[6,5],rechts]],[zm,9,12,1,[11,2],[ball,1,[11,2],rechts]]]]

	level(10,[_,Sw,Dyn,_Stat,Lo,_]),
	!,
% verlauf([1,[[[ball,1,[0,2],StaRi],[ball,2,[0,5],StaRi2],[ball,11,[0,-5],rechts]],Sw,Dyn,[[wi,4,[W1x2,W1y2],lo]],Lo,[[zm,10,St,Zi,[B3,B4],[ball,2,[B3,B4],Dir]],[zm,9,St2,Zi2,[Zm2x,Zm2y],[ball,1,[Zm2x,Zm2y],Dir2]]]]],Max,P0),
%Toleranz Level 7
	bester_verlauf2([1,[[[ball,1,[0,2],StaRi],[ball,2,[0,5],StaRi2],[ball,11,[0,-5],rechts]],Sw,Dyn,[[wi,4,[W1x2,W1y2],lo]],Lo,[[zm,10,St,Zi,[B3,B4],[ball,2,[B3,B4],Dir]],[zm,9,St2,Zi2,[Zm2x,Zm2y],[ball,1,[Zm2x,Zm2y],Dir2]]]]],P0,Max),
	free(A),free(B),free(C),free(D),free(E),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success10(P0,Max),
	!,
	send(@p,open).

% success10 erwartet als endzustand keine leere ballliste, sondern ein
% singleton. damit stellen wir sicher, dass der fiktive ball nicht als
% zeichen des misserfolgs betrachtet wird.

success10(X,Max):-
	member([Max,[[_]|_]],X),
	erfolgsmeldung,
	send(@p,open).

success10(_,_):-
	misserfolgsmeldung,
	send(@p,open).


richtungswahl10(rechts):-
	free(@selected_arrow),
	send(@rarrow,display,new(@selected_arrow,line(0,8,30,8))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(red)).

richtungswahl10(links):-
	free(@selected_arrow),
	send(@larrow,display,new(@selected_arrow,line(0,6,30,6))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(red)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungswahl10(oben):-
	free(@selected_arrow),
	send(@uparrow,display,new(@selected_arrow,line(7,1,7,31))),
	send(@upbox,fill_pattern,colour(red)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungswahl10(unten):-
	free(@selected_arrow),
	send(@darrow,display,new(@selected_arrow,line(7,0,7,30))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(red)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(yellow)).

richtung10(0,unten). richtung10(1,oben). richtung10(6,links). richtung10(8,rechts).


%Erste Zeitmaschine.
create_counting_box2110(X,Y):-display_counting_box2110(@p,@dialog,X,Y,325,275).

display_counting_box2110(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),

	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl110,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl110,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl110,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl110,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number210,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number210,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number210,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number210,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number210,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number210,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number210,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number210,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	%send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl110(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl110(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl110(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl110(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung110(0,unten). reiserichtung110(1,oben). reiserichtung110(6, links). reiserichtung110(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number210(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number210(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number210(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number210(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number210(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number210(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number210(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number210(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).


%Zweite Zeitmaschine.

create_counting_box2210(X,Y):-display_counting_box2210(@p,@dialog,X,Y,575,125).

display_counting_box2210(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox3),
	free(@number3),
	free(@number23),
	free(@cbox3),
	free(@plus3),
	free(@minus3),
	free(@plus23),
	free(@minus23),
	free(@cball3),
	free(@larrow3),free(@rarrow3),free(@darrow3),free(@uparrow3),
	free(@selected_arrow3),
	free(@lbox3),free(@rbox3),free(@dbox3),free(@upbox3),
	new(@cbox3,device),
	new(@plus3,device),
	new(@minus3,device),
	new(@plus23,device),
	new(@minus23,device),
	new(@larrow3,device),
	new(@rarrow3,device),
	new(@darrow3,device),
	new(@uparrow3,device),
	new(@selected_arrow3,device),

	send(@cbox3,display,new(@mainbox3,box(50,50)),point(60,60)),
	send(@mainbox3,fill_pattern,colour(purple)),

	%Pfeil nach links.
	send(@larrow3,display,new(@lbox3,box(32,16))),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@larrow3,display,new(_,line(0,7,30,7))),
	send(@larrow3,display,new(_,line(0,7,10,15))),
	send(@larrow3,display,new(_,line(0,7,10,0))),
	send(@larrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl310,links)))),
	send(@cbox3,display,@larrow3,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow3,display,new(@rbox3,box(32,16))),
	send(@rbox3,fill_pattern,colour(yellow)),
	send(@rarrow3,display,new(_,line(0,7,30,7))),
	send(@rarrow3,display,new(_,line(30,7,20,15))),
	send(@rarrow3,display,new(_,line(30,7,20,0))),
	send(@rarrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl310,rechts)))),
	send(@cbox3,display,@rarrow3,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow3,display,new(@upbox3,box(16,32))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@uparrow3,display,new(_,line(7,0,7,30))),
	send(@uparrow3,display,new(_,line(7,30,15,20))),
	send(@uparrow3,display,new(_,line(7,30,0,20))),
	send(@uparrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl310,oben)))),
	send(@cbox3,display,@uparrow3,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow3,display,new(@dbox3,box(16,32))),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@darrow3,display,new(_,line(7,0,7,30))),
	send(@darrow3,display,new(_,line(7,0,15,10))),
	send(@darrow3,display,new(_,line(7,0,0,10))),
	send(@darrow3,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl310,unten)))),
	send(@cbox3,display,@darrow3,point(75,40)),


	send(@plus3,display,new(_,box(15,15)),point(0,-5)),
	send(@plus3,display,new(_,text(+)),point(0,-5)),
	send(@minus3,display,new(_,box(15,15)),point(15,-5)),
	send(@minus3,display,new(_,text(-)),point(15,-5)),
	free(@number3),
	send(@cbox3,display,new(@number3,text(X)),point(75,60)),
	send(@plus3,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number2310,@reisezeiten3,@dialog,plus,0)))),
	send(@minus3,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number2310,@reisezeiten3,@dialog,minus,0)))),
	%+-10
	send(@plus3,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number2310,@reisezeiten3,@dialog,plus10,0)))),
	send(@minus3,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number2310,@reisezeiten3,@dialog,minus10,0)))),

	send(@cbox3,display,@plus3,point(50,50)),
	send(@cbox3,display,@minus3,point(100,50)),
	send(@cbox3,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox3,point(Ko1,Ko2)),

	send(@plus23,display,new(_,box(15,15)),point(0,5)),
	send(@plus23,display,new(_,text(+)),point(0,5)),
	send(@minus23,display,new(_,box(15,15)),point(15,5)),
	send(@minus23,display,new(_,text(-)),point(15,7)),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y)),point(75,85)),
	send(@plus23,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number2310,@reisezeiten3,@dialog,0,plus)))),
	send(@minus23,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number2310,@reisezeiten3,@dialog,0,minus)))),
	%+-10
	send(@plus23,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number2310,@reisezeiten3,@dialog,0,plus10)))),
	send(@minus23,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number2310,@reisezeiten3,@dialog,0,minus10)))),

	send(@cbox3,display,@plus23,point(50,100)),
	send(@cbox3,display,@minus23,point(100,100)),
	%send(@cbox3,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox3,point(Ko1,Ko2)).

%für die wahl einer richtung für maschine 2.
richtungswahl310(rechts):-
	free(@selected_arrow3),
	send(@rarrow3,display,new(@selected_arrow3,line(0,8,30,8))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(red)).

richtungswahl310(links):-
	free(@selected_arrow3),
	send(@larrow3,display,new(@selected_arrow3,line(0,6,30,6))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(red)),
	send(@rbox3,fill_pattern,colour(yellow)).

richtungswahl310(oben):-
	free(@selected_arrow3),
	send(@uparrow3,display,new(@selected_arrow3,line(7,1,7,31))),
	send(@upbox3,fill_pattern,colour(red)),
	send(@dbox3,fill_pattern,colour(yellow)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(yellow)).

richtungswahl310(unten):-
	free(@selected_arrow3),
	send(@darrow3,display,new(@selected_arrow3,line(7,0,7,30))),
	send(@upbox3,fill_pattern,colour(yellow)),
	send(@dbox3,fill_pattern,colour(red)),
	send(@lbox3,fill_pattern,colour(yellow)),
	send(@rbox3,fill_pattern,colour(yellow)).


%zum aktualisieren der reisezeiten von maschine 2.

new_number2310(@reisezeiten3,@dialog,plus,_):-
	get(@reisezeiten3,x,X),
	Y is X+1,
	send(@reisezeiten3,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number2310(@reisezeiten3,@dialog,minus,_):-
	get(@reisezeiten3,x,X),
	Y is X-1,
        send(@reisezeiten3,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number2310(@reisezeiten3,@dialog,_,plus):-
	get(@reisezeiten3,y,Y),
	Y1 is Y+1,
	send(@reisezeiten3,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

new_number2310(@reisezeiten3,@dialog,_,minus):-
	get(@reisezeiten3,y,Y),
	Y1 is Y-1,
	send(@reisezeiten3,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

%+-10
new_number2310(@reisezeiten3,@dialog,plus10,_):-
	get(@reisezeiten3,x,X),
	Y is X+10,
	send(@reisezeiten3,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number2310(@reisezeiten3,@dialog,minus10,_):-
	get(@reisezeiten3,x,X),
	Y is X-10,
        send(@reisezeiten3,x,Y),
	free(@number3),
	send(@cbox3,display,new(@number3,text(Y)),point(75,60)).

new_number2310(@reisezeiten3,@dialog,_,plus10):-
	get(@reisezeiten3,y,Y),
	Y1 is Y+10,
	send(@reisezeiten3,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).

new_number2310(@reisezeiten3,@dialog,_,minus10):-
	get(@reisezeiten3,y,Y),
	Y1 is Y-10,
	send(@reisezeiten3,y,Y1),
	free(@number23),
	send(@cbox3,display,new(@number23,text(Y1)),point(75,85)).



%Richtungswahl für Ball 2:
richtungswahl210(rechts):-
	free(@selected_arrow2),
	send(@rarrow2,display,new(@selected_arrow2,line(0,8,30,8))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(red)).

richtungswahl210(links):-
	free(@selected_arrow2),
	send(@larrow2,display,new(@selected_arrow2,line(0,6,30,6))),
	send(@upbox2,fill_pattern,colour(yellow1)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(red)),
	send(@rbox2,fill_pattern,colour(yellow)).

richtungswahl210(oben):-
	free(@selected_arrow2),
	send(@uparrow2,display,new(@selected_arrow2,line(7,1,7,31))),
	send(@upbox2,fill_pattern,colour(red)),
	send(@dbox2,fill_pattern,colour(yellow)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(yellow)).

richtungswahl210(unten):-
	free(@selected_arrow2),
	send(@darrow2,display,new(@selected_arrow2,line(7,0,7,30))),
	send(@upbox2,fill_pattern,colour(yellow)),
	send(@dbox2,fill_pattern,colour(red)),
	send(@lbox2,fill_pattern,colour(yellow)),
	send(@rbox2,fill_pattern,colour(yellow)).

%Winkel erzeugen.
draw_triangle_lo10(_,0).
draw_triangle_lo10([X,Y],A):-
	Endpoint is X-A,
	send(@wi1,display,line(X,Y,Endpoint,Y)),
	Yneu is Y+1,
	Aneu is A-1,
	draw_triangle_lo10([X,Yneu],Aneu).


rescale10(X,Y):-
	X1 is X mod 50,
	X2 is X-X1,
	Y is X2//50.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%Level 11 - Testfall 23
%Sollte als Einführung letztlich vor Level 10 gerückt werden.
%[1,[[[ball,1,[2,2],rechts],[ball,2,[2,6],rechts]],[[sw,6,[8,2],aus,[5]]],[[barr,5,[10,6],auf,[6]]],[[wi,4,[12,2],lo]],[[goal,3,[12,6],auf,[],[]]],[[zm,7,7,1,[8,6],[ball,2,[8,6],rechts]]]]]
%
%variabel: ballrichtung, zeitmaschine, winkel
%

level(11,[[[ball,1,[2,2],rechts],[ball,2,[2,6],rechts]],[[sw,6,[8,2],aus,[5]]],[[barr,5,[10,6],auf,[6]]],[[wi,4,[12,2],lo]],[[goal,3,[12,6],auf,[],[]]],[[zm,7,7,1,[8,6],[ball,2,[8,6],rechts]]]]).

showtest13(level11,Max):-
	!,
	free(@ci),free(@ball), free(@ci2),free(@ball2), free(@wi1),
	free(@conn),
	free(@smile),
	free(@p),
	free(@zm),
	%free(@tm),
	free(@reisezeiten1),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 1')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
        level(11,[_Ba,Sw,Dyn,_Stat,Lo,_]),
	preprocess_state([1,[[],Sw,Dyn,[],Lo,[]]],[1,[_Ba1,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),
	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),

	new(@ball,device),
	send(@ball,display,new(@ci,circle(50)),point(0,50)),
	send(@ci,fill_pattern,colour(green)),
	send(@p,display,@ball,point(100,100)),
	%Anzeigepfeil nach rechts, zum ball gehörig
	send(@ball,display,new(_,line(50,72,80,72))),
	send(@ball,display,new(_,line(80,72,70,80))),
	send(@ball,display,new(_,line(80,72,70,64))),

	new(@ball2,device),
	send(@ball2,display,new(@ci2,circle(50)),point(0,50)),
	send(@ci2,fill_pattern,colour(purple)),
	send(@p,display,@ball2,point(100,300)),
	%Anzeigepfeil nach rechts, zum ball2 gehörig
	send(@ball2,display,new(_,line(50,72,80,72))),
	send(@ball2,display,new(_,line(80,72,70,80))),
	send(@ball2,display,new(_,line(80,72,70,64))),



	%Statischer Winkel
	new(@wi1,device),
	draw_triangle_lo11([180,80],50),
	send(@wi1,recogniser,new(move_gesture(left))),
	send(@p,display,@wi1),


	send(@p,display,new(@conn,bezier_curve(point(425,125),point(525,325),point(300,115)))),
	send(@dialog,display,@p),

	create_counting_box2111(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl11,Max,@wi1,@cbox1))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level11,Max))),
	send(@dialog,open).
%A ist der Winkel, B die Zeitmaschine
playl11(Max,A,B):-
	free(@conn),
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	get(A,x,W1x),
	get(A,y,W1y),
	get(B,x,B1),
	get(B,y,B2),
	get(@selected_arrow1,y,DirCode),
	reiserichtung111(DirCode,Dir),
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,
	rescale(W1x,W1x1),
	rescale(W1y,W1y1),
	W1x2 is W1x1+3,
	W1y2 is W1y1+2,

	level(11,[Ba,Sw,Dyn,_Stat,Lo,_]),
	catch_virtual_objects([1,[Ba,Sw,Dyn,[[wi,4,[W1x2,W1y2],lo]],Lo,[[zm,7,St,Zi,[B3,B4],[ball,2,[B3,B4],Dir]]]]],Max,P0),
	!,
	free(A),
	free(B),
	send(@p,display,new(_,text('Virtuelle Objekte gefunden',center,bold)),point(250,250)),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	misserfolgsmeldung,
	!,
	send(@p,open).

playl11(Max,A,B):-
	free(@conn), free(@ball), free(@ball2),
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	get(A,x,W1x),
	get(A,y,W1y),
	get(B,x,B1),
	get(B,y,B2),
	get(@selected_arrow1,y,DirCode),
	reiserichtung111(DirCode,Dir),
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,
	rescale(W1x,W1x1),
	rescale(W1y,W1y1),
	W1x2 is W1x1+3,
	W1y2 is W1y1+2,
	level(11,[Ba,Sw,Dyn,_Stat,Lo,_]),
	!,
	free(A), free(B),
	%Toleranzänderung level 11
	bester_verlauf1([1,[Ba,Sw,Dyn,[[wi,4,[W1x2,W1y2],lo]],Lo,[[zm,7,St,Zi,[B3,B4],[ball,2,[B3,B4],Dir]]]]],P0,Max),
	!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success(P0,Max),
	!,
	send(@p,open).

%Erste Zeitmaschine.
create_counting_box2111(X,Y):-display_counting_box2111(@p,@dialog,X,Y,60,60).

display_counting_box2111(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(purple)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl111,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl111,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl111,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl111,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number211,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number211,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number211,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number211,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number211,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number211,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number211,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number211,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl111(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl111(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl111(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl111(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung111(0,unten). reiserichtung111(1,oben). reiserichtung111(6, links). reiserichtung111(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number211(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number211(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number211(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number211(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number211(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number211(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number211(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number211(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).


%Winkel erzeugen.
draw_triangle_lo11(_,0).
draw_triangle_lo11([X,Y],A):-
	Endpoint is X-A,
	send(@wi1,display,line(X,Y,Endpoint,Y)),
	Yneu is Y+1,
	Aneu is A-1,
	draw_triangle_lo11([X,Yneu],Aneu).





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
level(12,[[[ball,1,[2,3],rechts]],[[sw,6,[7,1],an,[8,9]],[sw,7,[7,3],an,[10,11]]],[[barr,8,[13,1],zu,[6]],[barr,9,[13,3],zu,[6]],[barr,10,[12,3],zu,[7]],[barr,11,[12,1],zu,[7]]],[[wi,2,[1,1],ro],[wi,3,[1,4],ru],[wi,4,[15,1],lo],[wi,5,[15,4],lu]],[[goal,12,[18,3],auf,[],[]]],[[zm,14,37,2,[6,1],[ball,1,[6,1],rechts]]]]).

showtest13(level12,Max):-
	!,
	free(@ball),free(@larrow),free(@rarrow),free(@darrow),free(@uparrow),
	free(@ci),free(@lbox),free(@rbox),free(@dbox),free(@upbox),free(@button1),free(@button2),free(@buci1),free(@buci2),

	free(@larrow2),free(@rarrow2),free(@darrow2),free(@uparrow2),
	free(@ci2),free(@lbox2),free(@rbox2),free(@dbox2),free(@upbox2),
        free(@cbox1),
	free(@cbox3),
	free(@conn),free(@conn1),free(@conn2),free(@conn3),
	free(@smile),
	free(@p),
	free(@zm1),
	free(@zm2),
	free(@wi1),
	free(@dwi2),
	free(@reisezeiten1),
	free(@dialog),
	free(start),
	free(quit),
	new(@dialog,dialog('Spielfeld Level 12 - nach einem Entwurf von Dominik Klein')),
	new(@p,picture),
	new(@reisezeiten1,point),
	send(@reisezeiten1,x,0),send(@reisezeiten1,y,0),
	level(12,[_Ba,Sw,Dyn,Stat,Lo,_]),
	preprocess_state([1,[[],Sw,Dyn,Stat,Lo,[]]],[1,[_,Sw1,Dyn1,Stat1,Lo1,Zm1]]),
	make_grid(1000),

%Erster Ball
	new(@ball,device),
	new(@larrow,device),
	new(@rarrow,device),
	new(@darrow,device),
	new(@uparrow,device),
	new(@ci,circle(50)),
	send(@ci,fill_pattern,colour(green)),
	send(@ball,display,@ci,point(60,60)),

	%Pfeil nach links.
	send(@larrow,display,new(@lbox,box(32,16))),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@larrow,display,new(_,line(0,7,30,7))),
	send(@larrow,display,new(_,line(0,7,10,15))),
	send(@larrow,display,new(_,line(0,7,10,0))),
	send(@larrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahll12,links)))),
	send(@ball,display,@larrow,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow,display,new(@rbox,box(32,16))),
	send(@rbox,fill_pattern,colour(yellow)),
	send(@rarrow,display,new(_,line(0,7,30,7))),
	send(@rarrow,display,new(_,line(30,7,20,15))),
	send(@rarrow,display,new(_,line(30,7,20,0))),
	send(@rarrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahll12,rechts)))),
	send(@ball,display,@rarrow,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow,display,new(@upbox,box(16,32))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@uparrow,display,new(_,line(7,0,7,30))),
	send(@uparrow,display,new(_,line(7,30,15,20))),
	send(@uparrow,display,new(_,line(7,30,0,20))),
	send(@uparrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahll12,oben)))),
	send(@ball,display,@uparrow,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow,display,new(@dbox,box(16,32))),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@darrow,display,new(_,line(7,0,7,30))),
	send(@darrow,display,new(_,line(7,0,15,10))),
	send(@darrow,display,new(_,line(7,0,0,10))),
	send(@darrow,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahll12,unten)))),
	send(@ball,display,@darrow,point(75,40)),

	send(@p,display,@ball,point(30,130)),

%Die Knöpfe
	new(@button1,device),
	new(@buci1,circle(1)),
	send(@buci1,fill_pattern,colour(red)),
	send(@button1,display,@buci1,point(60,60)),
	send(@button1,handle,handle(w,h/2,out)),
	send(@p,display,@button1,point(425,175)),

	new(@button2,device),
	new(@buci2,circle(1)),
	send(@buci2,fill_pattern,colour(red)),
	send(@button2,display,@buci2,point(60,60)),
	send(@button2,handle,handle(w,h/2,out)),
	send(@p,display,@button2,point(420,370)),


	free(@swcluster),
	new(@swcluster,device),
	maplist(display_sw(@swcluster),Sw1),
	send(@p,display,@swcluster),
	free(@dyncluster),
	new(@dyncluster,device),
	maplist(display_dyn(@dyncluster),Dyn1),
	send(@p,display,@dyncluster),
	free(@statcluster),
	new(@statcluster,device),
	maplist(display_stat(@statcluster),Stat1),
	send(@p,display,@statcluster),
	free(@locluster),
	new(@locluster,device),
	maplist(display_lo(@locluster),Lo1),
	send(@p,display,@locluster),
	free(@zmcluster),
	new(@zmcluster,device),
	insert_time(Zm1,1,Zm2),
	maplist(display_tm(@zmcluster),Zm2),
	send(@p,display,@zmcluster),


%Diverse Verbindungen

send(@p,display,new(@conn,bezier_curve(point(375,75),point(675,75),point(500,100)))),
send(@p,display,new(@conn1,bezier_curve(point(375,75),point(675,175),point(500,200)))),
send(@p,display,new(@conn2,bezier_curve(point(375,175),point(625,75),point(400,100)))),
send(@p,display,new(@conn3,bezier_curve(point(375,175),point(625,175),point(400,200)))),





	%send(@p,display,new(@conn,bezier_curve(point(225,75),point(375,75),point(300,115)))),
	send(@dialog,display,@p),

	create_counting_box21l12(0,0),

	send(@p,width(1200)),
	send(@p,height(600)),
	!,
	send(@dialog,append,button(start,message(@prolog,playl12,Max,@ball,@cbox1))),
	send(@dialog,append,button(back,message(@dialog,destroy))),
	send(@dialog,append,button(repeat,message(@prolog,showtest13,level12,Max))),
	send(@dialog,open).

% A ist der Ball, C ist dieZeitmaschine,

playl12(Max,A,C):-

free(@conn),free(@conn1),free(@conn2),free(@conn3),free(@button1),free(@button2),free(@buci1),free(@buci2),

	%Ballstartrichtung auslesen
	get(@selected_arrow,y,Ri),
	richtungl12(Ri,StaRi),

	%Erste Zeitmaschine (zm8)
	%Reisezeiten Zm1 auslesen
	get(@reisezeiten1,x,St),get(@reisezeiten1,y,Zi),
	%Position der Zeitmaschine 1 auslesen
	get(C,x,B1),
	get(C,y,B2),

	%Reiserichtung der Zeitmaschine 1 auslesen
	get(@selected_arrow1,y,DirCode),
	reiserichtung1l12(DirCode,Dir),

	%Koordinaten der Zeitmaschine 1 normieren.
	R1 is B1 mod 50,
	R2 is B2 mod 50,
	B3 is ((B1-R1)//50)+2,
	B4 is ((B2-R2)//50)+2,


%	%Koordinaten der Zeitmaschine normieren.
%	S1 is C1 mod 50,
%	S2 is C2 mod 50,
%	Zm2x is ((C1-S1)//50)+2,
%	Zm2y is ((C2-S2)//50)+2,


	level(12,[_,Sw,Dyn,Stat,Lo,_]),
	!,


%zeige_bild([1,[[[ball,1,[2,3],rechts]],[[sw,6,[7,1],an,[8,9]],[sw,7,[7,3],an,[10,11]]],[[barr,8,[13,1],zu,[6]],[barr,9,[13,3],zu,[6]],[barr,10,[12,3],zu,[7]],[barr,11,[12,1],zu,[7]]],[[wi,2,[1,1],ro],[wi,3,[1,4],ru],[wi,4,[15,1],lo],[wi,5,[15,4],lu]],[[goal,12,[18,3],auf,[],[]]],[[zm,14,37,2,[6,1],[ball,1,[6,1],rechts]]]]],50).
	bester_verlauf2([1,[[[ball,1,[2,3],StaRi]],Sw,Dyn,Stat,Lo,[[zm,14,St,Zi,[B3,B4],[ball,1,[B3,B4],Dir]]]]],P0,Max),

	free(A),free(C),!,
	preprocess_all(P0,P1),
	!,
	refine(P1,P2),
	!,
	verlauf_anzeigen(P2),
	!,
	send(timer(1.0),delay),
	!,
	success12(P0,Max),
	!,
	send(@p,open).

% Erfolgsmeldung

%success12(X,Max):-
%	member([Max,[[_]|_]],X),
%	erfolgsmeldung,
%	send(@p,open).

success12(X,Max):-
	member([Max,[[]|_]],X),
	erfolgsmeldung,
	send(@p,open).

success12(_,_):-
	misserfolgsmeldung,
	send(@p,open).



%Erste Zeitmaschine.
create_counting_box21l12(X,Y):-display_counting_box21l12(@p,@dialog,X,Y,60,60).

display_counting_box21l12(@p,@dialog,X,Y,Ko1,Ko2):-
	free(@mainbox1),
	free(@number1),
	free(@number21),
	free(@cbox1),
	free(@plus1),
	free(@minus1),
	free(@plus21),
	free(@minus21),
	free(@cball1),
	free(@larrow1),free(@rarrow1),free(@darrow1),free(@uparrow1),
	free(@selected_arrow1),
	free(@lbox1),free(@rbox1),free(@dbox1),free(@upbox1),
	new(@cbox1,device),
	new(@plus1,device),
	new(@minus1,device),
	new(@plus21,device),
	new(@minus21,device),
	new(@larrow1,device),
	new(@rarrow1,device),
	new(@darrow1,device),
	new(@uparrow1,device),
	new(@selected_arrow1,device),

	send(@cbox1,display,new(@mainbox1,box(50,50)),point(60,60)),
	send(@mainbox1,fill_pattern,colour(green)),

	%Pfeil nach links.
	send(@larrow1,display,new(@lbox1,box(32,16))),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@larrow1,display,new(_,line(0,7,30,7))),
	send(@larrow1,display,new(_,line(0,7,10,15))),
	send(@larrow1,display,new(_,line(0,7,10,0))),
	send(@larrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl1l12,links)))),
	send(@cbox1,display,@larrow1,point(38,75)),

	%Pfeil nach rechts
	send(@rarrow1,display,new(@rbox1,box(32,16))),
	send(@rbox1,fill_pattern,colour(yellow)),
	send(@rarrow1,display,new(_,line(0,7,30,7))),
	send(@rarrow1,display,new(_,line(30,7,20,15))),
	send(@rarrow1,display,new(_,line(30,7,20,0))),
	send(@rarrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl1l12,rechts)))),
	send(@cbox1,display,@rarrow1,point(100,75)),

	%Pfeil nach oben (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach unten)
	send(@uparrow1,display,new(@upbox1,box(16,32))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@uparrow1,display,new(_,line(7,0,7,30))),
	send(@uparrow1,display,new(_,line(7,30,15,20))),
	send(@uparrow1,display,new(_,line(7,30,0,20))),
	send(@uparrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl1l12,oben)))),
	send(@cbox1,display,@uparrow1,point(75,100)),

	%Pfeil nach unten (im Sinne der Bildschirmkoordinaten; tatsächlich zeigt er nach oben)
        send(@darrow1,display,new(@dbox1,box(16,32))),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@darrow1,display,new(_,line(7,0,7,30))),
	send(@darrow1,display,new(_,line(7,0,15,10))),
	send(@darrow1,display,new(_,line(7,0,0,10))),
	send(@darrow1,recogniser,new(click_gesture(left,'',single,message(@prolog,richtungswahl1l12,unten)))),
	send(@cbox1,display,@darrow1,point(75,40)),


	send(@plus1,display,new(_,box(15,15)),point(0,-5)),
	send(@plus1,display,new(_,text(+)),point(0,-5)),
	send(@minus1,display,new(_,box(15,15)),point(15,-5)),
	send(@minus1,display,new(_,text(-)),point(15,-5)),
	free(@number1),
	send(@cbox1,display,new(@number1,text(X)),point(75,60)),
	send(@plus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number2l12,@reisezeiten1,@dialog,plus,0)))),
	send(@minus1,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number2l12,@reisezeiten1,@dialog,minus,0)))),
	%+-10
	send(@plus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number2l12,@reisezeiten1,@dialog,plus10,0)))),
	send(@minus1,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number2l12,@reisezeiten1,@dialog,minus10,0)))),

	send(@cbox1,display,@plus1,point(50,50)),
	send(@cbox1,display,@minus1,point(100,50)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)),

	send(@plus21,display,new(_,box(15,15)),point(0,5)),
	send(@plus21,display,new(_,text(+)),point(0,5)),
	send(@minus21,display,new(_,box(15,15)),point(15,5)),
	send(@minus21,display,new(_,text(-)),point(15,7)),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y)),point(75,85)),
	send(@plus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number2l12,@reisezeiten1,@dialog,0,plus)))),
	send(@minus21,recogniser,new(click_gesture(left,'',single,message(@prolog,new_number2l12,@reisezeiten1,@dialog,0,minus)))),
	%+-10
	send(@plus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number2l12,@reisezeiten1,@dialog,0,plus10)))),
	send(@minus21,recogniser,new(click_gesture(right,'',single,message(@prolog,new_number2l12,@reisezeiten1,@dialog,0,minus10)))),

	send(@cbox1,display,@plus21,point(50,100)),
	send(@cbox1,display,@minus21,point(100,100)),
	send(@cbox1,recogniser,new(move_gesture(left))),
	send(@p,display,@cbox1,point(Ko1,Ko2)).


%	send(@dialog,display,@p),
%	send(@dialog,open).


%für die wahl einer richtung für maschine 1.
richtungswahl1l12(rechts):-
	free(@selected_arrow1),
	send(@rarrow1,display,new(@selected_arrow1,line(0,8,30,8))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(red)).

richtungswahl1l12(links):-
	free(@selected_arrow1),
	send(@larrow1,display,new(@selected_arrow1,line(0,6,30,6))),
	send(@upbox1,fill_pattern,colour(yellow1)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(red)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl1l12(oben):-
	free(@selected_arrow1),
	send(@uparrow1,display,new(@selected_arrow1,line(7,1,7,31))),
	send(@upbox1,fill_pattern,colour(red)),
	send(@dbox1,fill_pattern,colour(yellow)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

richtungswahl1l12(unten):-
	free(@selected_arrow1),
	send(@darrow1,display,new(@selected_arrow1,line(7,0,7,30))),
	send(@upbox1,fill_pattern,colour(yellow)),
	send(@dbox1,fill_pattern,colour(red)),
	send(@lbox1,fill_pattern,colour(yellow)),
	send(@rbox1,fill_pattern,colour(yellow)).

%damit können wir aus @selected_arrow die richtung wieder auslesen.
reiserichtung1l12(0,unten). reiserichtung1l12(1,oben). reiserichtung1l12(6, links). reiserichtung1l12(8,rechts).

%zum aktualisieren der reisezeiten von maschine 1.

new_number2l12(@reisezeiten1,@dialog,plus,_):-
	get(@reisezeiten1,x,X),
	Y is X+1,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number2l12(@reisezeiten1,@dialog,minus,_):-
	get(@reisezeiten1,x,X),
	Y is X-1,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number2l12(@reisezeiten1,@dialog,_,plus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number2l12(@reisezeiten1,@dialog,_,minus):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-1,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

%+-10
new_number2l12(@reisezeiten1,@dialog,plus10,_):-
	get(@reisezeiten1,x,X),
	Y is X+10,
	send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number2l12(@reisezeiten1,@dialog,minus10,_):-
	get(@reisezeiten1,x,X),
	Y is X-10,
        send(@reisezeiten1,x,Y),
	free(@number1),
	send(@cbox1,display,new(@number1,text(Y)),point(75,60)).

new_number2l12(@reisezeiten1,@dialog,_,plus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y+10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).

new_number2l12(@reisezeiten1,@dialog,_,minus10):-
	get(@reisezeiten1,y,Y),
	Y1 is Y-10,
	send(@reisezeiten1,y,Y1),
	free(@number21),
	send(@cbox1,display,new(@number21,text(Y1)),point(75,85)).



rescalel12(X,Y):-
	X1 is X mod 50,
	X2 is X-X1,
	Y is X2//50.


richtungswahll12(rechts):-
	free(@selected_arrow),
	send(@rarrow,display,new(@selected_arrow,line(0,8,30,8))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(red)).

richtungswahll12(links):-
	free(@selected_arrow),
	send(@larrow,display,new(@selected_arrow,line(0,6,30,6))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(red)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungswahll12(oben):-
	free(@selected_arrow),
	send(@uparrow,display,new(@selected_arrow,line(7,1,7,31))),
	send(@upbox,fill_pattern,colour(red)),
	send(@dbox,fill_pattern,colour(yellow)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungswahll12(unten):-
	free(@selected_arrow),
	send(@darrow,display,new(@selected_arrow,line(7,0,7,30))),
	send(@upbox,fill_pattern,colour(yellow)),
	send(@dbox,fill_pattern,colour(red)),
	send(@lbox,fill_pattern,colour(yellow)),
	send(@rbox,fill_pattern,colour(yellow)).

richtungl12(0,unten). richtungl12(1,oben). richtungl12(6,links). richtungl12(8,rechts).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Neuer Teil für Fehlertoleranz
%bester_verlauf probiert alle anfangskonstellationen aus, die sich
% von der eingegebenen in der startzeit um höchstens -2 bis +3
% unterscheiden; führt eine davon zu einem erfolgreichen verlauf,
% wird dieser angezeigt, andernfalls wird der eingegebene verlauf
% angezeigt.

% bester_verlauf1 ist für level, die ohne bälle auf dem feld enden
% sollen (also ohne hilfball für zukunftsreisen)

bester_verlauf1([I,X],Ver,Max):-
findall([I,Y],close_starting(X,Y),StartList),
verlaufliste(StartList,Max,VerlList),
member(Ver,VerlList),
member([Max,[[]|_]],Ver).

bester_verlauf1(X,Y,Max):-verlauf(X,Max,Y).


%bester_verlauf2 ist für level, die mit einem einzelnen ball enden
%(also mit hilfsball für zukunftsreisen)

bester_verlauf2([I,X],Ver,Max):-
findall([I,Y],close_starting(X,Y),StartList),
verlaufliste(StartList,Max,VerlList),
member(Ver,VerlList),
member([Max,[[_]|_]],Ver).

bester_verlauf2(X,Y,Max):-verlauf(X,Max,Y).

verlaufliste([],_,[]).
verlaufliste([X|Rest],Max,[Y|Rest1]):-
	!,verlauf(X,Max,Y),!,verlaufliste(Rest,Max,Rest1).

%generiert startzeitähnliche anfangskonfigurationen
close_starting([],[]).
close_starting([Ba,Sw,Dyn,Stat,Lo,Zm],[Ba,Sw,Dyn,Stat,Lo,Zm1]):-
	close_starting_times(Zm,Zm1).

%generiert ähnliche zeitmaschineneinstellungen
close_starting_times([],[]).
close_starting_times([[zm,Id,St,Zi,Ko,Ba]|Rest],[[zm,Id,StMod,Zi,Ko,Ba]|Rest1]):-
	small_diff(St,StMod),
	close_starting_times(Rest,Rest1).

small_diff(X,X).
small_diff(X,Y):-X>2,Y is X-2.
small_diff(X,Y):-X>1,Y is X-1.
small_diff(X,Y):-Y is X+1.
small_diff(X,Y):- Y is X+2.
small_diff(X,Y):- Y is X+3.

%Weitere Levelideen:

% zeige_bild([1,[[[ball,1,[1,10],oben]],[[sw,5,[12,6],aus,[4]],[sw,8,[4,12],an,[9]],[sw,12,[6,2],aus,[11]],[sw,14,[15,4],an,[13]],[sw,16,[8,6],aus,[17]]],[[dwi,4,[7,6],ro,auf,[5]],[barr,9,[13,2],zu,[8]],[dwi,11,[15,2],lo,auf,[12]],[barr,17,[14,2],auf,[16]],[barr,13,[2,2],zu,[14]]],[[wi,2,[1,12],ru],[wi,3,[7,12],lu],[wi,6,[15,6],lu]],[[goal,7,[1,2],auf,[],[]]],[[zm,15,25,2,[8,2],[ball,1,[8,2],links]],[zm,10,18,1,[10,6],[ball,1,[10,6],rechts]]]]],30).
%
%
%%Entwurf von Dominik:
%zeige_bild([1,[[[ball,1,[2,3],rechts]],[[sw,6,[7,1],an,[8,9]],[sw,7,[7,3],an,[10,11]]],[[barr,8,[13,1],zu,[6]],[barr,9,[13,3],zu,[6]],[barr,10,[12,3],zu,[7]],[barr,11,[12,1],zu,[7]]],[[wi,2,[1,1],ro],[wi,3,[1,4],ru],[wi,4,[15,1],lo],[wi,5,[15,4],lu]],[[goal,12,[18,3],auf,[],[]]],[[zm,14,37,2,[6,1],[ball,1,[6,1],rechts]]]]],50).



