﻿namespace = jason_race_events

##################################################
# The Will of the Crowd
# by Jason Cantalini
# 1
##################################################

scripted_effect tournament_horse_race_riot_effect = {
	scope:activity = {
		contest_wound_non_root_attendees_effect = yes
		add_activity_log_entry = {
			key = contest_horse_race_riot_log
			score = 50
			tags = { contest bad }
			character = scope:host
			
			# Effect
			scope:activity.activity_location.county = {
				change_county_control = minor_county_control_loss
				change_development_progress = minor_development_progress_loss
				add_county_modifier = {
					modifier = feeble_presence_modifier
					years = 5
				}
			}
		}
	}
	scope:host = {
		create_character_memory = { type = failed_riot_at_races_memory }
	}
}

#A riot seems about to start amidst the race-watching crowd
jason_race_events.1 = {
	type = activity_event
	title = jason_race_events.1.t
	desc = {
		desc = jason_race_events.1.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					root.location.county = {
						county_control >= 80
					}
					root.location.county = {
						county_opinion >= 0
					}
				}
				desc = jason_race_events.1.desc.rowdy_fans
			}
			triggered_desc = {
				trigger = {
					NOT = {
						scope:host = {
							this = root
						}
					}
				}
				desc = jason_race_events.1.desc.guest_local_anger
			}
			triggered_desc = {
				trigger = {
					scope:host = {
						this = root
					}
				}
				desc = jason_race_events.1.desc.host_local_anger
			}
		}
		desc = jason_race_events.1.desc.end
	}
	theme = tournament_contest
	window = widget_activity_locale_fullscreen_event
	left_portrait = {
		character = root
		animation = personality_vengeful
	}
	right_portrait = {
		trigger = {
			scope:host = {
				this != root
			}
		}
		character = scope:host
		animation = paranoia
	}

	trigger = {
		is_ai = no
		scope:host = {
			OR = {
				this = root
				is_ai = yes
			}
		}
		scope:activity = {
			root.location.county = {
				NOR = {
					AND = {
						county_control > 80
						county_opinion >= 0
						culture = {
							OR = {
								has_cultural_tradition = tradition_loyal_soldiers
								has_cultural_tradition = tradition_pacifism
								has_cultural_tradition = tradition_esteemed_hospitality
								has_cultural_tradition = tradition_xenophilic
	
							}
						}
					}
					AND = {
						county_control >= 80
						county_opinion >= 0
						faith = {
							OR = {
								has_doctrine = tenet_dharmic_pacifism
								has_doctrine = tenet_pacifism
							}
						}
					}
				}
			}
		}

		#Standard checks.
		NOT = {
			has_character_flag = horse_race_contest_event_cooldown
		}
		NOT = {
		 	scope:activity = {
		 		has_variable = jason_race_events_1_cooldown
		 	}
		}
	}

	immediate = {
		play_sound_effect = "event:/DLC/EP2/SFX/Ambience/2D/ep2_amb_2d_crowd_negative_reaction"		
		tournament_pivotal_flavor_variable_effect = { CONTEST = horse_race ID = 1 }
		add_character_flag = {
			flag = horse_race_contest_event_cooldown
			years = 1
		}
		set_variable = jason_race_events_1_cooldown
		every_knight = {
			limit = {
				is_ai = yes
				location = root.location
			}
			add_to_list = your_warriors
		}
		root = { add_to_list = your_warriors_and_you }
		every_knight = {
			limit = {
				is_adult = yes
				is_ai = yes
				location = root.location
			}
			add_to_list = your_warriors_and_you
		}
	}

	# try to calm the crowd with words
	option = {
		trigger = {
			scope:host = {
				this = root
			}
		}
		name = jason_race_events.1.a
		flavor = jason_race_events.1.a.tt

		duel = {
			skill = diplomacy
			value = medium_skill_rating
			#Your speech calms the people
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					add = 20
					dread > medium_dread
				}
				modifier = {
					add = 40
					dread > high_dread
				}
				modifier = {
					add = 10
					highest_held_title_tier = tier_kingdom
				}
				modifier = {
					add = 20
					highest_held_title_tier = tier_empire
				}
				desc = jason_race_events.1.a.tt.win
				send_interface_toast = {
					title = jason_race_events.1.a.tt.win
					left_icon = root
					scope:activity = {
						add_activity_log_entry = {
							key = contest_horse_race_riot_speech_log
							score = 50
							tags = { contest good }
							character = root

							# Effects
							root = {
								custom_tooltip = jason_race_events.1.a.tt.race
								add_diplomacy_lifestyle_xp = medium_lifestyle_xp
							}
							scope:activity.activity_location.county = {
								add_county_modifier = {
									modifier = speeches_and_games_modifier
									years = 5
								}
							}
						}
					}
					create_character_memory = { type = calmed_the_crowd_memory }
				}
			}
			#Your speech falls on deaf ears
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = jason_race_events.1.a.tt.lose
				send_interface_toast = {
					title = jason_race_events.1.a.tt.lose
					left_icon = root
					tournament_horse_race_riot_effect = yes
					create_character_memory = { type = failed_riot_at_races_memory }
				}
			}
		}
		stress_impact = {
			wrathful = massive_stress_gain
			vengeful = major_stress_gain
			craven = major_stress_gain
			shy = major_stress_gain
			paranoid = medium_stress_gain
			sadistic = medium_stress_gain
			callous = minor_stress_gain
			fickle = minor_stress_gain
		}

		ai_chance = {

			base = 100

			modifier = {
				factor = 0
				OR = {
					has_trait = wrathful
					has_trait = vengeful
					has_trait = craven
					has_trait = shy
					has_trait = paranoid
					has_trait = sadistic
					diplomacy <= low_skill_rating
				}
			}
			modifier = {
				add = -50
				OR = {
					has_trait = callous
					has_trait = fickle
				}
			}
			modifier = {
				add = -25
				diplomacy < medium_skill_rating
			}
			modifier = {
				add = -50
				diplomacy <= decent_skill_rating
			}
			modifier = {
				add = 100
					diplomacy >= high_skill_rating
			}
			modifier = {
				add = 75
				OR = {
					has_trait = compassionate
					has_trait = just
					has_trait = forgiving
					has_trait = gregarious
					has_trait = calm
				}
			}
			modifier = {
				add = 25
				highest_held_title_tier = tier_kingdom
			}
			modifier = {
				add = 50
				highest_held_title_tier = tier_empire
			}
			modifier = {
				add = 75
				dread > medium_dread
			}	
		}
	}
	# set your knights loose, to quell the crowd
	option = {
		name = jason_race_events.1.b
		flavor = jason_race_events.1.b.tt
		
		if = {
			limit = {
				scope:host = {
					this != root
				}
			}
			scope:host = {
				add_opinion = {
					target = root
					modifier = grateful_opinion
					opinion = 25
				}
			}
		}

		duel = {
			skill = martial
			value = medium_skill_rating
			#Your knights quash the mob back into line
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3
					min = -49
				}
				modifier = {
					add = 20
					scope:host = {
						this = root
					}
				}
				modifier = {
					add = 10
					dread > medium_dread
				}
				modifier = {
					add = 25
					dread > high_dread
				}
				modifier = {
					add = skilled_enforcers_value
					always = yes
				}
				modifier = {
					add = legendary_enforcers_value
					always = yes
				}
				modifier = {
					add = 25
					any_knight = {
						prowess > 25
						location = root.location
					}
				}
				modifier = {
					add = 40
					any_knight = {
						prowess > 30
						location = root.location
					}
				}
				modifier = {
					add = 25
					any_active_accolade = {
						has_accolade_type = thug_attribute
					}
				}
				modifier = {
					add = 25
					any_active_accolade = {
						has_accolade_type = marauder_attribute
					}
				}
				desc = jason_race_events.1.c.tt.win
				send_interface_toast = {
					title = jason_race_events.1.c.tt.win
					left_icon = root
					scope:activity = {
						add_activity_log_entry = {
							key = contest_horse_race_riot_broken_log
							score = 50
							tags = { contest bad }
							character = root

							# Effects
							root = {
								custom_tooltip = jason_race_events.1.a.tt.race
								add_dread = minor_dread_gain
							}
							scope:activity.activity_location.county = { change_county_control = minor_county_control_gain }
						}
					}
					if = {
						limit = {
							any_knight = {
								is_acclaimed = yes
								location = root.location
							}
						}
						every_knight = {
							limit = {
								is_acclaimed = yes
								location = root.location
							}
							accolade = { add_glory = minor_glory_gain }
						}
					}
				}
			}
			#Your knights are overpowered and you are forced to flee the riot
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3
					min = -49
				}
				send_interface_toast = {
					title = jason_race_events.1.c.tt.lose
					left_icon = root
					custom_tooltip = jason_race_events.1.c.tt.lose_knights
					hidden_effect = {
						random_in_list = {
							list = your_warriors
							weight = {
								base = 1
								modifier = {
									add = {
										value = prowess
										multiply = -2
									}
								}
							}
							increase_wounds_effect = { REASON = fight }
							save_scope_as = wounded
						}
					}
					hidden_effect = {
						random_in_list = {
							list = your_warriors
							weight = {
								base = 1
								modifier = {
									add = {
										value = prowess
										multiply = -2
									}
								}
							}
							increase_wounds_effect = { REASON = fight }
							save_scope_as = wounded2
						}
					}
				}
				desc = jason_race_events.1.c.tt.lose
				tournament_horse_race_riot_effect = yes
				scope:host = {
					create_character_memory = { type = failed_riot_at_races_memory }
				}
			}
		}

		if = {
			limit = {
				scope:host = {
					has_relation_rival = root
					has_relation_nemesis = root
				}
			}
			stress_impact = {
				fickle = medium_stress_loss
				base = medium_stress_gain
				compassionate = major_stress_gain
				forgiving = medium_stress_gain
				patient = minor_stress_gain
				calm = minor_stress_gain
				craven = minor_stress_gain
				trusting = minor_stress_gain
			}
		}
		else = {
			stress_impact = {
				compassionate = major_stress_gain
				forgiving = medium_stress_gain
				patient = minor_stress_gain
				calm = minor_stress_gain
				craven = minor_stress_gain
				trusting = minor_stress_gain
			}
		}

		ai_chance = {
			base = 100
			
			modifier = {
				factor = 0
				OR = {				
					has_trait = compassionate
					has_trait = forgiving
					martial <= low_skill_rating
				}
			}
			modifier = {
				add = -50
				OR = {
					has_trait = patient
					has_trait = calm
					has_trait = craven
					has_trait = trusting
				}
			}
			modifier = {
				add = 75
				OR = {				
					has_trait = sadistic
					has_trait = callous
					has_trait = wrathful
					has_trait = vengeful
				}
			}
			modifier = {
				add = 25
				dread > medium_dread
			}
			modifier = {
				add = 25
				any_knight = {
					prowess >= 20
					location = root.location
				}
			}
			modifier = {
				add = 50
				any_knight = {
					prowess > 25
					location = root.location
				}
			}
			modifier = {
				add = -50
				scope:host = {
					has_relation_rival = root
					has_relation_nemesis = root
				}
			}
			modifier = {
				add = -25
				martial < medium_skill_rating
			}
			modifier = {
				add = -50
				martial <= decent_skill_rating
			}
			modifier = {
				add = 100
				martial >= high_skill_rating
			}
		}
	}
	# you throw your support behind the rioters
	option = {
		trigger = {
			this != scope:host
		}
		name = jason_race_events.1.c
		flavor = jason_race_events.1.c.tt
		if = {
			limit = {
				any_knight = {
					is_acclaimed = yes
					location = root.location
					accolade = {
						OR = {
							has_accolade_type = thug_attribute
							has_accolade_type = marauder_attribute
						}
					}
				}
			}
			every_knight = {
				limit = {
					is_acclaimed = yes
					location = root.location
					accolade = {
						OR = {
							has_accolade_type = thug_attribute
							has_accolade_type = marauder_attribute
						}
					}
				}
				accolade = { add_glory = medium_glory_gain }
			}
		}
		progress_towards_rival_effect = {
			CHARACTER = scope:host
			REASON = rival_looted_tournament
 			OPINION = -25
		}
		tournament_horse_race_riot_effect = yes
		if = {
			limit = {
				scope:host = {
					has_relation_rival = root
					has_relation_nemesis = root
				}
			}
			stress_impact = {
				base = medium_stress_loss
				vengeful = minor_stress_loss
				compassionate = major_stress_gain
				forgiving = medium_stress_gain
				craven = minor_stress_gain
				just = minor_stress_gain
			}
		}
		else_if = {
			limit = {
				scope:host = { has_relation_friend = root }
			}
			stress_impact = {
				base = massive_stress_gain
				compassionate = massive_stress_gain
				loyal = major_stress_gain
				just = major_stress_gain
				craven = minor_stress_gain
			}
		}
		else = {
			stress_impact = {
				compassionate = major_stress_gain
				just = major_stress_gain
				craven = minor_stress_gain
			}
		}

		ai_chance = {
			base = 100
			
			modifier = {
				factor = 0
				OR = {			
					has_trait = compassionate
					has_trait = just
					scope:host = {
						has_relation_friend = root
					}
				}
			}
			modifier = {
				add = 400
				scope:host = {
					has_relation_rival = root
					has_relation_nemesis = root
				}
				NOT = {
					has_trait = forgiving
				}
			}
			modifier = {
				add = -50
				OR = {				
					has_trait = craven
					has_trait = forgiving
				}
			}
			modifier = {
				add = 75
				OR = {
					has_trait = sadistic
					has_trait = arbitrary
				}
			}
		}
	}
	# quit the scene and hope for the best
	option = {
		name = jason_race_events.1.d
		flavor = jason_race_events.1.d.tt
		custom_tooltip = jason_race_events.1.d.tt.leave
		if = {
			limit = { this = scope:host }
			add_prestige = medium_prestige_loss
		}
		else = { add_prestige = minor_prestige_loss }
		random_list = {
			50 = {
				modifier = {
					add = 10
					location.culture = { has_cultural_tradition = tradition_loyal_soldiers }
				}
				modifier = {
					add = 10
					location.county = { county_opinion > 20 }
				}
				modifier = {
					add = 10
					location.county.culture = { has_cultural_tradition = tradition_pacifism }
				}
				modifier = {
					add = 10
					location.county.culture = { has_cultural_tradition = tradition_ruling_caste }
				}
				modifier = {
					add = 5
					location.county.culture = { has_cultural_tradition = tradition_xenophilic }
				}
				modifier = {
					add = 5
					location.county.culture = { has_cultural_tradition = tradition_legalistic }
				}
				modifier = {
					add = 5
					location.county.faith = { has_doctrine = tenet_dharmic_pacifism }
				}
				modifier = {
					add = 5
					location.county.faith = { has_doctrine = tenet_pacifism }
				}
				desc = jason_race_events.1.d.tt.win
				send_interface_toast = {
					title = jason_race_events.1.d.tt.win
					left_icon = root
					custom_tooltip = jason_race_events.1.a.tt.race
				}
			}
			50 = {
				modifier = {
					add = 10
					this = scope:host
				}
				modifier = {
					add = 20
					location.county = { county_control <= 90 }
				}
				modifier = {
					add = 20
					location.county = { county_opinion < 0 }
				}
				modifier = {
					add = 15
					location.county.culture = { has_cultural_tradition = tradition_isolationist }
				}
				modifier = {
					add = 15
					location.county.culture = {
						OR = {
							has_cultural_tradition = tradition_byzantine_succession
							has_cultural_tradition = tradition_ep3_roman_ceremonies
						}
					}
				}
				modifier = {
					add = 15
					location.county.culture = { has_cultural_tradition = tradition_quarrelsome }

				}
				modifier = {
					add = 15
					location.county.culture = { has_cultural_tradition = tradition_warrior_culture }
				}
				modifier = {
					add = 5
					location.county.faith = { has_doctrine = tenet_warmonger }
				}
				desc = jason_race_events.1.d.tt.lose
				send_interface_toast = {
					title = jason_race_events.1.d.tt.lose
					left_icon = root
					tournament_horse_race_riot_effect = yes
				}
			}
		}
		if = {
			limit = {
				scope:host = { has_relation_friend = root }
			}
			stress_impact = {
				base = minor_stress_gain
				brave = major_stress_gain
				loyal = medium_stress_gain
				wrathful = medium_stress_gain
				just = minor_stress_gain
				vengeful = minor_stress_gain
			}
		}
		else = {
			stress_impact = {
				brave = major_stress_gain
				loyal = medium_stress_gain
				wrathful = medium_stress_gain
				just = minor_stress_gain
				vengeful = minor_stress_gain
			}
		}

		ai_chance = {
			base = 100
			
			modifier = {
				add = -50
				OR = {				
					has_trait = loyal
					has_trait = vengeful
					has_trait = just
				}
			}
			modifier = {
				add = 100
				OR = {
					has_trait = craven
					has_trait = paranoid
				}
			}
			modifier = {
				add = 75
				this != scope:host
			}
			modifier = {
				factor = 0
				OR = {
					has_trait = brave
					has_trait = wrathful
				}
			}
		}
	}
}

##################################################
# A Turn at Speed
# by Jason Cantalini
# 2
##################################################

#Your jockey pulls off a clever maneuver 
jason_race_events.2 = {
	type = activity_event
	title = jason_race_events.2.t
	desc =  jason_race_events.2.desc
	theme = tournament_contest
	window = widget_activity_locale_fullscreen_event
	left_portrait = {
		character = root
		animation = throne_room_cheer_1
	}

	trigger = {

		#Standard checks.
		exists = var:contest_aptitude
		root.var:horse_race_jockey ?= { exists = yes}
		scope:activity = {
			any_guest_subset_current_phase = {
				name = qualified
				this = root
			}
		}
		var:contest_aptitude >= mediocre_skill_rating
		NOT = {
			has_character_flag = horse_race_contest_event_cooldown
		}
		NOT = {
		 	scope:activity = {
		 		has_variable = jason_race_events_2_cooldown
		 	}
		}
	}

	immediate = {
		play_sound_effect = "event:/DLC/EP2/SFX/Ambience/2D/ep2_amb_2d_crowd_positive_reaction"		
		tournament_pivotal_flavor_variable_effect = { CONTEST = horse_race ID = 2 }
		add_character_flag = {
			flag = horse_race_contest_event_cooldown
			years = 1
		}
		set_variable = jason_race_events_2_cooldown
		activity_tournament_change_contestant_score_effect = { SCORE = increase_medium }
		tournament_horse_race_fetch_horse_effect = yes
		scope:activity = {
			add_activity_log_entry = {
				key = contest_horse_race_passing_maneuver_log
				score = 25
				tags = { contest }
				character = root
			}
		}
		root.var:horse_race_jockey = { save_scope_as = jockey }
	}

	# have this skilled rider train your cavalry
	option = {
		name = jason_race_events.2.a
		flavor = jason_race_events.2.a.tt

		remove_short_term_gold = minor_gold_value

		add_character_modifier = {
			modifier = deft_cavalrymen_modifier
			years = 20
		}

		if = {
			limit = {
				any_active_accolade = {
					OR = {
						has_accolade_type = outrider_attribute
						has_accolade_type = lancer_attribute
						has_accolade_type = horse_archer_attribute
					}
				}
			}
			every_knight = {
				limit = {
					is_acclaimed = yes
					location = root.location
					accolade = {
						OR = {
							has_accolade_type = outrider_attribute
							has_accolade_type = lancer_attribute
							has_accolade_type = horse_archer_attribute
						}
					}
				}
				accolade = { add_glory = minor_glory_gain }
			}
		}

		if = {
			limit = {
				any_knight = {
					has_trait = tourney_participant
					location = root.location
				}
			}
			every_knight = {
				limit = {
					has_trait = tourney_participant
					location = root.location
				}
				add_trait_xp = {
					trait = tourney_participant
					track = horse
					value = 3
				}
			}
		}

		stress_impact = {
			brave = minor_stress_loss
			greedy = minor_stress_gain
			content = minor_stress_gain
		}

		ai_chance = {

			base = 100

			modifier = {
				add = -50
				OR = {
					has_trait = greedy
					has_trait = content
				}
			}
			modifier = {
				add = 100
				has_trait = brave

			}
			modifier = {
				add = 100
				any_active_accolade = {
					OR = {
						has_accolade_type = outrider_attribute
						has_accolade_type = lancer_attribute
						has_accolade_type = horse_archer_attribute
					}
				}
			}
			modifier = {
				factor = 0
				number_maa_regiments_of_base_type = { type = archer_cavalry value < 1 }
			}
			modifier = {
				factor = 0
				number_maa_regiments_of_base_type = { type = light_cavalry value < 1 }
			}
			modifier = {
				factor = 0
				number_maa_regiments_of_base_type = { type = camel_cavalry value < 1 }
			}
			modifier = {
				factor = 0
				gold < medium_gold_value
			}
		}
	}
	# this inspires you to invest in good racers
	option = {
		name = jason_race_events.2.b
		flavor = jason_race_events.2.b.tt

		remove_short_term_gold = minor_gold_value

		add_character_modifier = {
			modifier = daring_scout_modifier
			years = 25
		}

		stress_impact = {
			impatient = minor_stress_loss
			content = medium_stress_gain
			patient = medium_stress_gain
			greedy = minor_stress_gain
		}

		ai_chance = {
			base = 100
			
			modifier = {
				factor = 0
				OR = {			
					has_trait = content
					has_trait = patient
				}
			}
			modifier = {
				add = -50
				has_trait = greedy

			}
			modifier = {
				add = 75
				has_trait = impatient

			}
			modifier = {
				factor = 0
				gold <= minor_gold_value
			}
			modifier = {
				add = -70
				gold <= medium_gold_value
			}
		}
	}
	# boast about your racers
	option = {
		name = jason_race_events.2.c
		flavor = jason_race_events.2.c.tt
		
		add_prestige = minor_prestige_gain

		stress_impact = {
			arrogant = minor_stress_loss
			humble = major_stress_gain
			shy = minor_stress_gain
			ambitious = minor_stress_gain
		}

		ai_chance = {
			base = 100
			
			modifier = {
				factor = 0
				has_trait = humble

			}
			modifier = {
				add = -75
				OR = {				
					has_trait = shy
					has_trait = ambitious
				}
			}
			modifier = {
				add = 100
				OR = {
					has_trait = ambitious
					has_trait = gregarious
				}
			}
		}
	}
}
