﻿# OVERRIDE of the vanilla file common/on_action/barter_on_actions.txt (CK3 1.20.1.1), same file name.
# Pilgrims and Preachers: Pilgrims missionaries are engine barter armies. Every vanilla block below is
# unchanged and runs in the `else` branch; the `if` branch (barterer has the Pilgrims camp purpose law
# ppr_camp_purpose_pilgrims) replaces the barter messages with preaching ones:
# - completion: no message to the preacher (ppr_on_preaching_completion, appended from
#   common/on_action/ppr_preaching_on_actions.txt, sends its own toast); the county holder gets
#   ppr_msg_preached_in_lands and ppr_blessed_my_lands_opinion instead of bartered_in_my_lands_opinion;
#   recently_bartered_modifier is still applied (the engine reads it to block a new mission there).
# - loot delivered: the alms are paid by ppr_missionaries_home_effect at the camp, not here.
# - defeat: ppr_msg_missionaries_alms_lost / ppr_msg_missionaries_alms_seized.
# The mod's `on_actions = { ppr_on_preaching_completion }` append still merges with this file.

### Bartering On-Actions

# Called when a barter action starts
# root is the bartering army
# scope:barterer is the person owning the bartering army
# scope:barony is the barony title that was bartered with
# scope:county is the county title for the barony
on_barter_action_start = {
	effect = {}
}

# Called weekly during a bartering action
# root is the bartering army
# scope:barterer is the person owning the bartering army
# scope:barony is the barony title that was bartered with
# scope:county is the county title for the barony
on_barter_action_weekly = {
	effect = {}
}

# Called when a barter action completes
# root is the bartering army
# scope:barterer is the person owning the bartering army
# scope:barony is the barony title that was bartered with
# scope:county is the county title for the barony
# scope:value is the amount of loot the army gained
on_barter_action_completion = {
	effect = {
		if = {
			# Pilgrims and Preachers: a sermon, not a barter.
			limit = {
				scope:barterer = { has_realm_law = ppr_camp_purpose_pilgrims }
			}
			scope:county.holder = {
				send_interface_message = {
					type = ppr_msg_preached_in_lands
					title = ppr_msg_preached_in_lands_title
					desc = ppr_msg_preached_in_lands_desc
					left_icon = scope:barterer
					right_icon = scope:barony

					# Required: the engine uses this modifier to block another mission in the holding.
					scope:barony.title_province = {
						add_province_modifier = {
							modifier = recently_bartered_modifier
							years = 5
						}
					}
					if = {
						limit = {
							scope:barterer != scope:barony.county.holder
						}
						add_opinion = {
							target = scope:barterer
							modifier = ppr_blessed_my_lands_opinion
						}
					}
				}
			}
		}
		else = {
			scope:barterer = {
				send_interface_message = {
					type = msg_barter_completed
					title = msg_barter_completed_barterer_title
					desc = msg_barter_completed_barterer_desc
					left_icon = scope:county.holder
					right_icon = scope:barony

					show_as_tooltip = {
						add_loot = scope:value
						scope:barony.title_province = {
							add_province_modifier = {
								modifier = recently_bartered_modifier
								years = 5
							}
						}
					}
					if = {
						limit = {
							scope:barterer != scope:barony.county.holder
						}
						reverse_add_opinion = {
							target = scope:barony.county.holder
							modifier = bartered_in_my_lands_opinion
						}
					}
				}
			}

			scope:county.holder = {
				send_interface_message = {
					type = msg_barter_completed
					title = msg_barter_completed_holder_title
					desc = msg_barter_completed_holder_desc
					left_icon = scope:barterer
					right_icon = scope:barony

					scope:barony.title_province = {
						add_province_modifier = {
							modifier = recently_bartered_modifier
							years = 5
						}
					}
				}
			}
		}
	}
}

# Called when a bartering army returns to their own territory and deposits loot
# root is the bartering army
# scope:barterer is the person owning the bartering army
# scope:barter_loot is the loot that you gained from the bartering mission
# Loot is applied to scope:barterer immediately before on-action is fired
on_barter_loot_delivered = {
	effect = {
		if = {
			# Pilgrims and Preachers: what the sermons earned (gold or piety, kept on the army) is
			# normally paid by ppr_missionaries_home_effect back in the camp's county. Should the
			# engine ever deliver the loot itself, pay it the same way (once: the carried values are
			# cleared when paid), with no vanilla barter message.
			limit = {
				scope:barterer = { has_realm_law = ppr_camp_purpose_pilgrims }
			}
			save_scope_as = army
			scope:barterer = {
				# The engine's purse is not the missionaries' purse: take back what it just paid.
				add_gold = {
					value = scope:barter_loot
					multiply = -1
				}
				ppr_missionaries_home_effect = yes
			}
		}
		else = {
			scope:barterer = {
				send_interface_message = {
					type = msg_barter_loot_delivered
					title = msg_barter_loot_delivered_title
					desc = msg_barter_loot_delivered_desc
					left_icon = scope:barterer
					scope:barterer = { add_gold = scope:barter_loot }
				}
			}
		}
	}
}

# Called when a bartering army is defeated in battle and gets its loot (if any) stolen
# Use root.barter_loot to see how much they're carrying
# root is the bartering army
# scope:barterer is the person owning the bartering army
# scope:receiver is the person getting the barter loot
# scope:barter_loot is how much loot they're carrying
on_defeat_barter_army = {
	effect = {
		if = {
			# Pilgrims and Preachers: missionaries robbed of their alms. Their own purse (army variables,
			# see ppr_alms_carried_value) is lost; the engine's barter purse goes to the winner as usual.
			limit = {
				scope:barterer = { has_realm_law = ppr_camp_purpose_pilgrims }
			}
			save_scope_as = ppr_purse_army
			scope:barterer = {
				save_scope_value_as = {
					name = ppr_alms_lost
					value = ppr_alms_carried_value
				}
			}
			remove_variable ?= ppr_gold_carried
			remove_variable ?= ppr_piety_carried
			scope:barterer = { ppr_refresh_missionary_purse_effect = yes }
			scope:barterer = {
				send_interface_message = {
					type = ppr_msg_missionaries_alms_lost
					title = ppr_msg_missionaries_alms_lost_title
					desc = ppr_msg_missionaries_alms_lost_desc
					left_icon = scope:barterer
					right_icon = scope:receiver
				}
			}
			scope:receiver ?= {
				send_interface_message = {
					type = ppr_msg_missionaries_alms_seized
					title = ppr_msg_missionaries_alms_seized_title
					desc = ppr_msg_missionaries_alms_seized_desc
					left_icon = scope:receiver
					right_icon = scope:barterer
				}
			}
		}
		else = {
			scope:barterer = {
				send_interface_message = {
					type = msg_barter_loot_lost
					title = msg_barter_loot_lost_title
					desc = msg_barter_loot_lost_desc
					left_icon = scope:barterer
					right_icon = scope:receiver
				}
			}
			scope:receiver ?= {
				send_interface_message = {
					type = msg_barter_loot_won
					title = msg_barter_loot_won_title
					desc = msg_barter_loot_won_desc
					left_icon = scope:receiver
					right_icon = scope:barterer
				}
			}
		}
	}
}
