
namespace = regula_corrupt_holy_order_event

regula_corrupt_holy_order_event.0001 = {
	type = character_event
	title = regula_corrupt_holy_order_event.0001.t
	desc = {
		desc = regula_corrupt_holy_order_event.0001.desc
		triggered_desc = {
			trigger = {
				NOT = { scope:order_founder = root }
				}
			desc = regula_corrupt_holy_order_event.0001.vassal.desc
		}
		desc = regula_corrupt_holy_order_event.0001.direct.desc
	}

	theme = regula_theme
	override_background = {
		event_background = regula_holy_order
	}
	right_portrait = {
		character = scope:order_founder
		animation = personality_bold
	}

	immediate = {
		if = {
			limit = {
				any_held_title = {
					barony_is_valid_for_holy_order_lease_cancellation_trigger = yes
					is_under_holy_order_lease = yes
					lessee_title.holder = {
						NOT = {
							faith = global_var:magister_character.faith
						}
						save_temporary_scope_as = infidel_grandmaster
					}
				}
			}
			scope:infidel_grandmaster = {
				save_scope_as = infidel_grandmaster2
				faith = {
					save_scope_as = corrupted_faith
					random_faith_holy_order = {
						limit = {
							leader = scope:infidel_grandmaster
						}
						save_scope_as = infidel_holy_order
						every_leased_title = {
							limit = {
								OR = {
									holder.top_liege = global_var:magister_character
									holder = global_var:magister_character
								}
								barony_is_valid_for_holy_order_lease_cancellation_trigger = yes
								tier = tier_barony
							}
							add_to_list = corruptible_titles
						}
					}
				}
				every_held_title = {
					limit = {
						holder.top_liege = global_var:magister_character
						barony_is_valid_for_holy_order_lease_cancellation_trigger = yes
						tier = tier_barony
					}
					add_to_list = corruptible_titles
				}
			}
		}
		else = {
			random_vassal_or_below = {
				limit = {
					has_trait = devoted_trait_group
					any_held_title = {
						barony_is_valid_for_holy_order_lease_cancellation_trigger = yes
						is_under_holy_order_lease = yes
						lessee_title.holder = {
							NOT = {
								faith = global_var:magister_character.faith
							}
							save_temporary_scope_as = infidel_grandmaster
						}
					}
				}
			}
			scope:infidel_grandmaster = {
				save_scope_as = infidel_grandmaster2
				faith = {
					save_scope_as = corrupted_faith
					random_faith_holy_order = {
						limit = {
							leader = scope:infidel_grandmaster
						}
						save_scope_as = infidel_holy_order
						every_leased_title = {
							limit = {
								OR = {
									holder.top_liege = global_var:magister_character
									holder = global_var:magister_character
								}
								barony_is_valid_for_holy_order_lease_cancellation_trigger = yes
								tier = tier_barony
							}
							add_to_list = corruptible_titles
						}
					}
				}
				every_held_title = {
					limit = {
						holder.top_liege = global_var:magister_character
						barony_is_valid_for_holy_order_lease_cancellation_trigger = yes
						tier = tier_barony
					}
					add_to_list = corruptible_titles
				}
			}
		}
		random_in_list = { 
			list = corruptible_titles
			save_scope_as = corruptible_capital
			holder = {
				save_scope_as = order_founder
			}
		}
	}


	option = { # Corrupt order
		name = regula_corrupt_holy_order_event.0001.a

		custom_tooltip = corrupt_holy_order_decision_effect_message

		create_character = {
			template = holy_order_leader_character
			location = scope:corruptible_capital.title_province
			save_scope_as = leader
		}

		scope:corruptible_capital = {
			revoke_lease = yes
		}

		create_holy_order = {
			leader = scope:leader
			capital = scope:corruptible_capital
			save_scope_as = new_holy_order
		}
		every_in_list = {
			list = corruptible_titles
			if = {
				limit = {
					NOT = { this = scope:corruptible_capital }
				}
				revoke_lease = yes
				lease_out_to = scope:new_holy_order
			}
		}

		hidden_effect = {
			scope:leader = {
				add_gold = 100 #So that they have some money to lend out
				add_piety_level = 2
				add_gold = holy_order_starting_gold
				add_trait = lustful
				add_trait = mulsa
				every_courtier = {
					add_trait = order_member
					add_trait = mulsa
				}
			}
			
			# Messages 
			global_var:magister_character = {
				send_interface_message = {
					type = holy_order_founded_message
					desc = regula_corrupt_holy_order_message
					left_icon = scope:leader
					right_icon = scope:new_holy_order.title
				}
			}
			every_ruler = {
				limit = {
					faith = scope:order_founder.faith
					NOT = { this = scope:order_founder }
					NOT = { this = global_var:magister_character }
				}
				send_interface_message = {
					type = holy_order_founded_message
					desc = someone_created_holy_order_message
					left_icon = scope:order_founder
					right_icon = scope:new_holy_order.title
				}
			}
			every_neighboring_top_liege_realm_owner = {
				limit = {
					NOT = { faith = scope:order_founder.faith }
				}
				send_interface_message = {
					type = enemy_holy_order_founded_message
					desc = other_faith_neighbor_created_holy_order_message
					left_icon = scope:order_founder
					right_icon = scope:new_holy_order.title
				}
			}	
		}
		faith = {
			change_fervor = {
				value = 5
			}
		}
		scope:corrupted_faith = {
			change_fervor = {
				value = -15
			}
		}

		trigger_event = regula_corrupt_holy_order_event.0002
	}

	option = { # Do nothing.
		name = regula_corrupt_holy_order_event.0001.b
		flavor = regula_corrupt_holy_order_event.0001.b.tt
		
		add_piety = 2000
	}
}

regula_corrupt_holy_order_event.0002 = {
	type = character_event
	title = regula_corrupt_holy_order_event.0002.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:new_recruit = {
						has_trait = giant
					}
				}
				desc = regula_corrupt_holy_order_event.0002.giant.desc
			}
			desc = regula_corrupt_holy_order_event.0002.commander.desc
		}
	}

	theme = regula_theme
	override_background = {
		event_background = regula_holy_order_interior
	}
	left_portrait = {
		character = scope:left_portrait
		outfit_tags = { no_cloak no_hat no_pants no_clothes }
		animation = personality_coward		
	}
	right_portrait = {
		character = scope:new_recruit
		outfit_tags = { no_cloak no_hat no_pants no_clothes }
		animation = war_defender
	}

	immediate = {
		random = {
			chance = 50
			regula_create_champion_giant_effect = { WHO = scope:order_founder }
			scope:created_character = { save_scope_as = new_recruit }
			regula_create_orgy_guest_effect = { WHO = scope:order_founder }
			scope:created_orgy_guest = { save_scope_as = new_plaything }
		}
		if = {
			limit = { NOT = { exists = scope:new_recruit } }
			regula_create_commander_effect = { WHO = root }
			scope:created_character = { save_scope_as = new_recruit }
		}
		hidden_effect = {
			scope:new_recruit = {
				add_trait = lustful
				set_sexuality = bisexual
				add_trait = mulsa
			}
			if = {
				limit = { exists = scope:new_plaything }
				scope:new_plaything = {
					add_character_flag = regula_short
					add_trait = mulsa
					save_scope_as = left_portrait
				}
			}
		}
	}

	option = {
		name = regula_corrupt_holy_order_event.0002.a

		recruit_courtier = scope:new_recruit
	}
	option = {
		name = regula_corrupt_holy_order_event.0002.b
		trigger = {
			exists = scope:new_plaything
		}
		
		recruit_courtier = scope:new_recruit
		recruit_courtier = scope:new_plaything
	}
}