civil_war = {
	# A generic civil war triggered by a failed coup or purge
	name = CB_NAME_CIVIL_WAR
	war_name = WAR_NAME_CIVIL_WAR
	sprite = 3
	truce_days = 0
	hostile_against_others = no
	is_permanent = yes
	is_revolt_cb = no
	can_ask_to_join_war = yes
	can_call_vassals = yes
	
	press_claim = yes
	
	can_use = {
		# Triggered by event only
		OR = {
			ROOT = { has_character_flag = flag_launching_civil_war }
			FROM = { has_character_flag = flag_launching_civil_war }
		}
	}
	
	can_use_title = {
		OR = {
			is_primary_holder_title = yes
			ROOT = { plot_target_title = { title = PREVPREV } }
		}
	}
	
	on_add = {
		log = "[Root.GetBestName] declared war on [From.GetBestName] using the CB civil_war." 
		# Remove initiating flags
		ROOT = { clr_character_flag = flag_launching_civil_war }
		FROM = { clr_character_flag = flag_launching_civil_war }
		
		# If FROM and ROOT share the same ideology, change that ideology's moral authority.
		if = {
			limit = {
				FROM = { primary_title = { tier = EMPEROR } }
				FROM = { religion = ROOT }
			}
			FROM = { religion_authority = { modifier = allowed_civil_war_empire } }
		}
		if = {
			limit = {
				FROM = { primary_title = { tier = KING } }
				FROM = { religion = ROOT }
			}
			FROM = { religion_authority = { modifier = allowed_civil_war_kingdom } }
		}
		if = {
			limit = {
				FROM = { primary_title = { tier = DUKE } }
				FROM = { religion = ROOT }
			}
			FROM = { religion_authority = { modifier = allowed_civil_war_duchy } }
		}
		if = {
			limit = {
				FROM = { primary_title = { tier = COUNT } }
				FROM = { religion = ROOT }
			}
			FROM = { religion_authority = { modifier = allowed_civil_war_county } }
		}
		
		# Downgrade FROM's public order
		FROM = {
			any_demesne_title = { limit = { has_law = public_order_1 } }
			add_law = public_order_0
		}
	}
	
	on_success = {
		log = "[Root.GetBestName] won the civil_war war against [From.GetBestName]."
		# The rebels have won!
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		
		# Determine incoming government type
		if = {
			limit = {
				# These should be preserved
				ROOT = {
					NOT = { government = merchant_republic_government }
					NOT = { government = pirate_kingdom }
					NOT = { government = pirate_republic }
					NOT = { government = hivemind_government }
					NOT = { government = colonial_government_government }
				}
			}
			# Oterhwise, the incoming government should use ROOT's power base and FROM's legitimacy type
			if = {
				limit = { FROM = { is_hereditary_trigger = yes } }
				if = {
					limit = { ROOT = { is_military_trigger = yes } }
					ROOT = { set_government_type = military_monarchy }
				}
				if = {
					limit = { ROOT = { is_corporate_trigger = yes } }
					ROOT = { set_government_type = corporate_monarchy }
				}
				if = {
					limit = { ROOT = { is_administrative_trigger = yes } }
					ROOT = { set_government_type = administrative_monarchy }
				}
			}
			if = {
				limit = { FROM = { is_elective_trigger = yes } }
				if = {
					limit = { ROOT = { is_military_trigger = yes } }
					ROOT = { set_government_type = military_republic }
				}
				if = {
					limit = { ROOT = { is_corporate_trigger = yes } }
					ROOT = { set_government_type = corporate_republic }
				}
				if = {
					limit = { ROOT = { is_administrative_trigger = yes } }
					ROOT = { set_government_type = administrative_republic }
				}
			}
			if = {
				limit = { FROM = { is_appointed_trigger = yes } }
				if = {
					limit = { ROOT = { is_military_trigger = yes } }
					ROOT = { set_government_type = military_directorate }
				}
				if = {
					limit = { ROOT = { is_corporate_trigger = yes } }
					ROOT = { set_government_type = corporate_directorate }
				}
				if = {
					limit = { ROOT = { is_administrative_trigger = yes } }
					ROOT = { set_government_type = administrative_directorate }
				}
			}
		}
		
		# Remove coup flags
		ROOT = { clr_character_flag = flag_failed_coup }
		FROM = { clr_character_flag = flag_failed_purge }
	}
	
	on_success_title = {
		usurp_title_plus_barony_if_unlanded = ROOT # Messy solution, should be cleaned up later
		
		# New provisional government takes office.
		if = {
			limit = { ROOT = { is_merchant_republic = no } }
			add_law = succ_feudal_elective
			add_law = true_cognatic_succession
		}
		if = {
			limit = {
				crownlaw_title = { title = PREV }
				OR = {
					has_law = crown_term_length_0
					has_law = crown_term_length_1
					has_law = crown_term_length_2
					has_law = crown_term_length_3
				}
			}
			add_law = crown_term_length_4
		}
		if = {
			limit = {
				NOT = { crownlaw_title = { title = PREV } }
				OR = {
					has_law = local_term_length_0
					has_law = local_term_length_1
					has_law = local_term_length_2
					has_law = local_term_length_3
				}
			}
			add_law = local_term_length_4
		}
	}
	
	on_fail = {
		# Initiator fails to achieve whatever it was they were after
		ROOT = {
			cancel_plot = plot_coup
			cancel_plot = plot_justified_coup
		}
		
		# Remove coup flags
		ROOT = { clr_character_flag = flag_failed_coup }
		FROM = { clr_character_flag = flag_failed_purge }
	}
	
	on_reverse_demand = {
		# Ruler has won!
		FROM = { prestige = 100 }
		# Imprison all rebels
		any_attacker = { imprison = FROM }
		# Imprison all of ROOT's co-conspirators (if not rebels)
		if = {
			limit = {
				ROOT = {
					OR = {
						has_plot = plot_coup
						has_plot = plot_justified_coup
					}
					plot_target_char = { character = FROM }
				}
			}
			ROOT = {
				any_plot_backer = {
					limit = { NOT = { war_with = FROM } }
					imprison = FROM
				}
			}
		}
		# All anti-ruler vassals are automatically no longer anti-ruler
		FROM = {
			any_vassal = {
				limit = { trait = anti_ruler }
				hidden_tooltip = {
					remove_trait = anti_ruler
					character_event = { id = laws.5 }
				}
			}
			any_courtier = {
				limit = { trait = anti_ruler }
				hidden_tooltip = {
					remove_trait = anti_ruler
					character_event = { id = laws.5 }
				}
			}
		}
		# If FROM is in a constitutional crisis, this resolves it in their favor.
		if = {
			limit = { FROM = { has_character_modifier = constitutional_crisis } }
			FROM = { character_event = { id = laws.1000 tooltip = "EVTTOOLTIPLAWS1000" } }
		}
		# Coups fail immediately
		ROOT = {
			cancel_plot = plot_coup
			cancel_plot = plot_justified_coup
		}
		
		# If FROM and ROOTs' ideologies are different, the former gets a bonus and
		# the latter takes a hit
		
		# Remove coup flags
		ROOT = { clr_character_flag = flag_failed_coup }
		FROM = { clr_character_flag = flag_failed_purge }
	}
	
	on_attacker_leader_death = {
		if = {
			limit = { ROOT = { has_character_flag = flag_failed_coup } }
			end_war = invalid
		}
	}
	
	on_defender_leader_death = {
		if = {
			limit = { FROM = { has_character_flag = flag_failed_purge } }
			end_war = invalid
	}

	attacker_ai_victory_worth = {
		factor = -1
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
}

cb_military_campaign = {
	# This CB is used by military vassals against enemies of their lieges.
	name = CB_NAME_MILITARY_CAMPAIGN
	war_name = WAR_NAME_MILITARY_CAMPAIGN
	sprite = 1
	truce_days = 0
	hostile_against_others = no
	can_ask_to_join_war = yes
	is_permanent = yes
	
	on_add = { log = "[Root.GetBestName] declared war on [From.GetBestName] using the CB cb_military_campaign." }
	
	can_use = {
		# Must be a military vassal
		ROOT = {
			independent = no
			is_military_trigger = yes
			NOT = { is_liege_or_above = FROM }
			NOT = { has_character_flag = flag_revolter }
		}
		FROM = { NOT = { is_liege_or_above = ROOT } }
		ROOT = { liege = { war_with = FROM } }
	}
	
	is_valid = {
		ROOT = {
			independent = no
			is_military_trigger = yes
			NOT = { is_liege_or_above = FROM }
		}
		FROM = { NOT = { is_liege_or_above = ROOT } }
		ROOT = { liege = { any_current_enemy = { character = FROM } } }
	}
	
	on_success = {
		log = "[Root.GetBestName] won the cb_military_campaign war against [From.GetBestName]."
		# If defender is already a prisoner, release them so they can be moved
		if = {
			limit = { FROM = { prisoner = yes } }
			FROM = { prisoner = no }
		}
		
		ROOT = {
			hidden_tooltip = { character_event = { id = campaign.0 } }
			# Defender is imprisoned by ROOT's liege
			liege = { FROM = { imprison = PREV } }
			# Root gains good modifier
			add_character_modifier = { name = successful_campaign duration = 3650 hidden = no }
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = { prestige = -100 }
	}
	
	on_fail = {
		# Attacker loses prestige
		ROOT = {
			hidden_tooltip = { character_event = { id = campaign.2 } }
			prestige = -100
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			participation_scaled_prestige = 100
		}
	}
	
	on_reverse_demand = {
		# Attacker loses LOTS of prestige, gets bad modifier
		ROOT = {
			hidden_tooltip = { character_event = { id = campaign.4 } }
			add_character_modifier = { name = disastrous_campaign duration = 3650 hidden = no }
			prestige = -200
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			participation_scaled_prestige = 100
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	attacker_ai_defeat_worth = {
		factor = 50
		modifier = {
			factor = 2
			ROOT = { trait = proud }
		}
		modifier = {
			factor = 0.5
			ROOT = { trait = humble }
		}
		modifier = {
			factor = 2
			ROOT = { trait = brave }
		}
		modifier = {
			factor = 0.5
			ROOT = { trait = craven }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 100 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 90 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 80 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 70 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 60 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 50 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 40 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 30 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 20 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { opinion = { who = liege value = 10 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -9 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -19 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -29 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -39 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -49 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -59 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -69 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -79 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -89 } } }
		}
		modifier = {
			factor = 0.8
			ROOT = { NOT = { opinion = { who = liege value = -99 } } }
		}
		modifier = {
			factor = 0.2
			ROOT = {
				OR = {
					any_plot = plot_coup
					any_plot = plot_justified_coup
				}
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				OR = {
					any_plot = plot_coup
					any_plot = plot_justified_coup
				}
				plot_power = 1
			}
		}
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 10
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.1 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.2 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.3 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.4 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.5 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.6 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.7 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.8 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 0.9 } }
		}
		modifier = {
			factor = 2
			ROOT = { liege = { religion_authority = 1 } }
		}
		modifier = {
			factor = 2
			ROOT = { trait = brave }
		}
		modifier = {
			factor = 0.5
			ROOT = { trait = craven }
		}
		modifier = {
			factor = 2
			ROOT = { trait = disciplined }
		}
		modifier = {
			factor = 0.5
			ROOT = { trait = carefree }
		}
		modifier = {
			factor = 2
			ROOT = { trait = proud }
		}
		modifier = {
			factor = 2
			ROOT = { trait = ambitious }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 100 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 90 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 80 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 70 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 60 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 50 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 40 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 30 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 20 } }
		}
		modifier = {
			factor = 0.8
			ROOT = { opinion = { who = FROM value = 10 } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -9 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -19 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -29 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -39 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -49 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -59 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -69 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -79 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -89 } } }
		}
		modifier = {
			factor = 1.25
			ROOT = { NOT = { opinion = { who = FROM value = -99 } } }
		}
	}
}

# Attempt to become the new regent by force
cb_seize_regency = {
	name = CB_NAME_SEIZE_REGENCY
	war_name = WAR_NAME_SEIZE_REGENCY
	sprite = 1
	truce_days = 1825
	hostile_against_others = yes
	attacker_can_call_allies = yes
	defender_can_call_allies = yes
	can_ask_to_join_war = yes
	is_permanent = yes
	
	on_add = { log = "[Root.GetBestName] declared war on [From.GetBestName] using the CB cb_seize_regency." }
	
	can_use = {
		ROOT = { same_liege = FROM }
		FROM = { has_minor_title = title_regent }
	}
	
	can_use_gui = {
		ROOT = {
			liege = {
				OR = {
					religion = terran_imperial
					religion = neo_feudal
					religion = space_pirate
					NOT = { religion_authority = 0.5 }
				}
			}
		}
	}
	
	is_valid = {
		ROOT = { same_liege = FROM }
		FROM = { has_minor_title = title_regent }
	}
	
	on_success = {
		log = "[Root.GetBestName] won the cb_seize_regency war against [From.GetBestName]."
		# Take over the regency, duh
		ROOT = { give_minor_title = title_regent }
		
		# Imprison former regent
		FROM = { imprison = ROOT }
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = { prestige = -100 }
	}
	
	on_fail = {
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			participation_scaled_prestige = 100
		}
		ROOT = { prestige = -100 }
	}
	
	on_reverse_demand = {
		any_attacker = {
			limit = { same_liege = FROM }
			imprison = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			participation_scaled_prestige = 100
		}
		ROOT = { prestige = -100 }
	}

	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
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				NOT = { trait = ambitious }
				opinion = { who = FROM value = 0 }
			}
		}
	}
}

democratic_rebellion = {
	name = CB_NAME_REBELLION
	war_name = WAR_NAME_REBELLION
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = yes
	defender_can_call_allies = yes
	can_ask_to_join_war = yes
	
	on_add = { log = "[Root.GetBestName] declared war on [From.GetBestName] using the CB democratic_rebellion." }
	
	can_use = { ROOT = { has_character_flag = flag_revolter } }
	
	is_valid = { ROOT = { has_character_flag = flag_revolter } }
	
	on_success = {
		log = "[Root.GetBestName] won the democratic_rebellion war against [From.GetBestName]."
		ROOT = {
			prestige = 100
			clr_character_flag = flag_revolter
			hidden_tooltip = {
				any_courtier = {
					opinion = { who = ROOT modifier = opinion_hero_of_the_revolution }
					reverse_opinion = { who = ROOT modifier = opinion_aided_my_revolution }
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = 100
			opinion = { who = ROOT modifier = opinion_hero_of_the_revolution }
			reverse_opinion = { who = ROOT modifier = opinion_aided_my_revolution }
		}
		FROM = {
			prestige = -100
			hidden_tooltip = {
				any_realm_province = {
					limit = {
						owner = { is_liege_or_above = PREVPREV }
						religion = ROOT
					}
					add_province_modifier = { name = successful_revolt days = 365 }
				}
			}
		}
		# Moral authority changes
		if = {
			limit = {
				FROM = {
					primary_title = { NOT = { higher_tier_than = duke } }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = fell_to_minor_rebellion years = 10 } }
			ROOT = {
				religion_authority = { modifier = successful_minor_rebellion years = 10 }
				piety = 100
			}
		}
		if = {
			limit = {
				FROM = {
					primary_title = { higher_tier_than = duke }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = fell_to_major_rebellion years = 60 } }
			ROOT = {
				religion_authority = { modifier = successful_major_rebellion years = 60 }
				piety = 500
			}
		}
		# Rebels establish new government, seize control
		ROOT = {
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = FROM
			if = {
				limit = { is_government_potential = merchant_republic_government }
				set_government_type = merchant_republic_government
			}
			if = {
				limit = { is_government_potential = pirate_republic }
				set_government_type = pirate_republic
			}
			if = {
				limit = {
					NOT = { is_government_potential = merchant_republic_government }
					NOT = { is_government_potential = pirate_republic }
				}
				set_government_type = corporate_republic
			}
		}
	}
	
	on_add = {
		# Downgrade FROM's public order
		FROM = {
			any_demesne_title = { limit = { has_law = public_order_1 } }
			add_law = public_order_0
		}
	}

	on_fail = {
		ROOT = { clr_character_flag = flag_revolter }
		FROM = {
			prestige = 100
			hidden_tooltip = { opinion = { modifier = opinion_rebel_traitor  who = ROOT months = 1200 } }
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = -100
			hidden_tooltip = { reverse_opinion = { who = FROM modifier = opinion_traitor months = 240 } }
		}
		ROOT = {
			imprison = FROM 
			hidden_tooltip = { add_character_modifier = { name = broken_spirit days = -1 } }
		}
	}

	on_reverse_demand = {
		ROOT = { clr_character_flag = flag_revolter }
		# Moral authority changes
		if = {
			limit = {
				FROM = {
					primary_title = { NOT = { higher_tier_than = duke } }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = crushed_minor_rebellion years = 10 } }
			ROOT = { religion_authority = { modifier = failed_minor_rebellion years = 10 } }
		}
		if = {
			limit = {
				FROM = {
					primary_title = { higher_tier_than = duke }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = crushed_major_rebellion years = 60 } }
			ROOT = { religion_authority = { modifier = failed_major_rebellion years = 60 } }
		}
		FROM = {
			prestige = 200
			hidden_tooltip = {
				opinion = { modifier = opinion_rebel_traitor  who = ROOT months = 1200 }
				add_character_modifier = { name = crushed_revolt days = 3650 }
				any_realm_province = {
					limit = {
						any_province_lord = { character = FROM }
						OR = {
							religion = terran_republican
							religion = terran_federalist
							religion = colonial_separatist
							owner = { is_corporate_trigger = yes }
						}
					}
					add_province_modifier = { name = "failed_revolt" days = 3650 }
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = -100
			imprison = FROM
			hidden_tooltip = { reverse_opinion = { who = FROM modifier = opinion_traitor months = 240 } }
		}
		ROOT = {
			imprison = FROM 
			hidden_tooltip = { add_character_modifier = { name = broken_spirit days = -1 } }
		}
	}

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

autocratic_rebellion = {
	name = CB_NAME_REBELLION
	war_name = WAR_NAME_REBELLION
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = yes
	defender_can_call_allies = yes
	can_ask_to_join_war = yes
	
	on_add = { log = "[Root.GetBestName] declared war on [From.GetBestName] using the CB autocratic_rebellion." }
	
	can_use = { ROOT = { has_character_flag = flag_revolter } }
	
	is_valid = { ROOT = { has_character_flag = flag_revolter } }
	
	on_success = {
		log = "[Root.GetBestName] won the autocratic_rebellion war against [From.GetBestName]."
		ROOT = {
			prestige = 100
			clr_character_flag = flag_revolter
			hidden_tooltip = {
				any_courtier = {
					opinion = { who = ROOT modifier = opinion_hero_of_the_revolution }
					reverse_opinion = { who = ROOT modifier = opinion_aided_my_revolution }
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = 100
			opinion = { who = ROOT modifier = opinion_hero_of_the_revolution }
			reverse_opinion = { who = ROOT modifier = opinion_aided_my_revolution }
		}
		FROM = {
			prestige = -100
			hidden_tooltip = {
				any_realm_province = {
					limit = {
						owner = { is_liege_or_above = PREVPREV }
						religion = ROOT
					}
					add_province_modifier = { name = successful_revolt days = 365 }
				}
			}
			# Set autocratic legitimacy
			any_demesne_title = {
				limit = { is_primary_holder_title_tier = yes }
				add_law = legitimacy_law_0
				add_law = local_legitimacy_law_0
			}
		}
		# Moral authority changes
		if = {
			limit = {
				FROM = {
					primary_title = { NOT = { higher_tier_than = duke } }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = fell_to_minor_rebellion years = 10 } }
			ROOT = {
				religion_authority = { modifier = successful_minor_rebellion years = 10 }
				piety = 100
			}
		}
		if = {
			limit = {
				FROM = {
					primary_title = { higher_tier_than = duke }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = fell_to_major_rebellion years = 60 } }
			ROOT = {
				religion_authority = { modifier = successful_major_rebellion years = 60 }
				piety = 500
			}
		}
		# Rebels establish new government, seize control
		ROOT = {
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = FROM
			if = {
				limit = { religion = space_pirate }
				set_government_type = pirate_kingdom
			}
			if = {
				limit = {
					OR = {
						religion = terran_imperial
						religion = neo_feudal
						religion = imperial_cult
						religion = bio_directionist
					}
				}
				set_government_type = military_monarchy
			}
			if = {
				limit = {
					NOT = { religion = space_pirate }
					NOT = { religion = terran_imperial }
					NOT = { religion = imperial_cult }
					NOT = { religion = bio_directionist }
					NOT = { religion = neo_feudal }
				}
				set_government_type = military_directorate
			}
		}
	}
	
	on_add = {
		# Downgrade FROM's public order
		FROM = {
			any_demesne_title = { limit = { has_law = public_order_1 } }
			add_law = public_order_0
		}
	}

	on_fail = {
		ROOT = { clr_character_flag = flag_revolter }
		FROM = {
			prestige = 100
			hidden_tooltip = { opinion = { modifier = opinion_rebel_traitor  who = ROOT months = 1200 } }
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = -100
			hidden_tooltip = { reverse_opinion = { who = FROM modifier = opinion_traitor months = 240 } }
		}
		ROOT = {
			imprison = FROM 
			hidden_tooltip = { add_character_modifier = { name = broken_spirit days = -1 } }
		}
	}

	on_reverse_demand = {
		ROOT = { clr_character_flag = flag_revolter }
		# Moral authority changes
		if = {
			limit = {
				FROM = {
					primary_title = { NOT = { higher_tier_than = duke } }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = crushed_minor_rebellion years = 10 } }
			ROOT = { religion_authority = { modifier = failed_minor_rebellion years = 10 } }
		}
		if = {
			limit = {
				FROM = {
					primary_title = { higher_tier_than = duke }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = crushed_major_rebellion years = 60 } }
			ROOT = { religion_authority = { modifier = failed_major_rebellion years = 60 } }
		}
		FROM = {
			prestige = 200
			hidden_tooltip = {
				opinion = { modifier = opinion_rebel_traitor  who = ROOT months = 1200 }
				add_character_modifier = { name = crushed_revolt days = 3650 }
				any_realm_province = {
					limit = {
						any_province_lord = { character = FROM }
						OR = {
							religion = terran_imperial
							religion = astrist
							religion = neo_feudal
							owner = { is_military_trigger = yes }
						}
					}
					add_province_modifier = { name = "failed_revolt" days = 3650 }
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = -100
			imprison = FROM
			hidden_tooltip = { reverse_opinion = { who = FROM modifier = opinion_traitor months = 240 } }
		}
		ROOT = {
			imprison = FROM 
			hidden_tooltip = { add_character_modifier = { name = broken_spirit days = -1 } }
		}
	}

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

theocratic_rebellion = {
	name = CB_NAME_REBELLION
	war_name = WAR_NAME_REBELLION
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = yes
	defender_can_call_allies = yes
	can_ask_to_join_war = yes
	
	on_add = { log = "[Root.GetBestName] declared war on [From.GetBestName] using the CB theocratic_rebellion." }
	
	can_use = { ROOT = { has_character_flag = flag_revolter } }
	
	is_valid = { ROOT = { has_character_flag = flag_revolter } }
	
	on_success = {
		log = "[Root.GetBestName] won the theocratic_rebellion war against [From.GetBestName]."
		ROOT = {
			prestige = 100
			clr_character_flag = flag_revolter
			hidden_tooltip = {
				any_courtier = {
					opinion = { who = ROOT modifier = opinion_hero_of_the_revolution }
					reverse_opinion = { who = ROOT modifier = opinion_aided_my_revolution }
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = 100
			opinion = { who = ROOT modifier = opinion_hero_of_the_revolution }
			reverse_opinion = { who = ROOT modifier = opinion_aided_my_revolution }
		}
		FROM = {
			prestige = -100
			hidden_tooltip = {
				any_realm_province = {
					limit = {
						owner = { is_liege_or_above = PREVPREV }
						religion = ROOT
					}
					add_province_modifier = { name = successful_revolt days = 365 }
				}
			}
		}
		# Moral authority changes
		if = {
			limit = {
				FROM = {
					primary_title = { NOT = { higher_tier_than = duke } }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = fell_to_minor_rebellion years = 10 } }
			ROOT = {
				religion_authority = { modifier = successful_minor_rebellion years = 10 }
				piety = 100
			}
		}
		if = {
			limit = {
				FROM = {
					primary_title = { higher_tier_than = duke }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = fell_to_major_rebellion years = 60 } }
			ROOT = {
				religion_authority = { modifier = successful_major_rebellion years = 50 }
				piety = 500
			}
		}
		ROOT = {
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = FROM
			set_government_type = administrative_directorate
		}
	}
	
	on_add = {
		# Downgrade FROM's public order
		FROM = {
			any_demesne_title = { limit = { has_law = public_order_1 } }
			add_law = public_order_0
		}
	}

	on_fail = {
		ROOT = { clr_character_flag = flag_revolter }
		FROM = {
			prestige = 100
			hidden_tooltip = { opinion = { modifier = opinion_rebel_traitor  who = ROOT months = 1200 } }
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = -100
			hidden_tooltip = { reverse_opinion = { who = FROM modifier = opinion_traitor months = 240 } }
		}
		ROOT = {
			imprison = FROM 
			hidden_tooltip = { add_character_modifier = { name = broken_spirit days = -1 } }
		}
	}

	on_reverse_demand = {
		ROOT = { clr_character_flag = flag_revolter }
		# Moral authority changes
		if = {
			limit = {
				FROM = {
					primary_title = { NOT = { higher_tier_than = duke } }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = crushed_minor_rebellion years = 10 } }
			ROOT = { religion_authority = { modifier = failed_minor_rebellion years = 10 } }
		}
		if = {
			limit = {
				FROM = {
					primary_title = { higher_tier_than = duke }
					NOT = { religion = ROOT }
				}
			}
			FROM = { religion_authority = { modifier = crushed_major_rebellion years = 60 } }
			ROOT = { religion_authority = { modifier = failed_major_rebellion years = 60 } }
		}
		FROM = {
			prestige = 200
			hidden_tooltip = {
				opinion = { modifier = opinion_rebel_traitor  who = ROOT months = 1200 }
				add_character_modifier = { name = crushed_revolt days = 3650 }
				any_realm_province = {
					limit = {
						any_province_lord = { character = FROM }
						OR = {
							religion = neo_socialist
							religion = orthodox
							religion = machine_cultist
							owner = { is_administrative = yes }
						}
					}
					add_province_modifier = { name = "failed_revolt" days = 3650 }
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = -100
			imprison = FROM
			hidden_tooltip = { reverse_opinion = { who = FROM modifier = opinion_traitor months = 240 } }
		}
		ROOT = {
			imprison = FROM 
			hidden_tooltip = { add_character_modifier = { name = broken_spirit days = -1 } }
		}
	}

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

# DAMN YOU PARADOX
cb_historical_independence_revolt = {
	name = CB_NAME_INDEPENDENCE
	war_name = WAR_NAME_FACTION_INDEPENDENCE
	sprite = 12
	truce_days = 3650
	hostile_against_others = no
	
	on_add = { log = "[Root.GetBestName] declared war on [From.GetBestName] using the CB cb_historical_independence." }
	
	can_use = {
		ROOT = {
			has_character_flag = flag_historical_independence_revolt_leader
			NOT = { any_war = { using_cb = cb_historical_independence_revolt } }
		}
	}
	
	on_add = {
		ROOT = { clr_character_flag = flag_historical_independence_revolt_leader }
	}
	
	is_valid = {
		ROOT = {
			OR = {
				independent = yes
				same_liege = FROM
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				liege = { character = PREV } # either independent
				liege = { FROM = { is_liege_or_above = PREV } } # or have shared liege
			}
		}
		FROM = {
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
			independent = yes
		}
	}

	on_success = {
		log = "[Root.GetBestName] won the cb_historical_independence_revolt war against [From.GetBestName]."
		hidden_tooltip = { ROOT = { clr_character_flag = flag_historical_independence_revolt_leader } }
		any_attacker = {
			if = {
				limit = {
					OR = {
						real_tier = emperor
						real_tier = king
					}
				}
				FROM = { religion_authority = { modifier = fell_to_major_rebellion years = 60 } }
				religion_authority = { modifier = successful_major_rebellion years = 60 }
			}
			if = {
				limit = {
					OR = {
						real_tier = duke
						real_tier = count
						real_tier = baron
					}
				}
				FROM = { religion_authority = { modifier = fell_to_minor_rebellion years = 10 } }
				religion_authority = { modifier = successful_minor_rebellion years = 10 }
			}
		}
		any_attacker = {
			set_defacto_liege = THIS
			hidden_tooltip = {
				participation_scaled_prestige = 100
				disband_event_forces = faction_revolters
				any_demesne_title = { add_weak_pressed_claim = FROM }
			}
		}
		FROM = {
			
			hidden_tooltip = { disband_event_forces = faction_loyalists }
		
			any_demesne_title = { # All titles
				limit = {
					OR = {
						tier = king
						tier = emperor
					}
					NOT = { has_law = centralization_0 }
				}
				if = {
					limit = { has_law = centralization_1 }
					add_law = centralization_0
				}
				if = {
					limit = { has_law = centralization_2 }
					add_law = centralization_1
				}
				if = {
					limit = { has_law = centralization_3 }
					add_law = centralization_2
				}
				if = {
					limit = { has_law = centralization_4 }
					add_law = centralization_3
				}
			}
		}
		
		# Eliminate the Infinite Money Property if present
		hidden_tooltip = { any_attacker = { character_event = { id = maintenance.550 } } }
	}

	on_fail = {
		hidden_tooltip = { ROOT = { clr_character_flag = flag_historical_independence_revolt_leader } }
		ROOT = {
			prestige = -50
#			prisoner = FROM
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		any_attacker = {
			limit = { character = ROOT }
			set_defacto_liege = FROM
			participation_scaled_prestige = -50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			set_defacto_liege = FROM
			hidden_tooltip = {
				participation_scaled_prestige = -50 
				disband_event_forces = faction_revolters
			}
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		hidden_tooltip = { ROOT = { clr_character_flag = flag_historical_independence_revolt_leader } }
		ROOT = {
			prestige = -100
			set_defacto_liege = FROM
			prisoner = FROM
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		
		any_attacker = {
			limit = { character = ROOT }
			set_defacto_liege = FROM
			participation_scaled_prestige = -100
			if = {
				limit = {
					OR = {
						real_tier = emperor
						real_tier = king
					}
				}
				FROM = { religion_authority = { modifier = crushed_major_rebellion years = 60 } }
				religion_authority = { modifier = failed_major_rebellion years = 60 }
			}
			if = {
				limit = {
					OR = {
						real_tier = duke
						real_tier = count
						real_tier = baron
					}
				}
				FROM = { religion_authority = { modifier = crushed_minor_rebellion years = 10 } }
				religion_authority = { modifier = failed_minor_rebellion years = 10 }
			}
		}
		
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			set_defacto_liege = FROM
			if = {
				limit = {
					OR = {
						real_tier = emperor
						real_tier = king
					}
				}
				FROM = { religion_authority = { modifier = crushed_major_rebellion years = 60 } }
				religion_authority = { modifier = failed_major_rebellion years = 60 }
			}
			if = {
				limit = {
					OR = {
						real_tier = duke
						real_tier = count
						real_tier = baron
					}
				}
				FROM = { religion_authority = { modifier = crushed_minor_rebellion years = 10 } }
				religion_authority = { modifier = failed_minor_rebellion years = 10 }
			}
			prisoner = FROM
			participation_scaled_prestige = -100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_independence_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		FROM = {
			prestige = 50
			hidden_tooltip = { disband_event_forces = faction_loyalists }
		}
	}

	# on_attacker_leader_death = {
		# hidden_tooltip = { any_defender = { letter_event = { id = 251 } } }

		# end_war = invalid
	# }

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