﻿##################################################
###
### Grand Ambitions
### by Chad Uhl
### 
### The head of a Powerful Family has ambitions for the throne.
###
##################################################

grand_ambitions_story_cycle = {

	on_setup = {
		set_variable = {
			name = eunuch
			value = no 
		}
	}

	on_end = {
		story_owner = {
			if = {
				limit = { has_character_flag = had_event_ga_0100 }
				remove_character_flag = had_event_ga_0100
			}
		}
		var:target_char ?= {
			capital_county ?= {
				if = {
					limit = { has_county_modifier = ep3_grand_ambitions_rioting_county_modifier }
				}
				remove_county_modifier = ep3_grand_ambitions_rioting_county_modifier
			}
		}
		if = {
			limit = {
				exists = global_var:current_grand_ambitions_counter
				global_var:current_grand_ambitions_counter > 0
			}
			change_global_variable = {
				name = current_grand_ambitions_counter
				subtract = 1
			}
		}
	}

	# Maintenance Events
	effect_group = {
		months = 1

		# Check if the old emperor is still holds the right title during the beginning
		triggered_effect = {
			trigger = {
				exists = var:target_char 
				var:target_char = {
					NOT = { any_held_title = { this = root.var:target_title } }
				}
			}
			effect = {
				story_owner = {
					trigger_event = grand_ambitions.0500
				}
			}
		}
		# Death maintenance is handled in the death on_action
	}

	effect_group = {
		months = 1
		chance = 100

		random_valid = {
			triggered_effect = {
				trigger = { # Claimant Faction Route
					var:method = flag:coup
				}
				effect = {
					scope:story = {
						var:target_title = { save_scope_as = target_title }
						var:target_char = { save_scope_as = target_char }
					}
					story_owner = {
						scope:target_char = {
							if = {
								limit = {
									any_targeting_faction = {
										faction_type = claimant_faction
										faction_leader = scope:story.story_owner
									}
								}
								random_targeting_faction = {
									faction_type = claimant_faction
									limit = {
										faction_leader = scope:story.story_owner
									}
									save_scope_as = story_faction
								}
							}
						}
						trigger_event = { on_action = grand_ambitions_powerful_family_coup_on_action }
					}
				}
			}
			triggered_effect = {
				trigger = { # Scheme Route
					var:method = flag:scheme
				}
				effect = {
					story_owner = {
						random_scheme = {
							type = depose
							save_scope_as = depose_scheme_scope
						}
						scope:story = {
							var:target_title = { save_scope_as = target_title }
							var:target_char = { save_scope_as = target_char }
						}
						trigger_event = { on_action = grand_ambitions_powerful_family_scheme_on_action }
					}
				}
			}
		}
	}

	effect_group = {
		months = 1
		chance = 100

		random_valid = {
			triggered_effect = {
				trigger = {
					story_owner = {
						any_character_war = {
							primary_attacker = scope:story.story_owner
							primary_defender = scope:story.var:target_char
							claimant = scope:story.story_owner
							save_temporary_scope_as = story_war
						}
					}
					var:method = flag:coup
					var:promised_foreign_ruler_claim = {
						NOT = { is_at_war_with = scope:story.var:target_char }
					}
					scope:story_war = {
						NOT = {
							any_war_attacker = {
								this = scope:story.var:promised_foreign_ruler_claim
							}
						}
					}
				}
				effect = {
					story_owner = {
						random_character_war = {
							limit = {
								primary_attacker = scope:story.story_owner
								primary_defender = scope:story.var:target_char
								claimant = scope:story.story_owner
							}
							save_scope_as = story_war
						}
						scope:story.var:promised_foreign_ruler_claim = { save_scope_as = joiner }
						send_interface_message = {
							type = event_war_good
							title = grand_ambitions_ally_joins_war
							right_icon = scope:joiner
							scope:story_war = {
								add_attacker = scope:joiner
							}
						}
					}
				}
			}
			triggered_effect = {
				trigger = {
					story_owner = {
						any_character_war = {
							primary_attacker = scope:story.story_owner
							primary_defender = scope:story.var:target_char
							claimant = scope:story.story_owner
							save_temporary_scope_as = story_war
						}
					}
					var:method = flag:coup
					has_variable = powerful_family
					scope:story_war = {
						NOT = {
							any_war_attacker = {
								this = scope:story.var:powerful_family.house_head
							}
						}
					}
				}
				effect = {
					story_owner = {
						random_character_war = {
							limit = {
								primary_attacker = scope:story.story_owner
								primary_defender = scope:story.var:target_char
								claimant = scope:story.story_owner
							}
							save_scope_as = story_war
						}
						scope:story.var:powerful_family.house_head = { save_scope_as = joiner }
						send_interface_message = {
							type = event_war_good
							title = grand_ambitions_ally_joins_war
							right_icon = scope:joiner
							scope:story_war = {
								add_attacker = scope:joiner
							}
						}
					}
				}
			}
		}
	}
}
