######################################################
#	Expert AI mod - scripted construction AI
######################################################

# THIS = STATE, PREV = BUILDER
EAI_has_free_shared_slot = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_has_free_shared_slot > THIS=[this.getname] PREV=[prev.getname]" }

	free_building_slots = { building = arms_factory size > 0 include_locked = no }
	free_building_slots = { building = industrial_complex size > 0 include_locked = no }
	if = { limit = { is_coastal = yes } free_building_slots = { building = dockyard size > 0 include_locked = no } }
	if = { limit = { PREV = { has_tech = synth_oil_experiments } check_variable = { building_level@synthetic_refinery < 3 } } free_building_slots = { building = synthetic_refinery size > 0 include_locked = no } }
	if = { limit = { PREV = { has_tech = fuel_silos } } free_building_slots = { building = fuel_silo size > 0 include_locked = no } }

	if = { limit = { check_variable = { _num_available_shared_slots > 0 } }

		free_building_slots = { building = arms_factory size > 1 include_locked = no }
		free_building_slots = { building = industrial_complex size > 1 include_locked = no }
		if = { limit = { is_coastal = yes } free_building_slots = { building = dockyard size > 1 include_locked = no } }
	}
}

# THIS = STATE, PREV = BUILDER
EAI_has_unreserved_shared_slot = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_has_unreserved_shared_slot > THIS=[this.getname] PREV=[prev.getname]" }

	set_temp_variable = { tmp_free_slots = modifier@local_building_slots }
	set_temp_variable = { tmp_free_slots_mod = 1 }
	add_to_temp_variable = { tmp_free_slots_mod = PREV.modifier@global_building_slots_factor }
	multiply_temp_variable = { tmp_free_slots = tmp_free_slots_mod }
	meta_trigger = {
		text = { set_temp_variable = { tmp_free_slots = [x] } }
		x = "[?tmp_free_slots|0]"
	}

	set_temp_variable = { tmp_used_slots = building_level@arms_factory }
	add_to_temp_variable = { tmp_used_slots = building_level@industrial_complex }
	add_to_temp_variable = { tmp_used_slots = building_level@dockyard }
	add_to_temp_variable = { tmp_used_slots = building_level@synthetic_refinery }
	add_to_temp_variable = { tmp_used_slots = building_level@fuel_silo }

	subtract_from_temp_variable = { tmp_free_slots = tmp_used_slots }

	# Extra slots added to a state 
	if = { limit = { EAI_has_free_shared_slot = yes check_variable = { tmp_free_slots < 1 } }
		
		set_temp_variable = { tmp_free_slots = 1 }
	}

	set_temp_variable = { tmp_free_unreserve_slots = tmp_free_slots }
	OR = {
		any_of = { array = EAI_PC_projects_in_state_of_@PREV value = EAI_has_unreserved_shared_slot_value

			if = { limit = { is_in_array = { global.EAI_PC_BUILDING_SHARED_TYPE = PREV.EAI_PC_building_type^EAI_has_unreserved_shared_slot_value } }

				subtract_from_temp_variable = { tmp_free_unreserve_slots = 1 }
			}
			always = no
		}

		check_variable = { tmp_free_unreserve_slots > 0 }
	}
}

# THIS = STATE, PREV = BUILDER, _project_building_type, _project_province_id, _match_project_type_id
EAI_PC_has_same_type_project_here = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_has_same_type_project_here > THIS=[this.getname] PREV=[prev.getname] _project_building_type=[?_project_building_type] _project_province_id=[?_project_province_id]" }

	any_of = { array = EAI_PC_projects_in_state_of_@PREV value = v_23423

		check_variable = { PREV.EAI_PC_building_type^v_23423 = _project_building_type }

		if = { limit = { check_variable = { _match_project_type_id > 0 } } check_variable = { PREV.EAI_PC_type_id^v_23423 = _match_project_type_id } }

		if = { limit = { is_in_array = { global.EAI_PC_BUILDING_PROVINCIAL = _project_building_type } }
		
			check_variable = { PREV.EAI_PC_target_province^v_23423 = _project_province_id }
		}
	}
}

# THIS = BUILDER, _get_queued_num_building_type, _get_queued_num_type_id
EAI_PC_get_total_queued_num = { if = { limit = { has_country_flag = EAI_PC_trace_functions } log = "EAI_PC_get_total_queued_num > THIS=[this.getname] PREV=[prev.getname]" } 

	set_temp_variable = { queued_type_num_ = 0 }

	all_of = { array = EAI_PC_queue value = queued_type_index value = queued_type_value

		set_temp_variable = { add_to_queued_num = 0 }

		if = {
			limit = {
				if = { limit = { check_variable = { _get_queued_num_building_type > 0 } } check_variable = { EAI_PC_building_type^queued_type_value = _get_queued_num_building_type } }
				if = { limit = { check_variable = { _get_queued_num_type_id > 0 } } check_variable = { EAI_PC_type_id^queued_type_value = _get_queued_num_type_id } }
			}

			add_to_temp_variable = { queued_type_num_ = 1 } 
		}
	}

	set_temp_variable = { _get_queued_num_type = 0 }
	set_temp_variable = { _get_queued_num_type_id = 0 }
}

### available construction slots

EAI_has_unlocked_building = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_has_unlocked_building > THIS=[this.getname] PREV=[prev.getname]" } 
	if = { limit = { check_variable = { _project_building_type = global.EAI_PC_INF } }

	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_AIR } }

	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_AA } }

	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_RADAR } }

		PREV = { has_tech = radio_detection }
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_MIC } }

	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_CIC } }

	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_NIC } }

	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_REF } }

		PREV = { has_tech = synth_oil_experiments }
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_SILO } }

		PREV = { has_tech = fuel_silos }
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_MTC } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_BUNKER } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_CBUNKER } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_LFACILITY } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_NFACILITY } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_AFACILITY } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_BFACILITY } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_RSITE } }

		PREV = { has_tech = experimental_rockets }
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_NBASE } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_RAIL } }

	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_HUB } }

	}
}

EAI_available_AIR = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_AIR > THIS=[this.getname] PREV=[prev.getname]" } 
	PREV = { has_full_control_of_state = PREV }

	free_building_slots = { building = air_base size > 0 include_locked = no }
	impassable = no
}

EAI_available_RADAR = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_RADAR > THIS=[this.getname] PREV=[prev.getname]" } 
	PREV = { has_full_control_of_state = PREV }

	free_building_slots = { building = radar_station size > 0 include_locked = no }
	impassable = no
}

EAI_available_INF = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_INF > THIS=[this.getname] PREV=[prev.getname]" } 
	PREV = { has_full_control_of_state = PREV }

	free_building_slots = { building = infrastructure size > 0 include_locked = no }
	impassable = no
}

EAI_available_AA = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_AA > THIS=[this.getname] PREV=[prev.getname]" } 
	PREV = { has_full_control_of_state = PREV }

	free_building_slots = { building = anti_air_building size > 0 include_locked = no }
	impassable = no
}

EAI_available_SHARED = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_SHARED > THIS=[this.getname] PREV=[prev.getname]" } 
	PREV = { has_full_control_of_state = PREV }

	EAI_has_free_shared_slot = yes
	impassable = no
}

EAI_available_REF = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_REF > THIS=[this.getname] PREV=[prev.getname]" } 
	EAI_has_free_shared_slot = yes
	impassable = no
}

EAI_available_NIC = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_NIC > THIS=[this.getname] PREV=[prev.getname]" } 
	EAI_has_free_shared_slot = yes
	impassable = no
}

EAI_available_SILO = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_SILO > THIS=[this.getname] PREV=[prev.getname]" } 
	EAI_has_free_shared_slot = yes
	impassable = no
}

EAI_available_RSITE = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_RSITE > THIS=[this.getname] PREV=[prev.getname]" } 
	EAI_has_free_shared_slot = yes
	impassable = no
}

EAI_available_LFACILITY = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_LFACILITY > THIS=[this.getname] PREV=[prev.getname]" } 
	can_construct_building = land_facility
	EAI_facility_available_for_new_project = yes
}

EAI_available_NFACILITY = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_NFACILITY > THIS=[this.getname] PREV=[prev.getname]" } 
	can_construct_building = naval_facility
	EAI_facility_available_for_new_project = yes
}

EAI_available_AFACILITY = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_AFACILITY > THIS=[this.getname] PREV=[prev.getname]" } 
	can_construct_building = air_facility
	EAI_facility_available_for_new_project = yes
}

EAI_available_BFACILITY = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_available_BFACILITY > THIS=[this.getname] PREV=[prev.getname]" } 
	can_construct_building = nuclear_facility
	EAI_facility_available_for_new_project = yes
}

EAI_facility_available_for_new_project = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_facility_available_for_new_project > THIS=[this.getname] PREV=[prev.getname]" } 

	set_temp_variable = { num_queued = 0 }
	OR = {
		any_of = { array = EAI_PC_projects_in_state_of_@PREV index = EAI_facility_available_for_new_project_i value = EAI_facility_available_for_new_project_v
			set_temp_variable = { tmp = PREV.EAI_PC_building_type^EAI_facility_available_for_new_project_v }
			if = { limit = { is_in_array = { global.EAI_PC_BUILDING_FACILITY = tmp } } add_to_temp_variable = { num_queued = 1 } }
			always = no
		}
		always = yes
	}
	check_variable = { num_queued < 1 }
}

### priority construction AI

# THIS = STATE, PREV = BUILDER, _project_building_type, _project_province_id
EAI_PC_can_build_project = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_can_build_project > THIS=[this.getname] PREV=[prev.getname] _project_building_type=[?_project_building_type] _project_province_id=[?_project_province_id]" } 

	OR = {
		AND = {
			EAI_PC_location_controlled_by_PREV = yes
		}
		AND = {
			check_variable = { PREV.EAI_PC_project_build_for_ally^_project_id = 1 }
			CONTROLLER = { ROOT = { EAI_PREV_on_same_side_with_THIS = yes } }
		}
	}

	EAI_PC_building_slot_available = yes
}

# THIS = STATE, PREV = BUILDER, _project_building_type, _project_build_for_ally, _project_province_id, _project_connect_id
EAI_PC_location_controlled_by_PREV = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_location_controlled_by_PREV > THIS=[this.getname] PREV=[prev.getname] _project_building_type=[?_project_building_type] _project_province_id=[?_project_province_id]" } 

	impassable = no

	# Province
	if = { limit = { is_in_array = { global.EAI_PC_BUILDING_PROVINCIAL = _project_building_type } }
	
		if = { limit = { check_variable = { _project_build_for_ally = 0 } }
			PREV = { controls_province = _project_province_id }
			if = { limit = { check_variable = { _project_building_type = global.EAI_PC_RAIL } }
				PREV = { controls_province = _project_connect_id }
			}
		}
		else = { # Ally project
			var:global.province_controllers^_project_province_id = { ROOT = { EAI_PREV_on_same_side_with_THIS = yes } }
			if = { limit = { check_variable = { _project_building_type = global.EAI_PC_RAIL } }
				var:global.province_controllers^_project_connect_id = { ROOT = { EAI_PREV_on_same_side_with_THIS = yes } }
			}
		}
	}

	# State
	else = {

		if = { limit = { check_variable = { _project_build_for_ally = 0 } }
			is_controlled_by = PREV
		}
		else = { # Ally project
			CONTROLLER = { ROOT = { EAI_PREV_on_same_side_with_THIS = yes } }
		}
	}
}

# THIS = STATE, PREV = BUILDER, _project_building_type, _project_province_id
EAI_PC_building_slot_available = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_building_slot_available > THIS=[this.getname] PREV=[prev.getname] _project_building_type=[?_project_building_type] _project_province_id=[?_project_province_id]" } 

	if = { limit = { check_variable = { _project_building_type = global.EAI_PC_INF } }

		free_building_slots = { building = infrastructure size > 0 include_locked = no }
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_AIR } }

		free_building_slots = { building = air_base size > 0 include_locked = no }
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_AA } }

		free_building_slots = { building = anti_air_building size > 0 include_locked = no }
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_RADAR } }

		free_building_slots = { building = radar_station size > 0 include_locked = no }
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_MIC } }

		EAI_has_free_shared_slot = yes
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_CIC } }

		EAI_has_free_shared_slot = yes
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_NIC } }

		EAI_has_free_shared_slot = yes
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_REF } }

		EAI_has_free_shared_slot = yes

		# Check that the AI has not queued any REF on this state, it should not be allowed to build PC type at the same time
		set_temp_variable = { tmp_num_ref = building_level@synthetic_refinery }
		free_building_slots = { building = synthetic_refinery size > 0 include_locked = no }
		if = { limit = { check_variable = { tmp_num_ref = 0 } } free_building_slots = { building = synthetic_refinery size > 2 include_locked = no } }
		if = { limit = { check_variable = { tmp_num_ref = 1 } } free_building_slots = { building = synthetic_refinery size > 1 include_locked = no } }
		if = { limit = { check_variable = { tmp_num_ref = 2 } } free_building_slots = { building = synthetic_refinery size > 0 include_locked = no } }
	}
	
	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_SILO } }

		EAI_has_free_shared_slot = yes
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_MTC } }

		check_variable = { building_level@arms_factory > 0 }
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_BUNKER } }

		meta_trigger = {
			text = {
				free_building_slots = { 
					building = bunker 
					size > 0
					include_locked = no 
					province = [x]
				}
			}
			x = "[?_project_province_id]"
		}
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_CBUNKER } }

		meta_trigger = {
			text = {
				free_building_slots = { 
					building = coastal_bunker 
					size > 0
					include_locked = no 
					province = [x]
				}
			}
			x = "[?_project_province_id]"
		}
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_LFACILITY } }

		EAI_PC_facility_slot_available = yes
		can_construct_building = land_facility
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_NFACILITY } }

		EAI_PC_facility_slot_available = yes
		can_construct_building = naval_facility
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_AFACILITY } }

		EAI_PC_facility_slot_available = yes
		can_construct_building = air_facility
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_BFACILITY } }

		EAI_PC_facility_slot_available = yes
		can_construct_building = nuclear_facility
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_RSITE } }

		free_building_slots = { building = rocket_site size > 0 include_locked = no }
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_NBASE } }

		meta_trigger = {
			text = {
				free_building_slots = { 
					building = naval_base
					size > 0
					include_locked = no 
					province = [x]
				}
			}
			x = "[?_project_province_id]"
		}
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_RAIL } }

		meta_trigger = {
			text = {
				check_variable = { global.EAI_PC_railway_connection_level_[x]^[y] < 5 }
			}
			x = "[?_project_province_id]"
			y = "[?_project_connect_id]"
		}
	}

	else_if = { limit = { check_variable = { _project_building_type = global.EAI_PC_HUB } }

		meta_trigger = {
			text = {
				free_building_slots = { 
					building = supply_node
					size > 0
					include_locked = no 
					province = [x]
				}
			}
			x = "[?_project_province_id]"
		}
	}
}

EAI_PC_facility_slot_available = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_facility_slot_available > THIS=[this.getname] PREV=[prev.getname]" } 

	set_temp_variable = { num_queued = 0 }
	OR = {
		any_of = { array = EAI_PC_projects_in_state_of_@PREV index = EAI_PC_facility_slot_available_i value = EAI_PC_facility_slot_available_v
			set_temp_variable = { tmp = PREV.EAI_PC_building_type^EAI_PC_facility_slot_available_v }
			if = { limit = { is_in_array = { global.EAI_PC_BUILDING_FACILITY = tmp } } add_to_temp_variable = { num_queued = 1 } }
			always = no
		}
		always = yes
	}
	check_variable = { num_queued < 2 }
}

### forts

EAI_has_naval_base = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_has_naval_base > THIS=[this.getname] PREV=[prev.getname]" } 
	any_province_building_level = {
		province = {
			all_provinces = yes
			limit_to_coastal = yes 
		}
		building = naval_base
		level > 0
	}
	any_province_building_level = {
		province = {
			all_provinces = yes
			limit_to_coastal = yes 
		}
		building = naval_base
		level < 3
	}
}

EAI_has_city = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_has_city > THIS=[this.getname] PREV=[prev.getname]" } 
	any_province_building_level = {
		province = {
			all_provinces = yes
			limit_to_victory_point = yes 
		}
		building = bunker
		level < 11
	}
}

EAI_no_border = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_no_border > THIS=[this.getname] PREV=[prev.getname]" } 
	CONTROLLER = { set_temp_variable = { tmp_controller_id = THIS.id } }
	NOT = { 
		any_neighbor_state = { 
			CONTROLLER = { 
				NOT = { 
					var:tmp_controller_id = { EAI_PREV_on_same_side_with_THIS = yes } 
				} 
			} 
		} 
	}
}

EAI_no_enemy_border = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_no_enemy_border > THIS=[this.getname] PREV=[prev.getname]" } 
	CONTROLLER = { set_temp_variable = { tmp_controller_id = THIS.id } }
	NOT = { 
		any_neighbor_state = { 
			CONTROLLER = { 
				has_war_with = var:tmp_controller_id 
			} 
		} 
	}
}

# THIS = STATE, PREV = BUILDER, _get_province_type_id
EAI_PC_province_is_coastal = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_province_is_coastal > THIS=[this.getname] PREV=[prev.getname] _get_province_type_id=[?_get_province_type_id]" }
	meta_trigger = {
		text = {
			any_province_building_level = {
				province = {
					id = [x]
					limit_to_coastal = yes 
				}
				building = bunker
				level < 11
			}
		}
		x = "[?_get_province_type_id]"
	}
}

# THIS = STATE, PREV = BUILDER, _get_province_type_id
EAI_PC_province_is_city = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_province_is_city > THIS=[this.getname] PREV=[prev.getname] _get_province_type_id=[?_get_province_type_id]" }
	meta_trigger = {
		text = {
			any_province_building_level = {
				province = {
					id = [x]
					limit_to_victory_point = yes 
				}
				building = bunker
				level < 11
			}
		}
		x = "[?_get_province_type_id]"
	}
}

# THIS = STATE, PREV = BUILDER, _get_province_type_id
EAI_PC_province_is_naval_base = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_province_is_naval_base > THIS=[this.getname] PREV=[prev.getname] _get_province_type_id=[?_get_province_type_id]" }
	meta_trigger = {
		text = {
			any_province_building_level = {
				province = {
					id = [x]
					limit_to_naval_base = yes 
				}
				building = bunker
				level < 11
			}
		}
		x = "[?_get_province_type_id]"
	}
}

# THIS = STATE, PREV = BUILDER, _get_province_type_id
EAI_PC_province_is_border = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_province_is_border > THIS=[this.getname] PREV=[prev.getname] _get_province_type_id=[?_get_province_type_id]" }
	meta_trigger = {
		text = {
			any_province_building_level = {
				province = {
					id = [x]
					limit_to_border = yes 
				}
				building = bunker
				level < 11
			}
		}
		x = "[?_get_province_type_id]"
	}
}

# THIS = STATE, PREV = BUILDER, _get_province_type_id
EAI_PC_province_is_passable = { if = { limit = { PREV = { has_country_flag = EAI_PC_trace_functions } } log = "EAI_PC_province_is_passable > THIS=[this.getname] PREV=[prev.getname] _get_province_type_id=[?_get_province_type_id]" }
	
	is_in_array = { global.EAI_MAP_PROV_LAND_TYPES = global.EAI_MAP_province_terrain_type^_get_province_type_id }
}

# THIS = STATE, PREV = BUILDER
EAI_PC_state_has_supply_hub = {
	OR = {
		any_province_building_level = {
			province = { all_provinces = yes }
			building = supply_node
			level > 0
		}
		any_province_building_level = {
			province = { all_provinces = yes }
			building = naval_base
			level > 0
		}
	}
}

# _province_hub_id
EAI_PC_prov_has_supply_hub = {
	meta_trigger = {
		text = {
			OR = {
				any_province_building_level = {
					province = { id = [x] }
					building = supply_node
					level > 0
				}
				any_province_building_level = {
					province = { id = [x] }
					building = naval_base
					level > 0
				}
			}
		}
		x = "[?_province_hub_id]"
	}
}

### air base

EAI_airbase_priority = { 
	OR = {
		AND = { # USA, JAP
			ROOT = { OR = { original_tag = USA original_tag = JAP } }
			air_base < 4
			is_island_state = yes 
			OR = { is_on_continent = asia is_on_continent = pacific }
		}

		AND = { # GER
			ROOT = { original_tag = GER }
			SOV = { EAI_is_threat = yes}
			air_base < 4
			any_neighbor_state = { CONTROLLER = { original_tag = SOV } }
			OR = { is_on_continent = europe }
		}

		AND = { # SOV
			ROOT = { original_tag = SOV }
			GER = { EAI_is_threat = yes}
			air_base < 4
			any_neighbor_state = { CONTROLLER = { original_tag = GER } }
			OR = { is_on_continent = europe }
		}
	}
}

### radar

# all existing radar locations
EAI_radar_location = { 
	OR = {
		state = 282 # tokyo
		state = 645 # iwo jima
		state = 648 # marcus
		state = 684 # caroline
		state = 646 # saipan
		state = 629 # hawaii
		state = 632 # wake
		state = 336 # singapore

		state = 631 # midway
		state = 630 # johnston

		state = 327 # manila
		state = 422 # ceylon

		state = 219 # moscow
		state = 217 # stalingrad
		state = 137 # sevastopol
		state = 195 # leningrad

		state = 116 # malta
		state = 118 # gibraltar
		state = 126 # london
		state = 446 # cairo

		state = 361 # washington
		state = 378 # san francisco
		state = 386 # seattle
		state = 366 # florida

		state = 685 # panama

		state = 60  # thuringen
		state = 5 	# ostpreussen

		state = 2 	# rome

		state = 41	# madrid

		state = 16	# paris

		state = 341 # istanbul

		state = 331 # new foundland
		state = 696 # bermuda

		state = 100 # iceland

		state = 698 # azores

		state = 801 # gambia (for ASW)
		state = 701 # sierra leone (for ASW)

		radar_station > 1

		has_state_flag = EAI_radar_site
	}
}

# good radar locations
EAI_radar_location_2 = {
	OR = {
		state = 282 # tokyo
		state = 645 # iwo jima
		state = 648 # marcus
		state = 684 # caroline
		state = 646 # saipan
		state = 629 # hawaii
		state = 632 # wake
		state = 336 # singapore

		state = 631 # midway
		state = 630 # johnston

		state = 327 # manila
		state = 422 # ceylon

		state = 219 # moscow
		state = 217 # stalingrad
		state = 137 # sevastopol
		state = 195 # leningrad

		state = 116 # malta
		state = 118 # gibraltar
		state = 126 # london
		state = 446 # cairo

		state = 361 # washington
		state = 378 # san francisco
		state = 386 # seattle
		state = 366 # florida

		state = 685 # panama

		state = 60  # thuringen
		state = 5 	# ostpreussen

		state = 2 	# rome

		state = 41	# madrid

		state = 16	# paris

		state = 341 # istanbul

		state = 331 # new foundland
		state = 696 # bermuda

		state = 100 # iceland

		state = 698 # azores

		state = 801 # gambia (for ASW)
		state = 701 # sierra leone (for ASW)
	}
}

### region priorities

# prioritise building in a certain area
EAI_region_priority = { 
	OR = {
		AND = { PREV = { NOT = { original_tag = SOV original_tag = CHI } } is_in_home_area = yes }

		AND = { PREV = { original_tag = SOV } EAI_central_russia = yes }

		AND = { PREV = { original_tag = CHI } EAI_western_china = yes }
	}
}

EAI_central_russia = {
	# OR = {
	# 	region = 149
	# 	region = 152
	# 	region = 134
	# 	region = 135
	# 	region = 147
	# 	region = 14
	# 	region = 151
	# 	region = 136
	# 	region = 138
	# 	region = 150
	# 	region = 40
	# 	region = 137
	# 	region = 133
	# }
	NOT = {
		region = 39
		region = 130
		region = 131
		region = 132
		region = 37
		region = 13
		region = 265
	}
}

EAI_western_china = {
	OR = { region = 164 region = 165 region = 144 region = 145 }
	NOT = { is_coastal = yes }
}