﻿namespace = tournament_events

##################################################
# You are offered a favour by a pretty person
# by James Beaumont
# 8000
##################################################

scripted_trigger 8000_valid_to_offer_favor = {
	is_ai = yes
	is_adult = yes
	is_lowborn = no # Prevent random uninteresting pool characters from showing up
	OR = {
		AND = {
			is_married = no
			is_concubine = no
		}
		is_consort_of = root
		might_cheat_on_every_partner_trigger = yes
	}
	NOT = {
		relation_with_character_is_incestuous_in_faith_trigger = {
			CHARACTER = root
			FAITH = root.faith
		}
	}
	OR = {
		is_attracted_to_gender_of = root
		has_sexuality = asexual # This is romance and courtship, asexuals should be fine with it most of the time
	}
	OR = {
		AND = {
			root.faith = { NOT = { has_doctrine_parameter = homosexuality_accepted } }
			NOT = { sex_same_as = root }
		}
		root.faith = { has_doctrine_parameter = homosexuality_accepted }
	}
	NOR = {
		has_relation_soulmate = root
		# Celibates should abstain always
		has_trait = celibate
		# No priests giving favours
		AND = {
			is_clergy = yes
			NOT = { faith = { has_doctrine_parameter = clergy_can_marry } }
		}
	}
}

tournament_events.8000 = { # Start
	type = activity_event
	title = tournament_events.8000.t
	desc = {
		desc = tournament_events.8000.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					any_in_list = {
						list = potential_favour_givers
						count > 1
					}
				}
				desc = tournament_events.8000.desc.mid.two
			}
			desc = tournament_events.8000.desc.mid
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					NOR = {
						is_attracted_to_gender_of = scope:favor_giver_1
						is_attracted_to_gender_of = scope:favor_giver_2
						has_sexuality = asexual
					}
				}
				desc = tournament_events.8000.desc.outro.homo
			}
		}
	}
	theme = tournament_locale_camp
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:favor_giver_1
		animation = flirtation
	}
	lower_right_portrait = {
		character = scope:favor_giver_2
		animation = flirtation
	}

	cooldown = { years = 1 }

	trigger = {
		activity_is_competing_trigger = yes
		scope:activity = {
			any_guest_subset_current_phase = { # There is a spectator who fancies you
				name = spectator
				8000_valid_to_offer_favor = yes
			}
		}
		NOT = { has_trait = celibate }
		OR = { # For AI priests in tournaments
			is_clergy = no
			faith = { has_doctrine_parameter = clergy_can_marry }
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			factor = 2
			has_activity_intent = woo_attendee_intent
		}
	}

	immediate = {
		hidden_effect = {
			save_scope_as = favor_recipient
			scope:activity = {
				every_guest_subset_current_phase = {
					name = spectator
					limit = { 8000_valid_to_offer_favor = yes }
					add_to_list = potential_favour_givers
				}
				random_in_list = {
					list = potential_favour_givers
					weight = {
						base = 20
						modifier = { # If they are wooing you, they're much more likely to do this
							factor = 100
							has_activity_intent = woo_attendee_intent
							intent_target = root
						}
						modifier = { # Get their intent target where possible
							add = 20
							this = root.intent_target
						}
						modifier = { # Get someone fertile for the player
							add = 20
							fertility > low_fertility
						}
						modifier = { # They love knights and stuff
							add = 20
							has_trait = gallant
						}
						modifier = {
							add = ai_honor
						}
						modifier = { # Cheaters are possible, but should be less likely
							add = 20
							OR = {
								AND = {
									is_married = no
									is_concubine = no
								}
								is_consort_of = root
								is_concubine_of = root
							}
						}
						modifier = { # Get someone interesting
							add = 20
							is_claimant = yes
						}
						modifier = {
							add = 20
							OR = {
								has_same_sinful_trait = root
								has_same_virtue_trait = root
							}
						}
					}
					save_scope_as = favor_giver_1
					hidden_effect = {
						create_artifact_tournament_favor_effect = {
							OWNER = scope:favor_giver_1
							RECIPIENT = root
						}
					}
					scope:newly_created_artifact = { save_scope_as = favor_1 }
				}
				if = {
					limit = {
						any_in_list = {
							list = potential_favour_givers
							count > 1
						}
					}
					random_in_list = {
						list = potential_favour_givers
						limit = {
							this != scope:favor_giver_1
						}
						weight = {
							base = 20
							modifier = { # If they are wooing you, they're much more likely to do this
								factor = 100
								has_activity_intent = woo_attendee_intent
								intent_target = root
							}
							modifier = { # Get their intent target where possible
								add = 20
								this = root.intent_target
							}
							modifier = {
								add = 20
								fertility > low_fertility
							}
							modifier = {
								add = 20
								has_trait = gallant
							}
							modifier = {
								add = ai_honor
							}
							modifier = {
								OR = {
									AND = {
										is_married = no
										is_concubine = no
									}
									is_consort_of = root
									is_concubine_of = root
								}
							}
							modifier = {
								add = 20
								OR = {
									has_same_sinful_trait = root
									has_same_virtue_trait = root
								}
							}
						}
						save_scope_as = favor_giver_2
						hidden_effect = {
							create_artifact_tournament_favor_effect = {
								OWNER = scope:favor_giver_2
								RECIPIENT = root
							}
						}
						scope:newly_created_artifact = { save_scope_as = favor_2 }
					}
				}
			}
		}
		add_prestige = medium_prestige_gain
		custom_tooltip = tournament_events.1251.favor_tt
	}

	option = { # Why, suitor 1 is perfect for me!
		name = tournament_events.8000.a
		scope:activity = {
			add_activity_log_entry = {
				key = tournament_favor_given_log
				score = 25
				tags = { favors good }
				character = scope:favor_recipient
				target = scope:favor_giver_1

				# Effects
				show_as_tooltip = {
					scope:favor_1 = { set_owner = root }
				}
				hidden_effect = {
					if = {
						limit = { exists = scope:favor_2 }
						destroy_artifact = scope:favor_2
					}
				}
			}
		}
		hidden_effect = {
			scope:favor_1 = { equip_artifact_to_owner_replace = yes }
		}
		if = {
			limit = {
				NOT = {
					# They are not wooing me
					scope:favor_giver_1 = {
						any_scheme = {
							type = courting
							scheme_target_character = root
						}
					}
					# I am not wooing them
					any_scheme = {
						type = courting
						scheme_target_character = root
					}
				}
			}
			scope:favor_giver_1 = {
				start_scheme = {
					type = courting
					target_character = root
				}
			}
		}
		else = {
			set_relation_soulmate = {
				reason = soulmate_tournament
				target = scope:favor_giver_1
			}
		}
		reverse_add_opinion = {
	 		target = scope:favor_giver_1
	 		modifier = flattered_opinion
	 		opinion = 20
		}
		if = {
			limit = { exists = scope:favor_giver_2 }
			reverse_add_opinion = {
		 		target = scope:favor_giver_2
		 		modifier = rebuffed_opinion
		 		opinion = -15
			}
		}
		stress_impact = {
			gallant = minor_stress_impact_loss
			arrogant = minor_stress_impact_loss
			chaste = minor_stress_impact_gain
			cynical = minor_stress_impact_gain
			zealous = minor_stress_impact_gain
			humble = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			modifier = {
				factor = 0
				OR = {
					has_trait = chaste
					has_trait = cynical
					has_trait = zealous
					has_trait = humble
				}
			}
			modifier = {
				factor = 0.5
				NOT = { # Only 0.5 since gay characters may pretend to be interested in a homophobic environment
					is_attracted_to_gender_of = scope:favor_giver_1
					has_sexuality = asexual
				}
			}
		}
	}

	option = { # Why, suitor 2 is perfect for me!
		name = tournament_events.8000.b
		trigger = {
			exists = scope:favor_giver_2
		}
		scope:activity = {
			add_activity_log_entry = {
				key = tournament_favor_given_log
				score = 25
				tags = { favors good }
				character = scope:favor_recipient
				target = scope:favor_giver_1

				# Effects
				show_as_tooltip = {
					scope:favor_2 = { set_owner = root }
				}
				hidden_effect = {
					if = {
						limit = { exists = scope:favor_1 }
						destroy_artifact = scope:favor_1
					}
				}
			}
		}
		hidden_effect = {
			scope:favor_2 = { equip_artifact_to_owner_replace = yes }
		}
		if = {
			limit = {
				NOT = {
					# They are not wooing me
					scope:favor_giver_2 = {
						any_scheme = {
							type = courting
							scheme_target_character = root
						}
					}
					# I am not wooing them
					any_scheme = {
						type = courting
						scheme_target_character = root
					}
				}
			}
			scope:favor_giver_2 = {
				start_scheme = {
					type = courting
					target_character = root
				}
			}
		}
		else = {
			set_relation_soulmate = {
				reason = soulmate_tournament
				target = scope:favor_giver_2
			}
		}
		reverse_add_opinion = {
	 		target = scope:favor_giver_2
	 		modifier = flattered_opinion
	 		opinion = 20
		}
		reverse_add_opinion = {
	 		target = scope:favor_giver_1
	 		modifier = rebuffed_opinion
	 		opinion = -15
		}
		stress_impact = {
			gallant = minor_stress_impact_loss
			arrogant = minor_stress_impact_loss
			chaste = minor_stress_impact_gain
			cynical = minor_stress_impact_gain
			zealous = minor_stress_impact_gain
			humble = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			modifier = {
				factor = 0
				OR = {
					has_trait = chaste
					has_trait = cynical
					has_trait = zealous
					has_trait = humble
				}
			}
			modifier = {
				factor = 0.5
				NOT = {
					is_attracted_to_gender_of = scope:favor_giver_1
					has_sexuality = asexual
				}
			}
		}
	}

	option = { # Oh bugger off both of you
		name = tournament_events.8000.c
		add_prestige = medium_prestige_gain
		activity_tournament_change_contestant_score_effect = { SCORE = increase_minor }
		reverse_add_opinion = {
	 		target = scope:favor_giver_1
	 		modifier = rebuffed_opinion
	 		opinion = -15
		}
		hidden_effect = {
			if = {
				limit = { exists = scope:favor_1 }
				destroy_artifact = scope:favor_1
			}
			if = {
				limit = { exists = scope:favor_2 }
				destroy_artifact = scope:favor_2
			}
		}
		if = {
			limit = { exists = scope:favor_giver_2 }
			reverse_add_opinion = {
		 		target = scope:favor_giver_2
		 		modifier = rebuffed_opinion
		 		opinion = -15
			}
		}
		stress_impact = {
			gallant = minor_stress_impact_gain
			arrogant = minor_stress_impact_gain
			lustful = minor_stress_impact_gain
			rakish = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			modifier = {
				factor = 0
				OR = {
					has_trait = gallant
					has_trait = arrogant
					has_trait = lustful
					has_trait = rakish
				}
			}
			modifier = {
				factor = 0.5
				NOT = {
					is_attracted_to_gender_of = scope:favor_giver_1
					has_sexuality = asexual
				}
			}
		}
	}
}
