﻿### Character Editor Presents / Traits Remove All Traits
DI_clear_all_traits_sgui = {
    scope = character

    effect = {
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_living_character = {
                limit = { has_character_flag = pinned_for_edit }



                clear_traits = yes
            }
        }
        else = {
            clear_traits = yes
        }
    }
}

### Pin Trait ###
DI_pin_trait = {
    scope = character

    saved_scopes = {
        trait # selected trait
    }

    effect = {
        add_to_variable_list = {
            name = DI_pinned_traits
            target = scope:trait
        }

        if = {
            limit = {
                NOT = { owns_story_of_type = DI_traits_story }
            }
            create_story = DI_traits_story
        }
        else = {
            every_owned_story = {
                limit = {
                    story_type = DI_traits_story
                }

                add_to_variable_list = {
                    name = DI_pinned_traits
                    target = scope:trait
                }
            }
        }
    }
}
### Unpin Trait ###
DI_unpin_trait = {
    scope = character

    saved_scopes = {
        trait # selected trait
    }

    effect = {
        remove_list_variable = {
            name = DI_pinned_traits
            target = scope:trait
        }

        every_owned_story = {
            limit = {
                story_type = DI_traits_story
            }

            remove_list_variable = {
                name = DI_pinned_traits
                target = scope:trait
            }

            if = {
                limit = {
                    NOT = { has_variable_list = DI_pinned_traits }
                }
                end_story = yes
            }
        }
    }
}

### Trait Pinned ###
DI_trait_pinned = {
    scope = character

    saved_scopes = {
        trait # selected trait
    }

    is_valid = {
        is_target_in_variable_list = {
            name = DI_pinned_traits
            target = scope:trait
        }
    }
}

### Check for pinned traits list on player character ###
DI_pinned_trait_check = {
    scope = character

    is_valid = {
        NOT = { has_variable_list = DI_pinned_traits }
    }

    effect = {
        debug_log = "DI_Debug: is_valid check"
    }
}

### Create pinned traits list for player character, from story cycle ###
DI_create_pinned_trait_list = {
    scope = character

    effect = {
        save_scope_as = target

        if = {
            limit = {
                owns_story_of_type = DI_traits_story
            }
            debug_log = "DI_Debug: story exists"
            every_owned_story = {
                limit = {
                    story_type = DI_traits_story
                }
                if = {
                    limit = {
                        has_variable_list = DI_pinned_traits
                    }
                    every_in_list = {
                        variable = DI_pinned_traits

                        scope:target = {
                            add_to_variable_list = {
                                name = DI_pinned_traits
                                target = prev
                            }
                        }
                    }
                }
                else = {
                    debug_log = "DI_Debug: no list"
                    end_story = yes
                }
            }
        }
        else = {
            debug_log = "DI_Debug: story does not exist"
            create_story = DI_traits_story
        }
    }
}

### Create a list of all traits ###
DI_trait_loader_sgui = {
    scope = character

    saved_scopes = {
        trait # selected trait
        tracked # bool, yes if trait has a track, no if not
    }

    effect = {
        if = {
            limit = {
                scope:tracked = flag:no
            }
            add_to_global_variable_list = {
                name = DI_all_traits
                target = scope:trait
            }
            if = {
                limit = {
                    scope:trait = {
                        has_trait_category = health
                    }
                }

                add_to_global_variable_list = {
                    name = DI_health_traits
                    target = scope:trait
                }
            }
            else_if = {
                limit = {
                    scope:trait = {
                        OR = {
                            has_trait_category = commander
                            has_trait_category = winter_commander
                        }
                    }
                }

                add_to_global_variable_list = {
                    name = DI_commander_traits
                    target = scope:trait
                }
            }
            else_if = {
                limit = {
                    scope:trait = {
                        has_trait_category = fame
                    }
                }

                add_to_global_variable_list = {
                    name = DI_fame_traits
                    target = scope:trait
                }
            }
            else_if = {
                limit = {
                    scope:trait = {
                        has_trait_category = lifestyle
                    }
                }

                add_to_global_variable_list = {
                    name = DI_lifestyle_traits
                    target = scope:trait
                }
            }
            else = {
                add_to_global_variable_list = {
                    name = DI_misc_traits
                    target = scope:trait
                }
            }
        }
        else_if = {
            limit = {
                scope:tracked = flag:yes
            }

            add_to_global_variable_list = {
                name = DI_tracked_traits
                target = scope:trait
            }
        }
    }
}

### Check if all trait lists have been generated ###
DI_traits_list = {
    scope = character

    is_valid = {
        has_global_variable_list = DI_all_traits
        has_global_variable_list = DI_health_traits
        has_global_variable_list = DI_commander_traits
        has_global_variable_list = DI_fame_traits
        has_global_variable_list = DI_lifestyle_traits
        has_global_variable_list = DI_misc_traits
        has_global_variable_list = DI_tracked_traits
    }
}

### Set trait track to 0 xp ###
DI_add_trait_xp_0 = {
    scope = character

    saved_scopes = {
        trait # selected trait
        track # selected track
    }

    effect = {
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_living_character = {
                limit = { has_character_flag = pinned_for_edit }

                hidden_effect = {
                    if = {
                        limit = {
                            NOT = { has_trait = scope:trait }
                        }
                        add_trait = scope:trait
                    }

                    if = {
                        limit = {
                            DI_trait_track_check = yes
                        }

                        DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = -100 }
                    }
                    else = {
                        add_trait_xp = {
                            trait = scope:trait
                            value = -100
                        }
                    }
                }
            }
        }
        else = {
            hidden_effect = {
                if = {
                    limit = {
                        NOT = { has_trait = scope:trait }
                    }
                    add_trait = scope:trait
                }

                if = {
                    limit = {
                        DI_trait_track_check = yes
                    }

                    DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = -100 }
                }
                else = {
                    add_trait_xp = {
                        trait = scope:trait
                        value = -100
                    }
                }
            }
        }
    }
}

### Set trait track to 50 xp ###
DI_add_trait_xp_1 = {
    scope = character

    saved_scopes = {
        trait # selected trait
        track # selected track
    }

    effect = {
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_living_character = {
                limit = { has_character_flag = pinned_for_edit }

                hidden_effect = {
                    if = {
                        limit = {
                            NOT = { has_trait = scope:trait }
                        }
                        add_trait = scope:trait
                    }

                    if = {
                        limit = {
                            DI_trait_track_check = yes
                        }

                        DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = -100 }
                        DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = 50 }
                    }
                    else = {
                        add_trait_xp = {
                            trait = scope:trait
                            value = -100
                        }

                        add_trait_xp = {
                            trait = scope:trait
                            value = 50
                        }
                    }
                }
            }
        }
        else = {
            hidden_effect = {
                if = {
                    limit = {
                        NOT = { has_trait = scope:trait }
                    }
                    add_trait = scope:trait
                }

                if = {
                    limit = {
                        DI_trait_track_check = yes
                    }

                    DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = -100 }
                    DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = 50 }
                }
                else = {
                    add_trait_xp = {
                        trait = scope:trait
                        value = -100
                    }

                    add_trait_xp = {
                        trait = scope:trait
                        value = 50
                    }
                }
            }
        }
    }
}

### Set trait track to 100 xp ###
DI_add_trait_xp_2 = {
    scope = character

    saved_scopes = {
        trait # selected trait
        track # selected track
    }

    effect = {
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_living_character = {
                limit = { has_character_flag = pinned_for_edit }

                hidden_effect = {
                    if = {
                        limit = {
                            NOT = { has_trait = scope:trait }
                        }
                        add_trait = scope:trait
                    }

                    if = {
                        limit = {
                            DI_trait_track_check = yes
                        }

                        DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = 100 }
                    }
                    else = {
                        add_trait_xp = {
                            trait = scope:trait
                            value = 100
                        }
                    }
                }
            }
        }
        else = {
            hidden_effect = {
                if = {
                    limit = {
                        NOT = { has_trait = scope:trait }
                    }
                    add_trait = scope:trait
                }

                if = {
                    limit = {
                        DI_trait_track_check = yes
                    }

                    DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = 100 }
                }
                else = {
                    add_trait_xp = {
                        trait = scope:trait
                        value = 100
                    }
                }
            }
        }
    }
}

### General Add Trait XP ###
DI_add_trait_xp = {
    scope = character

    saved_scopes = {
        trait # selected trait
        track # selected track
        xp # xp value
    }

    effect = {
        debug_log = "DI_Debug: add_trait_xp"
        debug_log_scopes = yes
        
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_living_character = {
                limit = { has_character_flag = pinned_for_edit }

                hidden_effect = {
                    if = {
                        limit = {
                            NOT = { has_trait = scope:trait }
                        }
                        add_trait = scope:trait
                    }

                    if = {
                        limit = {
                            DI_trait_track_check = yes
                        }

                        DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = -100 }
                        DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = scope:xp }
                    }
                    else = {
                        add_trait_xp = {
                            trait = scope:trait
                            value = -100
                        }
                        add_trait_xp = {
                            trait = scope:trait
                            value = scope:xp
                        }
                    }
                }
            }
        }
        else = {
            hidden_effect = {
                if = {
                    limit = {
                        NOT = { has_trait = scope:trait }
                    }
                    add_trait = scope:trait
                }

                if = {
                    limit = {
                        DI_trait_track_check = yes
                    }

                    DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = -100 }
                    DI_add_trait_xp = { TRAIT = scope:trait TRACK = scope:track VALUE = scope:xp }
                }
                else = {
                    add_trait_xp = {
                        trait = scope:trait
                        value = -100
                    }
                    add_trait_xp = {
                        trait = scope:trait
                        value = scope:xp
                    }
                }
            }
        }
    }
}