# See 00_README.txt in common/tradable_actions for documentation

trade_action_pledge_loyalty = {
	fire_and_forget = no

	potential = {
		has_overlord_dlc = yes
		has_overlord = FROM
		FROM = {
			is_country_type = default
		}
		NOT = {
			is_action_active = {
				action = trade_action_pledge_loyalty
				with_country = FROM
			}
		}
	}

	active = {
		has_overlord = FROM
	}

	on_traded_effect = {
		if = {
			limit = {
				opinion = {
					who = FROM
					value < 150
				}
			}
			add_modifier = {
				modifier = pledge_loyalty_1
			}
		}
		else = {
			add_modifier = {
				modifier = pledge_loyalty_2
			}
		}
	}

	on_deal_ended_sender_effect = {
		switch = {
			trigger = has_modifier
			pledge_loyalty_1 = { remove_modifier = pledge_loyalty_1 }
			pledge_loyalty_2 = { remove_modifier = pledge_loyalty_2 }
		}
	}

	ai_weight = {
		weight = 2
	}
}

trade_action_share_empire_data = {
	fire_and_forget = yes

	potential = {
		has_overlord_dlc = yes
		FROM = {
			is_country_type = default
		}
		OR = {
			AND = {
				FROM = { has_overlord = ROOT }
				intel = {
					who = FROM
					value <= 80
				}
			}
			AND = {
				has_overlord = FROM
				intel = {
					who = FROM
					value <= 40
				}
			}
		}
	}

	on_traded_effect = {
		FROM = {
			add_intel = {
				amount = 20
				who = ROOT
			}
		}
	}

	ai_weight = {
		weight = 10

		modifier = {
			factor = 2
			FROM = { has_overlord = PREV }
		}
	}
}

trade_action_bulwark_prefabs = {
	fire_and_forget = no

	potential = {
		has_overlord_dlc = yes
		has_overlord = FROM
		NOT = {
			is_action_active = {
				action = trade_action_bulwark_prefabs
				with_country = FROM
			}
		}
		is_specialist_subject_type = { TYPE = bulwark }
		FROM = { NOT = { has_modifier = bulwark_prefabs_overlord } }
	}

	active = {
		has_overlord = FROM
		is_specialist_subject_type = { TYPE = bulwark }
	}

	on_traded_effect = {
		if = {
			limit = { is_specialist_subject_tier = { TYPE = bulwark TIER = 3 } }
			ROOT = {
				add_modifier = {
					modifier = bulwark_prefabs_subject
					multiplier = 3
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = bulwark_prefabs_overlord
					multiplier = 3
				}
			}
		}
		else_if = {
			limit = { is_specialist_subject_tier = { TYPE = bulwark TIER = 2 } }
			ROOT = {
				add_modifier = {
					modifier = bulwark_prefabs_subject
					multiplier = 2
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = bulwark_prefabs_overlord
					multiplier = 2
				}
			}
		}
		else = {
			ROOT = {
				add_modifier = {
					modifier = bulwark_prefabs_subject
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = bulwark_prefabs_overlord
				}
			}
		}
	}

	on_deal_ended_sender_effect = {
		remove_modifier = bulwark_prefabs_subject
	}

	on_deal_ended_recipient_effect = {
		remove_modifier = bulwark_prefabs_overlord
	}

	ai_weight = {
		weight = 1

		modifier = {
			weight = 8
			OR = {
				AND = {
					is_specialist_subject_tier = {
						TYPE = bulwark
						TIER = 3
					}
					has_monthly_income = {
						resource = alloys
						value < 6
					}
				}
				AND = {
					is_specialist_subject_tier = {
						TYPE = bulwark
						TIER = 2
					}
					has_monthly_income = {
						resource = alloys
						value < 4
					}
				}
				has_monthly_income = {
					resource = alloys
					value < 2
				}
			}
		}

		modifier = {
			weight = 1.5
			OR = {
				AND = {
					is_specialist_subject_tier = {
						TYPE = bulwark
						TIER = 3
					}
					has_monthly_income = {
						resource = alloys
						value < 12
					}
				}
				AND = {
					is_specialist_subject_tier = {
						TYPE = bulwark
						TIER = 2
					}
					has_monthly_income = {
						resource = alloys
						value < 8
					}
				}
				has_monthly_income = {
					resource = alloys
					value < 4
				}
			}
		}
	}
}

trade_action_bulwark_technology = {
	fire_and_forget = yes

	potential = {
		has_overlord_dlc = yes
		has_overlord = FROM
		is_specialist_subject_type = { TYPE = bulwark }
		FROM = {
			inverted_switch = {
				trigger = has_technology
				tech_ftl_inhibitor = { ROOT = { has_technology = tech_ftl_inhibitor } }
				tech_planetary_shield_generator = { ROOT = { has_technology = tech_planetary_shield_generator } }
				tech_starbase_3 = { ROOT = { has_technology = tech_starbase_3 } }
				tech_modular_engineering = { ROOT = { has_technology = tech_modular_engineering } }
				tech_starbase_4 = { ROOT = { has_technology = tech_starbase_4 } }
				tech_starbase_5 = { ROOT = { has_technology = tech_starbase_5 } }
			}
			NOR = {
				has_modifier = bulwark_technology_physics_modifier
				has_modifier = bulwark_technology_engineering_modifier
			}
		}
	}

	on_traded_effect = {
		FROM = {
			inverted_switch = {
				trigger = has_technology
				tech_ftl_inhibitor = {
					add_research_option = tech_ftl_inhibitor
					ROOT = {
						add_modifier = {
							modifier = bulwark_technology_physics_modifier
							days = 360
						}
					}
				}
				tech_planetary_shield_generator = {
					add_research_option = tech_planetary_shield_generator
					ROOT = {
						add_modifier = {
							modifier = bulwark_technology_physics_modifier
							days = 360
						}
					}
				}
				tech_starbase_3 = {
					add_research_option = tech_starbase_3
					ROOT = {
						add_modifier = {
							modifier = bulwark_technology_engineering_modifier
							days = 360
						}
					}
				}
				tech_modular_engineering = {
					add_research_option = tech_modular_engineering
					ROOT = {
						add_modifier = {
							modifier = bulwark_technology_engineering_modifier
							days = 360
						}
					}
				}
				tech_starbase_4 = {
					add_research_option = tech_starbase_4
					ROOT = {
						add_modifier = {
							modifier = bulwark_technology_engineering_modifier
							days = 360
						}
					}
				}
				tech_starbase_5 = {
					add_research_option = tech_starbase_5
					ROOT = {
						add_modifier = {
							modifier = bulwark_technology_engineering_modifier
							days = 360
						}
					}
				}
			}
		}
	}

	ai_weight = {
		weight = 25
	}
}

trade_action_scholarium_hypotheses = {
	fire_and_forget = no

	potential = {
		has_overlord_dlc = yes
		has_overlord = FROM
		NOT = {
			is_action_active = {
				action = trade_action_scholarium_hypotheses
				with_country = FROM
			}
		}
		is_specialist_subject_type = { TYPE = scholarium }
		FROM = { NOT = { has_modifier = scholarium_hypotheses_overlord } }
	}

	active = {
		has_overlord = FROM
		is_specialist_subject_type = { TYPE = scholarium }
	}

	on_traded_effect = {
		ROOT = {
			add_modifier = {
				modifier = scholarium_hypotheses_subject
			}
		}
		custom_tooltip = double_newline
		FROM = {
			add_modifier = {
				modifier = scholarium_hypotheses_overlord
			}
		}
	}

	on_deal_ended_sender_effect = {
		remove_modifier = scholarium_hypotheses_subject
	}

	on_deal_ended_recipient_effect = {
		remove_modifier = scholarium_hypotheses_overlord
	}

	ai_weight = {
		weight = 4
	}
}

trade_action_scholarium_processors = {
	fire_and_forget = no

	potential = {
		has_overlord_dlc = yes
		has_overlord = FROM
		NOT = {
			is_action_active = {
				action = trade_action_scholarium_processors
				with_country = FROM
			}
		}
		is_specialist_subject_type = { TYPE = scholarium }
		FROM = {
			NOR = {
				has_modifier = scholarium_processors_overlord_1
				has_modifier = scholarium_processors_overlord_2
				has_modifier = scholarium_processors_overlord_3
			}
		}
	}

	active = {
		has_overlord = FROM
		is_specialist_subject_type = { TYPE = scholarium }
	}

	on_traded_effect = {
		if = {
			limit = {
				is_specialist_subject_tier = { TYPE = scholarium TIER = 3 }
			}
			ROOT = {
				add_modifier = {
					modifier = scholarium_processors_subject_3
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = scholarium_processors_overlord_3
				}
			}
		}
		else_if = {
			limit = {
				is_specialist_subject_tier = { TYPE = scholarium TIER = 2 }
			}
			ROOT = {
				add_modifier = {
					modifier = scholarium_processors_subject_2
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = scholarium_processors_overlord_2
				}
			}
		}
		else = {
			ROOT = {
				add_modifier = {
					modifier = scholarium_processors_subject_1
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = scholarium_processors_overlord_1
				}
			}
		}
	}

	on_deal_ended_sender_effect = {
		switch = {
			trigger = has_modifier
			scholarium_processors_subject_1 = { remove_modifier = scholarium_processors_subject_1 }
			scholarium_processors_subject_2 = { remove_modifier = scholarium_processors_subject_2 }
			scholarium_processors_subject_3 = { remove_modifier = scholarium_processors_subject_3 }
		}
	}

	on_deal_ended_recipient_effect = {
		switch = {
			trigger = has_modifier
			scholarium_processors_overlord_1 = { remove_modifier = scholarium_processors_overlord_1 }
			scholarium_processors_overlord_2 = { remove_modifier = scholarium_processors_overlord_2 }
			scholarium_processors_overlord_3 = { remove_modifier = scholarium_processors_overlord_3 }
		}
	}

	ai_weight = {
		weight = 2.5
	}
}

trade_action_prospectorium_techniques = {
	fire_and_forget = no

	potential = {
		has_overlord_dlc = yes
		has_overlord = FROM
		NOT = {
			is_action_active = {
				action = trade_action_prospectorium_techniques
				with_country = FROM
			}
		}
		is_specialist_subject_type = { TYPE = prospectorium }
		FROM = {
			NOR = {
				has_modifier = prospectorium_techniques_overlord_1
				has_modifier = prospectorium_techniques_overlord_2
				has_modifier = prospectorium_techniques_overlord_3
			}
		}
	}

	active = {
		has_overlord = FROM
		is_specialist_subject_type = { TYPE = prospectorium }
	}

	on_traded_effect = {
		if = {
			limit = {
				is_specialist_subject_tier = { TYPE = prospectorium TIER = 3 }
			}
			ROOT = {
				add_modifier = {
					modifier = prospectorium_techniques_subject_3
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = prospectorium_techniques_overlord_3
				}
			}
		}
		else_if = {
			limit = {
				is_specialist_subject_tier = { TYPE = prospectorium TIER = 2 }
			}
			ROOT = {
				add_modifier = {
					modifier = prospectorium_techniques_subject_2
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = prospectorium_techniques_overlord_2
				}
			}
		}
		else = {
			ROOT = {
				add_modifier = {
					modifier = prospectorium_techniques_subject_1
				}
			}
			custom_tooltip = double_newline
			FROM = {
				add_modifier = {
					modifier = prospectorium_techniques_overlord_1
				}
			}
		}
	}

	on_deal_ended_sender_effect = {
		switch = {
			trigger = has_modifier
			prospectorium_techniques_subject_1 = { remove_modifier = prospectorium_techniques_subject_1 }
			prospectorium_techniques_subject_2 = { remove_modifier = prospectorium_techniques_subject_2 }
			prospectorium_techniques_subject_3 = { remove_modifier = prospectorium_techniques_subject_3 }
		}
	}

	on_deal_ended_recipient_effect = {
		switch = {
			trigger = has_modifier
			prospectorium_techniques_overlord_1 = { remove_modifier = prospectorium_techniques_overlord_1 }
			prospectorium_techniques_overlord_2 = { remove_modifier = prospectorium_techniques_overlord_2 }
			prospectorium_techniques_overlord_3 = { remove_modifier = prospectorium_techniques_overlord_3 }
		}
	}

	ai_weight = {
		weight = 2.5
	}
}

trade_action_prospectorium_technology = {
	fire_and_forget = yes

	potential = {
		has_overlord_dlc = yes
		has_overlord = FROM
		is_specialist_subject_type = { TYPE = prospectorium }
		FROM = {
			inverted_switch = {
				trigger = has_technology
				tech_mine_volatile_motes = {
					ROOT = {
						has_technology = tech_mine_volatile_motes
						has_resource = {
							type = volatile_motes
							amount = 100
						}
					}
				}
				tech_mine_rare_crystals = {
					ROOT = {
						has_technology = tech_mine_rare_crystals
						has_resource = {
							type = rare_crystals
							amount = 100
						}
					}
				}
				tech_mine_exotic_gases = {
					ROOT = {
						has_technology = tech_mine_exotic_gases
						has_resource = {
							type = exotic_gases
							amount = 100
						}
					}
				}
				tech_mine_zro = {
					ROOT = {
						has_technology = tech_mine_zro
						has_resource = {
							type = sr_zro
							amount = 100
						}
					}
				}
				tech_mine_living_metal = {
					ROOT = {
						has_technology = tech_mine_living_metal
						has_resource = {
							type = sr_living_metal
							amount = 100
						}
					}
				}
				tech_mine_dark_matter = {
					ROOT = {
						has_technology = tech_mine_dark_matter
						has_resource = {
							type = sr_dark_matter
							amount = 100
						}
					}
				}
			}
			NOR = {
				has_modifier = prospectorium_technology_physics_modifier
				has_modifier = prospectorium_technology_society_modifier
				has_modifier = prospectorium_technology_engineering_modifier
			}
		}
	}

	on_traded_effect = {
		FROM = {
			inverted_switch = {
				trigger = has_technology
				tech_mine_volatile_motes = {
					add_research_option = tech_mine_volatile_motes
					add_resource = {
						volatile_motes = 100
					}
					ROOT = {
						add_modifier = {
							modifier = prospectorium_technology_physics_modifier
							days = 360
						}
						add_resource = {
							volatile_motes = -100
						}
					}
				}
				tech_mine_rare_crystals = {
					add_research_option = tech_mine_rare_crystals
					add_resource = {
						rare_crystals = 100
					}
					ROOT = {
						add_modifier = {
							modifier = prospectorium_technology_engineering_modifier
							days = 360
						}
						add_resource = {
							rare_crystals = -100
						}
					}
				}
				tech_mine_exotic_gases = {
					add_research_option = tech_mine_exotic_gases
					add_resource = {
						exotic_gases = 100
					}
					ROOT = {
						add_modifier = {
							modifier = prospectorium_technology_engineering_modifier
							days = 360
						}
						add_resource = {
							exotic_gases = -100
						}
					}
				}
				tech_mine_zro = {
					add_research_option = tech_mine_zro
					add_resource = {
						sr_zro = 100
					}
					ROOT = {
						add_modifier = {
							modifier = prospectorium_technology_society_modifier
							days = 360
						}
						add_resource = {
							sr_zro = -100
						}
					}
				}
				tech_mine_living_metal = {
					add_research_option = tech_mine_living_metal
					add_resource = {
						sr_living_metal = 100
					}
					ROOT = {
						add_modifier = {
							modifier = prospectorium_technology_engineering_modifier
							days = 360
						}
						add_resource = {
							sr_living_metal = -100
						}
					}
				}
				tech_mine_dark_matter = {
					add_research_option = tech_mine_dark_matter
					add_resource = {
						sr_dark_matter = 100
					}
					ROOT = {
						add_modifier = {
							modifier = prospectorium_technology_physics_modifier
							days = 360
						}
						add_resource = {
							sr_dark_matter = -100
						}
					}
				}
			}
		}
	}

	ai_weight = {
		weight = 25
	}
}

# Disabled for 4.5, To re-enable, uncomment this definition and restore these:
# scripted_triggers/09_scripted_triggers_nomads.txt - has_harvesting_rights_trigger: uncomment the body, delete always = no and exists = $NOMAD$
# script_values/08_script_values_nomads.txt - harvesting_agreement_owner_share_value: uncomment the body, delete base = 0
# inline_scripts/scripted_actions/harvest_resources_from_deposit.txt - uncomment the else_if branch
# inline_scripts/scripted_actions/harvest_resources_from_planet_class.txt - uncomment both if branches, then re-indent each else body and its wrapper
# inline_scripts/scripted_actions/harvest_automation_options.txt - uncomment the harvesting_rights option
# inline_scripts/scripted_actions/ai_harvest_automation_modifiers.txt - uncomment two NOT clauses and two whole modifiers
# inline_scripts/contracts/HarvestResource.txt - uncomment the NOT block in potential_issuer
# # SCOPE = settled empire granting exclusive harvesting rights in its space. FROM = Nomad receiving them.
# trade_action_harvesting_agreement = {
# 	fire_and_forget = no

# 	has_negotiable_value = yes
# 	default_negotiable_value = 0.50
# 	negotiable_value_step_size = 0.05
# 	negotiable_value_step_size_ctrl = 0.10
# 	negotiable_value_step_size_shift = 0.15
# 	negotiable_value_step_size_ctrl_shift = 0.25
# 	negotiable_value_is_percentage = yes
# 	# A share at or below 0.25 buys nothing, so a 10 year agreement needs 45% before diplomacy is counted.
# 	negotiable_value_ai_baseline = 0.25
# 	# 50 = every 5% of share above the baseline buys 2.5 years of accepted length.
# 	negotiable_value_ai_years_per_unit = 50

# 	potential = {
# 		has_nomads_dlc = yes
# 		is_nomadic = no
# 		is_country_type = default
# 		NOT = { is_at_war_with = from }
# 		# Block a subject barred from diplomacy. is_subject stops this matching an overlord's own agreement.
# 		NAND = {
# 			is_subject = yes
# 			any_agreement = {
# 				has_term_value = {
# 					term = subject_diplomacy
# 					value = subject_can_not_do_diplomacy
# 				}
# 			}
# 		}
# 		FROM = {
# 			is_nomadic = yes
# 			is_country_type = default
# 			NAND = {
# 				is_subject = yes
# 				any_agreement = {
# 					has_term_value = {
# 						term = subject_diplomacy
# 						value = subject_can_not_do_diplomacy
# 					}
# 				}
# 			}
# 		}
# 	}

# 	# Exclusive: one harvesting agreement per host.
# 	possible = {
# 		custom_tooltip = {
# 			fail_text = trade_action_harvesting_agreement_exclusive_fail_tt
# 			OR = {
# 				NOT = {
# 					is_action_active = {
# 						action = trade_action_harvesting_agreement
# 						with_country = any
# 					}
# 				}
# 				# Renegotiating with the current holder is not a second grant
# 				is_action_active = {
# 					action = trade_action_harvesting_agreement
# 					with_country = FROM
# 				}
# 			}
# 		}
# 	}

# 	active = {
# 		is_nomadic = no
# 		FROM = {
# 			is_nomadic = yes
# 		}
# 		NOT = {
# 			is_at_war_with = FROM
# 		}
# 	}

# 	on_traded_effect = {
# 		custom_tooltip = trade_action_harvesting_agreement_rights_tt
# 		custom_tooltip = trade_action_harvesting_agreement_share_tt
# 		custom_tooltip = trade_action_harvesting_agreement_no_contracts_tt
# 	}

# 	# Toast both parties when the agreement ends. FROM may be invalid if a country died.
# 	on_deal_ended_sender_effect = {
# 		if = {
# 			limit = {
# 				exists = from
# 			}
# 			create_message = {
# 				type = MESSAGE_HARVESTING_AGREEMENT_ENDED
# 				localization = MESSAGE_HARVESTING_AGREEMENT_ENDED_DESC
# 				days = @toast_message_days
# 				variable = {
# 					type = name
# 					localization = COUNTRY
# 					scope = from
# 				}
# 			}
# 		}
# 	}

# 	on_deal_ended_recipient_effect = {
# 		if = {
# 			limit = {
# 				exists = from
# 			}
# 			create_message = {
# 				type = MESSAGE_HARVESTING_AGREEMENT_ENDED
# 				localization = MESSAGE_HARVESTING_AGREEMENT_ENDED_DESC
# 				days = @toast_message_days
# 				variable = {
# 					type = name
# 					localization = COUNTRY
# 					scope = from
# 				}
# 			}
# 		}
# 	}

# 	# Magnitudes are a tuning item for the AI. A share above the baseline makes this term negative, so relationship
# 	# modifiers belong in ai_acceptance where they cannot invert their own sign.
# 	ai_weight = {
# 		# Cost per year of length the share has not paid for.
# 		weight = 10
# 	}

# 	# A flat bonus, applied outside the trade willingness multiplier. Twenty five points is worth one step of
# 	# the share spinner at the tuning above, at full trade willingness.
# 	ai_acceptance = {
# 		modifier = {
# 			add = 10
# 			has_embassy = from
# 			desc = HARVESTING_AGREEMENT_EMBASSY
# 		}
# 		modifier = {
# 			add = 15
# 			has_active_tradition = tr_diplomacy_diplomatic_networking
# 			desc = HARVESTING_AGREEMENT_DIPLOMATIC_NETWORKING
# 		}
# 		modifier = {
# 			add = 25
# 			opinion_level = {
# 				who = from
# 				level >= good
# 			}
# 			desc = HARVESTING_AGREEMENT_GOOD_OPINION
# 		}
# 		modifier = {
# 			add = 25
# 			trust = {
# 				who = from
# 				value >= 25
# 			}
# 			desc = HARVESTING_AGREEMENT_TRUST
# 		}
# 		modifier = {
# 			add = 50
# 			from = {
# 				has_country_flag = recently_completed_harvest_contract@root
# 			}
# 			desc = HARVESTING_AGREEMENT_RECENT_CONTRACT
# 		}
# 		modifier = {
# 			subtract = 100
# 			is_rival = from
# 			desc = HARVESTING_AGREEMENT_RIVAL
# 		}
# 	}
# }
