# =============================================================================
# Giga + Age of Wonder Compatibility Patch — Dyson Sphere  [single unified line]
# Design (per user): the ONLY Dyson Sphere path is
#       Giga Dyson Swarm  ->  AOW (star-class) Dyson Sphere
# AOW's own Dyson Sphere/Swarm build buttons and Giga's direct Dyson Sphere build
# are all disabled (see zzzz_compat_dyson_swarm.txt and
# zzzz_compat_giga_dyson_overrides.txt).
#
# AOW_MEGA_dyson_sphere_entry below is now an UPGRADE-ONLY transitional structure:
#   * upgrade_from = the completed Giga swarm stages (dyson_swarm_3 + dyson_swarm_3_basic
#     + the star-class ones) AND AOW's own swarm variants (AOW_MEGA_dyson_swarm_2_*
#     and AOW_MEGA_dyson_swarm_3_*) — so a finished swarm of ANY kind upgrades into it.
#     Having upgrade_from also removes it from the build menu (no standalone AOW build).
#   * its on_build_complete (fires on upgrade too) reads the ACTUAL star class and
#     redirects to AOW_MEGA_dyson_sphere_0_<class> (a zero-build-time passthrough).
#     sphere_0 then cascades: sphere_0 -> sphere_1 -> sphere_2 (see
#     zzzz_compat_dyson_variants.txt), so the player lands on sphere_2 ("Initial").
#     From there AOW's own sphere chain (2 -> 3 -> 4 -> 5 -> disco) takes over.
#   * the "no existing megastructure" placement check was removed because the
#     structure being upgraded (the swarm) is itself a megastructure.
#   * the single-star-system check is relaxed when is_upgrading = yes, since a Giga
#     swarm can be built in binary/trinary systems that the AOW sphere normally forbids.
# Giga's swarm->Giga-sphere link is severed in zzzz_compat_giga_dyson_overrides.txt.
# =============================================================================

AOW_MEGA_dyson_sphere_entry = {
	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
	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_DYSON_SPHERE"
	resources = {
		category = megastructures
		cost = {
			unity = 1000
		}
		cost = {
			alloys = 5000
		}
		upkeep = {
			energy = 5
		}
	}
	prerequisites = { "tech_dyson_sphere" }
	# COMPAT: reached by upgrading ANY completed Dyson Swarm (Giga or AOW, any star class).
	# Having upgrade_from also removes it from the build menu (no standalone AOW sphere build).
	upgrade_from = {
		dyson_swarm_3
		dyson_swarm_3_basic
		dyson_swarm_3_a_star
		dyson_swarm_3_b_star
		dyson_swarm_3_f_star
		dyson_swarm_3_g_star
		dyson_swarm_3_k_star
		dyson_swarm_3_m_giant_star
		dyson_swarm_3_m_star
		AOW_MEGA_dyson_swarm_2_hyper
		AOW_MEGA_dyson_swarm_2_super
		AOW_MEGA_dyson_swarm_2_o
		AOW_MEGA_dyson_swarm_2_b
		AOW_MEGA_dyson_swarm_2_giant
		AOW_MEGA_dyson_swarm_2_a
		AOW_MEGA_dyson_swarm_2_f
		AOW_MEGA_dyson_swarm_2_g
		AOW_MEGA_dyson_swarm_2_k
		AOW_MEGA_dyson_swarm_2_m
		AOW_MEGA_dyson_swarm_3_hyper
		AOW_MEGA_dyson_swarm_3_super
		AOW_MEGA_dyson_swarm_3_o
		AOW_MEGA_dyson_swarm_3_b
		AOW_MEGA_dyson_swarm_3_giant
		AOW_MEGA_dyson_swarm_3_a
		AOW_MEGA_dyson_swarm_3_f
		AOW_MEGA_dyson_swarm_3_g
		AOW_MEGA_dyson_swarm_3_k
		AOW_MEGA_dyson_swarm_3_m
	}
	# COMPAT FIX: the Giga-Swarm to AoW-Sphere bridge must always complete, no matter
	# what the Giga/AoW megastructure sort button is set to. This is AoW's own
	# AOW_trigger_mega_potential logic for KEY = dyson_sphere, inlined verbatim EXCEPT the
	# compat_mega_hide_aow filter line is omitted, so the sphere upgrade is never blocked
	# by the sort button. The entry is upgrade-only (it never appears in the build menu),
	# so exempting it adds zero menu clutter; AoW's real dyson-sphere enable/cap game
	# rules (no_player / no_ai flags + mega-cap vars) are preserved.
	potential = {
		if = {
			limit = { is_ai = no }
			OR = {
				NOT = { has_global_flag = AOW_flag_GLBL_no_player_dyson_sphere }
				event_target:global_event_country = {
					check_variable = { which = AOW_var_player_mega_cap_dyson_sphere value != 0 }
				}
			}
		}
		else_if = {
			limit = { is_ai = yes }
			OR = {
				NOT = { has_global_flag = AOW_flag_GLBL_no_ai_dyson_sphere }
				event_target:global_event_country = {
					check_variable = { which = AOW_var_ai_mega_cap_dyson_sphere value != 0 }
				}
			}
		}
	}
	possible = {
		hidden_trigger = { exists = starbase }
		custom_tooltip = {#on_build_start
			fail_text = "AOW_mega_requires_CNTR_no_more_megastructure"
			from = { NOT = { has_country_flag = AOW_flag_CNTR_no_more_dyson_sphere } }
		}
		custom_tooltip = {#必须在国境内
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
		custom_tooltip = {#星系内不能有非人工非土著的殖民地
			fail_text = "requires_no_colonies"
			AOW_trgr_SSTM_has_non_artificial_non_primitive_colony = no
		}
		custom_tooltip = {#有入侵性政策才能在土著星系建造
			fail_text = "blocked_by_pre_ftl_policy"
			AOW_trgr_SSTM_can_aggressively_interfere_with_pre_ftl = yes
		}
		custom_tooltip = {#必须是单恒星星系
			fail_text = "AOW_mega_requires_SSTM_single_star"
			OR = {
				from = { has_technology = AOW_TECH_dimension_hiding }
				AOW_trgr_SSTM_single_star_system = yes
			}
		}
	}
	placement_rules = {
		planet_possible = {
			custom_tooltip = {#必须光谱分类为OBAFGKM的恒星
				fail_text = "AOW_mega_requires_PLNT_obafgkm_star"
				AOW_trgr_PLNT_obafgkm_star = yes
			}
			if = {#超巨星和特超巨星上建造需要掌握多维空间科技
				limit = {
					planet = {
						OR = {
							AOW_trgr_PLNT_super_star = yes
							AOW_trgr_PLNT_hyper_star = yes
						}
					}
				}
				custom_tooltip = {
					fail_text = "AOW_mega_requires_PLNT_multi_dimensional_dyson_sphere"
					from = { has_technology = AOW_TECH_multi_dimensional_warehouse }
				}
			}
			custom_tooltip = {#不能有异常
				fail_text = "requires_no_anomaly"
				NOT = { has_anomaly = yes }
			}
			# COMPAT: removed the "no existing megastructure" check — this is reached by
			# upgrading the swarm, which is itself a megastructure on this star.
		}
	}

	# root = system
	# from = country
	ai_weight = {
		weight = 300
		#位置限制
		modifier = { factor = 0.1	starbase = { NOT = { has_starbase_size >= starbase_starfortress } } }
		modifier = { factor = 0.1	AOW_trgr_SSTM_neighbor_didnt_undercontrol = yes }
		#自身限制
		modifier = { factor = 1.5	from = { is_pacifist = yes } }
	}
	on_build_start = {
		set_star_flag = dyson_sphere_construction
		AOW_effect_check_mega_cap = {
			key = dyson_sphere
			constructing = dyson_sphere_0
		}
	}
	on_build_cancel = {
		remove_star_flag = dyson_sphere_construction
	}
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		set_star_flag = dyson_sphere_built
		from = { set_country_flag = built_dyson_sphere }
		fromfrom = {
			set_megastructure_flag = dyson_sphere
			set_megastructure_flag = AOW_flag_MEGA_dyson_sphere_@owner
			planet = { AOW_eft_PLNT_dismantle_orbital_station = yes }
			if = {
				limit = { planet = { AOW_trgr_PLNT_hyper_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_hyper
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_super_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_super
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_o_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_O
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_b_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_B
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_giant_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_giant
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_a_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_A
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_f_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_F
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_g_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_G
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_k_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_K
				finish_upgrade = yes
			}
			else_if = {
				limit = { planet = { AOW_trgr_PLNT_m_star = yes } }
				upgrade_megastructure_to = AOW_MEGA_dyson_sphere_0_M
				finish_upgrade = yes
			}
		}
	}
}
