:- consult(broadcast).
:- consult(cards).
:- consult(cursedice).
:- consult(dice).
:- consult(event).
:- consult(fight).
:- consult(gameover).
:- consult(ghost).
:- consult(player).
:- consult(structure).
:- consult(taoist).
:- consult(village).

main :- 	
	shufflecards(21),
	shufflevillages,
	broadcastUpdate,
	broadcastCurrentPlayer,
	nextPhase.

% Game is over
nextPhase :- 
	gameOver, !.

% Continue
nextPhase :-
	broadcastNoHighlight,
	broadcastClearDice,
	phase(Phase),
	next(Phase).

% Draw a ghost card
next(ghosts) :- taoIsDead, setPhase(buddha), nextPhase, !.
next(ghosts) :- setPhase(drawGhost), phaseDrawGhost.

% Move your taoist if taoist is alive
next(drawGhost) :- taoIsDead, setPhase(buddha), nextPhase, !.
next(drawGhost) :- setPhase(move), phaseMove.

% Fight or use village action if your taoist is alive
next(move) :- taoIsDead, setPhase(buddha), nextPhase, !.
next(move) :- setPhase(decide), phaseDecide.

% Place buddha if you have one
next(decide) :- taoIsDead, setPhase(buddha), nextPhase, !.
next(decide) :- setPhase(buddha), phaseBuddha.

% Change Player and start ghostAction phase for this player
next(buddha) :- nextPlayer, setBuffer(none), setPhase(ghosts), phaseGhosts.


nextPlayer :-
	currentPlayer(ID),
	nextPlayer(ID),
	playerChangeCurrent,
	broadcastCurrentPlayer.

nextPlayer(yellow) :- changePlayer(red).
nextPlayer(red) :- changePlayer(green).
nextPlayer(green) :- changePlayer(blue).
nextPlayer(blue) :- changePlayer(yellow).

changePlayer(ID) :-
	retract(currentPlayer(_)),
	assert(currentPlayer(ID)).


:- dynamic(phase/1).
phase(ghosts).
setPhase(Content) :-
	retractall(phase(_)),
	assert(phase(Content)).

:- dynamic(lockPhase/1).
lockPhase(false).
setLock(Lock) :-
	retractall(lockPhase(_)),
	assert(lockPhase(Lock)).


