###############################################
# ORDER OF THE SACRED MAGDALENE
# SACRED REGALIA COMMISSION EVENTS
#
# File:
# events/ggsm_regalia_events.txt
#
# Rank 1 = Q1 = 10% current wealth
# Rank 2 = Q2 = 15% current wealth
# Rank 3 = Q3 = 20% current wealth
# Rank 4 = Q4 = 25% current wealth
#
# No RNG. No failure. No theft.
# Payment guarantees the artifact.
###############################################

namespace = ggsm


###############################################
# 400 - CHOOSE ORDER ARTISAN
###############################################

long_character_event = {
    id = ggsm.400
    desc = ggsm.400.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy

    is_triggered_only = yes

    trigger = {
        society_member_of = ggsm_order_sacred_magdalene
        NOT = { has_character_flag = ggsm_regalia_commission_in_progress }
    }

    option = {
        name = ggsm.400.a
        long_character_event = { id = ggsm.401 }
    }

    option = {
        name = ggsm.400.b
        long_character_event = { id = ggsm.402 }
    }

    option = {
        name = ggsm.400.c
        long_character_event = { id = ggsm.403 }
    }

    option = {
        name = ggsm.400.d
        long_character_event = { id = ggsm.404 }
    }

    option = {
        name = ggsm.400.e
    }
}


###############################################
# 401 - ORDER LEATHERWORKER
###############################################

long_character_event = {
    id = ggsm.401
    desc = ggsm.401.desc
    picture = GFX_evt_weaponsmith
    border = GFX_event_long_frame_economy

    is_triggered_only = yes

    option = {
        name = ggsm.401.a
        set_character_flag = ggsm_commission_harness
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.401.b
        set_character_flag = ggsm_commission_scourge
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.401.c
        set_character_flag = ggsm_commission_choker
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.401.d
        long_character_event = { id = ggsm.400 }
    }
}


###############################################
# 402 - ORDER WEAPONSMITH
###############################################

long_character_event = {
    id = ggsm.402
    desc = ggsm.402.desc
    picture = GFX_evt_weaponsmith
    border = GFX_event_long_frame_economy

    is_triggered_only = yes

    option = {
        name = ggsm.402.a
        set_character_flag = ggsm_commission_rod
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.402.b
        long_character_event = { id = ggsm.400 }
    }
}


###############################################
# 403 - ORDER GOLDSMITH
###############################################

long_character_event = {
    id = ggsm.403
    desc = ggsm.403.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy

    is_triggered_only = yes

    option = {
        name = ggsm.403.a
        set_character_flag = ggsm_commission_scepter
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.403.b
        set_character_flag = ggsm_commission_crown
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.403.c
        set_character_flag = ggsm_commission_cuffs
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.403.d
        set_character_flag = ggsm_commission_piercings
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.403.e
        long_character_event = { id = ggsm.400 }
    }
}


###############################################
# 404 - ORDER VEIL-MAKER
###############################################

long_character_event = {
    id = ggsm.404
    desc = ggsm.404.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy

    is_triggered_only = yes

    option = {
        name = ggsm.404.a
        set_character_flag = ggsm_commission_veil
        long_character_event = { id = ggsm.410 }
    }

    option = {
        name = ggsm.404.b
        long_character_event = { id = ggsm.400 }
    }
}


###############################################
# 410 - CALCULATE COST AND CONFIRM
###############################################

long_character_event = {
    id = ggsm.410
    desc = ggsm.410.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy

    is_triggered_only = yes

    immediate = {

        export_to_variable = {
            which = ggsm_regalia_cost
            value = wealth
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 1
                }
            }
            multiply_variable = {
                which = ggsm_regalia_cost
                value = -0.10
            }
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 2
                }
            }
            multiply_variable = {
                which = ggsm_regalia_cost
                value = -0.15
            }
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 3
                }
            }
            multiply_variable = {
                which = ggsm_regalia_cost
                value = -0.20
            }
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 4
                }
            }
            multiply_variable = {
                which = ggsm_regalia_cost
                value = -0.25
            }
        }
    }

    option = {
        name = ggsm.410.a

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 1
                }
            }
            custom_tooltip = { text = ggsm_regalia_rank_1_tt }
            custom_tooltip = { text = ggsm_regalia_cost_rank_1_tt }
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 2
                }
            }
            custom_tooltip = { text = ggsm_regalia_rank_2_tt }
            custom_tooltip = { text = ggsm_regalia_cost_rank_2_tt }
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 3
                }
            }
            custom_tooltip = { text = ggsm_regalia_rank_3_tt }
            custom_tooltip = { text = ggsm_regalia_cost_rank_3_tt }
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 4
                }
            }
            custom_tooltip = { text = ggsm_regalia_rank_4_tt }
            custom_tooltip = { text = ggsm_regalia_cost_rank_4_tt }
        }

        custom_tooltip = { text = ggsm_regalia_guaranteed_tt }
        custom_tooltip = { text = ggsm_regalia_crafting_time_tt }

        wealth = ggsm_regalia_cost

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 1
                }
            }
            set_character_flag = ggsm_commission_q1
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 2
                }
            }
            set_character_flag = ggsm_commission_q2
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 3
                }
            }
            set_character_flag = ggsm_commission_q3
        }

        if = {
            limit = {
                society_rank = {
                    society = ggsm_order_sacred_magdalene
                    rank == 4
                }
            }
            set_character_flag = ggsm_commission_q4
        }

        set_character_flag = ggsm_regalia_commission_in_progress

        long_character_event = {
            id = ggsm.420
            days = 60
        }
    }

    option = {
        name = ggsm.410.b

        clr_character_flag = ggsm_commission_harness
        clr_character_flag = ggsm_commission_piercings
        clr_character_flag = ggsm_commission_rod
        clr_character_flag = ggsm_commission_scourge
        clr_character_flag = ggsm_commission_scepter
        clr_character_flag = ggsm_commission_crown
        clr_character_flag = ggsm_commission_cuffs
        clr_character_flag = ggsm_commission_choker
        clr_character_flag = ggsm_commission_veil

        set_variable = { which = ggsm_regalia_cost value = 0 }
    }
}


###############################################
# 420 - HIDDEN COMPLETION DISPATCHER
###############################################

long_character_event = {
    id = ggsm.420
    hide_window = yes
    is_triggered_only = yes

    immediate = {

        if = {
            limit = { has_character_flag = ggsm_commission_harness }
            long_character_event = { id = ggsm.421 }
        }
        else_if = {
            limit = { has_character_flag = ggsm_commission_rod }
            long_character_event = { id = ggsm.422 }
        }
        else_if = {
            limit = { has_character_flag = ggsm_commission_scourge }
            long_character_event = { id = ggsm.423 }
        }
        else_if = {
            limit = { has_character_flag = ggsm_commission_scepter }
            long_character_event = { id = ggsm.424 }
        }
        else_if = {
            limit = { has_character_flag = ggsm_commission_crown }
            long_character_event = { id = ggsm.425 }
        }
        else_if = {
            limit = { has_character_flag = ggsm_commission_cuffs }
            long_character_event = { id = ggsm.426 }
        }
        else_if = {
            limit = { has_character_flag = ggsm_commission_choker }
            long_character_event = { id = ggsm.427 }
        }
        else_if = {
            limit = { has_character_flag = ggsm_commission_veil }
            long_character_event = { id = ggsm.428 }
        }
        else_if = {
            limit = { has_character_flag = ggsm_commission_piercings }
            long_character_event = { id = ggsm.429 }
        }
    }
}


###############################################
# 421 - HARNESS COMPLETE
###############################################

long_character_event = {
    id = ggsm.421
    desc = ggsm.421.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.421.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_penitents_harness
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_penitents_harness }
                destroy_artifact = ggsm_penitents_harness
            }
            add_artifact = ggsm_harness_sacred_charity
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_penitents_harness }
                destroy_artifact = ggsm_penitents_harness
            }
            if = {
                limit = { has_artifact = ggsm_harness_sacred_charity }
                destroy_artifact = ggsm_harness_sacred_charity
            }
            add_artifact = ggsm_harness_open_mercy
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_penitents_harness }
                destroy_artifact = ggsm_penitents_harness
            }
            if = {
                limit = { has_artifact = ggsm_harness_sacred_charity }
                destroy_artifact = ggsm_harness_sacred_charity
            }
            if = {
                limit = { has_artifact = ggsm_harness_open_mercy }
                destroy_artifact = ggsm_harness_open_mercy
            }
            add_artifact = ggsm_courtesans_harness
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 422 - ROD COMPLETE
###############################################

long_character_event = {
    id = ggsm.422
    desc = ggsm.422.desc
    picture = GFX_evt_weaponsmith
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.422.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_penitents_rod
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_penitents_rod }
                destroy_artifact = ggsm_penitents_rod
            }
            add_artifact = ggsm_rod_sacred_charity
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_penitents_rod }
                destroy_artifact = ggsm_penitents_rod
            }
            if = {
                limit = { has_artifact = ggsm_rod_sacred_charity }
                destroy_artifact = ggsm_rod_sacred_charity
            }
            add_artifact = ggsm_rod_open_mercy
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_penitents_rod }
                destroy_artifact = ggsm_penitents_rod
            }
            if = {
                limit = { has_artifact = ggsm_rod_sacred_charity }
                destroy_artifact = ggsm_rod_sacred_charity
            }
            if = {
                limit = { has_artifact = ggsm_rod_open_mercy }
                destroy_artifact = ggsm_rod_open_mercy
            }
            add_artifact = ggsm_great_rod_magdalene
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 423 - SCOURGE COMPLETE
###############################################

long_character_event = {
    id = ggsm.423
    desc = ggsm.423.desc
    picture = GFX_evt_weaponsmith
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.423.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_penitents_scourge
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_penitents_scourge }
                destroy_artifact = ggsm_penitents_scourge
            }
            add_artifact = ggsm_scourge_confession
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_penitents_scourge }
                destroy_artifact = ggsm_penitents_scourge
            }
            if = {
                limit = { has_artifact = ggsm_scourge_confession }
                destroy_artifact = ggsm_scourge_confession
            }
            add_artifact = ggsm_scourge_sacred_discipline
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_penitents_scourge }
                destroy_artifact = ggsm_penitents_scourge
            }
            if = {
                limit = { has_artifact = ggsm_scourge_confession }
                destroy_artifact = ggsm_scourge_confession
            }
            if = {
                limit = { has_artifact = ggsm_scourge_sacred_discipline }
                destroy_artifact = ggsm_scourge_sacred_discipline
            }
            add_artifact = ggsm_magdalene_scourge
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 424 - SCEPTER COMPLETE
###############################################

long_character_event = {
    id = ggsm.424
    desc = ggsm.424.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.424.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_ivory_wand
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_ivory_wand }
                destroy_artifact = ggsm_ivory_wand
            }
            add_artifact = ggsm_scepter_charity
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_ivory_wand }
                destroy_artifact = ggsm_ivory_wand
            }
            if = {
                limit = { has_artifact = ggsm_scepter_charity }
                destroy_artifact = ggsm_scepter_charity
            }
            add_artifact = ggsm_scepter_carnal_charity
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_ivory_wand }
                destroy_artifact = ggsm_ivory_wand
            }
            if = {
                limit = { has_artifact = ggsm_scepter_charity }
                destroy_artifact = ggsm_scepter_charity
            }
            if = {
                limit = { has_artifact = ggsm_scepter_carnal_charity }
                destroy_artifact = ggsm_scepter_carnal_charity
            }
            add_artifact = ggsm_scepter_courtesan
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 425 - CROWN COMPLETE
###############################################

long_character_event = {
    id = ggsm.425
    desc = ggsm.425.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.425.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_rose_circlet
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_rose_circlet }
                destroy_artifact = ggsm_rose_circlet
            }
            add_artifact = ggsm_silver_crown_magdalene
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_rose_circlet }
                destroy_artifact = ggsm_rose_circlet
            }
            if = {
                limit = { has_artifact = ggsm_silver_crown_magdalene }
                destroy_artifact = ggsm_silver_crown_magdalene
            }
            add_artifact = ggsm_crown_open_mercy
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_rose_circlet }
                destroy_artifact = ggsm_rose_circlet
            }
            if = {
                limit = { has_artifact = ggsm_silver_crown_magdalene }
                destroy_artifact = ggsm_silver_crown_magdalene
            }
            if = {
                limit = { has_artifact = ggsm_crown_open_mercy }
                destroy_artifact = ggsm_crown_open_mercy
            }
            add_artifact = ggsm_crown_courtesan
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 426 - CUFFS COMPLETE
###############################################

long_character_event = {
    id = ggsm.426
    desc = ggsm.426.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.426.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_leather_cuffs
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_leather_cuffs }
                destroy_artifact = ggsm_leather_cuffs
            }
            add_artifact = ggsm_silver_cuffs_mercy
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_leather_cuffs }
                destroy_artifact = ggsm_leather_cuffs
            }
            if = {
                limit = { has_artifact = ggsm_silver_cuffs_mercy }
                destroy_artifact = ggsm_silver_cuffs_mercy
            }
            add_artifact = ggsm_bracelets_sacred_submission
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_leather_cuffs }
                destroy_artifact = ggsm_leather_cuffs
            }
            if = {
                limit = { has_artifact = ggsm_silver_cuffs_mercy }
                destroy_artifact = ggsm_silver_cuffs_mercy
            }
            if = {
                limit = { has_artifact = ggsm_bracelets_sacred_submission }
                destroy_artifact = ggsm_bracelets_sacred_submission
            }
            add_artifact = ggsm_golden_bonds_magdalene
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 427 - CHOKER COMPLETE
###############################################

long_character_event = {
    id = ggsm.427
    desc = ggsm.427.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.427.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_penitents_choker
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_penitents_choker }
                destroy_artifact = ggsm_penitents_choker
            }
            add_artifact = ggsm_choker_sacred_charity
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_penitents_choker }
                destroy_artifact = ggsm_penitents_choker
            }
            if = {
                limit = { has_artifact = ggsm_choker_sacred_charity }
                destroy_artifact = ggsm_choker_sacred_charity
            }
            add_artifact = ggsm_choker_open_mercy
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_penitents_choker }
                destroy_artifact = ggsm_penitents_choker
            }
            if = {
                limit = { has_artifact = ggsm_choker_sacred_charity }
                destroy_artifact = ggsm_choker_sacred_charity
            }
            if = {
                limit = { has_artifact = ggsm_choker_open_mercy }
                destroy_artifact = ggsm_choker_open_mercy
            }
            add_artifact = ggsm_courtesans_choker
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 428 - VEIL COMPLETE
###############################################

long_character_event = {
    id = ggsm.428
    desc = ggsm.428.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.428.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_penitents_veil
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_penitents_veil }
                destroy_artifact = ggsm_penitents_veil
            }
            add_artifact = ggsm_veil_sacred_charity
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_penitents_veil }
                destroy_artifact = ggsm_penitents_veil
            }
            if = {
                limit = { has_artifact = ggsm_veil_sacred_charity }
                destroy_artifact = ggsm_veil_sacred_charity
            }
            add_artifact = ggsm_veil_open_mercy
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_penitents_veil }
                destroy_artifact = ggsm_penitents_veil
            }
            if = {
                limit = { has_artifact = ggsm_veil_sacred_charity }
                destroy_artifact = ggsm_veil_sacred_charity
            }
            if = {
                limit = { has_artifact = ggsm_veil_open_mercy }
                destroy_artifact = ggsm_veil_open_mercy
            }
            add_artifact = ggsm_courtesans_veil
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 429 - SACRED PIERCINGS COMPLETE
###############################################

long_character_event = {
    id = ggsm.429
    desc = ggsm.429.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_economy
    is_triggered_only = yes

    option = {
        name = ggsm.429.a

        if = {
            limit = { has_character_flag = ggsm_commission_q1 }
            add_artifact = ggsm_penitents_rings
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q2 }
            if = {
                limit = { has_artifact = ggsm_penitents_rings }
                destroy_artifact = ggsm_penitents_rings
            }
            add_artifact = ggsm_rings_sacred_charity
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q3 }
            if = {
                limit = { has_artifact = ggsm_penitents_rings }
                destroy_artifact = ggsm_penitents_rings
            }
            if = {
                limit = { has_artifact = ggsm_rings_sacred_charity }
                destroy_artifact = ggsm_rings_sacred_charity
            }
            add_artifact = ggsm_chains_open_mercy
        }
        if = {
            limit = { has_character_flag = ggsm_commission_q4 }
            if = {
                limit = { has_artifact = ggsm_penitents_rings }
                destroy_artifact = ggsm_penitents_rings
            }
            if = {
                limit = { has_artifact = ggsm_rings_sacred_charity }
                destroy_artifact = ggsm_rings_sacred_charity
            }
            if = {
                limit = { has_artifact = ggsm_chains_open_mercy }
                destroy_artifact = ggsm_chains_open_mercy
            }
            add_artifact = ggsm_jewels_courtesan
        }

        long_character_event = { id = ggsm.430 }
    }
}


###############################################
# 430 - CLEANUP
###############################################

long_character_event = {
    id = ggsm.430
    hide_window = yes
    is_triggered_only = yes

    immediate = {

        clr_character_flag = ggsm_commission_harness
        clr_character_flag = ggsm_commission_piercings
        clr_character_flag = ggsm_commission_rod
        clr_character_flag = ggsm_commission_scourge
        clr_character_flag = ggsm_commission_scepter
        clr_character_flag = ggsm_commission_crown
        clr_character_flag = ggsm_commission_cuffs
        clr_character_flag = ggsm_commission_choker
        clr_character_flag = ggsm_commission_veil

        clr_character_flag = ggsm_commission_q1
        clr_character_flag = ggsm_commission_q2
        clr_character_flag = ggsm_commission_q3
        clr_character_flag = ggsm_commission_q4

        clr_character_flag = ggsm_regalia_commission_in_progress

        set_variable = { which = ggsm_regalia_cost value = 0 }
    }
}


############################################################
# ggsm.432
# BURIAL OF SACRED REGALIA
#
# All 36 wearable artifacts are destroyed at death,
# representing burial with the deceased sister.
# Sacred books are deliberately not destroyed.
############################################################

long_character_event = {
    id = ggsm.432
    desc = ggsm.432.desc
    picture = GFX_evt_treasure_chamber
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    immediate = {
        if = {
            limit = { has_artifact = ggsm_penitents_harness }
            destroy_artifact = ggsm_penitents_harness
        }
        if = {
            limit = { has_artifact = ggsm_penitents_rod }
            destroy_artifact = ggsm_penitents_rod
        }
        if = {
            limit = { has_artifact = ggsm_penitents_scourge }
            destroy_artifact = ggsm_penitents_scourge
        }
        if = {
            limit = { has_artifact = ggsm_ivory_wand }
            destroy_artifact = ggsm_ivory_wand
        }
        if = {
            limit = { has_artifact = ggsm_rose_circlet }
            destroy_artifact = ggsm_rose_circlet
        }
        if = {
            limit = { has_artifact = ggsm_leather_cuffs }
            destroy_artifact = ggsm_leather_cuffs
        }
        if = {
            limit = { has_artifact = ggsm_penitents_choker }
            destroy_artifact = ggsm_penitents_choker
        }
        if = {
            limit = { has_artifact = ggsm_penitents_veil }
            destroy_artifact = ggsm_penitents_veil
        }
        if = {
            limit = { has_artifact = ggsm_penitents_rings }
            destroy_artifact = ggsm_penitents_rings
        }

        if = {
            limit = { has_artifact = ggsm_harness_sacred_charity }
            destroy_artifact = ggsm_harness_sacred_charity
        }
        if = {
            limit = { has_artifact = ggsm_rod_sacred_charity }
            destroy_artifact = ggsm_rod_sacred_charity
        }
        if = {
            limit = { has_artifact = ggsm_scourge_confession }
            destroy_artifact = ggsm_scourge_confession
        }
        if = {
            limit = { has_artifact = ggsm_scepter_charity }
            destroy_artifact = ggsm_scepter_charity
        }
        if = {
            limit = { has_artifact = ggsm_silver_crown_magdalene }
            destroy_artifact = ggsm_silver_crown_magdalene
        }
        if = {
            limit = { has_artifact = ggsm_silver_cuffs_mercy }
            destroy_artifact = ggsm_silver_cuffs_mercy
        }
        if = {
            limit = { has_artifact = ggsm_choker_sacred_charity }
            destroy_artifact = ggsm_choker_sacred_charity
        }
        if = {
            limit = { has_artifact = ggsm_veil_sacred_charity }
            destroy_artifact = ggsm_veil_sacred_charity
        }
        if = {
            limit = { has_artifact = ggsm_rings_sacred_charity }
            destroy_artifact = ggsm_rings_sacred_charity
        }

        if = {
            limit = { has_artifact = ggsm_harness_open_mercy }
            destroy_artifact = ggsm_harness_open_mercy
        }
        if = {
            limit = { has_artifact = ggsm_rod_open_mercy }
            destroy_artifact = ggsm_rod_open_mercy
        }
        if = {
            limit = { has_artifact = ggsm_scourge_sacred_discipline }
            destroy_artifact = ggsm_scourge_sacred_discipline
        }
        if = {
            limit = { has_artifact = ggsm_scepter_carnal_charity }
            destroy_artifact = ggsm_scepter_carnal_charity
        }
        if = {
            limit = { has_artifact = ggsm_crown_open_mercy }
            destroy_artifact = ggsm_crown_open_mercy
        }
        if = {
            limit = { has_artifact = ggsm_bracelets_sacred_submission }
            destroy_artifact = ggsm_bracelets_sacred_submission
        }
        if = {
            limit = { has_artifact = ggsm_choker_open_mercy }
            destroy_artifact = ggsm_choker_open_mercy
        }
        if = {
            limit = { has_artifact = ggsm_veil_open_mercy }
            destroy_artifact = ggsm_veil_open_mercy
        }
        if = {
            limit = { has_artifact = ggsm_chains_open_mercy }
            destroy_artifact = ggsm_chains_open_mercy
        }

        if = {
            limit = { has_artifact = ggsm_courtesans_harness }
            destroy_artifact = ggsm_courtesans_harness
        }
        if = {
            limit = { has_artifact = ggsm_great_rod_magdalene }
            destroy_artifact = ggsm_great_rod_magdalene
        }
        if = {
            limit = { has_artifact = ggsm_magdalene_scourge }
            destroy_artifact = ggsm_magdalene_scourge
        }
        if = {
            limit = { has_artifact = ggsm_scepter_courtesan }
            destroy_artifact = ggsm_scepter_courtesan
        }
        if = {
            limit = { has_artifact = ggsm_crown_courtesan }
            destroy_artifact = ggsm_crown_courtesan
        }
        if = {
            limit = { has_artifact = ggsm_golden_bonds_magdalene }
            destroy_artifact = ggsm_golden_bonds_magdalene
        }
        if = {
            limit = { has_artifact = ggsm_courtesans_choker }
            destroy_artifact = ggsm_courtesans_choker
        }
        if = {
            limit = { has_artifact = ggsm_courtesans_veil }
            destroy_artifact = ggsm_courtesans_veil
        }
        if = {
            limit = { has_artifact = ggsm_jewels_courtesan }
            destroy_artifact = ggsm_jewels_courtesan
        }
    }

    option = {
        name = ggsm.432.a
    }
}


############################################################
# ORDER OF THE SACRED MAGDALENE
# SACRED TEXT ARTIFACT EVENTS
#
# 500-series
#
# Book hunting is intentionally NOT a commission.
# The sister chooses a textual tradition, follows a chain of
# rumors and archive leads, and accumulates search progress.
#
# Society rank is a CEILING, not a fixed result:
#
# Rank 1 may recover Q1.
# Rank 2 may recover Q1-Q2.
# Rank 3 may recover Q1-Q3.
# Rank 4 may recover Q1-Q4.
#
# Lower-quality books remain possible at every higher rank.
# Higher tiers are progressively rarer.
# Existing copies are excluded from the result tombola.
#
# The search checks progress every 30 days. Five points are
# required to reach the manuscript tombola. If twelve search
# cycles pass without enough progress, the trail goes cold.
#
# The Rule of Open Mercy is NOT in this search pool.
############################################################


############################################################
# ggsm.500
# CHOOSE THE SACRED TEXT TRADITION TO SEARCH FOR
############################################################

long_character_event = {
    id = ggsm.500
    desc = ggsm.500.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion

    is_triggered_only = yes

    trigger = {
        society_member_of = ggsm_order_sacred_magdalene
        NOT = { has_character_flag = ggsm_book_search_in_progress }
        NOT = { has_character_flag = ggsm_book_search_cooldown }
    }

    option = {
        name = ggsm.500.a
        set_character_flag = ggsm_search_theology
        long_character_event = { id = ggsm.501 }
    }

    option = {
        name = ggsm.500.b
        set_character_flag = ggsm_search_charity
        long_character_event = { id = ggsm.501 }
    }

    option = {
        name = ggsm.500.c
        set_character_flag = ggsm_search_houses
        long_character_event = { id = ggsm.501 }
    }

    option = {
        name = ggsm.500.d
        set_character_flag = ggsm_search_rites
        long_character_event = { id = ggsm.501 }
    }

    option = {
        name = ggsm.500.e
    }
}


############################################################
# ggsm.501
# BEGIN THE SEARCH
############################################################

long_character_event = {
    id = ggsm.501
    desc = ggsm.501.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion

    is_triggered_only = yes

    option = {
        name = ggsm.501.a

        custom_tooltip = { text = ggsm_book_search_rank_quality_tt }
        custom_tooltip = { text = ggsm_book_search_progress_tt }
        custom_tooltip = { text = ggsm_book_search_failure_tt }
        custom_tooltip = { text = ggsm_book_search_duplicates_tt }

        set_character_flag = ggsm_book_search_in_progress
        set_variable = { which = ggsm_book_search_score value = 0 }
        set_variable = { which = ggsm_book_search_cycles value = 0 }

        long_character_event = {
            id = ggsm.510
            days = 30
        }
    }

    option = {
        name = ggsm.501.b

        clr_character_flag = ggsm_search_theology
        clr_character_flag = ggsm_search_charity
        clr_character_flag = ggsm_search_houses
        clr_character_flag = ggsm_search_rites
    }
}


############################################################
# ggsm.510
# MONTHLY BOOK SEARCH EVALUATOR
############################################################

long_character_event = {
    id = ggsm.510
    hide_window = yes
    is_triggered_only = yes

    trigger = {
        has_character_flag = ggsm_book_search_in_progress
        society_member_of = ggsm_order_sacred_magdalene
    }

    immediate = {
        change_variable = { which = ggsm_book_search_cycles value = 1 }

        random_list = {
            35 = {
                modifier = { factor = 1.25 learning = 10 }
                modifier = { factor = 1.35 learning = 15 }
                modifier = { factor = 1.50 learning = 20 }
                modifier = { factor = 1.30 trait = diligent }
                modifier = { factor = 1.20 trait = patient }
                modifier = { factor = 1.25 trait = quick }
                modifier = { factor = 1.40 trait = genius }
                long_character_event = { id = ggsm.530 }
            }

            10 = {
                modifier = { factor = 1.30 learning = 12 }
                modifier = { factor = 1.50 learning = 18 }
                modifier = { factor = 1.35 trait = diligent }
                modifier = { factor = 1.30 trait = patient }
                modifier = { factor = 1.35 trait = quick }
                modifier = { factor = 1.60 trait = genius }
                long_character_event = { id = ggsm.531 }
            }

            20 = {
                modifier = { factor = 1.25 NOT = { learning = 8 } }
                modifier = { factor = 1.25 trait = slothful }
                long_character_event = { id = ggsm.532 }
            }

            15 = {
                modifier = { factor = 1.35 NOT = { learning = 8 } }
                modifier = { factor = 1.50 trait = slothful }
                modifier = { factor = 1.25 trait = arbitrary }
                long_character_event = { id = ggsm.533 }
            }

            10 = {
                long_character_event = { id = ggsm.534 }
            }

            10 = {
                modifier = { factor = 1.25 learning = 10 }
                modifier = { factor = 1.20 trait = lustful }
                modifier = { factor = 1.30 trait = nymphomaniac }
                long_character_event = { id = ggsm.535 }
            }
        }
    }
}


############################################################
# SEARCH PROGRESS EVENTS
############################################################

# Useful archival lead: +1
long_character_event = {
    id = ggsm.530
    desc = ggsm.530.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.530.a
        change_variable = { which = ggsm_book_search_score value = 1 }
        if = {
            limit = {
                check_variable = { which = ggsm_book_search_score value = 5 }
            }
            if = {
                limit = { has_character_flag = ggsm_search_theology }
                long_character_event = { id = ggsm.511 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_charity }
                long_character_event = { id = ggsm.512 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_houses }
                long_character_event = { id = ggsm.513 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_rites }
                long_character_event = { id = ggsm.514 }
            }
        }
        else_if = {
            limit = {
                check_variable = { which = ggsm_book_search_cycles value = 12 }
            }
            long_character_event = { id = ggsm.519 }
        }
        else = {
            long_character_event = { id = ggsm.510 days = 30 }
        }
    }
}


# Major breakthrough: +2
long_character_event = {
    id = ggsm.531
    desc = ggsm.531.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.531.a
        change_variable = { which = ggsm_book_search_score value = 2 }
        if = {
            limit = {
                check_variable = { which = ggsm_book_search_score value = 5 }
            }
            if = {
                limit = { has_character_flag = ggsm_search_theology }
                long_character_event = { id = ggsm.511 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_charity }
                long_character_event = { id = ggsm.512 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_houses }
                long_character_event = { id = ggsm.513 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_rites }
                long_character_event = { id = ggsm.514 }
            }
        }
        else_if = {
            limit = {
                check_variable = { which = ggsm_book_search_cycles value = 12 }
            }
            long_character_event = { id = ggsm.519 }
        }
        else = {
            long_character_event = { id = ggsm.510 days = 30 }
        }
    }
}


# Empty month: 0
long_character_event = {
    id = ggsm.532
    desc = ggsm.532.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.532.a
        if = {
            limit = {
                check_variable = { which = ggsm_book_search_score value = 5 }
            }
            if = {
                limit = { has_character_flag = ggsm_search_theology }
                long_character_event = { id = ggsm.511 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_charity }
                long_character_event = { id = ggsm.512 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_houses }
                long_character_event = { id = ggsm.513 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_rites }
                long_character_event = { id = ggsm.514 }
            }
        }
        else_if = {
            limit = {
                check_variable = { which = ggsm_book_search_cycles value = 12 }
            }
            long_character_event = { id = ggsm.519 }
        }
        else = {
            long_character_event = { id = ggsm.510 days = 30 }
        }
    }
}


# False trail / destruction: -1
long_character_event = {
    id = ggsm.533
    desc = ggsm.533.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.533.a
        change_variable = { which = ggsm_book_search_score value = -1 }
        if = {
            limit = {
                check_variable = { which = ggsm_book_search_score value = 5 }
            }
            if = {
                limit = { has_character_flag = ggsm_search_theology }
                long_character_event = { id = ggsm.511 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_charity }
                long_character_event = { id = ggsm.512 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_houses }
                long_character_event = { id = ggsm.513 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_rites }
                long_character_event = { id = ggsm.514 }
            }
        }
        else_if = {
            limit = {
                check_variable = { which = ggsm_book_search_cycles value = 12 }
            }
            long_character_event = { id = ggsm.519 }
        }
        else = {
            long_character_event = { id = ggsm.510 days = 30 }
        }
    }
}


# A keeper has a genuine clue but wants money.
long_character_event = {
    id = ggsm.534
    desc = ggsm.534.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.534.a
        scaled_wealth = -0.05
        change_variable = { which = ggsm_book_search_score value = 2 }
        if = {
            limit = {
                check_variable = { which = ggsm_book_search_score value = 5 }
            }
            if = {
                limit = { has_character_flag = ggsm_search_theology }
                long_character_event = { id = ggsm.511 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_charity }
                long_character_event = { id = ggsm.512 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_houses }
                long_character_event = { id = ggsm.513 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_rites }
                long_character_event = { id = ggsm.514 }
            }
        }
        else_if = {
            limit = {
                check_variable = { which = ggsm_book_search_cycles value = 12 }
            }
            long_character_event = { id = ggsm.519 }
        }
        else = {
            long_character_event = { id = ggsm.510 days = 30 }
        }
    }

    option = {
        name = ggsm.534.b
        if = {
            limit = {
                check_variable = { which = ggsm_book_search_score value = 5 }
            }
            if = {
                limit = { has_character_flag = ggsm_search_theology }
                long_character_event = { id = ggsm.511 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_charity }
                long_character_event = { id = ggsm.512 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_houses }
                long_character_event = { id = ggsm.513 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_rites }
                long_character_event = { id = ggsm.514 }
            }
        }
        else_if = {
            limit = {
                check_variable = { which = ggsm_book_search_cycles value = 12 }
            }
            long_character_event = { id = ggsm.519 }
        }
        else = {
            long_character_event = { id = ggsm.510 days = 30 }
        }
    }
}


# Oral tradition confirms a textual trail: +1
long_character_event = {
    id = ggsm.535
    desc = ggsm.535.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.535.a
        change_variable = { which = ggsm_book_search_score value = 1 }
        if = {
            limit = {
                check_variable = { which = ggsm_book_search_score value = 5 }
            }
            if = {
                limit = { has_character_flag = ggsm_search_theology }
                long_character_event = { id = ggsm.511 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_charity }
                long_character_event = { id = ggsm.512 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_houses }
                long_character_event = { id = ggsm.513 }
            }
            else_if = {
                limit = { has_character_flag = ggsm_search_rites }
                long_character_event = { id = ggsm.514 }
            }
        }
        else_if = {
            limit = {
                check_variable = { which = ggsm_book_search_cycles value = 12 }
            }
            long_character_event = { id = ggsm.519 }
        }
        else = {
            long_character_event = { id = ggsm.510 days = 30 }
        }
    }
}


############################################################
# RESULT TOMBOLAS
#
# Weights:
# Q1 = 60
# Q2 = 30
# Q3 = 15
# Q4 = 5
#
# Rank only unlocks the higher tiers. It never removes
# lower-quality books from the pool.
############################################################


############################################################
# ggsm.511
# THEOLOGY OF MAGDALENE MANUSCRIPT FOUND
############################################################

long_character_event = {
    id = ggsm.511
    desc = ggsm.511.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.511.a

        random_list = {
            60 = {
                modifier = { factor = 0 has_artifact = ggsm_book_meditations_magdalene }
                add_artifact = ggsm_book_meditations_magdalene
            }
            30 = {
                modifier = { factor = 0 NOT = { society_rank >= 2 } }
                modifier = { factor = 0 has_artifact = ggsm_book_reflections_sacred_mercy }
                add_artifact = ggsm_book_reflections_sacred_mercy
            }
            15 = {
                modifier = { factor = 0 NOT = { society_rank >= 3 } }
                modifier = { factor = 0 has_artifact = ggsm_book_theology_open_mercy }
                add_artifact = ggsm_book_theology_open_mercy
            }
            5 = {
                modifier = { factor = 0 NOT = { society_rank >= 4 } }
                modifier = { factor = 0 has_artifact = ggsm_book_great_theology_magdalene }
                add_artifact = ggsm_book_great_theology_magdalene
            }

            # Only survives if every eligible non-duplicate entry above is unavailable.
            1 = {
                modifier = {
                    factor = 0
                    OR = {
                        NOT = { has_artifact = ggsm_book_meditations_magdalene }
                        AND = {
                            society_rank >= 2
                            NOT = { has_artifact = ggsm_book_reflections_sacred_mercy }
                        }
                        AND = {
                            society_rank >= 3
                            NOT = { has_artifact = ggsm_book_theology_open_mercy }
                        }
                        AND = {
                            society_rank >= 4
                            NOT = { has_artifact = ggsm_book_great_theology_magdalene }
                        }
                    }
                }
                set_character_flag = ggsm_book_search_no_new_copy
            }
        }

        if = {
            limit = { has_character_flag = ggsm_book_search_no_new_copy }
            clr_character_flag = ggsm_book_search_no_new_copy
            long_character_event = { id = ggsm.518 }
        }
        else = {
            long_character_event = { id = ggsm.520 }
        }
    }
}


############################################################
# ggsm.512
# CARNAL CHARITY MANUSCRIPT FOUND
############################################################

long_character_event = {
    id = ggsm.512
    desc = ggsm.512.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.512.a

        random_list = {
            60 = {
                modifier = { factor = 0 has_artifact = ggsm_book_lessons_carnal_charity }
                add_artifact = ggsm_book_lessons_carnal_charity
            }
            30 = {
                modifier = { factor = 0 NOT = { society_rank >= 2 } }
                modifier = { factor = 0 has_artifact = ggsm_book_treatise_carnal_charity }
                add_artifact = ggsm_book_treatise_carnal_charity
            }
            15 = {
                modifier = { factor = 0 NOT = { society_rank >= 3 } }
                modifier = { factor = 0 has_artifact = ggsm_book_art_sacred_desire }
                add_artifact = ggsm_book_art_sacred_desire
            }
            5 = {
                modifier = { factor = 0 NOT = { society_rank >= 4 } }
                modifier = { factor = 0 has_artifact = ggsm_book_great_art_carnal_charity }
                add_artifact = ggsm_book_great_art_carnal_charity
            }

            1 = {
                modifier = {
                    factor = 0
                    OR = {
                        NOT = { has_artifact = ggsm_book_lessons_carnal_charity }
                        AND = {
                            society_rank >= 2
                            NOT = { has_artifact = ggsm_book_treatise_carnal_charity }
                        }
                        AND = {
                            society_rank >= 3
                            NOT = { has_artifact = ggsm_book_art_sacred_desire }
                        }
                        AND = {
                            society_rank >= 4
                            NOT = { has_artifact = ggsm_book_great_art_carnal_charity }
                        }
                    }
                }
                set_character_flag = ggsm_book_search_no_new_copy
            }
        }

        if = {
            limit = { has_character_flag = ggsm_book_search_no_new_copy }
            clr_character_flag = ggsm_book_search_no_new_copy
            long_character_event = { id = ggsm.518 }
        }
        else = {
            long_character_event = { id = ggsm.520 }
        }
    }
}


############################################################
# ggsm.513
# HOUSES OF MERCY MANUSCRIPT FOUND
############################################################

long_character_event = {
    id = ggsm.513
    desc = ggsm.513.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.513.a

        random_list = {
            60 = {
                modifier = { factor = 0 has_artifact = ggsm_book_notes_house_mercy }
                add_artifact = ggsm_book_notes_house_mercy
            }
            30 = {
                modifier = { factor = 0 NOT = { society_rank >= 2 } }
                modifier = { factor = 0 has_artifact = ggsm_book_guide_houses_mercy }
                add_artifact = ggsm_book_guide_houses_mercy
            }
            15 = {
                modifier = { factor = 0 NOT = { society_rank >= 3 } }
                modifier = { factor = 0 has_artifact = ggsm_book_administration_sacred_charity }
                add_artifact = ggsm_book_administration_sacred_charity
            }
            5 = {
                modifier = { factor = 0 NOT = { society_rank >= 4 } }
                modifier = { factor = 0 has_artifact = ggsm_book_great_rule_houses_mercy }
                add_artifact = ggsm_book_great_rule_houses_mercy
            }

            1 = {
                modifier = {
                    factor = 0
                    OR = {
                        NOT = { has_artifact = ggsm_book_notes_house_mercy }
                        AND = {
                            society_rank >= 2
                            NOT = { has_artifact = ggsm_book_guide_houses_mercy }
                        }
                        AND = {
                            society_rank >= 3
                            NOT = { has_artifact = ggsm_book_administration_sacred_charity }
                        }
                        AND = {
                            society_rank >= 4
                            NOT = { has_artifact = ggsm_book_great_rule_houses_mercy }
                        }
                    }
                }
                set_character_flag = ggsm_book_search_no_new_copy
            }
        }

        if = {
            limit = { has_character_flag = ggsm_book_search_no_new_copy }
            clr_character_flag = ggsm_book_search_no_new_copy
            long_character_event = { id = ggsm.518 }
        }
        else = {
            long_character_event = { id = ggsm.520 }
        }
    }
}


############################################################
# ggsm.514
# SECRET RITES MANUSCRIPT FOUND
############################################################

long_character_event = {
    id = ggsm.514
    desc = ggsm.514.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.514.a

        random_list = {
            60 = {
                modifier = { factor = 0 has_artifact = ggsm_book_observations_sacred_rites }
                add_artifact = ggsm_book_observations_sacred_rites
            }
            30 = {
                modifier = { factor = 0 NOT = { society_rank >= 2 } }
                modifier = { factor = 0 has_artifact = ggsm_book_hidden_rites }
                add_artifact = ggsm_book_hidden_rites
            }
            15 = {
                modifier = { factor = 0 NOT = { society_rank >= 3 } }
                modifier = { factor = 0 has_artifact = ggsm_book_mysteries_magdalene }
                add_artifact = ggsm_book_mysteries_magdalene
            }
            5 = {
                modifier = { factor = 0 NOT = { society_rank >= 4 } }
                modifier = { factor = 0 has_artifact = ggsm_book_red_book_magdalene }
                add_artifact = ggsm_book_red_book_magdalene
            }

            1 = {
                modifier = {
                    factor = 0
                    OR = {
                        NOT = { has_artifact = ggsm_book_observations_sacred_rites }
                        AND = {
                            society_rank >= 2
                            NOT = { has_artifact = ggsm_book_hidden_rites }
                        }
                        AND = {
                            society_rank >= 3
                            NOT = { has_artifact = ggsm_book_mysteries_magdalene }
                        }
                        AND = {
                            society_rank >= 4
                            NOT = { has_artifact = ggsm_book_red_book_magdalene }
                        }
                    }
                }
                set_character_flag = ggsm_book_search_no_new_copy
            }
        }

        if = {
            limit = { has_character_flag = ggsm_book_search_no_new_copy }
            clr_character_flag = ggsm_book_search_no_new_copy
            long_character_event = { id = ggsm.518 }
        }
        else = {
            long_character_event = { id = ggsm.520 }
        }
    }
}


############################################################
# ggsm.518
# SUCCESSFUL SEARCH, BUT NO NEW ELIGIBLE COPY REMAINS
############################################################

long_character_event = {
    id = ggsm.518
    desc = ggsm.518.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.518.a
        long_character_event = { id = ggsm.520 }
    }
}


############################################################
# ggsm.519
# SEARCH FAILURE
############################################################

long_character_event = {
    id = ggsm.519
    desc = ggsm.519.desc
    picture = GFX_evt_ggsm_sacred_books
    border = GFX_event_long_frame_religion
    is_triggered_only = yes

    option = {
        name = ggsm.519.a
        long_character_event = { id = ggsm.520 }
    }
}


############################################################
# ggsm.520
# CLEAN UP BOOK SEARCH
############################################################

long_character_event = {
    id = ggsm.520
    hide_window = yes
    is_triggered_only = yes

    immediate = {

        clr_character_flag = ggsm_search_theology
        clr_character_flag = ggsm_search_charity
        clr_character_flag = ggsm_search_houses
        clr_character_flag = ggsm_search_rites

        clr_character_flag = ggsm_book_search_q1
        clr_character_flag = ggsm_book_search_q2
        clr_character_flag = ggsm_book_search_q3
        clr_character_flag = ggsm_book_search_q4

        clr_character_flag = ggsm_book_search_no_new_copy
        clr_character_flag = ggsm_book_search_in_progress

        set_variable = { which = ggsm_book_search_score value = 0 }
        set_variable = { which = ggsm_book_search_cycles value = 0 }

        set_character_flag = ggsm_book_search_cooldown

        long_character_event = {
            id = ggsm.521
            days = 7300
        }
    }
}


############################################################
# ggsm.521
# BOOK SEARCH COOLDOWN EXPIRES
############################################################

long_character_event = {
    id = ggsm.521
    hide_window = yes
    is_triggered_only = yes

    immediate = {
        clr_character_flag = ggsm_book_search_cooldown
    }
}
