﻿abdicate_throne_decision = { 
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_abdicate.dds"
	}
	decision_group_type = major
	desc = abdicate_throne_decision_desc

	# AI looks at the decision every four years. Could do lower, don't wanna impact performance as there are too many people it is shown to at any given point.
	ai_check_interval = 48
	
	#Idk why this has to be here but it does
	ai_potential = { 
		always = yes
	}

	#Will add dynamism later. For now, it's just a 40% chance if the game rule is set.
	ai_will_do = { 
		base = 0

		modifier = {
			add = 40
			trigger = {
				has_game_rule = ai_abdicates_conditionally
			}
		}

		modifier = {
			add = 100
			trigger = {
				has_game_rule = ai_abdicates_when_possible
			}
		}

		modifier = {
			multiply = 0
			trigger = {
				has_game_rule = ai_abdicates_never
			}
		}
	}

	###Decision visibility checks.
	is_shown = {
		OR = {
			age >= 50
			stress_level >= 2
			health <= 2
			has_trait = excommunicated
			has_trait = content
			has_trait = lazy
			has_trait = blind
			has_trait = depressed_1
			has_trait = depressed_genetic
			AND = {
				faith = { has_doctrine = tenet_carnal_exaltation }
				OR = {
					has_trait = eunuch
					has_trait = impotent
				}
			}
		}
		NOR = {
			highest_held_title_tier = tier_county
			is_landless_adventurer = yes
		}
	}

	###King go bye bye 
	effect = {
		save_scope_as = exrulah
		
		# This was meant to trigger a background event that took away renown from you. Need to re-code one in, since the last one didn't work.
		#trigger_event = { 
		#	id = abdrenown_event.1 
		#} 

		primary_heir = {
			save_scope_as = nurulah
			trigger_event = {
				id = abdicate_event.3
				days = 7
			}
		}

		custom_tooltip = { 
			text = abdicate_throne_decision_result
			depose = yes
		 }

		add_trait = abdicator_trait
	}

	###Decision checks
	is_valid = {
		
		##None of the following can be met

		NOT = {
			has_trait = ambitious 
		}

		trigger_if = {
			limit = { is_commanding_army = yes }
			NOT = { is_commanding_army = yes }
		}

		trigger_if = {
			limit = { root = { is_imprisoned = yes } }
			NOT = { root = { is_imprisoned = yes } }
		}

		trigger_if = {
			limit = {
				root.primary_heir = { is_imprisoned = yes }
			}
			NOT = {
				root.primary_heir = { is_imprisoned = yes }
			}
		}

		# I've decided to let Emperors abdicate. I'd need to add a whole system where it's checked against both powerful vassals and the pope to make it realistic.
		#trigger_if = {
		#	limit = { 
		#		AND = {
		#			capital_province =  { geographical_region = world_europe }
		#			has_religion = religion:christianity_religion 
		#		}	
		#		NOT = { 
		#			top_liege = { has_title = title:e_byzantium } 
		#		}
		#	}
		#	
		#	NOT = { has_title = title:e_hre	}
		#
		#}

		trigger_if = { 
			limit = { 
				primary_heir = { age < 12 }
			}
			primary_heir = { age >= 12 }
		}

		trigger_if = {
			limit = { 
				primary_heir = { age < 16 } 
			}
			custom_tooltip = { 
				text = youneed2raiseyokids_tt
				primary_heir = {
					any_relation = {
						type = guardian
					}
				}
			}
		}
		
		trigger_if = {
			limit = {
				root.primary_title = {
					NOR = {
						has_title_law = princely_elective_succession_law
						has_title_law = gaelic_elective_succession_law
						has_title_law = feudal_elective_succession_law
						has_title_law = saxon_elective_succession_law
					}
				}
				NOT = {
					primary_heir.dynasty = root.dynasty
				}				
			}
			primary_heir.dynasty = root.dynasty
		}

		trigger_if = {
			limit = {
				root.primary_heir = root.liege
			}
			custom_tooltip = {
				text = yourheirisyoliege_tt
				NOT = {
					root.primary_heir = root.liege
				}
			}
		}

		trigger_if = {
			limit = { has_trait = greatest_of_khans }
			NOT = { has_trait = greatest_of_khans }
		}
		
		##One of these conditions must be met
		OR = {
			
			age >= 55
			
			#Only if faith has communion, check appears for excommunicayshun
			trigger_if = {
				limit = { 
					faith = { has_doctrine = tenet_communion } 
				}
				custom_tooltip = { 
					text = youareexcommed_tt 
					AND = {
						#YOU DONT NEED THE "faith = { has_doctrine = tenet_communion }" BECAUSE it's already in the limit
						#Thanks fernando
						#faith = { has_doctrine = tenet_communion }
						has_trait = excommunicated 
					}
				}
			}
			
			#If ya blind, game'll tell ya you can abdie
			trigger_if = { 
				limit = { has_trait = blind } 
				has_trait = blind 
			}
			
			#Check for lev 3 stress or either kind of depression. Custom tooltip is so both variants of melancho can be checked for without both appearing.

			custom_tooltip = { 
				text = youneedtherapy_tt 
				OR = {	
					has_trait = depressed_1 
					has_trait = depressed_genetic
					stress_level >= 3 
				}
			}
		
			#Custom tooltip turns 4 lines into 1. The OR has to be there, otherwise the box doesn't get checked
			custom_tooltip = { 
				text = youhaveligma_tt
				OR = {
					has_trait = cancer
					has_trait = bubonic_plague
					has_trait = typhus
					has_trait = smallpox 
				}
			}

			trigger_if = {
				limit = {
					OR = {
						health <= death_chance_dying_health
						health <= 1.2 
					}
					NOR = {
						has_trait = cancer
						has_trait = bubonic_plague
						has_trait = typhus
						has_trait = smallpox 
					}
				}
				custom_tooltip = { text = youareinpoorhealth_tt }
			}
			
			trigger_if = {
				limit = { 
					faith = {
						has_doctrine = tenet_carnal_exaltation
					}
				}
				AND = {
					faith = { has_doctrine = tenet_carnal_exaltation }
					OR = {
						has_trait = eunuch
						has_trait = impotent
					}
				}
			}
				
			#If lazy/content and abdicating without any other cause, heir must be an adult.
			AND = {
				custom_tooltip = { 
					text = youarelazyorcontent_tt 
					OR = {
						has_trait = content
						has_trait = lazy
					}
				}
				primary_heir = { age >= 16 }
			}

		}
	}

	###Cost - Excommunication renown cost moved to hidden event, gold cost is a modifier, so this is useless.
	#cost = { prestige = { value = 0 } }

}