/*
    To begin, type in the console:

    include dump_used_assets
    enableevent all

    Open your map.
    In 2D mode, press CTRL+V to search for the tiles and sounds used in the current map.
    You can set the gamevar "verbose" to 1 if you want verbose .csv formatted information that would normally clog up the output.
    Tip: With pk_quickmapcycling enabled, Ctrl-(LShift-)X loads the next (previous) map in a directory.

    The results from every map will be accumulated into master lists.
    When you have searched for tiles in all desired maps, press CTRL+D to print the finished lists.

    The results will be printed in mapster32.log.
*/

include names.h

gamearray usedTiles MAXTILES
gamearray usedSounds MAXSOUNDS

definequote 0
definequote 1 %ld


definequote 11 sprite,%ld,
definequote 12 floor,%ld,
definequote 13 ceiling,%ld,
definequote 14 wall,%ld,
definequote 15 overwall,%ld,

definequote 21 MUSICANDSFX triggered,%ld,
definequote 22 MUSICANDSFX ambient,%ld,
definequote 23 sector one-time,%ld,
definequote 24 MIKE,%ld,
definequote 25 switch,%ld,
definequote 26 MIRROR,%ld,
definequote 27 sector 65534,%ld,
definequote 28 doortile,%ld,

definequote 101 Searching for tiles used in current map...
definequote 102 Tile search finished.
definequote 103 Dumping tiles, cumulatively...
definequote 104 Tile dump finished.
definequote 105 Searching for sounds used in current map...
definequote 106 Sound search finished.
definequote 107 Dumping sounds, cumulatively...
definequote 108 Sound dump finished.
definequote 109 Search finished.
definequote 110 Dump finished.

gamevar variable 0 0

gamevar verbose 0 0

gamevar index 0 0

gamevar flag 0 0
gamevar input 0 0


defstate tile_search
    quote 101

    for variable allsprites
    {
        set index .picnum
        ifge index 0 ifle index MAXTILES set usedTiles[index] 1
        ife verbose 1
        {
            qsprintf 0 11 .picnum
            quote 0
        }
    }
    for variable allsectors
    {
        set index sector[variable].floorpicnum
        ifge index 0 ifle index MAXTILES set usedTiles[index] 2
        set index sector[variable].ceilingpicnum
        ifge index 0 ifle index MAXTILES set usedTiles[index] 3
        ife verbose 1
        {
            qsprintf 0 12 sector[variable].floorpicnum
            quote 0
            qsprintf 0 13 sector[variable].ceilingpicnum
            quote 0
        }
    }
    for variable allwalls
    {
        set index wall[variable].picnum
        ifge index 0 ifle index MAXTILES set usedTiles[index] 4
        set index wall[variable].overpicnum
        ifge index 0 ifle index MAXTILES set usedTiles[index] 5
        ife verbose 1
        {
            qsprintf 0 14 wall[variable].picnum
            quote 0
            qsprintf 0 15 wall[variable].overpicnum
            quote 0
        }
    }

    quote 102
ends

defstate tile_dump
    quote 103

    for variable range MAXTILES
        ifn usedTiles[variable] 0
        {
            qsprintf 0 1 variable
            quote 0
        }

    quote 104
ends


// these long "ife" lists exist because switch/case is broken/was broken at the original time of writing

// the three commented switches do not play sounds because their hitag is used for setting a multi-switch combination
defstate switch_identify
    ife input ACCESSSWITCH set flag 1
    ife input SLOTDOOR set flag 1 ife input 133 set flag 1
    ife input LIGHTSWITCH set flag 1 ife input 135 set flag 1
    ife input SPACEDOORSWITCH set flag 1 ife input 137 set flag 1
    ife input SPACELIGHTSWITCH set flag 1 ife input 139 set flag 1
    ife input FRANKENSTINESWITCH set flag 1 ife input 141 set flag 1
    ife input MULTISWITCH set flag 1 ife input 147 set flag 1 ife input 148 set flag 1 ife input 149 set flag 1
    // ife input DIPSWITCH set flag 1 ife input 163 set flag 1
    ife input DIPSWITCH2 set flag 1 ife input 165 set flag 1
    // ife input TECHSWITCH set flag 1 ife input 167 set flag 1
    ife input DIPSWITCH3 set flag 1 ife input 169 set flag 1
    ife input ACCESSSWITCH2 set flag 1
    ife input LIGHTSWITCH2 set flag 1 ife input 713 set flag 1
    ife input POWERSWITCH1 set flag 1 ife input 861 set flag 1
    ife input LOCKSWITCH1 set flag 1 ife input 863 set flag 1
    ife input POWERSWITCH2 set flag 1 ife input 865 set flag 1
    ife input HANDSWITCH set flag 1 ife input 1112 set flag 1
    ife input PULLSWITCH set flag 1 ife input 1123 set flag 1
    // ife input ALIENSWITCH set flag 1 ife input 1143 set flag 1
ends

defstate doortile_identify
    ife input DOORTILE1 set flag 1
    ife input DOORTILE2 set flag 1
    ife input DOORTILE3 set flag 1
    ife input DOORTILE4 set flag 1
    ife input DOORTILE5 set flag 1
    ife input DOORTILE6 set flag 1
    ife input DOORTILE7 set flag 1
    ife input DOORTILE8 set flag 1
    ife input DOORTILE9 set flag 1
    ife input DOORTILE10 set flag 1
    ife input DOORTILE11 set flag 1
    ife input DOORTILE12 set flag 1
    ife input DOORTILE14 set flag 1
    ife input DOORTILE15 set flag 1
    ife input DOORTILE16 set flag 1
    ife input DOORTILE17 set flag 1
    ife input DOORTILE18 set flag 1
    ife input DOORTILE19 set flag 1
    ife input DOORTILE20 set flag 1
    ife input DOORTILE21 set flag 1
    ife input DOORTILE22 set flag 1
    ife input DOORTILE23 set flag 1
ends

defstate sound_search
    quote 105

    for variable allsprites, ifactor MUSICANDSFX, ifl .lotag 1000 // rule out echo effect
    {
// This is how the code SHOULD be:
// activation sounds / triggered sounds (e.g. elevators)
        sectgetlotag
        ifn LOTAG 0, ifl LOTAG 10000
        {
            ifn .lotag 0 ifl .lotag 500
            {
                set index .lotag
                ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 1
                ife verbose 1
                {
                    qsprintf 0 21 .lotag
                    quote 0
                }
            }
            ifn .hitag 0 ifl .hitag 500
            {
                set index .hitag
                ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 1
                ife verbose 1
                {
                    qsprintf 0 21 .hitag
                    quote 0
                }
            }
        }
// ambient sounds
        else
        {
            ifn .lotag 0 ifl .lotag 500
            {
                set index .lotag
                ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 2
                ife verbose 1
                {
                    qsprintf 0 22 .lotag
                    quote 0
                }
            }
        }
    }

// one-time sector sounds
    for variable allsectors, ifge sector[variable].lotag 10000, ifl sector[variable].lotag 16383
    {
        set index sector[variable].lotag
        subvar index 10000
        ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 3
        ife verbose 1
        {
            qsprintf 0 23 index
            quote 0
        }
    }

// mikesnd
    for variable allsprites, ifactor MIKE
    {
        set index .hitag // this one's clever -- by default it will play sound 0, as seen in E4L7
        ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 4
        ife verbose 1
        {
            qsprintf 0 24 .hitag
            quote 0
        }
    }

// switches
    for variable allsprites
    {
        set flag 0
        set input .picnum
        state switch_identify
        ife flag 1, ifn .hitag 0
        {
            set index .hitag
            ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 5
            ife verbose 1
            {
                qsprintf 0 25 .hitag
                quote 0
            }
        }
    }

// mirror
    for variable allwalls
    {
        set index 0
        ife wall[variable].picnum MIRROR set index wall[variable].lotag
        ife wall[variable].overpicnum MIRROR set index wall[variable].lotag
        ifn index 0
        {
            ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 6
            ife verbose 1
            {
                qsprintf 0 26 index
                quote 0
            }
        }
    }

// sector lotag 65534 (E1L2 ending) customexitsound
    for variable allsectors, ife sector[variable].lotag 65534
    {
        set index sector[variable].hitag
        ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 7
        ife verbose 1
        {
            qsprintf 0 27 sector[variable].hitag
            quote 0
        }
    }

// doortiles
    for variable allsprites
    {
        set flag 0
        set input .picnum
        state doortile_identify
        ife flag 1, ifn .hitag 0
        {
            set index .hitag
            ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 8
            ife verbose 1
            {
                qsprintf 0 28 .hitag
                quote 0
            }
        }
    }
    for variable allwalls
    {
        set flag 0
        set input wall[variable].picnum
        state doortile_identify
        set input wall[variable].overpicnum
        state doortile_identify
        ife flag 1, ifn wall[variable].lotag 0, ifn wall[variable].hitag 0
        {
            set index wall[variable].hitag
            ifge index 0 ifle index MAXSOUNDS set usedSounds[index] 8
            ife verbose 1
            {
                qsprintf 0 28 wall[variable].hitag
                quote 0
            }
        }
    }

    quote 106
ends

defstate sound_dump
    quote 107

    for variable range MAXSOUNDS
        ifn usedSounds[variable] 0
        {
            qsprintf 0 1 variable
            quote 0
        }

    quote 108
ends



onevent EVENT_KEYS2D
    ifeitherctrl
    {
        ifhitkey KEY_V // find
        {
            state tile_search
            state sound_search
            quote 109
        }
        ifhitkey KEY_D // dump
        {
            state tile_dump
            state sound_dump
            quote 110
        }
    }
endevent
