@phase_0_entity_x = -15
@phase_0_entity_y = -5
@entity_x = -15
@entity_y = -15
spy_orb_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @phase_0_entity_x y = @phase_0_entity_y }
	build_time = 1800
	resources = {
		category = megastructures
		cost = {
			unity = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 5000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 5000
			mult = @halved_alloy_to_food_cost_ratio
		}


		upkeep = {
			energy = 20
		}
	}

	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_ONE_PER_COUNTRY"

	prerequisites = { "tech_sentry_array" }

	possible = {
		hidden_trigger = {
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_megastructure"
			has_no_non_gate_megastructure = yes
			NOT = {
				any_system_planet = {
					is_planet_class = pc_cosmogenesis_world
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_surveyed_system"
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prev.from
						status = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_spyorb"
			from = {
				OR = {
					has_relic = r_contingency_core
					NOT = { has_country_flag = built_spyorb }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_spyorb_two"
			from = { NOT = { has_country_flag = built_spyorb_contingency } }
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_survey_not_habitable"
				is_surveyed = {			# prevent leaking habitability information
					who = prev.from
					status = yes
				}
				colonizable_planet = no
			}
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = { has_anomaly = yes }
			}
			custom_tooltip = {
				fail_text = "requires_not_minor_planetary_body"
				NOR = {
					is_asteroid = yes
					is_moon = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_not_star"
				is_star = no
			}
			custom_tooltip = {
				fail_text = "requires_not_astral_scar"
				is_astral_scar = no
			}
			custom_tooltip = {
				fail_text = "requires_not_ring_world"
				is_ringworld = no
			}
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				NOT = { has_planet_flag = megastructure }
			}
		}
	}

	# root = system
	# from = country
	ai_weight = {
		factor = 10
		modifier = {
			factor = 0.1
			starbase = { NOT = { has_starbase_size >= starbase_starfortress } }
		}

		modifier = {
			factor = 0.1
			any_neighbor_system = {
				OR = {
					NOT = { exists = owner }
					AND = {
						exists = owner
						NOT = { owner = { is_same_value = from } }
					}
				}
			}
		}

		modifier = {
			factor = 2.0
			from = {
				is_militarist = yes
			}
		}
	}

	on_build_start = {}
	on_build_cancel = {}
	on_build_complete = {
		set_star_flag = spy_orb_built
		from = {
			if = {
				limit = {
					has_relic = r_contingency_core
					has_country_flag = built_spyorb
				}
				set_country_flag = built_spyorb_contingency
			}
			set_country_flag = built_spyorb
		}
	}
}

spy_orb_1 = {
	entity = "spyorb_phase_01_entity"
	construction_entity = "spyorb_phase_01_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 1800
	resources = {
		category = megastructures
		cost = {
			unity = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 10000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 40
		}
	}

	upgrade_from = {
		spy_orb_0
	}

	prerequisites = { "tech_sentry_array" }

	sensor_range = 20
	hyperlane_range = 30

	country_modifier = {
		add_base_country_intel = 10
		ship_windup_mult = -0.05
	}
}

spy_orb_2 = {
	entity = "spyorb_phase_02_entity"
	construction_entity = "spyorb_phase_02_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 1800
	resources = {
		category = megastructures
		cost = {
			unity = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 10000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 60
		}
	}

	upgrade_from = {
		spy_orb_1
	}

	prerequisites = { "tech_sentry_array" }

	sensor_range = 40
	hyperlane_range = 60

	country_modifier = {
		add_base_country_intel = 20
		ship_windup_mult = -0.1
		intel_decryption_add = 1
	}
}


spy_orb_3 = {
	entity = "spyorb_phase_03_entity"
	construction_entity = "spyorb_phase_03_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 1800
	resources = {
		category = megastructures
		cost = {
			unity = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 10000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 80
		}
	}
	upgrade_from = {
		spy_orb_2
	}

	prerequisites = { "tech_sentry_array" }

	sensor_range = 60
	hyperlane_range = 90

	country_modifier = {
		add_base_country_intel = 30
		ship_windup_mult = -0.15
		intel_decryption_add = 1
	}
}

spy_orb_4 = {
	entity = "spyorb_phase_04_entity"
	construction_entity = "spyorb_phase_04_construction_entity"
	portrait = "GFX_megastructure_spy_orb_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 1800
	resources = {
		category = megastructures
		cost = {
			unity = 2500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 10000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 100
		}
	}
	upgrade_from = {
		spy_orb_3
	}

	on_build_complete = {
		from = {
			set_country_flag = has_built_or_repaired_megastructure
		}
	}

	prerequisites = { "tech_sentry_array" }
	show_prereqs = yes

	country_modifier = {
		add_base_country_intel = 40
		ship_windup_mult = -0.2
		intel_decryption_add = 2
	}

	# effect implementation in game_rules (has_full_sensor_range)
}

# Permanently Ruined Sentry Array
spy_orb_permanently_ruined = {
	entity = "spy_orb_destroyed_entity"
	portrait = "GFX_megastructure_construction_background"
	show_galactic_map_icon = no
	show_in_outliner = no

	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }

	potential = {
		always = no
	}
}

# Ruined Sentry Array
spy_orb_ruined = {
	entity = "spy_orb_destroyed_entity"
	portrait = "GFX_megastructure_construction_background"

	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }

	potential = {
		always = no
	}
}

# Restored Sentry Array
spy_orb_restored = {
	entity = "spyorb_phase_04_entity"
	construction_entity = "spyorb_phase_04_construction_entity"
	portrait = "GFX_megastructure_spy_orb_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 4800
	resources = {
		category = megastructures
		cost = {
			unity = 3500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 15000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 15000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 15000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 100
		}
	}

	upgrade_from = {
		spy_orb_ruined
	}

	possible = {
		from = {
			has_technology = tech_mega_engineering
		}
	}

	on_build_start = {
		fromfrom = {
			set_graphical_culture = root.from
		}
	}

	on_build_complete = {
		every_system_ambient_object = {
			limit = {
				OR = {
					is_ambient_object_type = large_debris_object
					is_ambient_object_type = medium_debris_01_object
					is_ambient_object_type = medium_debris_02_object
					is_ambient_object_type = small_debris_object
				}
			}
			destroy_ambient_object = this
		}
		owner = {
			set_country_flag = has_built_or_repaired_megastructure
			country_event = { id = utopia.2015 }
		}
	}

	country_modifier = {
		add_base_country_intel = 40
		ship_windup_mult = -0.2
		intel_decryption_add = 2
	}
}
