waystation_megastructure = {
	entity = ""
	construction_entity = starbase_starport_section_entity
	portrait = "GFX_megastructure_construction_background"
	order_icon = "GFX_fleet_order_button_build_starbase_station"
	place_entity_on_planet_plane = no
	build_type = outside_gravity_well
	show_in_outliner = no
	use_planet_resource = no
	build_time = @waystation_build_time_180

	starbase = starbase_level_waystation_1

	resources = {
		category = megastructures_waystations
		cost = {
			influence = @waystation_cost_influence_100
		}
		cost = {
			trigger = {
				is_subject = yes
				any_agreement = {
					has_term_value = {
						term = subject_expand
						value = subject_can_expand_with_tithe
					}
				}
			}
			influence = @waystation_cost_influence_tithe_50
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = @waystation_cost_alloys_250
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = @waystation_cost_alloys_250
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = @waystation_cost_alloys_250
			mult = @halved_alloy_to_food_cost_ratio
		}
	}

	construction_blocks_and_blocked_by = self_type

	prerequisites = { "tech_waystation_1" }

	potential = {
		is_nomadic = yes
		# AI Arkships may only build waystations if all contracts are Build Waystation,
		# or if no constructor ships exist to do it instead.
		if = {
			limit = {
				is_ai = yes
				from = { is_arkship_fleet = yes }
			}
			NAND = {
				any_owned_contract = {
					NOT = { is_mission_type = contract_build_waystation }
				}
				any_owned_fleet = {
					any_owned_ship = { is_ship_size = nomads_constructor }
				}
			}
		}
	}

	possible = {
		# custom_tooltip = {
		# 	fail_text = "Requires Deposits in system."
			any_system_planet = {
				has_deposit = yes
			}
		# }
		custom_tooltip = {
			fail_text = "requires_no_expulsion_truce"
			if = {
				limit = {
					exists = owner
				}
				NOT = {
					from = {
						has_truce = root.owner
						has_country_flag = expelled_from_@root.owner
					}
				}
			}
		}
		if = {
			limit = {
				exists = owner
				from = {
					has_communications = root.owner
				}
			}
			custom_tooltip = {
				fail_text = "requires_intel_waystation"
				OR = {
					# Build Waystation contract allows you to ignore Intel requirements
					is_point_of_interest = {
						id = build_waystation_poi@root
						event_chain = contract_build_waystation_chain
						owner = from
					}
					from = {
						OR = {
							has_subject = root.owner
							has_overlord = root.owner
							AND = {
								is_subject = yes
								overlord? = {
									has_subject = root.owner
								}
							}
							has_intel = {
								who = root.owner
								intel = intel_economy_systems_high
							}
							has_stale_intel = {
								who = root.owner
								intel = intel_economy_systems_high
							}
						}
					}
				}
			}
		}
		else = {
			custom_tooltip = {
				fail_text = "requires_surveyed_system"
				OR = {
					# Build Waystation contract allows you to ignore Intel requirements
					is_point_of_interest = {
						id = build_waystation_poi@root
						event_chain = contract_build_waystation_chain
						owner = from
					}
					NOT = {
						any_system_planet = {
							is_surveyed = {
								who = prev.from
								status = no
							}
						}
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_waystations"
			NOT = {
				any_fleet_in_system = {
					is_waystation_ship = yes
				}
			}
		}
		custom_tooltip = {
			fail_text = "waystation_cannot_expand"
			from = {
				OR = {
					NOT = { is_subject = yes }
					NOT = {
						any_agreement = {
							has_term_value = {
								term = subject_expand
								value = subject_cannot_expand
							}
						}
					}
				}
			}
		}
	}

	context_menu_potential = {
		NOT = {
			any_fleet_in_system = {
				is_waystation_ship = yes
			}
		}
	}
	build_menu_potential = {
		NOT = { is_ship_class = shipclass_constructor }
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "BUILD_MEGASTRUCTURE_PLANET_TARGET_NEEDED"	# these loc lines are inverted
				always = no
			}
		}
	}

	# root = system
	# from = country
	ai_weight = {
		# Prefer systems with more orbital resource sites (energy, mining, research deposits)
		modifier = {
			add = value:num_orbital_sites
		}

		factor = 5

		# Do not build when hostile fleets are present in the system
		modifier = {
			factor = 0
			any_fleet_in_system = {
				owner? = {
					is_hostile = root.from
				}
			}
		}

		# Completely block when at 200% starbase capacity
		modifier = {
			factor = 0
			from = {
				used_starbase_capacity_percent >= 2.0
			}
		}

		# Completely block when at 50% starbase capacity within first 10 years
		modifier = {
			factor = 0
			years_passed <= 10
			from = {
				used_starbase_capacity_percent >= 0.5
			}
		}

		# Completely block when at 125% starbase capacity within first 20 years
		modifier = {
			factor = 0
			years_passed <= 20
			from = {
				used_starbase_capacity_percent >= 1.25
			}
		}

		# Very strongly discourage building when over 150% starbase capacity
		modifier = {
			factor = 0.1
			from = {
				used_starbase_capacity_percent >= 1.50
			}
		}

		# Strongly discourage building when over 125% starbase capacity
		modifier = {
			factor = 0.1
			from = {
				used_starbase_capacity_percent >= 1.25
			}
		}

		# Reduce desire when already at or above capacity
		modifier = {
			factor = 0.5
			from = {
				used_starbase_capacity_percent >= 1
			}
		}

		# Prefer systems adjacent to existing waystations (extend the wayline network)
		modifier = {
			factor = 3
			any_neighbor_system = {
				any_fleet_in_system = {
					is_waystation_ship = yes
					owner = {
						is_same_value = root.from
					}
				}
			}
		}

		# Bonus for systems with colonies (trade module benefit)
		modifier = {
			factor = 1.5
			from = {
				has_technology = tech_space_trading
			}
			any_system_planet = {
				is_colony = yes
			}
		}

		# Bonus for nebula systems (nebula refinery)
		modifier = {
			factor = 1.5
			from = {
				has_technology = tech_space_mining_1
			}
			solar_system = {
				is_inside_nebula = yes
			}
		}

		# Bonus for dimensional objects (dimensional shrine potential)
		modifier = {
			factor = 1.25
			from = {
				is_dimensional_worship_empire = yes
			}
			OR = {
				has_natural_wormhole = yes
				has_astral_rift = yes
				has_star_flag = lgate
				has_star_flag = lcluster_lgate
				has_star_flag = spawned_shroud_tunnel
			}
		}

		# Avoid systems adjacent to hostile empires (non-pirate empires)
		modifier = {
			factor = 0.25
			from = {
				NOT = {
					has_valid_civic = civic_void_reavers
				}
			}
			any_neighbor_system = {
				owner? = {
					NOT = {
						is_same_value = root.from
					}
					OR = {
						is_at_war_with = root.from
						is_rival = root.from
					}
				}
			}
		}

		# Reduce weight for systems owned by empires we don't have good relations with (non-pirate empires)
		modifier = {
			factor = 0.5
			from = {
				NOT = {
					has_valid_civic = civic_void_reavers
				}
			}
			owner? = {
				NOT = {
					is_same_value = root.from
				}
				NOT = {
					benefits_from_waystation_network = {
						NOMAD = root.from
						SYSTEM_OWNER = this
					}
				}
			}
		}

		# Pirate empires: prefer systems with high-development colonies owned by others
		modifier = {
			factor = 3
			from = {
				has_valid_civic = civic_void_reavers
			}
			owner? = {
				NOT = {
					is_same_value = root.from
				}
			}
			any_system_planet = {
				is_colony = yes
				pop_amount >= 2500
			}
		}

		# Significant bonus for systems with enclaves (enclave module potential)
		modifier = {
			factor = 1.75
			has_star_flag = enclave
			any_fleet_in_system = {
				owner? = {
					is_enclave_country = yes
				}
			}
		}

		# Strongly prefer the target system of a Build Waystation contract
		modifier = {
			factor = 100
			is_point_of_interest = {
				id = build_waystation_poi@root
				event_chain = contract_build_waystation_chain
				owner = from
			}
		}
	}

	on_build_complete = {
		prev = {
			fleet = {
				set_name = {
					key = WAYSTATION_NAME
					variable_string = "[root.GetName]"
				}
				every_owned_ship = {
					set_name = {
						key = WAYSTATION_NAME
						variable_string = "[root.GetName]"
					}
				}
				if = {
					limit = {
						has_cloaking_strength > 0
					}
					set_cloaking_active = yes
				}
			}
		}
		fromfrom = {
			remove_megastructure = this
		}
		fire_on_action = {
			on_action = on_waystation_built
			scopes = {
				from = from
				fromfrom = prev
			}
		}
		from = {
			# Tithe to the Overlord if there is a Restricted Expansion agreement in place
			if = {
				limit = {
					is_subject = yes
					any_agreement = {
						has_term_value = {
							term = subject_expand
							value = subject_can_expand_with_tithe
						}
					}
				}
				overlord? = {
					add_resource = {
						influence = @waystation_cost_influence_tithe_50
					}
				}
			}
			if = {
				limit = {
					NOT = { has_country_flag = waystation_built }
				}
				set_country_flag = waystation_built
			}
		}
	}
}
