@stage_1_research_bonus	= 100
@stage_2_research_bonus	= 200
@stage_3_research_bonus	= 300
@stage_4_research_bonus	= 350
@entity_x				= 0
@entity_y				= -20


think_tank_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 1800
	resources = {
		category = megastructures
		cost = {
			unity = 5000
		}
		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 = {
			energy = 5
		}
	}

	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_ONE_PER_COUNTRY"

	prerequisites = { "tech_science_nexus" }

	possible = {
		hidden_trigger = {
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
		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 = "requires_surveyed_system"
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prev.from
						status = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_think_tank"
			from = {
				OR = {
					has_relic = r_contingency_core
					NOT = { has_country_flag = built_think_tank }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_think_tank_two"
			from = { NOT = { has_country_flag = built_think_tank_contingency } }
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_survey_not_habitable"
				is_surveyed = {			# prevent leaking habitability information
					who = prev.from
					status = yes
				}
				colonizable_planet = no
			}
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = { has_anomaly = yes }
			}
			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
			}
			custom_tooltip = {
				fail_text = "requires_not_astral_scar"
				is_astral_scar = no
			}
			custom_tooltip = {
				fail_text = "requires_not_ring_world"
				is_ringworld = no
			}
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				NOT = { has_planet_flag = megastructure }
			}
		} # use these for all non-star megastructures
	}

	# root = system
	# from = country
	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 }
				}
			}
		}

		modifier = {
			factor = 2.0
			from = {
				is_materialist = yes
			}
		}
	}

	on_build_start = {}
	on_build_cancel = {}
	on_build_complete = {
		set_star_flag = think_tank_built
		fromfrom.planet = {
			set_planet_flag = has_megastructure
		}
		from = {
			if = {
				limit = {
					has_relic = r_contingency_core
					has_country_flag = built_think_tank
				}
				set_country_flag = built_think_tank_contingency
			}
			set_country_flag = built_think_tank
		}
	}
}

think_tank_1 = {
	entity = "thinktank_phase_01_entity"
	construction_entity = "thinktank_phase_01_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 3600
	resources = {
		category = megastructures
		cost = {
			unity = 5000
		}
		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
		}

		upkeep = {
			energy = 25
		}

		produces = {
			society_research = @stage_1_research_bonus
			engineering_research = @stage_1_research_bonus
			physics_research = @stage_1_research_bonus
		}
	}

	upgrade_from = {
		think_tank_0
	}

	prerequisites = { "tech_science_nexus" }

	country_modifier = {
		all_technology_research_speed = 0.05
	}
}

think_tank_2 = {
	entity = "thinktank_phase_02_entity"
	construction_entity = "thinktank_phase_02_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 3600
	resources = {
		category = megastructures
		cost = {
			unity = 5000
		}
		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
		}

		upkeep = {
			energy = 50
		}

		produces = {
			society_research = @stage_2_research_bonus
			engineering_research = @stage_2_research_bonus
			physics_research = @stage_2_research_bonus
		}
	}

	upgrade_from = {
		think_tank_1
	}

	prerequisites = { "tech_science_nexus" }

	country_modifier = {
		all_technology_research_speed = 0.10
	}
}

think_tank_3 = {
	entity = "thinktank_phase_03_entity"
	construction_entity = "thinktank_phase_03_entity"
	portrait = "GFX_megastructure_think_tank_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 3600
	resources = {
		category = megastructures
		cost = {
			unity = 5000
		}
		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
		}

		upkeep = {
			energy = 75
		}

		produces = {
			society_research = @stage_3_research_bonus
			engineering_research = @stage_3_research_bonus
			physics_research = @stage_3_research_bonus
		}
	}

	upgrade_from = {
		think_tank_2
	}

	on_build_complete = {
		from = {
			set_country_flag = has_built_or_repaired_megastructure
		}
	}

	prerequisites = { "tech_science_nexus" }
	show_prereqs = yes

	country_modifier = {
		all_technology_research_speed = 0.15
	}
}

#Leader sacrifice boosted
think_tank_4 = {
	entity = "thinktank_phase_03_entity"
	construction_entity = "thinktank_phase_03_entity"
	portrait = "GFX_megastructure_think_tank_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 20
	resources = {
		category = megastructures
		upkeep = {
			energy = 100
		}

		produces = {
			society_research = @stage_4_research_bonus
			engineering_research = @stage_4_research_bonus
			physics_research = @stage_4_research_bonus
		}
	}
	upgrade_from = {
		think_tank_3
	}

	on_build_complete = {
		from = {
			set_country_flag = has_built_or_repaired_megastructure
		}
	}

	prerequisites = { "tech_science_nexus" }

	# used from script only
	upgrade_desc = hide
	potential = { always = no }

	country_modifier = {
		all_technology_research_speed = 0.15
	}
}

# Permanently Ruined Science Nexus
think_tank_permanently_ruined = {
	entity = "think_tank_destroyed_01_entity"
	portrait = "GFX_megastructure_construction_background"
	show_galactic_map_icon = no
	show_in_outliner = no

	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }

	potential = {
		always = no
	}
}

# Ruined Science Nexus
think_tank_ruined = {
	entity = "think_tank_destroyed_01_entity"
	portrait = "GFX_megastructure_construction_background"

	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }

	potential = {
		always = no
	}
}

# Restored Science Nexus
think_tank_restored = {
	entity = "thinktank_phase_03_entity"
	construction_entity = "thinktank_phase_03_entity"
	portrait = "GFX_megastructure_think_tank_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = @entity_x y = @entity_y }
	build_time = 4800
	resources = {
		category = megastructures
		cost = {
			unity = 3200
		}
		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
		}

		upkeep = {
			energy = 75
		}

		produces = {
			society_research = @stage_3_research_bonus
			engineering_research = @stage_3_research_bonus
			physics_research = @stage_3_research_bonus
		}
	}

	upgrade_from = {
		think_tank_ruined
	}

	possible = {
		from = { has_technology = tech_mega_engineering }
	}

	country_modifier = {
		all_technology_research_speed = 0.15
	}

	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
				}
			}
			destroy_ambient_object = this
		}
		from = {
			set_country_flag = has_built_or_repaired_megastructure
			country_event = { id = utopia.2013 }
		}
	}
}
