
ui_agency_position = {

	set_temp_variable = { x0 = 0 }
	set_temp_variable = { x1 = 100 }
	set_temp_variable = { x2 = 200 }
	set_temp_variable = { x3 = 300 }
	set_temp_variable = { x4 = 400 }
	set_temp_variable = { x5 = 500 }
	set_temp_variable = { x6 = 600 }
	set_temp_variable = { x7 = 700 }
	set_temp_variable = { x8 = 800 }
	set_temp_variable = { x9 = 900 }
	set_temp_variable = { x10 = 1000 }
	set_temp_variable = { x11 = 1100 }
	set_temp_variable = { x12 = 1200 }
	set_temp_variable = { x13 = 1300 }
	set_temp_variable = { x14 = 1400 }
	set_temp_variable = { x15 = 1500 }
	set_temp_variable = { x16 = 1600 }
	set_temp_variable = { x17 = 1700 }
	set_temp_variable = { x18 = 1800 }
	set_temp_variable = { x19 = 1900 }
	set_temp_variable = { x20 = 2000 }
	set_temp_variable = { x21 = 2100 }
	set_temp_variable = { x22 = 2200 }
	
	set_temp_variable = { y0 = 0 }
	set_temp_variable = { y1 = 100 }
	set_temp_variable = { y2 = 200 }
	set_temp_variable = { y3 = 300 }
	set_temp_variable = { y4 = 400 }
	set_temp_variable = { y5 = 500 }
	set_temp_variable = { y6 = 600 }
	set_temp_variable = { y7 = 700 }
	set_temp_variable = { y8 = 800 }
	set_temp_variable = { y9 = 900 }
	set_temp_variable = { y10 = 1000 }
	set_temp_variable = { y11 = 1100 }
	set_temp_variable = { y12 = 1200 }
	set_temp_variable = { y13 = 1300 }
	set_temp_variable = { y14 = 1400 }
	set_temp_variable = { y15 = 1500 }
	set_temp_variable = { y16 = 1600 }
	set_temp_variable = { y17 = 1700 }
	set_temp_variable = { y18 = 1800 }
	set_temp_variable = { y19 = 1900 }
	set_temp_variable = { y20 = 2000 }
	set_temp_variable = { y21 = 2100 }
	set_temp_variable = { y22 = 2200 }
				
}
ui_agency_position_adjust = {

	set_temp_variable = { continuous_glow_x = upgrade_x }
	add_to_temp_variable = { continuous_glow_x = 13 }
	set_temp_variable = { continuous_glow_y = upgrade_y }
	add_to_temp_variable = { continuous_glow_y = -58 }
	if = {
		limit = {
			check_variable = { is_major_upgrade = 0 }
		}
		add_to_temp_variable = { continuous_glow_y = 22 }
	}
	
	set_temp_variable = { upgrade_icon_frame_x = upgrade_x }
	add_to_temp_variable = { upgrade_icon_frame_x = 13 }
	set_temp_variable = { upgrade_icon_frame_y = upgrade_y }
	add_to_temp_variable = { upgrade_icon_frame_y = -46 }
	
	set_temp_variable = { upgrade_icon_x = upgrade_x }
	add_to_temp_variable = { upgrade_icon_x = 48 }
	set_temp_variable = { upgrade_icon_y = upgrade_y }
	add_to_temp_variable = { upgrade_icon_y = -42 }
	
	set_temp_variable = { cancel_upgrade_x = upgrade_x }
	add_to_temp_variable = { cancel_upgrade_x = 147 }
	set_temp_variable = { cancel_upgrade_y = upgrade_y }
	add_to_temp_variable = { cancel_upgrade_y = 15 }
	
	set_temp_variable = { upgrade_name_x = upgrade_x }
	add_to_temp_variable = { upgrade_name_x = 11 }
	set_temp_variable = { upgrade_name_y = upgrade_y }
	add_to_temp_variable = { upgrade_name_y = 11 }
	
	set_temp_variable = { upgrade_level_x = upgrade_x }
	add_to_temp_variable = { upgrade_level_x = 57 }
	set_temp_variable = { upgrade_level_y = upgrade_y }
	add_to_temp_variable = { upgrade_level_y = 42 }
	
	set_temp_variable = { num_factories_x = upgrade_x }
	add_to_temp_variable = { num_factories_x = 157 }
	set_temp_variable = { num_factories_y = upgrade_y }
	add_to_temp_variable = { num_factories_y = 26 }
	
	set_temp_variable = { civilian_factory_icon_x = upgrade_x }
	add_to_temp_variable = { civilian_factory_icon_x = 166 }
	set_temp_variable = { civilian_factory_icon_y = upgrade_y }
	add_to_temp_variable = { civilian_factory_icon_y = 11 }
	
	set_temp_variable = { cost_background_x = upgrade_x }
	add_to_temp_variable = { cost_background_x = 157 }
	set_temp_variable = { cost_background_y = upgrade_y }
	add_to_temp_variable = { cost_background_y = 3 }
	
	set_temp_variable = { ai_score_x = upgrade_x }
	set_temp_variable = { ai_score_y = upgrade_y }
	add_to_temp_variable = { ai_score_y = 44 }
}

is_running_any_army_operation_against_PREV = {
	OR = {
		is_running_operation = {
			target = PREV
			operation = operation_infiltrate_armed_forces_army
		}
		is_running_operation = {
			target = PREV
			operation = operation_steal_tech_army
		}
		is_running_operation = {
			target = PREV
			operation = operation_target_army_leader
		}
		is_running_operation = {
			target = PREV
			operation = operation_target_attache
		}
		is_running_operation = {
			target = PREV
			operation = operation_fortification_plans
		}
	}
}

is_running_any_air_operation_against_PREV = {
	OR = {
		is_running_operation = {
			target = PREV
			operation = operation_infiltrate_armed_forces_airforce
		}
		is_running_operation = {
			target = PREV
			operation = operation_steal_tech_airforce
		}
		is_running_operation = {
			target = PREV
			operation = operation_coordinated_strike
		}
		is_running_operation = {
			target = PREV
			operation = operation_sabotage_radar
		}
	}
}

is_running_any_navy_operation_against_PREV = {
	OR = {
		is_running_operation = {
			target = PREV
			operation = operation_infiltrate_armed_forces_navy
		}
		is_running_operation = {
			target = PREV
			operation = operation_steal_tech_navy
		}
		is_running_operation = {
			target = PREV
			operation = operation_sabotage_port
		}
	}
}

is_running_any_civilian_operation_against_PREV = {
	OR = {
		is_running_operation = {
			target = PREV
			operation = operation_infiltrate_civilian
		}
		is_running_operation = {
			target = PREV
			operation = operation_collaboration_government
		}
		is_running_operation = {
			target = PREV
			operation = operation_steal_tech_civilian
		}
		is_running_operation = {
			target = PREV
			operation = operation_sabotage_fuel
		}
		is_running_operation = {
			target = PREV
			operation = operation_sabotage_infra
		}
		is_running_operation = {
			target = PREV
			operation = operation_sabotage_oil
		}
	}
}


ai_agency_upgrade_type_score = {
	# ai_type: army air navy civilian security operation operative cryptology
	
	set_temp_variable = { t = army_strength }
	multiply_temp_variable = { t = 0.005 }
	clamp_temp_variable = { var = t max = 1 }
	add_to_temp_variable = { ai_type_weight@army = t }
	
	set_temp_variable = { t = air_strength }
	multiply_temp_variable = { t = 0.001 }
	clamp_temp_variable = { var = t max = 1 }
	add_to_temp_variable = { ai_type_weight@air = t }
	
	set_temp_variable = { t = navy_strength }
	multiply_temp_variable = { t = 0.03 }
	clamp_temp_variable = { var = t max = 1 }
	add_to_temp_variable = { ai_type_weight@navy = t }
	
	set_temp_variable = { t = num_of_factories }
	multiply_temp_variable = { t = 0.005 }
	clamp_temp_variable = { var = t max = 1 }
	add_to_temp_variable = { ai_type_weight@civilian = t }

	set_temp_variable = { t = modifier@operative_slot }
	multiply_temp_variable = { t = 0.2 }
	clamp_temp_variable = { var = t max = 1 }
	add_to_temp_variable = { ai_type_weight@operation = t }
	add_to_temp_variable = { ai_type_weight@operative = t }

	if = {
		limit = {		
			all_of_scopes = {
				array = global.agency_countries
				
				if = {
					limit = {
						OR = {
							is_running_operation = {
								target = PREV
							}
							#any_operative_leader = {
							#	check_variable = { operation_country = PREV.PREV }
							#}
						}
					}
					add_to_temp_variable = { ai_type_weight@security = 1 }
				}
			}
		}
	}

	if = {
		limit = {
			has_war = yes
		}
		add_to_temp_variable = { ai_type_weight@security = 0.75 }
	}

	if = {
		limit = {
			any_enemy_country = {
				is_ai = no
			}
		}
		add_to_temp_variable = { ai_type_weight@security = 1.25 }
	}

	if = {
		limit = {
			num_of_operatives > 1
		}
		add_to_temp_variable = { ai_type_weight@security = 0.5 }
	}
	
}

ai_can_upgrade_agency = {
	has_intelligence_agency = yes
	
	set_temp_variable = { needed_ic = 28 }
	add_to_temp_variable = { needed_ic = agency_upgrade_cost }

	if = {
		limit = {
			check_variable = { agency_upgrade_number > 3 }
		}
		add_to_temp_variable = { needed_ic = 4 }
	}

	if = {
		limit = {
			check_variable = { agency_upgrade_number > 8 }
		}
		add_to_temp_variable = { needed_ic = 6 }
	}

	if = {
		limit = {
			check_variable = { agency_upgrade_number > 14 }
		}
		add_to_temp_variable = { needed_ic = 7 }
	}

	if = {
		limit = {
			check_variable = { agency_upgrade_number > 22 }
		}
		add_to_temp_variable = { needed_ic = 5 }
	}

	if = {
		limit = {
			is_major = yes
		}
		add_to_temp_variable = { needed_ic = -3 }
	}

	if = {
		limit = {
			has_war = yes
		}
		add_to_temp_variable = { needed_ic = -3 }
	}

	if = {
		limit = {
			num_of_civilian_factories < 24
		}
		add_to_temp_variable = { needed_ic = 10 }
	}
	
	if = {
		limit = {
			surrender_progress > 0.15
		}
		add_to_temp_variable = { needed_ic = 8 }
	}

	check_variable = { num_of_civilian_factories > needed_ic }
}

ai_can_run_operations = {
	has_intelligence_agency = yes
	num_of_operatives > 0
}

upgrade_agency_second_upgrade = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 15 }
	set_temp_variable = { duration = 90 }
	
	set_temp_variable = { level = 1 }
	
	agency_upgrade_number > 19
	
	set_temp_variable = { ai_score = 50 }
}
upgrade_agency_third_upgrade = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 30 }
	set_temp_variable = { duration = 90 }
	
	set_temp_variable = { level = 1 }
	
	has_done_agency_upgrade = upgrade_agency_second_upgrade
	agency_upgrade_number > 39
	
	set_temp_variable = { ai_score = 50 }
}
upgrade_army_department = {
	set_temp_variable = { branch = token:upgrade_army_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:army }
}
upgrade_army_no_token_use = {
	set_temp_variable = { branch = token:upgrade_army_department }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	custom_trigger_tooltip = {
		tooltip = {
			localization_key = AGENCY_MUTUALLY_EXCLUSIVE
			AGAINST = upgrade_army_stackable
		}
		NOT = { 
			has_country_flag = upgrade_army_stackable 
			has_done_agency_upgrade = upgrade_army_stackable
			is_in_array = { current_upgrade_array = token:upgrade_army_stackable }
		}
	}
	has_done_agency_upgrade = upgrade_army_department
	
	set_temp_variable = { ai_score = 0.5 }
	set_temp_variable = { ai_type = token:army }
}
upgrade_army_stackable = {
	set_temp_variable = { branch = token:upgrade_army_department }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	custom_trigger_tooltip = {
		tooltip = {
			localization_key = AGENCY_MUTUALLY_EXCLUSIVE
			AGAINST = upgrade_army_no_token_use
		}
		NOT = { 
			has_country_flag = upgrade_army_no_token_use
			has_done_agency_upgrade = upgrade_army_no_token_use 
			is_in_array = { current_upgrade_array = token:upgrade_army_no_token_use }
		}
	}
	has_done_agency_upgrade = upgrade_army_department
	
	set_temp_variable = { ai_score = 1.25 }
	set_temp_variable = { ai_type = token:army }
}
upgrade_army_enemy_intel_reduction = {
	set_temp_variable = { branch = token:upgrade_army_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 60 }
	
	set_temp_variable = { level = 1 }
	
	has_done_agency_upgrade = upgrade_army_department
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:army }
	
	all_of_scopes = {
		array = global.agency_countries
		
		if = {
			limit = {
				is_running_any_army_operation_against_PREV = yes
				add_to_temp_variable = { ai_score = 1 }
			}
		}
		
		add_to_temp_variable = { ai_score = intel_army_asset_level@PREV }
	}
}
upgrade_army_enemy_counter_intel = {
	set_temp_variable = { branch = token:upgrade_army_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	has_done_agency_upgrade = upgrade_army_enemy_intel_reduction
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:army }
	
	all_of_scopes = {
		array = global.agency_countries
		
		if = {
			limit = {
				is_running_any_army_operation_against_PREV = yes
				add_to_temp_variable = { ai_score = 1 }
			}
		}
	}
}
upgrade_army_defense_sweep = {
	set_temp_variable = { branch = token:upgrade_army_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	has_done_agency_upgrade = upgrade_army_enemy_intel_reduction
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:army }
	
	all_of_scopes = {
		array = global.agency_countries
		
		add_to_temp_variable = { ai_score = intel_army_asset_level@PREV }
	}
}

upgrade_naval_department = {
	set_temp_variable = { branch = token:upgrade_naval_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:navy }
}
upgrade_navy_no_token_use = {
	set_temp_variable = { branch = token:upgrade_naval_department }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	custom_trigger_tooltip = {
		tooltip = {
			localization_key = AGENCY_MUTUALLY_EXCLUSIVE
			AGAINST = upgrade_navy_stackable
		}
		NOT = { 
			has_country_flag = upgrade_navy_stackable 
			has_done_agency_upgrade = upgrade_navy_stackable 
			is_in_array = { current_upgrade_array = token:upgrade_navy_stackable }
		}
	}
	has_done_agency_upgrade = upgrade_naval_department
	
	set_temp_variable = { ai_score = 0.5 }
	set_temp_variable = { ai_type = token:navy }
}
upgrade_navy_stackable = {
	set_temp_variable = { branch = token:upgrade_naval_department }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	custom_trigger_tooltip = {
		tooltip = {
			localization_key = AGENCY_MUTUALLY_EXCLUSIVE
			AGAINST = upgrade_navy_no_token_use
		}
		NOT = { 
			has_country_flag = upgrade_navy_no_token_use 
			has_done_agency_upgrade = upgrade_navy_no_token_use 
			is_in_array = { current_upgrade_array = token:upgrade_navy_no_token_use }
		}
	}
	has_done_agency_upgrade = upgrade_naval_department
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:navy }
}
upgrade_navy_enemy_intel_reduction = {
	set_temp_variable = { branch = token:upgrade_naval_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 60 }
	
	set_temp_variable = { level = 1 }
	
	has_done_agency_upgrade = upgrade_naval_department
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:navy }
	
	all_of_scopes = {
		array = global.agency_countries
		
		if = {
			limit = {
				is_running_any_navy_operation_against_PREV = yes
				add_to_temp_variable = { ai_score = 1 }
			}
		}
		
		add_to_temp_variable = { ai_score = intel_navy_asset_level@PREV }
	}
}
upgrade_navy_enemy_counter_intel = {
	set_temp_variable = { branch = token:upgrade_naval_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	has_done_agency_upgrade = upgrade_navy_enemy_intel_reduction
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:navy }
	
	all_of_scopes = {
		array = global.agency_countries
		
		if = {
			limit = {
				is_running_any_navy_operation_against_PREV = yes
				add_to_temp_variable = { ai_score = 1 }
			}
		}
	}
}
upgrade_navy_defense_sweep = {
	set_temp_variable = { branch = token:upgrade_naval_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	has_done_agency_upgrade = upgrade_navy_enemy_intel_reduction
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:navy }
	
	all_of_scopes = {
		array = global.agency_countries
		
		add_to_temp_variable = { ai_score = intel_navy_asset_level@PREV }
	}
}

upgrade_airforce_department = {
	set_temp_variable = { branch = token:upgrade_airforce_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:air }
}
upgrade_air_no_token_use = {
	set_temp_variable = { branch = token:upgrade_airforce_department }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	custom_trigger_tooltip = {
		tooltip = {
			localization_key = AGENCY_MUTUALLY_EXCLUSIVE
			AGAINST = upgrade_air_stackable
		}
		NOT = { 
			has_country_flag = upgrade_air_stackable 
			has_done_agency_upgrade = upgrade_air_stackable
			is_in_array = { current_upgrade_array = token:upgrade_air_stackable }
		}
	}
	has_done_agency_upgrade = upgrade_airforce_department
	
	set_temp_variable = { ai_score = 0.5 }
	set_temp_variable = { ai_type = token:air }
}
upgrade_air_stackable = {
	set_temp_variable = { branch = token:upgrade_airforce_department }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	custom_trigger_tooltip = {
		tooltip = {
			localization_key = AGENCY_MUTUALLY_EXCLUSIVE
			AGAINST = upgrade_air_no_token_use
		}
		NOT = { 
			has_country_flag = upgrade_air_no_token_use 
			has_done_agency_upgrade = upgrade_air_no_token_use
			is_in_array = { current_upgrade_array = token:upgrade_air_no_token_use }
		}
	}
	has_done_agency_upgrade = upgrade_airforce_department
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:air }
}
upgrade_air_enemy_intel_reduction = {
	set_temp_variable = { branch = token:upgrade_airforce_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 60 }
	
	set_temp_variable = { level = 1 }
	
	has_done_agency_upgrade = upgrade_airforce_department
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:air }
	
	all_of_scopes = {
		array = global.agency_countries
		
		if = {
			limit = {
				is_running_any_air_operation_against_PREV = yes
				add_to_temp_variable = { ai_score = 1 }
			}
		}
		
		add_to_temp_variable = { ai_score = intel_air_asset_level@PREV }
	}
}
upgrade_air_enemy_counter_intel = {
	set_temp_variable = { branch = token:upgrade_airforce_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	has_done_agency_upgrade = upgrade_air_enemy_intel_reduction
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:air }
	
	all_of_scopes = {
		array = global.agency_countries
		
		if = {
			limit = {
				is_running_any_air_operation_against_PREV = yes
				add_to_temp_variable = { ai_score = 1 }
			}
		}
	}
}
upgrade_air_defense_sweep = {
	set_temp_variable = { branch = token:upgrade_airforce_department }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	has_done_agency_upgrade = upgrade_air_enemy_intel_reduction
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:air }
	
	all_of_scopes = {
		array = global.agency_countries
		
		add_to_temp_variable = { ai_score = intel_air_asset_level@PREV }
	}
}

upgrade_economy_civilian = {
	set_temp_variable = { branch = token:upgrade_economy_civilian }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:civilian }
	if = {
		limit = {
			OR = {
				is_in_array = { ai_operations = token:operation_sabotage_fuel }
				is_in_array = { ai_operations = token:operation_steal_tech_civilian }
				is_in_array = { ai_operations = token:operation_sabotage_oil }
			}
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_economy_no_token_use = {
	set_temp_variable = { branch = token:upgrade_economy_civilian }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	custom_trigger_tooltip = {
		tooltip = {
			localization_key = AGENCY_MUTUALLY_EXCLUSIVE
			AGAINST = upgrade_economy_stackable
		}
		NOT = { 
			has_country_flag = upgrade_economy_stackable 
			has_done_agency_upgrade = upgrade_economy_stackable
			is_in_array = { current_upgrade_array = token:upgrade_economy_stackable }
		}
	}
	has_done_agency_upgrade = upgrade_economy_civilian
	
	set_temp_variable = { ai_score = 0.5 }
	set_temp_variable = { ai_type = token:civilian }
}
upgrade_economy_stackable = {
	set_temp_variable = { branch = token:upgrade_economy_civilian }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	custom_trigger_tooltip = {
		tooltip = {
			localization_key = AGENCY_MUTUALLY_EXCLUSIVE
			AGAINST = upgrade_economy_no_token_use
		}
		NOT = { 
			has_country_flag = upgrade_economy_no_token_use 
			has_done_agency_upgrade = upgrade_economy_no_token_use
			is_in_array = { current_upgrade_array = token:upgrade_economy_no_token_use }
		}
	}
	has_done_agency_upgrade = upgrade_economy_civilian
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:civilian }
	if = {
		limit = {
			OR = {
				is_in_array = { ai_operations = token:operation_sabotage_fuel }
				is_in_array = { ai_operations = token:operation_steal_tech_civilian }
				is_in_array = { ai_operations = token:operation_sabotage_oil }
			}
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_economy_enemy_intel_reduction = {
	set_temp_variable = { branch = token:upgrade_economy_civilian }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 60 }
	
	set_temp_variable = { level = 1 }
	
	has_done_agency_upgrade = upgrade_economy_civilian
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:civilian }
	
	all_of_scopes = {
		array = global.agency_countries
		
		if = {
			limit = {
				is_running_any_civilian_operation_against_PREV = yes
				add_to_temp_variable = { ai_score = 1 }
			}
		}
		
		add_to_temp_variable = { ai_score = intel_civilian_asset_level@PREV }
	}
}
upgrade_economy_enemy_counter_intel = {
	set_temp_variable = { branch = token:upgrade_economy_civilian }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	has_done_agency_upgrade = upgrade_economy_enemy_intel_reduction
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:civilian }
	
	all_of_scopes = {
		array = global.agency_countries
		
		if = {
			limit = {
				is_running_any_civilian_operation_against_PREV = yes
				add_to_temp_variable = { ai_score = 1 }
			}
		}
	}
}
upgrade_civilian_defense_sweep = {
	set_temp_variable = { branch = token:upgrade_economy_civilian }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	has_done_agency_upgrade = upgrade_economy_enemy_intel_reduction
	
	set_temp_variable = { ai_score = 2 }
	set_temp_variable = { ai_type = token:security }
	set_temp_variable = { ai_type_2 = token:civilian }
	
	all_of_scopes = {
		array = global.agency_countries
		
		add_to_temp_variable = { ai_score = intel_civilian_asset_level@PREV }
	}
}

upgrade_passive_defense = {
	set_temp_variable = { branch = token:upgrade_passive_defense }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 70 }
	
	set_temp_variable = { level = 1 }
	
	set_temp_variable = { ai_score = 5 }
	set_temp_variable = { ai_type = token:security }
	
	all_of_scopes = {
		array = global.agency_countries
		
		add_to_temp_variable = { ai_score = intel_civilian_asset_level@PREV }
		add_to_temp_variable = { ai_score = intel_army_asset_level@PREV }
		add_to_temp_variable = { ai_score = intel_air_asset_level@PREV }
		add_to_temp_variable = { ai_score = intel_navy_asset_level@PREV }
	}
}
upgrade_defense_harmed = {
	set_temp_variable = { branch = token:upgrade_passive_defense }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	
	set_temp_variable = { ai_score = 3 }
	set_temp_variable = { ai_type = token:security }
}
upgrade_defense_intel_extraction = {
	set_temp_variable = { branch = token:upgrade_passive_defense }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	
	set_temp_variable = { ai_score = 3 }
	set_temp_variable = { ai_type = token:security }
}
upgrade_defense_critical_success = {
	set_temp_variable = { branch = token:upgrade_passive_defense }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_passive_defense
	
	set_temp_variable = { ai_score = 3 }
	set_temp_variable = { ai_type = token:security }
}
upgrade_security_department = {
	set_temp_variable = { branch = token:upgrade_passive_defense }
	set_temp_variable = { cost = 9 }
	set_temp_variable = { duration = 70 }
	
	set_temp_variable = { level = 2 }
	
	custom_trigger_tooltip = {
		tooltip = "Counterintelligence Upgrades > 19 ([?modifier@production_speed_security_branch_factor])"
		
		check_variable = { modifier@production_speed_security_branch_factor > 19 }
	}
	
	set_temp_variable = { ai_score = 10 }
	set_temp_variable = { ai_type = token:security }
}

upgrade_form_operations = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 9 }
	set_temp_variable = { duration = 70 }
	
	set_temp_variable = { level = 1 }
	
	set_temp_variable = { ai_score = 4 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_operation_infiltrate = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 2 }
	
	has_done_agency_upgrade = upgrade_form_operations
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_operation_infiltrate_critical_success = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_infiltrate
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_operation_infiltrate_risk = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_infiltrate
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_operation_infiltrate_cost = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 7 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_infiltrate
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
}

upgrade_operation_steal_tech = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_form_operations
	
	set_temp_variable = { ai_score = 1.5 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_steal_tech_effect = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_steal_tech
	
	set_temp_variable = { ai_score = 1.5 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_steal_tech_uses = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_steal_tech
	
	set_temp_variable = { ai_score = 1.5 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_steal_tech_aot = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_steal_tech
	
	set_temp_variable = { ai_score = 1.5 }
	set_temp_variable = { ai_type = token:operation }
}

upgrade_operation_target_unit_leader = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_form_operations
	
	set_temp_variable = { ai_score = 1.25 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_operation_target_army_leader_sick = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_target_unit_leader
	
	set_temp_variable = { ai_score = 1.25 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_operation_target_army_leader_meeting = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_target_unit_leader
	
	set_temp_variable = { ai_score = 1.25 }
	set_temp_variable = { ai_type = token:operation }
}
upgrade_operation_target_attache = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 1 }
	
	has_done_agency_upgrade = upgrade_operation_target_unit_leader
	
	set_temp_variable = { ai_score = 1.25 }
	set_temp_variable = { ai_type = token:operation }
}

upgrade_operation_sabotage = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_form_operations
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			OR = {
				is_in_array = { ai_operations = token:operation_sabotage_oil }
				is_in_array = { ai_operations = token:operation_sabotage_radar }
				is_in_array = { ai_operations = token:operation_sabotage_fuel }
			}
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_operation_sabotage_damage = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_sabotage
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			OR = {
				is_in_array = { ai_operations = token:operation_sabotage_oil }
				is_in_array = { ai_operations = token:operation_sabotage_radar }
				is_in_array = { ai_operations = token:operation_sabotage_fuel }
			}
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_operation_sabotage_uses = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_sabotage
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			OR = {
				is_in_array = { ai_operations = token:operation_sabotage_oil }
				is_in_array = { ai_operations = token:operation_sabotage_radar }
				is_in_array = { ai_operations = token:operation_sabotage_fuel }
			}
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_operation_sabotage_repair = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_sabotage
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			OR = {
				is_in_array = { ai_operations = token:operation_sabotage_oil }
				is_in_array = { ai_operations = token:operation_sabotage_radar }
				is_in_array = { ai_operations = token:operation_sabotage_fuel }
			}
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}

upgrade_operation_strengthen_resistance = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_form_operations
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			is_in_array = { ai_operations = token:operation_strengthen_resistance }
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_operation_strengthen_resistance_effect = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_strengthen_resistance
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			is_in_array = { ai_operations = token:operation_strengthen_resistance }
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_operation_strengthen_resistance_uses = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_strengthen_resistance
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			is_in_array = { ai_operations = token:operation_strengthen_resistance }
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}

upgrade_operation_strengthen_society = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_form_operations
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			is_in_array = { ai_operations = token:operation_strengthen_society }
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_operation_strengthen_society_effect = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_strengthen_society
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			is_in_array = { ai_operations = token:operation_strengthen_society }
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_operation_strengthen_society_uses = {
	set_temp_variable = { branch = token:upgrade_form_operations }
	set_temp_variable = { cost = 6 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_operation_strengthen_society
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:operation }
	if = {
		limit = {
			is_in_array = { ai_operations = token:operation_strengthen_society }
		}
		add_to_temp_variable = { ai_score = 1 }
	}
}
upgrade_expand_operations = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 8 }
	set_temp_variable = { duration = 70 }
	
	set_temp_variable = { level = 1 }
	
	set_temp_variable = { ai_score = 3 }
	set_temp_variable = { ai_type = token:operative }
}
upgrade_underground_contacts = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 7 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_expand_operations
	
	set_temp_variable = { ai_score = 1.5 }
	set_temp_variable = { ai_type = token:operative }
}
upgrade_training_centers = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 5 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_expand_operations
	
	set_temp_variable = { ai_score = 0.3 }
	set_temp_variable = { ai_type = token:operative }
}
upgrade_commando_training = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 1 }
	
	has_done_agency_upgrade = upgrade_expand_operations
	
	set_temp_variable = { ai_score = 0.1 }
	set_temp_variable = { ai_type = token:operative }
}
upgrade_diplo_training = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 3 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_expand_operations
	
	set_temp_variable = { ai_score = 0 }
	set_temp_variable = { ai_type = token:operative }
}
upgrade_psycho_warfare = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_expand_operations
	
	set_temp_variable = { ai_score = 0.5 }
	set_temp_variable = { ai_type = token:operative }
}
upgrade_invisible_ink = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 4 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_expand_operations
	
	set_temp_variable = { ai_score = 0 }
	set_temp_variable = { ai_type = token:operative }
}
upgrade_suicide_pills = {
	set_temp_variable = { branch = token:upgrade_expand_operations }
	set_temp_variable = { cost = 3 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 1 }
	
	has_done_agency_upgrade = upgrade_expand_operations
	
	set_temp_variable = { ai_score = 0 }
	set_temp_variable = { ai_type = token:operative }
}

upgrade_form_department = {
	set_temp_variable = { branch = token:upgrade_form_department }
	set_temp_variable = { cost = 15 }
	set_temp_variable = { duration = 70 }
	
	set_temp_variable = { level = 1 }
	
	has_tech = mechanical_computing
	
	set_temp_variable = { ai_score = 7 }
	set_temp_variable = { ai_type = token:cryptology }
}
upgrade_decryption_boost = {
	set_temp_variable = { branch = token:upgrade_form_department }
	set_temp_variable = { cost = 10 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 3 }
	
	has_done_agency_upgrade = upgrade_form_department
	
	set_temp_variable = { ai_score = 4 }
	set_temp_variable = { ai_type = token:cryptology }
}
upgrade_decryption_boost_2 = {
	set_temp_variable = { branch = token:upgrade_form_department }
	set_temp_variable = { cost = 25 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 4 }
	
	has_done_agency_upgrade = upgrade_decryption_boost
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:cryptology }
}
upgrade_crypto_strength = {
	set_temp_variable = { branch = token:upgrade_form_department }
	set_temp_variable = { cost = 10 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 4 }
	
	has_done_agency_upgrade = upgrade_form_department
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:cryptology }
	
	add_to_temp_variable = { ai_score = enemy_full_decryption }
	add_to_temp_variable = { ai_score = enemy_activated_decryption }
}
upgrade_crypto_strength_2 = {
	set_temp_variable = { branch = token:upgrade_form_department }
	set_temp_variable = { cost = 30 }
	set_temp_variable = { duration = 50 }
	
	set_temp_variable = { level = 4 }
	
	has_done_agency_upgrade = upgrade_crypto_strength
	
	set_temp_variable = { ai_score = 1 }
	set_temp_variable = { ai_type = token:cryptology }
	
	add_to_temp_variable = { ai_score = enemy_full_decryption }
	add_to_temp_variable = { ai_score = enemy_activated_decryption }
}






