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
			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 = {
				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 = {
				40 = {
					force_operative_leader_into_hiding = 45
					operative_leader_event = {
						id = lar_operative_event.2
						set_from_from = FROM
					}
				}
				35 = {
					capture_operative = {
						captured_by = FROM
					}
				}
				20 = {
					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 = ENG
					}
					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
				}
			}
		}
	}

	# 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 = {

	}

	#ROOT is winner #FROM gets annexed - This will also fire on_annex
}
