#################################
# Hive Encounter
# Text : Caligula
# Code : Malthus
#################################

#Note: Also in MEM as Malthus was kind enough to code this for me.

namespace = mem_hive_encounter

event = {
    id = mem_hive_encounter.1
    hide_window = yes
    is_triggered_only = yes

    trigger = {
        years_passed <= 31
        NOT = {
            has_global_flag = mem_hive_encounter_happened
        }
    }

    immediate = {
        IF = {
            limit = {
                any_playable_country = {
                    is_hive_empire = yes
                    is_ai = no
                    any_playable_country = {
                        has_communications = PREV 
                        NOT = {
							is_gestalt = yes
						}
                        OR = {
                            is_neutral_to = PREV
                            is_friendly_to = PREV
                            is_cordial_to = PREV
                            is_protective_to = PREV
                        }
                    }
                }
            }
            random_list = {
                # nothing happens
                1 = {}
                # the event starts
                1 = {
                    set_global_flag = mem_hive_encounter_happened
                    random_playable_country = {
                        limit = {
                            is_hive_empire = yes
                            is_ai = no
                            any_country = {
                                has_communications = PREV 
                                OR = {
                                    is_neutral_to = PREV
                                    is_friendly_to = PREV
                                    is_cordial_to = PREV
                                    is_protective_to = PREV
                                }
                            }
                        }
                        set_country_flag = mem_hive_encounter_hive_country
						save_event_target_as = mem_hive_encounter_hive_country
						if = {
							limit = {
								any_playable_country = {
									is_ai = no
									has_communications = PREV 
									NOT = {
										is_gestalt = yes
									}
									OR = {
										is_neutral_to = PREV
										is_friendly_to = PREV
										is_cordial_to = PREV
										is_protective_to = PREV
									}
								}
							}
							random_playable_country = {
								limit = {
									is_ai = no
									has_communications = PREV 
									is_country_type = default
									NOT = {
										is_gestalt = yes
									}
									OR = {
										is_neutral_to = PREV
										is_friendly_to = PREV
										is_cordial_to = PREV
										is_protective_to = PREV
									}
								}
								set_country_flag = mem_hive_encounter_normal_country
								save_event_target_as = mem_hive_encounter_normal_country
							}
						}
						
						else = {
							random_country = {
								limit = {
									has_communications = PREV 
									is_country_type = default
									NOT = {
										is_gestalt = yes
									}
									OR = {
										is_neutral_to = PREV
										is_friendly_to = PREV
										is_cordial_to = PREV
										is_protective_to = PREV
									}
								}
								set_country_flag = mem_hive_encounter_normal_country
								save_event_target_as = mem_hive_encounter_normal_country
							}
						}
                        country_event = { id = mem_hive_encounter.2 days = 30 random = 15 }
                    }
                }
            }
        }
        ELSE_IF = {
            limit = {
                any_playable_country = {
                    NOT = {
						is_gestalt = yes
					}
                    is_ai = no
                    any_playable_country = {
                        has_communications = PREV 
                        is_hive_empire = yes
                        OR = {
                            is_neutral_to = PREV
                            is_friendly_to = PREV
                            is_cordial_to = PREV
                            is_protective_to = PREV
                        }
                    }
                }
            }
            random_list = {
                # nothing happens
                1 = {}
                # the event starts
                1 = {
                    set_global_flag = mem_hive_encounter_happened
                    random_playable_country = {
                        limit = {
                            NOT = {
								is_gestalt = yes
							}
                            is_ai = no
                            any_playable_country = {
                                has_communications = PREV 
                                is_hive_empire = yes
                                OR = {
                                    is_neutral_to = PREV
                                    is_friendly_to = PREV
                                    is_cordial_to = PREV
                                    is_protective_to = PREV
                                }
                            }
                        }
                        set_country_flag = mem_hive_encounter_normal_country
                        save_event_target_as = mem_hive_encounter_normal_country
                        random_country = {
                            limit = {
                                has_communications = PREV 
                                is_country_type = default
                                is_hive_empire = yes
                                OR = {
                                    is_neutral_to = PREV
                                    is_friendly_to = PREV
                                    is_cordial_to = PREV
                                    is_protective_to = PREV
                                }
                            }
                            set_country_flag = mem_hive_encounter_hive_country
                            save_event_target_as = mem_hive_encounter_hive_country
                        }
                        # country gets message from hivemind event
                        country_event = { id = mem_hive_encounter.3 days = 30 random = 15 }
                    }
                }
            }
        }
    }
}

# Hivemind player decides wether or not to message a normal empire
country_event = {
    id = mem_hive_encounter.2
    title = mem_hive_encounter.2.name
    desc = mem_hive_encounter.2.desc
	picture = GFX_evt_hive_mind
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_normal_country
            }
            save_event_target_as = mem_hive_encounter_normal_country
        }
    }

    option = {
        name = mem_hive_encounter.2.a
        hidden_effect = {
			random_country = {
				limit = {
					has_country_flag = mem_hive_encounter_normal_country
				}
				country_event = { id = mem_hive_encounter.3 }
			}
        }
    }

    option = {
        name = mem_hive_encounter.2.b

    }
}

# Normal empire receives hive proposal
country_event = {
    id = mem_hive_encounter.3
    title = mem_hive_encounter.3.name
    desc = mem_hive_encounter.3.desc
    is_triggered_only = yes
    diplomatic = yes
    
    picture_event_data = {
		portrait = event_target:mem_hive_encounter_hive_country
		planet_background = event_target:mem_hive_encounter_hive_country
		graphical_culture = event_target:mem_hive_encounter_hive_country
		city_level = event_target:mem_hive_encounter_hive_country
		room = event_target:mem_hive_encounter_hive_country
    }
    
    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_hive_country
            }
            save_event_target_as = mem_hive_encounter_hive_country
        }
    }

    # looking for volunteers
    option = {
        name = mem_hive_encounter.3.a
        hidden_effect = {
            IF = {
                limit = {
                    OR = {
                        is_xenophobe = yes
                        AND = {
                            OR = {
                                has_faction = supremacist
				                has_faction = isolationist
                            }
                            NOR = {
                                has_faction = xenoist
                            }
                        }
                    }
                }
                # no volunteers, choose again
                country_event = { id = mem_hive_encounter.5 days = 30 }
            }
            ELSE = {
                random_list = {
                    # no volunteers, choose again
                    1 = {
                        country_event = { id = mem_hive_encounter.5 days = 30 }
                    }
                    # volunteers found
                    3 = {
                        set_global_flag = mem_hive_encounter_answer_1
                        country_event = { id = mem_hive_encounter.4 days = 30 }
                    }
                }
            }
        }
    }

    # sending criminals
    option = {
        name = mem_hive_encounter.3.b
        trigger = {
            is_egalitarian = no
        }
        IF = {
            limit = {
                has_faction = isolationist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = isolationist }
                add_modifier = {
                    modifier = mem_hive_encounter_dealed_with_xenos
                    years = 10 # TODO adjust timer
                }
            }
        }
        IF = {
            limit = {
                has_faction = supremacist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = supremacist }
                add_modifier = {
                    modifier = mem_hive_encounter_dealed_with_xenos
                    years = 10 # TODO adjust timer
                }
            }
        }
        IF = {
            limit = {
                has_faction = progressive
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = progressive }
                add_modifier = {
                    modifier = mem_hive_encounter_traded_prisoners
                    years = 10 # TODO adjust timer
                }
            }
        }
        event_target:mem_hive_encounter_hive_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_helped
            }
        }
        hidden_effect = {
            set_global_flag = mem_hive_encounter_answer_2
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_hive_country
                }
                country_event = { id = mem_hive_encounter.6 days = 30 }
            }
        }
    }

    # sending corpses
    option = {
        name = mem_hive_encounter.3.c
        IF = {
            limit = {
                has_faction = isolationist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = isolationist }
                add_modifier = {
                    modifier = mem_hive_encounter_dealed_with_xenos
                    years = 10 # TODO adjust timer
                }
            }
        }
        IF = {
            limit = {
                has_faction = supremacist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = supremacist }
                add_modifier = {
                    modifier = mem_hive_encounter_dealed_with_xenos
                    years = 10 # TODO adjust timer
                }
            }
        }
        IF = {
            limit = {
                has_faction = traditionalist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = traditionalist }
                add_modifier = {
                    modifier = mem_hive_encounter_desecrated_corpses
                    years = 10 # TODO adjust timer
                }
            }
        }
        event_target:mem_hive_encounter_hive_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_helped
            }
        }
        hidden_effect = {
            set_global_flag = mem_hive_encounter_answer_3
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_hive_country
                }
                country_event = { id = mem_hive_encounter.6 days = 30 }
            }
        }
    }

    # NOPE
    option = {
        name = mem_hive_encounter.3.d
        event_target:mem_hive_encounter_hive_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_help_refused
            }
        }
        hidden_effect = {
            set_global_flag = mem_hive_encounter_answer_4
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_hive_country
                }
                country_event = { id = mem_hive_encounter.6 days = 30 }
            }
        }
    }
}

# volunteers found
country_event = {
    id = mem_hive_encounter.4
    title = mem_hive_encounter.4.name
    desc = mem_hive_encounter.4.desc
    picture = GFX_evt_towel
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_hive_country
            }
            save_event_target_as = mem_hive_encounter_hive_country
            country_event = { id = mem_hive_encounter.6 }
        }
    }

    option = {
        name = mem_hive_encounter.4.b
        event_target:mem_hive_encounter_hive_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_helped
            }
        }
    }
}

# no volunteers found
country_event = {
    id = mem_hive_encounter.5
    title = mem_hive_encounter.5.name
    desc = mem_hive_encounter.5.desc
    picture = GFX_evt_hive_mind
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_hive_country
            }
            save_event_target_as = mem_hive_encounter_hive_country
        }
    }

    # sending criminals
    option = {
        name = mem_hive_encounter.3.b
        trigger = {
            is_egalitarian = no
        }
        IF = {
            limit = {
                has_faction = isolationist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = isolationist }
                add_modifier = {
                    modifier = mem_hive_encounter_dealed_with_xenos
                    years = 10 # TODO adjust timer
                }
            }
        }
        IF = {
            limit = {
                has_faction = supremacist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = supremacist }
                add_modifier = {
                    modifier = mem_hive_encounter_dealed_with_xenos
                    years = 10 # TODO adjust timer
                }
            }
        }
        IF = {
            limit = {
                has_faction = progressive
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = progressive }
                add_modifier = {
                    modifier = mem_hive_encounter_traded_prisoners
                    years = 10 # TODO adjust timer
                }
            }
        }
        event_target:mem_hive_encounter_hive_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_helped
            }
        }
        hidden_effect = {
            set_global_flag = mem_hive_encounter_answer_2
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_hive_country
                }
                country_event = { id = mem_hive_encounter.6 days = 30 }
            }
        }
    }

    # sending corpses
    option = {
        name = mem_hive_encounter.3.c
        IF = {
            limit = {
                has_faction = isolationist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = isolationist }
                add_modifier = {
                    modifier = mem_hive_encounter_dealed_with_xenos
                    years = 10 # TODO adjust timer
                }
            }
        }
        IF = {
            limit = {
                has_faction = supremacist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = supremacist }
                add_modifier = {
                    modifier = mem_hive_encounter_dealed_with_xenos
                    years = 10 # TODO adjust timer
                }
            }
        }
        IF = {
            limit = {
                has_faction = traditionalist
            }
            random_pop_faction = {
                limit = { is_pop_faction_type = traditionalist }
                add_modifier = {
                    modifier = mem_hive_encounter_desecrated_corpses
                    years = 10 # TODO adjust timer
                }
            }
        }
        event_target:mem_hive_encounter_hive_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_helped
            }
        }
        hidden_effect = {
            set_global_flag = mem_hive_encounter_answer_3
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_hive_country
                }
                country_event = { id = mem_hive_encounter.6 days = 30 }
            }
        }
    }

    # NOPE
    option = {
        name = mem_hive_encounter.3.d
        event_target:mem_hive_encounter_hive_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_help_refused
            }
        }
        hidden_effect = {
            set_global_flag = mem_hive_encounter_answer_4
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_hive_country
                }
                country_event = { id = mem_hive_encounter.6 days = 30 }
            }
        }
    }
}

# hive receives answer
country_event = {
    id = mem_hive_encounter.6
    title = mem_hive_encounter.3.name
    desc = {
        trigger = {
            switch = {
                trigger = has_global_flag
                mem_hive_encounter_answer_1 = { text = mem_hive_encounter.6.desc.a }
                mem_hive_encounter_answer_2 = { text = mem_hive_encounter.6.desc.b }
                mem_hive_encounter_answer_3 = { text = mem_hive_encounter.6.desc.c }
                mem_hive_encounter_answer_4 = { text = mem_hive_encounter.6.desc.d }
            }
        }
    }
    is_triggered_only = yes
    diplomatic = yes
    
    picture_event_data = {
		portrait = event_target:mem_hive_encounter_normal_country
		planet_background = event_target:mem_hive_encounter_normal_country
		graphical_culture = event_target:mem_hive_encounter_normal_country
		city_level = event_target:mem_hive_encounter_normal_country
		room = event_target:mem_hive_encounter_normal_country
    }
    
    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_normal_country
            }
            save_event_target_as = mem_hive_encounter_normal_country
        }
    }

    option = {
        name = mem_hive_encounter.6.a
        trigger = {
            OR = {
                has_global_flag = mem_hive_encounter_answer_1
                has_global_flag = mem_hive_encounter_answer_2
                has_global_flag = mem_hive_encounter_answer_3
            }
        }
        hidden_effect = {
            country_event = { id = mem_hive_encounter.7 days = 30 }
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_normal_country
                }
                country_event = { id = mem_hive_encounter.9 days = 30 }
            }
        }
    }

    option = {
        name = mem_hive_encounter.6.b
        trigger = {
            has_global_flag = mem_hive_encounter_answer_4
        }
    }
}

# specimens arrive
country_event = {
    id = mem_hive_encounter.7
    title = mem_hive_encounter.7.name
    desc = {
        trigger = {
            switch = {
                trigger = has_global_flag
                mem_hive_encounter_answer_1 = { text = mem_hive_encounter.7.desc.a }
                mem_hive_encounter_answer_2 = { text = mem_hive_encounter.7.desc.b }
                mem_hive_encounter_answer_3 = { text = mem_hive_encounter.7.desc.c }
            }
        }
    }
    picture = GFX_evt_society_research
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_normal_country
            }
            save_event_target_as = mem_hive_encounter_normal_country
        }
    }

    option = {
        name = mem_hive_encounter.7.a
#        trigger = {
#            has_global_flag = mem_hive_encounter_answer_1
#        }
        hidden_effect = {
            country_event = { id = mem_hive_encounter.8 days = 360 random = 25 }
        }
    }
}

# results for hive
country_event = {
    id = mem_hive_encounter.8
    title = mem_hive_encounter.8.name
    desc = {
        trigger = {
            switch = {
                trigger = has_global_flag
                mem_hive_encounter_answer_1 = { text = mem_hive_encounter.8.desc.a }
                mem_hive_encounter_answer_2 = { text = mem_hive_encounter.8.desc.a }
                mem_hive_encounter_answer_3 = { text = mem_hive_encounter.8.desc.b }
            }
        }
    }
    picture = GFX_evt_society_research
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_normal_country
            }
            save_event_target_as = mem_hive_encounter_normal_country
        }
    }

    option = {
        name = mem_hive_encounter.8.a
        add_monthly_resource_mult = {
            resource = society_research
            value = @tier3researchreward
            min = @tier3researchmin
            max = @tier3researchmax
        }
        IF = {
            limit = {
                OR = {
                    has_global_flag = mem_hive_encounter_answer_1
                    has_global_flag = mem_hive_encounter_answer_2
                }
                NOT = {
                    has_technology = tech_planetary_unification
                }
            }
            add_research_option = tech_planetary_unification
        }
        IF = {
            limit = {
                NOT = {
                    has_technology = tech_genome_mapping
                }
            }
            add_research_option = tech_genome_mapping
        }
        IF = {
            limit = {
                has_global_flag = mem_hive_encounter_answer_3
                has_tech_option = tech_genome_mapping
            }
            add_tech_progress = {
                tech = tech_genome_mapping
                progress = 0.20
            }
        }
        hidden_effect = {
            IF = {
                limit = {
                    has_global_flag = mem_hive_encounter_answer_1
                }
                country_event = { id = mem_hive_encounter.11 days = 60 random = 30 }
            }
        }
    }
}

# drones arrive
country_event = {
    id = mem_hive_encounter.9
    title = mem_hive_encounter.9.name
    desc = mem_hive_encounter.9.desc
    picture = GFX_evt_hive_mind
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_hive_country
            }
            save_event_target_as = mem_hive_encounter_hive_country
        }
    }

    option = {
        name = mem_hive_encounter.7.a
        hidden_effect = {
            country_event = { id = mem_hive_encounter.10 days = 360 random = 25 }
        }
    }
}

# results for normal country
country_event = {
    id = mem_hive_encounter.10
    title = mem_hive_encounter.10.name
    desc = {
        trigger = {
            switch = {
                trigger = has_global_flag
                mem_hive_encounter_drone_outcome_1 = { text = mem_hive_encounter.10.desc.a }
                mem_hive_encounter_drone_outcome_2 = { text = mem_hive_encounter.10.desc.b }
            }
        }
    }
    picture = GFX_evt_hive_mind
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_hive_country
            }
            save_event_target_as = mem_hive_encounter_hive_country
        }
        random_list = {
            1 = {
                set_global_flag = mem_hive_encounter_drone_outcome_1
            }
            2 = {
                set_global_flag = mem_hive_encounter_drone_outcome_2
            }
        }
		random_owned_leader = {
			limit = {
				leader_class = scientist
                is_head_of_research = yes
			}
			save_event_target_as = mem_hive_encounter_leader
		}
    }

    option = {
        name = mem_hive_encounter.10.a
        IF = {
            limit = {
                has_global_flag = mem_hive_encounter_drone_outcome_1
            }
            add_monthly_resource_mult = {
                resource = society_research
                value = @tier3researchreward
                min = @tier3researchmin
                max = @tier3researchmax
            }
            IF = {
                limit = {
                    NOT = {
                        has_technology = tech_genome_mapping
                    }
                }
                add_research_option = tech_genome_mapping
            }
            IF = {
                limit = {
                    has_tech_option = tech_genome_mapping
                }
                add_tech_progress = {
                    tech = tech_genome_mapping
                    progress = 0.20
                }
            }
            event_target:mem_hive_encounter_leader = {
                add_trait = {
					trait = leader_trait_expertise_psionics
					show_message = no
				}
				add_experience = 100
            }
        }
        ELSE = {
            add_monthly_resource_mult = {
                resource = society_research
                value = @tier2researchreward
                min = @tier2researchmin
                max = @tier2researchmax
            }
            IF = {
                limit = {
                    NOT = {
                        has_technology = tech_genome_mapping
                    }
                }
                add_research_option = tech_genome_mapping
            }
            IF = {
                limit = {
                    has_tech_option = tech_genome_mapping
                }
                add_tech_progress = {
                    tech = tech_genome_mapping
                    progress = 0.10
                }
            }
        }
    }
}

# decision for hive wether or not to send back the volunteers
country_event = {
    id = mem_hive_encounter.11
    title = mem_hive_encounter.11.name
    desc = mem_hive_encounter.11.desc
    picture = GFX_evt_society_research
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_normal_country
            }
            save_event_target_as = mem_hive_encounter_normal_country
        }
    }

    # send them back
    option = {
        name = mem_hive_encounter.11.a
        event_target:mem_hive_encounter_normal_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_good_hosts
            }
        }
        hidden_effect = {
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_normal_country
                }
                country_event = { id = mem_hive_encounter.12 days = 30 }
            }
        }
    }

    # keep them
    option = {
        name = mem_hive_encounter.11.b
        add_monthly_resource_mult = {
            resource = society_research
            value = @tier3researchreward
            min = @tier3researchmin
            max = @tier3researchmax
        }
        event_target:mem_hive_encounter_normal_country = {
            add_opinion_modifier = {
                who = root
                modifier = mem_opinion_hive_encounter_dishonourable
            }
        }
        hidden_effect = {
            random_country = {
                limit = {
                    has_country_flag = mem_hive_encounter_normal_country
                }
                country_event = { id = mem_hive_encounter.13 days = 30 }
            }
        }
    }
}

# volunteers return
country_event = {
    id = mem_hive_encounter.12
    title = mem_hive_encounter.12.name
    desc = mem_hive_encounter.12.desc
    picture = GFX_evt_towel
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_hive_country
            }
            save_event_target_as = mem_hive_encounter_hive_country
        }
    }

    option = {
        name = mem_hive_encounter.12.a
    }
}

# volunteers missing
country_event = {
    id = mem_hive_encounter.13
    title = mem_hive_encounter.13.name
    desc = mem_hive_encounter.13.desc
    picture = GFX_evt_society_research
    show_sound = event_default
    is_triggered_only = yes

    immediate = {
        random_country = {
            limit = {
                has_country_flag = mem_hive_encounter_hive_country
            }
            save_event_target_as = mem_hive_encounter_hive_country
        }
    }

    option = {
        name = mem_hive_encounter.13.a
    }
}
