interaction = {
    key = sale_of_titles
    random_seed = crown_land_share
	cooldown_months = 60

    trigger = {
        crown_land_share = 10
        NOT = { num_of_rebel_armies = 1 }
		NOT = { num_of_rebel_controlled_provinces = 1 }
		hidden_trigger = { 
			NOT = { is_subject_of_type = xorme_moh_administrative_district }
			has_any_estates = yes 
		}
    }
    effect = {
		if = {
			limit = {
				religion = inti
				check_variable = {
					which = inca_amount_of_mummies
					value = 1
				}
			}
			INC_sell_crownland_modified_by_mummies = yes
		}
		else_if = {
			limit = { 
				NOT = { has_government_attribute = no_crownland_money_loss_to_age } 
				NOT = { has_country_flag = enable_full_ducat_gain_from_selling_crownland_flag }
			}
			if = {
				limit = { current_age = age_of_discovery }
				add_years_of_estate_land_income = 2.5
			}
			else_if = {
				limit = { current_age = age_of_reformation }
				add_years_of_estate_land_income = 2.0
			}
			else_if = {
				limit = { current_age = age_of_absolutism }
				add_years_of_estate_land_income = 1.5
			}
			else_if = {
				limit = { current_age = age_of_revolutions }
				add_years_of_estate_land_income = 1.0
			}
		}
		else = {
			add_years_of_estate_land_income = 2.5
		}
		if = {
			limit = {
				NOT = { has_government_attribute = can_sell_crownland_to_single_estates }
				NOT = { has_country_flag = can_sell_crownland_to_estates_targeted_flag }
			}
			custom_tooltip = ESTATES_SALE_OF_TITLES_DESC
			give_estate_land_share_gigantic = { estate = all }
			add_estate_loyalty = {
				estate = all
				loyalty = 10
				short = yes
			}
			if = {
				limit = {
					has_government_attribute = selling_titles_increases_pa
				}
				add_patriarch_authority = 0.05
			}
		}
		else = {
			country_event = {
				id = estate_privileges_and_agendas_events.6
			}
		}
		if = {
			limit = {
				has_country_flag = recent_land_distribution
			}
			clr_country_flag = recent_land_distribution
		}
		set_country_flag = recent_land_distribution
		on_sale_of_titles_effect = yes
    }
	ai_will_do = {
		factor = 35
		modifier = {
			factor = 0
			OR = {
				NOT = { crown_land_share = 30 }
				AND = {
					is_absolutism_active = yes
					NOT = { crown_land_share = 45 }
				}
			}
		}
		modifier = {
			factor = 0.5
			OR = {
				NOT = { crown_land_share = 35 }
				AND = {
					is_absolutism_active = yes
					NOT = { crown_land_share = 50 }
				}
			}
		}
		modifier = {
			factor = 0
			NOT = { num_of_loans = 1 }
		}
		modifier = {
			factor = 0.5
			is_in_deficit = no
		}
		modifier = {
			factor = 1.25
			has_any_disaster = yes
		}
		modifier = {
			factor = 1.5
			num_of_loans = 2
			is_in_deficit = yes
		}
		modifier = {
			factor = 1.5
			num_of_loans = 5
		}
		modifier = {
			factor = 1.5
			num_of_loans = 10
		}
		modifier = {
			factor = 1.5
			num_of_loans = 15
		}
		modifier = {
			factor = 1.5
			num_of_loans = 20
		}
		modifier = {
			factor = 1.2
			NOT = {
				estate_loyalty = {
					estate = all
					loyalty = 25
				}
			}
		}
		modifier = {
			factor = 1.1
			crown_land_share = 80
		}
		modifier = {
			factor = 0.75
			num_of_powerful_estates = 1
		}
		modifier = {
			factor = 0.8
			has_country_flag = recent_land_distribution
			NOT = {
				had_country_flag = {
					flag = recent_land_distribution
					days = 5475
				}
			}
		}
		modifier = {
			factor = 1.2
			had_country_flag = {
				flag = recent_land_distribution
				days = 10950
			}
		}
		modifier = {
			factor = 0.5
			has_country_flag = recent_land_seizure
			NOT = {
				had_country_flag = {
					flag = recent_land_seizure
					days = 5475
				}
			}
		}
	}
}

interaction = {
    key = summon_the_diet
    random_seed = random
	cooldown_months = 60

    trigger = {
		hidden_trigger = {
			NOT = { is_subject_of_type = xorme_moh_administrative_district }	
		}
		if = {
			limit = {
				ROOT = { ai = no }
			}
			ROOT = {
				hidden_trigger = { has_any_estates = yes }
				NOT = { has_any_active_estate_agenda = yes }
				NOT = { has_government_attribute = blocked_call_diet }
			}
		}
		else_if = {
			limit = {
				ROOT = { xorme_ai_valid_nation = yes }
				has_global_flag = xorme_ai_initialization_happened			
			}
			ROOT = {
				hidden_trigger = { has_any_estates = yes }
				NOT = { has_any_active_estate_agenda = yes }
				NOT = { has_government_attribute = blocked_call_diet }
				is_at_war = no
				is_free_or_tributary_trigger = yes
				has_estate = xorme_ai_estate
			}
		}
		else = {
			always = no
		}
    }

    effect = {
        custom_tooltip = ESTATES_SUMMON_THE_DIET_DESC
	
		hidden_effect = {
			#PLAYER AGENDA
			if = {
				limit = { ROOT = { ai = no } }
				country_event = { id = estate_privileges_and_agendas_events.3 } 
			}
			#AI AGENDA
			else = {
				country_event = { id = xorme_ai_estates_and_agendas.1 }
			}  
		}
		custom_tooltip = SEPERATOR_TOOLTIP
		if = {
			limit = {
				has_country_flag = recent_estate_agenda
			}
			clr_country_flag = recent_estate_agenda
		}
		set_country_flag = recent_estate_agenda

		if = {
			limit = { NOT = { has_government_attribute = diet_increases_not_influence } }
			add_estate_influence_modifier = {
				estate = all
				desc = EST_VAL_DIET_SUMMONED
				duration = 7300
				influence = 5
				short = yes
			}
		}
        add_estate_loyalty = {
            estate = all
            loyalty = 5
            short = yes
        }
		if = {
			limit = {
				has_government_attribute = republican_tradition_from_diets
			}
			add_scaled_republican_tradition = 2
		}
		on_summon_the_diet_effect = yes
    }
	ai_will_do = {
		factor = 10.0
		modifier = {
			factor = 0
			crown_land_share = 85
		}
		modifier = {
			factor = 0
			estate_loyalty = {
				estate = all
				loyalty = 80
			}
		}
		modifier = {
			factor = 0.5
			estate_loyalty = {
				estate = all
				loyalty = 60
			}
		}
		modifier = {
			factor = 2.0
			estate_loyalty = {
				estate = all
				loyalty = 45
			}
		}
		modifier = {
			factor = 10.0
			ROOT = { xorme_ai_valid_nation = yes }
		}
	}
}

interaction = {
    key = seize_land
    random_seed = random
	cooldown_months = 60

    trigger = {
    	hidden_trigger = {
			NOT = { is_subject_of_type = xorme_moh_administrative_district }
		}
		hidden_trigger = { has_any_estates = yes }
        NOT = { crown_land_share = 100 }
        NOT = { num_of_rebel_armies = 1 }
		NOT = { num_of_rebel_controlled_provinces = 1 }
		#The following if/else_if-statements match those in estate_privileges_and_agendas_events.2.a. If those change, these should too.
		if = {
			limit = {
				OR = {
					has_estate_privilege = estate_brahmins_statutory_rights
					AND = {
						religion = hinduism
						has_estate = estate_brahmins
					}
				}
				has_estate = estate_brahmins
			}
			NOT = { has_estate_privilege = estate_brahmins_statutory_rights }
		}
		else_if = {
			limit = {
				OR = {
					has_estate_privilege = estate_nobles_statutory_rights
					AND = {
						has_estate = estate_nobles
						government = monarchy
					}
				}
			}
			NOT = { has_estate_privilege = estate_nobles_statutory_rights }
		}
		else_if = {
			limit = {
				OR = {
					has_estate_privilege = estate_burghers_statutory_rights
					AND = {
						has_estate = estate_burghers
						government = republic
					}
				}
			}
			NOT = { has_estate_privilege = estate_burghers_statutory_rights }
		}
		else_if = {
			limit = {
				OR = {
					has_estate_privilege = estate_church_statutory_rights
					AND = {
						has_estate = estate_church
						government = theocracy
					}
				}
			}
			NOT = { has_estate_privilege = estate_church_statutory_rights }
		}
        is_at_war = no
		if = {
			limit = { has_government_attribute = can_not_seize_land }
			NOT = { has_government_attribute_short_desc = can_not_seize_land }
		}
    }
    effect = {
		
		if = {
			limit = {
				has_country_flag = recent_land_seizure
			}
			clr_country_flag = recent_land_seizure
		}
		set_country_flag = recent_land_seizure
		if = {
			limit = {
				religion = inti
				check_variable = {
					which = inca_amount_of_mummies
					value = 1
				}
			}
			INC_seize_crownland_modified_by_mummies = yes
		}
		else_if = {
			limit = {
				NOT = { has_government_attribute = can_seize_crownland_of_single_estates }
				NOT = { has_country_flag = can_seize_crownland_to_estates_targeted_flag }
			}
			custom_tooltip = ESTATES_SEIZE_LAND_DESC
			take_estate_land_share_massive = { estate = all }
			custom_tooltip = SEPERATOR_TOOLTIP
			if = {
				limit = {
					has_country_flag = ned_lands_generality_flag
				}
				add_estate_loyalty = {
					estate = all
					loyalty = -10
					short = yes
				}
			}
			else_if = {
				limit = {
					has_government_attribute = seizing_land_loyalty
				}
				add_estate_loyalty = {
					estate = all
					loyalty = -15
					short = yes
				}
			}
			else = {
				add_estate_loyalty = {
					estate = all
					loyalty = -20
					short = yes
				}
			}
			custom_tooltip = ESTATES_SEIZE_LAND_EXCEPTION_DESC
			re_add_loyalty_from_seized_land = yes
			if = {
				limit = { NOT = { has_government_attribute = seizing_land_no_rebels } }
				custom_tooltip = seize_land_rebels_tt
			}
			if = {
				limit = {
					NOT = {
						estate_loyalty = {
							estate = all
							loyalty = 50
						}
					}
					estate_loyalty = {
						estate = all
						loyalty = 30
					}
				}
			}
			else_if = { 
				limit = {
					NOT = {
						estate_loyalty = {
							estate = all
							loyalty = 30
						}
					}
					estate_influence = {
						estate = all
						influence = 1
					}				
				}
				spawn_rebels_seize_land_switch = yes
			}
		}
		else = {
			country_event = {
				id = estate_privileges_and_agendas_events.7
			}
		}
		if = {
			limit = {
				has_government_attribute = seizing_land_takes_karma
			}
			add_karma = -10
		}
		if = {
			limit = { has_government_attribute = theocratic_monarchy_mechanic }
			change_statists_vs_orangists = 0.05
		}
		on_seize_land_effect = yes
	}
	ai_will_do = {
		factor = 75
		modifier = {
			factor = 0
			# This check is needed to prevent collision with day-1 event theocracy.1
			uses_devotion = yes
			has_heir = no
		}
		modifier = {
			factor = 0
			# This check is needed to prevent collision with day-1 event theocracy.1
			ai = yes
			NOT = { is_year = 1445 }
		}
		
		modifier = {
			factor = 0
			# This is a hack for countries with enclaves that can be hard to get to, to kill the rebels
			OR = {
				tag = TEU
				tag = KOL
				# Add more as necessary
			}
			NOT = {
				estate_loyalty = {
					estate = all
					loyalty = 50
				}
			}
		}
		modifier = {
			factor = 0.2
			NOT = {
				estate_loyalty = {
					estate = all
					loyalty = 50
				}
			}
			NOT = {
				army_size = 6
			}
		}
		modifier = {
			factor = 0.2

			# This custom event target is set by code. If ai_will_do is higher when this is set (and >50) than when it isn't,
			# the AI will raise maintenance and wait until the event target doesn't increase the ai_will_do score
			NOT = { has_saved_event_target = ai_suppose_full_maintenance }

			NOT = {
				estate_loyalty = {
					estate = all
					loyalty = 50
				}
			}

			OR = {
				NOT = { land_maintenance = 0.9 }
				num_mothballed_forts = 1
				NOT = { all_regiments_morale_percent = 90 }
				any_owned_province = {
					fort_level = 1
					NOT = { garrison = 400 }
				}
			}
		}
		modifier = {
			factor = 0
			crown_land_share = 30
			has_any_disaster = yes
			NOT = { #sorry, scripted triggers glitched out
				OR = {
					has_disaster = estate_brahmins_disaster
					has_disaster = estate_burghers_disaster
					has_disaster = estate_church_disaster
					has_disaster = estate_cossacks_disaster
					has_disaster = estate_dhimmi_disaster
					has_disaster = estate_jains_disaster
					has_disaster = estate_maratha_disaster
					has_disaster = estate_nobility_disaster
					has_disaster = estate_nomadic_tribes_disaster
					has_disaster = estate_rajput_disaster
					has_disaster = estate_vaisyas_disaster
				}
			}
		}
		modifier = {
			factor = 0.2
			crown_land_share = 30
			NOT = {
				estate_loyalty = {
					estate = all
					loyalty = 50
				}
			}
			if = {
				limit = { num_of_cities = 50 }
				NOT = { army_size_percentage = 0.2 }
			}
			else_if = {
				limit = { num_of_cities = 25 }
				NOT = { army_size_percentage = 0.3 }
			}
			else = {
				NOT = { army_size_percentage = 0.5 }
			}
			if = {
				limit = { is_emperor = yes }
				NOT = { army_size = 50 }
			}
			NOT = { army_size = 100 }
			NOT = { #sorry, scripted triggers glitched out
				OR = {
					has_disaster = estate_brahmins_disaster
					has_disaster = estate_burghers_disaster
					has_disaster = estate_church_disaster
					has_disaster = estate_cossacks_disaster
					has_disaster = estate_dhimmi_disaster
					has_disaster = estate_jains_disaster
					has_disaster = estate_maratha_disaster
					has_disaster = estate_nobility_disaster
					has_disaster = estate_nomadic_tribes_disaster
					has_disaster = estate_rajput_disaster
					has_disaster = estate_vaisyas_disaster
					has_disaster_progress = {
						disaster = estate_brahmins_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_burghers_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_church_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_cossacks_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_dhimmi_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_jains_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_maratha_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_nobility_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_nomadic_tribes_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_rajput_disaster
						value = 5
					}
					has_disaster_progress = {
						disaster = estate_vaisyas_disaster
						value = 5
					}
				}
			}
		}
		modifier = {
			factor = 1.5
			is_absolutism_active = yes
			NOT = { num_of_rebel_armies = 1 }
		}
		modifier = {
			factor = 1.5
			is_absolutism_active = yes
			NOT = { average_unrest = 1 }
			NOT = { num_of_rebel_armies = 1 }
		}
		modifier = {
			factor = 2
			NOT = { crown_land_share = 20 }
		}
		modifier = {
			factor = 1.5
			NOT = { crown_land_share = 30 }
		}
		modifier = {
			factor = 1.2
			NOT = { crown_land_share = 35 }
		}
		modifier = {
			factor = 1.2
			NOT = { crown_land_share = 50 }
		}
		modifier = {
			factor = 1.1
			stability = 2
		}
		modifier = {
			factor = 1.1
			stability = 3
		}
		modifier = {
			factor = 0.5
			average_unrest = 3
		}
		modifier = {
			factor = 5.0
			estate_loyalty = {
				estate = all
				loyalty = 50
			}
		}
		modifier = {
			factor = 10
			OR = {
				has_disaster = estate_brahmins_disaster
				has_disaster = estate_burghers_disaster
				has_disaster = estate_church_disaster
				has_disaster = estate_cossacks_disaster
				has_disaster = estate_dhimmi_disaster
				has_disaster = estate_jains_disaster
				has_disaster = estate_maratha_disaster
				has_disaster = estate_nobility_disaster
				has_disaster = estate_nomadic_tribes_disaster
				has_disaster = estate_rajput_disaster
				has_disaster = estate_vaisyas_disaster
				has_disaster_progress = {
					disaster = estate_brahmins_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_burghers_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_church_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_cossacks_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_dhimmi_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_jains_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_maratha_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_nobility_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_nomadic_tribes_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_rajput_disaster
					value = 5
				}
				has_disaster_progress = {
					disaster = estate_vaisyas_disaster
					value = 5
				}
			}
		}
	}
}