# Audax Validator EnableCommentMetadata

# Equivalent to "tributary_permanent_cb" ("# New8") in 01_cb_types
# Required because the vanilla chaining won't work with modded tributaries
# Only can_use has been changed from the vanilla version

expd_t_chainable_permanent_tributary_cb = { # New8
	name = CB_NAME_PERMANENT_TRIBUTARY
	war_name = WAR_NAME_PERMANENT_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
	
	sort_priority = 810
	
	can_use_gui = {
		check_if_crusader_trigger = yes
		ROOT = {
			relative_realm_size = {
				who = FROM 
				size > 1
			}
			conditional_tooltip = {
				trigger = {
					capital_scope = {
						NOT = {
							region = world_europe
						}
					}
				}
				prestige = 200
				TECH_MAJESTY = 2
			}
			conditional_tooltip = {
				trigger = {
					capital_scope = {
						region = world_europe
					}
				}
				prestige = 200
				TECH_MAJESTY = 4
			}
		}
	}
	
	can_use = {
		has_dlc = "Jade Dragon"

		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
			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 = {
				any_suzerain = {
					OR = {
						character = ROOT
						any_liege = {
							character = ROOT
						}
					}
				}
			}
		}
		
		# The vanilla adjacency condition must NOT be true (because you should use the vanilla CB)... 
		NOT = {
			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
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		# ... But this adjusted border condition MUST be true
		expd_t_from_borders_root_or_roots_tributaries = yes
		
		# The defender is not in any tributary war
		expd_t_from_is_not_in_any_tributary_war = yes
		
	}

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

	on_add = {
		# Chance to surrender early
		ROOT = { save_event_target_as = tribute_war_suzerain }
		FROM = {
			show_scope_change = no
			save_event_target_as = tribute_war_target
			set_character_flag = surrender_tributary_permanent_cb
			character_event = { id = JD.2500 days = 5 tooltip = surrender_tributary_tt }
		}
		hidden_tooltip = {
			# Audax Validator "." Ignore_NEXT
			fire_haruspicy_event_effect = yes
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				# Audax Validator "." Ignore_NEXT
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		FROM = {
			show_scope_change = no
			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 = {
			show_scope_change = no
			prestige = 200
			make_tributary = { who = FROM tributary_type = permanent }
		}
		
		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 = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			prestige = 100
		}
	}

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

		FROM = {
			show_scope_change = no
			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
		}
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}