﻿namespace = almohad

#Events for the Almohad invasion Story Cycle

almohad.0001 = {
	scope = none
	hidden = yes

	immediate = {
		find_title_for_almohad_spawn_effect = yes
	}
}

### Spawn the Almohads
almohad.0002 = {
	scope = none
	hidden = yes

	trigger = {
		NOT = { exists = faith:masmudi.religious_head_title.holder }
		scope:almohad_county = {
			faith = faith:masmudi
			holder = {
				trigger_if = { # Invalidate if the top liege has converted to Masmudi, but NOT if the top liege is the holder themselves, UNLESS they are also the holder of the Maghreb title (in which case the Almohad's goals have already been achieved)
					limit = {
						OR = {
							top_liege != this
							any_held_title = {
								this = title:k_maghreb
							}
						}
					}
					NOT = { top_liege.faith = faith:masmudi }
				}
				OR = { # Last resort check to make sure that we don't automatically game over a player
					is_ai = yes # Either they have to be AI
					any_held_title = { # Or they have to hold land elsewhere
						title_tier = county
						this != scope:almohad_county
					}
				}
			}
		}
	}

	immediate = {
		debug_log = "Almohads appeared!"
		debug_log_date = yes
		create_almohads_effect = yes
	}
}

### Almohads were defeated in battle
almohad.0003 = {
	hidden = yes
	scope = combat_side

	trigger = {
		exists = side_commander
		exists = side_commander.top_liege
		exists = global_var:spawned_almohads
		NOT = { exists = global_var:almohads_were_defeated }
		side_commander = {
			top_liege = {
				any_owned_story = {
					type = story_almohad_invasion
				}
			}
		}
	}

	immediate = {
		debug_log = "Almohads were defeated in battle!"
		set_global_variable = {
			name = almohads_were_defeated
			value = yes
		}
	}
}

# Maintanenance event to handle player responses to Almohads going to war
almohad.0004 = {
	scope = none
	hidden = yes

	trigger = {	
		exists = global_var:almohads_waiting_for_player_responses
		exists = global_var:almohads_player_responses_received
	}

	immediate = {
		if = {
			limit = {
				global_var:almohads_waiting_for_player_responses < global_var:almohads_player_responses_received
			}
			trigger_event = {
				id = almohad.0004
				days = 5
			}
		}
		else_if = {
			limit = { NOT = { exists = global_var:almohads_appeased } }
			almohads_go_to_war_effect = yes
		}
		else = {
			debug_log = "Almohads were appeased and will not go to war!"
			remove_global_variable = almohads_waiting_for_player_responses
			remove_global_variable = almohads_player_responses_received
		}
	}
}


################################
# Warning that a County has converted
# by Petter Vilberg
################################
almohad.1001 = {
	type = character_event
	title = almohad.1001.t
	desc = almohad.1001.desc
	theme = faith
	left_portrait = {
		character = root
		animation = worry
	}
	
	immediate = {
		faith:masmudi = {
			save_scope_as = masmudi
		}
		show_as_tooltip = {
			scope:almohad_county = {
				E_kCAFG_convert_faith_in_all_county_provinces = {
					FACTOR = 0.65
					VARIANCE = 0.25
					FAITH = faith:masmudi
					MAX = 0.9
				}
			}
		}

		# Paint with an extremely broad brush to ensure that any AI conversion progress in the county is lost
		hidden_effect = {
			if = {
				limit = {
					scope:almohad_county = {
						is_target_of_council_task = task_conversion
					}
				}
				scope:almohad_county.holder = {
					if = {
						limit = {
							exists = cp:councillor_court_chaplain
							cp:councillor_court_chaplain = {
								is_performing_council_task = task_conversion
							}
						}
						cp:councillor_court_chaplain = { start_default_task = yes }
					}
					every_liege_or_above = {
						limit = {
							exists = cp:councillor_court_chaplain
							cp:councillor_court_chaplain = {
								is_performing_council_task = task_conversion
							}	
						}
						cp:councillor_court_chaplain = { start_default_task = yes }
					}
				}					
			}
		}
	}

	option = { #It'll probably be fine
		name = almohad.1001.a
		flavor = almohad.1001.a.flavor
	}

	option = { # We better do something about this
		name = almohad.1001.b
		trigger = {
			exists = cp:councillor_court_chaplain
		}
		if = {
			limit = { cp:councillor_court_chaplain = { is_performing_council_task = task_conversion } }
			custom_tooltip = almohad.1001.b.conversion_tt
		}
		cp:councillor_court_chaplain = {
			set_council_task = {
				task_type = task_conversion
				target = scope:almohad_county.title_province
			}
		}
	}

	after = {
		custom_tooltip = almohad.1001.player_warning
	}
}


######################################
# The Almohads Have Risen to Attack!
# by Petter Vilberg
######################################
almohad.1002 = {
	type = character_event
	title = almohad.1001.t
	desc = almohad.1002.desc
	theme = faith
	override_background = {
		reference = terrain_scope
	}
	left_portrait = {
		character = scope:ibn_tumart
		animation = personality_zealous
	}

	immediate = {
		scope:almohad_county = {
			title_province = {
				save_scope_as = background_terrain_scope
			}
		}
	}

	option = { # Fight them off!
		name = almohad.1002.a
		if = {
			limit = {
				OR = {
					this = scope:first_target
					this = scope:second_target
				}
			}
			custom_tooltip = almohad.1002.a.target.tt
		}
		else = {
			custom_tooltip = almohad.1002.a.not_target.tt
		}
	}

	option = { # They have some good points...
		name = almohad.1002.b
		flavor = almohad.1002.b.flavor
		set_character_faith_with_conversion = faith:masmudi
		if = {
			limit = { 
				faith:masmudi = { has_doctrine = doctrine_monotheist }
			}
			custom_description_no_bullet = { text = mandala_monotheist_warning_tt }
		}
		add_piety = massive_piety_loss
		add_piety_level = -1
		scope:ibn_tumart = {
			add_opinion = {
				target = root
				modifier = pious_opinion
				opinion = 50
			}
		}
		scope:al_mumin = {
			add_opinion = {
				target = root
				modifier = pious_opinion
				opinion = 50
			}
		}
		if = {
			limit = {
				this = scope:first_target
			}
			set_global_variable = {
				name = almohads_appeased
				value = yes
			}
		}
		else_if = {
			limit = {
				this = scope:second_target
			}
			set_global_variable = {
				name = almohads_do_not_attack_second_target
				value = yes
			}
		}
	}

	after = {
		change_global_variable = {
			name = almohads_player_responses_received
			add = 1
		}
	}
}

################################
# Succession Among the Almohads
# by Petter Vilberg
################################
almohad.1003 = {
	type = character_event
	title = almohad.1001.t
	desc = almohad.1003.desc
	theme = faith
	override_background = {
		reference = terrain_scope
	}
	left_portrait = {
		character = scope:al_mumin
		animation = personality_zealous
	}
	right_portrait = scope:ibn_tumart

	immediate = {
		scope:almohad_county = {
			title_province = {
				save_scope_as = background_terrain_scope
			}
		}
	}

	option = {
		name = almohad.1003.a
		custom_tooltip = almohad.1003.a.tt
	}
}


################################
# Almohads Control Marrakesh
# by Petter Vilberg
################################
almohad.1004 = {
	type = character_event
	title = almohad.1004.t
	desc = almohad.1004.desc
	theme = realm
	left_portrait = root

	immediate = {
		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = no
		}
		title:c_marrakesh = {
			save_scope_as = marrakesh
			faith = {
				save_scope_as = old_faith
			}
			change_title_holder = {
				holder = root
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		faith:masmudi = {
			save_scope_as = masmudi
		}
	}

	option = { # This will be our new capital!
		name = almohad.1004.a
		set_realm_capital = scope:marrakesh
		scope:marrakesh = {
			change_development_level = -1
			E_kCAFG_convert_faith_in_all_county_provinces = {
				FACTOR = 0.7
				VARIANCE = 0.2
				FAITH = faith:masmudi
				MAX = 0.9
			}
			add_county_modifier = {
				modifier = rebuilding_marrakesh_modifier
				years = 50
			}
		}
	}

	option = { # Only convert
		name = almohad.1004.b
		scope:marrakesh = {
			change_development_level = -1
			E_kCAFG_convert_faith_in_all_county_provinces = {
				FACTOR = 0.6
				VARIANCE = 0.4
				FAITH = faith:masmudi
				MAX = 0.9
			}
			add_county_modifier = {
				modifier = rebuilding_marrakesh_modifier
				years = 50
			}
		}
	}

	option = { # Leave it be
		name = almohad.1004.c
		add_character_modifier = {
			modifier = refused_to_rebuild_marrakesh_modifier
			years = 10
		}
	}
}