﻿sc_create_custom_kingdom_effect = {
	custom_tooltip = create_new_kingdom

	# Save the character scope immediately to avoid 'this' or 'root' confusion
	save_scope_as = effect_founder
	
	primary_title = {
		save_scope_as = old_title
	}
	
	if = {
		limit = {
			scope:crisis_title = {
				tier > scope:old_title.tier
			}
		}

		# create_dynamic_title automatically saves to scope:new_title
		if = {
			limit = {
				scope:crisis_title = {
					tier = tier_kingdom
				}
			}
			create_dynamic_title = {
				tier = kingdom
				name = NEW_CREATED_TITLE_NAME
			}
		}
		else_if = {
			limit = {
				scope:crisis_title = {
					tier = tier_empire
				}
			}
			create_dynamic_title = {
				tier = empire
				name = NEW_CREATED_TITLE_NAME
			}
		}
		else_if = {
			limit = {
				scope:crisis_title = {
					tier = tier_hegemony
				}
			}
			create_dynamic_title = {
				tier = hegemony
				name = NEW_CREATED_TITLE_NAME
			}
		}

		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}
		
		scope:new_title = {
			change_title_holder = {
				holder = scope:effect_founder
				change = scope:change
			}
			set_variable = title_to_destroy
		}
		
		resolve_title_and_vassal_change = scope:change
		
		scope:new_title = {
			set_coa = scope:old_title
			set_color_from_title = scope:old_title
			set_capital_county = scope:old_title.title_capital_county
		}
		set_primary_title_to = scope:new_title
	}

	if = {
		limit = {
			scope:current_ruler = {
				has_government = administrative_government
			}
		}
		change_government = administrative_government
	}
	else_if = {
		limit = {
			scope:current_ruler = {
				has_government = clan_government
			}
		}
		change_government = clan_government
	}
	else_if = {
		limit = {
			scope:current_ruler = {
				has_government = tribal_government
			}
		}
		change_government = tribal_government
	}
	else_if = {
		limit = {
			scope:current_ruler = {
				has_government = celestial_government
			}
		}
		change_government = celestial_government
	}
	else_if = {
		limit = {
			scope:current_ruler = {
				has_government = feudal_government
			}
		}
		change_government = feudal_government
	}
}