# Vanilla diplomatic actions, re-coded as a targeted decision for more flexibility on hardcoded triggers.
# ROOT is targeted character
# FROM is the decision taker

targetted_decisions = {

	# "Demand Religious Conversion"
	# Note: need to disable DEMAND_RELIGIOUS_CONVERSION_INTERACTION_ENABLED in defines.lua
	# - Workaround to a bug with diploaction not working even if at +100 opinion
	# - Split in 2 decisions as there is no ai_target_filter for both direct vassals and courtiers.
	
	demand_courtier_religious_conversion = {
		ai_target_filter = home_court
		from_potential = {
			is_ruler = yes
			always = no
		}
		potential = {
			NOT = { religion = FROM }
			vassal_of = FROM
			is_ruler = no
			always = no
		}
		allow = {
			opinion = { who = FROM value = 25}
			FROM = {
				custom_tooltip = { 
					text = TT_NOT_UNREFORMED_PAGAN
					hidden_tooltip = {
						OR = {
							NOT = { religion_group = pagan_group }
							is_reformed_religion = yes
						}
					}
				}
			}
			NOT = { controls_religion = yes }
		}
		effect = {
			# Fire any event that would normally be fired by the on_action on_vassal_accepts_religious_conversion
			custom_tooltip = {
				text = "demand_courtier_religious_conversion_tooltip"
				hidden_tooltip = {
					FROM = { 
						character_event = { id = WtWSMSconv.1 }
					}
				}
			}
		}
		
		ai_will_do = {
			factor = 1
		}
	}
	
	demand_vassal_religious_conversion = {
		ai_target_filter = vassals
		from_potential = {
			is_ruler = yes
			always = no
		}
		potential = {
			NOT = { religion = FROM }
			vassal_of = FROM
			is_ruler = yes
			always = no
		}
		allow = {
			opinion = { who = FROM value = 35}
			FROM = {
				custom_tooltip = { 
					text = TT_NOT_UNREFORMED_PAGAN
					hidden_tooltip = {
						OR = {
							NOT = { religion_group = pagan_group }
							is_reformed_religion = yes
						}
					}
				}
			}
			NOT = { controls_religion = yes }
		}
		effect = {
			# Fire any event that would normally be fired by the on_action on_vassal_accepts_religious_conversion
			custom_tooltip = {
				text = "demand_vassal_religious_conversion_tooltip"
				hidden_tooltip = {
					FROM = {
						character_event = { id = WtWSMSconv.1 }
					}
				}
			}
		}
		
		ai_will_do = {
			factor = 1
		}
	}
	
	begin_foederati_negotiations_roman = { # Roman bordering "Barbarian" initiates negotiations 
		only_independent = yes
		ai_target_filter = independent_rulers
		ai_check_interval = 12

		potential = {
			foederati_barbarian_trigger = yes
		}
		
		from_potential = {
			foederati_roman_trigger = yes
		}
		
		allow = {
			FROM = {
				show_scope_change = no
				war = no # I'm not sure on whether this should be a general FROM one or just with ROOT?
				custom_tooltip = { # Borders "Barbarian"
					text = begin_foederati_negotiations_roman_borders_barbarian_realm_tt
					any_neighbor_independent_ruler = {
						mercenary = no
						holy_order = no
						OR = {
							has_romance_culture_trigger = no
							primary_title = { title = k_ostrogoths } 
						}
						NOR = {
							primary_title = { 
								higher_tier_than = KING
								bureaucratic_government_trigger = yes
								sassanid_government_trigger = yes
							}
						}
					}	
					shares_realm_border_with = ROOT
				}
				is_tributary = no
				custom_tooltip = {
					text = get_foederati_roman_cooldown_tt
					NOT = { has_character_modifier = get_foederati_timer }
				}
				prisoner = no
				NOT = { trait = incapable }
				is_inaccessible_trigger = no
			}
		}
		
		effect = {
			long_character_event = { id = WtWSMS_foederati.19 } # Barbarian accept or refuse to start negotiating with Roman
			FROM = {
				add_character_modifier = {
					name = get_foederati_timer
					duration = 3600
				}
			}
		}
		
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.001 # slow down
			}
			modifier = { 
				FROM = {
					realm_levy_diff = { 
						who = ROOT 
						value = 2000 
					} 
				}
				factor = 1.5 
			}
			modifier = {
				FROM = {
					realm_levy_diff = { 
						who = ROOT 
						value = 5000 
					}
				}
				factor = 3 
			}
			modifier = {
				FROM = { 
					realm_size < 50
				}
				factor = 0.5
			}
			modifier = { 				
				FROM = { 
					lower_tier_than = emperor
				}
				factor = 0.5
			}
		}
	}
	
	begin_foederati_negotiations_barbarian = { # "Barbarian" bordering Roman initiates negotiations
		only_independent = yes
		ai_target_filter = independent_rulers
		ai_check_interval = 12

		potential = {
			foederati_roman_trigger = yes
		}
		
		from_potential = {
			foederati_barbarian_trigger = yes
		}
		
		allow = {
			FROM = {
				show_scope_change = no
				war = no # I'm not sure on whether this should be a general FROM one or just with ROOT?
				custom_tooltip = { # Borders Roman
					text = become_foederati_barbarian_allow_borders_roman_realm_tt
					any_neighbor_independent_ruler = {
						primary_title = { higher_tier_than = DUKE }
						OR = { 
							has_romance_culture_trigger = yes
							has_landed_title = e_wre
							has_landed_title = e_byzantium 
							has_landed_title = e_roman_empire
						}
						mercenary = no
						holy_order = no
					}
					shares_realm_border_with = ROOT
				}
				is_tributary = no
				NOT = { tier = emperor }
				custom_tooltip = {
					text = become_foederati_barbarian_cooldown_tt
					NOT = { has_character_modifier = become_foederati_timer }
				}
				prisoner = no
				NOT = { trait = incapable }
				is_inaccessible_trigger = no
			}
		}
		
		effect = {
			long_character_event = { id = WtWSMS_foederati.1 } # Romans accept or refuse to start negotiating with barbarian			
			FROM = {
				add_character_modifier = {
					name = become_foederati_timer
					duration = 3600
				}
			}
		}
		
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.001 # slow down
			}
			modifier = { 
				FROM = { 
					trait = roman_nostalgia
				}
				factor = 1.5
			}
			modifier = { 
				ROOT = { 
					realm_levy_diff = { 
						who = FROM 
						value = 2000 
					} 
				}
				factor = 1.5 
			}
			modifier = { 
				ROOT = { 
					realm_levy_diff = { 
						who = FROM 
						value = 5000 
					} 
				}
				factor = 3 
			}
			modifier = { # Barbarian with a realm size larger than 50 is less likely to start negotiations
				FROM = { 
					realm_size > 50
				}
                factor = 0.5
            }
			modifier = { # Barbarian with a realm size larger than 100 is even less likely to start negotiations
                FROM = { 
					realm_size > 100
				}
                factor = 0.25
            }
			# TODO: AI should not start this unless there is pressure from the outside that warrant an alliance with Rome
		}
	}

	force_integrate_foederati = { # A diploaction to turn foederati into vassal by force
		only_independent = yes
		ai_check_interval = 12
		from_potential = {
			independent = yes
			mercenary = no
			holy_order = no
			is_landed = yes
			primary_title = { higher_tier_than = DUKE }
			OR = { 
				has_romance_culture_trigger = yes
				has_landed_title = e_wre
				has_landed_title = e_byzantium 
				has_landed_title = e_roman_empire
			}
		}
		potential = {
			is_tributary = {
				type = foederati 
				suzerain = FROM 
			}
		}
		allow = {
			ROOT = { 
				war = no
				prisoner = no
				NOT = { trait = incapable }
				is_inaccessible_trigger = no
				NOT = { has_truce = FROM }
			}
			FROM = { 
				war = no 
				prisoner = no
				NOT = { trait = incapable }
				is_inaccessible_trigger = no
				NOT = { has_truce = ROOT }
				primary_title = { higher_tier_than = ROOT }
				custom_tooltip = {
					text = land_promise_foederati_integration
					NOT = { # Can not integrate with ongoing land promise
						has_character_flag = promised_territory_to_@ROOT
					}
				}
			}
		}
		effect = {
			long_character_event = { id = WtWSMS_foederati.26 } 
		}
		ai_will_do = {
			factor = 0
		}
	}
}