namespace = TUL
character_event = {
	id = TUL.900
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		NOT = {year = 868}
		character = 163115
		NOT = {has_global_flag = tulunid_break}
		
	}
	immediate = {
		random_courtier = {
			limit = {character = 999997}
			ROOT = {
				any_demesne_title = {
					limit = {
						tier = COUNT
						location = {
							duchy = {
								title = d_sinai
							}
						}
					}
					grant_title = PREVPREV
				}
			}
			d_sinai = {
				grant_title = ROOT
				grant_title = PREV
			}
			set_defacto_liege = ROOT
			add_friend = ROOT
		}
		random_vassal = {
			limit = {character = 163138}
			ROOT = {
				any_demesne_title = {
					limit = {
						
						tier = COUNT
						location = {
							duchy = {
								title = d_aswan
							}
						}
						
					}
					grant_title = PREVPREV
				}
			}
			d_aswan = {
				grant_title = PREV
			}
			
		}
		random_independent_ruler = {
			limit = {character = 34014}
			ROOT = {set_defacto_liege = PREV}
			any_courtier = {
				limit = {character = 999995}
				character_event = {id = TUL.902 days = 1}
			}
		}
		
		character_event = {id = TUL.911 days = 150 random = 250}
		set_global_flag = tulunid_break
	}
}



character_event = {
	id = TUL.922
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		NOT = {year = 868}
		character = 999995
		NOT = {has_global_flag = tulunid_attack}
	}
	immediate = {
		
		character_event = {id = TUL.902 days = 1}
		set_global_flag = tulunid_attack
	}
}



character_event = {
	id = TUL.911
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		independent = no
		higher_tier_than = DUKE
	}
	immediate = {
		if = {
			limit = {
				OR = {
					war = yes
					in_revolt = yes
					prisoner = yes
				}
			}
			character_event = {id = TUL.911 days = 30}
			break = yes
		}
		character_event = {id = TUL.901 days = 1}
	}
}


character_event = {
	id = TUL.902
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		character_event = {id = TUL.903 days = 3285} #Attack Tulunids
	}
}

character_event = {
	id = TUL.999
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		NOT = {year = 950}
		has_landed_title = e_arabia
		has_landed_title = k_egypt
		ai = yes
		any_vassal = {
			dynasty = 800003
			prisoner = no
			war = no
			higher_tier_than = BARON
			is_feudal = yes
			ai = yes
		}
	}
	immediate = {
		random_vassal = {
			limit = {
				dynasty = 800003
				prisoner = no
				war = no
				higher_tier_than = BARON
				is_feudal = yes
				ai = yes
			}
			wealth = 100
			prestige = 500
			piety = 500
			save_event_target_as = egypt_viceroy
		}
		k_egypt = {
			grant_title = event_target:egypt_viceroy
			
		}
		if = {
			limit = {
				any_vassal = {
					has_landed_title = d_mamluks
					tier = DUKE
				}
			}
			random_vassal = {
				limit = {
					has_landed_title = d_mamluks
					tier = DUKE
				}
				set_defacto_liege = event_target:egypt_viceroy
			}
		}
		clear_event_target = egypt_viceroy
	}
}

character_event = {
	id = TUL.998
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		NOT = {year = 950}
		FROM = {
			has_landed_title = e_arabia
			has_landed_title = k_egypt
			ai = yes
			any_vassal = {
				dynasty = 800003
				prisoner = no
				higher_tier_than = BARON
				is_feudal = yes
				ai = yes
			}
		}
	}
	immediate = {
		FROM = {
			random_vassal = {
				limit = {
					dynasty = 800003
					prisoner = no
					higher_tier_than = BARON
					is_feudal = yes
					ai = yes
				}
				wealth = 100
				prestige = 500
				piety = 500
				save_event_target_as = egypt_viceroy
			}
		}
		k_egypt = {
			grant_title = event_target:egypt_viceroy
		}
		clear_event_target = egypt_viceroy
	}
}


character_event = {
	id = TUL.903
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		religion_group = muslim
		any_liege = {
			has_landed_title = e_arabia
			religion_group = muslim
		}
		any_independent_ruler = {
			religion_group = muslim
			tier = KING
			has_landed_title = k_egypt
			NOT = {has_landed_title = e_arabia}
		}
	}
	immediate = {
		if = {
			limit = {
				any_liege = {
					has_landed_title = e_arabia
					war = yes
				}
			}
			character_event = {id = TUL.903 days = 1600}
			break = yes
		}
		any_liege = {
			limit = {has_landed_title = e_arabia}
			narrative_event = {id = TUL.1001 days = 1}
		} #Attack Tulunids
	}
}


narrative_event = {
	id = TUL.1001
	title = TUL1001T
	desc = TUL1001
	picture = GFX_evt_emissary
	border = GFX_event_narrative_frame_diplomacy
	
	is_triggered_only = yes
	option = {#Attack Egypt
		ai_chance = {factor = 100}
		hidden_tooltip = {
			if = {
				limit = {NOT = {FROM = {liege = {character = ROOT}}}}
				FROM = {move_character = ROOT}
			}
			
			FROM = {give_minor_title = title_commander}
		}
		add_claim = k_egypt
		k_egypt = {
			holder_scope = {
				reverse_war = {
					target = ROOT
					casus_belli = claim
					thirdparty_title = PREV
					infamy = 0
				}
			}
		}
		capital_scope = {
			ROOT = {
				spawn_unit = {
					province = PREV
					
					owner = ROOT
					leader = THIS
					troops = {
						light_cavalry = { 1402 1402 }
						light_infantry = { 1444 1444 }
						camel_cavalry = { 2502 2502 }
					}
					disband_on_peace = yes
					
				}
								
				spawn_unit = {
					province = PREV
					
					owner = ROOT
					#leader = THIS
					troops = {
						light_cavalry = { 1402 1402 }
						light_infantry = { 1444 1444 }
						camel_cavalry = { 1502 1502 }
					}
					disband_on_peace = yes
					
				}
				
				spawn_unit = {
					province = PREV
					
					owner = ROOT
					#leader = THIS
					troops = {
						light_cavalry = { 1402 1402 }
						light_infantry = { 1444 1444 }
						camel_cavalry = { 1502 1502 }
					}
					disband_on_peace = yes
					
				}
				
				spawn_unit = {
					province = PREV
					
					owner = ROOT
					#leader = THIS
					troops = {
						light_cavalry = { 1402 1402 }
						light_infantry = { 1444 1444 }
						camel_cavalry = { 1502 1502 }
					}
					disband_on_peace = yes
					
				}
				
				spawn_unit = {
					province = PREV
					
					owner = ROOT
					#leader = THIS
					troops = {
						light_cavalry = { 1402 1402 }
						light_infantry = { 1444 1444 }
						camel_cavalry = { 1502 1502 }
					}
					disband_on_peace = yes
					
				}
				spawn_unit = {
					province = PREV
					
					owner = ROOT
					#leader = THIS
					troops = {
						light_cavalry = { 1402 1402 }
						light_infantry = { 1444 1444 }
						camel_cavalry = { 1502 1502 }
					}
					disband_on_peace = yes
					
				}
			}
		}
		name = TUL1001A #Allow
	}
	
	option = {#No
		ai_chance = {factor = 0}
		prestige = -100
		name = TUL1001B #Refuse
	}

}

character_event = {
	id = TUL.901
	title = TUL901T
	desc = TUL901
	picture = GFX_evt_emissary
	border = GFX_event_normal_frame_diplomacy
	
	trigger = {
		higher_tier_than = DUKE
		independent = no
	}
	is_triggered_only = yes
	option = { #Assert Independence
		ai_chance = {factor = 100}
		liege = {narrative_event = {id = TUL.1000 days = 1}}
		name = TUL901A
	}
	option = { #Leave it
		ai_chance = {factor = 0}
		name = TUL901B
	}
}

narrative_event = {
	id = TUL.1000
	title = TUL1000T
	desc = TUL1000
	picture = GFX_evt_emissary
	border = GFX_event_narrative_frame_diplomacy
	trigger = {
		FROM = {
			higher_tier_than = DUKE
			independent = no
		}
	}
	is_triggered_only = yes
	
	option = {
		ai_chance = {factor = 100}
		FROM = {set_defacto_liege = FROM}
		custom_tooltip = {
			TEXT = SYRIA_JERUSALEM_VASSAL
			hidden_tooltip = {
				any_vassal = {
					limit = {
						higher_tier_than = BARON
						primary_title = {
							kingdom = {
								OR = {
									title = k_jerusalem
									title = k_syria
								}
							}
						}
					}
					set_defacto_liege = FROM
				}
			}
		}
		hidden_tooltip = {
			FROM = {letter_event = {id = TUL.1101 days = 1}}
		}
		decadence = -25
		name = TUL1000A #Allow
	}
	
	option = {
		ai_chance = {factor = 0}
		hidden_tooltip = {
			FROM = {letter_event = {id = TUL.1102 days = 1}}
		}
		decadence = 50
		name = TUL1000B #Refuse
	}

}

letter_event = { #yes
	id = TUL.1101
	desc = TUL1101
	is_triggered_only = yes
	option = {
		name = TUL1101A
	}
}

letter_event = { #no
	id = TUL.1102
	desc = TUL1102
	is_triggered_only = yes
	option = {
		name = TUL1102A
	}
}