
@nsc_flagship_entity_offset_x = -20
@nsc_flagship_entity_offset_y = -20

nsc_flagship_shipyard = {
	entity = "flagship_construction_yard_entity"
	construction_entity = "flagship_construction_yard_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @nsc_flagship_entity_offset_x y = @nsc_flagship_entity_offset_y }
	build_time = 360
	resources = {
		category = megastructures
		cost = {
			alloys = 1000
			influence = 250
		}

		upkeep = {
			energy = 1
		}
	}

	prerequisites = { "tech_Flagship_1" }

	possible = {
		if = {
			limit = {
				from = {
					is_nomadic = no
				}
			}
			hidden_trigger = {
				exists = starbase
			}
			custom_tooltip = {
				fail_text = "requires_inside_border"
				is_inside_border = from
			}
		}
		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
						}
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_nsc_grand_shipyard"
			from = { NOT = { has_country_flag = built_nsc_grand_shipyard } }
		}
		custom_tooltip = {
			text = "requires_no_existing_nsc_flagship"
			from = { NOT = { any_controlled_ship = { is_ship_size = Flagship } } }
		}
		custom_tooltip = {
			text = "can_not_start_another_nsc_flagship_building"
			from = { NOT = { has_country_flag = currently_building_nsc_flagship } }
		}
		custom_tooltip = {
			fail_text = "requires_surveyed_system"
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prev.from
						status = no
					}
				}
			}
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_surveyed_planet"
				is_surveyed = {
					who = prev.from
					status = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				NOR = {
					has_carrier_flag = megastructure
					has_planet_flag = has_megastructure
					solar_system = { has_star_flag = ring_world_built }
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_ringworld_habitable_damaged
					is_planet_class = pc_ringworld_tech
					is_planet_class = pc_ringworld_tech_damaged
					is_planet_class = pc_ringworld_seam
					is_planet_class = pc_ringworld_seam_damaged
					is_planet_class = pc_habitat
				}
			}
			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_ring_world"
				has_ringworld_output_boost = no
			}
		}
	}

	# from = country
	ai_weight = {
		factor = 0 #no, they will get the ship via an event chain
	}

	on_build_start = {}
	on_build_cancel = {}
	
	on_build_complete = {
		from = { 
			set_country_flag = currently_building_nsc_flagship
			set_country_flag = built_nsc_grand_shipyard
		}
		fromfrom.planet = {
			if = {
				limit = { has_orbital_station = yes }
				orbital_station = {
					dismantle = yes
				}
			}
		}
	}
}

nsc_flagship_shipyard_complete = {
	entity = "flagship_construction_yard_entity"
	construction_entity = "flagship_construction_yard_entity"
	portrait = GFX_megastructure_construction_background
	place_entity_on_planet_plane = no
	show_in_outliner = no
	entity_offset = { x = @nsc_flagship_entity_offset_x y = @nsc_flagship_entity_offset_y }
	build_time = 250
	resources = {
		category = megastructures
		cost = {
			alloys = 2000
			unity = 2000
		}
		produces = {
			unity = 50
			influence = 5
		}
		upkeep = {
			energy = 2
		}
	}

	upgrade_from = {
		nsc_flagship_shipyard
	}

	prerequisites = { "tech_Flagship_1" }

	dismantle_cost = {
		category = megastructures
		cost = {
			energy = 500
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_Flagship_1
		}
	}

	on_dismantle_complete = {
		from = {
			add_resource = {
				alloys = 1000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}

	station_modifier = {
		starbase_shipyard_capacity_add = 15
		starbase_shipyard_build_speed_mult = 0.70
	}

	on_build_complete = { 
		from = { 
			country_event = { id = nsc_events_flagship.3 } 
		} 
	}
}