on_actions = {
	# called when an operative performing an offensive mission in a country
	# has been spotted
	on_operative_on_mission_spotted = {
		effect = {
            # SCOPE  the operative
            # ROOT   the country the operative was performing its mission in
            # FROM   the country the operative is operating for

			# base values
            set_temp_variable = {
                var = capture_chance
                value = 90
            }
            set_temp_variable = {
                var = kill_chance
                value = 10
            }			

            # nullify kill chance for some missions
            if = {
                limit = {
                    OR = {
                        operative_leader_mission = control_trade
                        operative_leader_mission = diplomatic_pressure
                    }
                }
                set_temp_variable = {
                    var = kill_chance
                    value = 0
                }
            }

            # Capture chance modifier unused as it would just increased or decrease death chance
            #multiply_temp_variable = {
            #   var = capture_chance
            #   value = own_capture_chance_factor # the operative's capture chance modifiers
            #}
            #clamp_temp_variable = {
            #   var = capture_chance
            #   min = 0
            #   max = 100
            #}

            random_list = {
                #log = yes # log picked effect in game.log
                temp_var:capture_chance = {
                   	capture_operative = {
                   	    captured_by = ROOT
                   	}  	
                }
                temp_var:kill_chance = {
                    kill_operative = {
                        killed_by = ROOT
                    }
                }
            }

        }
        
    }

	on_operative_captured = {
		effect = {
			# SCOPE  the operative
			# ROOT   the country the operative was performing its mission in
			# FROM   the country the operative is operating for
			operative_leader_event = {
					id = lar_operative_event.6
					recipient = ROOT
					originator = ROOT
					set_from_from = ROOT
					set_from = FROM
					days = 1
			}
			if = {
				limit = {
					NOT = { has_unit_leader_flag = le_clerc1 }
				}
				set_unit_leader_flag = le_clerc1
			}
			else_if = {
				limit = {
					has_unit_leader_flag = le_clerc1
				}
				set_unit_leader_flag = le_clerc2
			}
			random_list = {
				log = yes
				90 = { #regular
					operative_leader_event = {
						id = lar_operative_event.1
						set_from_from = ROOT
					}
				}
				10 = { #operative turned
					modifier = {
						has_trait = operative_tough
						factor = 0 #tough operatives never get turned
					}
					modifier = {
						has_trait = operative_double_agent
						factor = 0 #no tripple agents please
					}
					turn_operative = { turned_by = ROOT }
					# ensure the operative has the nationality of the country he is coming from
					add_nationality = FROM
					operative_leader_event = {
            	    	id = lar_operative_event.5
						set_from_from = FROM #tells the event which nationality to add
            		}
            	}
			}
		}
	}

	on_operative_death = {
		effect = {
			# SCOPE  the operative
			# ROOT   the killer country (optional)
			# FROM   the country the operative is operating for
			if = {
				limit = {
					country_exists = ROOT
				}

				# operative killed by a country, likelly while on mission
				operative_leader_event = {
					id = lar_operative_event.3
					set_from_from = ROOT
				}
			}
			else = {
				# other cause of death
			}
			
			if = {
				limit = {
					FROM = {
						NOT = { is_ai = yes }
						has_done_agency_upgrade = upgrade_suicide_pills
					}
				}
				set_global_flag = kill_me_alive_flag
			}
		}
	}
	
	# SCOPE_UNIT_LEADER [
	#	ROOT, FROM
	#	FROM.FROM : SCOPE_STATE ( will only be set if the operation has a specific selection_target )
	# ]
	on_operative_detected_during_operation = {
		effect = {
			random_list = {
				45 = {
					force_operative_leader_into_hiding = 45
					operative_leader_event = {
						id = lar_operative_event.2
						set_from_from = FROM
					}
				}
				25 = {
					capture_operative = {
						captured_by = FROM
					}
				}
				25 = {
					harm_operative_leader = 90
					operative_leader_event = {
						id = lar_operative_event.4
						set_from_from = FROM
					}
				}
				5 = {
					kill_operative = {
						killed_by = FROM
					}
				}
			}
		}
	}

	on_operation_completed = {
		effect = {
			# same scope setup as in operation outcome:
			# THIS: the operation
			# ROOT: the initiating country
			# FROM: the target country
			#log = "This: [This.GetName] From: [From.GetName] Root: [Root.GetName]"
			
			if = {
				limit = {
					ROOT = { has_country_flag = just_freed_operative_flag }
				}
				every_operative = {
					if = {
						limit = {
							NOT = { has_trait = operative_escape_artist }
						}
						random_list = {
							15 = { add_unit_leader_trait = operative_escape_artist }
							85 = {}
						}
					}
				}
				ROOT = { clr_country_flag = just_freed_operative_flag }
			}
			every_operative = { #add target nationality if linguist
				if = {
					limit = {
						has_trait = operative_linguist
						NOT = { operative_leader_mission = no_mission }
						NOT = { has_nationality = FROM }
					}
					random_list = {
						20 = { add_nationality = FROM }
						80 = {}
					}
				}
			}
			every_operative = {
				if = {
					limit = {
						has_nationality = FRA
					}
					ROOT = {
						add_to_variable = {
							var = fra_nat
							value = 1
						}
					}
				}
				if = {
					limit = {
						has_nationality = ZJA
					}
					ROOT = {
						add_to_variable = {
							var = eng_nat
							value = 1
						}
					}
				}
			}
			ROOT = {
				if = {
					limit = {
						check_variable = { fra_nat > 0 }
						check_variable = { eng_nat > 0 }
					}
					set_country_flag = achievement_listen_carefully_flag
				}
				set_variable = {
					var = fra_nat
					value = 0
				}
				set_variable = {
					var = eng_nat
					value = 0
				}
			}
		}
	}

	on_startup = {
		effect = {			
			if = {
				limit = {
					has_dlc = "La Resistance"
				}
				every_country = {
					capital_scope = {
						set_variable = { historical_capital_for_country = PREV }
					}
				}
			}
		}
	}

	# ROOT is capitulated country, FROM is winner
	on_capitulation = {
		effect = {
		}
	}

	# called a country fully decrypts cipher of a target country
	# scope is the target country that its cipher is decrypted
	# from scope is the decrypter country
	on_fully_decrypted_cipher = {

	}

	# called when a country activates its active cipher bonuses against a target
	# scope is the target country
	# from scope is the country that activates its bonuses
	on_activated_active_decryption_bonuses = {

	}

	#FROM is the one that joins the faction
	on_create_faction = {
		effect = {
			# SPA Iberian Pact - either POR or SPA join a faction - the other must be added too.
			
		}
	}

	#FROM is faction leader on join faction requests. THIS DOES NOT FIRE ON ADD_TO_FACTION EFFECT! USE ON_OFFER_JOIN_FACTION!
	on_join_faction = {
		effect = {
			# SPA Iberian Pact - either POR or SPA join a faction - the other must be added too.
			
		}
	}

	#FROM is country getting invited.
	on_offer_join_faction = {
		effect = {
			# SPA Iberian Pact - either POR or SPA join a faction - the other must be added too.
			
		}
	}

	# called when a country send volunteers to another
	# ROOT is sender, FROM is receiver
	on_send_volunteers = {
		effect = {
			
		}
	}

	#FROM is war target
	on_declare_war = {
		effect = {			
			
		}
	}
	#ROOT is winner #FROM gets annexed - This fires just before FROM gets annexed, meaning the country and everything it owns still exists. It will also fire on_annex and on_civil_war_end
	on_civil_war_end_before_annexation = {
		effect = {
			if = { #     
				limit = {
					FROM = {
						original_tag = ZRE
					}
					has_completed_focus = zre_omniki_zenki_win
					zre_vosstanie_v_partii_in_progress = yes					
					NOT = {
						any_other_country = {
							NOT = {
								tag = ROOT
								tag = FROM
							}
							original_tag = ZRE
							exists = yes
						}
					}
				}
				country_event = { id = ar_news.993 }
			}
			if = { #     
				limit = {
					FROM = {
						original_tag = ZRE
					}
					has_completed_focus = zre_omniki_generaly_win
					zre_vosstanie_v_partii_in_progress = yes					
					NOT = {
						any_other_country = {
							NOT = {
								tag = ROOT
								tag = FROM
							}
							original_tag = ZRE
							exists = yes
						}
					}
				}
				country_event = { id = ar_news.994 }
			}			
		}
	}

	#ROOT is winner #FROM gets annexed - This will also fire on_annex
	on_civil_war_end = {
		effect = {			
			
		}
	}

	# ROOT is capitulated country, FROM is winner
	on_capitulation = {
		effect = {				
			
		}
	}	
	

	#ROOT is new controller #FROM is old controller #FROM.FROM is state ID
	on_state_control_changed = {
		effect = {			
			
		}
	}

	#THIS is country that has just gotten into a war
	on_war = {
		
	}

	on_weekly = {
		effect = {
			if = {
				limit = { 
					has_intelligence_agency = yes
					is_ai = yes
				}
				update_operation_ai = yes
			}
		}
	}

	on_annex = {
		effect = {				
			
		}
	}
	
	# scope is operative
	# from scope is the country 
	#on_operative_recruited = {
	#	effect = {
	#	
	#	}
	#}
	
	# scope is operative
	# from scope is the country 
	#on_operative_created = {
	#	effect = {
	#	
	#	}
	#}
}
