"S7L1_finish.sce"
{
	INT joan_recno 
	INT jean_recno
	INT alencon_recno

	INT gough_recno
	INT gressart_recno

	INT place_recno_st_pierre
	INT place_recno_st_la_charite

	INT st_pierre_gate_recno
	INT invisible_wall_recno

	INT place_eng_camp
	INT place_recno_english_camp_2_trigger 

	INT place_eng_camp_3
	INT place_eng_camp_4
	INT place_eng_camp_5
	INT place_eng_camp_6
	INT place_eng_camp_7

	INT flag_gough_dead 
	INT flag_martin_dead 
	INT flag_gressart_dead 
	INT flag_collect_food_finished
	INT flag_food_citizen_all_dead 
	INT flag_gold_captured 
	INT flag_gold_returned
	INT flag_100_gold_found
	INT flag_go_on
	INT flag_begley_dead
	INT flag_hudleg_dead

	INT flag_letter_returned 
	INT flag_letter_returned_fail

	INT bool_var

	init 
	{
		joan_recno = FUNC("search_unit_by_name", "Joanna d'Arc")
		jean_recno = FUNC("search_unit_by_name", "Jean de Metz")
		alencon_recno = FUNC("search_unit_by_name", "Ksi¹¿ê Alencon")
		
		gough_recno = FUNC("search_unit_by_name", "Matthew Gough")
		gressart_recno = FUNC("search_unit_by_name", "Perrinet Gressart")
		
		place_recno_st_pierre = FUNC("search_place_by_name", "St Pierre")
		place_recno_st_la_charite = FUNC("search_place_by_name", "St La Charite")
		place_recno_english_camp_2_trigger = FUNC("search_place_by_name", "eng 2 trigger")

		st_pierre_gate_recno = FUNC("search_item_by_name", "StPierre gate")
		invisible_wall_recno = FUNC("search_item_by_name", "inv")

		place_eng_camp = FUNC("search_place_by_name", "eng camp")
		place_eng_camp_3 = FUNC("search_place_by_name", "eng camp 3")
		place_eng_camp_4 = FUNC("search_place_by_name", "eng camp 4")
		place_eng_camp_5 = FUNC("search_place_by_name", "eng camp 5")
		place_eng_camp_6 = FUNC("search_place_by_name", "eng camp 6")
		place_eng_camp_7 = FUNC("search_place_by_name", "eng camp 7")

	}

	flag_gressart_dead = FUNC("get_scenario_event_bit_flag", 3)
	flag_gough_dead = FUNC("get_scenario_event_bit_flag", 21)
	flag_collect_food_finished = FUNC("get_scenario_event_bit_flag_2", 6)
	flag_food_citizen_all_dead = FUNC("get_scenario_event_bit_flag_2", 4)
	flag_gold_captured = FUNC("get_scenario_bit_flag", 1)
	flag_gold_returned = FUNC("get_scenario_event_bit_flag_2", 12 )
	flag_100_gold_found = FUNC("get_scenario_event_bit_flag_2", 13)
	flag_go_on = FUNC("get_scenario_event_bit_flag_2", 16)
	flag_martin_dead =  FUNC("get_scenario_event_bit_flag_2", 21)
	flag_begley_dead = FUNC("get_scenario_event_bit_flag_2", 24)
	flag_hudleg_dead = FUNC("get_scenario_event_bit_flag_2", 25)
	flag_letter_returned = FUNC("get_scenario_event_bit_flag_2", 26) 
	flag_letter_returned_fail = FUNC("get_scenario_event_bit_flag_2", 27) 


	if( FUNC( "is_item_deleted_by_name", st_pierre_gate_recno, "StPierre gate") == 1 )
	{
		st_pierre_gate_recno = 0
	} 

// ********* Main Goal ******
// *** 1 *****

	if ( FUNC("get_end_condition_finish", 1) != 1 )
	{
		if( FUNC("get_place_captured", place_recno_st_pierre) == 1 )
		{
			FUNC("attention_call_message", "[0001N]You have captured Saint-Pierre-le-Moutier!")
			WAIT{ FUNC("pause_script", 4000) }	
			FUNC("set_end_condition_finish" , 1, "WIN")	
		}
	}

//	else
//	{
//		if( FUNC("get_place_captured", place_recno_st_pierre) == 1 )
//		{
//			if( FUNC("fightable_enemy_exist_in_place", place_recno_st_pierre, joan_recno) )
//			{
//				if( st_pierre_gate_recno )
//				{
//					FUNC( "set_item_nation" , st_pierre_gate_recno , "English" )
//				}
//
//				FUNC("attention_call_message", "[0001N]English have captured Saint-Pierre-le-Moutier!")
//				WAIT{ FUNC("pause_script", 4000) }	
//			
//				FUNC( "set_place_captured" , place_recno_st_pierre, 0)
//				FUNC("set_end_condition_finish" , 1, "LOSE")	
//			}
//		}
//	}

// *** 2 ****

	if ( FUNC("get_end_condition_finish", 2) != 1 )
	{
		if( FUNC("get_place_captured", place_recno_st_la_charite) == 1 )
		{
			FUNC("attention_call_message", "[0002N]You have captured La Charité-sur-Loire!")
			WAIT{ FUNC("pause_script", 4000) }	
			FUNC("set_end_condition_finish" , 2, "WIN")	
		}
	}

	//else
	//{
	//	if( FUNC("get_place_captured", place_recno_st_la_charite) == 1 )
	//	{
	//		if( FUNC("fightable_enemy_exist_in_place", place_recno_st_la_charite, joan_recno) )
	//		{
	//			FUNC( "set_place_captured" , place_recno_st_la_charite, 0)
	//			FUNC("attention_call_message", "[0001N]English have captured La Charité-sur-Loire!")
	//			WAIT{ FUNC("pause_script", 4000) }	
	//			FUNC("set_end_condition_finish" , 2, "LOSE")	
	//		}
	//	}
	//}

// *** 3 ***

	if( FUNC ("get_end_condition_enabled", 3 ) )
	{ 
		
		if ( FUNC("get_end_condition_finish", 3) != 1 )
		{
			if( FUNC("get_place_captured", place_eng_camp ) == 1 )
			{
				FUNC("attention_call_message", "[0003N]You have captured Varzy!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 3, "WIN")	
			}
		}

	}
		
// *** 4 ***

	
	if( FUNC ("get_end_condition_enabled", 4 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 4) != 1 )
		{
			if( flag_gough_dead  )
			{
				//FUNC("attention_call_message", "[0001N]You have killed Matthew Gough!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 4, "WIN")	
			}
		}
	}

// *** 5 ***

	if ( FUNC("get_end_condition_finish", 5) != 1 )
	{
		if( flag_gressart_dead  )
		{
			//FUNC("attention_call_message", "[0001N]You have killed Perrinet Gressart!")
			WAIT{ FUNC("pause_script", 4000) }	
			FUNC("set_end_condition_finish" , 5, "WIN")	
		}
	}

// *** 6 ***

	if( FUNC ("get_end_condition_enabled", 6 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 6 ) != 1 )
		{
			if( flag_100_gold_found )
			{
				FUNC("attention_call_message", "[0004N]You have recovered 100 barrels of gold from enemy installations!")
		
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 6, "WIN")	
			}
		}
	}

// *** 7 ***

	if( FUNC ("get_end_condition_enabled", 7 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 7 ) != 1 )
		{
			if ( FUNC("get_citizen_killed_count") >= 15 )
			{
				FUNC("call_gameover_msg", "[0005N]You have failed to protect French Citizens!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 7, "LOSE")	
			}
		}
	}

// *** 8 ***
//side goal

	if( FUNC ("get_end_condition_enabled", 8 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 8 ) == 1 )
		{
			if(  FUNC("get_enemy_bribed_cost") > 0 && ( FUNC("get_enemy_bribed_cost") + FUNC("get_enemy_bribed_cost") ) >= ( FUNC("get_gold_crates_get") * 500 ) )
			{
				FUNC("attention_call_message", "[0006N]You have used more than half of recovered gold for bribes!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 8, "LOSE")	
			}
		}
		else
		{
			if( FUNC("get_enemy_bribed_cost") > 0 && ( FUNC("get_enemy_bribed_cost") + FUNC("get_enemy_bribed_cost") ) < ( FUNC("get_gold_crates_get") * 500 ) )
			{
				FUNC("attention_call_message", "[0007N]You have used less than half of recovered gold for bribes!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 8, "WIN")	
			}
		}
	}
	
// *** 9 ***
// *** 10 ***
// *** 11 ****
// Joan defeat

	if ( FUNC("get_end_condition_finish", 9) != 1 )
	{
		if ( FUNC("is_unit_dead", joan_recno) == 1)
		{
			FUNC("call_gameover_msg", "[0008N]You have failed in your attempt to free northern France from the mercenary threat, having perished in the attempt!")
			WAIT{ FUNC("pause_script", 4000) }	
			FUNC("set_end_condition_finish" , 9, "LOSE")	
		}
	}

	// alencon defeat
	if ( FUNC("get_end_condition_finish", 10) != 1 )
	{
		if ( FUNC("is_unit_dead", alencon_recno) == 1)
		{
			FUNC("call_gameover_msg", "[0009N]Duke Of Alencon is defeated.")
			WAIT{ FUNC("pause_script", 4000) }	
			FUNC("set_end_condition_finish" , 10, "LOSE")	
		}
	}

	// metz defeat
	if ( FUNC("get_end_condition_finish", 11) != 1 )
	{
		if ( FUNC("is_unit_dead", jean_recno) == 1)
		{
			FUNC("call_gameover_msg", "[0010N]Jean de Metz is defeated.")
			WAIT{ FUNC("pause_script", 4000) }	
			FUNC("set_end_condition_finish" , 11, "LOSE")	
		
		}
	}

//**more goal

	if( FUNC ("get_end_condition_enabled", 12 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 12 ) != 1 )
		{
			if( flag_collect_food_finished )
			{
				FUNC("attention_call_message", "[0011N]You helped the villagers recover their food.")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 12, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 13 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 13 ) != 2 )
		{
			if ( flag_food_citizen_all_dead  )
			{
				FUNC("attention_call_message", "[0012N]The villagers have perished - you failed to provide protection as an escort!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 13, "LOSE")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 14 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 14) != 1 )
		{
			if( FUNC("get_place_captured", place_recno_english_camp_2_trigger) == 1 )
			{
				FUNC("attention_call_message", "[0013N]You have captured des champs du nord!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 14, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 15 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 15) != 1 )
		{
			if( flag_gold_captured )
			{
				FUNC("attention_call_message", "[0014N]You have destroyed the merchant cart.")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 15, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 16 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 16) != 1 )
		{
			if( flag_gold_returned )
			{
				//FUNC("attention_call_message", "[0001N]You have returned the gold to the villagers of Saint-Thomas-sur-le-pont!")
				FUNC("change_mission_gold_to_real_gold")
				FUNC("set_minimap_display_gold_enable" , 0)
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 16, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 17 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 17) != 1 )
		{
			if( FUNC("get_citizen_saved") >= 24 )
			{
				FUNC("attention_call_message", "[0015N]You have freed the soldiers held captive in La Charité-sur-Loire!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 17, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 18 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 18) != 1 )
		{
			if( FUNC("get_bribed_enemy_kill_count") >= 20 )
			{
				FUNC("attention_call_message", "[0016N]Mercenaries have killed too many English and Burgundian forces - the English and Burgundians have turned against the mercenaries!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 18, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 19 ) )
	{ 
		
		if ( FUNC("get_end_condition_finish", 19) != 1 )
		{
			if( FUNC("get_place_captured", place_eng_camp_3 ) == 1 )
			{
				FUNC("attention_call_message", "[0017N]You have captured de Decize!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 19, "WIN")	
			}
		}

	}

	if( FUNC ("get_end_condition_enabled", 20 ) )
	{ 
		
		if ( FUNC("get_end_condition_finish", 20) != 1 )
		{
			if( FUNC("get_place_captured", place_eng_camp_4 ) == 1 )
			{
				FUNC("attention_call_message", "[0018N]You have captured du Donzy!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 20, "WIN")	
			}
		}

	}

	
	if( FUNC ("get_end_condition_enabled", 21 ) )
	{ 
		
		if ( FUNC("get_end_condition_finish", 21) != 1 )
		{
			if( FUNC("get_place_captured", place_eng_camp_5 ) == 1 )
			{
				FUNC("attention_call_message", "[0019N]You have captured de Cercy-la-tour!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 21, "WIN")	
			}
		}

	}

	if( FUNC ("get_end_condition_enabled", 22 ) )
	{ 
		
		if ( FUNC("get_end_condition_finish", 22) != 1 )
		{
			if( FUNC("get_place_captured", place_eng_camp_6 ) == 1 )
			{
				FUNC("attention_call_message", "[0020N]You have captured de Pouges-les-Eaux!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 22, "WIN")	
			}
		}

	}

	if( FUNC ("get_end_condition_enabled", 23 ) )
	{ 
		
		if ( FUNC("get_end_condition_finish", 23) != 1 )
		{
			if( FUNC("get_place_captured", place_eng_camp_7 ) == 1 )
			{
				FUNC("attention_call_message", "[0021N]You have captured du Bois!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 23, "WIN")	
			}
		}

	}

	if( FUNC ("get_end_condition_enabled", 24 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 24) != 1 )
		{
			if( flag_martin_dead  )
			{
				//FUNC("attention_call_message", "[0001N]You have killed Matthew Gough!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 24, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 25 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 25) != 1 )
		{
			if( flag_begley_dead  )
			{
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 25, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 26 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 26) != 1 )
		{
			if( flag_hudleg_dead  )
			{
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 26, "WIN")	
			}
		}
	}

	if( FUNC ("get_end_condition_enabled", 27 ) )
	{ 
		if ( FUNC("get_end_condition_finish", 27) != 1 )
		{
			if( flag_letter_returned )
			{
				FUNC("attention_call_message", "[0022N]You have returned the letter and pendant to Patrice!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 27, "WIN")	
			}
		}
	}

	if( flag_go_on == 0 )
	{
		bool_var = 1

		if( FUNC ("get_end_condition_enabled", 1 ) )
		{ 
			if( FUNC("get_end_condition_finish", 1 ) )  
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}

		if( bool_var && FUNC ("get_end_condition_enabled", 3 ) )
		{ 
			if( FUNC("get_end_condition_finish", 3 ) )  
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}

		if( bool_var && FUNC ("get_end_condition_enabled", 24 ) )
		{ 
			if( FUNC("get_end_condition_finish", 24 ) )  
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}
		
		if( bool_var && FUNC ("get_end_condition_enabled", 4 ) )
		{ 
			if( FUNC("get_end_condition_finish", 4 ) )
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}
		
		if( bool_var && FUNC ("get_end_condition_enabled", 6 ) )
		{ 
			if( FUNC("get_end_condition_finish", 6 ) )
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}

		if( bool_var && FUNC ("get_end_condition_enabled", 7 ) )
		{ 
			if( FUNC("get_end_condition_finish", 7 ) ) 
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}
		
		if( bool_var && FUNC ("get_end_condition_enabled", 14 ) )
		{ 
			if( FUNC("get_end_condition_finish", 14 ) )  
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}

		if( bool_var && FUNC ("get_end_condition_enabled", 15 ) )
		{ 
			if( FUNC("get_end_condition_finish", 15 ) )  
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}

		if( bool_var && FUNC ("get_end_condition_enabled", 16 ) )
		{ 
			if( FUNC("get_end_condition_finish", 16 ) )  
			{
				bool_var = 1
			}
			else
			{
				bool_var = 0
			}
		}
		
		if( bool_var )
		{
			FUNC("del_item_by_name" , "inv" )
			FUNC("set_end_condition_enabled", 2, 1) 
			FUNC("set_end_condition_enabled", 5, 1)						
			FUNC("set_scenario_event_bit_flag_2", 16, 1)
			FUNC("attention_call_message_2", "[0023N]We have completed all our main objectives - now all that remains is to capture La Charité and wipe Perrinet Gressart from the map!", alencon_recno )
		}
	}

	exit
}