﻿raid_estate = {
	# Basic Setup
	skill = intrigue
	desc = "raid_estate_desc_general"
	success_desc = "RAID_ESTATE_SUCCESS_DESC"
	discovery_desc = "RAID_ESTATE_DISCOVERY_DESC"
	icon = icon_raid_estate_scheme
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = political
	target_type = character
	cooldown = { years = 10 }

	# Parameters
	speed_per_skill_point = t2_spsp_owner_value
	speed_per_target_skill_point = t2_spsp_target_value
	spymaster_speed_per_skill_point = t2_spsp_owner_spy_value
	target_spymaster_speed_per_skill_point = t2_spsp_target_spy_value
	tier_speed = 1
	base_progress_goal = t3_base_phase_length_value
	maximum_secrecy = 95
	base_maximum_success = t2_base_max_success_value
	phases_per_agent_charge = 1
	success_chance_growth_per_skill_point = t3_scgpsp_value

	is_secret = yes
	maximum_breaches = 5

	# Core Triggers
	allow = {
		is_imprisoned = no
		shared_raid_estate_participant_valid_trigger = yes
		scope:target = {
			house != scope:actor.house
			shared_raid_estate_participant_valid_trigger = yes
			# Valid Domicile
			domicile ?= {
				OR = {
					is_domicile_type = estate
					is_domicile_type = east_asian_estate
					is_domicile_type = japanese_manor
					is_domicile_type = feudal_domicile
				}
			}
		}
	}

	valid = {
		scope:owner = {
			shared_raid_estate_participant_valid_trigger = yes
		}
		scope:target = {
			shared_raid_estate_participant_valid_trigger = yes
			# Valid Domicile
			domicile ?= {
				OR = {
					is_domicile_type = estate
					is_domicile_type = east_asian_estate
					is_domicile_type = japanese_manor
					is_domicile_type = feudal_domicile
				}
			}
		}
	}

	# Agents
	agent_leave_threshold = -25
	agent_join_chance = {
		base = 0
		ai_agent_join_chance_basic_suite_modifier = yes
		ai_agent_join_chance_hostile_grievous_modifier = yes
		# House
		modifier = {
			add = -50
			scope:recipient.house ?= scope:target.house
			desc = TARGET_IS_MY_HOUSE_MEMBER
		}
	}
	valid_agent = { is_valid_agent_standard_trigger = yes }
	agent_groups_owner_perspective = { courtiers guests scripted_relations }

	odds_prediction = {
		add = hostile_scheme_base_odds_prediction_target_is_char_value
		add = odds_skill_contribution_intrigue_value
		add = agent_groups_owner_perspective_value
		add = odds_raid_estate_scheme_misc_value
		min = 0
	}

	# Base Chances
	base_success_chance = {
		base = 0
		scheme_type_skill_success_chance_modifier = { SKILL = INTRIGUE }
		hostile_scheme_base_chance_modifier = yes

		# Spiritual Head of Faith
		modifier = {
			add = -30
			desc = "SCHEME_VS_SPIRITUAL_HOF"
			scope:target.faith = {
				this = scope:owner.faith
				has_doctrine = doctrine_spiritual_head
				religious_head ?= scope:target
			}
		}
		# Sponsored Inspiration
		modifier = {
			add = -15
			desc = target_has_sponsored_inspiration
			scope:target = {
				inspiration ?= { exists = inspiration_sponsor }
			}
		}
		# Family Feud
		house_feud_hostile_scheme_success_modifier = yes
		# Diarchs are better at schemes within their liege's realm
		diarch_scheming_within_realm_bonus_modifier = yes
		# Modifiers
		# Context
		modifier = {
			scope:target.domicile.domicile_location.county ?= {
				county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
			}
			desc = RAID_ESTATE_IN_SUBREALM_DESC
			add = 10
		}
		modifier = {
			exists = scope:target.house
			scope:actor.top_liege.house ?= scope:target.house
			desc = RAID_ESTATE_TOP_LIEGE_DESC
			add = -25
		}
		# House
		modifier = {
			scope:owner.house = {
				is_powerful_family = yes
				is_dominant_family = no
			}
			desc = OWNER_POWERFUL_FAMILY_DESC
			add = 10
		}
		modifier = {
			scope:target.house = {
				is_powerful_family = yes
				is_dominant_family = no
			}
			desc = TARGET_POWERFUL_FAMILY_DESC
			add = -10
		}
		modifier = {
			scope:owner.house = { is_dominant_family = yes }
			desc = OWNER_DOMINANT_FAMILY_DESC
			add = 20
		}
		modifier = {
			scope:target.house = { is_dominant_family = yes }
			desc = TARGET_DOMINANT_FAMILY_DESC
			add = -20
		}
		# Influence
		modifier = {
			desc = OWNER_INFLUENCE_LEVEL_DESC
			add = {
				value = scope:owner.influence_level
				multiply = 5
			}
		}
		modifier = {
			desc = TARGET_INFLUENCE_LEVEL_DESC
			add = {
				value = scope:target.influence_level
				multiply = -5
			}
		}
		# Domicile
		modifier = { # WATCHTOWER
			scope:target.domicile ?= {
				OR = {
					has_domicile_building_or_higher = watchtower_01
					has_domicile_building_or_higher = japanese_watch_house_01
				}
			}
			desc = TARGET_DOMICILE_WATCHTOWER_DESC
			add = {
				value = -2.5
				scope:target.domicile = {
					if = {
						limit = {
							OR = {
								has_domicile_building_or_higher = watchtower_06
								has_domicile_building_or_higher = japanese_watch_house_06
							}
						}
						multiply = 6
					}
					else_if = {
						limit = {
							OR = {
								has_domicile_building_or_higher = watchtower_05
								has_domicile_building_or_higher = japanese_watch_house_05
							}
						}
						multiply = 5
					}
					else_if = {
						limit = {
							OR = {
								has_domicile_building_or_higher = watchtower_04
								has_domicile_building_or_higher = japanese_watch_house_04
							}
						}
						multiply = 4
					}
					else_if = {
						limit = {
							OR = {
								has_domicile_building_or_higher = watchtower_03
								has_domicile_building_or_higher = japanese_watch_house_03
							}
						}
						multiply = 3
					}
					else_if = {
						limit = {
							OR = {
								has_domicile_building_or_higher = watchtower_02
								has_domicile_building_or_higher = japanese_watch_house_02
							}
						}
						multiply = 2
					}
				}
			}
		}
		modifier = { # GUARDHOUSE
			scope:target.domicile ?= { has_domicile_building_or_higher = guardhouse_01 }
			desc = TARGET_DOMICILE_GUARDHOUSE_DESC
			add = {
				value = -5
				scope:target.domicile = {
					if = {
						limit = { has_domicile_building_or_higher = guardhouse_04 }
						multiply = 4
					}
					else_if = {
						limit = { has_domicile_building_or_higher = guardhouse_03 }
						multiply = 3
					}
					else_if = {
						limit = { has_domicile_building_or_higher = guardhouse_02 }
						multiply = 2
					}
				}
			}
		}
		modifier = { # WATCH HOUSE - JAPAN
			scope:target.domicile ?= { has_domicile_building_or_higher = japanese_watch_house_01 }
			desc = TARGET_DOMICILE_JAPANESE_WATCH_HOUSE_DESC
			add = {
				value = -2.5
				scope:target.domicile = {
					if = {
						limit = { has_domicile_building = japanese_watch_house_02 }
						multiply = 2
					}
					else_if = {
						limit = { has_domicile_building = japanese_watch_house_03 }
						multiply = 3
					}
					else_if = {
						limit = { has_domicile_building = japanese_watch_house_04 }
						multiply = 4
					}
					else_if = {
						limit = { has_domicile_building = japanese_watch_house_05 }
						multiply = 5
					}
					else_if = {
						limit = { has_domicile_building = japanese_watch_house_06 }
						multiply = 6
					}
				}
			}
		}
		modifier = { # BARRACKS
			scope:target.domicile ?= { has_domicile_building_or_higher = barracks_01 }
			desc = TARGET_DOMICILE_BARRACKS_DESC
			add = {
				value = -2.5
				scope:target.domicile = {
					if = {
						limit = { has_domicile_building_or_higher = barracks_06 }
						multiply = 6
					}
					else_if = {
						limit = { has_domicile_building_or_higher = barracks_05 }
						multiply = 5
					}
					else_if = {
						limit = { has_domicile_building_or_higher = barracks_04 }
						multiply = 4
					}
					else_if = {
						limit = { has_domicile_building_or_higher = barracks_03 }
						multiply = 3
					}
					else_if = {
						limit = { has_domicile_building_or_higher = barracks_02 }
						multiply = 2
					}
				}
			}
		}
		modifier = { # BARRACKS - JAPAN
			scope:target.domicile ?= { has_domicile_building_or_higher = japanese_barracks_02 }
			desc = TARGET_DOMICILE_JAPAN_BARRACKS_DESC
			add = {
				value = -2.5
				scope:target.domicile = {
					if = {
						limit = { has_domicile_building = japanese_barracks_02 }
						multiply = 6
					}
					else_if = {
						limit = { has_domicile_building = japanese_barracks_03 }
						multiply = 5
					}
					else_if = {
						limit = { has_domicile_building = japanese_barracks_04 }
						multiply = 4
					}
					else_if = {
						limit = { has_domicile_building = japanese_barracks_05 }
						multiply = 3
					}
					else_if = {
						limit = { has_domicile_building = japanese_barracks_06 }
						multiply = 2
					}
				}
			}
		}
		modifier = { # VILLA
			scope:target.domicile ?= { has_domicile_building_or_higher = estate_main_02 }
			add = {
				value = -2.5
				scope:target.domicile = {
					if = {
						limit = { has_domicile_building_or_higher = estate_main_05 }
						multiply = 4
					}
					else_if = {
						limit = { has_domicile_building_or_higher = estate_main_04 }
						multiply = 3
					}
					else_if = {
						limit = { has_domicile_building_or_higher = estate_main_03 }
						multiply = 2
					}
				}
			}
			desc = OWNER_DOMICILE_MAIN_DESC
		}
		modifier = { # MANOR
			scope:target.domicile ?= { has_domicile_building_or_higher = japanese_manor_main_02 }
			add = {
				value = -2.5
				scope:target.domicile = {
					if = {
						limit = { has_domicile_building = japanese_manor_main_03 }
						multiply = 2
					}
					if = {
						limit = { has_domicile_building = japanese_manor_main_04 }
						multiply = 3
					}
					else_if = {
						limit = { has_domicile_building = japanese_manor_main_05 }
						multiply = 4
					}
					else_if = {
						limit = { has_domicile_building = japanese_manor_main_06 }
						multiply = 5
					}
				}
			}
			desc = TARGET_DOMICILE_MAIN_JAPAN_DESC
		}
		modifier = {
			add = 10
			desc = eparch_scheme_bonus_with_value_desc
			scope:owner = { has_court_position = eparch_court_position }
			scope:target = {
				domicile ?= { domicile_location = owner.top_liege.capital_province }
			}
		}
		modifier = {
			add = 10
			desc = CB_COHESION_PARAMETER_DISCOUNT
			scope:owner.confederation ?= { has_cohesion_level_parameter = any_member_raid_estate_success_chance }
		}
	}
	base_secrecy = {
		add = secrecy_base_value
		add = countermeasure_apply_secrecy_maluses_value
		add = eparch_scheme_secrecy_bonus_value
	}

	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:calculating
		}
		add_scheme_starting_opportunities_intrigue_effect = yes
		# Fallback agents — a balanced config if you start the scheme from script.
		if = {
			limit = {
				scheme_owner = {
					NOT = { has_variable = agents_added }
				}
			}
			add_agent_slot = agent_scout
			add_agent_slot = agent_scout
			add_agent_slot = agent_muscle
			add_agent_slot = agent_infiltrator
			add_agent_slot = agent_lookout
		}
	}
	on_phase_completed = {
		suppress_scheme_follow_up_event_till_input_given_effect = yes
		raid_estate_scheme_prep_effect = yes
		cap_schemes_and_fire_reminders_effect = yes
	}
	on_hud_click  = { raid_estate_scheme_prep_effect = yes }
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			raid_estate_scheme_prep_effect = yes
			purge_ai_scheme_slots_effect = yes
		}
	}
	on_monthly = {
		hostile_scheme_monthly_discovery_chance_effect = yes
		if = {
			limit = {
				NOT = { exists = scope:discovery_event_happening }
			}
			fire_generic_contract_scheme_on_going_events_effect = yes
		}
	}

	## Invalidation
	on_invalidated = {
		scheme_target_character = { save_scope_as = target }
		scheme_owner = {
			save_scope_as = owner
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = raid_estate_invalidated_title
				left_icon = scope:target
				if = { # Not noble family head
					limit = {
						NOT = {
							scope:target = {
								any_held_title = { is_noble_family_title = yes }
								holder.house.house_head ?= this
							}
						}
					}
					custom_tooltip = scheme_target_not_noble_family
				}
				else_if = { # No estate
					limit = {
						NOT = {
							scope:target.domicile ?= { is_domicile_type = estate }
						}
					}
					custom_tooltip = scheme_target_not_estate_owner
				}
				else_if = { # Not noble family head
					limit = {
						NOT = {
							scope:owner = {
								any_held_title = { is_noble_family_title = yes }
								house.house_head ?= this
							}
						}
					}
					custom_tooltip = scheme_owner_not_noble_family
				}
				else_if = { # Not same realm
					limit = { scope:owner.top_liege != scope:target.top_liege }
					custom_tooltip = scheme_owner_not_same_realm
				}
			}
		}
	}
}
