# Grand Archive

@entity_x				= -16
@entity_y				= 4

grand_archive_0 = {
	entity = "grand_archive_entity"
	construction_entity = "grand_archive_entity"
	portrait = "GFX_megastructure_grand_archive_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	is_grand_archive = yes
	build_time = 1800
	resources = {
		category = megastructures_grand_archive
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 1000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 500
			food = 500
		}

		upkeep = {
			energy = 10
		}
	}

	construction_blocks_and_blocked_by = self_type
	prerequisites = { "tech_galactic_archivism" }
	show_prereqs = yes

	potential = {
		is_country_type = default
		has_grand_archive = no
	}

	possible = {
		if = {
			limit = {
				from = {
					is_nomadic = no
				}
			}
			hidden_trigger = {
				exists = starbase
			}
			custom_tooltip = {
				fail_text = "requires_inside_border"
				is_inside_border = from
			}
			custom_tooltip = {
				fail_text = must_have_2500_pops
				OR = {
					from = {
						pop_amount >= 2500
					}
					from.owner = {
						is_wilderness_empire = yes
					}
				}
			}
		}
		else = {
			custom_tooltip = {
				fail_text = requires_tier_2_waystation_on_wayline
				any_starbase_in_system = {
					is_waystation_starbase = yes
					owner? = {
						is_same_value = from
					}
					any_starbase_in_network = {
						has_starbase_size >= starbase_waystation_2
						owner? = {
							is_same_value = from
						}
					}
				}
			}
		}
		inline_script = megastructures/system_surveyed_or_waystation_check
	}

	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
				}
			}
		} # use these for all non-star megastructures
	}

	country_modifier = {
		country_vivarium_max_capacity_add = @grand_archive_vivarium_capacity
	}

	# Root = System
	# From = Country
	ai_weight = {
		factor = 5
		modifier = {
			factor = 3
			from = {
				acquired_specimen_count > 6
			}
		}
		modifier = {
			factor = 0
			from = {
				is_unfriendly = yes
			}
		}
	}

	on_build_complete = {
		from = {
			country_event = {
				id = grand_archive.8090
				scopes = {
					from = fromfrom
					fromfrom = prev
					fromfromfrom = fromfromfrom
				}
			}
			if = {
				limit = {
					has_country_flag = grand_archive_destroyed
				}
				remove_country_flag = grand_archive_destroyed
				country_event = {
					id = grand_archive.4580
					scopes = { from = fromfrom.solar_system }
				}
			}
		}
		fromfrom.planet = {
			set_planet_flag = has_megastructure
		}
	}
}
