include "Tools.BSF"


// called at the start of every turn.
// You would tend to use this for events
FUNCTION StartTurn(side)
{
	if (GetTurn() == -1)
		{
			// Setup VPs, damaged tiles, ...
			PreBattleSetup();
			AddLimbers(0);
		}
	else
		{
 

			Log("\n**** Side, Start Turn", side, GetTurn());
		}

}

FUNCTION StartTurnPost(side)
{
	int i;
	int id;
	int team;
	int stage;
	int destinationX;
	int destinationY;
	int distance;
	int trigger;
	int turn;

	SetUniversalVar("TurnCounter", GetUniversalVar("TurnCounter") + 1);

	turn = GetTurn();

  

	if (side == 1)
		{

			// Light foot in woods
			team = 0;
			stage = GetTeamActivationStage(1, team);
			if (stage == 0) // Hold Position
				{
					ClearTeamAllDestinations(1, team);
					MoveTeamCoord(1, team, -1, -1, 64);

					if (PlayerUnitReachedY(29, 0, 1, 1) == 1)
						{
							SetTeamActivationStage(1, team, 1);
							stage = 1;
						}
				}

			if (stage == 1)
				{
					MoveTeamCoord(1, team, -1, -1, 16);
				}

			// Rearguard infantry
			team = 1;
			stage = GetTeamActivationStage(1, team);
			if (stage == 0) // Hold Position
				{
					ClearTeamAllDestinations(1, team);
					MoveTeamCoord(1, team, -1, -1, 64);

					if (PlayerUnitIn(23, 36, 24, 38, 0, 1, 1) == 1)
						{
							SetTeamActivationStage(1, team, 1);
							stage = 1;
						}
				}

			if (stage == 1)
				{
					MoveTeamCoord(1, team, -1, -1, 144);
				}

			if (stage == 1)
				{
					MoveTeamCoord(1, team, -1, -1, 16);
				}
				
			// There is no team 2. (Light Horse and Horse teams combined)

			// Remaining infantry
			team = 3;
			stage = GetTeamActivationStage(1, team);
			if (stage == 0) // Hold Position
				{
					ClearTeamAllDestinations(1, team);
					MoveTeamCoord(1, team, -1, -1, 64);
					
					if ((PlayerUnitIn(20, 38, 27, 39, 0, 0, 1) == 1) || (PlayerUnitReachedY(40, 0, 0, 1) == 1))
						{
							SetTeamActivationStage(1, team, 1);
							stage = 1;
						}
				}

			if (stage == 1)
				{
					MoveTeamCoord(1, team, -1, -1, 144);
				}

			// Cavalry
			team = 4;
			stage = GetTeamActivationStage(1, team);
			if (stage == 0) // Hold Position
				{
					ClearTeamAllDestinations(1, team);
					MoveTeamCoord(1, team, -1, -1, 64);
					
					if ((PlayerUnitIn(20, 38, 27, 39, 0, 0, 1) == 1) || (PlayerUnitReachedY(40, 0, 0, 1) == 1))
						{
							SetTeamActivationStage(1, team, 1);
							stage = 1;
						}
				}

			if (stage == 1)
				{
					MoveTeamCoord(1, team, -1, -1, 400);
				}

			// Central light troops in village
			team = 5;
			stage = GetTeamActivationStage(1, team);
			if (stage == 0) // Hold Position
				{
					ClearTeamAllDestinations(1, team);
					MoveTeamCoord(1, team, -1, -1, 64);
				}

		}
}


FUNCTION PreBattleSetup()
{
	// Set multiplier for unit strength reporting
	SetUniversalVar("StrengthMultiplier", 100);

	// Set counter for StartTurnPost AI routines
	SetUniversalVar("TurnCounter", 0);

	// Customise units for scenarios
	CustomiseUnits();

 

	// Needs to be set if scenario uses deadline
	SetUniversalVar("ScenarioDeadline", 44);
}

FUNCTION VictoryConditions()
{
	StandardVictoryConditions(GetCampaignVar("BreakPoint1"), GetCampaignVar("BreakPoint2"), GetCampaignVar("BreakPoint1"), GetCampaignVar("BreakPoint2"), 1, 0);
	
	return 1; // VictoryConditions() must return a non-zero value so that the main program knows it exists and therefore not to use the default version.
}


// called every game tick.
// You would tend to use this for reactive logic and win situations etc
FUNCTION Tick(side)
{
 
}

//FUNCTION ACHIEVEMENT_PLACEHOLDER()
//{
//}


FUNCTION DrawScenarioUI()
	{
		StandardScenarioUI("IDS_SCENUI_VC_STANDARD", 1);
		
		return 1; // DrawScenarioUI() must return a non-zero value so that the main program knows it exists and therefore not to use the default version.
	}


FUNCTION WIN_SCENARIO_CALLBACK(winner)
{
			if( GetGlobal("victory") == 1 )
				{
						return 0 ;
				}
			else
				{
						return -1 ;
				}
}

 

// Any scenario that needs units to vary from standard definition should have a version of this function - and that includes all where slight random variation in unit size is desired
FUNCTION CustomiseUnits()
{
	int i;
	int id;
	int starting_strength; // starting strength - percent of full base unit strength
	int mode;

	mode = GetDiffMode();

	// Customise Side 0 units
	for (i = 0; i < GetUnitCount(0); i++)
	{
		id = GetUnitID(0,i);
		if (id != -1)
			{
				starting_strength = 100;

				// Adjust starting unit strengths
				RandomiseStartingStrength(id, starting_strength);
			}
	}

	for (i = 0; i < GetUnitCount(1); i++)
	{
		id = GetUnitID(1,i);
		if (id != -1)
			{
				starting_strength = 100;
				RandomiseStartingStrength(id, starting_strength);
			}
	}
}

// Adjust available points according to difficulty.
FUNCTION FORCE_POINTS_CALLBACK(side, points)
{
	int mode;
	int i;
	int id;

	mode = GetDiffMode();

	if (mode == 0)
		{
			return 100;
		}

		if ((mode == 1) || (mode == 2)) // Easy / Very Easy
		{
			return 210;
		}

	if ((mode == 3) || (mode == 4)) // Hard / Very Hard
		{
			return 0;
		}

}

