# see README.txt for example

@position_offset_x = -14
@position_offset_y = -14
#these polar coordinates define the same point as position_offset above
@polar_offset_angle = 45
@polar_offset_radius = 19.799	# sqrt( 14^2 + 14^2 )
@plane_offset = 5

cosmogenesis_world_0 = {
	entity = ""
	construction_entity = "neural_calculator_phase_1_entity"
	portrait = "GFX_megastructure_construction_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = @plane_offset
	build_time = 2400

	resources = {
		category = megastructures
		cost = {
			influence = 300
		}
		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
		}
	}

	construction_blocks_and_blocked_by = self_type
	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_ONE_PER_COUNTRY"

	prerequisites = { "tech_cosmogenesis_world" }

	possible = {
		inline_script = megastructures/system_ownership_or_waystation_check
		custom_tooltip = {
			fail_text = "requires_no_existing_cosmogenesis_world"
			from = {
				NOT = { has_country_flag = cosmogenesis_world_built }
			}
		}
		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 = cosmogenesis_aborted_tt
			from = { NOT = { has_country_flag = cosmogenesis_aborted } }
		}
	}

	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 = {
						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_orbital_debris"
				NOR = {
					has_carrier_flag = has_orbital_debris
				}
			}

			# 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_star"
				is_star = no
			}
			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

		# Non-nomads need an owned starport-sized starbase in the system...
		modifier = {
			factor = 0
			from = { is_nomadic = no }
			starbase = { NOT = { has_starbase_size >= starbase_starport } }
		}
		# ...nomads use a tier-2 waystation on their network instead (mirrors system_ownership_or_waystation_check).
		modifier = {
			factor = 0
			from = { is_nomadic = yes }
			NOT = {
				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 }
					}
				}
			}
		}

		modifier = {
			factor = 0
			any_system_planet = {
				is_planet_class = pc_habitat
				is_colony = no
			}
		}

		modifier = {
			factor = 3
			solar_system = {
				exists = owner
				is_same_value = owner.capital_scope.solar_system
			}
		}
		modifier = {
			factor = 2
			exists = sector
			sector = { has_sector_type = core_sector }
		}
	}

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		from = { set_country_flag = cosmogenesis_world_built }
		spawn_planet = {
			class = "pc_cosmogenesis_world"
			location = fromfrom.planet
			orbit_location = yes
			orbit_angle_offset = @polar_offset_angle
			orbit_distance_offset = @polar_offset_radius
			plane_offset = @plane_offset
			size = 3 #One is used for a hidden government district.
			has_ring = no

			init_effect = {
				set_name = {
					key = pc_cosmogenesis_world
				}
				set_planet_entity = {
					entity = "neural_calculator_phase_1_entity"
				}
				set_surveyed = {
					surveyed = yes
					surveyor = FROM
				}
				set_all_comms_surveyed = yes
				clear_blockers = yes

				set_planet_flag = megastructure
				set_carrier_flag = cosmogenesis_world_resettle@from

				fromfrom.planet = {
					set_planet_flag = has_megastructure
					set_planet_flag = has_cosmogenesis_world
					if = {
						limit = { has_orbital_station = yes }
						orbital_station = {
							dismantle = yes
						}
					}
				}

				set_owner = from
				set_controller = from
				set_planet_purge_type = purge_cosmogenesis # see crisis.301
				create_pop_group = {
					species = owner_main_species
				}
				from = {
					room_name_override = ""
				}
				if = {
					limit = {
						from = {
							has_country_flag = cosmogenesis_aborted
						}
					}
					spawn_lathe_cracker_effect = yes
				}
			}
		}
		remove_megastructure = fromfrom
		solar_system = {
			random_system_megastructure = {
				limit = {
				 	is_megastructure_type = cosmogenesis_world_ruined
				}
				remove_megastructure = this
			}
		}
	}
}

# Ruined Version
cosmogenesis_world_ruined = {
	entity = "neural_calculator_destroyed_entity"
	construction_entity = "neural_calculator_destroyed_entity"
	portrait = "GFX_megastructure_construction_background"
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	plane_offset = @plane_offset

	potential = {
		always = no
	}
}
