# check_holy_order_donation_effect
# holy_order_set_law_effect
# teutonic_order_upgrade_capital_effect
# holy_order_distribute_titles_effect

# setup_holy_order_religion_scopes
# abdicate_holy_order_if_religion_changes_effect

check_holy_order_donation_effect = {
	hidden_effect = {
		if = {
			limit = {
				is_title_active = d_knights_hospitaller

				ROOT = {
					religion = d_knights_hospitaller
					is_heretic = no
					NOT = { has_character_flag = refused_to_give_island_to_knights }

					has_alternate_start_parameter = {
						key = religion_names
						value = random
					}
				}

				# Checking if it is an island the Hospitallers could be interested in
				OR = {
					title = d_cyprus

					trigger_if = {
						limit = { title = d_sicily }

						c_malta = {
							holder_scope = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}

					title = c_malta
					title = d_corsica
					title = d_sardinia
					title = d_krete

					trigger_if = {
						limit = { title = d_abydos }

						c_lesbos = {
							holder_scope = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}

					title = c_lesbos

					trigger_if = {
						limit = { title = d_cibyrrhaeot }

						c_rhodos = {
							holder_scope = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}

					title = c_rhodos

					trigger_if = {
						limit = { title = d_samos }

						c_chios = {
							holder_scope = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}

					title = c_chios

					trigger_if = {
						limit = { title = d_aegean_islands }

						c_naxos = {
							holder_scope = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}

					title = c_naxos
					title = d_cephalonia
				}

				# Don't have an appropriate island yet
				d_knights_hospitaller = {
					holder_scope = {
						c_malta = {
							holder_scope = {
								NOR = {
									character = PREVPREV
									is_vassal_or_below_of = PREVPREV
								}
							}
						}

						c_lesbos = {
							holder_scope = {
								NOR = {
									character = PREVPREV
									is_vassal_or_below_of = PREVPREV
								}
							}
						}

						c_rhodos = {
							holder_scope = {
								NOR = {
									character = PREVPREV
									is_vassal_or_below_of = PREVPREV
								}
							}
						}

						c_chios = {
							holder_scope = {
								NOR = {
									character = PREVPREV
									is_vassal_or_below_of = PREVPREV
								}
							}
						}

						c_naxos = {
							holder_scope = {
								NOR = {
									character = PREVPREV
									is_vassal_or_below_of = PREVPREV
								}
							}
						}

						d_sardinia = {
							any_direct_de_jure_vassal_title = {
								holder_scope = {
									NOR = {
										character = PREVPREVPREV
										is_vassal_or_below_of = PREVPREVPREV
									}
								}
							}
						}

						d_cyprus = {
							any_direct_de_jure_vassal_title = {
								holder_scope = {
									NOR = {
										character = PREVPREVPREV
										is_vassal_or_below_of = PREVPREVPREV
									}
								}
							}
						}

						d_corsica = {
							any_direct_de_jure_vassal_title = {
								holder_scope = {
									NOR = {
										character = PREVPREVPREV
										is_vassal_or_below_of = PREVPREVPREV
									}
								}
							}
						}

						d_krete = {
							any_direct_de_jure_vassal_title = {
								holder_scope = {
									NOR = {
										character = PREVPREVPREV
										is_vassal_or_below_of = PREVPREVPREV
									}
								}
							}
						}

						d_cephalonia = {
							any_direct_de_jure_vassal_title = {
								holder_scope = {
									NOR = {
										character = PREVPREVPREV
										is_vassal_or_below_of = PREVPREVPREV
									}
								}
							}
						}
					}
				}
			}

			if = {
				limit = { title = d_abydos }

				c_lesbos = {
					save_event_target_as = potential_title
				}
			}
			else_if = {
				limit = { title = d_cibyrrhaeot }

				c_rhodos = {
					save_event_target_as = potential_title
				}
			}
			else_if = {
				limit = { title = d_sicily }

				c_malta = {
					save_event_target_as = potential_title
				}
			}
			else = {
				save_event_target_as = potential_title
			}

			ROOT = {
				letter_event = {
					id = HFP.44001
					days = 1
				}
			}
		}

		if = { # Checking if it is a Duchy the Teutonics could be interested in
			limit = {
				# Duke tier
				real_tier = DUKE

				# Somewhere in the north
				OR = {
					region = world_europe_north
					region = custom_eastern_baltic
					region = custom_russia
				}

				# Checking that the Teutonics are active
				is_title_active = d_teutonic_order
				year >= 1150
				NOT = { has_global_flag = teutons_asked_for_land }

				d_teutonic_order = {
					holder_scope = {
						religion = ROOT # Checking that Root is the same religion as the Teutonics
						is_landed = no # Checking that the Teutonics don't already have a Duchy

						NOT = { # Checking that the Teutonics aren't already in a Northern Crusade
							any_war = {
								using_cb = northern_crusade_war
							}
						}
					}
				}

				# Checking that ROOT owns the whole Duchy
				ROOT = {
					completely_controls = PREV
					is_heretic = no
					NOT = { has_character_flag = refused_to_give_land_to_teutonics }
				}

				# Checking that ROOT doesn't already own the associated Kingdom of that title
				kingdom = {
					trigger_if = {
						limit = { has_holder = yes }
						NOT = { holder = ROOT }
					}
				}

				OR = {
					# Is by water
					any_direct_de_jure_vassal_title = {
						location = {
							is_coastal = yes
						}

						holder_scope = {
							OR = {
								character = FROM
								is_vassal_or_below_of = FROM
							}
						}
					}

					# Is the neighbor to another Catholic realm
					any_direct_de_jure_vassal_title = {
						location = {
							any_neighbor_province = {
								OR = {
									religion = ROOT

									holder_scope = {
										OR = {
											religion = ROOT

											any_liege = {
												religion = ROOT
											}
										}
									}
								}
							}
						}
					}
				}

				# Is adjacent to infidels
				any_direct_de_jure_vassal_title = {
					location = {
						any_neighbor_province = {
							owner = {
								OR = {
									NOT = { religion_group = ROOT }

									any_liege = {
										NOT = { religion_group = ROOT }
									}
								}
							}
						}
					}
				}
			}

			save_event_target_as = holy_order_potential_title

			ROOT = {
				letter_event = {
					id = HFP.44006
					days = 1
				}
			}
		}
	}
}

holy_order_set_law_effect = {
	add_law = revoke_title_law_2
	add_law = ze_administration_laws_1
	add_law = law_voting_power_0
	add_law = war_voting_power_0
	add_law = revoke_title_voting_power_0
	add_law = imprison_voting_power_0
	add_law = grant_title_voting_power_0
	add_law = banish_voting_power_0
	add_law = execution_voting_power_0
	add_law = succession_voting_power_0
	add_law = centralization_3
}

teutonic_order_upgrade_capital_effect = {
	any_demesne_title = {
		limit = {
			lower_real_tier_than = DUKE
			holding_type = tribal
		}

		convert_to = castle

		if = {
			limit = {
				location = {
					has_city = no
					num_of_empty_holdings >= 2
				}
			}

			location = {
				build_holding = {
					type = city
				}
			}
		}

		if = {
			limit = {
				location = {
					has_temple = no
					num_of_empty_holdings >= 2
				}
			}

			location = {
				build_holding = {
					type = temple
				}
			}
		}
	}
}

holy_order_distribute_titles_effect = {
	any_demesne_title = {
		limit = {
			lower_real_tier_than = DUKE
			NOT = { holding_type = castle }
		}

		if = {
			limit = { holding_type = city }

			create_random_steward = {
				random_traits = yes
				dynasty = random
			}
		}

		else_if = {
			limit = { holding_type = temple }

			create_random_priest = {
				random_traits = yes
				dynasty = random
			}
		}
		else = {
			create_random_soldier = {
				random_traits = yes
				dynasty = random
			}
		}

		grant_title_no_opinion = new_character
	}
}


# Fires on_startup
setup_holy_order_religion_scopes = {
	if = {
		limit = {
			is_save_game = no
			NOT = { has_global_flag = setup_holy_order_religion_scopes }
		}

		set_global_flag = setup_holy_order_religion_scopes

		# Christian
		d_knights_templar = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = catholic
			}
		}

		d_livonian_order = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = catholic
			}
		}

		d_teutonic_order = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = catholic
			}
		}

		k_teutonic_state = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = catholic
			}
		}

		d_knights_hospitaller = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = catholic
			}
		}

		d_knights_santiago = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = catholic
			}
		}

		d_knights_calatrava = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = catholic
			}
		}

		d_holy_sepulchre = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = orthodox
			}
		}

		d_saint_anthony = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = miaphysite
			}
		}

		d_saint_addai = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = nestorian
			}
		}

		# Muslim
		d_fedayeen = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = shiite
			}
		}

		d_bektashi = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = sunni
			}
		}

		d_haruriyyah = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = ibadi
			}
		}

		# Judaic
		d_zealots = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = jewish
			}
		}

		# Mazdan
		d_immortals = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = zoroastrian
			}
		}

		d_army_light = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = manichaean
			}
		}

		# Pagan
		d_jomsvikings = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = norse_pagan
			}
		}

		d_sky_lords = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = tengri_pagan_reformed
			}
		}

		d_spirit_guardians = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = west_african_pagan_reformed
			}
		}

		d_warriors_perun = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = slavic_pagan_reformed
			}
		}

		d_chosen_perkunas = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = baltic_pagan_reformed
			}
		}

		d_sons_kaleva = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = finnish_pagan_reformed
			}
		}

		d_huitzilopochtli = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = aztec_pagan_reformed
			}
		}

		d_dralhas_children = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = bon_pagan_reformed
			}
		}

		d_zun_warriors = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = zun_pagan_reformed
			}
		}

		d_myrmidons = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = hellenic_pagan_reformed
			}
		}

		# Dharmic
		d_followers_arjuna = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = hindu
			}
		}

		d_ashokas_chosen = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = buddhist
			}
		}

		d_bulls_rishabha = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = jain
			}
		}
		
		### ACR ###
		d_fianna = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = celtic_pagan
			}
		}
		d_red_dragons = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = celtic_pagan_reformed
			}
		}
		d_saint_boudicca = {
			save_persistent_event_target = {
				name = britannic_pagan
				scope = catholic
			}
		}
		d_anubis = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = egyptian_pagan
			}
		}
		d_horus = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = egyptian_pagan_reformed
			}
		}
		d_legio_hellenica = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = hellenic_pagan
			}
		}
		d_men_of_steel = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = promethean_pagan
			}
		}
		d_sickles = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = titan_pagan
			}
		}
		d_burning_blades = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = aten_pagan
			}
		}

		log = "Set-up holy order persistent event target religions"
	}
}

abdicate_holy_order_if_religion_changes_effect = {
	if = {
		limit = {
			holy_order = yes
			ai = yes

			any_demesne_title = {
				holy_order = yes

				persistent_event_target:holy_order_religion = {
					NOT = { religion = PREVPREV }
				}
			}
		}

		log = "[This.GetTitledFirstName] was found to be unfit to lead the [This.PrimaryTitle.GetName] due to religious conflict!\nTitle is [This.PrimaryTitle.holy_order_religion.GetName], but leader is [This.Religion.GetName].\n"
		log = "ROOT = [Root.GetTitledFirstName]\nFROM = [From.GetTitledFirstName]\nPREV = [Prev.GetTitledFirstName]"
		abdicate = yes
	}
}

