namespace = SMKingdom

narrative_event = {
	id = SMKingdom.1
	title = SMKingdom.1.title
	desc = SMKingdom.1.desc
	picture = GFX_evt_siege
	border = GFX_event_narrative_frame_war
	major = yes
	is_triggered_only = yes
	
	option = {
		name = SMKingdom.1.option
		trigger = {
			character = ROOT
		}
	}
	option = {
		name = SMKingdom.1.option2
		trigger = {
			NOT= {
				character = ROOT
			}
		}
	}
}

narrative_event = {
	id = SMKingdom.2
	title = SMKingdom.2.title
	desc = SMKingdom.2.desc
	picture = GFX_evt_siege
	border = GFX_event_narrative_frame_war
	major = yes
	is_triggered_only = yes
	
	option = {
		name = SMKingdom.2.option
		trigger = {
			character = ROOT
		}
	}
	option = {
		name = SMKingdom.2.option2
		trigger = {
			NOT= {
				character = ROOT
			}
		}
	}
}

narrative_event = {
	id = SMKingdom.3
	title = SMKingdom.1.title
	desc = SMKingdom.1.CBdesc
	picture = GFX_evt_siege
	border = GFX_event_narrative_frame_war
	major = yes
	is_triggered_only = yes
	
	option = {
		name = SMKingdom.1.option
		trigger = {
			character = ROOT
		}
	}
	option = {
		name = SMKingdom.1.option2
		trigger = {
			NOT= {
				character = ROOT
			}
		}
	}
}

narrative_event = {
	id = SMKingdom.4
	title = SMKingdom.2.title
	desc = SMKingdom.2.CBdesc
	picture = GFX_evt_siege
	border = GFX_event_narrative_frame_war
	major = yes
	is_triggered_only = yes
	
	option = {
		name = SMKingdom.2.option
		trigger = {
			character = ROOT
		}
	}
	option = {
		name = SMKingdom.2.option2
		trigger = {
			NOT= {
				character = ROOT
			}
		}
	}
}

# create empire title
character_event = {
	id = SMKingdom.5
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		ROOT = {
			primary_title = {
				hidden_tooltip = {
				create_title = {
					tier = EMPEROR
					landless = no
					temporary = no
					custom_created = yes
					culture = ROOT
					holder = ROOT
					base_title = THIS
					copy_title_laws = yes
				}

				
					ROOT = {
						primary_title = {
							holder_scope = {
								any_title_under = {
									limit = {
										tier = KING
										empire = {
											has_holder = no
										}
									}
									de_jure_liege = PREVPREV
								}
								any_title_under = {
									limit = {
										tier = DUKE
										location = {
											kingdom = {
												empire = {
													has_holder = no
												}
												
												NOT = { de_jure_liege_or_above = PREVPREVPREVPREV }
												ROOT = {
													completely_controls = PREV
												}
											}
										}
									}
									location = {
										kingdom = {
											de_jure_liege = PREVPREVPREVPREV
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	option = {
		name = SMKingdom.5.option
	}
}

# create king title
character_event = {
	id = SMKingdom.6
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		ROOT = {
			primary_title = {
				hidden_tooltip = {
					create_title = {
						tier = KING
						landless = no
						temporary = no
						custom_created = yes
						culture = ROOT
						holder = ROOT
						base_title = THIS
						copy_title_laws = yes
					}

					empire = {
						ROOT = {
							primary_title = {
								de_jure_liege = PREVPREV
							}
						}
					}

					ROOT = {
						primary_title = {
							holder_scope = {
								any_title_under = {
									limit = {
										tier = DUKE
										kingdom = {
											has_holder = no
										}
									}
									de_jure_liege = PREVPREV
								}
								any_title_under = {
									limit = {
										tier = COUNT
										location = {
											duchy = {
												kingdom = {
													has_holder = no
												}
												
												NOT = { de_jure_liege_or_above = PREVPREVPREVPREV }
												ROOT = {
													completely_controls = PREV
												}
											}
										}
									}
									location = {
										duchy = {
											de_jure_liege = PREVPREVPREVPREV
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	option = {
		name = SMKingdom.6.option
	}
}

character_event = {
	id = SMKingdom.8
	hide_window = yes
	is_triggered_only = yes
	
	# Uses options to get around randomness seed not changing
	
	option = {
		name = SMKingdom.8.option
		# Do nothing
		ai_chance = {
			factor = 1
		}
	}
	
	option = {
		name = SMKingdom.8.option2
		if = {
			limit = {
				FROM = {
					independent = yes
				}
			}
			set_defacto_liege = ROOT 
		}
		# break free from current liege, but now only report to the higher up liege
		if = {
			limit = {
				FROM = {
					independent = no
				}
			}
			FROM = {
				liege = {
					ROOT = {
						set_defacto_liege = PREV 
					}
				}
			}
		}		
		FROM = {
			opinion = { modifier = opinion_angry who = ROOT years = 5 }
		}
		opinion = { modifier = opinion_dislike who = FROM years = 5 }
		ai_chance = {
			factor = 1
			# Increased chance for kings to break away
			modifier = {
				factor = 4
				tier = king 
			}
			# Very much like leige
			modifier = {
				factor = 0
				opinion = { who = FROM value = 70 } 
				FROM = {
					NOT = { character = ROOT }
					is_liege_of = ROOT
				}
			}
			# Dislike the leige
			modifier = {
				factor = 2
				NOT = { opinion = { who = FROM value = 0 } } 
				FROM = {
					NOT = { character = ROOT }
					is_liege_of = ROOT
				}
			}
			modifier = {
				factor = 0.5
				de_jure_liege = FROM
			}
			modifier = {
				factor = 1.25
				tier = duke
			}
			modifier = {
				factor = 0.25
				tier = count
			}
			modifier = {
				factor = 0
				tier = baron
			}
			modifier = {
				factor = 4
				trait = ambitious
			}
			modifier = {
				factor = 1.5
				trait = arbitrary
			}
			modifier = {
				factor = 3
				trait = deceitful
			}
			modifier = {
				factor = 0.1
				trait = craven
			}
			modifier = {
				factor = 4
				trait = lunatic
			}
			modifier = {
				factor = 0.1
				trait = imbecile
			}
			modifier = {
				factor = 0.01
				trait = content
			}
			# Council members not as likely to go independent
			modifier = {
				factor = 0.1
				is_voter = yes
			}
			# If council discontent more likely to break away
			modifier = {
				factor = 3
				is_voter = yes
				FROM = {
					OR = {
						is_council_content = no
						NOT = { primary_title = { has_law = war_voting_power_1 } }
					}
				}
			}
		}
	}
}

character_event = {
	id = SMKingdom.9
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				tier = emperor
			}
			if = {
				limit = {
					num_of_emperor_titles = 2
				}
				character_event = { id = SMKingdom.13}
				#character_event = { id = SMKingdom.20}
				#any_vassal = {
				#	character_event = {id = SMKingdom.8}
				#}
			}
			if = {
				limit = {
					NOT = { num_of_emperor_titles = 2 }
				}
				character_event = {id = SMKingdom.10}
			}
			break = yes
		}
		if = {
			limit = {
				num_of_king_titles = 2
			}
			character_event = { id = SMKingdom.13}
			#character_event = { id = SMKingdom.20}
			#any_vassal = {
			#	character_event = {id = SMKingdom.8}
			#}
			break = yes
		}
		character_event = {id = SMKingdom.10}
	}
	
	option = {
		name = OK
	}
}

character_event = {
	id = SMKingdom.10
	hide_window = yes
	is_triggered_only = yes
		
	# Some vassals turn independent
	option = {
		name = SMKingdom.10.name
		ai_chance = {
			factor = 1
		}
		any_vassal = {
			character_event = {id = SMKingdom.8}
		}
	}
	
	# Title is destroyed
	option = {
		name = SMKingdom.10.name2
		character_event = {id = SMKingdom.12}
		ai_chance = {
			factor = 0.1
			modifier = {
				factor = 10
				NOT = { realm_size = 15 }
				tier = king
			}
			modifier = {
				factor = 10
				NOT = { realm_size = 80 }
				tier = emperor
			}
			modifier = {
				factor = 10
				has_law = crown_authority_0
			}
			modifier = {
				factor = 5
				has_law = crown_authority_1
			}
			modifier = {
				factor = 1
				has_law = crown_authority_2
			}
			modifier = {
				factor = 0.5
				has_law = crown_authority_3
			}
			modifier = {
				factor = 0.1
				has_law = crown_authority_4
			}
			modifier = {
				factor = 0.1
				has_dlc = "Zeus"
				NOT = {	calc_true_if = {
						amount = 1
						has_law = law_voting_power_1
						has_law = banish_voting_power_1
						has_law = execution_voting_power_1
						has_law = revoke_title_voting_power_1
						has_law = grant_title_voting_power_1
						has_law = imprison_voting_power_1
						has_law = war_voting_power_1
					}
				}
			}
			modifier = {
				factor = 0.5
				has_dlc = "Zeus"
				calc_true_if = {
					amount = 1
					has_law = law_voting_power_1
					has_law = banish_voting_power_1
					has_law = execution_voting_power_1
					has_law = revoke_title_voting_power_1
					has_law = grant_title_voting_power_1
					has_law = imprison_voting_power_1
					has_law = war_voting_power_1
				}
			}
			modifier = {
				factor = 2
				has_dlc = "Zeus"
				calc_true_if = {
					amount = 2
					has_law = law_voting_power_1
					has_law = banish_voting_power_1
					has_law = execution_voting_power_1
					has_law = revoke_title_voting_power_1
					has_law = grant_title_voting_power_1
					has_law = imprison_voting_power_1
					has_law = war_voting_power_1
				}
			}
			modifier = {
				factor = 5
				has_dlc = "Zeus"
				calc_true_if = {
					amount = 4
					has_law = law_voting_power_1
					has_law = banish_voting_power_1
					has_law = execution_voting_power_1
					has_law = revoke_title_voting_power_1
					has_law = grant_title_voting_power_1
					has_law = imprison_voting_power_1
					has_law = war_voting_power_1
				}
			}
			modifier = {
				factor = 2 # stacks with previous factors
				has_dlc = "Zeus"
				calc_true_if = {
					amount = 6
					has_law = law_voting_power_1
					has_law = banish_voting_power_1
					has_law = execution_voting_power_1
					has_law = revoke_title_voting_power_1
					has_law = grant_title_voting_power_1
					has_law = imprison_voting_power_1
					has_law = war_voting_power_1
				}
			}
			# Special law enforced from revolt
			modifier = {
				factor = 2
				has_dlc = "Zeus"
				has_law = succession_voting_power_1
			}
			modifier = {
				factor = 0.01
				tier = duke
			}
			modifier = {
				factor = 2
				is_council_content = no
			}
			modifier = {
				factor = 0
				tier = count
			}
			modifier = {
				factor = 0
				tier = baron
			}
			# Specialized titles are not destroyable i.e the pope
			modifier = {
				factor = 0
				primary_title = {
					is_primary_type_title = yes
				}
			}
		}
	}

}

# Just for independence
character_event = {
	id = SMKingdom.11
	hide_window = yes
	is_triggered_only = yes
		
	# Some vassals turn independent
	immediate = {
		name = SMKingdom.11.name
		any_vassal = {
			character_event = {id = SMKingdom.8}
		}
	}
	
	option = {
		name = OK
	}
}

# Narrative for title being destroyed
narrative_event = {
	id = SMKingdom.12
	is_triggered_only = yes
	
	title = SMKingdom.12.title
	desc = SMKingdom.12.desc
	picture = GFX_evt_siege
	border = GFX_event_narrative_frame_war
	is_triggered_only = yes
	
	option = {
		name = SMKingdom.12.option
		primary_title = {
			destroy_landed_title = THIS
		}
	}
}

# Narrative for your non primary titles being shared out by the council
narrative_event = {
	id = SMKingdom.13
	is_triggered_only = yes
	
	title = SMKingdom.13.title
	desc = SMKingdom.13.desc
	picture = GFX_evt_siege
	border = GFX_event_narrative_frame_war
	is_triggered_only = yes
	
	option = {
		name = SMKingdom.13.option
		character_event = { id = SMKingdom.20 }
	}
}

# If multiple titles at your rank, on faction war loss titles (except primary) will be shared amongst your highest strength council members (vassals without conclave or no council)

character_event = {
	id = SMKingdom.20
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {

	}
	
	option = {
		name = SMKingdom.20.option
		any_vassal = {
			character_event = {id = SMKingdom.8}
		}
		ai_chance = {
			factor = 1
		}
	}
	
	option = {
		name = SMKingdom.20.option2
		random_demesne_title = {
			limit = {
				OR = {
					AND = {
						tier = emperor
						ROOT = {
							primary_title = {
								tier = emperor
							}
						}
					}
					AND = {
						tier = king
						ROOT = {
							primary_title = {
								tier = king
							}
						}
					}
				}
				temporary = no
				ROOT = {
					primary_title = {
						NOT = { title = PREVPREV }
					}
				}
			}
			ROOT = {
				most_popular_vassal = {
					limit = {
						higher_tier_than = BARON
						prisoner = no
						NOR = {
							trait = in_hiding
							trait = incapable
							trait = eunuch
						}
						age = 16
						OR = {
							is_female = no
							AND = {
								is_female = yes
								ROOT = { has_law = true_cognatic_succession }
							}
						}
					}
					grant_title = PREVPREV
					ROOT = {
						character_event = {id = SMKingdom.20 days = 1} # Recursive as most popular vassal will change as each title is dished out by the council
					}
				}
			}
		}
		ai_chance = {
			factor = 1
			modifier = {
				factor = 0.1
				NOT = {
					# Any of these laws enabled will make a chance for council to grant the multiple titles to most popular vassals
					OR = {
						has_law = revoke_title_voting_power_1
						has_law = grant_title_voting_power_1
						has_law = succession_voting_power_1
						has_law = crown_authority_0
						has_law = crown_authority_1
					}
				}
			}
			modifier = {
				factor = 2
				is_council_content = no
			}
		}
	}

}

