#####################################
#
#	001 - 100: Start-up events
#
#####################################
#
#	INDEX
#
#	001: New-game set-up
#	002: Apply Plus game rules
#	003: Apply Ruler Designer fixes
#	004: Ruler Designer fixes that require a delay
#
#####################################

namespace = startPlus

character_event = {
	id = startPlus.001

	hide_window = yes
	is_triggered_only = yes # on_startup

	only_playable = yes

	trigger = {
		owns = event_target:pulse_province
		is_save_game = no
	}

	immediate = {
		log = "startPlus.001"

		# Make sure the HRE and Carolingian Empire aren't active at the same time
		if = {
			limit = { NOT = { has_global_flag = hre_formed } }

			activate_title = {
				title = e_hre
				status = no
			}
		}
		else_if = {
			limit = { is_title_active = e_karling }

			activate_title = {
				title = e_karling
				status = no
			}
		}

		# Mark exceptions to Crown Authority destruction rule
		any_landed_title = {
			limit = {
				has_law = crown_authority_0
				NOT = { has_title_flag = do_not_destroy }
			}

			set_title_flag = do_not_destroy
		}

		# Hand out coronation traits to independent feudal kings and emperors
		crown_distribution = yes

		# Imperial Decadence setup
		imperial_decadence_setup = yes

		# Adjust necessary CoAs for some titles
		set_byzantium_name = yes
		set_english_coas = yes
		set_carpathia_coas = yes
		set_finland_coas = yes

		# Remove irrelevant claims
		remove_irrelevant_claims = yes

		# Apply Imperial government to all historical Roman/Byzantine Emperors
		set_historical_emperors = yes
	}

	after = {
		character_event = { id = startPlus.002 } # Apply Plus game rules
		character_event = { id = CombatTraits.600 } # Hand out combat traits (TODO: Move to on_actions?)
		any_player = {
			character_event = { id = startPlus.003 } # Ruler designer cleanup
			narrative_event = { id = startPlus.005 } # Ironman/Checksum warning
		}

		# State of the empire
		e_byzantium = {
			holder_scope = {
				narrative_event = { id = Plus.1300 }
			}
		}
	}
}

character_event = { # Apply Plus game rules
	id = startPlus.002

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		NOT = { has_global_flag = plus_rules_applied }
	}

	immediate = {
		set_global_flag = plus_rules_applied
		log = "Applying Plus game rules"

		### DE JURE EMPIRES ###
		if = {
			limit = {
				has_game_rule = {
					name = vanilla_empires
					value = no
				}
			}

			any_landed_title = {
				limit = {
					real_tier = EMPEROR
					is_titular = no
					has_holder = no

					NOR = {
						any_claimant = { always = yes }
						title = e_byzantium
						title = e_karling
						title = e_hre
					}
				}

				any_direct_de_jure_vassal_title = {
					de_jure_liege = e_placeholder
				}
			}

			# Manual adjustments
			k_brittany = { de_jure_liege = e_placeholder }
			k_armenia = { de_jure_liege = e_placeholder }
			k_georgia = { de_jure_liege = e_placeholder }
			k_serbia = { de_jure_liege = e_placeholder }

			if = {
				limit = { start_date < 843.8.10 } # Treaty of Verdun
				k_germany = { de_jure_liege = e_francia }
			}

			if = {
				limit = { start_date >= 962.2.2 } # HRE formed
				k_burgundy = { de_jure_liege = e_placeholder }
			}

			if = {
				limit = { e_bulgaria = { is_titular = yes } }
				k_bulgaria = { de_jure_liege = e_placeholder }
			}

			if = {
				limit = { start_date >= 1204.1.1 }
				k_anatolia = { de_jure_liege = e_placeholder }
			}

			if = {
				limit = { start_date >= 1326.1.1 }
				k_trebizond = { de_jure_liege = e_placeholder }
			}
		}

		### CRUSADE DELAY ###
		if = {
			limit = {
				has_game_rule = {
					name = crusade_timing
					value = activate_immediately
				}
			}

			set_global_flag = muslim_jihads_unlocked
			set_global_flag = christian_crusades_unlocked
			set_global_flag = pagan_ghws_unlocked
		}

		### GOVERNMENT CONVERSION
		if = {
			limit = {
				has_game_rule = {
					name = government_conversion
					value = gofeudal
				}
			}

			any_province = {
				limit = { has_tribal = yes }

				any_province_holding = {
					limit = { holding_type = tribal }
					convert_to = castle
					refill_holding_levy = yes
				}

				add_province_modifier = {
					name = new_buildings_timer
					years = 100
					hidden = yes
				}

				owner = {
					if = {
						limit = { is_tribal = yes }
						set_correct_feudal_government_type_effect = yes
					}

					any_demesne_title = {
						if = {
							limit = { is_crown_law_title = yes }
							set_title_flag = do_not_destroy
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				has_game_rule = {
					name = government_conversion
					value = gotribal
				}
			}

			any_province = {
				limit = { has_tribal = no }

				capital_holding = {
					convert_to = tribal
					refill_holding_levy = yes
				}

				any_province_holding = {
					limit = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}

					destroy_settlement = THIS
				}

				owner = {
					if = {
						limit = { is_theocracy = no }
						set_government_type = tribal_government
					}
				}
			}
		}

		### NEW WORLD ORDER
		# TODO: Remove in favor of vanilla Shattered World and tweaks to it
		if = {
			limit = {
				has_game_rule = {
					name = new_world_order
					value = yes
				}
			}

			character_event = { id = emf_nwo.0 }
		}

		### ANCIENT RELIGIONS - Handled through NWO if NWO is enabled
		else_if = {
			limit = {
				NOT = {
					has_game_rule = {
						name = ancient_religions
						value = no
					}
				}
			}

			character_event = { id = Plus.2702 }
		}

		### BABY PORTRAITS ###
		if = {
			limit = {
				has_game_rule = {
					name = baby_portraits
					value = yes
				}
			}

			any_character = {
				limit = {
					age < 3

					NOR = {
						trait = baby_boy_fair
						trait = baby_boy_tanned
						trait = baby_boy_dark
						trait = baby_boy_darker
						trait = baby_girl_fair
						trait = baby_girl_tanned
						trait = baby_girl_dark
						trait = baby_girl_darker
					}
				}

				assign_baby_portrait_effect = yes # Automatically choses the proper trait based on gender & ethnicity
			}
		}
	}
}

character_event = { # Apply Ruler Designer fixes
	id = startPlus.003

	is_triggered_only = yes # startPlus.001
	hide_window = yes

	immediate = {
		# Fix RD Immortal
		if = {
			limit = { trait = immortal }

			remove_trait = immortal
			add_trait = immortal
		}

		# Fix fired councilor opinions on game start due to new ruler
		if = {
			limit = { ruled_years <= 1 } # Check for RD character. May not be needed as Ruler Designer no longer empties council

			any_courtier_or_vassal = {
				limit = {
					has_opinion_modifier = {
						name = opinion_recently_fired
						who = PREV
					}
				}

				remove_opinion = {
					name = opinion_recently_fired
					who = PREV
				}
			}
		}
	}

	after = {
		character_event = { id = startPlus.004 days = 7 } # RD fixes that require a delay
	}
}

character_event = { # Ruler Designer fixes that require a delay
	id = startPlus.004

	is_triggered_only = yes # startPlus.003
	hide_window = yes

	immediate = {
		# Remove RD Traits
		if = {
			limit = {
				trait = customize_ruler
				NOT = { has_character_flag = customizing_ruler }
			}

			remove_trait = customize_ruler
		}

		if = {
			limit = { trait = minority_culture }

			save_event_target_as = minority_culture
			remove_trait = minority_culture

			if = {
				limit = {
					top_liege = {
						NOT = {
							any_realm_province = {
								culture = event_target:minority_culture
								held_under_PREV = yes
							}
						}
					}
				}

				capital_scope = {
					culture = event_target:minority_culture

					any_neighbor_province = {
						if = {
							limit = { owned_by = event_target:minority_culture }

							random = {
								chance = 50
								culture = event_target:minority_culture
							}
						}
					}
				}
			}
		}
	}
}

narrative_event = { # Ironman and checksum warning
	id = startPlus.005

	is_triggered_only = yes # startPlus.001

	trigger = {
		OR = { # Validator will probably bitch about this while checksum verification is disabled. It's ok though.
			is_ironman = yes
			# supported_checksum = no # disabled during beta
		}
	}

	title = EVTNAME_Plus_1752 # WARNING!

	desc = {
		trigger = {
			always = no # disabled during beta
			is_ironman = yes
			supported_checksum = no
		}

		text = EVTDESC_Plus_1752 #EVTDESC_A_startPlus_005 # TODO: Write something for this
	}

	desc = {
		trigger = {
			is_ironman = yes
			supported_checksum = yes
		}

		text = EVTDESC_Plus_1752
	}

	desc = {
		trigger = {
			always = no # disabled during beta
			is_ironman = no
			supported_checksum = no
		}

		text = EVTDESC_Plus_372
	}

	desc = {
		trigger = {
			is_ironman = no
			supported_checksum = yes
		}

		text = "I shouldn't be here. Please tell the mod devs."
	}

	option = {
		name = OK
	}
}