# see README.txt for example

@central_orbital_build_time = 1800
@central_orbital_alloy_cost = 1500
@central_orbital_influence_cost = 200

# Habitat Central Complex
habitat_central_complex = {
	entity = "habitat_phase_03_entity"
	construction_entity = "habitat_phase_03_entity"
	portrait = "GFX_megastructure_habitat_background"
	place_entity_on_planet_plane = no
	show_galactic_map_icon = no
	hide_name = yes
	show_in_outliner = no
	entity_offset = { x = 7 y = -7 }
	build_time = @central_orbital_build_time

	resources = {
		category = megastructures_habitat
		cost = {
			influence = @central_orbital_influence_cost
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = @central_orbital_alloy_cost
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = @central_orbital_alloy_cost
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = @central_orbital_alloy_cost
			mult = @halved_alloy_to_food_cost_ratio
		}
	}

	construction_blocks_and_blocked_by = self_type
	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_HABITAT_CENTRAL_COMPLEX"
	build_system_tooltip = habitat_tooltip
	tooltip_system_score = {
		base = 0
		add = value:max_habitat_districts_value
	}
	tooltip_best_systems_header = "MEGASTRUCTURE_HABITAT_BEST_SYSTEMS_TOOLTIP_HEADER"
	tooltip_system_filter = {
		NOT = {
			OR = {
				any_system_planet = {
					OR = {
						has_planet_flag = habitat
						has_planet_flag = hold_the_line_habitat
					}
				}
				has_megastructure = habitat_central_complex_ruined
			}
		}
	}
	tooltip_system_score_low_threshold = 10
	tooltip_system_score_high_threshold = 16

	potential = {
		OR = {
			has_technology = tech_habitat_1
			has_civic = civic_diadochi
			has_civic = civic_great_khans_legacy
			has_country_flag = democratic_khanate_flag
		}
	}

	possible = {
		hidden_trigger = {
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
		custom_tooltip = {
			fail_text = "requires_not_habitat_central_complex"
			NOT = {
				OR = {
					any_system_planet = {
						OR = {
							has_planet_flag = habitat
							has_planet_flag = hold_the_line_habitat
						}
					}
					has_megastructure = habitat_central_complex_ruined
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_megastructure"
			NOT = {
				any_system_megastructure = {
					is_megastructure_type = ring_world_ruined
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_orbital_debris"
			NOT = {
				any_system_planet = {
					has_carrier_flag = has_orbital_debris
				}
			}
		}

		# The AI has a hard cap on number of habitats scaling with galaxy size and difficulty
		hidden_trigger = {
			from = {
				if = {
					limit = {
						is_ai = yes
					}
					count_planet_within_border = {
						count <= value:ai_habitat_cap
						limit = {
							is_planet_class = pc_habitat
						}
					}
				}
			}
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_surveyed_planet"
				is_surveyed = {			# prevent leaking habitability information
					who = prev.from
					status = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = { has_anomaly = yes }
			}
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				#can_build_megastructure_on_planet = yes
				NOR = {
					has_planet_flag = megastructure
					has_planet_flag = has_megastructure
					solar_system = {
						OR = {
							has_star_flag = ring_world_built
							has_star_flag = ithomes_gate
						}
					}
					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
					is_planet_class = pc_cosmogenesis_world
				}
			}

			# balance for habitats
			custom_tooltip = {
				fail_text = "requires_not_minor_planetary_body"
				NOR = {
					is_asteroid = yes
					is_moon = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_not_astral_scar"
				is_astral_scar = no
			}
			custom_tooltip = {
				fail_text = "requires_not_solarpunk"
				NOT = {
					solar_system = {
						has_star_flag = solarpunk_system_02
					}
				}
			}
			if = {
				limit = {
					from = { is_ai = yes }
				}
				or = {
					has_deposit_for = shipclass_mining_station
					has_deposit_for = shipclass_research_station
				}
			}
		} # use these for all non-star megastructures
	}

	# root = system
	# from = country
	ai_weight = {
		factor = 5

		modifier = {
			# AI shouldn't build habitats if they have any uncolonised habitats.
			factor = 0
			owner = {
				any_planet_within_border = {
					is_planet_class = pc_habitat
					is_colony = no
				}
			}
		}

		modifier = {
			# AI shouldn't build habitats if they don't have starports in the system
			# unless they have either the Void Dwellers origin or Voidborn AP.
			factor = 0
			owner = {
				is_void_dweller_empire = no
			}
			starbase = { NOT = { has_starbase_size >= starbase_starport } }
		}

		modifier = {
			factor = 5
			exists = sector
			sector = { has_sector_type = core_sector }
		}

		modifier = {
			factor = 100
			OR = {
				has_star_flag = ideal_habitat_t1
				has_star_flag = ideal_habitat_t2
			}
		}

		modifier = {
			factor = value:num_orbital_sites
		}
	}

	on_build_complete = {
		fromfrom.planet = {
			save_event_target_as = target_planet
		}
		if = {
			limit = {
				fromfrom.planet = {
					is_star = yes
				}
			}
			spawn_habitat_effect = {
				HABITAT_OWNER = from
				TARGET_PLANET = event_target:target_planet
				DISTANCE = 19.798
			}
		}
		else = {
			spawn_habitat_effect = {
				HABITAT_OWNER = from
				TARGET_PLANET = event_target:target_planet
				DISTANCE = 9.899
			}
		}

		fromfrom.solar_system = {
			set_star_flag = has_habitat
		}
		from = {
			save_event_target_as = habitat_owner
		}
		remove_megastructure = fromfrom
		from = { country_event = { id = megastructures.10 } }
	}
}

# Ruined Habitat Central Complex
habitat_central_complex_ruined = {
	entity = "habitat_destroyed_01_entity"
	construction_entity = "habitat_destroyed_01_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	show_in_outliner = no
	entity_offset = { x = 7 y = -7 }

	potential = {
		always = no
	}
}

# Restored Habitat Central Complex
habitat_central_complex_restored = {
	entity = "habitat_phase_03_entity"
	construction_entity = "habitat_phase_03_entity"
	portrait = "GFX_megastructure_habitat_background"
	place_entity_on_planet_plane = no
	show_galactic_map_icon = no
	hide_name = yes
	show_in_outliner = no
	entity_offset = { x = 7 y = -7 }
	build_time = @central_orbital_build_time

	resources = {
		category = megastructures_habitat
		cost = {
			influence = @central_orbital_influence_cost
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = @central_orbital_alloy_cost
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = @central_orbital_alloy_cost
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = @central_orbital_alloy_cost
			mult = @halved_alloy_to_food_cost_ratio
		}

	}

	upgrade_from = {
		habitat_central_complex_ruined
	}

	possible = {
		from = { has_technology = tech_habitat_1 }
	}

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

	on_build_complete = {
		solar_system = {
			random_system_planet = {
				limit = {
					has_planet_flag = habitat_build_site
				}
				save_event_target_as = target_planet
			}
		}
		if = {
			limit = {
				event_target:target_planet = {
					is_star = yes
				}
			}
			spawn_habitat_effect = {
				HABITAT_OWNER = from
				TARGET_PLANET = event_target:target_planet
				DISTANCE = 19.798
			}
		}
		else = {
			spawn_habitat_effect = {
				HABITAT_OWNER = from
				TARGET_PLANET = event_target:target_planet
				DISTANCE = 9.899
			}
		}
		fromfrom.solar_system = {
			set_star_flag = has_habitat
		}

		from = {
			save_event_target_as = habitat_owner
		}
		solar_system = {
			spawn_orbital_effect = {
				TYPE = major
				HABITAT_OWNER = event_target:habitat_owner
				TARGET_PLANET = event_target:target_planet
			}
		}
		remove_megastructure = fromfrom
		from = { country_event = { id = megastructures.10 } }
		# Fixing the name for the adSalivul habitat
		if = {
			limit = {
				solar_system = {
					has_star_flag = adSalivul_system
				}
			}
			event_target:target_habitat = {
				set_name = NAME_Patriotic_Institute_of_Exalted_Benevolence
			}
		}
	}
}

# Major Orbital
habitat_major_orbital = {
	entity = "habitat_phase_02_entity"
	construction_entity = "habitat_phase_02_entity"
	portrait = "GFX_megastructure_habitat_background"
	place_entity_on_planet_plane = no
	show_galactic_map_icon = no
	use_planet_resource = yes
	hide_name = yes
	show_in_outliner = no
	entity_offset = { x = -7 y = -7 }
	build_time = 900

	construction_blocks_and_blocked_by = none

	potential = {
		always = no
	}

	possible = {
		always = no
	}

	placement_rules = {
	}

	# root = system
	# from = country
	ai_weight = {
		factor = 0
	}

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		fromfrom.planet = {
			set_carrier_flag = has_orbital
			if = {
				limit = { has_orbital_station = yes }
				orbital_station = {
					dismantle = yes
				}
			}
		}
	}
}

# Minor Orbital
habitat_minor_orbital = {
	entity = "habitat_phase_01_entity"
	construction_entity = "habitat_phase_01_entity"
	portrait = "GFX_megastructure_habitat_background"
	place_entity_on_planet_plane = no
	show_galactic_map_icon = no
	use_planet_resource = yes
	hide_name = yes
	show_in_outliner = no
	entity_offset = { x = -7 y = -7 }
	build_time = 900

	construction_blocks_and_blocked_by = none

	potential = {
		always = no
	}

	possible = {
		always = no
	}

	placement_rules = {
	}

	# root = system
	# from = country
	ai_weight = {
		factor = 0
	}

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		fromfrom.planet = {
			set_carrier_flag = has_orbital
			if = {
				limit = { has_orbital_station = yes }
				orbital_station = {
					dismantle = yes
				}
			}
		}
	}
}
