# duchy_adventure
# kingdom_adventure
# manifest_destiny_invasion

duchy_adventure = {
	name = CB_NAME_DUCHY_ADVENTURE
	war_name = WAR_DUCHY_ADVENTURE
	sprite = 9
	truce_days = 3650
	hostile_against_others = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character

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

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

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			prestige = 250

			hidden_effect = {
				clr_character_flag = duchy_adventurer
				wealth = 500
			}
		}

		defender = {
			show_scope_change = no

			prestige = -250
		}
	}

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

		custom_tooltip = {
			text = other_invasion_succ_tip

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

				trigger_switch = {
					on_trigger = has_character_flag

					# Special case: Seljuk succeeds in his first conquest
					seljuk_himself = {
						narrative_event = {
							id = 60206
							days = 1
						}
					}

					# Special case: Timur succeeds in his first conquest
					timur_himself = {
						narrative_event = {
							id = 60306
							days = 1
						}
					}

					# Special case: Sabuktigin succeeds in his first conquest
					sabuktigin_himself = {
						narrative_event = {
							id = 60406
							days = 1
						}
					}

					# Special case: Sultanate of Rum creator succeeds in his first conquest
					rum_creator = {
						character_event = {
							id = 60610
							days = 600
							random = 300
						}
					}

					# CK2Plus
					# Special case: Rollo succeeds in his invasion of Normandy
					is_rollo = {
						character_event = {
							id = Plus.2752
							days = 1
						}
					}
				}

				# 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
				landless_government_check_effect = yes
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -100
			imprison = defender

			hidden_effect = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100

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

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -250
			imprison = defender

			hidden_effect = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}

		defender = {
			show_scope_change = no

			prestige = 250
			wealth = 100
		}
	}

	on_attacker_leader_death = {
		attacker = {
			hidden_effect = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}

		end_war = invalid
	}

	on_invalidation = {
		log = "CB(duchy_adventure): 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

		mult_modifier = {
			factor = 0.25
			ROOT = { has_character_flag = is_rollo }
		}
	}
}

# Used mainly for Muslims adventuring into India
kingdom_adventure = {
	name = CB_NAME_DUCHY_ADVENTURE
	war_name = WAR_DUCHY_ADVENTURE
	sprite = 9
	truce_days = 3650

	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

	can_use = {
		# Attacker
		ROOT = {
			OR = {
				has_character_flag = duchy_adventurer
				has_character_flag = rum_creator
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			prestige = 500
			piety = 250

			hidden_effect = {
				clr_character_flag = duchy_adventurer
				remove_do_not_disturb_effect = yes

				if = {
					limit = {
						has_nickname = no
						NOT = { has_character_flag = is_child_of_destiny }
					}

					# Set event targets for dynamic nicknames
					save_event_target_as = nickname_receiver
					defender = { save_event_target_as = nickname_target }

					random_list = {
						 5 = { give_nickname = nick_the_victorious }
						 5 = { give_nickname = nick_the_conqueror }
						 5 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_rel_group }
						10 = { give_nickname = nick_the_tiger }
						10 = { give_nickname = nick_the_world_burner }

						10 = {
							give_nickname = nick_the_scourge_of_god

							trigger = {
								NOT = { religion = event_target:nickname_target }
							}
						}
					}
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = -500
			piety = -250
		}
	}

	on_success_title = {
		custom_tooltip = {
			text = other_invasion_succ_tip

			attacker = {
				if = { # Special stuff for player Child of Destiny (AI Children do not have this flag)
					limit = { has_character_flag = child_of_destiny_outwards_focus }

					defender = { # Ensure that the Child of Destiny always gets one County in the targeted Kingdom (mainly useful when testing with yesmen, but can also happen in rare occurences where the target is imprisoned)
						random_realm_title = {
							limit = {
								real_tier = COUNT
								de_jure_liege_or_above = PREVPREV
							}

							usurp_title = {
								target = attacker
								type = host
							}
						}
					}

					liege = { save_event_target_as = old_liege_target }

					if = { # Abandon all old titles if you were a vassal
						limit = { independent = no }

						any_demesne_title = {
							limit = {
								NOR = {
									title = PREVPREV
									de_jure_liege_or_above = PREVPREV
								}
							}

							grant_title = event_target:old_liege_target
						}

						any_vassal = {
							limit = {
								capital_scope = {
									NOR = {
										province = PREVPREVPREV
										de_jure_liege_or_above = PREVPREVPREV
									}
								}
							}

							set_defacto_liege = event_target:old_liege_target
						}
					}
				}
			}

			defender = { # The adventurer takes ANY demesne title, including the targeted Kingdom, within the area of the targeted kingdom
				any_demesne_title = {
					limit = {
						OR = {
							title = PREVPREV
							de_jure_liege_or_above = PREVPREV
						}

						attacker = {
							NOT = { has_character_flag = rum_creator }
						}
					}

					usurp_title = {
						target = attacker
						type = host
					}
				}
			}

			if = { # Claim the targeted Kingdom if it isn't held by anyone
				limit = { has_holder = no }

				usurp_title = {
					target = attacker
					type = host
				}
			}

			attacker = {
				if = { # Create a Kingdom if the adventurer does not have one by this point
					limit = { lower_real_tier_than = KING }

					if = {
						limit = { has_character_flag = rum_creator }

						k_rum = {
							grant_title = attacker
						}

						capital_scope = {
							culture = attacker
						}

						# Special case: Sultanate of Rum might start yet another war
						character_event = {
							id = 60610
							days = 600
							random = 300
						}
					}
					else_if = {
						limit = { has_character_flag = is_child_of_destiny }

						save_event_target_as = target_name

						create_title = {
							tier = KING
							landless = no
							temporary = no
							custom_created = yes
							culture = attacker
							holder = attacker
							name = DESTINY_KINGDOM
							base_title = PREV
							copy_title_laws = yes
						}

						capital_scope = {
							county = {
								save_event_target_as = coa_target
							}
						}

						primary_title = {
							set_coa = event_target:coa_target
						}

						if = {
							limit = {
								NOT = { religion_group = muslim }
							}

							any_demesne_title = {
								limit = { higher_real_tier_than = COUNT }
								set_title_flag = non_manual_law_change # No prestige loss
								add_law = succ_primogeniture
								clr_title_flag = non_manual_law_change # No prestige loss
							}
						}

						character_event = {
							id = HF.303 # Child of Destiny can choose to adopt culture/religion of newly conquered land.
							days = 3
						}

						if = { # Give bloodline to Child of Destiny
							limit = {
								higher_real_tier_than = DUKE

								NOT = {
									any_owned_bloodline = {
										has_bloodline_flag = created_bloodline
										bloodline_is_active_for = PREV
									}
								}
							}

							character_event = {
								id = HF.304 # Bloodline event
								days = 14
							}
						}
					}

					else = {
						create_title = {
							tier = KING
							landless = no
							temporary = no
							custom_created = yes
							culture = attacker
							holder = attacker
							name = INDIAN_MUSLIM_KINGDOM
							base_title = PREV
							copy_title_laws = yes
						}

						culture = capital_scope

						any_child = {
							limit = {
								is_ruler = no
								ai = yes
							}

							culture = attacker
						}

						any_courtier = {
							limit = {
								is_ruler = no
								ai = yes
							}

							culture = attacker
						}
					}
				}

				# Preserve Vassals under the targeted kingdom
				vassalize_or_take_under_title = { # Take all vassals held by the target under the title
					title = PREV
					enemy = defender
					type = host
				}

				set_defacto_liege = attacker # Always go Independent
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_invalidation = {
		attacker = {
			clr_character_flag = child_of_destiny_made_player_choice
			clr_character_flag = child_of_destiny_adventure
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -100
			clear_wealth = yes
			imprison = defender

			hidden_effect = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer

				if = {
					limit = { has_character_flag = is_child_of_destiny }

					remove_trait = disinherited
					set_global_flag = child_of_destiny_invasion_fail@attacker
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100

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

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			clear_wealth = yes
			imprison = defender

			hidden_effect = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer

				if = {
					limit = { has_character_flag = is_child_of_destiny }
					remove_trait = disinherited
					set_global_flag = child_of_destiny_invasion_reverse_demand@attacker
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = 200
		}
	}

	on_attacker_leader_death = {
		attacker = {
			hidden_effect = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}

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

# This can be used for various special cases; the great conquerors
manifest_destiny_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 3650

	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes

	can_use_gui = {
		check_if_crusader_trigger = yes

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

	can_use = {
		# Attacker
		ROOT = {
			OR = {
				AND = {
					dynasty = 613 # Seljuks
					year < 1200
					has_regent = no
				}

				AND = {
					dynasty = 800 # Timurids
					has_regent = no
				}
			}

			trigger_if = {
				limit = {
					ai = no
					multiplayer = yes
				}

				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}

			NOT = { is_vassal_or_below_of = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		capital_scope = {
			OR = {
				region = custom_persia
				region = custom_baluchistan
				region = custom_afghanistan
				region = custom_mesopotamia
				region = custom_azerbaijan
				region = custom_armenia
				region = custom_trebizond
				region = custom_nikaea
				region = custom_syria
				region = custom_arabia
				region = custom_jerusalem
				region = custom_egypt
				region = custom_khorasan
			}
		}

		# Target must have at least 3 counties within the target kingdom
		FROM = {
			num_of_realm_counties = {
				value >= 3
				title = PREV
			}
		}

		# The attacker needs at least one county in the target kingdom, or a border
		any_direct_de_jure_vassal_title = {
			any_direct_de_jure_vassal_title = {
				OR = {
					holder_scope = {
						OR = {
							character = ROOT
							is_vassal_or_below_of = ROOT
						}
					}

					location = {
						any_neighbor_province = {
							owner = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}
					}
				}
			}
		}
	}

	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 = {
		attacker = {
			show_scope_change = no

			prestige = 200
			participation_scaled_prestige = 200

			hidden_effect = {
				if = {
					limit = { has_nickname = no }

					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_magnificent }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_conqueror }
					}
				}
			}

		#	if = {
		#		limit = {
		#			uses_decadence = yes
		#			defender = { uses_decadence = no }
		#		}
		#
		#		participation_scaled_decadence = -20
		#	}
		}

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

			hidden_effect = {
				participation_scaled_prestige = 200

				dynasty_head = {
					if = {
						limit = {
							uses_decadence = yes
							defender = { uses_decadence = no }
						}

						participation_scaled_decadence = -20
					}
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = -200
		}
	}

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

		hidden_effect = { destroy_tradeposts_effect = yes }

		custom_tooltip = {
			text = other_invasion_succ_tip

			attacker = {
				vassalize_or_take_under_title_destroy_duchies = {
					title = PREV
					enemy = defender
					is_crusade = yes # Even if the kingdom title holder is not participating in the war, gain holdings occupied by all participants
					type = invasion
				}
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}

		# Collapse after the 1066 Seljuk invasion of Armenia
		if = {
			limit = {
				has_global_flag = 1066_start_byz
				title = k_armenia
				attacker = { has_landed_title = k_persia }
				defender = { has_landed_title = e_byzantium }
				NOT = { has_global_flag = collapse_of_themata_system }
			}

			defender = {
				narrative_event = { id = Plus.1306 }
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -100
		}

		defender = {
			show_scope_change = no

			prestige = 100
			participation_scaled_prestige = 100
			roman_victory_triumph_effect = yes
		}

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

	on_fail_title = {
		# The Byzantine Empire wins the 1066 war against the Seljuks
		if = {
			limit = {
				has_global_flag = 1066_start_byz
				title = k_armenia
				attacker = { has_landed_title = k_persia }
				defender = { has_landed_title = e_byzantium }
			}

			defender = {
				character_event = { id = Plus.1307 }
			}
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

		#	if = {
		#		limit = {
		#			NOT = {
		#				any_liege = {
		#					dynasty = PREV
		#				}
		#			}
		#		}
		#
		#		decadence = 10
		#	}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 200
			roman_victory_triumph_effect = yes
		}

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

	on_reverse_demand_title = {
		# The Byzantine Empire wins the 1066 war against the Seljuks
		if = {
			limit = {
				has_global_flag = 1066_start_byz
				title = k_armenia
				attacker = { has_landed_title = k_persia }
				defender = { has_landed_title = e_byzantium }
			}

			defender = {
				character_event = { id = Plus.1307 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 5

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
			}
		}

		coronation_factor = yes
	}
}