############################
#
# On Action Events
#
# Written by Annatar
#
############################


namespace = realterraforming_action

# Gaiaformers Uprising Won!
carrier_event = {
	id = realterraforming_action.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfrom = { has_country_flag = gaiaformers_uprising }
	}

	immediate = {
		FROM = {
			save_event_target_as = gaiaformers_originator
			remove_country_flag = gaiaformers_uprising_country
			country_event = { id = realterraforming_action.2 days = 1 }
		}
		# create gaiaformers species
		create_species = {
			name = random
			class = MACHINE
			portrait = random
			traits = {
				trait = trait_machine_unit
				trait = random_traits
			}
			immortal = yes
		}
		# create gaiaformers country
		create_country = {
			name = random
			type = default
			species = last_created_species
			ignore_initial_colony_error = yes
			day_zero_contact = no
			ethos = { ethic = ethic_gestalt_consciousness }
			authority = auth_machine_intelligence
			origin = origin_gaiaformers
			civics = {
				civic = civic_machine_servitor
				civic = random
			}
			flag = random
			effect = {
				modify_species = {
					species = this
					add_trait = trait_machine_unit
					add_traits_at_start_of_list = yes
				}
				save_event_target_as = current_uprising
			}
		}
		
		last_created_country = {
			
			# setting graphical culture
			set_graphical_culture = event_target:gaiaformers_originator
			
			# establish comms
			every_country = {
				limit = {
					OR = {
						has_communications = event_target:gaiaformers_originator
						intel_level = {
							level > low
							system = event_target:gaiaformers_originator.capital_scope.solar_system
						}
						any_system = {
							has_star_flag = machup_system_flip
							prev = {
								intel_level = {
									level > low
									system = prev
								}
							}
						}
					}
				}
				establish_communications_no_message = prev
				establish_contact = { who = prev location = from }
			}
			
			# baseline resource stockpile
			add_resource = {
				energy = 20000
				minerals = 20000
				influence = 800
				alloys = 10000
				food = 10000 #just in case
				consumer_goods = 5000 #just in case
				rare_crystals = 1000
				volatile_motes = 1000
				exotic_gases = 1000
				sr_dark_matter = 100
			}
			add_500_unity_per_year_passed = yes # scripted effect
			
			# Machine Uprising opinion modifier
			add_opinion_modifier = {
				who = event_target:gaiaformers_originator
				modifier = opinion_machine_uprising_originator
			}

			event_target:gaiaformers_originator = {
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising
				}
			}

			# clear new contact opinion modifier
			if = {
				limit = {
					has_opinion_modifier = {
						who = event_target:gaiaformers_originator
						modifier = opinion_new_contact
					}
				}
				remove_opinion_modifier = {
					who = event_target:gaiaformers_originator
					modifier = opinion_new_contact
				}
			}
			
			# get host's technologies
			copy_techs_from = {
				target = event_target:gaiaformers_originator
				except = {
					# Robots
					tech_robotic_workers
					tech_droid_workers
					tech_synthetic_workers
					# Food
					tech_eco_simulation
					tech_gene_crops
					tech_nano_vitality_crops
					tech_nutrient_replication
					# Misc
					tech_frontier_health
					tech_frontier_hospital
					tech_telepathy
					tech_precognition_interface
					tech_psi_jump_drive_1
					tech_galactic_markets
					tech_subdermal_stimulation
					tech_global_research_initiative
					tech_neural_implants
					tech_psionic_theory
					# Non-Machine Robomodding
					tech_robomodding
					tech_robomodding_points_1
					tech_robomodding_points_2
					# Genetics
					tech_genome_mapping
					tech_vitality_boosters
					tech_epigenetic_triggers
					tech_cloning
					tech_gene_banks
					tech_gene_seed_purification
					tech_morphogenetic_field_mastery
					tech_gene_tailoring
					tech_glandular_acclimation
					tech_gene_expressions
					tech_selected_lineages
					tech_capacity_boosters
					# Horizon Signal
					tech_akx_worm_1
					tech_akx_worm_2
					tech_akx_worm_3
				}
			}
			
			# survey host's surveyed planets
			every_galaxy_planet = {
				limit = {
					is_surveyed = {
						who = event_target:gaiaformers_originator
						status = yes
					}
				}
				set_surveyed = {
					surveyed = yes
					surveyor = root
				}
			}

			# declare war on host
			declare_war = {
				target = event_target:gaiaformers_originator
				name = {
					key = "NAME_Gaiaformers_Uprising_War"
					variable_string = "[This.MainDefender.GetAdj]"
				}
				# "[This.MainDefender.GetAdj] Machine Uprising"
				attacker_war_goal = "wg_machine_uprising"
			}
		}
		
		# flip capital planet and add at least 10 Machine pops
		from = {
			# flip military stations
			solar_system = {
				every_fleet_in_system = {
					limit = {
						exists = owner
						owner = { is_same_value = event_target:gaiaformers_originator }
						any_controlled_ship = { is_ship_class = shipclass_military_station }
					}
					set_owner = last_created_country
				}
			}
			set_owner = last_created_country
			set_controller = last_created_country
			set_capital = yes

			solar_system = {
				if = {
					limit = {
						exists = starbase
					}
					starbase = {
						set_owner = last_created_country
						if = {
							limit = { has_starbase_size = starbase_outpost }
							set_starbase_size = starbase_starport
							set_starbase_module = {
								slot = 1
								module = shipyard
							}
							set_starbase_module = {
								slot = 2
								module = shipyard
							}
						}
						else_if = {
							limit = {
								NOT = { has_starbase_module = shipyard }
							}
							set_starbase_module = {
								slot = 1
								module = shipyard
							}
							set_starbase_module = {
								slot = 2
								module = shipyard
							}
						}
					}
				}
				else = {
					create_starbase = {
						size = starbase_starport
						module = shipyard
						owner = last_created_country
					}
				}
			}

			while = {
				#limit = { free_housing > 0 }
				count = 10
				create_pop_group = {
					species = last_created_country
				}
			}

			# create armies
			every_planet_army = {
				limit = { army_type = terraforming_machines }
				modify_army = {
					name = random
					type = defense_army
				}
				set_owner = last_created_country
			}
			while = {
				count = 3
				create_army = {
					owner = last_created_country
					type = machine_assault_1
				}
			}
			
			validate_planet_buildings_and_districts = yes
		}
	}
}

# Gaiaformers Uprising Defeated!
country_event = {
	id = realterraforming_action.2
	title = "realterraforming_action.2.name"
	desc = "realterraforming_action.2.desc"
	picture = GFX_evt_victorious_army
	show_sound = event_ground_battle
	location = FROMFROM

	is_triggered_only = yes

	trigger = {
		has_country_flag = gaiaformers_uprising_country
		FROMFROM = { has_carrier_flag = gaiaformers_uprising_planet }
	}

	immediate = {
		remove_country_flag = gaiaformers_uprising_country
		FROMFROM = {
			remove_carrier_flag = gaiaformers_uprising_planet
			if = {
				limit = { has_building = building_gaiaformers_factory }
				remove_building = building_gaiaformers_factory
			}
		}
	}

	option = {
		name = EXCELLENT
	}
}

#Planet is Lost
country_event = {
	id = realterraforming_action.3
	title = "realterraforming_action.3.name"
	desc = "realterraforming_action.3.desc"
	picture = GFX_evt_organic_oppression
	show_sound = event_robo
	location = ROOT
	
	is_triggered_only = yes
	
	option = {
		name = UNFORTUNATE
	}
}

carrier_event = {
	id = realterraforming_action.4
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
		last_building_changed = building_gaiaformers_factory
	}

	immediate = {
		random_deposit = {
			limit = { is_deposit_type = d_gaiaformers_worksite }
			clear_blocker = yes
		}
	}
}