﻿namespace = gdp_merchant_scheme_advantage

scripted_effect merchant_scheme_advantage_save_scopes_effect = {
	scope:scheme = {
		task_contract = {
			save_scope_as = task_contract
			task_contract_taker = { save_scope_as = merchant }
			task_contract_employer = {
				save_scope_as = trader
				assigned_trade_route ?= { save_scope_as = route }
			}
			task_contract_location = {
				save_scope_as = task_contract_location
				barony.trade_market = {
					save_scope_as = market
					every_character_in_market = {
						limit = {
							government_has_flag = government_is_merchant
							NOT = { this = scope:merchant }
						}
						add_to_list = potential_rivals
					}
				}
			}
			task_contract_target ?= { save_scope_as = task_contract_target }
		}
		every_scheme_agent_character = {
			limit = {
				NOT = { this = scope:trader }
			}
			add_to_list = potential_agents
		}
	}
	hidden_effect = {
		# Save portrait scope
		random_list = {
			10 = {
				trigger = {
					any_in_list = { list = potential_agents }
				}
				random_in_list = {
					list = potential_agents
					limit = {
						is_available_quick = {
							imprisoned = no
							incapable = no
						}
						NOT = { this = scope:trader }
					}
					save_scope_as = agent
					save_scope_as = portrait
				}
			}
			10 = {
				trigger = {
					court_position:partner_court_position ?= {
						is_available_quick = {
							imprisoned = no
							incapable = no
						}
						NOT = { this = scope:trader }
					}
				}
				court_position:partner_court_position = {
					save_scope_as = partner
					save_scope_as = portrait
				}
			}
			30 = {
				scope:trader = { save_scope_as = portrait }
			}
		}
		# Save rival scope
		random_in_list = {
			list = potential_rivals
			save_scope_as = rival
		}
		# Save malus
		random_list = {
			10 = {
				modifier = {
					add = -10
					scope:scheme = { has_scheme_modifier = gdp_merchant_scheme_advantage_success_chance_potential_modifier }
				}
				save_scope_value_as = { name = malus value = flag:potential }
			}
			10 = {
				modifier = {
					add = -10
					scope:scheme = { has_scheme_modifier = gdp_merchant_scheme_advantage_success_chance_growth_modifier }
				}
				save_scope_value_as = { name = malus value = flag:growth }
			}
			10 = {
				modifier = {
					add = -10
					scope:scheme = { has_scheme_modifier = gdp_merchant_scheme_advantage_success_chance_length_modifier }
				}
				save_scope_value_as = { name = malus value = flag:length }
			}
			10 = {
				save_scope_value_as = { name = malus value = flag:opportunity }
			}
			10 = {
				trigger = {	NOT = { scope:portrait = scope:trader } }
				save_scope_value_as = { name = malus value = flag:quit }
			}
			10 = {
				trigger = { NOT = { scope:task_contract = { has_task_contract_type = promote_goods } } }
				save_scope_value_as = { name = malus value = flag:competitiveness }
			}
			10 = {
				trigger = {
					exists = scope:rival.house
					NOT = { scope:merchant.house ?= scope:rival.house }
				}
				save_scope_value_as = { name = malus value = flag:relation }
			}
		}
		# Save duel type
		random_list = {
			10 = {
				trigger = { house.house_unity_value >= 33 }
				save_scope_value_as = { name = duel_type value = flag:unity }
			}
			10 = {
				save_scope_value_as = { name = duel_type value = flag:stress }
			}
			10 = {
				save_scope_value_as = { name = duel_type value = flag:associate }
			}
		}
		# Save spend type
		random_list = {
			10 = {
				trigger = {
					house.house_unity_value >= 5
					NOT = { scope:duel_type = flag:unity }
				}
				save_scope_value_as = { name = spend_type value = flag:unity }
			}
			10 = {
				trigger = { NOT = { scope:duel_type = flag:stress } }
				save_scope_value_as = { name = spend_type value = flag:stress }
			}
			10 = {
				trigger = { NOT = { scope:duel_type = flag:associate } }
				save_scope_value_as = { name = spend_type value = flag:associate }
			}
		}
	}
}

scripted_effect merchant_scheme_advantage_upset_outcome_effect = {
	save_scope_value_as = { name = opinion value = $OPINION_LOSS$ }
	save_scope_value_as = { name = stress value = $STRESS_GAIN$ }
	save_scope_value_as = { name = unity value = $UNITY_LOSS$ }
	if = {
		limit = { scope:opinion != 0 }
		scope:portrait = {
			add_opinion = {
				target = root
				modifier = upset_opinion
				opinion = scope:opinion
			}
		}
	}
	scope:portrait = {
		if = {
			limit = { scope:stress != 0 }
			stress_and_fulfillment_impact = { base = scope:stress }
		}
	}
	scope:merchant.house = {
		if = {
			limit = { scope:unity != 0 }
			add_unity_value = {
				value = scope:unity
				character = root
				desc = gdp_merchant_scheme_advantage.desc
			}
		}
	}
}

scripted_effect gdp_merchant_scheme_advantage_add_opportunity_effect = {
	scope:merchant = {
		send_interface_toast = {
			type = event_toast_effect_good
			title = gdp_merchant_scheme_advantage_outcome
			right_icon = scope:trader
			custom_tooltip = {
				text = gdp_merchant_scheme_advantage_add_2_opportunities_tt
				scope:scheme = { change_opportunities = 2 }
			}
		}
	}
}

scripted_effect gdp_merchant_scheme_advantage_malus_outcome_effect = {
	scope:merchant = {
		send_interface_toast = {
			type = event_toast_effect_bad
			title = gdp_merchant_scheme_advantage_outcome
			switch = {
				trigger = scope:malus
				flag:potential = {
					scope:scheme = {
						add_scheme_modifier = { type = gdp_merchant_scheme_advantage_success_chance_potential_modifier }
					}
				}
				flag:growth = {
					scope:scheme = {
						add_scheme_modifier = { type = gdp_merchant_scheme_advantage_success_chance_growth_modifier }
					}
				}
				flag:length = {
					scope:scheme = {
						add_scheme_modifier = { type = gdp_merchant_scheme_advantage_success_chance_length_modifier }
					}
				}
				flag:opportunity = {
					custom_tooltip = {
						text = gdp_merchant_scheme_advantage_subtract_opportunity_tt
						scope:scheme = { change_opportunities = -1 }
					}
				}
				flag:quit = {
					scope:portrait = { move_to_pool = yes }
				}
				flag:competitiveness = {
					scope:market = {
						add_competitiveness_modifier_for_character = {
							character = scope:merchant
							modifier = gdp_merchant_scheme_advantage_market_competitiveness_modifier
						}
					}
				}
				flag:relation = {
					scope:merchant.house = {
						change_house_relation_effect = {
							HOUSE = scope:rival.house
							VALUE = house_relation_damage_minor_value 
							REASON = scheme_push_for_advantage
							CHAR = scope:merchant
							TARGET_CHAR = scope:rival
							TITLE = dummy_female
						}
					}
				}
			}
		}
	}
}

scripted_effect gdp_merchant_scheme_advantage_contract_failure_effect = {
	scope:merchant = {
		send_interface_toast = {
			type = event_toast_effect_bad
			title = gdp_merchant_scheme_advantage_outcome
			right_icon = scope:trader
			scope:task_contract = { complete_task_contract = failure_standard }
		}
	}
}

gdp_merchant_scheme_advantage.1000 = {
	type = character_event
	title = gdp_merchant_scheme_advantage.1000.t
	desc = {
		desc = gdp_merchant_scheme_advantage.1000.intro
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:rival }
				desc = gdp_merchant_scheme_advantage.1000.intro.rival
			}
			triggered_desc = {
				trigger = { exists = scope:partner }
				desc = gdp_merchant_scheme_advantage.1000.intro.partner
			}
			triggered_desc = {
				trigger = { exists = scope:agent }
				desc = gdp_merchant_scheme_advantage.1000.intro.agent
			}
			desc = gdp_merchant_scheme_advantage.1000.intro.trader
		}
		first_valid = {
			triggered_desc = {
				trigger = { scope:duel_type = flag:unity }
				desc = gdp_merchant_scheme_advantage.1000.desc.unity
			}
			triggered_desc = {
				trigger = { scope:duel_type = flag:stress }
				desc = gdp_merchant_scheme_advantage.1000.desc.stress
			}
			desc = gdp_merchant_scheme_advantage.1000.desc.associate
		}
	}
	theme = trade
	left_portrait = {
		character = root
		animation = arguing
		camera = camera_event_very_left
	}
	right_portrait = {
		character = scope:portrait
		animation = disbelief
		camera = camera_event_very_right
	}
	lower_right_portrait = scope:rival
	override_background = { reference = study }

	immediate = {
		merchant_scheme_advantage_save_scopes_effect = yes
	}

	option = { # Crack whip
		name = gdp_merchant_scheme_advantage.1000.a
		trigger = { scope:duel_type = flag:associate }
		flavor = gdp_merchant_scheme_advantage.1000.a.flavor
		scope:portrait = {
			duel = {
				skill = stewardship
				value = decent_skill_rating
				10 = {
					base_duel_compare_modifier = { MULTIPLIER = 3.5 VALUE = scope:duel_value }
					min = 5
					gdp_merchant_scheme_advantage_add_opportunity_effect = yes
				}
				10 = {
					base_duel_compare_modifier = { MULTIPLIER = -1 VALUE = scope:duel_value }
					min = 5
					gdp_merchant_scheme_advantage_malus_outcome_effect = yes
				}
				5 = {
					base_duel_compare_modifier = { MULTIPLIER = -3.5 VALUE = scope:duel_value }
					min = 5
					gdp_merchant_scheme_advantage_contract_failure_effect = yes
				}
			}
		}
		merchant_scheme_advantage_upset_outcome_effect = {
			OPINION_LOSS = -25
			STRESS_GAIN = medium_stress_gain
			UNITY_LOSS = major_unity_loss
		}
		stress_and_fulfillment_impact = {
			compassionate = minor_stress_impact_gain
			patient = minor_stress_impact_gain
		}
		# No ai_chance
	}

	option = { # Leverage House Unity
		name = gdp_merchant_scheme_advantage.1000.b
		trigger = { scope:duel_type = flag:unity }
		flavor = gdp_merchant_scheme_advantage.1000.b.flavor
		save_scope_value_as = {
			name = pseudo_duel_value
			value = {
				value = scope:merchant.house.house_unity_value
				subtract = 100
			}
		}
		random_list = {
			5 = {
				base_duel_compare_modifier = { MULTIPLIER = 3.5 VALUE = scope:pseudo_duel_value }
				min = 5
				gdp_merchant_scheme_advantage_add_opportunity_effect = yes
			}
			10 = {
				base_duel_compare_modifier = { MULTIPLIER = -1 VALUE = scope:pseudo_duel_value }
				min = 5
				gdp_merchant_scheme_advantage_malus_outcome_effect = yes
			}
			5 = {
				base_duel_compare_modifier = { MULTIPLIER = -3.5 VALUE = scope:pseudo_duel_value }
				min = 5
				gdp_merchant_scheme_advantage_contract_failure_effect = yes
			}
		}
		merchant_scheme_advantage_upset_outcome_effect = {
			OPINION_LOSS = 0
			STRESS_GAIN = 0
			UNITY_LOSS = massive_unity_loss
		}
		every_courtier = {
			custom = custom.every_associate
			add_opinion = {
				target = root
				modifier = upset_opinion
				opinion = -15
			}
		}
		# No ai_chance
	}

	option = { # Do *something* yourself
		name = gdp_merchant_scheme_advantage.1000.c
		trigger = { scope:duel_type = flag:stress }
		flavor = gdp_merchant_scheme_advantage.1000.c.flavor
		duel = {
			skill = stewardship
			value = decent_skill_rating
			10 = {
				base_duel_compare_modifier = { MULTIPLIER = 3.5 VALUE = scope:duel_value }
				min = 5
				gdp_merchant_scheme_advantage_add_opportunity_effect = yes
			}
			10 = {
				base_duel_compare_modifier = { MULTIPLIER = -1 VALUE = scope:duel_value }
				min = 5
				gdp_merchant_scheme_advantage_malus_outcome_effect = yes
			}
			5 = {
				base_duel_compare_modifier = { MULTIPLIER = -3.5 VALUE = scope:duel_value }
				min = 5
				gdp_merchant_scheme_advantage_contract_failure_effect = yes
			}
		}
		stress_and_fulfillment_impact = {
			base = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
		}
		# No ai_chance
	}

	option = { # Spend
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:spend_type = flag:associate }
						desc = gdp_merchant_scheme_advantage.1000.d.associate
					}
					triggered_desc = {
						trigger = { scope:spend_type = flag:unity }
						desc = gdp_merchant_scheme_advantage.1000.d.unity
					}
						desc = gdp_merchant_scheme_advantage.1000.d.stress
				}
			}
		}
		custom_tooltip = {
			text = gdp_merchant_scheme_advantage_add_1_opportunity_tt
			scope:scheme = { change_opportunities = 1 }
		}
		switch = {
			trigger = scope:spend_type
			flag:associate = {
				merchant_scheme_advantage_upset_outcome_effect = {
					OPINION_LOSS = -10
					STRESS_GAIN = minor_stress_impact_gain
					UNITY_LOSS = minor_unity_loss
				}
			}
			flag:unity = {
				stress_and_fulfillment_impact = { base = medium_stress_impact_gain }
				scope:merchant.house = {
					add_unity_value = {
						value = medium_unity_loss
						character = root
						desc = gdp_merchant_scheme_advantage.desc
					}
				}
			}
			flag:stress = {
				stress_and_fulfillment_impact = {
					base = miniscule_stress_impact_gain
					lazy = miniscule_stress_impact_gain
				}
			}
		}
		# No ai_chance
	}

	option = { # Ah forget it
		name = gdp_merchant_scheme_advantage.1000.e
		stress_and_fulfillment_impact = {
			impatient = minor_stress_impact_gain
		}
		# No ai_chance
	}
}

scripted_effect gdp_merchant_scheme_advantage_recruit_outcome_effect = {
	custom_tooltip = {
		text = gdp_merchant_scheme_advantage_add_1_opportunity_tt
		scope:scheme = { change_opportunities = 1 }
	}
	scope:merchant = {
		add_courtier = scope:recruit
		hidden_effect = {
			reverse_add_opinion = {
				target = scope:recruit
				modifier = recruited_me_opinion
				opinion = 50
			}
		}
		house = {
			add_unity_value = {
				value = minor_unity_loss
				character = root
				desc = gdp_merchant_scheme_advantage.desc
			}
		}
		if = {
			limit = { exists = scope:recruit_liege.house }
			reverse_add_opinion = {
				target = scope:recruit_liege
				modifier = upset_opinion
				opinion = -15
			}
			house = {
				change_house_relation_effect = {
					HOUSE = scope:recruit_liege.house
					VALUE = house_relation_damage_minor_value 
					REASON = scheme_push_for_advantage
					CHAR = scope:merchant
					TARGET_CHAR = scope:recruit_liege
					TITLE = dummy_female
				}
			}
			scope:recruit_liege.house = {
				add_unity_value = {
					value = minor_unity_loss
					character = root
					desc = gdp_merchant_scheme_advantage.desc
				}
			}
		}
	}
}

gdp_merchant_scheme_advantage.1010 = {
	type = character_event
	title = gdp_merchant_scheme_advantage.1010.t
	desc = {
		desc = gdp_merchant_scheme_advantage.1010.intro
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:recruit_liege }
				desc = gdp_merchant_scheme_advantage.1010.desc.liege
			}
			desc = gdp_merchant_scheme_advantage.1010.desc.recruit
		}
	}
	theme = trade
	left_portrait = {
		character = root
		animation = worry
		camera = camera_event_very_left
	}
	right_portrait = {
		character = scope:trader
		animation = debating
		camera = camera_event_very_right
	}
	lower_center_portrait = scope:recruit
	lower_right_portrait = scope:recruit_liege
	override_background = { reference = study }
	cooldown = { years = 5 }

	immediate = {
		merchant_scheme_advantage_save_scopes_effect = yes
		domicile.domicile_location.barony.trade_market = {
			every_character_in_market = {
				limit = {
					government_has_flag = government_is_merchant
					NOT = { this = root }
				}
				add_to_list = potential_rivals
			}
		}
		every_in_list = {
			list = potential_rivals
			every_courtier = { add_to_list = potential_rival_recruits }
		}
		hidden_effect = {
			random = {
				chance = 50
				random_in_list = {
					list = potential_rival_recruits
					limit = {
						is_available_quick = {
							adult = yes
							imprisoned = no
							incapable = no
						}
						NOR = {
							house = liege.house
							is_married = yes
							opinion = {
								target = liege
								value >= 50
							}
							has_any_good_relationship_with_character_trigger = { CHARACTER = liege }
						}
					}
					save_scope_as = recruit
				}
			}
		}
		if = {
			limit = {
				NOT = { exists = scope:recruit }
			}
			hidden_effect = {
				random_list = {
					10 = {
						create_character = {
							template = bad_merchant_template
							dynasty = none
							faith = location.faith
							culture = location.culture
							location = root.location
							gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
							save_scope_as = recruit
						}
					}
					10 = {
						create_character = {
							template = pool_repopulate_local_flavor
							dynasty = none
							location = root.location
							gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
							save_scope_as = recruit
						}
					}
				}
			}
		}
		scope:recruit.liege = { save_scope_as = recruit_liege }
	}

	option = { # Duel
		name = gdp_merchant_scheme_advantage.1010.a
		flavor = gdp_merchant_scheme_advantage.1010.a.flavor
		duel = {
			skill = diplomacy
			value = decent_skill_rating
			10 = {
				base_duel_compare_modifier = { MULTIPLIER = 3.5 VALUE = scope:duel_value }
				min = 10
				scope:merchant = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = gdp_merchant_scheme_advantage_outcome
						right_icon = scope:trader
						pay_short_term_gold = {
							gold = minor_gold_value
							target = scope:recruit
						}
						gdp_merchant_scheme_advantage_recruit_outcome_effect = yes
					}
				}
			}
			10 = {
				base_duel_compare_modifier = { MULTIPLIER = -3.5 VALUE = scope:duel_value }
				min = 10
				scope:merchant = {
					send_interface_toast = {
						type = event_toast_effect_bad
						title = gdp_merchant_scheme_advantage_outcome
						right_icon = scope:trader
						add_prestige = miniscule_prestige_loss
					}
				}
			}
		}
		# No ai_chance
	}

	option = { # Spend
		name = gdp_merchant_scheme_advantage.1010.b
		flavor = gdp_merchant_scheme_advantage.1010.b.flavor
		scope:merchant = {
			pay_short_term_gold = {
				gold = medium_gold_value
				target = scope:recruit
			}
		}
		gdp_merchant_scheme_advantage_recruit_outcome_effect = yes
		stress_and_fulfillment_impact = {
			greedy = minor_stress_impact_gain
		}
		# No ai_chance
	}

	option = { # Ah forget it
		name = gdp_merchant_scheme_advantage.1010.e
		stress_and_fulfillment_impact = {
			impatient = minor_stress_impact_gain
		}
		# No ai_chance
	}
}

gdp_merchant_scheme_advantage.1020 = {
	type = character_event
	title = gdp_merchant_scheme_advantage.1020.t
	desc = gdp_merchant_scheme_advantage.1020.desc
	theme = trade
	left_portrait = {
		character = root
		animation = war_over_win
		camera = camera_event_very_left
	}
	right_portrait = {
		character = scope:trader
		animation = thinking
		camera = camera_event_very_right
	}
	lower_center_portrait = scope:patron
	override_background = { reference = study }
	cooldown = { years = 5 }

	trigger = {
		merchant_has_usable_charter_trigger = {
			ONLY_EQUIPPED = no
			INCLUDE_FORGERIES = no
		}
	}

	immediate = {
		merchant_scheme_advantage_save_scopes_effect = yes
		random_character_artifact = {
			limit = {
				save_temporary_scope_as = charter_temp
				var:charter_patron ?= { save_temporary_scope_as = patron_temp } # is charter
				root = { can_benefit_from_artifact = scope:charter_temp }
			}
			var:charter_patron = { save_scope_as = patron }
			save_scope_as = charter
		}
	}

	option = { # Duel
		name = gdp_merchant_scheme_advantage.1020.a
		duel = {
			skill = diplomacy
			target = scope:patron
			10 = {
				base_duel_compare_modifier = { MULTIPLIER = 3.5 VALUE = scope:duel_value }
				min = 5
				gdp_merchant_scheme_advantage_add_opportunity_effect = yes
			}
			10 = {
				base_duel_compare_modifier = { MULTIPLIER = -1 VALUE = scope:duel_value }
				min = 5
				add_prestige = miniscule_prestige_loss
				reverse_add_opinion = {
					target = scope:patron
					modifier = angry_opinion
					opinion = -15
				}
				merchant_charter_abuse_counter_effect = {
					PATRON = scope:patron
					MERCHANT = scope:merchant
				}
			}
			5 = {
				base_duel_compare_modifier = { MULTIPLIER = -3.5 VALUE = scope:duel_value }
				min = 5
				reverse_add_opinion = {
					target = scope:patron
					modifier = angry_opinion
					opinion = -25
				}
				merchant_charter_destroy_held_charters_effect = {
					MERCHANT = scope:merchant
					PATRON = scope:patron
				}
			}
		}
		# No ai_chance
	}

	option = { # Spend
		name = gdp_merchant_scheme_advantage.1020.b
		custom_tooltip = {
			text = gdp_merchant_scheme_advantage_add_1_opportunity_tt
			scope:scheme = { change_opportunities = 1 }
		}
		reverse_add_opinion = {
			target = scope:patron
			modifier = angry_opinion
			opinion = -10
		}
		merchant_charter_abuse_counter_effect = {
			PATRON = scope:patron
			MERCHANT = scope:merchant
		}
		stress_and_fulfillment_impact = {
			craven = minor_stress_impact_gain
		}
		# No ai_chance
	}

	option = { # Ah forget it
		name = gdp_merchant_scheme_advantage.1020.e
		stress_and_fulfillment_impact = {
			impatient = minor_stress_impact_gain
		}
		# No ai_chance
	}
}

gdp_merchant_scheme_advantage.9999 = { # suppressor
	type = character_event
	hidden = yes
	orphan = yes
	
	trigger = {
		exists = var:push_for_advantage_cooldown
	}
}
