namespace = harem

#################################################
# Harem Rally Events
#################################################
# Cost money and piety to initiate with a decision.
# For duration of event give a tax bonus.
# Possible outcomes or events during the rally:
# - Gain gold from donations
# - Piety points gained
# - Character joins realm
# - County is converted to None
# - Character is converted to None
# - Research points gained
# - Entertainers
##################################################

# Beginning preparations
character_event = {
	id = harem.10000
	desc = "EVTDESCharem.10000"
	picture = GFX_evt_haremrally
	
	is_triggered_only = yes

	immediate = {
		hidden_tooltip = {
			character_event = {
				id = harem.10001
				days = 1
				random = 2
			}
		}
	}

	option = {
		name = "EVTOPTAharem.10000"
	}
}

# Rally Preparations
character_event = {
	id = harem.10001
	desc = "EVTDESCharem.10001"
	picture = GFX_evt_feast

	only_rulers = yes
	capable_only = yes
	prisoner = no

	is_triggered_only = yes

	trigger = {
		war = no
		has_character_modifier = hosting_harem_rally
		NOT = {
			has_character_flag = rally_prepared
		}
	}

	option = {
		name = "EVTOPTAharem.10001" #Plan to give speech yourself
		trigger = {
			NOT = { trait = shy }
			diplomacy = 8
		}
		ai_chance = {
			factor = 30

			modifier = {
				factor = 2
				trait = gregarious
			}
			modifier = {
				factor = 2
				primary_title = { higher_tier_than = DUKE }
			}
		}
		set_character_flag = rally_prepared
		set_character_flag = rally_speaker
		prestige = 10
		hidden_tooltip = {
			character_event = {
				id = harem.10002 #send invites
				days = 2
				random = 5
			}
		}
	}
	option = {
		name = "EVTOPTBharem.10001" #Invite speaker from realm
		trigger = {
			any_realm_character = {
				diplomacy = 8
				OR = {
					religion_group = harem_group
					trait = harem_lover
				}
			}
		}
		ai_chance = {
			factor = 30
		}
		random_realm_character = {
			limit = {
				diplomacy = 8
				OR = {
					religion_group = harem_group
					trait = harem_lover
				}
			}
			# set this character as the speaker and make sure he attends rally
			set_character_flag = rally_speaker
			set_character_flag = coming_to_rally
			treasury = 10
			prestige = 10
		}
		set_character_flag = rally_prepared
		treasury = -10
		hidden_tooltip = {
			character_event = {
				id = harem.10002
				days = 2
				random = 5
			}
		}

	}
	option = {
		name = "EVTOPTCharem.10001" #Invite speaker from another realm
		
		ai_chance = {
			factor = 30
			
			modifier = {
				factor = 0.5
				trait = greedy
			}
		}
		
		# Create a new random speaker
		create_character = {
			random_traits = yes
			religion = harem
			dynasty = random
		}
		new_character = {
#			hidden_tooltip = {
#				if = {
#					limit = {
#						NOT = { diplomacy = 10 }
#					}
#					diplomacy = 10
#				}
#			}
			diplomacy = 10
			set_character_flag = rally_speaker
			set_character_flag = coming_to_rally
			treasury = 20
			prestige = 10
		}
		
		set_character_flag = rally_prepared
		treasury = -20

		hidden_tooltip = {
			character_event = {
				id = harem.10002
				days = 2
				random = 5
			}
		}

	}
}

###################################
# Invitations to harem
###################################

# Send invites
character_event = {
	id = harem.10002
	desc = "EVTDESCharem.10002"
	picture = GFX_evt_feast

	is_triggered_only = yes

	only_rulers = yes
	capable_only = yes
	prisoner = no

	trigger = {
		war = no
		has_character_modifier = hosting_harem_rally
		has_character_flag = rally_prepared
		NOT = {
			has_character_flag = sent_invitations
		}
	}
	
	option = {
		name = "EVTOPTAharem.10002"
		set_character_flag = sent_invitations
		any_realm_character = {
			limit = {
				prisoner = no
				NOT = { trait = incapable }
				NOT = { has_character_modifier = hosting_harem_rally }
				war = no
				is_adult = yes
			}
			letter_event = {
				id = harem.10003
				days = 1
				random = 2
				tooltip = "EVTTOOLTIPharem.10003"
			}
		}
	}
}

# Character is invited to the rally
letter_event = {
	id = harem.10003
	desc = "EVTDESCharem.10003"

	is_triggered_only = yes

	option = {
		name = "EVTOPTAharem.10003" #Travel to the rally
		ai_chance = {
			factor = 1
			modifier  = {
				factor = 0.1
				ai = yes
				trait = in_hiding
			}
			modifier = {
				factor = 2
				opinion = { who = from value = -40 }
			}
			modifier  = {
				factor = 2
				opinion = { who = from value = -20 }
			}
			modifier = {
				factor = 2
				OR = {
					religion_group = harem_group
					trait = harem_lover
					trait = lustful
				}
			}
			modifier = {
				factor = 2
					religion_group = harem_group
					trait = zealous
			}
		}
		set_character_flag = coming_to_rally
		if = {
			limit = { trait = in_hiding	}
			remove_trait = in_hiding
			clr_character_flag = do_not_disturb
			add_character_modifier = {
				name = went_out_of_hiding_timer
				duration = 180
				hidden = yes
			}
			hidden_tooltip = { character_event = { id = CM.6400 } } # Notify plotters and family
		}
	}
	option = {
		name = "EVTOPTBharem.10003" #Refuse
		ai_chance = {
			factor = 1
			modifier  = {
				factor = 0
				opinion = { who = from value = 0 }
				NOT = { trait = in_hiding }
			}
			modifier  = {
				factor = 2
				NOT = { opinion = { who = from value = -60 } }
			}
			modifier  = {
				factor = 2
				NOT = { opinion = { who = from value = -80 } }
			}
			modifier = {
				factor = 1.5
				NOT = { religion_group = harem_group }
			}
			modifier = {
				factor = 2
				NOT = { religion_group = harem_group }
					trait = zealous
			}
		}
	}
}

###################################
# Events during Rally
###################################

#The guests have arrived
character_event = {
	id = harem.10004
	desc = "EVTDESCharem.10004"
	picture = GFX_evt_haremrally

	only_rulers = yes
	capable_only = yes
	prisoner = no

	trigger = {
		war = no
		has_character_modifier = hosting_harem_rally
		has_character_flag = sent_invitations
		NOT = {
			has_character_flag = host_harem_rally_started
		}
		any_realm_character = {
			NOT = {
				has_character_flag = guest_harem_rally_started
			}
			has_character_flag = coming_to_rally
		}
	}

	mean_time_to_happen = {
		days = 14
	}

	option = {
		name = "EVTOPTAharem.10004"
		set_character_flag = host_harem_rally_started
		
		any_realm_character = {
			limit = {
				has_character_flag =  coming_to_rally
			}
			character_event = {
				id = harem.10005
				tooltip = "EVTTOOLTIP72016"
			}
		}
		
		# Trigger the entertainment event
		character_event = {
			id = harem.10007
			days = 1
		}
		
		log = "harem.10004: The guests have arrived."
	}
}

#Welcome to the rally!
character_event = {
	id = harem.10005
	desc = "EVTDESCharem.10005"
	picture = GFX_evt_haremrally

	is_triggered_only = yes

	option = {
		name = "EVTOPTAharem.10005"
		set_character_flag = guest_harem_rally_started
	}
}

# Donations of gold
character_event = {
	id = harem.10006
	desc = EVTDESCharem.10006
	picture = GFX_evt_haremrally
	
	is_triggered_only = yes
	
	immediate = {
		set_character_flag = harem_rally_donations
	}
	
	option = {
		name = EVTOPTAharem.10006
		random_list = {
			50 = { wealth = 10 }
			40 = { wealth = 25 }
			10 = { wealth = 50 }
		}
		
		log = "harem.10006: harem rally donations"
	}
}

# 1st, Musicians and some other entertainers from feast event
character_event = {
	id = harem.10007
	desc = EVTDESCharem.10007
	picture = GFX_evt_haremrally

	is_triggered_only = yes
	
	immediate = {
		log = "harem.10007: Entertainment event at the rally."
	}
	
	option = {
		name = EVTOPTAharem.10007
		prestige = 10
		
		set_character_flag = harem_rally_entertainment
		
		random_realm_character = {
			limit = {
				has_character_flag = guest_harem_rally_started
			}
			
			character_event = {
				id = 72068
				tooltip = "EVTTOOLTIP72068"
			}
		}
		
		# if host is speaker, then speak
		if = { limit = { has_character_flag = rally_speaker }
			character_event = {
				id = harem.10008
				days = 1
			}
		}
		
		#else, have the speaker speak
		if = { limit = { NOT = { has_character_flag = rally_speaker} }
			random_realm_character = {
				limit = {
					has_character_flag = rally_speaker
					has_character_flag = guest_harem_rally_started					
				}
				
				character_event = {
					id = harem.10008
					days = 1
				}
			}
		}
	}
}


# 2nd, Speech
character_event = {
	id = harem.10008
	desc = EVTDESCharem.10008
	picture = GFX_evt_haremrally

	is_triggered_only = yes
	
	immediate = {
		log = "harem.10008: [ROOT.GetTitledName] delivers charm at harem Rally."
		
		# speaker gains prestige
		prestige = 10
		
		# if speaker is the host
		if = {
			limit = {
				has_character_flag = host_harem_rally_started
				has_character_flag = harem_rally_entertainment
			}
			save_event_target_as = target_host
		}
		
		# if speaker is not the host
		if = {
			limit = {
				NOT = { has_character_flag = host_harem_rally_started }
				NOT = { has_character_flag = harem_rally_entertainment }
			}
			
			random_realm_character = {
				limit = {
					has_character_flag = host_harem_rally_started
					has_character_flag = harem_rally_entertainment
				}
				save_event_target_as = target_host
			}
		}
		
		# set host flags and trigger host character events
		event_target:target_host = {
			log = "harem.10008: Triggering final rally events with [THIS.GetTitledName] as host."
			
			clr_character_flag = harem_rally_entertainment
			set_character_flag = harem_rally_speech
			
			#trigger donations event after 2 days
			character_event = {
				id = harem.10006
				days = 2
			}
			
			# trigger final event after 3 days
			character_event = {
				id = harem.10013
				days = 4
			}
		}
		
		any_realm_character = {
			limit = {
				NOT = { religion = harem }
				NOT = { trait = harem_lover }
				NOT = { has_character_flag = harem_rally_conversion }
				has_character_flag = coming_to_rally
				has_character_flag = guest_harem_rally_started
			}
			character_event = {
				id = harem.10011
			}
		}
		
		# trigger Conversion events in Closeted Atheist attendees
		any_realm_character = {
			limit = {
				NOT = { religion = harem }
				NOT = { has_character_flag = harem_rally_conversion }
				has_character_flag = coming_to_rally
				has_character_flag = harem_harem_rally_started
				trait = harem_lover
			}
			character_event = {
				id = harem.10010
			}
		}
	}
	
	# County Conversion
	# Inspire people to come out as harem lovers
	# 		This is really what the main focus of the harem should be.
	option = {
		name = EVTOPTAharem.10008
		
		ai_chance = { factor = 9 }
		
		trigger = {
			# host has a province that can be converted, and no character conversion events happened during this rally
			event_target:target_host = {
				NOT = { has_character_flag = harem_rally_conversion }
				any_demesne_province = { NOT = { religion = harem } }
			}
		}
		
		# fire off province conversion event in host's province
		event_target:target_host = {
			random_demesne_province = {
				# TODO: Consider limiting to the capital province that is hosting the rally?
				limit = { NOT = { religion = harem } }
				
				#trigger conversion event
				province_event = {
					id = harem.10009
					days = 1
				}
			}
		}
	}
	
	# Organize research efforts
	option = {
		name = EVTOPTBharem.10008
		ai_chance = { factor = 1 }
		
		#trigger research event
		event_target:target_host = {
			character_event = {
				id = harem.10012
				days = 1
			}
		}
	}
}

## Things that happen after entertainment and speech
# County is converted to harem
province_event = {
	id = harem.10009
	desc = EVTDESCharem.10009
	picture = GFX_evt_haremrally
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTAharem.10009
		owner = {
			piety = 100
			set_character_flag = harem_rally_conversion
		}
		religion = harem
		log = "harem.10009: province conversion"
	}
}

# Character is converted to harem
character_event = {
	id = harem.10010
	desc = EVTDESCharem.10010
	picture = GFX_evt_haremrally
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	immediate = {
		set_character_flag = harem_rally_conversion
		random_realm_character = {
			limit = { has_character_flag = host_harem_rally_started }
			set_character_flag = harem_rally_conversion
		}
	}
	
	# Become harem lover
	option = {
		name = EVTOPTAharem.10010
		ai_chance = {
			factor = 1
			modifier = {
				factor = 2
				trait = brave
			}
			modifier = {
				factor = 2
				OR = {
					any_spouse = {
						religion = harem
						hidden_tooltip = {
							is_alive = yes
						}
					}
					any_consort = {
						religion = harem
						hidden_tooltip = {
							is_alive = yes
						}
					}
				}
			}
		}
		
		if = {
			limit = { higher_tier_than = BARON }
			religion_authority = {
				modifier = ruler_converted_from
			}
		}
		religion = harem
		if = {
			limit = { higher_tier_than = BARON }
			hidden_tooltip = {
				religion_authority = {
					modifier = ruler_converted_to
				}
			}
		}
		piety = 100
		
		random = {
			chance = 70
			
			add_trait = brave
			hidden_tooltip = {
				character_event = {
					id = 38270 #Notify brave
				}
			}
		}
		
		log = "harem.10010: [ROOT.GetTitledName] converted to harem"
	}
	
	# Keep your lack of faith to yourself
	option = {
		name = EVTOPTBharem.10010
		ai_chance = {
			factor = 1
			modifier = {
				factor = 2
				trait = craven
			}
			modifier = {
				factor = 2
				trait = shy
			}
		}
		
		log = "harem.10010: [ROOT.GetTitledName] did not convert"
	}
}

# Character gains Harem Lover trait
character_event = {
	id = harem.10011
	desc = EVTDESCharem.10011
	picture = GFX_evt_haremrally
	border = GFX_event_normal_frame_religion
	
	is_triggered_only = yes
	
	immediate = {
		set_character_flag = harem_rally_conversion
		random_realm_character = {
			limit = { has_character_flag = host_harem_rally_started }
			set_character_flag = harem_rally_conversion
		}
	}
	
	# Become Harem_lover
	option = {
		name = EVTOPTAharem.10011
		ai_chance = {
			factor = 0.5
			
			modifier = {
				factor = 1.5
				trait = lustful
			}
			
			modifier = {
				factor = 1.5
				learning = 10
			}
			
			modifier = {
				factor = 2
				learning = 20
			}
			
			modifier = {
				factor = 0.01
				trait = zealous
			}
		}
		
		add_trait = harem_lover
		piety = -10
		
		log = "harem.10011: [ROOT.GetTitledName] gains Harem Lover trait"
	}
	
	# Remain faithful to religion
	option = {
		name = EVTOPTBharem.10011
		ai_chance = {
			factor = 0.30
			
			modifier = {
				factor = 1.5
				piety = 100
			}
			
			modifier = {
				factor = 2
				piety = 250
			}
			
			modifier = {
				factor = 2
				trait = zealous
			}
		}
		
		piety = 5
		log = "harem.10011: [ROOT.GetTitledName] didn't gain Harem Lover trait"
	}
}


# Research points gained
character_event = {
	id = harem.10012
	desc = EVTDESCharem.10012
	picture = GFX_evt_haremrally
	
	is_triggered_only = yes
	
	immediate = {
		log = "harem.10012: Research Points gained."
	}
	
	# military
	option = {
		name = EVTOPTAharem.10012
		random_list = {
			50 = { military_techpoints = 10 }
			40 = { military_techpoints = 20 }
			10 = { military_techpoints = 30 }
		}
	}
	
	# economy
	option = {
		name = EVTOPTBharem.10012
		random_list = {
			50 = { economy_techpoints = 10 }
			40 = { economy_techpoints = 20 }
			10 = { economy_techpoints = 30 }
		}
	}
	
	# culture
	option = {
		name = EVTOPTCharem.10012
		random_list = {
			50 = { culture_techpoints = 10 }
			40 = { culture_techpoints = 20 }
			10 = { culture_techpoints = 30 }
		}
	}
}

# Piety points and religion_authority gained
character_event = {
	id = harem.10013
	desc = EVTDESCharem.10013
	picture = GFX_evt_haremrally
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTAharem.10013
		piety = 50
		religion_authority = 2
		}
		
#		log = "harem.10013: Piety points and religion_authority gained."
	}

###########################################
# Flag management                         #
###########################################

# Safety catch - clears character flags and modifiers
character_event = {
	id = harem.10999

	hide_window = yes
	
	is_triggered_only = yes
	
	immediate = {
		remove_character_modifier = hosting_harem_rally
		clr_character_flag = sent_invitations
		clr_character_flag = host_harem_rally_started
		clr_character_flag = rally_prepared
		clr_character_flag = rally_speaker
		clr_character_flag = harem_rally_donations
		clr_character_flag = harem_rally_entertainment
		clr_character_flag = harem_rally_speech
		clr_character_flag = harem_rally_conversion
		clr_character_flag = harem_rally_research
		clr_character_flag = do_not_disturb
		
		any_realm_character = {
			clr_character_flag = coming_to_rally
			clr_character_flag = rally_speaker
			clr_character_flag = guest_harem_rally_started
		}
		
		log = "harem.10999: cleared flags."
	}
}
