decisions = {

	hold_a_epic_tournament  = {
		is_high_prio = yes
		
		only_independent = yes
		potential = {
			is_playable = yes
			demesne_size = 1
			independent = yes
			higher_tier_than = DUKE
			NOR = {
				is_nomadic = yes
				religion_group = muslim
				religion = celtic_pagan
				religion = celtic_pagan_reformed
				any_demesne_title = { NOT = { higher_tier_than = count } is_occupied = yes }				
				has_character_modifier = epic_tournament
				has_character_modifier = epic_tournament_furusiyya
			}
			OR = {
				trait = crowned
				trait = crowned_by_pope
				trait = strong_basileus
				trait = weak_basileus
				has_horde_culture = yes
				is_feudal = no
			}
			OR = {
				gender_equality_trigger = yes
				NOT = { is_female = yes	}
			}
			OR = {
				NOT = { has_character_flag = tournament_cooldown }
				had_character_flag = {
					flag = tournament_cooldown
					days = 365
				}
			}
		}
		allow = {
			war = no
			is_ill = no
			has_regent = no
			ruled_years = 5
			NOT = { is_inaccessible_trigger = yes }
			prestige = 500
			wealth = 200
			custom_tooltip = {
				text = IS_NOT_BUSY
				hidden_tooltip = {
					is_not_busy_trigger = yes
					NOT = { has_character_flag = planning_nerge }
					NOT = { has_character_flag = planning_blot }
					NOT = { trait = on_pilgrimage }
					NOT = { is_inaccessible_trigger = yes }
					in_command_trigger = no
				}
			}
			OR = {
				is_female = no
				custom_tooltip = {
					text = FEMALE_CAN_FIGHT_IN_TOURNAMENT
					hidden_tooltip = {
						OR = {
							trait = martial_lady_pagan
							trait = martial_lady
							trait = martial_lady_tengri
						}
					}
				}
				custom_tooltip = {
					text = HAS_CHAMPION_AVAILABLE
					hidden_tooltip = {
						OR = {
							any_vassal = {
								OR = {
									has_minor_title = title_ladys_champion
									has_minor_title = title_champion
								}
								prisoner = no
								NOT = { trait = incapable }
							}
							any_courtier = {
								OR = {
									has_minor_title = title_ladys_champion
									has_minor_title = title_champion
								}
								prisoner = no
								NOT = { trait = incapable }
							}
						}
					}
				}
			}
		}
		effect = {
			add_character_modifier = {
				name = epic_tournament
				years = 100
				hidden = yes
			}
			wealth = -200
			prestige = 100
			character_event = { id = 70033 tooltip = "tournament_invite" }
			hidden_tooltip = {
				add_character_modifier = {
					name = "holding_epic_tournament"
					duration = 210
				}
				add_character_modifier = {
					name = do_not_disturb
					days = 210
					hidden = yes
				}
				character_event = { id = 70001 days = 60 } # Tournament start
				character_event = { id = 70999 days = 210 } # Safety catch flag clearing
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				NOT = { wealth = 300 }
			}
			modifier = {
				factor = 0
				has_character_modifier = ai_austerity
			}
		}
	}
	
	buy_indulgence_for_sins = {
		only_playable = yes
		potential = {
			has_regent = no
			is_heretic = no
			OR = {
				religion = catholic
				AND = {
					religion = chalcedonian
					rightful_religious_head_scope = { trait = latin_rites }
				}
			}
			demesne_size = 1
			NOT = { trait = excommunicated }
			rightful_religious_head_scope = {
				is_alive = yes
				NOT = { character = ROOT }
			}
		}
		allow = {
			prisoner = no
			NOT = { trait = incapable }
			NOT = { is_inaccessible_trigger = yes }
			treasury = 50
			custom_tooltip = {
				text = RECENT_INDULGENCE
				hidden_tooltip = {
					NOT = { has_character_modifier = indulgence_timer }
				}
			}
		}
		effect = {
			treasury = -50
			piety = 25
			if = {
				limit = { any_vassal = { leads_faction = faction_tradition } }
				custom_tooltip = { text = TRADITIONFACTIONMOODPLUS }
				hidden_tooltip = {
					random_vassal = { limit = { leads_faction = faction_tradition } set_variable = { which = "faction_mood_increase" value = 1 } }
					character_event = { id = PlusFaction.200 days = 1 } #recalculate faction moods
				}
			}
			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					modifier = bought_indulgences
					years = 3
				}
			}
			hidden_tooltip = {
				if = {
					limit = { ai = no }
					add_character_modifier = {
						name = indulgence_timer
						duration = 1095
						hidden = yes
					}
				}
				if = {
					limit = { ai = yes }
					add_character_modifier = {
						name = indulgence_timer
						duration = 3650
						hidden = yes
					}
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.1
				always = yes
			}
			modifier = {
				factor = 0
				piety = 0
				OR = {
					trait = cynical
					trait = cruel
					trait = greedy
				}
			}
			modifier = {
				factor = 0
				piety = 100
			}
			modifier = {
				factor = 0
				piety = 0
				NOT = { treasury = 200 }
			}
		}
	}

	issue_declaration_of_repentance = {
		only_playable = yes
		potential = {
			is_playable = yes
			OR = {
				religion = catholic
				religion = chalcedonian
			}
			has_regent = no
			demesne_size = 1
			OR = {
				NOT = { has_character_flag = ask_for_repentance }
				had_character_flag = {
					flag = ask_for_repentance
					days = 1825
				}
			}
			rightful_religious_head_scope = {
				ROOT = {
					excommunicated_for = PREV
				}
			}
		}
		allow = {
			scaled_wealth = 2.0
			is_heretic = no
		}
		effect = {
			set_character_flag = ask_for_repentance
			rightful_religious_head_scope = {
				character_event = { id = 39252 days = 5 random = 12 tooltip = "repentance_ask" }
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	demand_liege_title = {
		only_playable = yes
		is_high_prio = yes
		potential = {
			ai = no
			is_playable = yes
			NOT = { has_character_flag = requested_de_jure_title }
			liege = {
				NOT = { character = ROOT }
				primary_title = { higher_tier_than = DUKE }
				any_demesne_title = {
					tier = DUKE
					has_de_jure_pretension = ROOT
					can_be_given_away = yes
					NOT = {
						holder_scope = {
							any_demesne_province = {
								duchy = { title = PREVPREVPREV }
							}
						}
					}
				}
			}
			has_regent = no
			tier = count
		}
		allow = {
			liege = {
				opinion = {
					who = ROOT
					value = 25
				}
			}
		}
		effect = {
			set_character_flag = requested_de_jure_title
			liege = {
				set_character_flag = wants_duchy_title
				letter_event = { id = 63041 days = 4 tooltip = "EVTTOOLTIP63041" } # "vassal_request_events.txt"
			}
		}
		ai_will_do = {
			factor = 0 # The AI is limited to the similar event 63040
		}
	}
	
	sadaqah_saddka  = {
		only_playable = yes
		potential = {
			is_ruler = yes
			has_regent = no
			demesne_size = 1
			OR = {
				religion = shiite
				religion = sunni
			}
			rightful_religious_head_scope = {
				is_alive = yes
				NOT = { character = ROOT }
			}
		}
		allow = {
			treasury = 50
			custom_tooltip = {
				text = RECENTLY_GAVE_TO_CHARITY
				hidden_tooltip = {
					NOT = { has_character_modifier = charity_timer }
				}
			}
		}
		effect = {
			treasury = -50
			piety = 25
			if = {
				limit = { any_vassal = { leads_faction = faction_tradition } }
				custom_tooltip = { text = TRADITIONFACTIONMOODPLUS }
				hidden_tooltip = {
					random_vassal = { limit = { leads_faction = faction_tradition } set_variable = { which = "faction_mood_increase" value = 1 } }
					character_event = { id = PlusFaction.200 days = 1 } #recalculate faction moods
				}
			}
			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					modifier = opinion_sadaqah
					years = 3
				}
			}
			hidden_tooltip = {
				if = {
					limit = { ai = no }
					add_character_modifier = {
						name = charity_timer
						duration = 1095
						hidden = yes
					}
				}
				if = {
					limit = { ai = yes }
					add_character_modifier = {
						name = charity_timer
						duration = 3650
						hidden = yes
					}
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.1
				always = yes
			}
			modifier = {
				factor = 0
				OR = {
					trait = cynical
					trait = cruel
					trait = greedy
					lower_tier_than = duke
				}
			}
			modifier = {
				factor = 0
				piety = 100
			}
			modifier = {
				factor = 0
				NOT = { treasury = 200 }
			}
			modifier = {
				factor = 0
				has_character_modifier = ai_austerity
			}
		}
	}

	hold_a_furusiyya  = {
		is_high_prio = yes
		
		only_independent = yes
		potential = {
			is_playable = yes
			demesne_size = 1
			higher_tier_than = DUKE
			religion_group = muslim
			NOR = {
				any_demesne_title = { NOT = { higher_tier_than = count } is_occupied = yes }
				has_character_modifier = epic_tournament
				has_character_modifier = epic_tournament_furusiyya
			}
			OR = {
				gender_equality_trigger = yes
				NOT = { is_female = yes	}
			}
			OR = {
				trait = crowned
				trait = crowned_by_pope
				trait = strong_basileus
				trait = weak_basileus
				has_horde_culture = yes
				is_feudal = no
			}
			OR = {
				NOT = { has_character_flag = tournament_cooldown }
				had_character_flag = {
					flag = tournament_cooldown
					days = 365
				}
			}
		}
		allow = {
			war = no
			is_ill = no
			has_regent = no
			ruled_years = 5
			prestige = 500
			wealth = 200
			custom_tooltip = {
				text = IS_NOT_BUSY
				hidden_tooltip = {
					is_not_busy_trigger = yes
					NOT = { has_character_flag = planning_nerge }
					NOT = { trait = on_hajj }
					NOT = { is_inaccessible_trigger = yes }
					in_command_trigger = no
				}
			}
			OR = {
				is_female = no
				custom_tooltip = {
					text = HAS_CHAMPION_AVAILABLE
					hidden_tooltip = {
						OR = {
							any_vassal = {
								OR = {
									has_minor_title = title_ladys_champion
									has_minor_title = title_champion
								}
								prisoner = no
								NOT = { trait = incapable }
							}
							any_courtier = {
								OR = {
									has_minor_title = title_ladys_champion
									has_minor_title = title_champion
								}
								prisoner = no
								NOT = { trait = incapable }
							}
						}
					}
				}
			}
		}
		effect = {
			add_character_modifier = {
				name = epic_tournament_furusiyya
				years = 100
				hidden = yes
			}
			wealth = -200
			prestige = 100
			if = {
				limit = {
					dynasty_head = { character = ROOT }
				}
				piety = 50
			}
			character_event = { id = 88283 tooltip = "tournament_invite" }
			hidden_tooltip = {
				add_character_modifier = {
					name = "holding_epic_tournament"
					duration = 210
				}
				add_character_modifier = {
					name = do_not_disturb
					days = 210
					hidden = yes
				}
				character_event = { id = 70001 days = 60 } # Tournament start
				character_event = { id = 70999 days = 210 } # Safety catch flag clearing
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				NOT = { wealth = 300 }
			}
			modifier = {
				factor = 0
				has_character_modifier = ai_austerity
			}
		}
	}
	
	restore_roman_empire = {
		is_high_prio = yes
		
		only_independent = yes
		potential = {
			is_playable = yes
			has_dlc = "Legacy of Rome"
			OR = {
				has_landed_title = e_byzantium
				has_landed_title = e_latin_empire
				has_landed_title = e_italy
				has_landed_title = e_hre
				has_landed_title = e_karling
			}
			independent = yes
			has_regent = no
			NOT = { has_global_flag = rome_restored }
			OR = {
				religion_group = christian
				religion_group = hellenic_pagan_group
			}
		}
		allow = {
			prestige = 3000
			custom_tooltip = {
				text = restore_roman_empire_allow_tooltip
				hidden_tooltip = {
					completely_controls = d_latium
					completely_controls = d_venice
					completely_controls = d_ferrara
					completely_controls = d_sicily
					completely_controls = d_genoa
					completely_controls = d_capua
					completely_controls = d_apulia
					completely_controls = d_thrace
					completely_controls = d_karvuna
					completely_controls = d_thessalonika
					completely_controls = d_turnovo
					completely_controls = d_antioch
					completely_controls = d_jerusalem
					completely_controls = d_alexandria
					completely_controls = d_athens
					completely_controls = d_tunis
					completely_controls = d_croatia
					completely_controls = d_dalmatia
					completely_controls = d_dioclea
				}
			}
		}
		effect = {
			if = {
				limit = { NOT = { is_title_active = e_roman_empire } }
				hidden_tooltip = {
					activate_title = { title = e_roman_empire status = yes }
				}
			}	
			narrative_event = { id = LoR.20 tooltip = EVTTOOLTIPLoR.20 }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	claim_constantinople = {
		is_high_prio = yes
		
		only_independent = yes
		potential = {
			is_nomadic = no
			is_playable = yes
			has_dlc = "Legacy of Rome"
			independent = yes
			has_landed_title = e_byzantium
			NOT = { has_landed_title = c_byzantion }
			any_realm_lord = { 
				ai = yes
				has_landed_title = c_byzantion
			}
		}
		allow = {
			NOT = { has_landed_title = c_byzantion }
			any_realm_lord = { 
				has_landed_title = c_byzantion 
			}
		}
		effect = {
			ROOT = {
				c_byzantion = {
					usurp_title_plus_barony_if_unlanded_and_vassals = PREV
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	make_rome_capital = {
		is_high_prio = yes
		
		only_independent = yes
		potential = {
			is_playable = yes
			ai = no
			has_dlc = "Legacy of Rome"
			independent = yes
			has_landed_title = e_roman_empire
			capital_scope = {
				NOT = { province_id = 333 }
			}
			any_realm_lord = { 
				ai = yes
				has_landed_title = c_roma 
			}
			any_realm_lord = { 
				ai = yes
				has_landed_title = b_tivoli
			}
		}
		allow = {
			has_landed_title = e_roman_empire
			any_realm_lord = { has_landed_title = c_roma }
		}
		effect = {
			custom_tooltip = {
				text = rome_capital
				hidden_tooltip = {
					b_tivoli = { province_capital = yes }
					ROOT = {
						b_tivoli = {
							usurp_title = PREV
						}
					}
					b_tivoli = { 
						set_name = Rome 
						ROOT = { capital = PREV }
					}
				}
			}
		}
		ai_will_do = {
			factor = 0
		}
	}
	
	petition_doge_for_tp = {
		only_playable = yes
		is_high_prio = yes
		potential = {
			is_playable = yes
			is_merchant_republic = no
			is_patrician = yes
			liege = { is_merchant_republic = yes }
			NOT = { has_character_flag = petition_doge_for_tp_taken }
			OR = {
				liege = {
					any_vassal = {
						is_patrician = yes
						num_of_trade_post_diff = {
							character = ROOT
							value = 10
						}
					}
				}
				AND = {
					NOT = { num_of_trade_posts = 1 }
					liege = {
						any_vassal = {
							is_patrician = yes
							num_of_trade_posts = 10
						}
					}
				}
			}
		}
		allow = {
			liege = {
				opinion = {
					who = ROOT
					value = 25
				}
			}
		}
		effect = {
			set_character_flag = petition_doge_for_tp_taken
			liege = {
				letter_event = { id = REP.500 days = 5 tooltip = EVTTOOLTIP_REP_500 }
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	usurp_tp = {
		only_playable = yes
		is_high_prio = yes
		potential = {
			is_playable = yes
			is_merchant_republic = yes
			is_patrician = yes
			NOT = { has_character_flag = usurp_tp_taken }
			OR = {
				any_vassal = {
					is_patrician = yes
					num_of_trade_post_diff = {
						character = ROOT
						value = 10
					}
				}
				AND = {
					NOT = { num_of_trade_posts = 1 }
					any_vassal = {
						is_patrician = yes
						num_of_trade_posts = 10
					}
				}
			}
		}
		allow = {
			is_merchant_republic = yes
		}
		effect = {
			set_character_flag = usurp_tp_taken
			if = {
				limit = {
					num_of_trade_posts = 1
					any_vassal = {
						is_patrician = yes
						num_of_trade_post_diff = {
							character = ROOT
							value = 10
						}
					}
				}
				random_vassal = {
					limit = {
						is_patrician = yes
						num_of_trade_post_diff = {
							character = ROOT
							value = 10
						}
					}
					set_character_flag = target_patrician_doge
					letter_event = { id = REP.510 days = 5 tooltip = EVTTOOLTIP_REP_510 }
				}
			}
			if = {
				limit = {
					NOT = { num_of_trade_posts = 1 }
					any_vassal = {
						is_patrician = yes
						num_of_trade_posts = 10
					}
				}
				random_vassal = {
					limit = {
						is_patrician = yes
						num_of_trade_posts = 10
					}
					set_character_flag = target_patrician_doge
					letter_event = { id = REP.510 days = 5 tooltip = EVTTOOLTIP_REP_510 }
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	organize_varangian_guard = {
		is_high_prio = yes
		only_independent = yes
		potential = {
			is_playable = yes
			independent = yes
			is_female = no
			prisoner = no
			is_adult = yes
			NOT = { trait = incapable }
			OR = {
				has_landed_title = e_byzantium
				has_landed_title = e_roman_empire
			}
			has_regent = no
			has_global_flag = viking_age_started
			NOT = { has_global_flag = varangian_guard_founded }
			any_playable_ruler = {
				religion = norse_pagan
				capital_scope = {
					distance = {
						where = 496	# Constantinople
						value = 400	# Not too far away
					}
				}
			}
		}
		allow = {
			prestige = 300
			wealth = 300
		}
		effect = {
			set_global_flag = varangian_guard_founded
			prestige = -300
			wealth = -300
			activate_title = { title = d_varangian_guard status = yes }
			hidden_tooltip = {
				narrative_event = { id = TOG.3100 }
				create_character = {
					random_traits = yes
					religion = norse_pagan
					culture = norse
					dynasty = NONE
					female = no
					age = 34
					trait = skilled_tactician
				}
				new_character = {
					d_varangian_guard = {
						grant_title = PREV
					}
					set_defacto_liege = ROOT
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	create_hungary = {
		is_high_prio = yes
		only_independent = yes
		potential = {
			NOT = { has_dlc = "Horse Lords" }
			is_playable = yes
			is_nomadic = no
			independent = yes
			culture = hungarian
			k_hungary = { has_holder = no }
			has_landed_title = k_magyar
			k_carpathia = { is_titular = no }
			NOT = { has_global_flag = hungary_formed }
		}
		allow = {
			war = no
			has_regent = no
			ruled_years = 5
			custom_tooltip = {
				text = CTT_MUST_BE_REFORMED
				OR = {
					is_pagan_religion = no
					is_reformed_pagan_religion = yes
				}
			}
			completely_controls = d_pest
			OR = {
				completely_controls = d_transylvania
				completely_controls = d_ungvar
				completely_controls = d_nyitra
			}
		}
		effect = {
			set_global_flag = hungary_formed
			hidden_tooltip = {
				disband_event_forces = king_magyar_forces
				switch_to_feudal_effect = yes
				k_hungary = {
					reset_coa = THIS
				}
				d_pecs = {
					de_jure_liege = k_hungary
				}
				d_esztergom = {
					de_jure_liege = k_hungary
				}
				d_nyitra = {
					de_jure_liege = k_hungary
				}
				d_ungvar = {
					de_jure_liege = k_hungary
				}
				d_pest = {
					de_jure_liege = k_hungary
				}
				d_transylvania = {
					de_jure_liege = k_hungary
				}
				d_temes = {
					de_jure_liege = k_hungary
				}
				narrative_event = { id = 62910 tooltip = EVTTOOLTIP62910 }
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	become_saoshyant = {
		only_independent = yes
		is_high_prio = yes
		
		potential = {
			has_dlc = "The Old Gods"
			is_playable = yes
			is_adult = yes
			OR = {
				religion = zoroastrian
				religion = mazdaki
				religion = zurvanist
			}
			has_landed_title = e_persia
			NOT = { has_global_flag = saoshyant_appears }
		}
		allow = {
			war = no
			completely_controls = d_tabriz
			completely_controls = d_khorasan
			completely_controls = d_hormuz
			completely_controls = d_fars
			completely_controls = d_khiva
			completely_controls = d_dihistan
			completely_controls = d_merv
			completely_controls = d_sistan
			completely_controls = d_mazandaran
			completely_controls = d_basra
			completely_controls = d_baghdad
			completely_controls = d_mosul
			completely_controls = d_jazira
			#completely_controls = d_mafaza
			completely_controls = d_khozistan
			#completely_controls = d_jibal
			completely_controls = d_kurdistan
			completely_controls = d_khuttal
			completely_controls = d_ferghana
			#completely_controls = d_mudar
			completely_controls = d_samarra
			#completely_controls = d_gilan
			completely_controls = d_herat
			completely_controls = d_balkh
			OR = {
				piety = 2000
				AND = {
					piety = 1500
					OR = {
						trait = elusive_shadow
						trait = grey_eminence
						trait = midas_touched
						trait = brilliant_strategist
						trait = mastermind_theologian
						trait = genius
					}
				}
			}
		}
		effect = {
			set_global_flag = saoshyant_appears
			give_nickname = nick_the_saoshyant
			add_trait = saoshyant
			prestige = 500
			piety = 500	
			hidden_tooltip = {
				narrative_event = { id = TOG.6000 }
				any_child = {
					character_event = { id = TOG.6001 }
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	create_leon = {
		only_playable = yes
		is_high_prio = yes
		
		potential = {
			is_playable = yes
			tier = KING
			has_landed_title = k_asturias
			k_leon = { has_holder = no }
		}

		allow = {
			war = no
			NOT = { trait = incapable }
			primary_title = { title = k_asturias }
			completely_controls = d_leon
			completely_controls = d_asturias
		}
		effect = {
			custom_tooltip = { text = asturias_becomes_leon_tooltip }
			k_asturias = {
				dejure_liege_title = {
					save_event_target_as = old_imperial_liege
				}
				k_leon = {
					hidden_tooltip = {
						activate_title = { title = k_leon status = yes }
						grant_title = ROOT
						make_primary_title = yes 
						copy_title_laws = PREV
						copy_title_history = PREV
						de_jure_liege = event_target:old_imperial_liege # Add Leon to Asturia's imperial liege
					}
				}
				hidden_tooltip = {
					de_jure_liege = e_placeholder # Remove Asturias from the previous de Jure liege
					any_direct_de_jure_vassal_title = {
						de_jure_liege = k_leon
					}
					destroy_landed_title = THIS
				}
			}
			if = {
				limit = {
					has_landed_title = c_leon
					NOT = { capital_scope = { province_id = 190 } }

				}
				c_leon = { ROOT = { capital = PREV } }
			}
			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_great
			}
			
			hidden_tooltip = {
				activate_title = { title = k_asturias status = no }
				character_event = { id = 62920 }
				any_playable_ruler = {
					limit = {
						ai = no
						capital_scope = {
							region = world_europe_west_iberia
						}
					}
					character_event = { id = 62921 }
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	create_castille = {
		is_high_prio = yes
		only_independent = yes
		potential = {
			is_playable = yes
			has_landed_title = d_castilla
			independent = yes
			NOT = { higher_tier_than = DUKE }
			k_castille = {
				has_holder = no
			}
			year = 1000
		}
		allow = {
			war = no
			NOT = { trait = incapable }
			completely_controls = d_castilla
			prestige = 1000
			wealth = 100
		}
		effect = {
			wealth = -100
			prestige = 500
			activate_title = { title = k_castille status = yes }
			k_castille = {
				grant_title = ROOT
				copy_title_laws = d_castilla
			}
			d_castilla = {
				de_jure_liege = k_castille
			}
			if = {
				limit = { completely_controls = d_toledo }
				d_toledo = {
					de_jure_liege = k_castille
				}
			}
			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_great
			}
			hidden_tooltip = {
				any_playable_ruler = {
					limit = {
						ai = no
						capital_scope = {
							region = world_europe_west_iberia
						}
					}
					character_event = { id = 62923 }
				}
			}
			if = {
				limit = {
					has_landed_title = c_burgos
					NOT = { capital_scope = { province_id = 199 } }

				}
				c_burgos = { ROOT = { capital = PREV } }
			}
			hidden_tooltip = { character_event = { id = 62922 } }
		}
		ai_will_do = {
			factor = 1
		}
	}

	create_portugal = {
		is_high_prio = yes
		only_independent = yes
		potential = {
			is_playable = yes
			has_landed_title = d_porto
			independent = yes
			NOT = { higher_tier_than = DUKE }
			k_portugal = {
				has_holder = no
			}
			year = 1000
		}
		allow = {
			war = no
			NOT = { trait = incapable }
			completely_controls = d_porto
			prestige = 1000
			wealth = 100
		}
		effect = {
			wealth = -100
			prestige = 500
			k_portugal = {
				grant_title = ROOT
				copy_title_laws = d_porto
			}
			d_porto = {
				de_jure_liege = k_portugal
			}
			if = {
				limit = { completely_controls = d_beja }
				d_beja = {
					de_jure_liege = k_portugal
				}
			}
			if = {
				limit = {
					has_landed_title = c_porto
					NOT = { capital_scope = { province_id = 158 } }

				}
				c_porto = { ROOT = { capital = PREV } }
			}
			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_great
			}
			hidden_tooltip = {
				any_playable_ruler = {
					limit = {
						ai = no
						capital_scope = {
							region = world_europe_west_iberia
						}
					}
					character_event = { id = 62925 }
				}
			}
			hidden_tooltip = { character_event = { id = 62924 } }
		}
		ai_will_do = {
			factor = 1
		}
	}

	create_aragon = {
		is_high_prio = yes
		only_independent = yes
		potential = {
			is_playable = yes
			has_landed_title = d_aragon
			independent = yes
			NOT = { higher_tier_than = DUKE }
			k_aragon = {
				has_holder = no
			}
			year = 1000
		}
		allow = {
			war = no
			NOT = { trait = incapable }
			completely_controls = d_aragon
			prestige = 1000
			wealth = 100
		}
		effect = {
			wealth = -100
			prestige = 500
			k_aragon = {
				grant_title = ROOT
				copy_title_laws = d_aragon
			}
			d_aragon = {
				de_jure_liege = k_aragon
			}
			if = {
				limit = { completely_controls = d_barcelona }
				d_barcelona = {
					de_jure_liege = k_aragon
				}
			}
			hidden_tooltip = {
				any_playable_ruler = {
					limit = {
						ai = no
						capital_scope = {
							region = world_europe_west_iberia
						}
					}
					character_event = { id = 62927 }
				}
			}
			hidden_tooltip = { character_event = { id = 62926 } }
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	restore_priesthood = {
		is_high_prio = yes
		only_independent = yes
		potential = {
			has_dlc = "The Old Gods"
			is_playable = yes
			religion = zoroastrian
			independent = yes
			NOT = { has_global_flag = zoroastrian_priesthood_founded }
		}
		allow = {
			realm_learning = 25
			piety = 500
			wealth = 500
			war = no
			religion_authority = 0.6
			tier = emperor
			is_heretic = no
			has_regent = no
		}
		effect = {
			set_global_flag = zoroastrian_priesthood_founded
			wealth = -500
			piety = -250
			prestige = 250
			activate_title = { title = d_zoroastrian status = yes }
			hidden_tooltip = {
				random_realm_lord = {
					limit = {
						is_theocracy = yes
						religion = zoroastrian
						OR = {
							any_demesne_title = {
								is_holy_site = ROOT
							}
							NOT = {
								any_realm_lord = {
									is_theocracy = yes
									any_demesne_title = {
										is_holy_site = ROOT
									}
								}
							}
						}
					}
					d_zoroastrian = {
						grant_title = PREV
					}
					narrative_event = { id = TOG.6004 days = 2 }
				}
			}
			d_zoroastrian = {
				holder_scope = {
					ROOT = {
						primary_title = {
							holder_scope = {
								set_defacto_vassal = PREVPREVPREV
							}
						}
					}
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	build_third_temple = {
		only_playable = yes
		is_high_prio = yes
		
		potential = {
			has_dlc = "Sons of Abraham"
			is_playable = yes
			religion = jewish
			774 = {
				NOR = {
					has_province_flag = third_temple_built
					has_province_flag = building_third_temple
				}
			}
		}
		allow = {
			is_heretic = no
			has_regent = no
			higher_tier_than = DUKE
			completely_controls = c_jerusalem
			774 = { religion = jewish }
			wealth = 1500
			piety = 1500
		}
		effect = {
			wealth = -1500
			774 = {
				set_province_flag = building_third_temple
				add_province_modifier = {
					name = third_temple_builder
					duration = 1460
				}
			}
			hidden_tooltip = {
				character_event = { id = SoA.100 }
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	create_israel = {
		only_playable = yes
		is_high_prio = yes
		
		potential = {
			has_dlc = "Sons of Abraham"
			is_playable = yes
			religion_group = jewish_group			
			k_israel = { has_holder = no }
			NOT = { has_global_flag = israel_remade }
		}
		allow = {
			piety = 500
			prestige = 500
			is_heretic = no
			has_regent = no
			independent = yes
			completely_controls = d_jerusalem
			completely_controls = d_ascalon
			completely_controls = d_galilee
			completely_controls = d_oultrejourdain
		}
		effect = {
			set_global_flag = israel_remade
			set_character_flag = centralize_without_penalties
			k_israel = {
				hidden_tooltip = {
					ROOT = {
						primary_title = {
							k_israel = {
								copy_title_laws = PREV
							}
						}
					}
				}
				gain_title = ROOT				
			}
			clr_character_flag = centralize_without_penalties
			hidden_tooltip = {
				any_character = {
					limit = { has_claim = k_jerusalem }
					if = {
						limit = { has_weak_claim = k_jerusalem }
						add_weak_pressed_claim = k_israel
						remove_claim = k_jerusalem
					}
					if = {
						limit = { has_strong_claim = k_jerusalem }
						add_pressed_claim = k_israel
						remove_claim = k_jerusalem
					}
				}
				k_jerusalem = {
					any_direct_de_jure_vassal_title = {
						de_jure_liege = k_israel
					}
					destroy_landed_title = yes 
				}
			}
			narrative_event = { id = SoA.102 tooltip = EVTTOOLTIP_SoA_102 }
			activate_title = { title = d_sons_of_judea status = yes }
			d_sons_of_judea = {
				holder_scope = {
					set_defacto_liege = PREVPREV
					pf_liege_change_effect = yes
				}
			}
			prestige = 1000
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	welcome_jews = {
		only_playable = yes
		potential = {
			has_dlc = "Sons of Abraham"
			is_playable = yes
			primary_title = { higher_tier_than = DUKE }
			NOR = { 
				religion_group = jewish_group 
				has_character_modifier = expelled_jews_timer
				has_character_flag = pogrom
			}
			has_character_modifier = expelled_jewish
		}
		allow = {
			has_character_modifier = expelled_jewish
		}
		effect = {
			prestige = 50
			remove_character_modifier = expelled_jewish
			if = {
				limit = {
					NOT = { trait = sympathy_judaism }
					NOT = { trait = zealous }
				}
				random = {
					chance = 50
					add_trait = sympathy_judaism
				}
			}
			if = {
				limit = { trait = zealous }
				remove_trait = zealous
			}
			custom_tooltip = {
				text = jews_are_welcome
			}
			hidden_tooltip = {
				character_event = { id = SoA.106 }
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 10
				trait = sympathy_judaism
			}
			modifier = {
				factor = 0.1 # Less likely to do it
				always = yes
			}
			modifier = {
				factor = 0
				trait = zealous
			}
		}
	}
	
	expel_jews = {
		only_independent = yes
		
		potential = {
			is_playable = yes
			has_dlc = "Sons of Abraham"
			OR = {
				religion_group = christian
				religion_group = muslim
				religion_group = zoroastrian_group
			}
			NOR = {
				religion_group = jewish_group
				has_character_modifier = expelled_jewish
				has_character_flag = pogrom
				is_nomadic = yes
				secret_religion = jewish
				secret_religion = samaritan
				secret_religion = karaite
			}
			OR = {
				any_demesne_province = { any_province_holding = { has_building = ct_jewish_community_1 } }
				any_realm_lord = {
					is_republic = yes
					any_demesne_province = { any_province_holding = { has_building = ct_jewish_community_1 } }
				}
				has_character_modifier = borrowed_from_jews
				any_courtier = { religion_group = jewish_group }
			}
		}
		allow = {
			has_regent = no
			independent = yes
			NOT = { any_realm_province = { religion = jewish } }
		}
		effect = {
			prestige = -100
			if = {
				limit = { trait = sympathy_judaism }
				remove_trait = sympathy_judaism
			}
			if = {
				limit = { has_character_modifier = borrowed_from_jews }
				remove_character_modifier = borrowed_from_jews
				any_demesne_title = {
					limit = { tier = count }
					location = {
						add_province_modifier = {
							name = offended_moneylenders
							duration = 3650
						}
					}
				}				
				set_character_flag = offended_moneylenders
			}
			if = {
				limit = { has_character_modifier = loan_timer_jews }
				remove_character_modifier = loan_timer_jews
			}			
			add_character_modifier = {
				name = expelled_jewish
				duration = 21900 #in days. 60 years
				inherit = no
			}
			any_friend = {
				limit = { religion_group = jewish_group }
				remove_friend = ROOT
			}
			custom_tooltip = {
				text = jews_are_expelled
			}
			hidden_tooltip = {
				character_event = { id = SoA.105 }
				add_character_modifier = {
					name = expelled_jews_timer
					duration = 21900 #in days. 60 years
					hidden = yes
					inherit = yes
				}
				set_character_flag = pogrom	
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				OR = {
					trait = sympathy_judaism
					trait = kind
					trait = just
					trait = charitable
				}
			}
			modifier = {
				factor = 0
				any_spouse = {
					religion = jewish
				}
			}
			modifier = {
				factor = 0
				any_child = {
					religion = jewish
				}
			}
			modifier = {
				factor = 0
				any_courtier = {
					religion = jewish
					reverse_opinion = { who = ROOT value = 20 }
				}
			}
			modifier = {
				factor = 0
				any_friend = {
					religion_group = jewish_group
				}
			}
			modifier = {
				factor = 0.001 # Less likely to do it
				always = yes
			}
			modifier = {
				factor = 1.2
				trait = zealous
				trait = arbitrary
			}
			modifier = {
				factor = 0
				trait = just
				has_character_modifier = borrowed_from_jews
			}
			modifier = {
				factor = 2
				has_character_modifier = borrowed_from_jews
				trait = greedy
			}
			modifier = {
				factor = 25
				has_character_modifier = ai_austerity
				OR = {
					any_demesne_province = {
						any_province_holding = {
							has_building = ct_jewish_community_1
						}
					}
					any_realm_lord = {
						is_republic = yes
						any_demesne_province = {
							any_province_holding = {
								has_building = ct_jewish_community_1
							}
						}
					}
				}
			}
		}
	}
	
	embrace_mutazila = {
		only_playable = yes
		
		potential = {
			has_dlc = "Sons of Abraham"
			is_playable = yes
			is_adult = yes
			prisoner = no
			OR = {
				religion = sunni
				religion = zikri
				religion = quranist
			}
			NOT = {
				trait = incapable
				trait = mutazilite
				trait = ashari
			}
		}
		
		allow = {
			piety = 50
			learning = 8
		}
		
		effect = {
			character_event = { id = SoA.600 tooltip = EVTTOOLTIP_SoA_600 }
		}
		
		
		ai_will_do = {
			factor = 1
			
			modifier = {
				factor = 0
				trait = zealous
			}
			modifier = {
				factor = 1.2
				trait = cynical
			}
			modifier = {
				factor = 1.2
				liege = { trait = mutazilite }
			}
			modifier = {
				factor = 0.005 # Slow it down
				always = yes
			}
		}
	}
	
	embrace_ashari = {
		only_playable = yes
		
		potential = {	
			has_dlc = "Sons of Abraham"
			is_playable = yes
			is_adult = yes
			prisoner = no
			OR = {
				religion = sunni
				religion = zikri
				religion = quranist
			}
			NOT = {
				trait = incapable
				trait = mutazilite
				trait = ashari
			}
		}
		
		allow = {
			piety = 50
			learning = 8
		}
		
		effect = {
			add_trait = ashari
			character_event = { id = SoA.601 tooltip = EVTTOOLTIP_SoA_601 }
		}
		
		
		ai_will_do = {
			factor = 1
			
			modifier = {
				factor = 0
				trait = cynical
			}
			modifier = {
				factor = 1.2
				trait = zealous
			}
			modifier = {
				factor = 1.2
				liege = { trait = ashari }
			}
			modifier = {
				factor = 0.005 # Slow it down
				always = yes
			}
		}
	}
	
	become_samrat_chakravartin = {
		only_independent = yes
		
		is_high_prio = yes
		potential = {
			has_dlc = "Rajas of India"
			is_playable = yes
			is_feudal = yes
			religion_group = indian_group	
			capital_scope = {
				region = world_india
			}
			primary_title = {
				higher_tier_than = DUKE
			}
			e_india = {
				has_holder = no
			}
		}
		allow = {
			independent = yes
			is_adult = yes
			prisoner = no
			NOT = {	trait = incapable }
			war = no
			prestige = 1000
			wealth = 500
			OR = {
				has_landed_title = e_rajastan
				e_rajastan = { has_holder = no }
			}
			OR = {
				has_landed_title = e_bengal
				e_bengal = { has_holder = no }
			}
			OR = {
				has_landed_title = e_deccan
				e_deccan = { has_holder = no }
			}
			completely_controls_region = world_india
		}
		effect = {
			wealth = -500
			prestige = 500
			activate_title = { title = e_india status = yes }
			hidden_tooltip = {
				any_title = {
					limit = {
						tier = KING
						region = world_india
					}
					de_jure_liege = e_india
				}
			}
			primary_title = {
				e_india = {
					grant_title = ROOT
					copy_title_laws = PREV
					copy_title_history = PREV
				}
			}
			hidden_tooltip = { e_india = { make_primary_title = yes } }
			if = {
				limit = {
					OR = {
						has_landed_title = e_rajastan
						e_rajastan = { has_holder = no }
					}
				}
				e_rajastan = { destroy_landed_title = THIS }
				activate_title = { title = e_rajastan status = no }
			}
			if = {
				limit = {
					OR = {
						has_landed_title = e_bengal
						e_bengal = { has_holder = no }
					}
				}
				e_bengal = { destroy_landed_title = THIS }
				activate_title = { title = e_bengal status = no }
			}
			if = {
				limit = {
					OR = {
						has_landed_title = e_deccan
						e_deccan = { has_holder = no }
					}
				}
				e_deccan = { destroy_landed_title = THIS }
				activate_title = { title = e_deccan status = no }
			}
			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_magnificent
			}
			chronicle = {
				entry = CHRONICLE_UNITED_INDIA
				picture = GFX_evt_throne_room
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	form_the_hre = {
		only_independent = yes
		is_high_prio = yes
		
		potential = {
			is_playable = yes
			OR = {
				religion = chalcedonian
				religion = catholic
				religion = fraticelli
			}
			is_heretic = no
			e_hre = { has_holder = no }
			e_france = { has_holder = no }
			e_karling = { has_holder = no }
			e_roman_empire = { has_holder = no }
			NOT = { tier = EMPEROR }
			e_hre = { has_title_flag = hre_formed }
		}
		allow = {
			has_regent = no
			prestige = 1000
			realm_size = 180
			has_landed_title = k_germany
			any_demesne_title = {
				tier = KING
				empire = { title = e_hre }
				NOT = { title = k_germany }
			}
			scaled_wealth = 1
		}
		effect = {
			scaled_wealth = -1
			give_nickname = nick_the_great
			primary_title = {
				e_hre = {
					grant_title = ROOT
					copy_title_laws = PREV
					add_law = succ_feudal_elective
				}
			}
			any_demesne_title = {
				limit = {
					tier = KING
					NOT = { title = k_germany }
				}
				destroy_landed_title = THIS
			}
		}
		ai_will_do = {
			factor = 1
		}
	}

	form_carolingian_empire = {
		is_high_prio = yes
		
		only_independent = yes
		potential = {
			is_playable = yes
			independent = yes
			dynasty = 25061
			OR = {
				religion = catholic
				religion = chalcedonian
			}
			is_heretic = no
			OR = {
				NOT = { tier = EMPEROR }
				has_landed_title = e_france
			}
			OR = {
				e_france = { has_holder = no }
				has_landed_title = e_france
			}
			e_roman_empire = { has_holder = no }
			rightful_religious_head_scope = {
				OR = {
					controls_religion = yes
					has_landed_title = d_pentarch_rome
				}
			}
			NOR = {
				e_karling = { has_title_flag = carolingian_empire_formed }
				e_hre = { has_title_flag = hre_formed }
			}
		}
		allow = {
			war = no
			has_regent = no
			piety = 100
			custom_tooltip = {
				text = IS_NOT_BUSY
				hidden_tooltip = {
					is_not_busy_trigger = yes
					NOT = { trait = on_pilgrimage }
					NOT = { is_inaccessible_trigger = yes }
				}
			}
			custom_tooltip = {
				text = CONTROLS_100_PROVINCES
				hidden_tooltip = {
					any_realm_title = {
						tier = count
						count = 100
					}
				}
			}
			custom_tooltip = {
				text = POPE_WOULD_HEAR_REQUEST
				hidden_tooltip = {
					NOT = { trait = excommunicated }
					OR = {
						ai = yes
						rightful_religious_head_scope = {
							opinion = { who = ROOT value = 0 }
						}
					}
				}
			}
			custom_tooltip = {
				text = CONTROLS_NEUSTRIA
				hidden_tooltip = {
					OR = {
						has_landed_title = k_france
						any_vassal = { has_landed_title = k_france }
					}
				}
			}
			custom_tooltip = {
				text = CONTROLS_LOMBARDY
				hidden_tooltip = {
					OR = {
						has_landed_title = k_italy
						has_landed_title = k_lombardy
						any_vassal = { has_landed_title = k_italy }
						any_vassal = { has_landed_title = k_lombardy }
					}
				}
			}
			custom_tooltip = {
				text = CONTROLS_AUSTRASIA
				hidden_tooltip = {
					OR = {
						has_landed_title = k_lotharingia
						any_vassal = { has_landed_title = k_lotharingia }
					}
				}
			}
			OR = {
				custom_tooltip = {
					text = CONTROLS_BAVARIA
					hidden_tooltip = {
						OR = {
							has_landed_title = k_bavaria
							any_vassal = { has_landed_title = k_bavaria }
						}
					}
				}
				custom_tooltip = {
					text = CONTROLS_BAVARIA2
					hidden_tooltip = {
						completely_controls = d_bavaria
						k_bavaria = { has_holder = no }
					}
				}
			}
			OR = {
				has_landed_title = e_france
				scaled_wealth = 1
			}
		}
		effect = {
			e_karling = { set_title_flag = carolingian_empire_formed }
			if = {
				limit = { lower_tier_than = EMPEROR }
				scaled_wealth = -1
			}
			prestige = 1000
			add_trait = crowned_by_pope
			give_nickname = nick_the_great
			hidden_tooltip = {
				activate_title = { title = e_karling status = yes }
				activate_title = { title = k_germany status = yes }
				any_demesne_title = {
					limit = { tier = KING }
					de_jure_liege = e_karling
				}
				e_france = {
					any_direct_de_jure_vassal_title = {
						de_jure_liege = e_karling
					}
				}
			}
			primary_title = {
				k_germany = {
					grant_title = ROOT
					copy_title_laws = PREV
				}
				e_karling = {
					grant_title = ROOT
					copy_title_laws = PREV
					add_law = succ_elective_gavelkind
				}
			}
			any_demesne_title = {
				limit = {
					tier = EMPEROR
					NOT = { title = e_karling }
				}
				destroy_landed_title = THIS
			}
			if = {
				limit = { has_landed_title = k_saxony }
				k_saxony = { destroy_landed_title = yes }
			}
			if = {
				limit = { has_landed_title = k_bavaria }
				k_bavaria = { destroy_landed_title = yes }
			}
			if = {
				limit = {
					any_vassal = { has_landed_title = k_saxony }
				}
				random_vassal = {
					limit = { has_landed_title = k_saxony }
					any_vassal = {
						limit = { tier = DUKE }
						set_defacto_liege = ROOT
						pf_liege_change_effect = yes
					}
					k_saxony = { destroy_landed_title = yes }
					set_defacto_liege = ROOT
					pf_liege_change_effect = yes
					opinion = { who = ROOT modifier = opinion_revoked_my_title months = 120 }
				}
			}
			if = {
				limit = {
					any_vassal = { has_landed_title = k_bavaria }
				}
				random_vassal = {
					limit = { has_landed_title = k_bavaria }
					any_vassal = {
						limit = { tier = DUKE }
						set_defacto_liege = ROOT
						pf_liege_change_effect = yes
					}
					k_bavaria = { destroy_landed_title = yes }
					set_defacto_liege = ROOT
					pf_liege_change_effect = yes
					opinion = { who = ROOT modifier = opinion_revoked_my_title months = 120 }
				}
			}
			if = {
				limit = { rightful_religious_head_scope = { NOT = { is_friend = ROOT } } }
				rightful_religious_head_scope = { add_friend = ROOT }
			}
			# Rename/Redistribute kingdoms
			hidden_tooltip = {
				k_france = {
					set_name="WEST_FRANCIA"
					adjective="WEST_FRANCIA_ADJ"
				}
				d_normandy = {
					set_name=""
					adjective=""
				}
				k_lotharingia = {
					set_name="MIDDLE_FRANCIA"
					adjective="MIDDLE_FRANCIA_ADJ"
				}
				d_swabia = { de_jure_liege = k_germany }
				d_baden = { de_jure_liege = k_germany }
				d_swiss = { de_jure_liege = k_germany }
				d_franconia = { de_jure_liege = k_germany }
				d_koln = { de_jure_liege = k_germany }
				if = {
					limit = { k_saxony = { has_holder = no } }
					if = {
						limit = { d_eastphalia = { de_jure_liege = k_saxony } }
						d_eastphalia = { de_jure_liege = k_germany }
					}
					if = {
						limit = { d_westphalia = { de_jure_liege = k_saxony } }
						d_westphalia = { de_jure_liege = k_germany }
					}
					if = {
						limit = { d_thuringia = { de_jure_liege = k_saxony } }
						d_thuringia = { de_jure_liege = k_germany }
					}
				}
				if = {
					limit = { d_bavaria = { de_jure_liege = k_bavaria } }
					d_bavaria = { de_jure_liege = k_germany }
				}
				if = {
					limit = { d_osterreich = { de_jure_liege = k_bavaria } }
					d_osterreich = { de_jure_liege = k_germany }
				}
				if = {
					limit = { d_tyrol = { de_jure_liege = k_bavaria } }
					d_tyrol = { de_jure_liege = k_germany }
				}
				if = {
					limit = { d_carinthia = { de_jure_liege = k_bavaria } }
					d_carinthia = { de_jure_liege = k_germany }
				}
				if = {
					limit = { d_champagne = { de_jure_liege = k_lotharingia } }
					d_champagne = { de_jure_liege = k_france }
				}
				if = {
					limit = { d_burgundy = { de_jure_liege = k_lotharingia } }
					d_burgundy = { de_jure_liege = k_france }
				}
				k_germany = { de_jure_liege = e_karling }
			}
			# Give lands/vassals to the Pope
			any_realm_lord = {
				limit = {
					any_demesne_title = {
						OR = {
							title = c_roma
							title = c_ravenna
							title = c_ferrara
							title = c_urbino
							title = c_spoleto
							title = c_orvieto
						}
					}
				}
				hidden_tooltip = { character_event = { id = Plus.1164 } }
			}
			any_demesne_title = {
				limit = {
					location = { is_capital = no }
					OR = {
						title = c_roma
						title = c_ravenna
						title = c_ferrara
						title = c_urbino
						title = c_spoleto
						title = c_orvieto
					}
				}
				ROOT = { rightful_religious_head_scope = { grant_title = PREVPREV } }
				hidden_tooltip = { remove_claim = ROOT }
			}
			#give aachen to emperor
			c_julich = {
				if = {
					limit = {
						owner = {
							ai = yes
							same_realm = ROOT
						}
					}
					grant_title_no_opinion = ROOT
					ROOT = {
						if = {
							limit = {
								ai = yes
							}
							capital = PREV
						}
					}
				}
			}
			hidden_tooltip = {
				# Byzantine Emperor will be very upset
				if = {
					limit = {
						e_byzantium = { has_holder = yes }
						NOT = { k_papal_state = { has_title_flag = schism_happened } }
						k_chalcedonian = { has_holder = no }
					}
					496 = { change_variable = { which = "schism_tracker" value = 1 } }
					rightful_religious_head_scope = {
						save_event_target_as = current_roman_pope
						e_byzantium = {
							holder_scope = {
								save_event_target_as = current_byzantine_emperor
								opinion = { who = ROOT modifier = opinion_unhappy months = 1200 }
								opinion = { who = PREVPREV modifier = opinion_unhappy multiplier = 2 months = 1200 }
								rightful_religious_head_scope = {
									opinion = { who = ROOT modifier = opinion_unhappy months = 1200 }
									opinion = { who = PREVPREVPREV modifier = opinion_unhappy multiplier = 2 months = 1200 }
								}
							}
						}
					}
					narrative_event = { id = Plus.2278 days = 3 }
				}
				if = {
					limit = { trait = exalted4 }
					any_vassal = {
						opinion = {
							modifier = opinion_exalted
							who = ROOT
							multiplier = 2
							years = 10
						}
					}
				}
				if = {
					limit = { NOT = { trait = exalted4 } }
					add_trait = exalted4
				}
				if = {
					limit = { ai = yes }
					add_trait = zealous
				}
			}
			chronicle = {
				entry = CHRONICLE_FOUNDED_CAROLINGIAN
				picture = GFX_evt_coronation
			}
			if = {
				limit = {
					has_character_flag = is_charlemagne
					NOT = { has_character_flag = charlemagne_coronation }
				}
				hidden_tooltip = { narrative_event = { id = CM.1500 } } # Charlemagne coronation
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				NOR = {
					has_landed_title = k_saxony
					any_vassal = { has_landed_title = k_saxony }
					k_saxony = { has_holder = no }
					AND = {
						k_saxony = { holder_scope = { NOT = { religion = ROOT } } }
						NOT = { has_strong_claim = k_saxony }
						has_global_flag = saxon_wars
					}
				}
			}
		}
	}
		
	form_new_kingdom = {
		only_independent = yes
		is_high_prio = yes
		
		only_playable = yes
		potential = {
			has_dlc = "Charlemagne"
			is_playable = yes
			real_tier = DUKE
			NOT = { real_tier = KING }
			independent = yes
			primary_title = { is_primary_type_title = no }
			
			holy_order = no
			
			NOT = {
				has_game_rule = {
					name = dynamic_realms
					value = off
				}
			}
			
		}
		allow = {
			war = no
			has_regent = no
			piety = 0
			prestige = 1000
			wealth = 500
			capital_scope = { TECH_MAJESTY = 1 }
			custom_tooltip = {
				text = HAS_TWO_DUKE_TITLES
				hidden_tooltip = {
					any_demesne_title = {
						tier = DUKE
						is_titular = no
						count = 2
					}
				}
			}
			custom_tooltip = {
				text = ALLOWED_CUSTOM_KINGDOM
				hidden_tooltip = { allowed_custom_titles_trigger = yes }
			}
		}
		effect = {
			wealth = -500
			prestige = -1000
			primary_title = {
				create_title = {
					tier = KING
					landless = no
					temporary = no
					custom_created = yes
					culture = ROOT
					holder = ROOT
					base_title = THIS
					copy_title_laws = yes
				}

			}
			custom_tooltip = { text = form_new_kingdom_tooltip }
			chronicle = {
				entry = CHRONICLE_FOUNDED_NEW_KINGDOM_OR_EMPIRE
				portrait = [Root.GetID]
			}
			primary_title = {
				set_title_flag = custom_created_title
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				NOR = {
					AND = {
						trait = proud
						prestige = 2500
					}
					AND = {
						trait = ambitious
						prestige = 2500
					}
					AND = {
						realm_size = 70
						any_demesne_title = {
							count = 4
							tier = DUKE
						}
					}
					prestige = 5000
				}
			}
		}
	}

	form_new_empire = {
		is_high_prio = yes
		only_playable = yes
		only_independent = yes
		potential = {
			has_dlc = "Charlemagne"
			is_playable = yes
			is_nomadic = no
			real_tier = KING
			NOT = { real_tier = EMPEROR }
			independent = yes
			primary_title = { is_primary_type_title = no }
			NOT = {
				has_game_rule = {
					name = dynamic_realms
					value = off
				}
			}
		}
		allow = {
			war = no
			has_regent = no
			piety = 0
			prestige = 2500
			wealth = 1000
			capital_scope = { TECH_MAJESTY = 2 }
			has_empire_size_trigger = yes
			empire_creation_kingdom_rules_trigger = yes
			custom_tooltip = {
				text = ALLOWED_CUSTOM_EMPIRE
				hidden_tooltip = { allowed_custom_titles_trigger = yes }
			}
		}
		effect = {
			wealth = -1000
			prestige = -2500
			primary_title = {
				create_title = {
					tier = EMPEROR
					landless = no
					temporary = no
					custom_created = yes
					culture = ROOT
					holder = ROOT
					base_title = THIS
					copy_title_laws = yes
				}
			}
			custom_tooltip = { text = form_new_empire_tooltip }
			chronicle = {
				entry = CHRONICLE_FOUNDED_NEW_KINGDOM_OR_EMPIRE
				portrait = [Root.GetID]
			}
			primary_title = {
				set_title_flag = custom_created_title
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				has_character_flag = is_charlemagne
			}
			modifier = {
				factor = 0
				OR = {
					has_landed_title = e_mongol_empire
					has_landed_title = e_golden_horde
					has_landed_title = e_il-khanate
					has_landed_title = e_chagatai
				}
			}
			modifier = {
				factor = 0
				NOR = {
					AND = {
						trait = proud
						prestige = 5000
					}
					AND = {
						trait = ambitious
						prestige = 5000
					}
					any_demesne_title = {
						count = 4
						tier = KING
					}
					prestige = 10000
				}
			}
		}
	}
	
	# Hold Wargames - for nomads
	hold_nomad_wargames = {
		only_playable = yes
		
		potential = {
			is_playable = yes
			is_nomadic = yes
			NOT = { has_character_modifier = holding_nomad_wargames }
			NOT = { has_character_modifier = held_nomad_wargames }
		}
		allow = {
			is_adult = yes
			prisoner = no
			NOT = { trait = incapable }
			NOT = { is_inaccessible_trigger = yes }
			war = no
			any_demesne_title = {
				NOT = { higher_tier_than = count }
				is_occupied = no
			}
			wealth = 20
			prestige = 100
			is_not_busy_trigger = yes
		}
		effect = {
			wealth = -30
			add_character_modifier = {
				name = holding_nomad_wargames
				duration = -1
			}
			set_character_flag = recent_minor_decision
			hidden_tooltip = { character_event = { id = HL.7000 } }
		}
		ai_will_do = {
			factor = 0.1
			modifier = {
				factor = 0
				NOT = {
					wealth = 200
				}
			}
			modifier = {
				factor = 2
				current_heir = {
					dynasty = ROOT
					combat_rating = 8
				}
			}
			modifier = {
				factor = 0.5
				NOT = { martial = 10 }
			}	
			modifier = {
				factor = 0
				has_character_flag = recent_minor_decision
				NOT = {
					had_character_flag = {
						flag = recent_minor_decision
						days = 730
					}
				}
			}	
		}
	}
	
	convert_to_feudalism_indep = {
		is_high_prio = yes
		only_rulers = yes
		only_independent = yes
		potential = {
			is_playable = yes
			is_tribal = yes
			is_ruler = yes
			independent = yes
		}
		allow = {
			war = no
			OR = {
				primary_title = { has_law = tribal_organization_4 }
				AND = {
					primary_title = { has_law = tribal_organization_2 }
					TECH_LEGALISM = 3
					TECH_NOBLE_CUSTOMS = 3
					TECH_MAJESTY = 3
				}
			}
			capital_scope = {
				TECH_NOBLE_CUSTOMS = 1
				OR = {
					custom_tooltip = {
						text = TT_NOT_UNREFORMED_PAGAN
						hidden_tooltip = {
							OR = {
								ROOT = { is_pagan_religion = no }
								ROOT = { is_reformed_pagan_religion = yes }
								ROOT = { religion = tibetan_pagan }
							}
						}
					}
					TECH_NOBLE_CUSTOMS = 3
				}
			}
			capital_holding = {
				OR = {
					holding_type = castle
					AND = {
						holding_type = tribal
						has_building = tb_hillfort_4
					}
				}
			}
		}
		effect = {
			#notify of holdings that will not upgrade
			any_demesne_province = {
				limit = {
					culture = ROOT
					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_hillfort_4 }
					}
				}
				random_province_holding = {
					if = {
						limit = {
							holding_type = tribal
						}
						custom_tooltip = { text = TRIBAL_NOT_UPGRADE }
					}
				}
			}
			#notify of holdings that will cause an issue
			any_demesne_province = {
				limit = {
					NOT = { culture = ROOT }
					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_hillfort_4 }
					}
				}
				random_province_holding = {
					if = {
						limit = {
							holding_type = tribal
						}
						custom_tooltip = { text = TRIBAL_PROVINCE_NOT_CULTURE_WARNING }
					}
				}
			}
			#notify of vassals that will cause an issue
			any_vassal = {
				limit = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
				}
				custom_tooltip = { text = TRIBAL_VASSAL_NOT_CULTURE_WARNING }
			}
			#upgrade tribal holdings which have full hillforts
			any_demesne_province = {
				limit = {
					any_province_holding = {
						holding_type = tribal
						has_building = tb_hillfort_4
					}
				}
				any_province_holding = {
					limit = {
						holding_type = tribal
						has_building = tb_hillfort_4
					}
					set_title_flag = refill_levy
					convert_to = CASTLE
					hidden_tooltip = {
						refill_holding_levy = yes
					}
				}
				hidden_tooltip = {
					add_province_modifier = {
						name = new_buildings_timer
						duration = 36500
						hidden = yes
					}
				}
			}
			if = {
				limit = {
					NOT = { religion_group = muslim }
					NOT = { religion = bogomilist }
					NAND = { 
						culture_group = tibetan_group
						OR = {
							religion = bon
							religion = buddhist
							religion = tibetan_pagan
						}
						higher_tier_than = BARON
					}
				}
				if = {
					limit = { ai = no }
					chronicle = {
						entry = CHRONICLE_ADOPTED_FEUDALISM
						picture = GFX_evt_castle_construction
					}
				}
				set_government_type = feudal_government
			}
			if = {
				limit = { religion_group = muslim }
				if = {
					limit = { ai = no }
					chronicle = {
						entry = CHRONICLE_ADOPTED_IQTA
						picture = GFX_evt_castle_construction
					}
				}
				set_government_type = muslim_government
			}
			if = {
				limit = { religion = bogomilist }
				if = {
					limit = { ai = no }
					chronicle = {
						entry = CHRONICLE_ADOPTED_BOGOMILIST
						picture = GFX_evt_castle_construction
					}
				}
				set_government_type = bogomilist_government
			}
			if = {
				limit = {
					AND = { 
						culture_group = tibetan_group
						OR = {
							religion = bon
							religion = buddhist
							religion = tibetan_pagan
						}
						higher_tier_than = BARON
					}
				}
				set_government_type = theocratic_feudal_government
			}
			if = {
				limit = { higher_tier_than = king }
				primary_title = {
					add_law = feudal_administration
				}
			}
			primary_title = {
				hidden_tooltip = { revoke_law = tribal_organization_4 }
			}
			hidden_tooltip = {
				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}
				character_event = { id = Plus.352 days = 3 } #refill the levies
			}
		}
		ai_will_do = {
			factor = 1 # On average ca 1 year before taken
			modifier = {
				factor = 0
				any_demesne_title = {
					tier = baron
					holding_type = tribal
					NOT = { has_building = tb_hillfort_4 }
					location = { NOT = { culture = ROOT } }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.75 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 2
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.5 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 3
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.25 }
				}
			}
		}
	}
	
	convert_to_feudalism_vassal = {
		is_high_prio = yes
		only_rulers = yes
		potential = {
			is_tribal = yes
			independent = no
			is_ruler = yes
			any_demesne_title = {
				tier = baron
				holding_type = tribal
			}
		}
		allow = {
			war = no
			liege = {
				is_tribal = no
				is_nomadic = no
			}
			OR = {
				capital_holding = {
					OR = {
						holding_type = castle
						AND = {
							holding_type = tribal
							has_building = tb_hillfort_4
						}
					}
				}
				AND = {
					NOT = { tier = BARON }
					capital_scope = {
						any_province_holding = {
							holding_type = castle
							holder_scope = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
								}
							}
						}
					}
				}
			}
		}
		effect = {
			#notify of holdings that will not upgrade
			any_demesne_province = {
				limit = {
					culture = ROOT
					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_hillfort_4 }
					}
				}
				random_province_holding = {
					if = {
						limit = {
							holding_type = tribal
						}
						custom_tooltip = { text = TRIBAL_NOT_UPGRADE }
					}
				}
			}
			#notify of holdings that will cause an issue
			any_demesne_province = {
				limit = {
					NOT = { culture = ROOT }
					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_hillfort_4 }
					}
				}
				random_province_holding = {
					if = {
						limit = {
							holding_type = tribal
						}
						custom_tooltip = { text = TRIBAL_PROVINCE_NOT_CULTURE_WARNING }
					}
				}
			}
			#notify of vassals that will cause an issue
			any_vassal = {
				limit = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
				}
				custom_tooltip = { text = TRIBAL_VASSAL_NOT_CULTURE_WARNING }
			}			
		
			#upgrade tribal holdings which have full hillforts
			any_demesne_province = {
				limit = {
					any_province_holding = {
						holding_type = tribal
						has_building = tb_hillfort_4
					}
				}
				any_province_holding = {
					limit = {
						holding_type = tribal
						has_building = tb_hillfort_4
					}
					set_title_flag = refill_levy
					convert_to = CASTLE
					hidden_tooltip = {
						refill_holding_levy = yes
					}
				}
				hidden_tooltip = {
					add_province_modifier = {
						name = new_buildings_timer
						duration = 36500
						hidden = yes
					}
				}
			}
			
			#if upgrading from non-capital castle
			hidden_tooltip = {
				if = {
					limit = {
						capital_holding = {
							holding_type = tribal
							ROOT = { NOT = { tier = BARON } }
						}
						capital_scope = {
							any_province_holding = {
								holding_type = castle
								holder_scope = {
									OR = {
										character = ROOT
										is_liege_or_above = ROOT
									}
								}
							}
						}
					}
					
					capital_scope = {
						random_province_holding = {
							limit = {
								holding_type = castle
								holder_scope = {
									OR = {
										character = ROOT
										is_liege_or_above = ROOT
									}
								}
							}
							if = {
								limit = {
									NOT = { holder = ROOT }
								}
								usurp_title = ROOT
							}
							make_capital_holding = yes
							ROOT = { set_character_flag = feudalize_handled_capital }
						}
					}
				}
			}
			
			if = {
				limit = {
					NOT = { religion_group = muslim }
					NOT = { religion = bogomilist }
					NAND = { 
						culture_group = tibetan_group
						OR = {
							religion = bon
							religion = buddhist
							religion = tibetan_pagan
						}
						higher_tier_than = BARON
					}
				}
				if = {
					limit = { ai = no }
					chronicle = {
						entry = CHRONICLE_ADOPTED_FEUDALISM
						picture = GFX_evt_castle_construction
					}
				}
				set_government_type = feudal_government
			}
			if = {
				limit = { religion_group = muslim }
				if = {
					limit = { ai = no }
					chronicle = {
						entry = CHRONICLE_ADOPTED_IQTA
						picture = GFX_evt_castle_construction
					}
				}
				set_government_type = muslim_government
			}
			if = {
				limit = { religion = bogomilist }
				if = {
					limit = { ai = no }
					chronicle = {
						entry = CHRONICLE_ADOPTED_BOGOMILIST
						picture = GFX_evt_castle_construction
					}
				}
				set_government_type = bogomilist_government
			}
			if = {
				limit = {
					AND = { 
						culture_group = tibetan_group
						OR = {
							religion = bon
							religion = buddhist
							religion = tibetan_pagan
						}
						higher_tier_than = BARON
					}
				}
				set_government_type = theocratic_feudal_government
			}
			primary_title = {
				hidden_tooltip = {
					revoke_law = tribal_organization_0
					revoke_law = tribal_organization_1
					revoke_law = tribal_organization_2
					revoke_law = tribal_organization_3
					revoke_law = tribal_organization_4
				}
			}
			hidden_tooltip = {
				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}
				character_event = { id = Plus.352 days = 3 } #refill the levies
				liege = {
					character_event = { id = 55100 } # Notify Liege
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 0
				any_demesne_title = {
					tier = baron
					holding_type = tribal
					NOT = { has_building = tb_hillfort_4 }
					location = { NOT = { culture = ROOT } }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.75 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 2
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.5 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 3
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.25 }
				}
			}
			modifier = {
				factor = 0.001
				liege = {
					OR = {
						NOT = { religion_group = ROOT }
						AND = {
							is_pagan_religion = yes
							NOT = { religion = ROOT }
						}
					}
				}
			}
			modifier = {
				factor = 0.01
				liege = {
					NOT = { culture_group = ROOT }
				}
			}
			
			modifier = {
				factor = 0.01
				liege = {
					religion_group = ROOT
					is_pagan_religion = no
					NOT = { religion = ROOT }
				}
			}
			modifier = {
				factor = 0.1
				liege = {
					culture_group = ROOT
					NOT = { culture = ROOT }
				}
			}
			modifier = {
				factor = 0.5
				liege = { is_republic = yes }
			}
			modifier = {
				factor = 1.5
				liege = { is_feudal = yes }
			}
			modifier = {
				factor = 5
				OR = {
					trait = misguided_warrior
					trait = tough_soldier
					trait = skilled_tactician
					trait = brilliant_strategist
				}
			}
			modifier = {
				factor = 0.5
				liege = { is_republic = yes }
				OR = {
					is_patrician = yes
					leads_faction = faction_prosperity
					in_faction = faction_prosperity
				}
			}
		}
	}

	convert_to_republic_indep = {
		is_high_prio = yes
		only_rulers = yes
		only_independent = yes
		potential = {
			is_playable = yes
			is_tribal = yes
			is_ruler = yes
			independent = yes
			OR = {
				ai = yes
				has_dlc = "The Republic"
			}
		}
		allow = {
			war = no
			is_female = no
			higher_tier_than = COUNT
			primary_title = { has_law = tribal_organization_4 }
			capital_scope = {
				port = yes
				TECH_TRADE_PRACTICES = 1
				OR = {
					custom_tooltip = {
						text = TT_NOT_UNREFORMED_PAGAN
						hidden_tooltip = {
							OR = {
								ROOT = { is_pagan_religion = no }
								ROOT = { is_reformed_pagan_religion = yes }
							}
						}
					}
					TECH_TRADE_PRACTICES = 3
				}
			}
			capital_holding = {
				OR = {
					holding_type = city
					AND = {
						holding_type = tribal
						has_building = tb_market_town_4
					}
				}
			}
		}
		effect = {
			#notify of holdings that will not upgrade
			any_demesne_province = {
				limit = {
					culture = ROOT
					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_market_town_4 }
					}
				}
				random_province_holding = {
					if = {
						limit = {
							holding_type = tribal
						}
						custom_tooltip = { text = TRIBAL_NOT_UPGRADE }
					}
				}
			}
			#notify of holdings that will cause an issue
			any_demesne_province = {
				limit = {
					NOT = { culture = ROOT }
					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_market_town_4 }
					}
				}
				random_province_holding = {
					if = {
						limit = {
							holding_type = tribal
						}
						custom_tooltip = { text = TRIBAL_PROVINCE_NOT_CULTURE_WARNING }
					}
				}
			}
			#notify of vassals that will cause an issue
			any_vassal = {
				limit = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
				}
				custom_tooltip = { text = TRIBAL_VASSAL_NOT_CULTURE_WARNING }
			}
			
			hidden_tooltip = {
				create_family_palace = yes				
				set_government_type = merchant_republic_government
				
				if = {
					limit = {
						NOT = {
							num_of_government_vassals = {
								government = merchant_republic_government
								value = 1
							}
						}
					}
					
					any_unique_dynasty_vassal = { # gives random vassals a family palace
						count = 4
						limit = {
							is_female = no
							OR = {
								is_republic = yes
								is_tribal = yes
							}
							is_patrician = no
							prisoner = no
							NOT = { trait = incapable }
							is_adult = yes
							NOT = { dynasty = none }
						}	
						if = {
							limit = { is_tribal = yes }		
							hidden_tooltip = {
								capital_holding = {
									if = {
										limit = { holding_type = tribal }
										set_title_flag = refill_levy
										convert_to = CITY
										hidden_tooltip = {
											refill_holding_levy = yes
											location = {
												add_province_modifier = {
													name = new_buildings_timer
													duration = 36500
													hidden = yes
												}
											}
										}
									}
								}
								any_demesne_province = {
									limit = {
										any_province_holding = {
											holding_type = tribal
											has_building = tb_market_town_4
										}
									}
									any_province_holding = {
										limit = {
											holding_type = tribal
											has_building = tb_market_town_4
										}
										set_title_flag = refill_levy
										convert_to = CITY
										hidden_tooltip = {
											refill_holding_levy = yes
										}
									}
									hidden_tooltip = {
										add_province_modifier = {
											name = new_buildings_timer
											duration = 36500
											hidden = yes
										}
									}
								}
							}
						}
						create_family_palace = yes
					}
				}
			}
				
			#upgrade tribal holdings which have full market towns
			any_demesne_province = {
				limit = {
					any_province_holding = {
						holding_type = tribal
						has_building = tb_market_town_4
					}
				}
				any_province_holding = {
					limit = {
						holding_type = tribal
						has_building = tb_market_town_4
					}
					set_title_flag = refill_levy
					convert_to = CITY
					hidden_tooltip = {
						refill_holding_levy = yes
					}
				}
				hidden_tooltip = {
					add_province_modifier = {
						name = new_buildings_timer
						duration = 36500
						hidden = yes
					}
				}
			}
			if = {
				limit = { ai = no }
				chronicle = {
					entry = CHRONICLE_FOUNDED_MERCHANT_REPUBLIC
					picture = GFX_evt_busy_trading_dock_republic
				}
			}
			
			set_government_type = merchant_republic_government
			
			hidden_tooltip = {
				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}
				character_event = { id = Plus.352 days = 3 } #refill the levies
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 0
				any_demesne_title = {
					tier = baron
					holding_type = tribal
					NOT = { has_building = tb_market_town_4 }
					location = { NOT = { culture = ROOT } }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.75 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 2
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.5 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 3
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.25 }
				}
			}
			modifier = {
				factor = 2.0
				stewardship = 10
			}
			modifier = {
				factor = 4.0
				capital_holding = { holding_type = city }
			}
		}
	}
	
	convert_to_republic_vassal = {
		is_high_prio = yes
		only_rulers = yes
		potential = {
			is_tribal = yes
			independent = no
			is_ruler = yes
			any_demesne_title = {
				tier = baron
				holding_type = tribal
			}
			OR = {
				ai = yes
				has_dlc = "The Republic"
			}
		}
		allow = {
			war = no
			is_female = no
			higher_tier_than = COUNT
			capital_scope = { port = yes }
			liege = {
				OR = {
					is_feudal = yes
					is_theocracy = yes
				}
			}
			capital_holding = {
				OR = {
					holding_type = city
					AND = {
						holding_type = tribal
						has_building = tb_market_town_4
					}
				}
			}
		}
		effect = {
			#notify of holdings that will not upgrade
			any_demesne_province = {
				limit = {
					culture = ROOT
					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_market_town_4 }
					}
				}
				random_province_holding = {
					if = {
						limit = {
							holding_type = tribal
						}
						custom_tooltip = { text = TRIBAL_NOT_UPGRADE }
					}
				}
			}
			#notify of holdings that will cause an issue
			any_demesne_province = {
				limit = {
					NOT = { culture = ROOT }
					any_province_holding = {
						holding_type = tribal
						NOT = { has_building = tb_market_town_4 }
					}
				}
				random_province_holding = {
					if = {
						limit = {
							holding_type = tribal
						}
						custom_tooltip = { text = TRIBAL_PROVINCE_NOT_CULTURE_WARNING }
					}
				}
			}
			#notify of vassals that will cause an issue
			any_vassal = {
				limit = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
				}
				custom_tooltip = { text = TRIBAL_VASSAL_NOT_CULTURE_WARNING }
			}
			
			hidden_tooltip = {
				create_family_palace = yes
				if = {
					limit = {
						NOT = {
							num_of_government_vassals = {
								government = merchant_republic_government
								value = 1
							}
						}
					}
					any_unique_dynasty_vassal = { # gives random vassals a family palace
						count = 4
						limit = {
							is_female = no
							OR = {
								is_republic = yes
								is_tribal = yes
							}
							is_patrician = no
							prisoner = no
							NOT = { trait = incapable }
							is_adult = yes
							NOT = { dynasty = none }
						}	
						if = {
							limit = { is_tribal = yes }		
							hidden_tooltip = {
								capital_holding = {
									if = {
										limit = { holding_type = tribal }
										set_title_flag = refill_levy
										convert_to = CITY
										hidden_tooltip = {
											refill_holding_levy = yes
											location = {
												add_province_modifier = {
													name = new_buildings_timer
													duration = 36500
													hidden = yes
												}
											}
										}
									}
								}
								any_demesne_province = {
									limit = {
										any_province_holding = {
											holding_type = tribal
											has_building = tb_market_town_4
										}
									}
									any_province_holding = {
										limit = {
											holding_type = tribal
											has_building = tb_market_town_4
										}
										set_title_flag = refill_levy
										convert_to = CITY
										hidden_tooltip = {
											refill_holding_levy = yes
										}
									}
									hidden_tooltip = {
										add_province_modifier = {
											name = new_buildings_timer
											duration = 36500
											hidden = yes
										}
									}
								}
							}
						}
						create_family_palace = yes
					}
				}
			}
			
			#upgrade tribal holdings which have full market towns
			any_demesne_province = {
				limit = {
					any_province_holding = {
						holding_type = tribal
						has_building = tb_market_town_4
					}
				}
				any_province_holding = {
					limit = {
						holding_type = tribal
						has_building = tb_market_town_4
					}
					set_title_flag = refill_levy
					convert_to = CITY
					hidden_tooltip = {
						refill_holding_levy = yes
					}
				}
				hidden_tooltip = {
					add_province_modifier = {
						name = new_buildings_timer
						duration = 36500
						hidden = yes
					}
				}
			}
			if = {
				limit = { ai = no }
				chronicle = {
					entry = CHRONICLE_FOUNDED_MERCHANT_REPUBLIC
					picture = GFX_evt_busy_trading_dock_republic
				}
			}
			
			set_government_type = merchant_republic_government
			
			hidden_tooltip = {
				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}
				liege = {
					character_event = { id = 55100 } # Notify Liege
				}
				character_event = { id = Plus.352 days = 3 } #refill the levies
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 0
				any_demesne_title = {
					tier = baron
					holding_type = tribal
					NOT = { has_building = tb_market_town_4 }
					location = { NOT = { culture = ROOT } }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.75 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 2
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.5 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 3
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.25 }
				}
			}
			modifier = {
				factor = 0.001
				liege = {
					OR = {
						NOT = { religion_group = ROOT }
						AND = {
							is_pagan_religion = yes
							NOT = { religion = ROOT }
						}
					}
				}
			}
			modifier = {
				factor = 0.01
				liege = {
					NOT = { culture_group = ROOT }
				}
			}
			
			modifier = {
				factor = 0.01
				liege = {
					religion_group = ROOT
					is_pagan_religion = no
					NOT = { religion = ROOT }
				}
			}
			modifier = {
				factor = 0.1
				liege = {
					culture_group = ROOT
					NOT = { culture = ROOT }
				}
			}
			modifier = {
				factor = 0
				NOT = {
					independent = yes
					liege = { is_republic = yes }
					is_patrician = yes
					leads_faction = faction_prosperity
					in_faction = faction_prosperity
					trait = fortune_builder
					trait = midas_touched
				}
			}
			modifier = {
				factor = 0.1
				OR = {
					liege = { is_feudal = yes }
					liege = { is_theocracy = yes }
					independent = yes
				}
				NOT = {
					trait = indulgent_wastrel
					trait = thrifty_clerk
					trait = fortune_builder
					trait = midas_touched
				}
			}
			modifier = {
				factor = 0.5
				liege = { is_republic = yes }
				NOT = {
					is_patrician = yes
					trait = indulgent_wastrel
					trait = thrifty_clerk
					trait = fortune_builder
					trait = midas_touched
				}
			}
			modifier = {
				factor = 1.5
				trait = thrifty_clerk
			}
			modifier = {
				factor = 2.5
				trait = fortune_builder
			}
			modifier = {
				factor = 5
				trait = midas_touched
			}
			modifier = {
				factor = 10
				OR = {
					is_patrician = yes
					is_republic = yes
				}
			}
			#feudal liege has less than 5 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 5
						}
					}
				}
			}
			#feudal liege has a republic vassal and less than 10 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_republic = yes
						higher_tier_than = BARON
					}
					NOT = {
						any_vassal = {
							is_republic = yes
							higher_tier_than = BARON
							count = 2
						}
					}
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 10
						}
					}
				}
			}
			#feudal liege has two republic vassals and less than 20 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_republic = yes
						higher_tier_than = BARON
						count = 2
					}
					NOT = {
						any_vassal = {
							is_republic = yes
							higher_tier_than = BARON
							count = 3
						}
					}
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 20
						}
					}
				}
			}
			#feudal liege has three republic vassals and less than 30 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_republic = yes
						higher_tier_than = BARON
						count = 3
					}
					NOT = {
						any_vassal = {
							is_republic = yes
							higher_tier_than = BARON
							count = 4
						}
					}
					NOT = {
						any_vassal = {
							is_feudal = yes
							higher_tier_than = BARON
							count = 30
						}
					}
				}
			}
			#feudal liege has four republic vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_republic = yes
						higher_tier_than = BARON
						count = 4
					}
				}
			}
		}
	}
	
	convert_to_city_vassal = {
		is_high_prio = yes
		only_rulers = yes
		potential = {
			is_tribal = yes
			independent = no
			ai = yes
			is_ruler = yes
			any_demesne_title = {
				tier = baron
				holding_type = tribal
			}
		}
		allow = {
			war = no
			is_female = no
			OR = {
				lower_tier_than = DUKE
				NOT = { capital_scope = { port = yes } }
				liege = { is_republic = yes }
			}
			liege = {
				OR = {
					is_feudal = yes
					is_republic = yes
					is_theocracy = yes
				}
			}
			capital_holding = {
				OR = {
					holding_type = city
					AND = {
						holding_type = tribal
						has_building = tb_market_town_4
					}
					AND = {
						holding_type = tribal
						has_building = tb_hillfort_4
					}
				}
			}
		}
		effect = {
			#upgrade tribal holdings which have full market towns
			any_demesne_province = {
				limit = {
					any_province_holding = {
						holding_type = tribal
						OR = {
							has_building = tb_market_town_4
							has_building = tb_hillfort_4
						}
					}
				}
				any_province_holding = {
					limit = {
						holding_type = tribal
						OR = {
							has_building = tb_market_town_4
							has_building = tb_hillfort_4
						}
					}
					set_title_flag = refill_levy
					convert_to = CITY
					hidden_tooltip = {
						refill_holding_levy = yes
					}
				}
				hidden_tooltip = {
					add_province_modifier = {
						name = new_buildings_timer
						duration = 36500
						hidden = yes
					}
				}
			}
	
			set_government_type = republic_government
			
			hidden_tooltip = {
				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}
				liege = {
					character_event = { id = 55100 } # Notify Liege
				}
				character_event = { id = Plus.352 days = 3 } #refill the levies
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 0
				any_demesne_title = {
					tier = baron
					holding_type = tribal
					NOT = { has_building = tb_market_town_4 }
					NOT = { has_building = tb_hillfort_4 }
					location = { NOT = { culture = ROOT } }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.75 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 2
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.5 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 3
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { culture = ROOT }
					relative_power = { who = ROOT power = 0.25 }
				}
			}
			modifier = {
				factor = 0.001
				liege = {
					OR = {
						NOT = { religion_group = ROOT }
						AND = {
							is_pagan_religion = yes
							NOT = { religion = ROOT }
						}
					}
				}
			}
			modifier = {
				factor = 0.01
				liege = {
					NOT = { culture_group = ROOT }
				}
			}
			
			modifier = {
				factor = 0.01
				liege = {
					religion_group = ROOT
					is_pagan_religion = no
					NOT = { religion = ROOT }
				}
			}
			modifier = {
				factor = 0.1
				liege = {
					culture_group = ROOT
					NOT = { culture = ROOT }
				}
			}
			modifier = {
				factor = 0
				NOT = {
					independent = yes
					liege = { is_republic = yes }
					is_patrician = yes
					leads_faction = faction_prosperity
					in_faction = faction_prosperity
					trait = fortune_builder
					trait = midas_touched
				}
			}
			modifier = {
				factor = 0.1
				OR = {
					liege = { is_feudal = yes }
					liege = { is_theocracy = yes }
					independent = yes
				}
				NOT = {
					trait = indulgent_wastrel
					trait = thrifty_clerk
					trait = fortune_builder
					trait = midas_touched
				}
			}
			modifier = {
				factor = 0.5
				liege = { is_republic = yes }
				NOT = {
					is_patrician = yes
					trait = indulgent_wastrel
					trait = thrifty_clerk
					trait = fortune_builder
					trait = midas_touched
				}
			}
			modifier = {
				factor = 1.5
				trait = thrifty_clerk
			}
			modifier = {
				factor = 2.5
				trait = fortune_builder
			}
			modifier = {
				factor = 5
				trait = midas_touched
			}
			modifier = {
				factor = 10
				OR = {
					is_patrician = yes
					is_republic = yes
				}
			}
			#feudal liege has less than 5 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 5
						}
					}
				}
			}
			#feudal liege has a republic vassal and less than 10 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_republic = yes
						higher_tier_than = BARON
					}
					NOT = {
						any_vassal = {
							is_republic = yes
							higher_tier_than = BARON
							count = 2
						}
					}
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 10
						}
					}
				}
			}
			#feudal liege has two republic vassals and less than 20 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_republic = yes
						higher_tier_than = BARON
						count = 2
					}
					NOT = {
						any_vassal = {
							is_republic = yes
							higher_tier_than = BARON
							count = 3
						}
					}
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 20
						}
					}
				}
			}
			#feudal liege has three republic vassals and less than 30 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_republic = yes
						higher_tier_than = BARON
						count = 3
					}
					NOT = {
						any_vassal = {
							is_republic = yes
							higher_tier_than = BARON
							count = 4
						}
					}
					NOT = {
						any_vassal = {
							is_feudal = yes
							higher_tier_than = BARON
							count = 30
						}
					}
				}
			}
			#feudal liege has four republic vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_republic = yes
						higher_tier_than = BARON
						count = 4
					}
				}
			}
		}
	}
	
	convert_to_theocracy_vassal = {
		is_high_prio = yes
		only_rulers = yes
		potential = {
			is_tribal = yes
			independent = no
			ai = yes
			NOT = { religion_group = muslim }
			is_ruler = yes
			any_demesne_title = {
				tier = baron
				holding_type = tribal
			}
		}
		allow = {
			war = no
			is_adult = yes
			OR = {
				is_female = no
				religion_allows_female_temple_holders = yes
			}
			liege = {
				OR = {
					is_feudal = yes
					is_republic = yes
					is_theocracy = yes
				}
			}
			capital_holding = {
				OR = {
					holding_type = temple
					AND = {
						holding_type = tribal
						has_building = tb_market_town_4
					}
					AND = {
						holding_type = tribal
						has_building = tb_hillfort_4
					}
				}
			}
		}
		effect = {
			#upgrade tribal holdings which have full market towns
			any_demesne_province = {
				limit = {
					any_province_holding = {
						holding_type = tribal
						OR = {
							has_building = tb_market_town_4
							has_building = tb_hillfort_4
						}
					}
				}
				any_province_holding = {
					limit = { 
						holding_type = tribal
						OR = {
							has_building = tb_market_town_4
							has_building = tb_hillfort_4
						}
					}
					set_title_flag = refill_levy
					convert_to = temple
					hidden_tooltip = {
						refill_holding_levy = yes
					}
				}
				hidden_tooltip = {
					add_province_modifier = {
						name = new_buildings_timer
						duration = 36500
						hidden = yes
					}
				}
			}
	
			set_government_type = theocracy_government
			
			hidden_tooltip = {
				any_demesne_title = {
					if = {
						limit = { is_crown_law_title = yes }
						set_title_flag = do_not_destroy
					}
				}
				liege = {
					character_event = { id = 55100 } # Notify Liege
				}
				character_event = { id = Plus.352 days = 3 } #refill the levies
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.2
				always = yes
			}
			modifier = {
				factor = 0
				any_demesne_title = {
					tier = baron
					holding_type = tribal
					NOT = { has_building = tb_market_town_4 }
					NOT = { has_building = tb_hillfort_4 }
					location = { NOT = { religion = ROOT } }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { religion = ROOT }
					relative_power = { who = ROOT power = 0.75 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 2
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { religion = ROOT }
					relative_power = { who = ROOT power = 0.5 }
				}
			}
			modifier = {
				factor = 0
				any_vassal = {
					count = 3
					higher_tier_than = BARON
					is_tribal = yes
					NOT = { religion = ROOT }
					relative_power = { who = ROOT power = 0.25 }
				}
			}
			modifier = {
				factor = 0.01
				liege = {
					NOT = { culture_group = ROOT }
				}
			}
			modifier = {
				factor = 0.1
				liege = {
					culture_group = ROOT
					NOT = { culture = ROOT }
				}
			}
			modifier = {
				factor = 0
				liege = { NOT = { religion = ROOT } }
			}
			modifier = {
				factor = 0.1
				NOT = { trait = zealous }
			}
			modifier = {
				factor = 0
				NOT = {
					independent = yes
					liege = { is_theocracy = yes }
					leads_faction = faction_tradition
					in_faction = faction_tradition
					trait = scholarly_theologian
					trait = mastermind_theologian
				}
			}
			modifier = {
				factor = 0.1
				OR = {
					liege = { is_feudal = yes }
					liege = { is_republic = yes }
					independent = yes
				}
				NOT = {
					trait = detached_priest
					trait = martial_cleric
					trait = scholarly_theologian
					trait = mastermind_theologian
				}
			}
			modifier = {
				factor = 0.5
				liege = { is_theocracy = yes }
				NOT = {
					trait = detached_priest
					trait = martial_cleric
					trait = scholarly_theologian
					trait = mastermind_theologian
				}
			}
			modifier = {
				factor = 1.5
				trait = scholarly_theologian
			}
			modifier = {
				factor = 2.5
				trait = mastermind_theologian
			}
			modifier = {
				factor = 10
				is_theocracy = yes
			}
			#feudal liege has less than 5 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 5
						}
					}
				}
			}
			#feudal liege has a theocracy vassal and less than 10 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_theocracy = yes
						higher_tier_than = BARON
					}
					NOT = {
						any_vassal = {
							is_theocracy = yes
							higher_tier_than = BARON
							count = 2
						}
					}
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 10
						}
					}
				}
			}
			#feudal liege has two theocracy vassals and less than 20 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_theocracy = yes
						higher_tier_than = BARON
						count = 2
					}
					NOT = {
						any_vassal = {
							is_theocracy = yes
							higher_tier_than = BARON
							count = 3
						}
					}
					NOT = {
						any_vassal = { 
							is_feudal = yes
							higher_tier_than = BARON
							count = 20
						}
					}
				}
			}
			#feudal liege has three theocracy vassals and less than 30 feudal vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_theocracy = yes
						higher_tier_than = BARON
						count = 3
					}
					NOT = {
						any_vassal = {
							is_theocracy = yes
							higher_tier_than = BARON
							count = 4
						}
					}
					NOT = {
						any_vassal = {
							is_feudal = yes
							higher_tier_than = BARON
							count = 30
						}
					}
				}
			}
			#feudal liege has four theocracy vassals
			modifier = {
				factor = 0
				independent = no
				liege = {
					is_feudal = yes
					any_vassal = {
						is_theocracy = yes
						higher_tier_than = BARON
						count = 4
					}
				}
			}
		}
	}
	
	ai_destroy_custom_title = {
		is_high_prio = yes
		potential = {
			ai = yes
			any_demesne_title = {
				higher_tier_than = DUKE
				is_titular = yes
				has_title_flag = custom_created_title
				ROOT = {
					any_demesne_title = {
						tier = PREVPREV
						is_titular = no
						temporary = no
						is_primary_type_title = no
						is_landless_type_title = no
					}
				}
			}
		}
		allow = {
			war = no
		}
		effect = {
			random_demesne_title = {
				limit = {
					higher_tier_than = DUKE
					is_titular = yes
					has_title_flag = custom_created_title
					ROOT = {
						any_demesne_title = {
							tier = PREVPREV
							is_titular = no
							temporary = no
							is_primary_type_title = no
							is_landless_type_title = no
						}
					}
				}
				destroy_landed_title = THIS
			}
		}
		ai_will_do = {
			factor = 1
		}
	}

	become_merchant_republic = {
		is_high_prio = yes
		only_rulers = yes
		potential = {
			has_dlc = "Charlemagne"
			OR = {
				ai = yes
				has_dlc = "The Republic"
			}
			OR = {
				independent = yes
				liege = {
					NOT = { any_demesne_title = { temporary = yes } }
					higher_real_tier_than = DUKE
					war = no
				}
			}
			is_ruler = yes
			is_republic = yes
			is_merchant_republic = no
			is_patrician = no
			real_tier = COUNT
			NOT = {
				any_demesne_title = { temporary = yes }
			}
			is_not_busy_trigger = yes
		}
		allow = {
			capital_scope = {
				port = yes
				TECH_TRADE_PRACTICES = 1
				OR = {
					custom_tooltip = {
						text = TT_NOT_UNREFORMED_PAGAN
						hidden_tooltip = {
							OR = {
								ROOT = { is_pagan_religion = no }
								ROOT = { is_reformed_pagan_religion = yes }
							}
						}
					}
					TECH_TRADE_PRACTICES = 3
				}
			}		
			war = no
			has_regent = no
			piety = 0
			wealth = 250
			prestige = 250
		}
		effect = {
			wealth = -250
			prestige = -250
			if = {
				limit = { independent = no }
				liege = { save_event_target_as = current_liege }
			}
			if = {
				limit = { dynasty = none }
				dynasty = father_bastard
			}
			any_demesne_title = {
				limit = {
					OR = {
						NOT = { has_law = succ_seniority }
						NOT = { has_law = agnatic_succession }
					}
				}
				if = {
					limit = { NOT = { has_law = succ_seniority } }
					add_law = succ_seniority
				}
				if = {
					limit = { NOT = { has_law = agnatic_succession } }
					add_law = agnatic_succession
				}
			}
			primary_title = {
				create_title = {
					tier = DUKE
					landless = no
					temporary = no
					custom_created = yes
					culture = ROOT
					holder = ROOT
					base_title = THIS
					copy_title_laws = yes
				}
			}
			if = {
				limit = { event_target:current_liege = { is_alive = yes } }
				set_defacto_liege = event_target:current_liege
				pf_liege_change_effect = yes
			}
			primary_title = { set_title_flag = custom_created_title }
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				NOT = {
					trait = ambitious
					trait = proud
					trait = midas_touched
				}
			}
			modifier = {
				factor = 0
				OR = {
					trait = content
					trait = humble
					trait = inbred
					is_dumb_trigger = yes
					NOT = { stewardship = 8 }
				}
			}
		}
	}
}

