# 10
invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 7
	truce_days = 3650
	sort_priority = 2000

	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no

	can_use = {
		ROOT = {
			mercenary = no
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes

		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

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

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

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			prestige = 200
			participation_scaled_prestige = 200
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 200 }
		}

		defender = {
			show_scope_change = no

			prestige = -200
		}

		hidden_effect = {
			defender = {
				any_vassal = {
					set_character_flag = preserve_vassal_structure_@attacker
				}
			}

			attacker = {
				occupy_minors_of_occupied_settlements = defender
				gain_all_occupied_titles = defender

				if = {
					limit = { has_nickname = no }

					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_victorious }
						10 = { give_nickname = nick_the_bold }
						10 = { give_nickname = nick_the_conqueror }
					}
				}
				else_if = {
					limit = { has_nickname = nick_the_bastard }
					give_nickname = nick_the_conqueror
				}

				roman_victory_triumph_effect = yes
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] Invasion CB of [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		custom_tooltip = {
			text = invasion_succ_tip

			usurp_title_plus_barony_if_unlanded = {
				target = attacker
				type = invasion
			}

			defender = {
				set_defacto_liege = attacker

				if = {
					limit = { is_vassal_or_below_of = attacker}
					imprison = attacker
				}
			}

			any_independent_ruler = {
				limit = { has_character_flag = preserve_vassal_structure_@attacker }

				set_defacto_liege = attacker
				clr_character_flag = preserve_vassal_structure_@attacker
			}

			if = {
				limit = {
					higher_real_tier_than = BARON
					NOT = { culture = attacker }
				}

				conquest_culture = attacker
			}
		}

		attacker = {
			show_scope_change = no

			if = {
				limit = { character = c_140 } # William 'the Conqueror'

				if = {
					limit = {
						has_dlc = "Holy Fury"

						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = william_the_conqueror_bloodline
							}
						}
					}

					create_bloodline = {
						type = william_the_conqueror
					}
				}

				hidden_effect = {
					PREV = {
						set_title_flag = non_manual_law_change # No prestige loss
						add_law = succ_primogeniture
						clr_title_flag = non_manual_law_change # No prestige loss
					}

					any_vassal = {
						limit = { culture = anglo-saxon }

						opinion = {
							name = opinion_subjugated
							who = attacker
						}
					}

					# Help AI William by usurping extra land
					if = {
						limit = { ai = yes }

						any_de_jure_vassal_title = {
							limit = {
								OR = {
									title = d_essex
									title = d_kent
									title = d_cornwall
									title = d_somerset
									title = d_hwicce
									title = d_east_anglia
								}
							}

							usurp_title_plus_barony_if_unlanded = {
								target = attacker
								type = invasion
							}

							any_de_jure_vassal_title = {
								limit = {
									holder_scope = {
										culture = anglo-saxon
										ai = yes # But not from players
										same_realm = attacker
									}
								}

								usurp_title_plus_barony_if_unlanded = {
									target = attacker
									type = invasion
								}
							}
						}
					}

					c_middlesex = {
						if = {
							limit = { holder = attacker }

							capital_holding = {
								attacker = { capital = PREV }
							}
						}
					}
				}
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -200
		}

		defender = {
			show_scope_change = no

			prestige = 100
			participation_scaled_prestige = 100
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -300

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 300
			roman_victory_triumph_effect = yes
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 300 }
		}
	}

	on_reverse_demand_title = {
		attacker = {
			show_scope_change = no

			remove_claim = PREV
		}
	}

	on_attacker_leader_death = {
		any_defender = {
			letter_event = { id = 251 } # Our enemy has died, the war has ended
		}

		end_war = whitepeace
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
			}

			trigger_if = {
				limit = {
					ROOT = {
						is_tribal = yes
					}

					FROM = {
						is_nomadic = yes
					}
				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
			trigger_else_if = {
				limit = {
					ROOT = {
						is_nomadic = no
						is_tribal = no
					}

					FROM = {
						is_nomadic = yes
					}
				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}

		coronation_factor = yes
	}
}

# 29
imperial_reconquest = {
	name = CB_NAME_IMPERIAL
	war_name = WAR_NAME_IMPERIAL
	sprite = 17
	truce_days = 3650
	sort_priority = 1900

	is_permanent = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = no
	is_holy_war = yes

	can_use_gui = {
		check_if_crusader_trigger = yes

		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			has_landed_title = e_roman_empire
			independent = yes
			mercenary = no
			NOT = { same_realm = FROM }

			OR = {
				religion_group = christian
				religion_openly_hellenic_or_reformed_trigger = yes
			}

			# Neighbours or close
			OR = {
				shares_realm_border_with = FROM

				distance_from_realm = {
					who = FROM
					value < 150
				}
			}
		}
	}

	can_use_title = {
		capital_scope = {
			OR = {
				region = custom_byzantium
				region = custom_nikaea
				region = custom_trebizond
				region = custom_armenia
				region = custom_bulgaria
				region = custom_serbia
				region = custom_croatia
				region = custom_italy
				region = custom_sicily
				region = custom_germany
				region = custom_burgundy
				region = custom_france
				region = custom_aquitaine
				region = custom_brittany
				region = custom_england
				region = custom_wales
				region = custom_frisia
				region = custom_lotharingia
				region = world_europe_west_iberia
				region = custom_syria
				region = custom_jerusalem
				region = custom_egypt
				region = custom_africa
			}
		}

		trigger_if = {
			limit = {
				FROM = {
					shares_realm_border_with = ROOT
				}
			}

			any_de_jure_vassal_title = {
				location = {
					owner = {
						OR = {
							character = FROM
							is_vassal_or_below_of = FROM
						}
					}

					any_neighbor_province = {
						owner = {
							OR = {
								character = ROOT
								is_vassal_or_below_of = ROOT
							}
						}
					}
				}
			}
		}
		trigger_else = {
			any_de_jure_vassal_title = {
				location = {
					distance_from_realm = {
						who = ROOT
						value < 150
					}
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }

			OR = {
				religion_group = christian
				religion_openly_hellenic_or_reformed_trigger = yes
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 200
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 200 }
		}

		defender = {
			show_scope_change = no

			prestige = -200
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] imperial reconquest of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		custom_tooltip = {
			text = other_invasion_succ_tip

			attacker = {
				vassalize_or_take_under_title = {
					title = PREV
					enemy = defender
					type = invasion
				}
			}
		}

		if = {
			limit = { holder = defender }

			usurp_title_only = {
				target = attacker
				type = invasion
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -100
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 100
			participation_scaled_prestige = 200

			dynasty_head = {
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}
			}
		}

		any_defender = {
			limit = { NOT = { character = defender } }

			hidden_effect = {
				participation_scaled_piety = 100
				participation_scaled_prestige = 200

				dynasty_head = {
					if = {
						limit = { uses_decadence = yes }
						participation_scaled_decadence = -10
					}
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

roman_christian_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365
	battle_warscore_mult = 0.5

	hostile_against_others = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	check_de_jure_tier = DUKE
	is_permanent = no # Disabled in CK2Plus
	allow_whitepeace = no

	can_use = {
		# Attacker
		ROOT = { has_character_flag = roman_christian_revolter }
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = defender
			clr_character_flag = religious_revolter

			gain_all_occupied_titles = {
				who = defender
				type = revolt
			}
		}

		defender = {
			show_scope_change = no

			prestige = -500
			piety = -500
			abdicate = yes

			religion_authority = {
				name = lost_religious_revolt
			}
		}
	}

	on_success_title = {
		attacker = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = defender
				same_religion = yes # Only vassalize rulers of my religion
				is_religious = yes
				type = holy_war
			}
		}

		if = {
			limit = {
				trigger_if = {
					limit = { has_holder = yes }

					controls_religion = no

					holder_scope = {
						OR = {
							character = defender
							is_vassal_or_below_of = defender
						}
					}
				}
			}

			usurp_title = {
				target = attacker
				type = holy_war
			}
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			clear_wealth = yes
			imprison = defender
			clr_character_flag = religious_revolter

			hidden_effect = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100
			piety = 200

			hidden_effect = {
				opinion = {
					name = opinion_rebel_traitor
					who = attacker
					years = 100
				}

				add_character_modifier = {
					name = crushed_revolt
					years = 10
				}
			}

			religion_authority = {
				name = won_religious_revolt
			}

			if = {
				limit = { religion_openly_hellenic_or_reformed_trigger = yes }

				333 = { # If the Emperor has already revealed his conversion, convert the province immediately
					religion = defender
				}
			}
			else = {
				333 = { # If the Emperor burning Rome is about to switch to hellenism and wins the war, Rome turns hellenic automatically
					set_province_flag = flag_rome_primed_for_conversion
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 150
	}
}

abrahamic_antihellenic_revolt = {
	name = CB_ANTIHELLENIC_WAR
	war_name = WAR_NAME_ANTIHELLENIC_WAR
	sprite = 12
	truce_days = 3650

	major_revolt = yes
	allow_whitepeace = no
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	is_independence = yes
	is_permanent = no # Disabled in CK2Plus

	can_use = {
		# Attacker
		ROOT = {
			vassal_of = FROM
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			liege_before_war = { character = FROM }
		}

		# Defender
		FROM = {
			independent = yes
			in_revolt = no

			any_demesne_title = {
				NOT = { lower_real_tier_than = ROOT }
				temporary = no
			}

			liege_before_war = {
				in_revolt = no

				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		hidden_effect = {
			attacker = {
				clr_character_flag = flag_abrahamic_revolter
				prestige = 100
				piety = 100
				participation_scaled_prestige = 100
				participation_scaled_piety = 100

				religion_authority = {
					name = won_holy_war
					years = 20
				}

				any_demesne_title = {
					add_pressed_claim = defender
				}
			}

			any_attacker = {
				limit = { NOT = { character = attacker } }

				participation_scaled_prestige = 100
				participation_scaled_piety = 100
				clr_character_flag = flag_abrahamic_revolter
			}
		}

		defender = {
			show_scope_change = no

			if = {
				limit = {
					current_heir = {
						religion_group = pagan_group
					}
				}

				abdicate_to = attacker
			}
			else = {
				abdicate = yes
			}

			death = { death_reason = death_execution_burning }

			hidden_effect = {
				religion_authority = {
					name = lost_holy_war
					years = 20
				}
			}
		}
	}

	on_reverse_demand = {
		any_player = {
			narrative_event = { id = HF.23117 } # The Hellenic Emperor won, ensuring the rebirth of the faith
		}

		attacker = {
			show_scope_change = no

			imprison = defender

			hidden_effect = {
				religion_authority = {
					name = lost_holy_war
					years = 20
				}
			}
		}

		any_attacker = {
			hidden_effect = {
				participation_scaled_prestige = -100
				participation_scaled_piety = -100
				clr_character_flag = flag_abrahamic_revolter
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100
			piety = 100

			hidden_effect = {
				if = { # Allow free revocation of a title
					limit = {
						NOT = {
							has_opinion_modifier = {
								name = opinion_traitor
								who = attacker
							}
						}
					}

					opinion = {
						who = attacker
						name = opinion_traitor
					}
				}

				religion_authority = {
					name = won_holy_war
					years = 20
				}

				if = {
					limit = { ai = yes }

					narrative_event = { id = HF.23117 } # The Hellenic Emperor won, ensuring the rebirth of the faith
				}
			}
		}
	}

	on_invalidation = {
		any_attacker = {
			clr_character_flag = flag_abrahamic_revolter
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 150
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

### Otto Specific CBs
thankmars_rebellion = {
	name = CB_NAME_THANKMARS_REBELLION
	war_name = WAR_NAME_THANKMARS_REBELLION
	diplo_view_region = world_europe_west_germania
	sprite = 12
	truce_days = 3650

	allow_whitepeace = no
	allowed_to_target_tributaries = no
	can_ask_to_join_war = no
	major_revolt = yes
	can_call_allies = no
	is_permanent = no

	can_use = {
		# Attacker
		ROOT = {
			has_character_flag = is_thankmar
		}

		# Defender
		FROM = {
			has_character_flag = is_otto
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			has_character_flag = is_thankmar
		}

		# Defender
		FROM = {
			has_character_flag = is_otto
		}
	}

	on_invalidation = {
		any_attacker = {
			set_defacto_liege = FROM
		}

		if = {
			limit = {
				attacker = {
					is_alive = no
				}

				defender = {
					is_alive = yes
				}
			}

			any_attacker = {
				imprison = attacker
			}
		}
	}

	on_success = {
		custom_tooltip = {
			text = THANKMARS_REBELLION_VICTORY_TT

			attacker = {
				remove_trait = bastard
				dynasty = 669 # Liudolfinger

				any_child = {
					limit = {
						NOT = { trait = bastard }
					}
					dynasty = 669 # Liudolfinger
				}

				narrative_event = { id = LT.40009 } # Thankmar won rebellion
			}

			any_attacker = {
				set_defacto_liege = attacker
			}

			defender = {
				any_demesne_title = {
					grant_title_no_opinion = attacker
				}

				imprison = attacker
			}
		}
	}

	on_reverse_demand = {
		any_attacker = {
			show_scope_change = no

			imprison = defender

			hidden_effect = {
				set_defacto_liege = defender
			}
		}

		defender = {
			show_scope_change = no

			reverse_imprison = attacker
			narrative_event = { id = LT.40011 } # Otto won against rebellion
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}