﻿namespace = regula_travel_contract_event

# Events for laamp_regula_transport_daughter contract

# Based on regula_tdc_set_contract_scopes_effect in ep3_contract_events.txt
scripted_effect regula_tdc_set_contract_scopes_effect = {
    scope:task_contract.var:pickup_location = {
        save_scope_as = pickup_location
    }
    scope:task_contract.var:journey_target_location = {
        save_scope_as = journey_target_location
    }
    scope:task_contract.task_contract_taker ?= {
        save_scope_as = task_contract_taker
    }
    scope:task_contract.task_contract_employer ?= {
        save_scope_as = task_contract_employer
    }
    if = {
        limit = {
            exists = scope:task_contract.task_contract_destination
        }
        scope:task_contract.task_contract_destination = {
            save_scope_as = task_contract_destination
        }
    }
    else = {
        scope:task_contract.var:task_contract_destination ?= {
            save_scope_as = task_contract_destination
        }
    }
    if = {
        limit = {
            exists = scope:task_contract.var:task_contract_object
        }
        scope:task_contract.var:task_contract_object = {
            save_scope_as = task_contract_object
        }
    }
}


# Triggered when the laamp_regula_transport_daughter contract is accepted
regula_travel_contract_event.0020 = {
    type = character_event
    title = regula_travel_contract_event.0020.t
    desc = regula_travel_contract_event.0020.desc

    theme = landless_adventurer
    override_background = { reference = terrain }

    #task_contract_taker, you
    left_portrait = {
        character = root
        animation = personality_compassionate
    }

    #character giving you the contract
    right_portrait = {
        character = scope:task_contract_employer
        animation = chancellor
    }

    #chracter you have to escort
    lower_right_portrait = {
        character = scope:task_contract_object
        animation = personality_bold
    }

    immediate = {
        regula_tdc_set_contract_scopes_effect = yes
    }

    #take the contract and start the travel without domicile
    option = {

        name = ep3_contract_event.0001.child


        custom_tooltip = regula_travel_contract_event.0020.tt_escort
        # scope:task_contract_object = {
        # 	visit_court_of = root
        # 	add_character_flag = blocked_from_leaving
        # }
        root = {
            #start_contract_travel_without_domicile = yes
            start_travel_plan = {
                destination = scope:pickup_location
                destination = scope:journey_target_location
                destination = scope:pickup_location
                on_arrival_destinations = all
            }
        }

        #AI should always try to complete the contract
        ai_chance = {
            base = 1
        }
    }
    #take the contract and start the travel with domicile
    option = {
        name = ep3_contract_event.0001.with_domicile

        custom_tooltip = regula_travel_contract_event.0020.tt_escort

        root = {
            # start_contract_travel_with_domicile = yes
            start_travel_plan = {
                destination = scope:pickup_location
                destination = scope:journey_target_location
                destination = scope:pickup_location
                travel_with_domicile = yes
                return_trip = no
                on_arrival_destinations = all
            }
        }

        custom_tooltip = ep3_contract_event.0001.with_domicile.tt
        #AI should always try to complete the contract
        ai_chance = {
            base = 0
        }
    }
    #invalidate the contract
    option = {
        name = ep3_contract_event.0001.invalidate
        scope:task_contract = {
            invalidate_contract = yes
        }
        #AI should always try to complete the contract
        ai_chance = {
            base = 0
        }
    }
}

# Triggered when player arrives to the pickup location.
regula_travel_contract_event.0021 = {
    type = character_event
    title = regula_travel_contract_event.0020.t
    desc = regula_travel_contract_event.0021.desc

    theme = landless_adventurer
    override_background = { reference = terrain }
    #task_contract_taker, you
    left_portrait = {
        character = scope:task_contract_object
        animation = personality_bold
    }

    right_portrait = {
        character = scope:task_contract_employer
        #animation = personality_compassionate
        animation = love
    }


    immediate = {
        regula_tdc_set_contract_scopes_effect = yes

        scope:task_contract = {
            set_variable = visited_pickup_location
            set_variable = {
                name = task_contract_destination
                value = scope:journey_target_location
            }
        }
    }

    option = {

        name = ep3_contract_event.0001.child

        custom_tooltip = regula_travel_contract_event.0020.tt_escort

        scope:task_contract_object = {
            visit_court_of = root
            add_character_flag = blocked_from_leaving
        }

        #AI should always try to complete the contract
        ai_chance = {
            base = 1
        }
    }
    #invalidate the contract
    option = {
        name = ep3_contract_event.0001.invalidate
        scope:task_contract = {
            invalidate_contract = yes
        }
        #AI should always try to complete the contract
        ai_chance = {
            base = 0
        }
    }
}

# Triggered when player arrives to the journey_target_location.
regula_travel_contract_event.0022 = {
    type = character_event
    hidden = yes

    immediate = {
        regula_tdc_set_contract_scopes_effect = yes

        scope:task_contract = {
            set_variable = visited_journey_target_location
            set_variable = {
                name = task_contract_destination
                value = scope:task_contract.var:pickup_location
            }
        }

        scope:task_contract.var:task_contract_object = {
            if = {
                limit = {
                    has_trait = devoted_trait_group
                }
                root = {
                    trigger_event = regula_travel_contract_event.0024
                }
            }
            else_if = {
                root = {
                    trigger_event = regula_travel_contract_event.0023
                }
            }
        }
    }
}

# Triggered when player arrives to the journey_target_location, the girl is not charmed yet
regula_travel_contract_event.0023 = {
    type = character_event
    title = regula_travel_contract_event.0020.t
    desc = regula_travel_contract_event.0023.desc

    theme = landless_adventurer
    override_background = { reference = terrain }

    #task_contract_taker, you
    right_portrait = {
        character = scope:task_contract_object
        animation = personality_coward
    }

    immediate = {
        regula_tdc_set_contract_scopes_effect = yes
        scope:task_contract.var:journey_target_location = {
            save_scope_as = journey_target_location
        }

    }

    # Standard Charm
    option = {
        name = regula_travel_contract_event.0023.charm
        flavor = fascinare_success.1000.charm.tt

        trigger = {
            has_trait = magister_trait_group
        }

        add_piety_no_experience = regula_fascinare_rebate
        add_piety = { subtract = regula_fascinare_cost }
        scope:task_contract_object = {
            fascinare_success_effect = { CHARACTER = root }
            # TODO: Make event-specific memory
            create_memory_fascinare_default = { CHARACTER = root }

            # add_character_flag = {
            # 	flag = is_naked
            # 	days = 1
            # }
        }

        ai_chance = {
            base = 100
        }
    }

    # Standard Sex
    option = {
        name = regula_travel_contract_event.0023.charm_and_sex

        trigger = {
            has_trait = magister_trait_group
            scope:task_contract_object = {
                is_visibly_fertile = yes
            }
        }

        add_piety = { subtract = regula_fascinare_cost }
        scope:task_contract_object = {
            fascinare_success_effect = { CHARACTER = root }
            # TODO: Make event-specific memory
            create_memory_fascinare_default = { CHARACTER = root }

            # add_character_flag = {
            # 	flag = is_naked
            # 	days = 1
            # }
        }

        regula_sex_with_target_normal = { TARGET = scope:task_contract_object }

        ai_chance = {
            base = 100
        }
    }

    # No charm
    option = {
        name = regula_travel_contract_event.0023.no_charm

        ai_chance = {
            base = 0
        }
    }
}

# Triggered when player arrives to the journey_target_location and the girl is already charmed
regula_travel_contract_event.0024 = {
    type = character_event
    title = regula_travel_contract_event.0020.t
    desc = regula_travel_contract_event.0024.desc

    theme = landless_adventurer
    override_background = { reference = terrain }
    #task_contract_taker, you
    right_portrait = {
        character = scope:task_contract_object
        animation = love
    }


    immediate = {
        regula_tdc_set_contract_scopes_effect = yes
        scope:task_contract.var:journey_target_location = {
            save_scope_as = journey_target_location
        }
    }

    # Standard Sex
    option = {
        name = fascinare_success.1000.sex

        regula_sex_with_target_normal = { TARGET = scope:task_contract_object }
        if = {
            limit = {
                scope:task_contract_object = {
                    is_pregnant = yes
                    NOT = { has_trait = regula_blessed_pregnancy }
                }
            }
            scope:task_contract_object = {
                set_pregnancy_gender = female
                add_trait = regula_blessed_pregnancy
            }
        }

        ai_chance = {
            base = 100
        }
    }

    # Do nothing
    option = {
        name = regula_travel_contract_event.0024.do_nothing

        ai_chance = {
            base = 0
        }
    }
}

# Completion event
regula_travel_contract_event.0028 = {
    type = character_event
    title = regula_travel_contract_event.0020.t
    desc = {
        first_valid = {
            triggered_desc = {
                trigger = {
                    scope:task_contract.var:task_contract_object ?= {
                        # Not charmed -> Mother is not happy
                        is_regula_devoted_trigger = no
                    }
                }
                desc = regula_travel_contract_event.0028.desc_not_charmed
            }
            desc = regula_travel_contract_event.0028.desc
        }
    }


    theme = landless_adventurer
    override_background = { reference = throne_room }
    left_portrait = {
        character = scope:task_contract_object
        animation = personality_compassionate
    }
    right_portrait = {
        character = scope:task_contract_employer

        animation = personality_compassionate
    }


    immediate = {
        regula_tdc_set_contract_scopes_effect = yes

        root.current_travel_plan = { remove_character = scope:task_contract_object }

        scope:task_contract_object = {
            remove_variable = blocked_from_leaving
            visit_court_of = scope:task_contract_employer
        }

    }


    option = {
        name = ep3_contract_event.0011.b
        trigger = {
            scope:task_contract.var:task_contract_object = {
                is_ruler = no
                is_married = no
            }
        }
        add_courtier = scope:task_contract_object
        current_travel_plan = {
            add_companion = scope:task_contract_object
        }
        #AI should be willing to take more people, as it's probably their best bet to get them
        ai_chance = {
            base = 200
        }
    }

    # Standard Sex
    option = {
        name = regula_travel_contract_event.0028.sex_with_employer

        regula_sex_with_target_normal = { TARGET = scope:task_contract_employer }

        ai_chance = {
            base = 50
        }
    }

    # Sex with both
    option = {
        name = regula_travel_contract_event.0028.sex_with_both

        trigger = {
            scope:task_contract_object = {
                has_trait = devoted_trait_group
            }
        }

        regula_sex_with_target_normal = { TARGET = scope:task_contract_employer }
        regula_sex_with_target_normal = { TARGET = scope:task_contract_object }

        ai_chance = {
            base = 100
        }
    }
    # Just stop
    option = {
        name = regula_travel_contract_event.0028.finish

        ai_chance = {
            base = 0
        }
    }

    after = {
        scope:task_contract = {
            if = {
                limit = {
                    var:task_contract_object ?= {
                        is_alive = no
                    }
                }
                complete_task_contract = failure_standard
            }
            else_if = {
                # You was supposed to charm her -> Mother is not happy
                limit = {
                    var:task_contract_object ?= {
                        NOT = {
                            has_trait = devoted_trait_group
                        }
                    }
                }
                complete_task_contract = success_qualified
                var:task_contract_object ?= {
                    # Prevent creation of a new contract with same character for a year
                    set_variable = {
                        name = traveled_without_chariming
                        value = yes
                        days = 1068 # 3 years
                    }
                }
            }
            else_if = {
                limit = {
                    # It took too long to finish the contract
                    time_since_contract_taken > 360
                }
                complete_task_contract = success_standard
            }
            else = {
                complete_task_contract = success_critical
            }


        }
    }
}

#transport contract invalidated - triggered whenever a transport contract is invalidated for any reason
#on_invalidation has to specify scope:task_contract_invalidation_reason
# Based on ep3_contract_event.0012
regula_travel_contract_event.0029 = {
    type = letter_event
    opening = {
        triggered_desc = {
            trigger = {
                scope:task_contract_invalidation_reason = flag:escortee_dead
            }
            desc = ep3_contract_event.0012.opening_angry
        }
        triggered_desc = {
            trigger = {
                scope:task_contract_invalidation_reason = flag:employer_dead
            }
            desc = ep3_contract_event.0012.opening
        }
    }
    desc = {
        first_valid = {
            triggered_desc = {
                trigger = { scope:task_contract_invalidation_reason = flag:employer_dead }
                desc = ep3_contract_event.0012.desc_employer_dead
            }
            triggered_desc = {
                trigger = { scope:task_contract_invalidation_reason = flag:escortee_dead }
                desc = ep3_contract_event.0012.desc_escortee_dead
            }
        }
    }
    sender = scope:sender
    immediate = {
        if = {
            limit = {
                exists = current_travel_plan
                NOT = { exists = scope:travel_destination }
            }
            current_travel_plan.final_destination_province = {
                save_scope_as = travel_destination
            }
        }
        #For those with heirs
        if = {
            limit = { exists = scope:employer_heir }
            scope:employer_heir = { save_scope_as = sender }
        }
        #Or just the employer, straight up
        else = {
            scope:task_contract_employer = { save_scope_as = sender }
        }

        #stuff that would be nice to see in tooltip, but is already executed in on_invalidation
        show_as_tooltip = {
            if = {
                limit = {
                    scope:task_contract_object ?= { is_alive = yes }
                }
                scope:task_contract_object = { return_to_court = yes }
            }
        }
        switch = {
            trigger = scope:task_contract_invalidation_reason
            #employer or target died, get gold compensation
            flag:employer_dead = { add_gold = task_contract_invalidated_gold_gain_value }
            #character you were suppose to escort died
            flag:escortee_dead = {
                add_prestige = task_contract_failure_prestige_loss_full_value
                scope:task_contract_employer = {
                    progress_towards_rival_effect = {
                        REASON = rival_escortee_died
                        CHARACTER = root
                        OPINION = default_rival_opinion
                    }
                }
            }
        }
    }
    #Let's head home
    option = {
        name = {
            trigger = {
                scope:task_contract_invalidation_reason = flag:employer_dead
            }
            text = ep3_contract_event.0012.a
        }
        name = {
            trigger = {
                scope:task_contract_invalidation_reason = flag:escortee_dead
            }
            text = ep3_contract_event.0012.b
        }
        return_home = yes
        ai_chance = {
            base = 10
        }
    }
    #Keep going
    option = {
        name = ep3_contract_event.0012.c
        trigger = { exists = scope:travel_destination }
        ai_chance = {
            base = 0
        }
    }
    after = {
        remove_variable = invalidated_task_contract_type
    }
}

