establish_mandala_trade_vassal = {
	category = influence

	require_acceptance = no

	is_visible = {
		is_subject_of = FROM
		is_subject_of_type = mandala_trade_vassal
		can_create_subject_type = {
			subject = FROM
			subject_type = mandala_trade_vassal
		}
	}

	is_allowed = {
		hidden_trigger = {
			is_subject_of = FROM
			is_subject_of_type = mandala_trade_vassal
			can_create_subject_type = {
				subject = FROM
				subject_type = mandala_trade_vassal
			}
		}
		NOT = { truce_with = FROM }
		is_at_war = no
		FROM = { is_at_war = no }
		liberty_desire = 75
	}

	on_accept = {
		if = {
			limit = {
				is_subject_of = FROM
				is_subject_of_type = mandala_trade_vassal
				can_create_subject_type = {
					subject = FROM
					subject_type = mandala_trade_vassal
				}
				NOT = { truce_with = FROM }
				is_at_war = no
				FROM = { is_at_war = no }
				liberty_desire = 75
			}
			FROM = {
				ROOT = {
					grant_independence = yes
				}
				transfer_subjects_of_type_to = { type = vassal who = ROOT }
				transfer_subjects_of_type_to = { type = client_vassal who = ROOT }
				transfer_subjects_of_type_to = { type = march who = ROOT }
				transfer_subjects_of_type_to = { type = client_march who = ROOT }
				transfer_subjects_of_type_to = { type = roman_client_kingdom who = ROOT }
				transfer_subjects_of_type_to = { type = voivodeship who = ROOT }
				transfer_subjects_of_type_to = { type = fief_subject who = ROOT }
				transfer_subjects_of_type_to = { type = crusader_state_vassal who = ROOT }
				transfer_subjects_of_type_to = { type = nahuatl_vassal who = ROOT }
				transfer_subjects_of_type_to = { type = peasant_vassal who = ROOT }
				transfer_subjects_of_type_to = { type = muslim_tributary_vassal who = ROOT }
				transfer_subjects_of_type_to = { type = integrated_tribe_subject who = ROOT }
				transfer_subjects_of_type_to = { type = naval_march who = ROOT }
				transfer_subjects_of_type_to = { type = iqta_vassal who = ROOT }
				transfer_subjects_of_type_to = { type = pagan_chiefdom who = ROOT }
				transfer_subjects_of_type_to = { type = administrative_region who = ROOT }
				transfer_subjects_of_type_to = { type = defensive_march who = ROOT }
				transfer_subjects_of_type_to = { type = agricultural_march who = ROOT }
				transfer_subjects_of_type_to = { type = religious_march who = ROOT }
				if = {
					limit = {
						has_subject_of_type = mandala_trade_vassal
					}
					every_subject_country = {
						limit = {
							is_subject_of_type = mandala_trade_vassal
						}
						add_opinion = {
							who = ROOT
							modifier = mandala_trade_vassal_forced
						}
						set_country_flag = will_get_reduced_liberty_desire
					}
					transfer_subjects_of_type_to = { type = mandala_trade_vassal who = ROOT }
				}
			}
			every_subject_country = {
				limit = {
					is_subject_of_type = mandala_trade_vassal
					has_country_flag = will_get_reduced_liberty_desire
				}
				clr_country_flag = will_get_reduced_liberty_desire
				add_liberty_desire = -50
			}
			create_subject = {
				subject = FROM
				subject_type = mandala_trade_vassal
				keep_liberty_desire = yes
			}
			FROM = {
				add_opinion = {
					who = ROOT
					modifier = mandala_trade_vassal_forced
				}
				add_truce_with = ROOT
				add_liberty_desire = -100
				country_event = { id = subjects_expanded_mandala_trade_vassal_events.1 }
			}
		}
	}

	ai_will_do = {
		num_of_free_diplomatic_relations = 1
		NOT = { war_exhaustion = 5 }
		OR = {
			AND = {
				num_of_merchants = 1
				NOT = { num_of_cities = 25 }
			}
			AND = {
				num_of_merchants = 2
				num_of_cities = 25
			}
		}
		OR = {
			army_size = FROM
			num_of_cities = FROM
			num_of_subjects = 2
			liberty_desire = 85
		}
	}
}

cancel_mandala_trade_vassal = {
	category = influence

	require_acceptance = no

	is_visible = {
		FROM = {
			is_subject_of = ROOT
			is_subject_of_type = mandala_trade_vassal
		}
	}

	is_allowed = {
		hidden_trigger = {
			FROM = {
				is_subject_of = ROOT
				is_subject_of_type = mandala_trade_vassal
			}
		}
		NOT = { truce_with = FROM }
		prestige = 0
	}

	on_accept = {
		if = {
			limit = {
				FROM = {
					is_subject_of = ROOT
					is_subject_of_type = mandala_trade_vassal
				}
				NOT = { truce_with = FROM }
				prestige = 0
			}
			FROM = {
				if = {
					limit = {
						NOT = { liberty_desire = 50 }
					}
					add_opinion = {
						who = ROOT
						modifier = vassal_abandoned
					}
				}
				grant_independence = yes
				add_truce_with = ROOT
			}
			add_truce_with = FROM
			add_prestige = -25
		}
	}

	ai_will_do = {
		FROM = {
			liberty_desire = 100
			years_in_vassalage_under = {
				who = ROOT
				years = 50
				all_subject_country = yes
			}
		}
	}
}

mandala_trade_vassal_enable_ban_on_internal_wars = {
	category = subject_interactions

	require_acceptance = no

	is_visible = {
		FROM = {
			is_subject_of = ROOT
			is_subject_of_type = mandala_trade_vassal
			NOT = { has_country_flag = mandala_trade_vassal_internal_war_ban }
		}
	}

	is_allowed = {
		hidden_trigger = {
			FROM = {
				is_subject_of = ROOT
				is_subject_of_type = mandala_trade_vassal
				NOT = { has_country_flag = mandala_trade_vassal_internal_war_ban }
			}
		}
		NOT = { truce_with = FROM }
		is_at_war = no
		FROM = { is_at_war = no }
		FROM = { NOT = { liberty_desire = 33 } }
	}

	on_accept = {
		if = {
			limit = {
				FROM = {
					is_subject_of = ROOT
					is_subject_of_type = mandala_trade_vassal
					NOT = { has_country_flag = mandala_trade_vassal_internal_war_ban }
				}
				NOT = { truce_with = FROM }
				is_at_war = no
				FROM = { is_at_war = no }
				FROM = { NOT = { liberty_desire = 33 } }
			}
			FROM = {
				set_country_flag = mandala_trade_vassal_internal_war_ban
			}
		}
	}

	ai_will_do = {
		always = no
	}
}

mandala_trade_vassal_disable_ban_on_internal_wars = {
	category = subject_interactions

	require_acceptance = no

	is_visible = {
		FROM = {
			is_subject_of = ROOT
			is_subject_of_type = mandala_trade_vassal
			has_country_flag = mandala_trade_vassal_internal_war_ban
		}
	}

	is_allowed = {
		hidden_trigger = {
			FROM = {
				is_subject_of = ROOT
				is_subject_of_type = mandala_trade_vassal
				has_country_flag = mandala_trade_vassal_internal_war_ban
			}
		}
		is_at_war = no
		FROM = { is_at_war = no }
		prestige = 0
	}

	on_accept = {
		if = {
			limit = {
				FROM = {
					is_subject_of = ROOT
					is_subject_of_type = mandala_trade_vassal
					has_country_flag = mandala_trade_vassal_internal_war_ban
				}
				is_at_war = no
				FROM = { is_at_war = no }
			}
			add_prestige = -10
			FROM = {
				clr_country_flag = mandala_trade_vassal_internal_war_ban
			}
		}
	}

	ai_will_do = {
		always = yes
	}
}