############################
#
# Pirate Events
#
# Written by Henrik Eklund
#
############################

namespace = pirate

# Birth of Space Piracy
country_event = {
	id = pirate.1
	title = "pirate.1.name"
	picture = GFX_evt_exploding_ship
	show_sound = event_space_battle
	
	desc = { # Spiritual
		text = pirate.1.desc_01
		trigger = {
			OR = {
				has_government = theocratic_republic
				has_government = transcendent_republic
				has_government = theocratic_oligarchy
				has_government = transcendent_oligarchy
				has_government = divine_mandate
				has_government = transcendent_empire
			}
		}
	}
	
	desc = { # Militarist
		text = pirate.1.desc_02
		trigger = {
			OR = {
				has_government = military_dictatorship
				has_government = martial_empire
				has_government = military_junta
				has_government = ordered_stratocracy
				has_government = military_republic
				has_government = martial_demarchy
			}
		}
	}
	
	desc = { # Materialist
		text = pirate.1.desc_03
		trigger = {
			OR = {
				has_government = despotic_hegemony
				has_government = ai_overlordship
				has_government = science_directorate
				has_government = illuminated_technocracy
				has_government = direct_democracy
				has_government = subconscious_consensus
			}
		}
	}
	
	desc = { # Pacifist
		text = pirate.1.desc_04
		trigger = {
			OR = {
				has_government = moral_democracy
				has_government = irenic_democracy
				has_government = peaceful_bureaucracy
				has_government = irenic_protectorate
				has_government = enlightened_monarchy
				has_government = irenic_monarchy
			}
		}
	}
	
	desc = { # Generic Autocracy
		text = pirate.1.desc_05
		trigger = {
			OR = {
				has_government = despotic_empire
				has_government = star_empire
			}
		}
	}
	
	desc = { # Generic Oligarchy
		text = pirate.1.desc_06
		trigger = {
			OR = {
				has_government = plutocratic_oligarchy
				has_government = mega_corporation
			}
		}
	}
	
	desc = { # Generic Democracy
		text = pirate.1.desc_07
		trigger = {
			OR = {
				has_government = indirect_democracy
				has_government = democratic_utopia
			}
		}
	}
	
	trigger = {
		is_country_type = default
		NOT = { has_country_flag = birth_of_piracy }
		exists = capital_scope
		capital_scope = {
			solar_system = {
				ROOT = {
					any_controlled_planet = {
						OR = { 
							has_mining_station = yes 
							has_research_station = yes
						}
						solar_system = {
							NOT = { is_same_value = PREVPREVPREV }
						}
					}
				}
			}
		}
	}
	
	is_triggered_only = yes

	#mean_time_to_happen = { 
	#	months = 100 #Triggered by pirate.4 now
	#}
	
	immediate = {
		set_country_flag = birth_of_piracy
		capital_scope = { save_event_target_as = capital }
		capital_scope = {
			solar_system = { save_event_target_as = capital_system }
		}
		save_event_target_as = pirate_target
		owner_species = { save_event_target_as = owner_species }
		
		# If Warp or Hyper Drive
		if = {
			limit = { 
				OR = {
					has_technology = "tech_warp_drive_1" 
					has_technology = "tech_hyper_drive_1"
				}
			}
			random_system = {
				limit = {
					distance = {
						source = event_target:capital
						max_distance = 60
						min_distance = 5
					}
					any_planet = { is_asteroid = yes }
					NOT = { has_star_flag = hostile_system }
					OR = {
						has_owner = no
						is_owned_by = ROOT
					}
				}
				random_system_planet = {
					limit = { is_asteroid = yes }
					save_event_target_as = pirate_home
					create_country = {
						name = "Pirates"
						type = pirate
						species = event_target:owner_species
						name_list = "PRT1"
						flag = {
							icon = {
								category = "pirate"
								file = "flag_pirate_7.dds"
							}
							background = {
								category = "backgrounds"
								file = "00_solid.dds"
							}
							colors ={
								"red"
								"red"
								"null"
								"null"
							}
						}
					}
					last_created_country = {
						set_name = random
						save_event_target_as = pirate_band
						set_relation_flag = {
							who = ROOT
							flag = pirate_relation
						}
						establish_communications_no_message = ROOT
						
						create_fleet = { name = "Pirate Station" }
						if = {
							limit = {
								root = { has_technology = tech_mass_drivers_1 }
							}
							last_created_fleet = {
								set_owner = PREV
								create_ship = {
									name = random
									design = "Pirate Nest"
									graphical_culture = "pirate_01"
								}
								set_location = PREVPREV
							}
						}
						if = {
							limit = {
								root = { has_technology = tech_lasers_1 }
							}
							last_created_fleet = {
								set_owner = PREV
								create_ship = {
									name = random
									design = "Pirate Lair"
									graphical_culture = "pirate_01"
								}
								set_location = PREVPREV
							}
						}
						if = {
							limit = {
								root = { has_technology = tech_missiles_1 }
							}
							last_created_fleet = {
								set_owner = PREV
								create_ship = {
									name = random
									design = "Pirate Den"
									graphical_culture = "pirate_01"
								}
								set_location = PREVPREV
							}
						}
						create_leader = {
							type = admiral
							species = owner_main_species
							name = random
							skill = 100
						}
						create_fleet = { name = "Pirate Fleet" }
						last_created_fleet = {
							set_fleet_stance = aggressive
							set_aggro_range_measure_from = self
							set_aggro_range = 150
							set_owner = PREV
							if = {
								limit = {
									root = { has_technology = tech_mass_drivers_1 }
								}
								create_ship_design = {
									design = "Reaver"
									ftl = ROOT
								}
							}
							if = {
								limit = {
									root = { has_technology = tech_lasers_1 }
								}
								create_ship_design = {
									design = "Hunter"
									ftl = ROOT
								}
							}
							if = {
								limit = {
									root = { has_technology = tech_missiles_1 }
								}
								create_ship_design = {
									design = "Vagabond"
									ftl = ROOT
								}
							}
							create_ship = {
								name = random
								design = last_created_design
								graphical_culture = "pirate_01"
							}
							create_ship = {
								name = random
								design = last_created_design
								graphical_culture = "pirate_01"
							}
							create_ship = {
								name = random
								design = last_created_design
								graphical_culture = "pirate_01"
							}
							create_ship = {
								name = random
								design = last_created_design
								graphical_culture = "pirate_01"
							}
							assign_leader = last_created_leader
							set_location = PREVPREV
							
							queue_actions = {
								repeat = {
									find_random_system = {
										trigger = {
											id = "pirate.1.trigger.1"
											NOT = { 
												has_star_flag = hostile_system 
												is_same_value = event_target:capital_system
											}
											any_planet = {
												OR = {
													has_mining_station = yes
													has_research_station = yes
												}
												exists = controller
												controller = { is_same_value = event_target:pirate_target }
											}
											distance = {
												source = PREV
												max_distance = 100
												min_distance = 20
											}
										}
										found_system = {
											move_to = THIS
										}	
									}
									find_closest_planet = {
										trigger = {
											id = "pirate.1.trigger.2"
											OR = {
												has_mining_station = yes
												has_research_station = yes
											}
										}
										found_planet = {
											orbit_planet = THIS
											wait = {
												duration = 20
											}
										}
									}
									find_random_system = {
										trigger = {
											id = "pirate.1.trigger.3"
											any_planet = {
												is_planet = event_target:pirate_home
											}
										}
										found_system = {
											move_to = THIS
										}	
									}
									find_closest_planet = {
										trigger = {
											id = "pirate.1.trigger.4"
											is_planet = event_target:pirate_home
										}
										found_planet = {
											orbit_planet = THIS
											wait = {
												duration = 200
												random = 150
											}
										}
									}
								}
							}
						}
						if = {
							limit = {
								ROOT = { has_technology = "tech_warp_drive_1" }
							}
							set_country_flag = warp_pirates
							else = {
								if = {
									limit = {
										ROOT = { has_technology = "tech_hyper_drive_1" }
									}
									set_country_flag = hyper_pirates
								}
							}
						}
					}
				}
			}
			# If Wormhole
			else = {
				if = {
					limit = { has_technology = "tech_wormhole_generation_1" }
					random_system = {
						limit = {
							distance = {
								source = event_target:capital
								max_distance = 60
								min_distance = 5
							}
							any_planet = { is_asteroid = yes }
							NOT = { has_star_flag = hostile_system }
							OR = {
								has_owner = no
								is_owned_by = ROOT
							}
						}
						random_system_planet = {
							limit = { is_asteroid = yes }
							save_event_target_as = pirate_home
							create_country = {
								name = "Pirates"
								type = pirate
								species = event_target:owner_species
								name_list = "PRT1"
								flag = {
									icon = {
										category = "pirate"
										file = "flag_pirate_7.dds"
									}
									background = {
										category = "backgrounds"
										file = "00_solid.dds"
									}
									colors ={
										"red"
										"red"
										"null"
										"null"
									}
								}
							}
							solar_system = {
								create_wormhole_station = { 
									owner = last_created_country
									angle = random
								}
							}
							last_created_country = {
								set_name = random
								save_event_target_as = pirate_band
								set_relation_flag = {
									who = ROOT
									flag = pirate_relation
								}
								set_country_flag = wormhole_pirates
								establish_communications_no_message = ROOT
								
								create_fleet = { name = "Pirate Station" }
								last_created_fleet = {
									set_owner = PREV
									create_ship = {
										name = random
										design = "Pirate Lair"
										graphical_culture = "pirate_01"
									}
									set_location = PREVPREV
								}
								create_leader = {
									type = admiral
									species = owner_main_species
									name = random
									skill = 100
								}
								create_fleet = { name = "Pirate Fleet" }
								last_created_fleet = {
									set_fleet_stance = aggressive
									set_aggro_range_measure_from = self
									set_aggro_range = 150
									set_owner = PREV
									if = {
										limit = {
											root = { has_technology = tech_mass_drivers_1 }
										}
										create_ship_design = {
											design = "Reaver"
											ftl = ROOT
										}
									}
									if = {
										limit = {
											root = { has_technology = tech_lasers_1 }
										}
										create_ship_design = {
											design = "Hunter"
											ftl = ROOT
										}
									}
									if = {
										limit = {
											root = { has_technology = tech_missiles_1 }
										}
										create_ship_design = {
											design = "Vagabond"
											ftl = ROOT
										}
									}
									create_ship = {
										name = random
										design = last_created_design
										graphical_culture = "pirate_01"
									}
									create_ship = {
										name = random
										design = last_created_design
										graphical_culture = "pirate_01"
									}
									create_ship = {
										name = random
										design = last_created_design
										graphical_culture = "pirate_01"
									}
									assign_leader = last_created_leader
									set_location = PREVPREV
									
									queue_actions = {
										repeat = {
											find_random_system = {
												trigger = {
													id = "pirate.1.trigger.5"
													NOT = { 
														has_star_flag = hostile_system 
														is_same_value = event_target:capital_system
													}
													any_planet = {
														OR = {
															has_mining_station = yes
															has_research_station = yes
														}
														exists = controller
														controller = { is_same_value = event_target:pirate_target }
													}
													distance = {
														source = PREV
														max_distance = 100
														min_distance = 20
													}
												}
												found_system = {
													move_to = THIS
												}	
											}
											find_closest_planet = {
												trigger = {
													id = "pirate.1.trigger.6"
													OR = {
														has_mining_station = yes
														has_research_station = yes
													}
												}
												found_planet = {
													orbit_planet = THIS
													wait = {
														duration = 20
													}
												}
											}
											find_random_system = {
												trigger = {
													id = "pirate.1.trigger.7"
													any_planet = {
														is_planet = event_target:pirate_home
													}
												}
												found_system = {
													move_to = THIS
												}	
											}
											find_closest_planet = {
												trigger = {
													id = "pirate.1.trigger.8"
													is_planet = event_target:pirate_home
												}
												found_planet = {
													orbit_planet = THIS
													wait = {
														duration = 200
														random = 150
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	option = {
		name = pirate.2.a
		trigger = {
			OR = {
				has_government = theocratic_republic
				has_government = transcendent_republic
				has_government = theocratic_oligarchy
				has_government = transcendent_oligarchy
				has_government = divine_mandate
				has_government = transcendent_empire
			}
		}
	}
	option = {
		name = pirate.2.b
		trigger = {
			OR = {
				has_government = military_dictatorship
				has_government = martial_empire
				has_government = military_junta
				has_government = ordered_stratocracy
				has_government = military_republic
				has_government = martial_demarchy
			}
		}
	}
	option = {
		name = pirate.2.c
		trigger = {
			OR = {
				has_government = despotic_hegemony
				has_government = ai_overlordship
				has_government = science_directorate
				has_government = illuminated_technocracy
				has_government = direct_democracy
				has_government = subconscious_consensus
			}
		}
	}
	option = {
		name = pirate.2.d
		trigger = {
			OR = {
				has_government = moral_democracy
				has_government = irenic_democracy
				has_government = peaceful_bureaucracy
				has_government = irenic_protectorate
				has_government = enlightened_monarchy
				has_government = irenic_monarchy
			}
		}
	}
	option = {
		name = pirate.2.e
		trigger = {
			OR = {
				has_government = despotic_empire
				has_government = star_empire
			}
		}
	}
	option = {
		name = pirate.2.f
		trigger = {
			OR = {
				has_government = plutocratic_oligarchy
				has_government = mega_corporation
			}
		}
	}
	option = {
		name = pirate.2.g
		trigger = {
			OR = {
				has_government = indirect_democracy
				has_government = democratic_utopia
			}
		}
	}
}

# New Ships (HIDDEN)
country_event = {
	id = pirate.2
	hide_window = yes
	
	trigger = {
		is_country_type = pirate
		OR = {
			num_fleets < 2
			num_ships < 3
		}
		any_owned_ship = {
			is_ship_size = pirate_station
		}
	}
	
	mean_time_to_happen = {
		months = 100
	}
	
	immediate = {
		random_country = {
			limit = {
				reverse_has_relation_flag = { flag = pirate_relation who = ROOT }
			}
			save_event_target_as = pirate_target
			capital_scope = {
				solar_system = { save_event_target_as = capital_system }
			}
		}
		create_leader = {
			type = admiral
			species = owner_main_species
			name = random
			skill = 100
		}
		random_owned_ship = {
			limit = { is_ship_size = pirate_station }
			save_event_target_as = pirate_home
			create_fleet = { name = "Pirate Fleet" }
			
			if = { # Warp
				limit = { 
					owner = { has_country_flag = warp_pirates }
					exists = event_target:pirate_target
				}
				last_created_fleet = {
					set_fleet_stance = aggressive
					set_aggro_range_measure_from = self
					set_aggro_range = 150
					set_owner = ROOT
					create_ship_design = {
						design = "Reaver"
						ftl = warp
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					assign_leader = last_created_leader
					set_location = event_target:pirate_home
					
					queue_actions = {
						repeat = {
							find_random_system = {
								trigger = {
									id = "pirate.2.trigger.1"
									NOT = { 
										has_star_flag = hostile_system 
										is_same_value = event_target:capital_system
									}
									any_planet = {
										OR = {
											has_mining_station = yes
											has_research_station = yes
										}
										exists = controller
										controller = { is_same_value = event_target:pirate_target }
									}
									distance = {
										source = PREV
										max_distance = 100
										min_distance = 20
									}
								}
								found_system = {
									move_to = THIS
								}	
							}
							find_closest_planet = {
								trigger = {
									id = "pirate.2.trigger.2"
									OR = {
										has_mining_station = yes
										has_research_station = yes
									}
								}
								found_planet = {
									orbit_planet = THIS
									wait = {
										duration = 20
									}
								}
							}
							find_random_system = {
								trigger = {
									id = "pirate.2.trigger.3"
									any_planet = {
										is_planet = event_target:pirate_home
									}
								}
								found_system = {
									move_to = THIS
								}	
							}
							find_closest_planet = {
								trigger = {
									id = "pirate.2.trigger.4"
									is_planet = event_target:pirate_home
								}
								found_planet = {
									orbit_planet = THIS
									wait = {
										duration = 200
										random = 150
									}
								}
							}
						}
					}
				}
				break = yes
			}
			
			if = { # Hypedrive
				limit = { 
					owner = { has_country_flag = hyperdrive_pirates }
					exists = event_target:pirate_target
				}
				last_created_fleet = {
					set_owner = ROOT
					create_ship_design = {
						design = "Reaver"
						ftl = hyperdrive
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					assign_leader = last_created_leader
					set_location = event_target:pirate_home
					
					queue_actions = {
						repeat = {
							find_random_system = {
								trigger = {
									id = "pirate.2.trigger.5"
									NOT = { 
										has_star_flag = hostile_system 
										is_same_value = event_target:capital_system
									}
									any_planet = {
										OR = {
											has_mining_station = yes
											has_research_station = yes
										}
										exists = controller
										controller = { is_same_value = event_target:pirate_target }
									}
									distance = {
										source = PREV
										max_distance = 100
										min_distance = 20
									}
								}
								found_system = {
									move_to = THIS
								}	
							}
							find_closest_planet = {
								trigger = {
									id = "pirate.2.trigger.6"
									OR = {
										has_mining_station = yes
										has_research_station = yes
									}
								}
								found_planet = {
									orbit_planet = THIS
									wait = {
										duration = 20
									}
								}
							}
							find_random_system = {
								trigger = {
									id = "pirate.2.trigger.7"
									any_planet = {
										is_planet = event_target:pirate_home
									}
								}
								found_system = {
									move_to = THIS
								}	
							}
							find_closest_planet = {
								trigger = {
									id = "pirate.2.trigger.8"
									is_planet = event_target:pirate_home
								}
								found_planet = {
									orbit_planet = THIS
									wait = {
										duration = 200
										random = 150
									}
								}
							}
						}
					}
				}
				break = yes
			}
			
			if = { # Wormhole
				limit = { 
					owner = { has_country_flag = wormhole_pirates }
					exists = event_target:pirate_target
				}
				last_created_fleet = {
					set_owner = ROOT
					create_ship_design = {
						design = "Reaver"
						ftl = wormhole
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					create_ship = {
						name = random
						design = last_created_design
						graphical_culture = "pirate_01"
					}
					assign_leader = last_created_leader
					set_location = event_target:pirate_home
					
					queue_actions = {
						repeat = {
							find_random_system = {
								trigger = {
									id = "pirate.2.trigger.9"
									NOT = { 
										has_star_flag = hostile_system 
										is_same_value = event_target:capital_system
									}
									any_planet = {
										OR = {
											has_mining_station = yes
											has_research_station = yes
										}
										exists = controller
										controller = { is_same_value = event_target:pirate_target }
									}
									distance = {
										source = PREV
										max_distance = 100
										min_distance = 20
									}
								}
								found_system = {
									move_to = THIS
								}	
							}
							find_closest_planet = {
								trigger = {
									id = "pirate.2.trigger.10"
									OR = {
										has_mining_station = yes
										has_research_station = yes
									}
								}
								found_planet = {
									orbit_planet = THIS
									wait = {
										duration = 20
									}
								}
							}
							find_random_system = {
								trigger = {
									id = "pirate.2.trigger.11"
									any_planet = {
										is_planet = event_target:pirate_home
									}
								}
								found_system = {
									move_to = THIS
								}	
							}
							find_closest_planet = {
								trigger = {
									id = "pirate.2.trigger.12"
									is_planet = event_target:pirate_home
								}
								found_planet = {
									orbit_planet = THIS
									wait = {
										duration = 200
										random = 150
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

#Starting the pirate chain
ship_event = {
	id = pirate.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner = { is_country_type = default }
		from = {
			solar_system = {
				NOT = {
					any_planet = {
						is_homeworld = yes
					}
				}
			}
		}
		NOT = {
			owner = {
				has_country_flag = outer_system_mining_stations_constructed
				has_country_flag = birth_of_piracy 
			}
		}
	}

	immediate = {
		owner = {
			if = {
				limit = {
					NOT = {
						check_variable = {
							which = outer_system_mining_stations
							value > 0
						}
					}
				}
				set_variable = {
					which = "outer_system_mining_stations"
					value = 1
				}
				else = {
					change_variable = {
						which = "outer_system_mining_stations" 
						value = 1
					}
				}
			}
			if = {
				limit = {
					check_variable = {
						which = outer_system_mining_stations
						value > 4
					}
				}
				set_country_flag = outer_system_mining_stations_constructed
			}
		}
	}
}

event = {
	id = pirate.4
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		every_country = {
			limit = {
				is_ai = no
				NOT = { has_country_flag = birth_of_piracy }
			}
			country_event = { id = pirate.5 }
		}
	}
}

#yearly pulse event
country_event = {
	id = pirate.5
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = default
		OR = {
			has_country_flag = outer_system_mining_stations_constructed
			any_owned_planet = {
				is_homeworld = no
			}
		}
	}

	immediate = {	
		random_list = {
			30 = { country_event = { id = pirate.1 days = 50 random = 25 } }
			30 = { country_event = { id = pirate.1 days = 150 random = 50 } }
			40 = {  }
		}
	}
}