% This buffer is for notes you don't want to save.
% If you want to create a file, visit that file with C-x C-f,
% then enter the text in that file's own buffer.

:-dynamic resistance/1.
:-dynamic white/1.
:-dynamic taos/1.


/**
	@descr Opens a dialog to choose how the player wants to use white dices and tao markers.
	@form  gui_fight(Resistance of ghost (Vector), White dices, Taomarkers(Vector)). 
*/
gui_fight(Resistance,White,Taos,GhostID):-
	   free(@fightmessage),
	   new(@fightmessage,label(fightmessage)),
	   send(@fightmessage,font,new(_,font(helvetica,roman,16))),
	   send(@fightmessage,colour,colour(red)),
	   file(ghostcard,GhostID,large,FilePath),
	   free(@fightghost),
	   new(@fightghost,bitmap(FilePath)),
	   assert((resistance(1))),
	   retractall(resistance(_)),
	   assert(resistance(Resistance)),
	   assert((white(1))),
	   retractall(white(_)),
	   assert(white(White)),
	   assert((taos(1))),
	   retractall(taos(_)),
	   assert(taos(Taos)),
	   free(@fight),
	   free(@whites),
	   free(@taos),
	   free(@dices),
	   free(@enemy),
	   free(@accept),
	   free(@abort),
	   new(@fight, dialog('Fight')),
	   new(@enemy, dialog_group('Ghosts remaining resistance')),
	   resistance,
	   new(@whites, dialog_group('White Dices')),
	   whitedices,	  		   
	   new(@taos, dialog_group('Available taomarkers')),
	   taos,
	   string(fight,acceptbutton,AcceptValue),
	   string(fight,abortbutton,AbortValue),
	   new(@accept, button(AcceptValue,message(@prolog, acceptfight))),
	   new(@abort, button(AbortValue,message(@prolog, abortfight))),
	   send(@fight, display, @fightghost,point(10,10)), 
	   send(@fight, display, @enemy,point(250,10)),
	   send(@fight, display, @whites,point(250,170)),
	   send(@fight, display, @taos, point(250,250)),
	   send(@fight, display, @accept,point(280,330)),
	   send(@fight, display, @abort,point(380,330)),
	   send(@fight, display, @fightmessage,point(150,380)),
	   send(@fight,open).

	       	
				   
/**
	@descr Displays the resitance of the ghost.
	@form resistance.
*/
resistance:-
	resistance(Resistance),
	free(@labeldicesyellow),
	free(@labeldicesred),
	free(@labeldicesgreen),
	free(@labeldicesblue),
	free(@labeldicesblack),
	new(@labeldicesyellow, label(labeldicesyellow)),
	new(@labeldicesred, label(labeldicesred)),
	new(@labeldicesgreen, label(labeldicesgreen)),
	new(@labeldicesblue, label(labeldicesblue)),
	new(@labeldicesblack, label(labeldicesblack)),
	Resistance=[Ye,Red,Gr,Bl,Black],
	string_concat('Yellow: ',Ye,B),
	string_concat('Red: ',Red,A),
	string_concat('Green: ',Gr,C),
	string_concat('Blue: ',Bl,D),
	string_concat('Black: ',Black,E),	
	send(@labeldicesyellow, value, B),
	send(@labeldicesred, value, A),
	send(@labeldicesgreen, value, C),
	send(@labeldicesblue, value, D),	
	send(@labeldicesblack, value, E),
	send(@labeldicesred, colour, red),
	send(@labeldicesyellow, colour, yellow),
	send(@labeldicesgreen, colour, green),
	send(@labeldicesblue, colour, blue),
	send(@enemy, append, @labeldicesred),
	send(@enemy, append, @labeldicesyellow),
	send(@enemy, append, @labeldicesgreen),
	send(@enemy, append, @labeldicesblue),
	send(@enemy, append, @labeldicesblack),
	send(@fight, append, @enemy).

/**
	@descr Displays how many white dices the player has.
	@form  whitedices.
*/
whitedices:-
	   free(@wred),
	   free(@wyel),
	   free(@wgre),
	   free(@wblu),
	   free(@wbla),
	   free(@whitedices),
	   
	   free(@labelwhitedices),
	   white(A),
	   string_concat('White dices left: ',A,B),
	   new(@labelwhitedices,label(whitedices)),
	   send(@labelwhitedices, value, B),
 	   new(@wred, button(red,message(@prolog,redbuttonpressed))),
 	   new(@wyel, button(yellow,message(@prolog,yelbuttonpressed))),
 	   new(@wgre, button(green,message(@prolog,greenbuttonpressed))),
 	   new(@wblu, button(blue,message(@prolog,bluebuttonpressed))),
 	   new(@wbla, button(black,message(@prolog,blackbuttonpressed))),
		   
	   send(@whites,append,@labelwhitedices),
	   send(@whites, append, @wyel),
	   send(@whites,append,@wred),
	   send(@whites,append,@wgre),
	   send(@whites, append, @wblu),
	   send(@whites,append,@wbla),
	   send(@wyel, colour, yellow),
	   send(@wred, colour, red),
	   send(@wgre, colour, green),
	   send(@wblu, colour, blue).

/**
	@descr Displaye how many taomarkers the player has.
	@form  taos.
*/
taos:-
	   taos([Y,R,G,Bl,Black]),
	   free(@tred),
	   free(@tyel),
	   free(@tgre),
	   free(@tblu),
	   free(@tbla),
	   new(@tyel, button(Y,message(@prolog,taoyelbuttonpressed))),
 	   new(@tred, button(R,message(@prolog,taoredbuttonpressed))),
 	   new(@tgre, button(G,message(@prolog,taogreenbuttonpressed))),
 	   new(@tblu, button(Bl,message(@prolog,taobluebuttonpressed))),
 	   new(@tbla, button(Black,message(@prolog,taoblackbuttonpressed))),
	   send(@taos,append,@tyel),
	   send(@taos, append, @tred),
	   send(@taos,append,@tgre),
	   send(@taos, append, @tblu),
	   send(@taos,append,@tbla),
	   send(@tyel, colour, yellow),
	   send(@tred, colour, red),
	   send(@tgre, colour, green),
	   send(@tblu, colour, blue).

/**
	@descr Checks whether the given number is negative.
	@form  negative(Number).
*/
negative(X):-X<0.	


/**
	@descr Changes the amount of recistance needed to win against the ghost.
	@form  changefightdices([Changes to white dices|Changes to resistance]). 	
*/
changefightdices([D|Vector]):-
	taos(Taos),
	white(W),
	Wh is W+D,
        not(negative(Wh)),
	resistance(Resistance),
	vectoradd(Resistance,Vector,NewRes),
        sublist(negative,Taos,[]),
	sublist(negative,NewRes,[]),
	retract(resistance(_)),
	assert(resistance(NewRes)),
	retractall(white(_)),
	assert(white(Wh)),
	NewRes=[Yel,Red,Gr,Bl,Black],
	string_concat('Yellow: ', Yel, Y2),
	string_concat('Green: ', Gr, Gr2),
	string_concat('Blue: ', Bl, Bl2),
	string_concat('Black: ', Black, Black2),
	send(@labeldicesyellow, value, Y2),
	send(@labeldicesgreen, value, Gr2),
	send(@labeldicesblue, value, Bl2),
	send(@labeldicesblack, value, Black2),
	string_concat('Red: ',Red,R2),
	send(@labeldicesred, value, R2),
	string_concat('White dices left: ',Wh,W2),
	send(@labelwhitedices, value, W2).

/**
	@descr Same as above but returns a message if the player wants to use a taomarker that is not needed.
*/
changefightdices([_|Vector]):-
	noFightMessage,
	resistance(Resistance),
	vectoradd(Resistance,Vector,NewRes),
	not(sublist(negative,NewRes,[])),
	string(fight,notallowed,Message),
	displayFightMessage(Message),
	write_ln('white dice error'),
	false.

redbuttonpressed:-
        changefightdices([-1,0,-1,0,0,0]).

yelbuttonpressed:-
        changefightdices([-1,-1,0,0,0,0]).

greenbuttonpressed:-
        changefightdices([-1,0,0,-1,0,0]).

bluebuttonpressed:-
        changefightdices([-1,0,0,0,-1,0]).

blackbuttonpressed:-
        changefightdices([-1,0,0,0,0,-1]).


taoyelbuttonpressed:-
	taochange([-1,0,0,0,0]),
	changefightdices([0,-1,0,0,0,0]).
taoredbuttonpressed:-
	taochange([0,-1,0,0,0]),
	changefightdices([0,0,-1,0,0,0]).
taogreenbuttonpressed:-
	taochange([0,0,-1,0,0]),
	changefightdices([0,0,0,-1,0,0]).
taobluebuttonpressed:-
	taochange([0,0,0,-1,0]),
	changefightdices([0,0,0,0,-1,0]).
taoblackbuttonpressed:-
	taochange([0,0,0,0,-1]),
	changefightdices([0,0,0,0,0,-1]).

/**
	@descr Changes the amount of available taomarkers.
	@form  taochange(Changes to taomarkers (Vector)). 	
*/
taochange(Vector):-
	noFightMessage,
	resistance(Resistance),
        vectoradd(Resistance,Vector,NewRes),
	sublist(negative,NewRes,[]),              
	taos(T),
	vectoradd(T,Vector,Taos),
	sublist(negative,Taos,[]),
        Taos=[Y,R,G,Bl,Black],
	retractall(taos(_)),
	assert(taos(Taos)),
	send(@tyel,label,Y),
	send(@tred,label,R),
	send(@tgre,label,G),
	send(@tblu,label,Bl),
	send(@tbla,label,Black).
	
taochange(Vector):-
	noFightMessage,
	resistance(Resistance),
        vectoradd(Resistance,Vector,NewRes),
	not(sublist(negative,NewRes,[])),
	string(fight,notallowed,Message),
	displayFightMessage(Message),
	write_ln('taofehler').
	

/**
	@descr Describes what happens when the player pushes the accept button.
	@form  acceptfight.
*/	
acceptfight:-
	taos(Taos),
	white(White),
	(resistance([0,0,0,0,0])->
	% ghost is defeated:
	(
	     % commented out b/c sent twice otherwise
	     % markersChosen(White,Taos), 
	     string(fight,ghostdefeated,Message),
	     displayFightMessage(Message)
	);
	% ghost is not defeated
	(    string(fight,notenough,Message),
	     displayFightMessage(Message)
	)), white(White), taos(Taos),  
	return_fight_accepted(White,Taos),
	send(@fight, destroy),
	free(@fight).

/**
	@descr Describes what happens when the player pushes the abort button.
	@form  abortfight.
*/	
abortfight:-
	send(@fight, destroy),
	free(@fight),
	% fight was aborted
	return_fight_aborted,
	write_ln('FIXME: Server bescheidsagen').


/**
	@descr Adds two vectors and returns the result.
        @form  vectoradd(Vector1, Vector2, Sum)
*/
vectoradd([],[],[]).
vectoradd([A|R1],[B|R2],[C|R3]):-
	C is A+B,
	vectoradd(R1,R2,R3).

displayFightMessage(Message):-
	send(@fightmessage,value,Message).

noFightMessage:-
	displayFightMessage('').
	
	
