"S3L1_finish.sce"
{
	INT joan_recno 
	INT num_of_enemy_a_a
	INT num_of_enemy_a_b
	INT num_of_enemy_a_c
	INT num_of_enemy_a_d
	INT num_of_enemy_a_e
	INT num_of_enemy_a_f
	INT num_of_enemy_a_g
	INT num_of_enemy_a_h
	INT num_of_enemy_a_i
	INT num_of_enemy_a_j
	INT num_of_enemy_a_k
	INT num_of_enemy_a_l
	INT gate_door_recno
	INT john_recno
	INT william_recno
	INT orlean_recno
	INT citizen_killed
	INT check_enemy_place_recno
	INT check_enemy_place_recno_2
	INT john_retreat_place_recno
	INT first_wave_place

	init 
	{
		joan_recno = FUNC("search_unit_by_name", "Jeanne d'Arc") 
		orlean_recno = FUNC("search_unit_by_name", "Bastard von Orleans")
		gate_door_recno = FUNC("search_item_by_name", "Castle Gate")
		check_enemy_place_recno = FUNC("search_place_by_name", "Check Enemy Place")
		check_enemy_place_recno_2 = FUNC("search_place_by_name", "Check Enemy Place 2")
		john_retreat_place_recno = FUNC("search_place_by_name", "John Retreat Place")
		first_wave_place = FUNC("search_place_by_name", "First_Wave_Place")

	}

// ********* Main Goal ******
// *** 1 *****
	// save citizens
	if ( FUNC("get_end_condition_finish", 1) == 0 )
	{
		if( FUNC("get_scenario_stage") > 3 && FUNC("number_of_enemy_in_place", check_enemy_place_recno, joan_recno) == 0 )
		{
			FUNC("set_end_condition_finish" , 1, "WIN")	
			FUNC("set_end_condition_enabled", 6, 0)
			FUNC("end_kill_interface")
		}
		if( FUNC("get_scenario_stage") == 5 )
		{
			FUNC("set_end_condition_finish" , 1, "WIN")	
			FUNC("set_end_condition_enabled", 6, 0)
			FUNC("end_kill_interface")
		}
	}

// *** 2 *****
	// protect the Bastard of Orléans
	if ( FUNC("get_end_condition_finish", 2) == 0 )
	{
		if( FUNC("get_scenario_stage") > 5 )
		{
			//WAIT { FUNC("pause_script", 10000) }
			if( FUNC("get_end_condition_finish", 10) == 0 )
			{
				FUNC("set_end_condition_finish" , 10, "WIN")
				FUNC("add_unit_experience", FUNC("get_main_character"), 5000)
				WAIT { FUNC("pause_script", 10000) }
			}
			FUNC("set_end_condition_finish", 2, "WIN")
			FUNC("set_end_condition_finish", 8, "WIN")
		}
	}

// *** 3 ****
	// defeat the William de la Pole

	if ( FUNC("get_end_condition_finish", 3) == 0 )
	{
		if( FUNC("get_scenario_stage") > 4 )
		{
			if( FUNC("get_scenario_count_time") > 10000 )
			{
				william_recno = FUNC("search_unit_by_name", "William de la Pole")
				if( FUNC("get_unit_retreat", william_recno) == -1 )
				{
					FUNC("start_cut_scene_2", 1, 0, william_recno, 200, ( 180 - FUNC("get_unit_dir", william_recno) ), 10, 1)
					FUNC("set_unit_can_process_in_cut_scene", 0, 0)
					FUNC("set_unit_can_process_in_cut_scene", william_recno, 1)

					FUNC("set_idle", william_recno)
					FUNC("stop_curr_task", william_recno)
					ACTION("give_task", william_recno, "IDLE", 1, 1, 0, 0, 0)
					ACTION("stop_all_order", william_recno)
					
					WAIT{ FUNC("pause_script", 1) }
					ACTION("give_task", william_recno, "IDLE", 1, 1, 0, 0, 0)
					ACTION("stop_all_order", william_recno)
					FUNC("set_idle", william_recno)

					WAIT{ FUNC("is_cut_scene_status_black_screen") }
					FUNC("change_camera_in_cut_scene", 0, william_recno, 200, ( 180 - FUNC("get_unit_dir", william_recno) ), 10)

					WAIT{ DIALOG("add_dialog", william_recno, "[0001N]French scum! This time you had the advantage of surprise - but rest assured, I will return to destroy you and your villainous kind!") }

					FUNC("enable_unit_bounding_volume", william_recno, 0)

					WAIT{ ACTION("give_order", william_recno, "APPROACH_PLACE", 1, 1, john_retreat_place_recno, 0, 0, 0, 3000) }

					DIALOG("del_all_dialog") 
					FUNC("end_cut_scene_2", 0, 1)
					WAIT{ FUNC("is_cut_scene_status_black_screen") }
					FUNC("del_unit", william_recno)
					WAIT{ FUNC("is_cut_scene_finished") }

					FUNC("set_end_condition_finish" , 3, "WIN")	
				}

				//if( william_recno == 0 )
				//{
				//	FUNC("attention_call_message_2", "French scum! This time you had the advantage of surprise - but rest assured, I will return to destroy you and your villainous kind!", "William de la Pole")
				//	//FUNC("attention_call_message", "William de la Pole is defeated!")
				//	WAIT{ FUNC("pause_script", 20000) }	
				//	FUNC("set_end_condition_finish" , 3, "WIN")	
				//}

				if( FUNC("get_end_condition_finish", 3) > 0 && FUNC("get_end_condition_finish", 4) > 0 )
				{
					//FUNC("attention_call_message", "Here is the end of the demo. Thanks for playing!")
					FUNC("set_scenario_stage", 6)
				}
			}
		}
	}

// *** 4 ****
	// defeat John de la Pole

	if ( FUNC("get_end_condition_finish", 4) == 0 )
	{
		if( FUNC("get_scenario_stage") > 4 )
		{
			if( FUNC("get_scenario_count_time") > 10000 )
			{
				john_recno = FUNC("search_unit_by_name", "John de la Pole")

				if( FUNC("get_unit_retreat", john_recno) == -1 )
				{
					FUNC("start_cut_scene_2", 1, 0, john_recno, 200, ( 180 - FUNC("get_unit_dir", john_recno) ), 10, 1)
					FUNC("set_unit_can_process_in_cut_scene", 0, 0)
					FUNC("set_unit_can_process_in_cut_scene", john_recno, 1)

					FUNC("set_idle", john_recno)
					FUNC("stop_curr_task", john_recno)
					ACTION("give_task", john_recno, "IDLE", 1, 1, 0, 0, 0)
					ACTION("stop_all_order", john_recno)
					
					WAIT{ FUNC("pause_script", 1) }
					ACTION("give_task", john_recno, "IDLE", 1, 1, 0, 0, 0)
					ACTION("stop_all_order", john_recno)
					FUNC("set_idle", john_recno)

					WAIT{ FUNC("is_cut_scene_status_black_screen") }
					FUNC("change_camera_in_cut_scene", 0, john_recno, 200, ( 180 - FUNC("get_unit_dir", john_recno) ), 10)

					WAIT{ DIALOG("add_dialog", john_recno, "[0002N]Wretched vassals! This time I underestimated you and your worthless pack of hounds, but rest assured I will return, and you shall suffer!") }

					FUNC("enable_unit_bounding_volume", john_recno, 0)

					WAIT{ ACTION("give_order", john_recno, "APPROACH_PLACE", 1, 1, john_retreat_place_recno, 0, 0, 0, 3000) }

					DIALOG("del_all_dialog") 
					FUNC("end_cut_scene_2", 0, 1)
					WAIT{ FUNC("is_cut_scene_status_black_screen") }
					FUNC("del_unit", john_recno)
					WAIT{ FUNC("is_cut_scene_finished") }

					FUNC("set_end_condition_finish" , 4, "WIN")	
				}

				//if( john_recno == 0 )
				//{
				//	FUNC("attention_call_message_2", "Wretched vassals! This time I underestimated you and your worthless pack of hounds, but rest assured I will return, and you shall suffer!", "John de la pole")
				//	//FUNC("attention_call_message", "John de la Pole is defeated!")
				//	WAIT{ FUNC("pause_script", 20000) }	
				//	FUNC("set_end_condition_finish" , 4, "WIN")	
				//}

				if( FUNC("get_end_condition_finish", 3) > 0 && FUNC("get_end_condition_finish", 4) > 0 )
				{
					//FUNC("attention_call_message", "Here is the end of the demo. Thanks for playing!")
					//FUNC("set_victory_display_flag", 1)
					FUNC("set_scenario_stage", 6)
				}
			}
		}
	}

// ********* Side Goal ******
// *** 5 *****
	// kill all the English assault forces in the city

	if ( FUNC("get_end_condition_finish", 5) == 0 )	
	{
		if( FUNC("get_scenario_stage") > 2 )
		{
			//if( FUNC("get_game_time_limit") < 1050000 )
			if( FUNC("get_scenario_count_time") > 30000 )
			{
				if( FUNC("people_by_nation_exist_in_place", first_wave_place, "ENGLISH") == 0 )
				{
					FUNC("attention_call_message", "[0003N]All the English assault forces in the city of Orléans have been defeated!")

					//FUNC("set_scenario_stage", 4)
					//FUNC("set_scenario_count_time")

					WAIT{ FUNC("pause_script", 4000) }	
					FUNC("set_end_condition_finish" , 5, "WIN")	

					//FUNC("attention_call_message", "You can press ENTER key in front of the castle door in order to open or close it!")
				}
			}
		}
	}

// ********* Lose Goal *********
// *** 6 ****
	// more than 17 citizens are killed
	if ( FUNC("get_end_condition_finish", 6) == 0 && FUNC("get_end_condition_enabled", 6) == 1)
	{
		if( FUNC("get_scenario_stage") > 2 && FUNC("get_end_condition_finish", 1) == 0 )
		{
			citizen_killed = FUNC("get_citizen_killed_count")
			//FUNC("display_string", "Citizens killed:", 20, 20 )
			//FUNC("display_number", citizen_killed, 140, 20 )

			if ( FUNC("get_citizen_killed_count") > 17)
			{
				FUNC("call_gameover_msg", "[0004N]You have failed to protect the lives of the innocent citizens of Orléans!")
				WAIT{ FUNC("pause_script", 4000) }	
				FUNC("set_end_condition_finish" , 6, "LOSE")	
			}
		}
	}

// *** 7 ****
	// Joan defeat
	if ( FUNC("get_end_condition_finish", 7) == 0 )
	{
		if ( FUNC("is_unit_dead", joan_recno) == 1)
		{
			FUNC("call_gameover_msg", "[0005N]You have failed to repel the enemy forces and assure the safety of Orléans!")
			WAIT{ FUNC("pause_script", 4000) }	
			FUNC("set_end_condition_finish" , 7, "LOSE")	
		}
	}

// *** 8 ****
	// Orléans defeat
	if ( FUNC("get_end_condition_finish", 8) == 0 )
	{
		if ( FUNC("is_unit_dead", orlean_recno) == 1)
		{
			FUNC("call_gameover_msg", "[0006N]You have failed to protect the life of the Bastard of Orléans!")
			WAIT{ FUNC("pause_script", 4000) }	
			FUNC("set_end_condition_finish" , 8, "LOSE")	
		}
	}

// ********* Main Goal ******
// *** 9 *****
	// meet Orleans
	if ( FUNC("get_end_condition_finish", 9) == 0 )
	{
		if( FUNC("get_scenario_stage") == 2)
		{
			if( FUNC("get_scenario_event_bit_flag", 0) == 1 )
			{
				FUNC("set_end_condition_finish" , 9, "WIN")
				FUNC("set_end_condition_enabled", 13, 1)
			}
		}
	}

// ******** Side Goal ******
// *** 10 ****
	// prevent enemy from coming inside castle
	if( FUNC("get_end_condition_finish", 10) == 0 )
	{
		if( FUNC("get_scenario_stage") == 5 )
		{
			if( FUNC("number_of_enemy_in_place", check_enemy_place_recno, joan_recno) > 0 )
			{
				FUNC("set_end_condition_finish" , 10, "LOSE")
			}
		}
	}

	exit
}
