#	Example:
# 
#	example_trigger = {
#		tag = GER
#		is_ai = no
#	}
#
#
#	In a script file:
#
#	trigger = {
#		exampel_trigger = yes
#	}
#

pd_calculate_province_cost = {
	if = {
		limit = {
			check_variable = { ROOT.pd_selected = 1 }
		}
		# Calculate cost
		set_temp_variable = { pts = THIS.state_strategic_value }
		round_temp_variable = pts
		clamp_temp_variable = {
			var = pts
			min = 1
		}
		# Cost modifiers
		if = {
			limit = { 
				NOT = { ROOT = { has_country_flag = pd_root_gives } }
				is_core_of = ROOT 
			}
			divide_temp_variable = { pts = 2 }
		}
		else_if = {
			limit = { 
				NOT = { ROOT = { has_country_flag = pd_root_gives } }
				is_claimed_by = ROOT 
			}
			divide_temp_variable = { pts = 1.5 }
		}
	}
	else_if = {
		limit = {
			check_variable = { ROOT.pd_selected = 2 }
		}
		# Calculate cost
		set_temp_variable = { pts = THIS.state_strategic_value }
		divide_temp_variable = { pts = 2 }
		round_temp_variable = pts
		clamp_temp_variable = {
			var = pts
			min = 1
		}
	}
	else_if = {
		limit = {
			check_variable = { ROOT.pd_selected = 3 }
		}
		# Calculate cost
		set_temp_variable = { pts = THIS.state_strategic_value }
		divide_temp_variable = { pts = 4 }
		round_temp_variable = pts
		clamp_temp_variable = {
			var = pts
			min = 1
		} 
	}
	else_if = {
		limit = {
			check_variable = { ROOT.pd_selected = 4 }
		}
		# Calculate cost
		set_temp_variable = { pts = THIS.state_strategic_value }
		divide_temp_variable = { pts = 3 }
		round_temp_variable = pts
		clamp_temp_variable = {
			var = pts
			min = 1
		} 
	}
	if = {
		limit = { is_owned_and_controlled_by = THIS.owner }
		multiply_temp_variable = { pts = 2 }
	}
	always = yes
}

pd_is_marked = {
	OR = {
		AND = {
			check_variable = { ROOT.pd_selected = 1 }
			ROOT = {
				is_in_array = {
					array = pd_take_array
					value = PREV
				}
			}
		}
		AND = {
			check_variable = { ROOT.pd_selected = 2 }
			ROOT = {
				is_in_array = {
					array = pd_puppet_array
					value = PREV
				}
			}
		}
		AND = {
			check_variable = { ROOT.pd_selected = 3 }
			ROOT = {
				is_in_array = {
					array = pd_demilitarize_array
					value = PREV
				}
			}
		}
		AND = {
			check_variable = { ROOT.pd_selected = 4 }
			ROOT = {
				is_in_array = {
					array = pd_liberate_array
					value = PREV
				}
			}
		}
	}
}

pd_is_invisible = {
	OR = {
		AND = {
			NOT = { check_variable = { ROOT.pd_selected = 1 } }
			ROOT = {
				is_in_array = {
					array = pd_take_array
					value = PREV
				}
			}
		}
		AND = {
			NOT = { check_variable = { ROOT.pd_selected = 2 } }
			ROOT = {
				is_in_array = {
					array = pd_puppet_array
					value = PREV
				}
			}
		}
		AND = {
			NOT = { check_variable = { ROOT.pd_selected = 3 } }
			ROOT = {
				is_in_array = {
					array = pd_demilitarize_array
					value = PREV
				}
			}
		}
		AND = {
			NOT = { check_variable = { ROOT.pd_selected = 4 } }
			ROOT = {
				is_in_array = {
					array = pd_liberate_array
					value = PREV
				}
			}
		}
		AND = {
			check_variable = { ROOT.pd_selected = 4 }
			
			NOT = {
				any_of_scopes = {
					array = THIS.core_countries
				
					NOT = { original_tag = PREV.owner }
					NOT = { tag = ROOT }
				}
			}
		}
	}
}

###

pd_ai_will_offer_peace = {
	# root is the sender and this is receiver
	
	# TODO
	always = no
}

pd_ai_will_accept = {
	# base
	set_temp_variable = { will_accept = -100 }
			
	# close to surrender 
	set_temp_variable = { sp = var:THIS.surrender_progress }
	multiply_temp_variable = { sp = 200 }
	round_temp_variable = sp
	subtract_from_temp_variable = { sp = 100 }
	clamp_temp_variable = {
		var = sp
		min = 0
		max = 100
	}
	add_to_temp_variable = { will_accept = sp }
	
	# war support
	set_temp_variable = { ws = var:THIS.has_war_support }
	multiply_temp_variable = { ws = -200 }
	round_temp_variable = ws
	add_to_temp_variable = { ws = 100 }
	add_to_temp_variable = { will_accept = ws }
	
	# stability 
	set_temp_variable = { s = var:THIS.has_stability }
	multiply_temp_variable = { s = -200 }
	round_temp_variable = s
	add_to_temp_variable = { s = 100 }
	add_to_temp_variable = { will_accept = s }
			
	# relative war score 
	set_temp_variable = { rvp = ROOT.vp@PREV }
	set_temp_variable = { tvp = THIS.vp@ROOT }
	subtract_from_temp_variable = { rvp = tvp }
	round_temp_variable = rvp
	add_to_temp_variable = { will_accept = rvp }
			
	# strength ratio
	set_temp_variable = { str = THIS.enemies_strength_ratio@ROOT }
	multiply_temp_variable = { str = 100 }
	round_temp_variable = str
	subtract_from_temp_variable = { str = 100 }
	clamp_temp_variable = {
		var = str
		max = 1000
	}
	add_to_temp_variable = { will_accept = str }
	
	# world tension (while threat < 0.2 -> 0, later -100 for each 0.1)
	set_temp_variable = { wt = global.threat }
	multiply_temp_variable = { wt = -1000 }
	round_temp_variable = wt
	add_to_temp_variable = { wt = 200 }
	clamp_temp_variable = {
		var = wt
		max = 0
	}
	add_to_temp_variable = { will_accept = wt }
	
	# peace deal cost
	subtract_from_temp_variable = { will_accept = ROOT.pd_total_cost }
	
	# wants to much!
	if = {
		limit = {
			check_variable = { ROOT.vp@PREV < ROOT.pd_total_cost }
		}
		subtract_from_temp_variable = { will_accept = 10000 }
	}
	
	# is major
	if = {
		limit = {
			is_major = yes
		}
		subtract_from_temp_variable = { will_accept = 200 }
	}
	
	# is in faction
	if = {
		limit = {
			is_in_faction = yes
		}
		subtract_from_temp_variable = { will_accept = 100 }
	}
	
	# Final check
	check_variable = { will_accept > 0 }
}