# see README.txt for example

ring_world_1 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = -7 y = -7 }
	build_time = 1800
	resources = {
		category = megastructures_oeo_megastructures
		cost = {
			influence = 500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 5000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 5000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			trade = @oeo_ring_1_trade_upkeep
		}
	}
	custom_tooltip_requirements = "oeo_MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_MEGASTRUCTURE_LIMIT"

	prerequisites = { "tech_ring_world" }

	possible = {
		hidden_trigger = {
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
		custom_tooltip = {
			fail_text = "requires_surveyed_system"
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prev.from
						status = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_habitable_planets"
			NOT = {
				any_system_planet = {
					colonizable_planet = yes
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_black_hole"
			NOT = { is_star_class = sc_black_hole }
		}
		custom_tooltip = {
			fail_text = "requires_no_binary_trinary"
			NOR = {
				is_star_class = sc_binary_1
				is_star_class = sc_binary_2
				is_star_class = sc_binary_3
				is_star_class = sc_binary_4
				is_star_class = sc_binary_5
				is_star_class = sc_binary_6
				is_star_class = sc_binary_7
				is_star_class = sc_binary_8
				is_star_class = sc_binary_9
				is_star_class = sc_binary_10
				is_star_class = sc_trinary_1
				is_star_class = sc_trinary_2
				is_star_class = sc_trinary_3
				is_star_class = sc_trinary_4
			}
		}

		custom_tooltip = {
			fail_text = "requires_no_existing_megastructure"
			####Custom Scripted Trigger######
			has_no_oeo_non_gate_megastructure = yes
			NOT = {
				any_system_planet = {
					is_planet_class = pc_cosmogenesis_world
				}
			}
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "must_build_around_star"
				is_star = yes
			}
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = { has_anomaly = yes }
			}
			custom_tooltip = {
				fail_text = "requires_planets_for_material"
				hidden:solar_system = { any_system_planet = { NOT = { is_star = yes } } }
			}
		}
	}

	ai_weight = {
		factor = 10

		modifier = {
			factor = 0.1
			starbase = { NOT = { has_starbase_size >= starbase_starfortress } }
		}

		modifier = {
			factor = 0.1
			any_neighbor_system = {
				exists = owner
				NOT = {
					owner = { is_same_value = from }
				}
			}
		}
	}

	on_build_start = {}
	on_build_cancel = {}
	on_build_complete = {
		set_star_flag = ring_world_built
	}
}
# Temporarily used for creating specific habitable or structural sections.
ring_world_2_intermediate = {
	entity = ""
	construction_entity = "ringworld_seam_entity_01_full_entity"
	portrait = "GFX_megastructure_construction_background"
	show_galactic_map_icon = no
	show_in_outliner = no

	build_time = 4800
	resources = {
		category = megastructures_oeo_megastructures
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 10000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}
		upkeep = {
			trade = @oeo_ring_2_trade_upkeep
		}
	}

	upgrade_from = {
		ring_world_1
	}

	prerequisites = { "tech_ring_world" }

	on_build_start = {}
	on_build_cancel = {}
	on_build_complete = {
		every_system_planet = {
			limit = { is_star = no }
			remove_planet = yes
		}
		set_asteroid_belt = {
			radius = 0
		}

		remove_megastructure = fromfrom
		spawn_megastructure = {
			name = "NAME_Ring_Section_A"
			type = "ring_world_2"
			orbit_angle = 0
			orbit_distance = 45
			owner = from
			graphical_culture = fromfrom
			planet = star
		}
		spawn_megastructure = {
			name = "NAME_Ring_Section_B"
			type = "ring_world_2"
			orbit_angle = 90
			orbit_distance = 45
			owner = from
			graphical_culture = fromfrom
			planet = star
		}
		spawn_megastructure = {
			name = "NAME_Ring_Section_C"
			type = "ring_world_2"
			orbit_angle = 180
			orbit_distance = 45
			owner = from
			graphical_culture = fromfrom
			planet = star
		}
		spawn_megastructure = {
			name = "NAME_Ring_Section_D"
			type = "ring_world_2"
			orbit_angle = 270
			orbit_distance = 45
			owner = from
			graphical_culture = fromfrom
			planet = star
		}
	}
}

ring_world_2 = {
	entity = "ringworld_3x_seams_entity"
	portrait = "GFX_megastructure_construction_background"

	upgrade_from = {
		ring_world_2_intermediate
	}
	resources = {
		category = megastructures_oeo_megastructures
		upkeep = {
			trade = @oeo_ring_2_trade_upkeep
		}
	}
	# used from script only
	upgrade_desc = hide
	potential = { always = no }
}
ring_world_3_intermediate = {
	entity = ""
	construction_entity = "ringworld_phase_3_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	build_time = 3600
	show_galactic_map_icon = no
	show_in_outliner = no
	resources = {
		category = megastructures
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 10000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}
	}

	upgrade_from = {
		ring_world_2
	}

	prerequisites = { "tech_ring_world" }
	show_prereqs = yes
	prereq_name = "RING_WORLD_SHOW_NAME"

	on_build_complete = {
		from = {
			set_country_flag = has_built_or_repaired_megastructure
			set_country_flag = has_ring_world
		}

		spawn_planet = {
			class = "pc_ringworld_tech"
			location = fromfrom
			orbit_angle_offset = 30
			init_effect = {
				set_name = "NAME_Ring_Section"
				set_planet_entity = {
					entity = "ringworld_tech_entity_01_entity"
					graphical_culture = fromfrom
				}
				set_surveyed = {
					surveyed = yes
					surveyor = FROM
				}
				set_all_comms_surveyed = yes
				set_planet_flag = megastructure
			}
		}
		spawn_planet = {
			class = "pc_ringworld_seam"
			location = fromfrom
			init_effect = {
				set_name = "NAME_Ring_Section"
				set_planet_entity = {
					entity = "ringworld_seam_entity_01_entity"
					graphical_culture = fromfrom
				}
				set_surveyed = {
					surveyed = yes
					surveyor = FROM
				}
				set_all_comms_surveyed = yes
				set_planet_flag = megastructure
			}
		}
		spawn_planet = {
			class = "pc_ringworld_habitable"
			location = fromfrom
			orbit_angle_offset = -30
			init_effect = {
				set_name = "NAME_Habitable_Section_A"
				set_planet_entity = {
					entity = "ringworld_habitable_entity_01_entity"
					graphical_culture = fromfrom
				}
				set_surveyed = {
					surveyed = yes
					surveyor = FROM
				}
				set_all_comms_surveyed = yes
				clear_blockers = yes
				save_event_target_as = ring_section
				trigger_megastructure_icon = yes
				set_planet_flag = megastructure
			}
		}
		fromfrom.planet = {
			#Edited to allow star megastructures#
			#set_planet_flag = has_megastructure
		}
		remove_megastructure = fromfrom
		if = {
			limit = {
				NOT = { has_star_flag = ring_section_1 }
			}
			set_star_flag = ring_section_1
			event_target:ring_section = { set_name = "NAME_Habitable_Section_A" }
			break = yes
		}
		if = {
			limit = {
				has_star_flag = ring_section_1
				NOT = { has_star_flag = ring_section_2 }
			}
			set_star_flag = ring_section_2
			event_target:ring_section = { set_name = "NAME_Habitable_Section_B" }
			break = yes
		}
		if = {
			limit = {
				has_star_flag = ring_section_2
				NOT = { has_star_flag = ring_section_3 }
			}
			set_star_flag = ring_section_3
			event_target:ring_section = { set_name = "NAME_Habitable_Section_C" }
			break = yes
		}
		if = {
			limit = { has_star_flag = ring_section_3 }
			remove_star_flag = ring_section_1
			remove_star_flag = ring_section_2
			remove_star_flag = ring_section_3
			event_target:ring_section = { set_name = "NAME_Habitable_Section_D" }
			break = yes
		}
	}
}

# Ruined Ring World Segment
ring_world_ruined = {
	entity = "ringworld_destroyed_entity"
	portrait = "GFX_megastructure_construction_background"

	potential = {
		always = no
	}
}

# Restored Ring World Segment
ring_world_restored = {
	entity = ""
	construction_entity = "ringworld_phase_3_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	build_time = 3600
	resources = {
		category = megastructures
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 10000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}
	}

	upgrade_from = {
		ring_world_ruined
	}

	possible = {
		from = {
			has_technology = tech_mega_engineering
		}
		custom_tooltip = {
			fail_text = "requires_surveyed_system"
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prev.from
						status = no
					}
				}
			}
		}
	}

	ai_weight = {
		weight = 300
		modifier = {
			weight = 0
			from = { is_country_type = fallen_empire }
		}
	}

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

	on_build_complete = {
		every_system_ambient_object = {
			limit = {
				OR = {
					is_ambient_object_type = large_debris_object
					is_ambient_object_type = medium_debris_01_object
					is_ambient_object_type = medium_debris_02_object
					is_ambient_object_type = small_debris_object
				}
			}
			destroy_ambient_object = this
		}
		spawn_planet = {
			class = "pc_ringworld_tech"
			location = fromfrom
			orbit_angle_offset = -30
			init_effect = {
				set_name = "NAME_Ring_Section"
				set_planet_entity = {
					entity = "ringworld_tech_entity_01_entity"
					graphical_culture = fromfrom
				}
				set_surveyed = {
					surveyed = yes
					surveyor = FROM
				}
				set_all_comms_surveyed = yes
				set_planet_flag = megastructure
			}
		}
		spawn_planet = {
			class = "pc_ringworld_seam"
			location = fromfrom

			init_effect = {
				set_name = "NAME_Ring_Section"
				set_planet_entity = {
					entity = "ringworld_seam_entity_01_entity"
					graphical_culture = fromfrom
				}
				set_surveyed = {
					surveyed = yes
					surveyor = FROM
				}
				set_all_comms_surveyed = yes
				set_planet_flag = megastructure
			}
		}
		spawn_planet = {
			class = "pc_ringworld_habitable"
			location = fromfrom
			orbit_angle_offset = 30
			init_effect = {
				set_name = "NAME_Habitable_Section_A"
				set_planet_entity = {
					entity = "ringworld_habitable_entity_01_entity"
					graphical_culture = fromfrom
				}
				set_surveyed = {
					surveyed = yes
					surveyor = FROM
				}
				set_all_comms_surveyed = yes
				clear_blockers = yes
				save_event_target_as = ring_section
				trigger_megastructure_icon = yes
				set_planet_flag = megastructure
			}
		}
		remove_megastructure = fromfrom
		from = {
			set_country_flag = has_built_or_repaired_megastructure
			country_event = { id = utopia.2017 }
			set_country_flag = has_ring_world
		}
		if = {
			limit = {
				NOT = { has_star_flag = ring_section_1 }
			}
			set_star_flag = ring_section_1
			event_target:ring_section = { set_name = "NAME_Habitable_Section_A" }
			break = yes
		}
		if = {
			limit = {
				has_star_flag = ring_section_1
				NOT = { has_star_flag = ring_section_2 }
			}
			set_star_flag = ring_section_2
			event_target:ring_section = { set_name = "NAME_Habitable_Section_B" }
			break = yes
		}
		if = {
			limit = {
				has_star_flag = ring_section_2
				NOT = { has_star_flag = ring_section_3 }
			}
			set_star_flag = ring_section_3
			event_target:ring_section = { set_name = "NAME_Habitable_Section_C" }
			break = yes
		}
		if = {
			limit = { has_star_flag = ring_section_3 }
			remove_star_flag = ring_section_1
			remove_star_flag = ring_section_2
			remove_star_flag = ring_section_3
			event_target:ring_section = { set_name = "NAME_Habitable_Section_D" }
			break = yes
		}
	}
}

