###############################
# Birth of Islam
#
# Written by Zusk
#
# zusk_event.51 -> zusk_event.52, zusk_event.54, zusk_islam_event.1 -> zusk_islam_event.8
###############################

namespace = zusk_event
namespace = zusk_islam_event

######################
#
#  Dynamic Prophet
#
#####################

# Dynamic Prophet appears
province_event = {
	id = zusk_event.51
	hide_window = yes
	
	picture = GFX_evt_peasants
	border = GFX_event_normal_frame_war

	trigger = {
		has_game_rule = { name = rise_of_islam value = experimental_historical } # Disabled without rule for now, needs more testing
		year = 590
		NOT = { has_global_flag = bedouin_arabic_arrived }
		province_id = 1665 # Badr
	}
	
	mean_time_to_happen = {
		years = 20
	}
	
	immediate = {
		set_global_flag = bedouin_arabic_arrived

		activate_title = { title = d_islamic status = yes }

		create_character = {
			#Random name
			# occluded = yes # occluded does not work with create_character command
			random_traits = no
			dynasty = 597
			religion = islamic
			culture = bedouin_arabic
			trait = the_prophet
			female = no
			age = 45
			
			attributes = {
				martial = 9
				diplomacy = 9
				intrigue = 7
				stewardship = 9
				learning = 12
			}
			
			trait = "mastermind_theologian"
			trait = "mystic"
			trait = "zealous"
			trait = "patient"
			trait = "gregarious"
			trait = "ambitious"
		}
		
		new_character = {
			set_graphical_culture = muhammad_culture # will set muhammadgfx, which is a workaround to occluded = yes
			wealth = 500

			d_islamic = {
				grant_title = PREV
			}
			set_defacto_liege = THIS

			c_badr = {
				grant_title = PREV
			}

			add_claim = e_arabia
			add_claim = e_persia
			
			character_event = { id = zusk_event.54 }
		}
	}
	option = {
		name = OK
	}
}

# Declaration of war
# ROOT is the Prophet, FROM is the province
character_event = {
	id = zusk_event.54
	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		create_character = {
			random_traits = yes
			dynasty = random
			religion = islamic
			culture = bedouin_arabic
			female = no
			age = 27
			attributes = {
				martial = 6
			}
			trait = skilled_tactician
		}
		new_character = {
			spawn_unit = {
				province = FROM
				home = FROM
				owner = ROOT
				troops =
				{
					horse_archers = { 2700 2700 }
					light_cavalry = { 1600 1600 }
					light_infantry = { 541 541 }
					heavy_infantry = { 210 210 }
					knights = { 47 47 }
				}
				attrition = 0.25
			}
		}
		create_character = {
			random_traits = yes
			dynasty = random
			religion = islamic
			culture = bedouin_arabic
			female = no
			age = 27
			attributes = {
				martial = 6
			}
			trait = skilled_tactician
		}
		new_character = {
			spawn_unit = {
				province = FROM
				home = FROM
				owner = ROOT
				troops =
				{
					horse_archers = { 2700 2700 }
					light_cavalry = { 1600 1600 }
					light_infantry = { 541 541 }
					heavy_infantry = { 210 210 }
					knights = { 47 47 }
				}
				attrition = 0.25
			}
		}
		create_character = {
			random_traits = yes
			dynasty = random
			religion = islamic
			culture = bedouin_arabic
			female = no
			age = 27
			attributes = {
				martial = 6
			}
			trait = skilled_tactician
		}
		new_character = {
			spawn_unit = {
				province = FROM
				home = FROM
				owner = ROOT
				troops =
				{
					horse_archers = { 2700 2700 }
					light_cavalry = { 1600 1600 }
					light_infantry = { 541 541 }
					heavy_infantry = { 210 210 }
					knights = { 47 47 }
				}
				attrition = 0.25
			 }
		}
		create_character = {
			random_traits = yes
			dynasty = random
			religion = islamic
			culture = bedouin_arabic
			female = no
			age = 27
			attributes = {
				martial = 6
			}
			trait = skilled_tactician
		}
		new_character = {
			spawn_unit = {
				province = FROM
				home = FROM
				owner = ROOT
				troops =
				{
					horse_archers = { 2700 2700 }
					light_cavalry = { 1600 1600 }
					light_infantry = { 541 541 }
					heavy_infantry = { 210 210 }
					knights = { 47 47 }
				}
				attrition = 0.25
			 }
		}
		create_character = {
			random_traits = yes
			dynasty = random
			religion = islamic
			culture = bedouin_arabic
			female = no
			age = 27
			attributes = {
				martial = 6
			}
			trait = skilled_tactician
		}
		new_character = {
			spawn_unit = {
				province = FROM
				home = FROM
				owner = ROOT
				troops =
				{
					horse_archers = { 2700 2700 }
					light_cavalry = { 1600 1600 }
					light_infantry = { 541 541 }
					heavy_infantry = { 210 210 }
					knights = { 47 47 }
				}
				attrition = 0.25
			 }
		}
		create_character = {
			random_traits = yes
			dynasty = random
			religion = islamic
			culture = bedouin_arabic
			female = no
			age = 27
			attributes = {
				martial = 6
			}
			trait = skilled_tactician
		}
		new_character = {
			spawn_unit = {
				province = FROM
				home = FROM
				owner = ROOT
				troops =
				{
					horse_archers = { 2700 2700 }
					light_cavalry = { 1600 1600 }
					light_infantry = { 541 541 }
					heavy_infantry = { 210 210 }
					knights = { 47 47 }
				}
				attrition = 0.25
			 }
		}
		create_character = {
			random_traits = yes
			dynasty = random
			religion = islamic
			culture = bedouin_arabic
			female = no
			age = 27
			attributes = {
				martial = 6
			}
			trait = skilled_tactician
		}
		new_character = {
			spawn_unit = {
				province = FROM
				home = ROOT
				owner = THIS
				troops =
				{
					horse_archers = { 2700 2700 }
					light_cavalry = { 1600 1600 }
					light_infantry = { 541 541 }
					heavy_infantry = { 210 210 }
					knights = { 47 47 }
				}
				attrition = 0.25
			 }
		}
		create_character = {
			random_traits = yes
			dynasty = random
			religion = islamic
			culture = bedouin_arabic
			female = no
			age = 27
			attributes = {
				martial = 6
			}
			trait = skilled_tactician
		}
		new_character = {
			spawn_unit = {
				province = FROM
				home = FROM
				owner = ROOT
				troops =
				{
					horse_archers = { 2700 2700 }
					light_cavalry = { 1600 1600 }
					light_infantry = { 541 541 }
					heavy_infantry = { 210 210 }
					knights = { 47 47 }
				}
				attrition = 0.25
			}
		}
		
		# DoW on the province top liege
		719 = { #Mecca
			empire = {
				ROOT = {
					war = {
						casus_belli = unification
						target = PREVPREV # The top liege of the owner
						thirdparty_title = PREV # The de jure empire
					}
				}
			}
		}
	}

	option = {
		name = OK
	}
}

#[on_birth] Remove muhammad graphical_culture for the descendents of the Prophet
character_event = {
	id = zusk_event.52
	
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
		graphical_culture = muhammadgfx
	}
	
	immediate = {
		set_graphical_culture = adnanite
	}
}

######################
#
#	 Birth of the Prophet
#
#####################

# NOTE: event chain is disabled via trigger always = no rather than commenting all the lines
# This allows the Validator to catch syntax errors.

province_event = {
	id = zusk_islam_event.1
	hide_window = yes # Muhammad isn't important until he receives his message from Gabriel

	trigger = {
		has_game_rule = { name = rise_of_islam value = experimental_dynamic } # Disable event chain without rule as it is not finished nor tested
		NOT = { has_global_flag = prophet_born }
		year = 570
		NOT = { year = 600 }
		culture = adnanite
	}
	
	mean_time_to_happen = {
		years = 30
		modifier = {
			factor = 0.75
			province_id = 719 #Mecca
		}
		modifier = {
			factor = 0.75
			religion = semitic_pagan
		}
	}
	
	immediate = {
		set_global_flag = prophet_born

		if = {
			limit = {
				religion_group = pagan_group
				NOT = { religion = semitic_pagan_reformed }
			}
			set_global_flag = islam_pagan_influence
		}

		else_if = {
			limit = { religion = semitic_pagan_reformed }
			set_global_flag = islam_arab_pagan_influence
		}
			
		else_if = {
			limit = { religion_group = christian }
			set_global_flag = islam_christian_influence
		}

		else_if = {
			limit = { religion_group = zoroastrian_group }
			set_global_flag = islam_zoroastrian_influence
		}

		else_if = {
			limit = { religion_group = jewish_group }
			set_global_flag = islam_jewish_influence
		}

		else_if = {
			limit = { religion = buddhist }
			set_global_flag = islam_buddhist_influence
		}

		else_if = {
			limit = { religion = hindu }
			set_global_flag = islam_hindu_influence
		}

		else_if = {
			limit = { religion = jain }
			set_global_flag = islam_jain_influence
		}

		else_if = {
			limit = { religion = bosporan_faith }
			set_global_flag = islam_hypsistarian_influence
		}

		create_character = {
			name = Muhammad
			random_traits = no
			dynasty = 7296
			religion = semitic_pagan
			culture = adnanite
			# occluded = yes # occluded does not work with create_character command
			female = no
			age = 1
		}

		new_character = {
			set_character_flag = the_prophet
			set_graphical_culture = muhammad_culture # will set muhammadgfx, which is a workaround to occluded = yes
		}
	}
}

#######################
##
##	 Prophet receives vision
##
######################

character_event = {
	id = zusk_islam_event.2
	desc = zusk_islam_event.2.desc
	picture = GFX_evt_monk
	border = GFX_event_normal_frame_religion
	
	capable_only = yes
	prisoner = no
	has_character_flag = the_prophet
	min_age = 30
	
	trigger = {
		NOT = { has_global_flag = birth_of_islam }
	}
	
	mean_time_to_happen = {
		years = 5
		modifier = {
			factor = 2
			NOT = { learning = 4 }
		}
		modifier = {
			factor = 1.5
			NOT = { learning = 8 }
		}
		modifier = {
			factor = 0.8
			learning = 10
		}
		modifier = {
			factor = 0.8
			learning = 12
		}
		modifier = {
			factor = 0.8
			learning = 14
		}
		modifier = {
			factor = 0.8
			learning = 16
		}
		modifier = {
			factor = 0.8
			learning = 18
		}
		modifier = {
			factor = 0.8
			learning = 20
		}
	}
	
	immediate = {

		set_global_flag = birth_of_islam

		if = {
			limit = { has_global_flag = islam_pagan_influence }
			# I am leaving the effect for this blank for now.
		}

		else_if = {
			limit = { has_global_flag = islam_arab_pagan_influence }
			set_parent_religion = { religion = islamic parent = semitic_pagan_reformed }
		}

		else_if = {
			limit = { has_global_flag = islam_christian_influence }
			set_parent_religion = { religion = islamic parent = nicene }
		}

		else_if = {
			limit = { has_global_flag = islam_zoroastrian_influence }
			set_parent_religion = { religion = islamic parent = zoroastrian }
		}

		else_if = {
			limit = { has_global_flag = islam_jewish_influence }
			set_parent_religion = { religion = islamic parent = jewish }
		}
		else_if = {
			limit = { has_global_flag = islam_buddhist_influence }
			set_parent_religion = { religion = islamic parent = buddhist }
		}

		else_if = {
			limit = { has_global_flag = islam_hindu_influence }
			set_parent_religion = { religion = islamic parent = hindu }
		}

		else_if = {
			limit = { has_global_flag = islam_jain_influence }
			set_parent_religion = { religion = islamic parent = jain }
		}

		else_if = {
			limit = { has_global_flag = islam_hypsistarian_influence }
			set_parent_religion = { religion = islamic parent = bosporan_faith }
		}
	}
	
	option = {
		name = OK
		religion = islamic
		add_trait = "the_prophet"
	}
}

#######################
##
##	Children gains Sayyid trait from descendant of Prophet
##
######################

character_event = {
	id = zusk_islam_event.3
	hide_window = yes
	
	has_global_flag = birth_of_islam
	
	trigger = {
		OR = {
			mother = { trait = the_prophet }
			father = { trait = the_prophet }
		}
		NOT = { trait = sayyid }
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		add_trait = sayyid
	}
	
	option = {
		name = OK
	}
}

#######################
##
##	 Unlanded Muhammad speaks to ruler for recognition
##
######################

character_event = {
	id = zusk_islam_event.4
	desc = zusk_islam_event.4.desc
	picture = GFX_evt_monk
	border = GFX_event_normal_frame_religion
	
	capable_only = yes
	prisoner = no
	has_character_flag = the_prophet
	has_global_flag = birth_of_islam
	min_age = 30
	
	trigger = {
		NOT = { has_global_flag = prophet_wants_birth_province }
		is_landed = no
	}
	
	mean_time_to_happen = {
		months = 1
	}
	
	immediate = {
		set_global_flag = prophet_wants_birth_province
	}
	
	option = {
		name = OK

		top_liege = {
			character_event = {	id = zusk_islam_event.5	days = 3 tooltip = EVTTOOLTIP66110 }
		}
	}

}

#######################
##
##	 ...For the ruler
##
######################

character_event = {
	id = zusk_islam_event.5
	desc = zusk_islam_event.5.desc
	picture = GFX_evt_monk
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = ZUSK5ISLAMOP1 #Convert to Islam
		ai_chance = {
			factor = 25
		}
			
		religion = islamic
		set_character_flag = can_give_islamic_units
		job_spiritual = {
			opinion = {
				modifier = opinion_fired_from_council
				who = ROOT
				years = 5
			}
			remove_title = job_spiritual
		}
		FROM = {
			give_job_title = job_spiritual
		}
			
	}

	option = {
		name = ZUSK5ISLAMOP2 #Let him have his land
		ai_chance = {
			factor = 50
		}

		FROM = {
			location = {
				county = {
					usurp_title_plus_barony_if_unlanded = PREVPREV
				}
			}
		}
	}

	option = {
		name = ZUSK5ISLAMOP3 #Try to arrest him
		tooltip_info = intrigue
		trigger = {
			OR = {
				intrigue = 12
				ai = no
			}
		}

		ai_chance = {
			factor = 25
		}

		FROM = {
			imprison = yes
		}
	}
}

#######################
##
##	 Muslims first batch of event troops
##
######################

character_event = {
	id = zusk_islam_event.6
	desc = zusk_islam_event.6.desc
	picture = GFX_evt_kaaba
	border = GFX_event_normal_frame_religion
	
	capable_only = yes
	prisoner = no
	
	trigger = {
		OR = {
			AND = {
				has_character_flag = the_prophet
				has_global_flag = birth_of_islam
				is_landed = yes
				independent = yes
			}
			has_character_flag = can_give_islamic_units
		}
		NOT = { has_global_flag = first_islamic_units_given }
	}
	
	mean_time_to_happen = {
		months = 1
	}
	
	immediate = {
		set_global_flag = first_islamic_units_given
	}
	
	option = {
	name = OK
		capital_scope = {
			ROOT = {
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
			}
		}
	}
}

######################
#
# If Muslims get a Emperor title, get a second batch of event troops
#
#####################

character_event = {
	id = zusk_islam_event.7
	desc = zusk_islam_event.7.desc
	picture = GFX_evt_kaaba
	border = GFX_event_normal_frame_religion
	
	capable_only = yes
	prisoner = no
	religion_group = muslim
	
	trigger = {
		tier = EMPEROR
		NOT = { has_global_flag = second_islamic_units_given }
	}
	
	mean_time_to_happen = {
		months = 1
	}
	
	immediate = {
		set_global_flag = second_islamic_units_given
	}
	
	option = {
		name = OK

		add_claim = e_persia
		
		capital_scope = {
			ROOT = {
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
				spawn_unit = {
					province = PREV
					home = PREV
					owner = THIS
					troops = {
						horse_archers = { 2700 2700 }
						light_cavalry = { 1600 1600 }
						light_infantry = { 541 541 }
						heavy_infantry = { 210 210 }
						knights = { 47 47 }
					}
					attrition = 0.25
				}
			}
		}
	}
}

######################
#
# Sunni/Shiite Split
#
#####################

character_event = {
	id = zusk_islam_event.8
	desc = zusk_islam_event.8.desc
	picture = GFX_evt_kaaba
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	trigger = {
		NOT = { has_global_flag = sunni_shiite_split }
		trait = the_prophet
		any_child = {
			is_female = no
			NOT = { any_heir_title = { always = yes } }
		}
		any_child = {
			any_heir_title = { always = yes }
		}
	}

	immediate = {
		# name = EVTOPTA24500
		set_global_flag = sunni_shiite_split
		any_realm_province = {
			limit = {
				religion = islamic
			}
			religion = sunni
		}
		any_child = {
			limit = {
				is_female = no
				NOT = { any_heir_title = { always = yes } }
			}
			religion = shiite
		}
		any_child = {
			limit = { any_heir_title = { always = yes } }
			religion = sunni
		}
		any_child = {
			limit = { religion = islamic }
			religion = sunni
		}
	}
	
	option = {
		name = OK
	}
}
