/**
 @descr move taoist
 @form moveTaoist
 */
phaseMove :-
	currentPlayer(ID),
	taoist(ID,_,_,_,_,CurPos),
	movePossible(CurPos,PosList),
	playerTaoMove(PosList).

/**
 @descr move taoist one tile and start next phase
 @form executeMove(PlayerID,Position)
 */
executeMove(Pos) :-
	currentPlayer(ID),
	taoist(ID,Name,Markers,Qi,YY,CurPos),
	movePossible(CurPos,PosList),
	member(Pos,PosList),
	retract(taoist(ID,_,_,_,_,_)),
	assert(taoist(ID,Name,Markers,Qi,YY,Pos)),
	broadcastMove(ID,Pos),
	nextPhase.

/**
 @descr move taoist to any tile
 @form executeMove(PlayerID,TileID)
 */
executeMove(ID,Pos) :-
	member(Pos,[1,2,3,4,5,6,7,8,9]),
	retract(taoist(ID,Name,Markers,Qi,YY,_)),
	assert(taoist(ID,Name,Markers,Qi,YY,Pos)),
	broadcastMove(ID,Pos).

/**
 @descr asks player, what to do next
 @form taoDoNext
 */
phaseDecide :-
	currentPlayer(PlayerID),
	taoist(PlayerID,_,_,_,_,Pos),
	neighbourDirect(Pos,FieldList),
	playerTaoDoNext(Pos,FieldList).

/**
 @descr start next phase depending on the user's choice
 @form taoDoNext(Choice)
 */
phaseDecide(Next) :- 
	Next = (_,_),
	fight.

phaseDecide(Next) :-
	not(Next = (_,_)),
	village.

/**	
 @descr Change the amount of Qi points by Amount of the current player.
 @form changeQi(+Amount)
 */
addQi(Amount):-
	currentPlayer(Player),
	addQi(Player,Amount).

addQi(Player,Amount) :-
	taoist(Player,P2,_,Qi,_,_),
	NewQi is Qi + Amount,
	NewQi =< 0,
	village(1,_,_,_,Pos),
	retract(taoist(Player,_,_,_,_,_)),
	assert(taoist(Player,P2,[0,0,0,0,0],0,0,Pos)),
	broadcastQi(Player),
	broadcastTaoistDead(Player),
	!.	

addQi(Player,Amount) :-
	taoist(Player,P2,P3,Qi,P5,P6),
	NewQi is Qi + Amount,	
	retract(taoist(Player,_,_,_,_,_)),
	assert(taoist(Player,P2,P3,NewQi,P5,P6)),
	broadcastQi(Player).



/**
 @descr Add the given TaoMarkerList to the amount of TaoMarkers of the current player.
 @form changeTaoMarkers(+TaoMarkerList)
*/
addTaoMarkers(TaoMarkerList):-
	currentPlayer(Player),
	taoist(Player,P2,OldTaoMarkerList,P4,P5,P6),
	vectoradd(TaoMarkerList,OldTaoMarkerList,NewTaoMarkerList),
	normRes(NewTaoMarkerList,NormTaoMarkerList),
	retract(taoist(Player,_,_,_,_,_)),
	assert(taoist(Player,P2,NormTaoMarkerList,P4,P5,P6)),
	broadcastTaoMarkers(Player,NewTaoMarkerList).



/**
@descr change the TaoMarkers of the current Players into the given List
@form changeTaoMarkers(+TaoMarkerList)
*/	
setTaoMarkers(TaoMarkerList):-
	currentPlayer(Player),
	normRes(TaoMarkerList,NormTaoMarkerList),
	retract(taoist(Player,P2,_,P4,P5,P6)),
	assert(taoist(Player,P2,NormTaoMarkerList,P4,P5,P6)),
	broadcastTaoMarkers(Player,TaoMarkerList).

addYY(Player) :-
	retract(Player,Name,Taos,Qi,_,Pos),
	assert(Player,Name,Taos,Qi,1,Pos),
	broadcastYY(Player).

/**
 @descr Returns a list of possible positions to move to, current position inclusive
 @form movePossible(Position,List)
 */
movePossible(1,[1,2,4,5]).
movePossible(2,[1,2,3,4,5,6]).
movePossible(3,[2,3,5,6]).
movePossible(4,[1,2,4,5,7,8]).
movePossible(5,[1,2,3,4,5,6,7,8,9]).
movePossible(6,[2,3,5,6,8,9]).
movePossible(7,[4,5,7,8]).
movePossible(8,[4,5,6,7,8,9]).
movePossible(9,[5,6,8,9]).
