
###########################################
#                                         #
# Charlemagne Major Events                #             
#                                         #
# ID CM.500-CM.999                        #
#                                         #
###########################################

# Written by Tobias Bodlund

namespace = CM

# Fall of the Kingdom of the Avars
narrative_event = {
	id = CM.501
	desc = EVTDESC_CM_501
	title = EVTTITLE_CM_501
	picture = GFX_evt_throne_room
	border = GFX_event_narrative_frame_war
	
	only_playable = yes

	major = yes
	
	trigger = {
		has_landed_title = k_carpathia
		NOT = { culture = avar }
		NOT = { has_global_flag = avar_khaganate_renamed }
	}

	major_trigger = {
		capital_scope = {
			NOT = {
				distance = {
					where = 522 # Pest
					value = 600	# Same part of the world
				}
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		set_global_flag = avar_khaganate_renamed
	}
	
	option = {
		name = EVTOPTA_CM_501
		trigger = {
			has_landed_title = k_carpathia
		}
		k_carpathia = {
			set_name = "KINGDOM_PANNONIA"
			adjective = "KINGDOM_PANNONIA_ADJ"
			reset_coa = THIS
			clr_title_flag = carpathia_is_avaria
		}
	}
	option = {
		name = EVTOPTB_CM_501
		trigger = {
			NOT = { has_landed_title = k_carpathia }
			culture = avar
		}
	}
	option = {
		name = EVTOPTC_CM_501
		trigger = {
			NOT = { has_landed_title = k_carpathia }
			NOT = { culture = avar }
		}
	}
}

# Fall of the Kingdom of the Lombards
narrative_event = {
	id = CM.502
	desc = EVTDESC_CM_502
	title = EVTTITLE_CM_502
	picture = GFX_evt_book_carolingian_script
	border = GFX_event_narrative_frame_war
	
	only_playable = yes
	has_global_flag = game_start_charlemagne

	major = yes
	
	trigger = {
		has_landed_title = k_lombardy
		NOT = { culture = longobard }
		NOT = { has_global_flag = fall_of_lombard_kingdom }
	}

	major_trigger = {
		capital_scope = {
			NOT = {
				distance = {
					where = 235 # Lombardy
					value = 800	# Same part of the world
				}
			}
		}
	}
	
	mean_time_to_happen = {
		days = 1
	}
	
	immediate = {
		set_global_flag = fall_of_lombard_kingdom
	}
	
	option = {
		name = EVTOPTA_CM_502
		trigger = {
			has_landed_title = k_lombardy
		}
		hidden_tooltip = {
			activate_title = { title = k_italy status = yes }
			k_lombardy = {
				any_direct_de_jure_vassal_title = {
					de_jure_liege = k_italy
				}
			}
			k_italy = {
				copy_title_laws = k_lombardy
				copy_title_history = k_lombardy
				grant_title = ROOT
			}
			any_realm_character = {
				limit = { has_claim = k_lombardy }
				k_italy = { add_pressed_claim = PREV }
				remove_claim = k_lombardy
			}
			any_playable_ruler = {
				any_realm_character = {
					limit = { has_claim = k_lombardy }
					k_italy = { add_pressed_claim = PREV }
					remove_claim = k_lombardy
				}
			}
			k_lombardy = {
				destroy_landed_title = THIS
			}
			activate_title = { title = k_lombardy status = no }
		}
	}
	option = {
		name = EVTOPTB_CM_502
		trigger = {
			NOT = { has_landed_title = k_lombardy }
			culture = longobard
		}
	}
	option = {
		name = EVTOPTC_CM_501
		trigger = {
			NOT = { has_landed_title = k_lombardy }
			NOT = { culture = longobard }
		}
	}
}

# Start of the Viking Age
narrative_event = {
	id = CM.503
	title = EVTTITLE_CM_503
	desc = EVTDESC_CM_503
	picture = GFX_evt_vikings_arriving_oldgods
	border = GFX_event_narrative_frame_war

	major = yes
	hide_from = yes
	only_playable = yes
	religion = miaphysite
	has_global_flag = game_start_charlemagne

	is_triggered_only = yes
	
	trigger = {
		controls_religion = yes # Let it happen for the Coptic Pope - he's always around
		year = 793
		NOT = { year = 867 }
		NOT = { has_global_flag = viking_age_started }
	}
	
	immediate = {
		log = "the Viking Age has begun!"
		set_global_flag = viking_age_started
		enable_prepared_invasion = norse_pagan
		enable_prepared_invasion = norse_pagan_reformed
		any_province = {
			limit = {
				owner = { culture = norse }
				culture = norse
				port = yes
			}
			change_tech = {
				technology = TECH_NAVAL
				value = 1
			}
		}
		any_character = {
			limit = {
				culture = norse
				is_ruler = yes
			}
			any_demesne_title = {
				if = {
					limit = {
						tier = BARON
						location = {
							port = yes
							culture = norse
						}
						owner = {
							is_feudal = yes
							culture = norse
						}
						NOT = { has_building = ca_shipyard_1 }
					}
					add_building = ca_shipyard_1
				}
				if = {
					limit = {
						tier = BARON
						location = {
							port = yes
							culture = norse
						}
						owner = {
							is_republic = yes
							culture = norse
						}
						NOT = { has_building = ct_shipyard_1 }
					}
					add_building = ct_shipyard_1
				}
				if = {
					limit = {
						tier = BARON
						location = {
							port = yes
							culture = norse
						}
						owner = {
							is_priest = yes
							culture = norse
						}
						NOT = { has_building = tp_shipyard_1 }
					}
					add_building = tp_shipyard_1
				}
				if = {
					limit = {
						tier = BARON
						location = {
							port = yes
							culture = norse
						}
						owner = {
							is_tribal = yes
							culture = norse
						}
						NOT = { has_building = tb_shipyard_1 }
					}
					add_building = tb_shipyard_1
				}
			}
		}
		any_playable_ruler = {
			limit = {
				ai = no
				capital_scope = {
					NOT = { region = world_india }
					NOT = { region = world_africa_west }
					NOT = { region = world_africa_east }
				}
			}
			chronicle = {
				entry = CHRONICLE_VIKING_AGE_BEGINS
				picture = GFX_evt_longships_at_sea_oldgods
			}
		}
	}

	option = {
		name = EVTOPTA_CM_503
		trigger = {
			NOT = { culture = norse }
			capital_scope = {
				NOT = { region = world_india }
				NOT = { region = world_africa_west }
				NOT = { region = world_africa_east }
			}
		}
		custom_tooltip = { text = viking_age_shipyards_and_tech_tooltip }
		custom_tooltip = { text = viking_age_cb_tooltip }
	}
	option = {
		name = EVTOPTB_CM_503
		trigger = {
			culture = norse
		}
		custom_tooltip = { text = viking_age_shipyards_and_tech_tooltip }
		custom_tooltip = { text = viking_age_cb_tooltip }
	}
	option = {
		name = EVTOPTC_CM_503
		trigger = {
			NOT = { culture = norse }
			capital_scope = {
				OR = {
					region = world_india
					region = world_africa_west
					region = world_africa_east
				}
			}
		}
		custom_tooltip = { text = viking_age_shipyards_and_tech_tooltip }
		custom_tooltip = { text = viking_age_cb_tooltip }
	}
}

