#########################
#     Migration CB
# 
#
#    Written by Zusk
#
########################

migration = {
	name = CB_NAME_MIGRATION
	war_name = WAR_NAME_MIGRATION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = no
	max_attacker_battle_score = 50 # Limit warscore from battles for attacker to prevent a peace without occupation
	capturing_defender_is_complete_victory = no # Capturing the defender as a prisoner is not considered an automatic victory for the attacker

	allow_distant = yes # Distant is calculated through the event that fires the CB

	allowed_to_target_tributaries = no

	on_add = {
		# Sets the flag for giving units & giving the provinces away if you win the war
		ROOT = { character_event = { id = zusk_event.12 } }
		# Gives troops for every province of your culture that you own
		ROOT = { character_event = { id = zusk_event.13 } }
		
		# Log migrations to check if MTTH for AI is good
		log = "Migratory Invasion from [Root.GetTitledName] to [From.GetTitledName] started."
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	can_use = {
		OR = {
			ai = yes # AI checks are done via the event that triggers the war
			# Conditions for the player
			AND = {
				NOT = { year = 800 } # The migration period ended around 800
				ROOT = {
					OR = {
						religion = tengri_pagan
						religion = slavic_pagan
						religion = germanic_pagan
						culture = alan
					}
					NOT = { has_character_modifier = launched_migration_war }
					NOT = { higher_tier_than = KING } # No Emperors
					NOT = { culture = FROM }
					independent = yes # No vassals
					war = no # Ensures that the events activate correctly  
				}
				FROM = {
					independent = yes
					war = no
				}
			}
		}
	}
	
	can_use_title = {
	}
	
	is_valid_title = {
	}

	on_success = {
		FROM = {  # Up to three bordering provinces that are occupied become the attackers religion and culture
			random_realm_province = {
				limit = {
					controlled_by = ROOT
				}
				culture = ROOT
				religion = ROOT
			}
			random_realm_province = {
				limit = {
					any_neighbor_province = {
						culture = ROOT 
						religion = ROOT
					}
					OR = {
						NOT = { religion = ROOT }
						NOT = { culture = ROOT }
					}
					controlled_by = ROOT
				}
				culture = ROOT
				religion = ROOT
			}
			random_realm_province = {
 				limit = {
					any_neighbor_province = {
						culture = ROOT 
						religion = ROOT
					}
					OR = {
						NOT = { religion = ROOT }
						NOT = { culture = ROOT }
					}
					controlled_by = ROOT
				}
				culture = ROOT
				religion = ROOT
			}
		}
		FROM = { # Any realm province that is bordering an occupied province is also taken, you don't get baronies though
			any_realm_province = {
				limit = {
					any_neighbor_province = {
						controlled_by = ROOT
					}
				}
				county = { usurp_title = ROOT }
			}
			prestige = -200
		}
		ROOT = { # Any occupied province is taken
			prestige = 200
			occupy_minors_of_occupied_settlements = FROM 
			gain_all_occupied_titles = { who = FROM type = invasion }
			
			# In case of nomadic provinces, create tribal settlements
			any_realm_province = {
				limit = {
					NOT = {
						any_province_holding = {
							NOT = { holding_type = nomad }
						}
					}
				}
				build_holding = { type = tribal }
			}
			
			# Give away old provinces
			character_event = { id = zusk_event.14 }
		}
		log = "Migratory Invasion from [Root.GetTitledName] to [From.GetTitledName] succeeded."
	}

	on_fail = {
		ROOT = {
			prestige = -50
			
			# Does not give away provinces but cleans up the migration_give_away flag used in the event
			character_event = { id = zusk_event.15 }
		}
		FROM = {
			prestige = 50
		}
		log = "Migratory Invasion from [Root.GetTitledName] to [From.GetTitledName] whitepeaced."
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 8.0
			}

			# Does not give away provinces but cleans up the migration_give_away flag used in the event
			character_event = { id = zusk_event.15 }
		}
		FROM = {
			prestige = 200
		}
		log = "Migratory Invasion from [Root.GetTitledName] to [From.GetTitledName] was lost."
	}

	on_attacker_leader_death = {
		# Does not give away provinces but cleans up the migration_give_away flag used in the event
		ROOT = { 
			character_event = { id = zusk_event.15 }
		}

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

		end_war = whitepeace
		log = "Migratory Invasion from [Root.GetTitledName] to [From.GetTitledName] invalidated."
	}

	attacker_ai_victory_worth = {
		factor = 500 # Worth more, to make wars drag on longer and make a winning AI get more provinces 
	}
	
	attacker_ai_defeat_worth = {
		factor = 50 # The migrating AI, on the other hand can more easily quit if it is beaten back early
	}

	defender_ai_victory_worth = {
		factor = 50
	}
	
	defender_ai_defeat_worth = {
		factor = 500
	}

	ai_will_do = { 
		factor = 0 # Fired from event for the AI
	}		
}

unification = {
	name = CB_NAME_UNIFICATION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = EMPEROR
	
	allowed_to_target_tributaries = no
	
	can_use = {
		FROM = {
			independent = yes
			higher_tier_than = COUNT
		}
	}

	can_use_title = {
		has_holder = no
		ROOT = { has_claim = PREV }
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	is_valid = {
		FROM = {
			independent = yes
			higher_tier_than = COUNT
		}
	}

	is_valid_title = {
		has_holder = no
		ROOT = { 
			has_claim = PREV
		}
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		hidden_tooltip = {
			# Julius Nepos becomes independent following Odoacers war
			if = {
				limit = {
					ROOT = {
						character = 55 # Odoacer
					}
				}
				FROM = { 
					any_vassal = { 
						limit = { 
							character = 175110 # Julius Nepos
							has_landed_title = k_dalmatia
						} 
						set_defacto_liege = THIS # Grant independence immediately, to prevent title usurpation
						character_event = { id = zusk_event.26 days = 1 } # Delay granting e_wre, so that below titular titles aren't transferred
					}
				}
			}
			ROOT = { # Without this Theodoric doesn't get Italy if he wins
				if = {
					limit = {
						has_character_flag = give_italy_if_win 
						NOT = { has_landed_title = k_italy }
					}
					primary_title = {
						k_italy = {
							activate_title = { title = THIS status = yes } # Otherwise grant_title doesn't work
							grant_title = ROOT
							copy_title_laws = PREV
						}
					}
					clr_character_flag = give_italy_if_win
				}
			}
		}
		
		ROOT = {
			prestige = 200
		}
		FROM = {
			prestige = -200
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		hidden_tooltip = {
			ROOT = {
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = FROM
			}
		}
	}
	
	on_success_title = {

		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					subjugate_or_take_under_title = { # If the target only has territory within the kingdom, he is simply vassalized
						title = PREV
						enemy = FROM
						is_crusade = yes
						type = invasion
					}
					gain_settlements_under_title = {
						title = PREV
						enemy = FROM
					}
				}
			}
		}
	}

	on_fail = {
		hidden_tooltip = {
			ROOT = {
				clr_character_flag = give_italy_if_win
			}
		}
		FROM = {
			prestige = 100
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { 
				NOT = { 
					character = FROM
				}
			}
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			prestige = -200
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			prestige = 200
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}
	
	on_invalidation	= {
		hidden_tooltip = {
			ROOT = {
				disband_event_forces = history_mark
				disband_event_forces = theodoric_italy_invasion
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = 200
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = { 
		factor = 10
		modifier = {
			factor = 50
			ROOT = {
				character = 63
			}
			FROM = {
				character = 175110
			}
		}	

		modifier = {
			factor = 100
			ROOT = {
				character = 63
			}
			FROM = {
				character = 55
			}
		}
	}
}

odoacer_takeover_war = {
	name = CB_NAME_POWERFUL_VASSAL_TAKEOVER
	war_name = WAR_NAME_POWERFUL_VASSAL_TAKEOVER
	sprite = 8
	truce_days = 3650
	can_call_allies = no
	can_ask_to_join_war = no
	check_de_jure_tier = EMPEROR
	#major_revolt = yes
	max_defender_occupation_score = 50 # not needed in landless Odoacer's case, but should be kept for post-start usages of this CB

	can_use = {
		ROOT = {
			vassal_of = FROM
			OR = {
				has_landed_title = k_italy
				has_landed_title = d_odoacer
			}
		}
		FROM = {
			independent = yes
			higher_tier_than = COUNT
		}
	}
	
	can_use_title = {
		has_holder = no
		ROOT = { has_claim = PREV }
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			has_character_flag = powerful_vassal_takeover_target
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
	
	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		FROM = {
			if = {
				limit = { has_landed_title = e_wre }
				save_global_event_target_as = defeated_western_emperor
			}
		}
		any_attacker = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
		hidden_tooltip = {
			# Julius Nepos becomes independent following Odoacers war
			if = {
				limit = {
					ROOT = {
						character = 55 # Odoacer
					}
				}
				FROM = {
					any_vassal = { 
						limit = { 
							character = 175110 # Julius Nepos
							has_landed_title = k_dalmatia
						}
						set_defacto_liege = THIS # Grant independence immediately, to prevent title usurpation
						character_event = { id = zusk_event.26 days = 1 } # Delay granting e_wre, so that below titular titles aren't transferred
					}
				}
			}
		}
		hidden_tooltip = {
			ROOT = {
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = FROM
			}
		}
		FROM = {
			hidden_tooltip = {
				disband_event_forces = faction_loyalists
				clr_character_flag = powerful_vassal_takeover_target
			}
		}
		k_italy = {
			grant_title = ROOT
		}
		set_global_flag = italy_bureaucratic
		d_odoacer = {
			unsafe_destroy_landed_title = THIS
		}
		activate_title = { title = d_odoacer status = no }
	}

	on_success_title = { #This is the target title 
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					subjugate_or_take_under_title = { # If the target only has territory within the kingdom, he is simply vassalized
						title = PREV
						enemy = FROM
						is_crusade = yes
						type = invasion
					}
					gain_settlements_under_title = {
						title = PREV
						enemy = FROM
					}
				}
				if = {
					limit = {
						ROOT = {
							character = 55 # Odoacer
						}
					}
					d_alpes = {
						ROOT = {
							subjugate_or_take_under_title = { # If the target only has territory within the kingdom, he is simply vassalized
								title = PREV
								enemy = FROM
								is_crusade = yes
								type = invasion
							}
							gain_settlements_under_title = {
								title = PREV
								enemy = FROM
							}
						}
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_powerful_vassal_takeover_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = {
			hidden_tooltip = {
				disband_event_forces = faction_loyalists
				clr_character_flag = powerful_vassal_takeover_target
			}
		}
		d_odoacer = {
			unsafe_destroy_landed_title = THIS
		}
		activate_title = { title = d_odoacer status = no }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}

		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_powerful_vassal_takeover_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = {
			hidden_tooltip = {
				disband_event_forces = faction_loyalists
				clr_character_flag = powerful_vassal_takeover_target
			}
		}
		d_odoacer = {
			unsafe_destroy_landed_title = THIS
		}
		activate_title = { title = d_odoacer status = no }
		imprison = FROM
	}

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

tributary_foederati_cb = { 
	name = CB_NAME_FOEDERATI_TRIBUTARY
	war_name = WAR_NAME_FOEDERATI_TRIBUTARY
	sprite = 37
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	can_use = {
		always = no # Not used in diplomacy for now, only through event
		ROOT = {
			is_nomadic = no
			independent = yes
			mercenary = no
			OR = {
				is_tributary = no
				is_tributary = {
					type = offmap
				}
			}
			NOT = { is_vassal_or_below = FROM }
			NOT = { is_liege_or_above = FROM }
			NOT = {
				rightful_religious_head_scope = {
					character = FROM
				}
			}
			NAND = {
				holy_order = yes
				religion = FROM
			}
		}
		FROM = {
			is_nomadic = no
			higher_tier_than = BARON
			NOT = {
				is_offmap_governor = yes
			}
			NAND = {
				holy_order = yes
				religion = ROOT
			}
			independent = yes
			NOT = {
				suzerain = {
					OR = {
						character = ROOT
						any_liege = {
							character = ROOT
						}
					}
				}
			}
		}
		
		# The attacker needs a border, or be at most two sea zones away from one of the target's counties
		FROM = {
			any_realm_province = {
				any_neighbor_province = {
					OR = {
						AND = {
							has_owner = yes
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
									top_liege = {
										is_tributary = {
											type = permanent
											suzerain = ROOT
										}
									}
								}
							}
						}
						AND = {
							is_land = no
							any_neighbor_province = {
								OR = {
									AND = {
										has_owner = yes
										owner = {
											OR = {
												character = ROOT
												is_liege_or_above = ROOT
											}
										}
									}
									AND = {
										is_land = no
										any_neighbor_province = {
											owner = {
												OR = {
													character = ROOT
													is_liege_or_above = ROOT
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		# The defender is not in any tributary war
		FROM = {
			NOR = {
				any_war = {
					using_cb = tributary_cb
				}
				any_war = {
					using_cb = tributary_permanent_cb
				}
				any_war = {
					using_cb = tributary_nomad_cb
				}
				any_war = {
					using_cb = tributary_foederati_cb
				}
			}
		}
	}

	is_valid = {
		NOT = {
			ROOT = {
				rightful_religious_head_scope = {
					character = FROM 
				}
			}
		}
	}

	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		FROM = {
			save_event_target_as = target_loser
			prestige = -100
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
			opinion = {
				modifier = opinion_forced_tributary 
				who = ROOT
				years = 15
			}
		}

		ROOT = {
			prestige = 200
			long_character_event = {
				id = WtWSMS_foederati.30 # Victorious Roman force barbarians into foederati status after enforcing demands
			}
		}
		
		if  = {
			limit = {
				ROOT = {
					uses_decadence = yes
				}
			}
			ROOT = {
				decadence = -10
			}
		}
		if  = {
			limit = {
				ROOT = {
					is_nomadic = yes
				}
			}
			ROOT = {
				clan_title = {
					ROOT = {
						any_vassal = {
							limit = { is_nomadic = yes }
							clan_title = {
								clan_opinion = {
									who = PREVPREVPREV
									modifier = won_tributary_war
									years = 5
								}
							}
						}
					}
				}
			}
		}
		
		if = {
			limit = {
				ROOT = {
					is_tribal = yes
				}
			}
			ROOT = {
				any_vassal = {
					limit = { is_tribal = yes }
					opinion = {
						who = PREV
						modifier = won_tributary_war
						years = 5
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			prestige = 100
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}

		FROM = {
			prestige = 200
		}
	}
	
	on_attacker_leader_death = {
		end_war = invalid
	}

	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.5  # Low prio CB
		}
	}
}