/***
  @descr This file is used for defining dice functions:
      <ul>
	<li> rolling the dice
	<li> list incremention function
      </ul>
  @author Wir
  @date 24.02.2010
*/
  
% ########### TEST-WUERFEL ###########
/**
dice(Z,V) :-
	retract(diceCounter(X)),
	roll_dice_fake(X,V),	
	XN is X + 1,
	assert(diceCounter(XN)),
	broadcastColorDice(Z,V).

:- dynamic(diceCounter/1).
diceCounter(0).

roll_dice_fake(0,[1,0,0,1,0,0]).
roll_dice_fake(1,[0,3,0,0,0,0]).
roll_dice_fake(2,[0,0,1,0,2,0]).
roll_dice_fake(3,[2,0,0,1,0,0]).
*/
% ########### TEST-WUERFEL ###########
  

/**
  @form dice(+Count,-List)
  @descr rolls the die Count - times, returns a List with rolled colors
*/
dice(Z,V) :-
	diceStolen(Stolen),
	DiceLeft = Z - Stolen,
	DiceLeft =< 0,
	V = [0,0,0,0,0,0],
	broadcastColorDice(0,V).

dice(Z,V) :- 
	diceStolen(Stolen),
	ZN is Z - Stolen,
	roll_dice(ZN,[0,0,0,0,0,0],V),
	broadcastColorDice(ZN,V).

roll_dice(0,V,V).
roll_dice(Z,V,VN) :-
	Z > 0,
	ZN is Z - 1,
	roll_die(V,V2),
	roll_dice(ZN,V2,VN).

/**
  @descr rolls one die
  @form roll_die(?List,?List)
*/
roll_die(V,VN) :-
	Z is random(6),
	inc(V,Z,VN).

/**
  @descr increments the int at position
  @form inc(+list,+position,-list)
*/
inc([E|R],0,[EN|R]) :-
	EN is E + 1.

inc([E|R],Z,[E|RN]) :-
	Z > 0,
	ZN is Z - 1,
	inc(R,ZN,RN).

diceStolen(DiceCountStolen) :-
	findall(GhostID,
		(   ghostField(_,GhostID,_),
		    ghost(GhostID,_,_,_,stealDie,_,_,_)),
		StealList1),
	findall(GhostID,
		(   ghostField(_,GhostID,_),
		    ghost(GhostID,_,_,_,addGhostStealDie,_,_,_)),
		StealList2),
	length(StealList1,L1),
	length(StealList2,L2),
	DiceCountStolen is L1 + L2.

