###########################################
#
# Religious Decisions for Sons of Abraham
#
###########################################
# Written by Henrik Fåhraeus
###########################################

decisions = {
	
	create_fraticelli_papacy = {
		only_playable = yes
		ai_check_interval = 3
		is_high_prio = yes
	
		potential = {
			is_playable = yes
			religion = fraticelli
			NOT = { is_title_active = d_fraticelli }
			has_dlc = "Sons of Abraham"
		}	
		allow = {
			piety = 1000
			completely_controls = c_roma
		}
		effect = {
			activate_title = { title = d_fraticelli status = yes }
			piety = 1000
			religion_authority = {
				modifier = papacy_instituted
				years = 20
			}
			hidden_tooltip = {
				if = {
					limit = {
						any_realm_lord = {
							has_landed_title = b_roma
							religion = ROOT
							is_theocracy = yes
						}
					}
					random_realm_lord = {
						limit = {
							has_landed_title = b_roma
							religion = ROOT
							is_theocracy = yes
						}
						d_fraticelli = { grant_title = PREV }
						set_defacto_liege = ROOT
						pf_liege_change_effect = yes
						wealth = 500
						add_friend = ROOT
					}
				}
				if = {
					limit = {
						d_fraticelli = { has_holder = no }
					}
					create_random_priest = {
						random_traits = yes
						dynasty = random
						religion = ROOT
						culture = ROOT
						age = 40
						health = 7
						trait = shrewd
						trait = zealous
						trait = scholar
						trait = mastermind_theologian
						attributes = {
							learning = 5
							diplomacy = 5
							martial = 2
							stewardship = 5
							intrigue = 5
						}
					}
					new_character = {
						d_fraticelli = { grant_title = PREV }
						b_roma = { grant_title = PREV }
						wealth = 500
						add_friend = ROOT
					}
				}
			}
		}
		
		ai_will_do = {
			factor = 1
		}
	}

	create_iconoclast_patriarch = {
		is_high_prio = yes
		only_playable = yes
		only_independent = yes
		ai_check_interval = 3
		potential = {
			is_playable = yes
			independent = yes
			religion = iconoclast
			NOR = {
				is_title_active = d_iconoclast
				any_demesne_title = { temporary = yes }
			}
		}
		allow = {
			tier = EMPEROR
			war = no
			has_regent = no
			piety = 1000
			prestige = 1000
			NOT = { trait = cynical }
		}
		effect = {
			piety = 1000
			religion_authority = {
				modifier = patriarchy_instituted
				years = 20
			}
			activate_title = { title = d_iconoclast status = yes }
			hidden_tooltip = {
				any_realm_province = {
					if = {
						limit = { religion = iconoclast }
						add_province_modifier = {
							name = appeased_religious
							months = 60
						}
					}
				}
			}
			if = {
				limit = { 
					NOT = { any_realm_title = { title = b_hagiasophia } }
				}
				create_character = {
					random_traits = yes
					dynasty = random
					religion = iconoclast
					culture = ROOT
					age = 39
					trait = shrewd
					trait = zealous
					trait = iconoclast_trait
					trait = mastermind_theologian
					attributes = {
						learning = 5
						diplomacy = 5
						martial = 2
						stewardship = 5
						intrigue = 5
					}
				}
				new_character = {
					d_iconoclast = { grant_title = PREV }
					if = {
						limit = {
							ROOT = {
								any_realm_title = { is_holy_site = PREV }
							}
						}
						ROOT = {
							random_realm_title = {
								limit = { is_holy_site = ROOT }
								gain_title = PREVPREV
							}
						}
					}
					if = {
						limit = {
							ROOT = {
								NOT = { any_realm_title = { is_holy_site = PREV } }
							}
						}
						ROOT = {
							random_realm_title = {
								limit = {
									is_theocracy = yes
									location = { is_capital = yes }
								}
								gain_title = PREVPREV
							}
						}
					}
					wealth = 500
					add_friend = ROOT
					set_defacto_liege = ROOT
				}
			}
			if = {
				limit = { any_realm_title = { title = b_hagiasophia } }
				if = {
					limit = {
						b_hagiasophia = {
							holder_scope = {
								NOT = { has_landed_title = k_orthodox }
							}
						}
					}
					if = {
						limit = {
							b_hagiasophia = {
								holder_scope = {
									religion = ROOT
									is_theocracy = yes
								}
							}
						}
						b_hagiasophia = {
							holder_scope = {
								d_iconoclast = { grant_title = PREV }
								wealth = 500
								add_friend = ROOT
								set_defacto_liege = ROOT
								pf_liege_change_effect = yes
							}
						}
					}
					if = {
						limit = {
							b_hagiasophia = {
								holder_scope = {
									OR = {
										NOT = { religion = ROOT }
										is_theocracy = no
									}
								}
							}
						}
						create_character = {
							random_traits = yes
							dynasty = random
							religion = iconoclast
							culture = ROOT
							age = 39
							trait = shrewd
							trait = zealous
							trait = iconoclast_trait
							trait = mastermind_theologian
							attributes = {
								learning = 5
								diplomacy = 5
								martial = 2
								stewardship = 5
								intrigue = 5
							}
						}
						new_character = {
							d_iconoclast = { grant_title = PREV }
							b_hagiasophia = { grant_title = PREV }
							wealth = 500
							add_friend = ROOT
							set_defacto_liege = ROOT
						}
					}
				}
				if = {
					limit = {
						b_hagiasophia = {
							holder_scope = {
								has_landed_title = k_orthodox
							}
						}
					}
					k_orthodox = {
						holder_scope = {
							d_iconoclast = { grant_title = PREV }
							unsafe_religion = iconoclast  # Use this effect with care
							set_defacto_liege = ROOT
							pf_liege_change_effect = yes
							add_friend = ROOT
						}
						hidden_tooltip = { unsafe_destroy_landed_title = THIS }
					}
					activate_title = { title = k_orthodox status = no }
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	restore_pope_in_rome = {
		is_high_prio = yes
		only_playable = yes
		only_independent = yes
		ai_check_interval = 3
		potential = {
			is_playable = yes
			independent = yes
			has_dlc = "Sons of Abraham"
			
			OR = {
				religion = catholic
				AND = {
					religion = fraticelli
					is_title_active = d_fraticelli
				}
				AND = {
					religion = chalcedonian
					k_papal_chalcedon = { has_holder = yes }
				}
			}
			
			controls_religion = no
			NOT = { rightful_religious_head = ROOT }
			
			c_roma = {
				holder_scope = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
					OR = {
						AND = {
							ROOT = { religion = catholic }
							k_papal_state = {
								holder_scope = {
									NOT = { character = PREVPREV }
									NOT = { is_vassal_or_below = PREVPREV }
								}
							}
						}
						AND = {
							ROOT = { religion = fraticelli }
							d_fraticelli = {
								holder_scope = {
									NOT = { character = PREVPREV }
									NOT = { is_vassal_or_below = PREVPREV }
								}
							}
						}
						AND = {
							ROOT = { religion = chalcedonian }
							k_papal_chalcedon = {
								holder_scope = {
									NOT = { character = PREVPREV }
									NOT = { is_vassal_or_below = PREVPREV }
								}
							}
						}
					}
				}
			}
		}
		
		effect = {		
			if = {
				limit = {
					religion = catholic
				}
				k_papal_state = {
					holder_scope = {
						c_roma = {
							grant_title = PREV
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = { 
										OR = {
											character = ROOT
											is_liege_or_above = ROOT
										}
									}
								}
								grant_title = PREVPREV
							}
						}
						save_event_target_as = restored_pope
					}
				}
			}
			if = {
				limit = {
					religion = catholic
					NOT = { has_character_flag = restored_catholic_pope_in_rome }
				}
				k_papal_state = {
					holder_scope = {
						opinion = {
							who = ROOT
							modifier = opinion_restored_to_rome
						}
					}
				}
				piety = 1000
				set_character_flag = restored_catholic_pope_in_rome
			}
			
			if = {
				limit = {
					religion = fraticelli
				}
				d_fraticelli = {
					holder_scope = {
						c_roma = {
							grant_title = PREV
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = { 
										OR = {
											character = ROOT
											is_liege_or_above = ROOT
										}
									}
								}
								grant_title = PREVPREV
							}
						}
						save_event_target_as = restored_pope
					}
				}
			}
			if = {
				limit = {
					religion = fraticelli
					NOT = { has_character_flag = restored_fraticelli_pope_in_rome }
				}
				d_fraticelli = {
					holder_scope = {
						opinion = {
							who = ROOT
							modifier = opinion_restored_to_rome
						}
					}
				}
				piety = 1000
				set_character_flag = restored_fraticelli_pope_in_rome
			}
			
			if = {
				limit = {
					religion = chalcedonian
				}
				k_papal_chalcedon = {
					holder_scope = {
						c_roma = {
							grant_title = PREV
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = { 
										OR = {
											character = ROOT
											is_liege_or_above = ROOT
										}
									}
								}
								grant_title = PREVPREV
							}
						}
					}
				}
			}
			if = {
				limit = {
					religion = chalcedonian
					NOT = { has_character_flag = restored_chalcedonian_pope_in_rome }
				}
				k_papal_chalcedon = {
					holder_scope = {
						opinion = {
							who = ROOT
							modifier = opinion_restored_to_rome
						}
					}
				}
				piety = 1000
				set_character_flag = restored_chalcedonian_pope_in_rome
			}
		}
		
		
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				NOT = {
					any_demesne_province = {
						NOT = { province_id = 333 }
					}
				}
			}
			modifier = {
				factor = 0
				c_roma = {
					holder_scope = {
						OR = {
							ai = no
							any_liege = { ai = no }  # AI will never take Rome province away from a player
						}
					}
				}
			}
		}
	}
	
	restore_ecumenical_patriarchate = {
		is_high_prio = yes
		only_playable = yes
		only_independent = yes
		ai_check_interval = 3
		potential = {
			is_playable = yes
			religion = orthodox
			independent = yes
			OR = {
				NOT = { is_title_active = k_orthodox }
				k_orthodox = { has_holder = no }
			}
		}
		allow = {
			war = no
			tier = EMPEROR
			completely_controls = c_byzantion
		}
		effect = {
			piety = 250
			if = {
				limit = { NOT = { is_title_active = k_orthodox } }
				activate_title = { title = k_orthodox status = yes }
			}
			hidden_tooltip = {
				if = {
					limit = {
						any_realm_lord = {
							has_landed_title = b_hagiasophia
							religion = ROOT
							is_theocracy = yes
						}
					}
					random_realm_lord = {
						limit = {
							has_landed_title = b_hagiasophia
							religion = ROOT
							is_theocracy = yes
						}
						k_orthodox = { grant_title = PREV }
						set_defacto_liege = ROOT
						pf_liege_change_effect = yes
						add_friend = ROOT
					}
				}
				if = {
					limit = {
						k_orthodox = { has_holder = no }
					}
					create_random_priest = {
						random_traits = yes
						dynasty = random
						religion = ROOT
						culture = ROOT
						age = 40
						health = 7
						trait = shrewd
						trait = zealous
						trait = scholar
						trait = mastermind_theologian
						attributes = {
							learning = 5
							diplomacy = 5
							martial = 2
							stewardship = 5
							intrigue = 5
						}
					}
					new_character = {
						k_orthodox = { grant_title = PREV }
						b_hagiasophia = { grant_title = PREV }
						wealth = 500
						add_friend = ROOT
					}
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}	

	vassalize_paulician_patriarch = {
		ai_check_interval = 3
		only_playable = yes
		
		potential = {
			NOT = { is_inaccessible_trigger = yes }
			religion = paulician
			controls_religion = no
			NOT = { rightful_religious_head = ROOT }
			religion_head = {
				independent = yes
				lower_tier_than = KING
				any_demesne_title = { controls_religion = no }
			}
		}
		allow = {
			war = no
			piety = 500
			tier = EMPEROR
			any_realm_title = { is_holy_site = ROOT }
			religion_head = {
				war = no
				opinion = { who = ROOT value = 0 }
			}
		}
		effect = {
			piety = 500
			religion_head = {
				add_friend = ROOT
				if = {
					limit = {
						ROOT = {
							NOT = { any_realm_title = { title = b_hagiasophia } }
						}
					}
					ROOT = {
						random_realm_title = {
							limit = { is_holy_site = ROOT }
							grant_title = PREVPREV
						}
					}
				}
				if = {
					limit = {
						ROOT = {
							any_realm_title = { title = b_hagiasophia }
						}
					}
					b_hagiasophia = { grant_title = PREV }
				}
				set_defacto_liege = ROOT
				pf_liege_change_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
		}
	}

	become_paulician_patriarch = {
		is_high_prio = yes
		icon_religion = paulician
		only_playable = yes
		ai_check_interval = 3
		potential = {
			is_playable = yes
			religion = paulician
			d_paulician = { has_holder = no }
			NOT = { any_demesne_title = { temporary = yes } }
		}
		allow = {
			war = no
			has_regent = no
			independent = yes
			prestige = 1000
			piety = 1000
			higher_tier_than = DUKE
			any_realm_title = { is_holy_site = ROOT }
		}
		effect = {
			add_trait = paragon
			if = {
				limit = { NOT = { is_title_active = d_paulician } }
				activate_title = { title = d_paulician status = yes }
			}
			d_paulician = { gain_title = ROOT }
			hidden_tooltip = { narrative_event = { id = Plus.2107 } }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	become_messalian_patriarch = {
		is_high_prio = yes
		ai_check_interval = 3
		potential = {
			religion = messalian
			is_theocracy = yes
			has_landed_title = d_pentarch_antioch
			NOT = {
				is_title_active = d_messalian
				any_demesne_title = { temporary = yes }
			}
		}
		allow = {
			war = no
			has_regent = no
		}
		effect = {
			piety = 250
			religion_authority = {
				modifier = patriarchy_instituted
				years = 20
			}
			activate_title = { title = d_messalian status = yes }
			d_messalian = { gain_title = ROOT }
		}
		ai_will_do = {
			factor = 1
		}			
	}

	vassalize_religion_head = {
		is_high_prio = yes
		only_playable = yes
		ai_check_interval = 3
		only_independent = yes
		
		potential = {
			controls_religion = no
			NOT = { rightful_religious_head = ROOT }
			religion_head = {
				is_alive = yes
				independent = yes
				NOT = { any_demesne_title = { controls_religion = no } }
			}
			NOR = {
				religion = chalcedonian
				religion = catholic
				religion = orthodox
				religion = fraticelli
				religion_group = muslim
			}
		}
		allow = {
			war = no
			piety = 500
			higher_tier_than = DUKE
			any_realm_title = {
				tier = BARON
				holding_type = temple
				is_holy_site = ROOT
			}
			religion_head = {
				war = no
				opinion = { who = ROOT value = 0 }
			}
		}
		effect = {
			piety = -500
			prestige = 500
			add_trait = paragon
			religion_head = {
				add_friend = ROOT
				ROOT = {
					if = {
						limit = {
							NOR = {
								AND = {
									religion = jewish
									any_realm_title = { title = b_jerusalem }
								}
								AND = {
									religion = samaritan
									any_realm_title = { title = b_mnttabor }
								}
								AND = {
									religion = bogomilist
									any_realm_title = { title = b_tyrnovo }
								}
								AND = {
									religion = apostolic
									any_realm_title = { title = b_etchmiadzin }
								}
								AND = {
									religion = yazidi
									any_realm_title = { title = b_karamlish }
								}
								AND = {
									OR = {
										religion = miaphysite
										religion = monophysite
									}
									any_realm_title = { title = b_alexandria }
								}
								AND = {
									OR = {
										religion = monothelite
										religion = messalian
										religion = nestorian
									}
									any_realm_title = { title = b_antiocheia }
								}
								AND = {
									OR = {
										religion = nestorian
										religion = manichean
									}
									any_realm_title = { title = b_bagdad }
								}
							}
						}
						random_realm_title = {
							limit = { is_holy_site = ROOT }
							grant_title = PREVPREV
						}
					}
					if = {
						limit = {
							OR = {
								religion = miaphysite
								religion = monophysite
							}
							any_realm_title = { title = b_alexandria }
						}
						b_alexandria = { grant_title = PREVPREV }
					}
					if = {
						limit = {
							OR = {
								religion = monothelite
								religion = messalian
								religion = nestorian
							}
							any_realm_title = { title = b_antiocheia }
						}
						b_antiocheia = { grant_title = PREVPREV }
					}
					if = {
						limit = {
							OR = {
								religion = nestorian
								religion = manichean
							}
							any_realm_title = { title = b_bagdad }
						}
						b_bagdad = { grant_title = PREVPREV }
					}
					if = {
						limit = {
							religion = apostolic
							any_realm_title = { title = b_etchmiadzin }
						}
						b_etchmiadzin = { grant_title = PREVPREV }
					}
					if = {
						limit = {
							religion = bogomilist
							any_realm_title = { title = b_tyrnovo }
						}
						b_jerusalem = { grant_title = PREVPREV }
					}			
					if = {
						limit = {
							religion = jewish
							any_realm_title = { title = b_jerusalem }
						}
						b_jerusalem = { grant_title = PREVPREV }
					}
					if = {
						limit = {
							religion = samaritan
							any_realm_title = { title = b_mnttabor }
						}
						b_mnttabor = { grant_title = PREVPREV }
					}
					if = {
						limit = {
							religion = yazidi
							any_realm_title = { title = b_karamlish }
						}
						b_karamlish = { grant_title = PREVPREV }
					}
				}
				set_defacto_liege = ROOT
				pf_liege_change_effect = yes
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
}

