//import "std.zh"

//Change these values to match your item setup
const int GBS_SMALL=93;
const int GBS_MAGIC=8;
const int GBS_MIRROR=37;
const int GBS_FAKESMALL=144;
const int GBS_FAKEMAGIC=145;
const int GBS_FAKEMIRROR=146;
const int SFX_GBSHIELD=17;

global script slot_2{
    void run(){
    //Initializations
    bool shieldon;
    int lastdir;
    int gbsound;
    bool setdir = false;
        
        while(true){
            //GBShield strafe setup
            //if( (!(Link->Action==LA_ATTACKING))
            //&& (!(Link->Action==LA_CHARGING))
            //&& (!(Link->Action==LA_SPINNING))) {lastdir=Link->Dir;}


            
            int nw = ComboAt(Link->X + 8, Link->Y + 12);

            if(nowalkshield(nw)){          
                Link->Item[GBS_MIRROR]=false;
                Link->Item[GBS_MAGIC]=false;
                Link->Item[GBS_SMALL]=false;
                shieldon=false;
                setdir=false;
            }

            else if(Link->InputR && !shieldon){ //Enable shield when R is pressed
            //else if( UsingItem(GBS_FAKESMALL) || UsingItem(GBS_FAKEMAGIC) || UsingItem(GBS_FAKEMIRROR) ) //Enable shield when using dummy
                shieldon=true;
                if(!setdir)
                {
                    lastdir=Link->Dir;
                    setdir=true;
                }
                gbsound=SFX_GBSHIELD;
                if(Link->Item[GBS_FAKEMIRROR]) Link->Item[GBS_MIRROR]=true;
                else if(Link->Item[GBS_FAKEMAGIC]) Link->Item[GBS_MAGIC]=true;
                else if(Link->Item[GBS_FAKESMALL]) Link->Item[GBS_SMALL]=true;
                else{
                    gbsound=0;
                    shieldon=false;
                    setdir=false;
                }
                if(gbsound>0) Game->PlaySound(SFX_GBSHIELD);
            }
            else if(!Link->InputR && shieldon){ //Remove shield when R is released
                Link->Item[GBS_MIRROR]=false;
                Link->Item[GBS_MAGIC]=false;
                Link->Item[GBS_SMALL]=false;
                shieldon=false;
                setdir=false;
            }

            //check if the hookshot is on screen
            bool nohookshotonscreen = true;
            for(int i=1;i<=Screen->NumLWeapons();i++){
                lweapon testedlw = Screen->LoadLWeapon(i);
                if(testedlw->ID == LW_HOOKSHOT)
                    nohookshotonscreen=false;
            }
            
            bool dirchecks = Link->InputR
                && shieldon
                && (!(Link->Action==LA_ATTACKING))
                && (!(Link->Action==LA_CHARGING))
                && (!(Link->Action==LA_SPINNING))
                && nohookshotonscreen;

            
            //Turn off inputs "L" and "R".
            Link->InputL=false;
            Link->InputR=false;    


            //Strafe while R is held down
            if(dirchecks){
                Link->Dir=lastdir;
            }

            Waitframe();
            }                 // end while
        }                  //end void run

    bool nowalkshield(int underlink){
        underlink = ComboAt(Link->X, Link->Y + 8);
        return (Screen->ComboT[underlink] == 31) || (Screen->ComboT[underlink] == 32) || (Screen->ComboT[underlink] == 33) || (Screen->ComboT[underlink] == 34)
         || (Screen->ComboT[underlink] == 120) || (Screen->ComboT[underlink] == 121) || (Screen->ComboT[underlink] == 122) || (Screen->ComboT[underlink] == 19)
         || (Screen->ComboT[underlink] == 89) || (Screen->ComboT[underlink] == 90) || (Screen->ComboT[underlink] == 91) || (Screen->ComboT[underlink] == 6)
         || (Screen->ComboT[underlink] == 18) || (Screen->ComboT[underlink] == 86) || (Screen->ComboT[underlink] == 87) || (Screen->ComboT[underlink] == 88)
         || (Screen->ComboT[underlink] == 38) || (Screen->ComboT[underlink] == 36) || (Screen->ComboT[underlink] == 39) || (Screen->ComboT[underlink] == 40)
         || (Screen->ComboT[underlink] == 37) || (Screen->ComboT[underlink] == 3);
    }
}