# 34: The CB used by "Peasant revolter" characters
peasant_revolt = {
	name = CB_NAME_PEASANT_CONQUEST
	war_name = WAR_NAME_PEASANT_CONQUEST
	sprite = 16
	truce_days = 365

	hostile_against_others = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	press_claim = yes
	# attacker_can_call_allies = no
	# defender_can_call_allies = no
	can_ask_to_join_war = no
	is_independence = yes

	can_use = {
		# Attacker
		ROOT = {
			primary_title = { temporary = yes } # Revolter or adventurer trigger
			war = no

			NOT = {
				is_vassal_or_below_of = FROM
				has_character_modifier = holy_truce_timer
			}
		}

		FROM = {
			NOT = { has_character_modifier = holy_truce_timer }
		}
	}

	can_use_title = {
		real_tier = COUNT
	}

	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_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their peasant revolt for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		usurp_title_plus_barony_if_unlanded = {
			target = attacker
			type = revolt
		}

		attacker = {
			show_scope_change = no

			hidden_effect = {
				# Trigger automatic government validation, so actual government will depend on capital holding
				if = {
					limit = { is_nomadic = yes }
					set_government_type = feudal_government
				}
			}

			set_defacto_liege = attacker

			# CK2Plus
			hidden_effect = {
				wealth = 100

				if = {
					limit = { is_lowborn = yes }
					dynasty = father_bastard
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = -100
			clr_character_flag = has_current_revolt
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail_title = {
		attacker = {
			show_scope_change = no

			clear_wealth = yes
			imprison = defender

			hidden_effect = {
				add_character_modifier = {
					name = broken_spirit
					years = 10
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 10
			clr_character_flag = has_current_revolt

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

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			clear_wealth = yes
			imprison = defender

			hidden_effect = {
				add_character_modifier = {
					name = broken_spirit
					years = 10
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100
			clr_character_flag = has_current_revolt

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

				add_character_modifier = {
					name = crushed_major_revolt
					years = 10
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(peasant_revolt): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	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
	}
}

# 35: The CB used by "Heretic revolter" characters
heretic_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365

	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

	can_use = {
		# Attacker
		ROOT = {
			has_character_flag = heretic_revolter
			NOT = { has_character_modifier = holy_truce_timer }
		}

		# Defender
		FROM = {
			NOT = { has_character_modifier = holy_truce_timer }
		}
	}

	is_valid_title = {
		# Attacker
		ROOT = {
			NOT = { religion = FROM }
		}

		# CK2Plus
		any_direct_de_jure_vassal_title = {
			location = {
				religion = ROOT
			}

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

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] heretic revolt for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		attacker = {
			show_scope_change = no

			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = defender

			gain_all_occupied_titles = {
				who = defender
				type = revolt
			}

			clr_character_flag = heretic_revolter

			# CK2Plus
			hidden_effect = {
				wealth = 100

				if = {
					limit = { is_lowborn = yes }
					dynasty = father_bastard
				}
			}
		}

		dejure_liege_title = {
			show_scope_change = no

			any_de_jure_vassal_title = {
				show_scope_change = no

				limit = {
					real_tier = COUNT
					location = { religion = attacker }
					holder_scope = { same_realm = defender }
				}

				usurp_title_plus_barony_if_unlanded = {
					target = attacker
					type = revolt
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = -500
			piety = -500
			clr_character_flag = has_current_revolt

			religion_authority = {
				name = lost_heretic_revolt
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			clear_wealth = yes
			imprison = defender
			clr_character_flag = heretic_revolter

			hidden_effect = {
				add_character_modifier = {
					name = broken_spirit
					years = 10
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 50
			piety = 100
			clr_character_flag = has_current_revolt

			religion_authority = {
				name = won_heretic_revolt
			}

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

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			imprison = defender
			clr_character_flag = heretic_revolter

			hidden_effect = {
				add_character_modifier = {
					name = broken_spirit
					years = 10
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100
			piety = 200
			clr_character_flag = has_current_revolt

			religion_authority = {
				name = won_heretic_revolt
			}

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

				add_character_modifier = {
					name = crushed_major_revolt
					years = 10
				}
			}
		}
	}

	on_reverse_demand_title = {
		hidden_effect = {
			location = {
				add_province_modifier = {
					name = rebellion_heretic_province
					years = 5
				}

				province_event = {
					id = TOG.1032
					days = 1
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(heretic_revolt): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	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 = 150
	}
}

# 36: The CB used by "Religious revolter" characters
religious_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365

	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

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

	is_valid_title = {
		# Attacker
		ROOT = {
			NOT = { religion = FROM }
		}

		# CK2Plus
		any_direct_de_jure_vassal_title = {
			location = {
				religion = ROOT
			}

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

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] [Root.Religion.GetName] religious revolt for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		attacker = {
			show_scope_change = no

			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = defender

			gain_all_occupied_titles = {
				who = defender
				type = revolt
			}

			clr_character_flag = religious_revolter

			# CK2Plus
			hidden_effect = {
				wealth = 100

				if = {
					limit = { is_lowborn = yes }
					dynasty = father_bastard
				}
			}
		}

		# CK2Plus
		dejure_liege_title  = {
			show_scope_change = no

			any_de_jure_vassal_title = {
				show_scope_change = no

				limit = {
					real_tier = COUNT
					location = { religion = attacker }
					holder_scope = { same_realm = defender }
				}

				usurp_title_plus_barony_if_unlanded = {
					target = attacker
					type = revolt
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = -500
			piety = -500
			clr_character_flag = has_current_revolt

			religion_authority = {
				name = lost_religious_revolt
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			imprison = defender
			clr_character_flag = religious_revolter

			hidden_effect = {
				add_character_modifier = {
					name = broken_spirit
					years = 10
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 50
			piety = 100
			clr_character_flag = has_current_revolt

			religion_authority = {
				name = won_religious_revolt
			}

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

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			imprison = defender
			clr_character_flag = religious_revolter

			hidden_effect = {
				add_character_modifier = {
					name = broken_spirit
					years = 10
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100
			piety = 200
			clr_character_flag = has_current_revolt

			religion_authority = {
				name = won_religious_revolt
			}

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

				add_character_modifier = {
					name = crushed_revolt
					years = 10
				}
			}
		}
	}

	on_reverse_demand_title = {
		hidden_effect = {
			location = {
				add_province_modifier = {
					name = rebellion_religion_province
					years = 5
				}

				province_event = {
					id = TOG.1072
					days = 1
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(religious_revolt): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	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 = 150
	}
}

# 37: The CB used by "Nationalist Revolter" characters
liberation_revolt = {
	name = CB_NAME_LIBERATION_REVOLT
	war_name = WAR_NAME_LIBERATION_REVOLT
	sprite = 16
	truce_days = 365

	hostile_against_others = yes
	check_de_jure_tier = KING # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character
	press_claim = yes
	can_ask_to_join_war = no
	is_independence = yes

	can_use = {
		# Attacker
		ROOT = {
			has_character_flag = nationalist_rebel
			NOT = { has_character_modifier = holy_truce_timer }
		}

		# Defender
		FROM = {
			NOT = { has_character_modifier = holy_truce_timer }
		}
	}

	can_use_title = {
		real_tier = KING
		culture = ROOT
	}

	is_valid = {
		# Attacker
		ROOT = {
			NOT = { culture = FROM }
			has_character_flag = nationalist_rebel
		}
	}

	is_valid_title = {
		# Defender
		FROM = {
			num_of_realm_counties = {
				value >= 1
				title = PREV
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		log = "CB(liberation_revolt): VICTORY: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		attacker = {
			show_scope_change = no

			# Create new dynasty if the revolter is lowborn
			hidden_effect = {
				if = {
					limit = { is_lowborn = yes }
					dynasty = father_bastard
				}

				if = {
					limit = { has_character_flag = began_revolt }
					wealth = 100
				}
			}

			# Give liberator nickname
			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_liberator
			}

			prestige = 500
			hidden_effect = { disband_event_forces = nationalist_rebels }
		}

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

		defender = {
			show_scope_change = no

			prestige = -500
			clr_character_flag = has_current_revolt
		}

		hidden_tooltip = {
			any_attacker = {
				limit = {
					has_character_flag = nationalist_rebel

					NOR = {
						character = attacker
						is_vassal_or_below_of = attacker
					}
				}

				clr_character_flag = nationalist_rebel
				set_character_flag = will_vassalize

				if = {
					limit = { has_any_opinion_modifier = liberation_revolt_against }

					random_opinion_modifier_target = {
						limit = {
							reverse_has_opinion_modifier = {
								name = liberation_revolt_against
								who = PREV
							}
						}

						reverse_remove_opinion = {
							name = liberation_revolt_against
							who = PREV
						}

						log = "----> [Prev.GetBestName] is a rebel who has won against [This.GetBestName] (Liberation Revolt)"
					}
				}
			}

			attacker = {
				any_realm_lord = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { character = PREV }
					}

					clr_character_flag = nationalist_rebel
					set_character_flag = will_vassalize

					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }

						random_opinion_modifier_target = {
							limit = {
								reverse_has_opinion_modifier = {
									name = liberation_revolt_against
									who = PREV
								}
							}

							reverse_remove_opinion = {
								name = liberation_revolt_against
								who = PREV
							}

							log = "----> [Prev.GetBestName] is a rebel who has won against [This.GetBestName] (Liberation Revolt)"
						}
					}
				}
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] [Root.Culture.GetName] liberation revolt of [This.GetName] against [From.GetBestName] (ID: [From.GetID])"

		# Tooltips for the CB results
		attacker = {
			show_scope_change = no

			custom_tooltip = { text = LIBERATION_TOOLTIP }
		}

		if = {
			limit = {
				has_holder = no
				any_previous_holder = { always = yes }
				attacker = { num_of_count_titles < 5 }
			}

			attacker = {
				show_scope_change = no

				custom_tooltip = { text = LIBERATION_TOOLTIP2 }
			}
		}

		any_attacker = {
			show_scope_change = no

			limit = {
				has_character_flag = nationalist_rebel

				NOR = {
					character = attacker
					is_vassal_or_below_of = attacker
				}
			}

			custom_tooltip = { text = LIBERATION_TOOLTIP3 }
		}

		attacker = {
			show_scope_change = no

			any_realm_lord = {
				limit = { has_character_flag = nationalist_rebel }

				custom_tooltip = { text = LIBERATION_TOOLTIP3 }
			}
		}

		hidden_effect = {
			# Usurp any titles in the kingdom held by the top liege or any vassal who is
			# his culture, not the attacker's culture group, or their capital is elsewhere
			any_de_jure_vassal_title = {
				limit = {
					real_tier = DUKE

					holder_scope = {
						same_realm = defender

						OR = {
							character = defender
							culture = defender

							NOR = {
								culture_group = attacker

								capital_scope = {
									de_jure_liege_or_above = PREVPREVPREV
								}
							}
						}
					}
				}

				set_title_flag = nationalist_usurp
			}

			any_de_jure_vassal_title = {
				limit = {
					real_tier = BARON
					is_nomadic = no

					holder_scope = {
						same_realm = defender

						OR = {
							character = defender
							culture = defender

							NOR = {
								culture_group = attacker

								capital_scope = {
									de_jure_liege_or_above = PREVPREVPREV
								}
							}
						}
					}
				}

				log = "----> [Root.GetBestName] usurps the [This.GetFullName] (Liberation Revolt)"

				usurp_title = {
					target = attacker
					type = revolt
				}
			}

			any_de_jure_vassal_title = {
				limit = {
					real_tier = COUNT

					holder_scope = {
						same_realm = defender

						OR = {
							character = defender
							culture = defender

							NOR = {
								culture_group = attacker

								capital_scope = {
									de_jure_liege_or_above = PREVPREVPREV
								}
							}
						}
					}
				}

				log = "----> [Root.GetBestName] usurps the [This.GetFullName] (Liberation Revolt)"

				usurp_title = {
					target = attacker
					type = revolt
				}
			}

			any_de_jure_vassal_title = {
				limit = { has_title_flag = nationalist_usurp }

				clr_title_flag = nationalist_usurp
				log = "----> [Root.GetBestName] usurps the [This.GetFullName] (Liberation Revolt)"

				usurp_title = {
					target = attacker
					type = revolt
				}
			}
		}

		attacker = {
			show_scope_change = no

			# Take kingdom title if it is owned by the top liege or a vassal
			if = {
				limit = {
					PREV = {
						has_holder = yes

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

				log = "----> [Root.GetBestName] usurps the [Prev.GetFullName] (Liberation Revolt)"

				usurp_title = {
					target = PREV
					type = revolt
				}
			}

			# Take kingdom title if nobody holds it (but it was previously created)
			# and at least five counties are held
			# TODO: Perhaps check how many rulers below could be vassalised and use that number instead?
			if = {
				limit = {
					num_of_count_titles >= 5

					PREV = {
						has_holder = no
						any_previous_holder = { always = yes }
					}
				}

				usurp_title = {
					target = PREV
					type = revolt
				}
			}
		}

		hidden_effect = {
			# Vassalize any lower tier vassals of the same culture group
			any_de_jure_vassal_title = {
				limit = {
					holder_scope = {
						top_liege = { character = defender }
						lower_real_tier_than = attacker

						NOR = {
							any_liege = {
								culture_group = attacker
								NOT = { character = defender }
							}
						}
					}
				}

				holder_scope = {
					log = "----> [This.GetBestName] is vassalized by [Root.GetBestName] (Liberation Revolt)"
					set_defacto_liege = attacker
				}
			}

			# Usurp any vassals who cannot be vassalized
			any_de_jure_vassal_title = {
				limit = {
					holder_scope = {
						top_liege = { character = defender }
						NOT = { lower_real_tier_than = attacker }
					}
				}

				log = "----> [Root.GetBestName] usurps the [This.GetFullName] (Liberation Revolt)"

				usurp_title_plus_barony_if_unlanded = {
					target = attacker
					type = revolt
				}
			}
		}

		attacker = {
			show_scope_change = no

			set_defacto_liege = THIS
			clr_character_flag = nationalist_rebel
		}

		# Vassalize any allies and set opinions
		hidden_effect = {
			any_attacker = {
				limit = {
					has_character_flag = will_vassalize

					NOR = {
						character = attacker
						is_vassal_or_below_of = attacker
					}
				}

				log = "----> [This.GetBestName] is a fellow rebel and is thus vassalized to [Root.GetBestName] (Liberation Revolt)"
				clr_character_flag = will_vassalize

				if = {
					limit = { lower_real_tier_than = attacker }
					set_defacto_liege = attacker
				}
				else = {
					opinion = {
						name = in_non_aggression_pact
						who = attacker
						years = 20
						origin_description = HELPED_WIN_REVOLT
					}

					reverse_opinion = {
						name = in_non_aggression_pact
						who = attacker
						years = 20
						origin_description = HELPED_WIN_REVOLT
					}
				}

				reverse_opinion = {
					name = opinion_very_grateful
					who = attacker
					years = 20
				}

				opinion = {
					name = opinion_very_grateful
					who = attacker
					years = 20
				}
			}

			attacker = {
				any_realm_lord = {
					limit = { has_character_flag = will_vassalize }

					log = "----> [This.GetBestName] is a fellow rebel and is thus vassalized to [Root.GetBestName] (Liberation Revolt)"
					clr_character_flag = will_vassalize

					if = {
						limit = { lower_real_tier_than = attacker }
						set_defacto_liege = attacker
					}
					else = {
						opinion = {
							name = in_non_aggression_pact
							who = attacker
							years = 20
							origin_description = HELPED_WIN_REVOLT
						}

						reverse_opinion = {
							name = in_non_aggression_pact
							who = attacker
							years = 20
							origin_description = HELPED_WIN_REVOLT
						}
					}

					reverse_opinion = {
						name = opinion_very_grateful
						who = attacker
						years = 20
					}

					opinion = {
						name = opinion_very_grateful
						who = attacker
						years = 20
					}
				}
			}

			attacker = {
				# Destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }

					unsafe_destroy_landed_title = THIS
				}

				# Make sure revolter doesn't have an unacceptable government type
				if = {
					limit = { has_character_flag = began_revolt }

					clr_character_flag = began_revolt
					landless_government_check_effect = yes
				}
			}

			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		log = "CB(liberation_revolt): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		# Re-vassalize rebels
		hidden_effect = {
			any_attacker = {
				limit = {
					has_character_flag = nationalist_rebel

					NOR = {
						has_character_flag = began_revolt
						is_vassal_or_below_of = attacker
						character = attacker
					}
				}

				clr_character_flag = nationalist_rebel
				participation_scaled_prestige = -200

				reverse_opinion = {
					name = opinion_civil_war_loser
					who = FROM
					years = 5
				}

				opinion = {
					name = opinion_coerced_into_leaving_faction
					who = FROM
					years = 5
				}

				if = {
					limit = { has_any_opinion_modifier = liberation_revolt_against }

					random_opinion_modifier_target = {
						limit = {
							reverse_has_opinion_modifier = {
								name = liberation_revolt_against
								who = PREV
							}
						}

						log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

						reverse_remove_opinion = {
							name = liberation_revolt_against
							who = PREV
						}

						set_defacto_vassal = PREV
					}
				}

				else = {
					log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
					set_defacto_liege = FROM
				}
			}

			attacker = {
				# First re-vassalize vassals
				any_realm_lord = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}

					clr_character_flag = nationalist_rebel

					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }

						random_opinion_modifier_target = {
							limit = {
								reverse_has_opinion_modifier = {
									name = liberation_revolt_against
									who = PREV
								}
							}

							log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

							reverse_remove_opinion = {
								name = liberation_revolt_against
								who = PREV
							}

							set_defacto_vassal = PREV
						}
					}
					else = {
						log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
				}

				# Then destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }

					owner = {
						any_courtier = {
							banish = yes
						}
					}

					unsafe_destroy_landed_title = THIS
				}

				if = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}

					clr_character_flag = nationalist_rebel

					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }

						random_opinion_modifier_target = {
							limit = {
								reverse_has_opinion_modifier = {
									name = liberation_revolt_against
									who = PREV
								}
							}

							log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

							reverse_remove_opinion = {
								name = liberation_revolt_against
								who = PREV
							}

							set_defacto_vassal = PREV
						}
					}
					else = {
						log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
				}
			}
		}

		defender = {
			show_scope_change = no

			clr_character_flag = has_current_revolt
			prestige = 200

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

		attacker = {
			prestige = -200

			hidden_effect = {
				disband_event_forces = nationalist_rebels
				disband_event_forces = nationalist_rebels_1
			}

			if = {
				limit = { has_character_flag = began_revolt }

				imprison = defender
				clr_character_flag = nationalist_rebel
				clr_character_flag = began_revolt

				hidden_effect = {
					reverse_opinion = {
						name = opinion_rebel_traitor
						who = defender
						years = 100
					}

					add_character_modifier = {
						name = broken_spirit
						years = 10
					}

					remove_trait = inspiring_leader
				}
			}
			else = {
				random_courtier = {
					limit = { has_character_flag = began_revolt }

					clr_character_flag = began_revolt
					imprison = defender

					hidden_effect = {
						reverse_opinion = {
							name = opinion_rebel_traitor
							who = defender
							years = 100
						}

						add_character_modifier = {
							name = broken_spirit
							years = 10
						}

						remove_trait = inspiring_leader
					}
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(liberation_revolt): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		defender = {
			clr_character_flag = has_current_revolt
			prestige = 200

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

				add_character_modifier = {
					name = crushed_major_revolt
					years = 10
				}
			}
		}

		# Re-vassalize rebels
		hidden_tooltip = {
			any_attacker = {
				limit = {
					has_character_flag = nationalist_rebel

					NOR = {
						has_character_flag = began_revolt
						is_vassal_or_below_of = attacker
						character = attacker
					}
				}

				clr_character_flag = nationalist_rebel
				participation_scaled_prestige = -200

				reverse_opinion = {
					name = opinion_civil_war_loser
					who = defender
					years = 5
				}

				opinion = {
					name = opinion_coerced_into_leaving_faction
					who = defender
					years = 5
				}

				imprison = defender

				if = {
					limit = { has_any_opinion_modifier = liberation_revolt_against }

					random_opinion_modifier_target = {
						limit = {
							reverse_has_opinion_modifier = {
								name = liberation_revolt_against
								who = PREV
							}
						}

						log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

						reverse_remove_opinion = {
							name = liberation_revolt_against
							who = PREV
						}

						set_defacto_vassal = PREV
					}
				}
				else = {
					log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
					set_defacto_liege = defender
				}
			}

			attacker = {
				# First re-vassalize vassals
				any_realm_lord = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}

					clr_character_flag = nationalist_rebel
					participation_scaled_prestige = -200

					reverse_opinion = {
						name = opinion_civil_war_loser
						who = defender
						years = 5
					}

					opinion = {
						name = opinion_coerced_into_leaving_faction
						who = defender
						years = 5
					}

					imprison = defender

					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }

						random_opinion_modifier_target = {
							limit = {
								reverse_has_opinion_modifier = {
									name = liberation_revolt_against
									who = PREV
								}
							}

							log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

							reverse_remove_opinion = {
								name = liberation_revolt_against
								who = PREV
							}

							set_defacto_vassal = PREV
						}
					}
					else = {
						log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}

				}

				# Then destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }
					unsafe_destroy_landed_title = THIS
				}

				if = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}

					clr_character_flag = nationalist_rebel

					reverse_opinion = {
						name = opinion_civil_war_loser
						who = defender
						years = 5
					}

					opinion = {
						name = opinion_coerced_into_leaving_faction
						who = defender
						years = 5
					}

					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }

						random_opinion_modifier_target = {
							limit = {
								reverse_has_opinion_modifier = {
									name = liberation_revolt_against
									who = PREV
								}
							}

							log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

							reverse_remove_opinion = {
								name = liberation_revolt_against
								who = PREV
							}

							set_defacto_vassal = PREV
						}
					}
					else = {
						log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
				}
			}
		}

		attacker = {
			show_scope_change = no

			prestige = -200

			hidden_effect = {
				disband_event_forces = nationalist_rebels
				disband_event_forces = nationalist_rebels_1
			}

			if = {
				limit = { has_character_flag = began_revolt }

				imprison = defender
				clr_character_flag = nationalist_rebel
				clr_character_flag = began_revolt

				hidden_effect = {
					reverse_opinion = {
						name = opinion_rebel_traitor
						who = defender
						years = 100
					}

					add_character_modifier = {
						name = broken_spirit
						years = 10
					}

					remove_trait = inspiring_leader
				}
			}
			else = {
				clr_character_flag = nationalist_rebel
				imprison = defender

				hidden_effect = {
					reverse_opinion = {
						name = opinion_rebel_traitor
						who = defender
						years = 5
					}
				}

				random_courtier = {
					limit = { has_character_flag = began_revolt }

					clr_character_flag = began_revolt
					imprison = defender

					hidden_effect = {
						reverse_opinion = {
							name = opinion_rebel_traitor
							who = defender
							years = 100
						}

						add_character_modifier = {
							name = broken_spirit
							years = 10
						}

						remove_trait = inspiring_leader
					}
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(liberation_revolt): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		# Re-vassalize rebels
		hidden_effect = {
			any_attacker = {
				limit = {
					has_character_flag = nationalist_rebel

					NOR = {
						has_character_flag = began_revolt
						is_vassal_or_below_of = attacker
						character = attacker
					}
				}

				clr_character_flag = nationalist_rebel
				participation_scaled_prestige = -200

				reverse_opinion = {
					name = opinion_civil_war_loser
					who = defender
					years = 5
				}

				opinion = {
					name = opinion_coerced_into_leaving_faction
					who = defender
					years = 5
				}

				if = {
					limit = { has_any_opinion_modifier = liberation_revolt_against }

					random_opinion_modifier_target = {
						limit = {
							reverse_has_opinion_modifier = {
								name = liberation_revolt_against
								who = PREV
							}
						}

						log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

						reverse_remove_opinion = {
							name = liberation_revolt_against
							who = PREV
						}

						set_defacto_vassal = PREV
					}
				}
				else = {
					log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
					set_defacto_liege = defender
				}
			}

			attacker = {
				# First re-vassalize vassals
				any_realm_lord = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}

					clr_character_flag = nationalist_rebel

					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }

						random_opinion_modifier_target = {
							limit = {
								reverse_has_opinion_modifier = {
									name = liberation_revolt_against
									who = PREV
								}
							}

							log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

							reverse_remove_opinion = {
								name = liberation_revolt_against
								who = PREV
							}

							set_defacto_vassal = PREV
						}
					}
					else = {
						log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = defender
					}
				}

				# Then destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }
					unsafe_destroy_landed_title = THIS
				}

				if = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}

					clr_character_flag = nationalist_rebel

					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }

						random_opinion_modifier_target = {
							limit = {
								reverse_has_opinion_modifier = {
									name = liberation_revolt_against
									who = PREV
								}
							}

							log = "----> [Prev.GetBestName] is a rebel who has been re-vassalized to [This.GetBestName] (Liberation Revolt)"

							reverse_remove_opinion = {
								name = liberation_revolt_against
								who = PREV
							}

							set_defacto_vassal = PREV
						}
					}
					else = {
						log = "----> [This.GetBestName] is a rebel who has been re-vassalized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = defender
					}
				}
			}

			defender = {
				clr_character_flag = has_current_revolt
			}

			attacker = {
				disband_event_forces = nationalist_rebels
				disband_event_forces = nationalist_rebels_1

				if = {
					limit = { has_character_flag = began_revolt }

					clr_character_flag = nationalist_rebel
					clr_character_flag = began_revolt
				}
				else = {
					random_courtier = {
						limit = { has_character_flag = began_revolt }
						clr_character_flag = began_revolt
					}
				}
			}
		}
	}

	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
	}
}

