﻿# Effect which initialises the a given character as having been scouted by
# the given magister character.
#
# scope = The target character being scouted.
# scope:councillor = the councillor_raid_leader performing the scout target task.
# MAGISTER = The magister whose councillor is doing the scouting.
regula_scout_target_effect = {
	save_temporary_scope_as = scoutee
	add_character_modifier = {
		modifier = regula_scouted_target_modifier
		months = { 12 36 }
	}
	add_secret = {
		type = regula_scouted_target_secret
	}

	$MAGISTER$ = {
		send_interface_message = {
			type = secret_discovered_message
			title = regula_scouted_target_success_title
			left_icon = scope:councillor
			right_icon = scope:scoutee

			scope:scoutee = {
				every_secret = {
					limit = {
						secret_type = regula_scouted_target_secret
					}
					reveal_to = $MAGISTER$
				}
			}
		}
	}
}

# Selects a character to be taken as a captive during border raids.
#
# scope:councillor_liege = liege of the councillor_raid_leader
# scope:border_raids_county = the county being raided
# SAVE_AS = the output scope to save the captive character to
regula_border_raids_select_captive_effect = {
	if = { # If a previously scouted target is in the county, pick one.
		limit = {
			scope:councillor_liege = {
				any_known_secret = {
					secret_type = regula_scouted_target_secret
					secret_owner = {
						location.county ?= scope:border_raids_county
					}
				}
			}
		}

		scope:councillor_liege = {
			random_known_secret = {
				limit = {
					secret_type = regula_scouted_target_secret
					secret_owner = {
						location.county ?= scope:border_raids_county
					}
				}
				secret_owner = {
					save_scope_as = $SAVE_AS$
				}
			}
		}
	}
	else_if = { # Else, if there is an existing pool character, pick one.
		limit = {
			scope:border_raids_county = {
				any_county_province = {
					any_pool_character = {
						province = this
						is_valid_border_raids_pool_captive = yes
					}
				}
			}
		}

		scope:border_raids_county = {
			random_county_province = {
				limit = {
					any_pool_character = {
						province = this
						is_valid_border_raids_pool_captive = yes
					}
				}

				random_pool_character = {
					province = this
					limit = {
						is_valid_border_raids_pool_captive = yes
					}
					save_scope_as = $SAVE_AS$
				}
			}
		}
	}
	else = { # Otherwise, create a new character.
		scope:border_raids_county = {
			random_county_province = {
				save_temporary_scope_as = tmp_create_character_location
			}
		}
		regula_create_random_lowborn_woman_effect = {
			SAVE_AS = $SAVE_AS$
			LOCATION = scope:tmp_create_character_location
			CULTURE = scope:border_raids_county.culture
			FAITH = scope:border_raids_county.faith
		}
	}
}

# Adds appropriate opinion modifiers for characters who were raided.
#
# scope:councillor_liege = liege of the councillor_raid_leader
# scope:border_raids_angry_liege = the liege being raided
regula_border_raids_add_negative_opinion_effect = {
	scope:border_raids_angry_liege = {
		if = {
			limit = {
				has_opinion_modifier = {
					target = scope:councillor_liege
					modifier = raided_me_opinion
				}
			}
			remove_opinion = { # We reapply it so that the counter is reset
				target = scope:councillor_liege
				modifier = raided_me_opinion
			}
		}
		add_opinion = {
			target = scope:councillor_liege
			modifier = raided_me_opinion
		}
		every_liege_or_above = {
			if = {
				limit = {
					has_opinion_modifier = {
						target = scope:councillor_liege
						modifier = raided_me_opinion
					}
				}
				remove_opinion = { # We reapply it so that the counter is reset
					target = scope:councillor_liege
					modifier = raided_me_opinion
				}
			}
			add_opinion = {
				target = scope:councillor_liege
				modifier = raided_me_opinion
			}
		}
	}
}

# Applies effects for the completion of task_border_raids.
# In particular, determines the county raided, the amount of gold gained,
# and the outcome (captive, liege angered, nothing).
#
# scope:councillor = councillor_raid_leader
# scope:councillor_liege = liege of the councillor_raid_leader
regula_border_raids_effect = {
	# Select a county getting hit by the raid.
	scope:councillor_liege = {
		random_character_to_title_neighboring_county = {
			limit = {
				is_valid_border_raids_county = yes
			}

			save_temporary_scope_as = border_raids_county
		}
	}

	save_temporary_scope_value_as = {
		name = border_raids_gold
		value = task_border_raids_gold_gain
	}

	random_list = {
		0 = { # Capture Lowborn
			modifier = {
				# 70% - 100% chance based on skill
				add = task_border_raids_captive_chance
			}

			regula_border_raids_select_captive_effect = {
				SAVE_AS = border_raids_captive
			}

			scope:councillor_liege = {
				send_interface_message = {
					type = event_martial_good_with_text
					title = regula_border_raids_captured_title
					desc = regula_border_raids_captured_desc
					left_icon = scope:border_raids_county
					right_icon = scope:border_raids_captive

					add_gold = scope:border_raids_gold

					imprison = {
						target = scope:border_raids_captive
						type = dungeon
					}

					scope:border_raids_captive = {
						add_opinion = {
							modifier = imprisoned_me
							target = scope:councillor_liege
						}

						stress_impact = {
							base = medium_stress_impact_gain
							craven = minor_stress_impact_gain
						}
					}

					stress_impact = {
						greedy = miniscule_stress_impact_loss
						sadistic = minor_stress_impact_loss
					}
				}
			}
		}
		0 = { # Foreign Liege Angered
			modifier = {
				# 10% - 0% chance based on skill
				add = task_border_raids_anger_liege_chance
			}

			scope:border_raids_county = {
				holder = {
					save_temporary_scope_as = border_raids_angry_liege
				}
			}

			scope:councillor_liege = {
				send_interface_message = {
					type = event_martial_bad_with_text
					title = regula_border_raids_angered_title
					desc = regula_border_raids_angered_desc
					left_icon = scope:border_raids_county
					right_icon = scope:border_raids_angry_liege

					add_gold = scope:border_raids_gold

					regula_border_raids_add_negative_opinion_effect = yes

					stress_impact = {
						craven = minor_stress_impact_gain
						greedy = miniscule_stress_impact_loss
						paranoid = minor_stress_impact_gain
						shy = miniscule_stress_impact_gain
					}
				}
			}
		}
		0 = { # No added effect
			modifier = {
				# 20% - 0% chance based on skill
				add = {
					value = 100
					subtract = task_border_raids_captive_chance
					subtract = task_border_raids_anger_liege_chance
				}
			}

			scope:councillor_liege = {
				send_interface_message = {
					type = event_martial_good_with_text
					title = regula_border_gold_only_title
					desc = regula_border_gold_only_desc
					right_icon = scope:border_raids_county

					add_gold = scope:border_raids_gold

					stress_impact = {
						greedy = miniscule_stress_impact_loss
					}
				}
			}
		}
	}
}
