############################
#
# Spread of communications
#
# Written by Martin Anward
#
############################

namespace = communications_spread

# Communications - neighbor
country_event = {
	id = communications_spread.1
	title = "communications_spread.1.name"
	desc = "communications_spread.1.desc"
	picture = GFX_evt_star_chart
	show_sound = event_alien_signal
	
	trigger = {
		is_country_type = default
		NOT = { has_country_flag = starting_event }
		any_neighbor_country = {
			is_country_type = default
			NOT = { is_country = ROOT }
			has_communications = ROOT
			any_neighbor_country = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
				}
				NOT = { is_country = ROOT }
				has_communications = PREV
				NOT = { has_communications = ROOT }
			}
		}
	}
	
	immediate = {
		random_neighbor_country = {
			limit = {		
				is_country_type = default
				NOT = { is_country = ROOT }
				has_communications = ROOT
				any_neighbor_country = {
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
					}
					NOT = { is_country = ROOT }
					has_communications = PREV
					NOT = { has_communications = ROOT }
				}				
			}
			save_event_target_as = CommSpreader
			random_neighbor_country = {
				limit = {
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
					}
					NOT = { is_country = ROOT }
					has_communications = PREV
					NOT = { has_communications = ROOT }
				}
				save_event_target_as = contact_empire
				establish_communications_no_message = root
			}				
		}
	}

	mean_time_to_happen = {
		months = 6
		modifier = {
			factor = 0.5
			mid_game_years_passed > 0
		}
		modifier = {
			factor = 0.5
			mid_game_years_passed > 5
		}
		modifier = {
			factor = 0.5
			end_game_years_passed > 0
		}		
		modifier = {
			factor = 0.5
			end_game_years_passed > 5
		}		
	}

	option = {
		name = "communications_spread.1.a"		
		custom_tooltip = communications_spread.1.tooltip
		hidden_effect = {
			country_event = { id = action.1 }
			event_target:contact_empire = {
				country_event = { id = communications_spread.2 }
			}
		}			
	}
}

country_event = {
	id = communications_spread.2
	title = "communications_spread.2.name"
	desc = "communications_spread.2.desc"
	picture = GFX_evt_star_chart
	show_sound = event_alien_signal
	
	is_triggered_only = yes

	option = {
		name = "communications_spread.2.a"			
		custom_tooltip = communications_spread.2.tooltip
		hidden_effect = {
			FROM = { save_event_target_as = contact_empire }
			country_event = { id = action.1 }		
		}
	}
}

# Communications - any
country_event = {
	id = communications_spread.3
	title = "communications_spread.1.name"
	desc = "communications_spread.1.desc"
	picture = GFX_evt_star_chart
	show_sound = event_alien_signal
	
	trigger = {
		is_country_type = default
		NOT = { has_country_flag = starting_event }
		any_country = {
			is_country_type = default
			NOT = { is_country = ROOT }
			has_communications = ROOT
			any_neighbor_country = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
				}
				NOT = { is_country = ROOT }
				has_communications = PREV
				NOT = { has_communications = ROOT }
			}
		}
	}
	
	immediate = {
		random_country = {
			limit = {		
				is_country_type = default
				NOT = { is_country = ROOT }
				has_communications = ROOT
				any_neighbor_country = {
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
					}
					NOT = { is_country = ROOT }
					has_communications = PREV
					NOT = { has_communications = ROOT }
				}				
			}
			save_event_target_as = CommSpreader
			random_neighbor_country = {
				limit = {
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
					}
					NOT = { is_country = ROOT }
					has_communications = PREV
					NOT = { has_communications = ROOT }
				}
				save_event_target_as = contact_empire
				establish_communications_no_message = root
			}				
		}
	}

	mean_time_to_happen = {
		months = 12
		modifier = {
			factor = 0.5
			years_passed > 5
		}
		modifier = {
			factor = 0.5
			years_passed > 10
		}
		modifier = {
			factor = 0.5
			years_passed > 15
		}		
		modifier = {
			factor = 0.5
			years_passed > 20
		}			
	}

	option = {
		name = "communications_spread.1.a"		
		custom_tooltip = communications_spread.1.tooltip
		hidden_effect = {
			country_event = { id = action.1 }
			event_target:contact_empire = {
				country_event = { id = communications_spread.2 }
			}
		}
	}
}

# Primitives gain comms with anyone that has comms with their system owner
country_event = {
	id = communications_spread.4
	hide_window = yes
	
	trigger = {
		is_country_type = primitive
		capital_scope.solar_system = {
			exists = owner
			owner = {
				NOT = { is_same_value = root }
			}
		}
		any_playable_country = {
			has_communications = capital_scope.solar_system.owner
			NOT = { has_communications = root }
		}
	}
	
	mean_time_to_happen = {
		months = 1
	}
	
	immediate = {
		every_playable_country = {
			limit = { 
				has_communications = root.capital_scope.solar_system.owner 
				NOT = { has_communications = root }
			}
			establish_communications_no_message = root
		}	
	}
}
