//import "std.zh"
//import "ffcscript.zh"

const int screenWidth = 256;
const int screenHeight = 176;

const int SFX_SWITCHGUNSHOT = 91;
const int SFX_SWITCHGUNHIT = 92;
const int FFCS_SWITCHGUN = 4; //# of switchGun FFC script
const int WPS_LINKSWITCHOUT = 91; //Weap/misc sprite of Link switching out
const int WPS_LINKSWITCHIN = 92; //Weap/misc sprite of Link switching in
const int CT_SWITCHBLOCK = 142; //Combo type of switch block
const int CF_SWITCHTHRU = 98; //Combo flag that switch shots can pass through even if solid
const int switchGunDelay = 50; //Delay between switch out and switch in

item script switchGun{
    void run ( int sprite, int speed, int groundCombo, int blockSwitchOut, int blockSwitchIn ){
        if ( !CountFFCsRunning(FFCS_SWITCHGUN) ){
            float args[5] = {sprite, speed, groundCombo, blockSwitchOut, blockSwitchIn};
            RunFFCScript(FFCS_SWITCHGUN, args);
        }
    }
}

ffc script switchGunFFC{
    void run ( int sprite, int speed, int groundCombo, int blockSwitchOut, int blockSwitchIn ){
        int blockCombo;
        
        Game->PlaySound(SFX_SWITCHGUNSHOT);
        lweapon shot = NextToLink(LW_SCRIPT1, 1);
        shot->CollDetection = false;
        shot->Step = speed;
        shot->UseSprite(sprite);
        if ( Link->Dir == DIR_LEFT || Link->Dir == DIR_RIGHT ) shot->Tile++; //If shooting horizontally, change tile
        if ( Link->Dir == DIR_DOWN ) shot->Flip = 3; //Down = vertical/horizontal flip
        if ( Link->Dir == DIR_LEFT ) shot->Flip = 1; //Left = horizontal flip
        
        while ( shot->isValid() ){
            if ( Screen->isSolid(shot->X+8, shot->Y+8) ){ //If hits something solid
                blockCombo = ComboAt(shot->X+8, shot->Y+8); //Find the combo under it
                if ( Screen->ComboT[blockCombo] == CT_SWITCHBLOCK ) break; //If it's a block, continue to next part
                else if ( !ComboFI(blockCombo, CF_SWITCHTHRU) ){ //Or if not "switchThru" flag
                    shot->DeadState = WDS_DEAD; //Kill the shot
                    return;
                }
            }
            if ( shot->X > screenWidth || shot->X < 0 || shot->Y > screenHeight || shot->Y < 0 ){
                shot->DeadState = WDS_DEAD;
                return;
            }
            Waitframe();
        }
        Game->PlaySound(SFX_SWITCHGUNHIT);
        shot->Step = 0;
        Link->Invisible = true; //Make animations
        lweapon linkAnim = CreateLWeaponAt(LW_SPARKLE,Link->X,Link->Y);
        linkAnim->UseSprite(WPS_LINKSWITCHOUT);
        lweapon blockAnim = CreateLWeaponAt(LW_SPARKLE,shot->X,shot->Y);
        linkAnim->UseSprite(blockSwitchOut);
        
        for ( int i = 0; i < switchGunDelay; i++ ){
            shot->DeadState = WDS_ALIVE;
            WaitNoAction(); //Wait for animations to finish
        }
        
        //Make switch in animations
        linkAnim = CreateLWeaponAt(LW_SPARKLE,shot->X,shot->Y);
        linkAnim->UseSprite(WPS_LINKSWITCHIN);
        blockAnim = CreateLWeaponAt(LW_SPARKLE,Link->X,Link->Y);
        linkAnim->UseSprite(blockSwitchIn);
        
        //Now switch Link and block
        Screen->ComboD[ComboAt(Link->X+8, Link->Y+8)] = Screen->ComboD[blockCombo];
        Screen->ComboD[ComboAt(shot->X+8, shot->Y+8)] = Screen->ComboD[groundCombo];
        Link->X = shot->X;
        Link->Y = shot->Y;
        Link->Invisible = false;
        shot->DeadState = WDS_DEAD;
    }
}