﻿loaa_pillage_contract_cb = {
	icon = ducal_conquest_cb
	group = invasion

	combine_into_one = yes
	should_show_war_goal_subview = yes
	allow_hostages = no
	mutually_exclusive_titles = { always = yes }
	interface_priority = 110
	target_top_liege_if_outside_realm = no
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150

	allowed_for_character = {
		any_character_active_contract = {
			OR = {
				has_task_contract_type = loaa_pillage_contract
				# has_task_contract_type = loaa_pillage_contract_ai
			}
		}
	}

	allowed_against_character = {
		scope:attacker = {
			any_character_active_contract = {
				OR = {
					has_task_contract_type = loaa_pillage_contract
					# has_task_contract_type = loaa_pillage_contract_ai
				}
				var:task_contract_target ?= scope:defender
			}
		}
	}

	target_titles = all
	target_title_tier = county # county
	target_de_jure_regions_above = no
	ignore_effect = change_title_holder

	ai_score_mult = {
		value = fp2_border_raid_ai_targeting_value
		#Declaring war on the frankokratia leader can really ruin the whole thing
		# if = {
		# 	limit = {
		# 		NOR = {
		# 			exists = global_var:AGOT_is_loaded
		# 			exists = global_var:curr_province
		# 		}
		# 	}
		# 	if = {
		# 		limit = {
		# 			scope:defender = {
		# 				any_owned_story = {
		# 					story_type = frankokratia_story
		# 					NOT = { has_variable = franko_war_complete }
		# 				}
		# 			}
		# 		}
		# 		multiply = 0
		# 	}
		# }
	}

	valid_to_start = {
		exists = scope:target
		NOT = {
			scope:target.title_province = { has_province_modifier = recently_looted_modifier }
		}
	}

	should_invalidate = {
		scope:attacker = {
			NOT = {
				any_character_active_contract = {
					OR = {
						has_task_contract_type = loaa_pillage_contract
						# has_task_contract_type = loaa_pillage_contract_ai
					}
					var:task_contract_target ?= scope:defender
				}
			}
		}
	}

	on_invalidated_desc = msg_county_conquest_war_invalidated_message

	on_invalidated = {
		scope:attacker = {
			if = {
				limit = {
					any_character_active_contract = {
						OR = {
							has_task_contract_type = loaa_pillage_contract
							# has_task_contract_type = loaa_pillage_contract_ai
						}
						var:task_contract_target ?= scope:defender
					}
				}
				every_character_active_contract = {
					limit = {
						OR = {
							has_task_contract_type = loaa_pillage_contract
							# has_task_contract_type = loaa_pillage_contract_ai
						}
						var:task_contract_target ?= scope:defender
					}
					invalidate_contract = yes
				}
			}
		}
	}

    #it's free, because someone employs you to do that
	cost = {
	}

	on_declaration = {
		on_declared_war = yes
	}

	on_victory_desc = {
		triggered_desc = {
			trigger = {
				scope:attacker = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_victory_attacker
		}
		triggered_desc = {
			trigger = {
				scope:defender = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_victory_defender
		}
	}

	on_victory = {
		scope:attacker = {
            random_character_active_contract = {
				limit = {
                    OR = {
						has_task_contract_type = loaa_pillage_contract
						# has_task_contract_type = loaa_pillage_contract_ai
					}
					var:task_contract_target = scope:defender
				}
				save_scope_as = pillage_contract
				task_contract_employer = {
					save_scope_as = task_contract_employer
				}
			}
		}

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }

		every_in_list = {
			list = target_titles
			save_scope_as = raid_county
			# Set raided province modifier
			title_province = {
				add_province_modifier = {
					modifier = recently_looted_modifier
					years = 5
				}
			}
			show_as_tooltip = {
				every_in_list = {
					list = target_titles
					fp2_border_raid_damage_effect = yes
				}
			}
			# Find province for building destruction
			ordered_county_province = {
				order_by = num_buildings
				random = {
					chance = 25
					modifier = { factor = destroy_building_soldier_multiplier_value } # chance increases with more soldiers
					modifier = { add = destroy_building_development_multiplier_value } # chance increases with development
					custom_tooltip = destroy_random_building_tt
					destroy_random_building_variable_effect = yes
				}
			}
		}

		hidden_effect = {
			scope:attacker = {
				send_interface_message = {
					type = event_raid_good
					title = border_raid_message_title
					show_as_tooltip = {
						every_in_list = {
							list = target_titles
							fp2_border_raid_damage_effect = yes
						}
					}
				}
			}
			scope:defender = {
				send_interface_message = {
					type = event_raid_bad
					title = border_raid_message_title
					every_in_list = {
						list = target_titles
						fp2_border_raid_damage_effect = yes
					}
				}
			}
		}

		show_as_tooltip = {
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change
				add_claim_on_loss = yes
			}
			every_in_list = {
				list = target_titles
				change_title_holder = {
					holder = scope:attacker
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}

		# Prestige Progress for the Attacker
		every_in_list = {
			list = target_titles
			scope:attacker = {
				add_prestige_experience = miniscule_prestige_value
			}
		}

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Break alliances now out of diplo range
		if = {
			limit = {
				scope:attacker = {
					any_ally = {
						NOT = { in_diplomatic_range = scope:attacker }
					}
				}
			}
			scope:attacker = {
				every_ally = {
					limit = {
						NOT = { in_diplomatic_range = scope:attacker }
					}
					break_alliance = scope:attacker
				}
			}
		}
        #complete the contract
        hidden_effect = {
            scope:pillage_contract = {
                complete_task_contract = success_standard
            }
        }

		# Truce
		add_truce_attacker_victory_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_white_peace_desc = {
		triggered_desc = {
			trigger = {
				scope:attacker = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_white_peace_attacker
		}
		triggered_desc = {
			trigger = {
				scope:defender = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_white_peace_defender
		}
	}

	on_white_peace = {
		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = miniscule_prestige_value
				multiply = -1.0
			}
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

        #complete the contract

        scope:attacker = {
            scope:pillage_contract = {
                complete_task_contract = failure_standard
            }
        }

		add_truce_white_peace_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		triggered_desc = {
			trigger = {
				scope:attacker = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_defeat_attacker
		}
		triggered_desc = {
			trigger = {
				scope:defender = { is_local_player = yes }
			}
			desc = border_raid_cb_desc_defeat_defender
		}
	}

	on_defeat = {
		# Legitimacy
		#add_legitimacy_attacker_defeat_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 1
			}
			add_prestige = {
				value = medium_prestige_value
				multiply = -1.0
			}
		}

		# Prestige for Defender
		scope:defender = {
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		add_truce_attacker_defeat_effect = yes

		scope:attacker = {
			save_temporary_scope_as = loser
		}

        scope:attacker = {
            scope:pillage_contract = {
                complete_task_contract = failure_standard
            }
        }

		on_lost_aggression_war_discontent_loss = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	transfer_behavior = transfer

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "EP3_PILLAGING_FORAY_WAR_NAME"
	war_name_base = "EP3_PILLAGING_FORAY_WAR_NAME_BASE"
	cb_name = "EP3_PILLAGING_FORAY_CB_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	# The war will last 2 years at most
	defender_ticking_warscore_delay = { months = 3 }
	defender_ticking_warscore = 0.05

	ticking_war_score_targets_entire_realm = yes
	attacker_ticking_warscore = 0
	attacker_wargoal_percentage = 0.01
	attacker_score_from_occupation_scale = 50
	defender_score_from_occupation_scale = 50
	attacker_score_from_battles_scale = 200
	defender_score_from_battles_scale = 200

	# attacker_wargoal_percentage = 1
	# attacker_ticking_warscore = 1

	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

}