const int WEPC_VBD_RADIUS = 24;
const int WEPC_VBD_HALFHEIGHT = 10;

actor BreakDashWep_ScriptBar : ScriptBar {}

actor BreakDashWep : BaseMM8BDMWep 10109
{
    //$Category MM8BDM-Weapons/Close
    //$Title Break Dash
    //$Sprite BDASA0
    Weapon.AmmoUse 3
    Weapon.AmmoGive 28
    Weapon.SlotNumber 4
    Inventory.Pickupmessage "$PU_BREAKDASH"
    Obituary "$OB_BREAKDASH"
    Tag "$TAG_BREAKDASH"
    weapon.ammotype "BreakDashAmmo"
    inventory.icon "BDASWI"
    States
    {
    SpawnLoop:
        BDAS A -1
        loop
    Ready:
        BDAS H 0 ACS_NamedExecuteWithResult("core_weaponcolor",CLR_BREAKDASH)
        BDAS H 1 A_WeaponReady
        goto Ready+1
    Deselect:
        BDAS H 0 A_JumpIfInventory("WeaponCharge",1,1)
        goto DeselectSwap
        BDAS H 2 A_TakeInventory("WeaponCharge",999)
        goto DeselectSwap
    Select:
        BDAS H 0
        goto SelectSwap
    Fire:
        BDAS H 0 A_JumpIfInventory("WeaponCharge", 23, "Hold3")
        BDAS H 0 A_JumpIfNoAmmo("Fire1")
        BDAS H 1
        goto Hold
    AltFire:
        BDAS H 0
        goto Fire1
    Hold:
        BDAS H 0 A_JumpIfInventory("WeaponCharge",4,"Hold2")
        BDAS H 0 A_JumpIfInventory("WeaponCharge",3,"HoldStart")
        BDAS H 2 A_GiveInventory("WeaponCharge",1) // no rage rune on this one
        BDAS H 0 A_Refire
        goto Fire1
    HoldStart:
        BDAS H 0 ACS_NamedExecuteWithResult("core_wepcharge_rage",1,0) // gives one weapon charge only if rage rune
        BDAS H 0 A_PlaySoundEx("weapons/mmv/breakdashcharge", "Weapon")
        goto Hold2
    Hold2:
        BDAS H 0 A_JumpIfInventory("WeaponCharge",23,"Hold3")
        BDAS H 0 A_JumpIfInventory("WeaponCharge",22,"HoldStart2")
        BDAS I 1 ACS_NamedExecuteWithResult("core_chargecolor",CLR_BREAKDASHCHARGE1,TCLR_CHARGE1)
        BDAS G 0 A_Refire("Hold2.a")
        goto Fire1
    Hold2.a:
        BDAS H 1 ACS_NamedExecuteWithResult("core_chargecolor",CLR_BREAKDASH,TCLR_NONE)
        BDAS H 0 A_GiveInventory("WeaponCharge", 1 + CallACS("core_checkrune", RUNE_RAGE))
        BDAS G 0 A_Refire("Hold2")
        goto Fire1
    HoldStart2:
        BDAS H 0 A_GunFlash("Sound.BusterCharge", GFF_NOEXTCHANGE)
        BDAS H 0 A_GiveInventory("WeaponCharge", 1 + CallACS("core_checkrune", RUNE_RAGE))
        goto Hold3
    Hold3:
        BDAS J 1 ACS_NamedExecuteWithResult("core_chargecolor",CLR_BREAKDASHCHARGE2,TCLR_CHARGE2)
        BDAS H 0 A_Refire("Hold3.a")
        goto Fire2
    Hold3.a:
        BDAS K 1 ACS_NamedExecuteWithResult("core_chargecolor",CLR_BREAKDASHCHARGE3,TCLR_CHARGE3)
        BDAS H 0 A_Refire("Hold3.b")
        goto Fire2
    Hold3.b:
        BDAS H 1 ACS_NamedExecuteWithResult("core_chargecolor",CLR_BREAKDASH,TCLR_NONE)
        BDAS H 0 A_JumpIfInventory("IsBot", 1, "BotFire")
        BDAS H 0 A_Refire("Hold3")
        goto Fire2
    BotFire:
        BDAS H 0 A_Jump(64, "Fire2")
        BDAS H 0 A_Refire("Hold3")
        goto Fire2

    Hold3Ready:
        BDAS H 0 A_JumpIfNoAmmo("NoAmmo")
        BDAS H 0 A_GunFlash("Sound.BusterCharge", GFF_NOEXTCHANGE)
        BDAS H 0 A_GiveInventory("WeaponCharge",23)
        BDAS H 0 A_TakeInventory("BreakDashHit", 1)
        BDAS J 0 ACS_ExecuteAlways(998,0,215,1)
        BDAS J 1 A_WeaponReady
        BDAS K 0 ACS_ExecuteAlways(998,0,216,1)
        BDAS K 1 A_WeaponReady
        BDAS H 0 ACS_ExecuteAlways(998,0,213,1)
        BDAS H 1 A_WeaponReady
        goto Hold3Ready+3
    
    Fire1:
        BDAS H 0 ACS_NamedExecuteWithResult("core_chargecolor",CLR_BREAKDASH,TCLR_NONE)
        BDAS H 0 A_StopSoundEx ("SoundSlot5") 
        BDAS H 0 A_TakeInventory("WeaponCharge",500)
        BDAS H 0 A_ClearRefire
        BDAS H 0 A_PlaySoundEx("weapons/mmv/breakdashbuster", "Weapon")
        BDAS H 0 A_FireCustomMissile("MegaShot", 0, 0, 8, 0)
        BDAS LM 3
        BDAS H 2
        goto Ready+1
    Fire2:
        BDAS G 0 A_TakeInventory("WeaponCharge",500)
        BDAS G 0 A_ClearRefire
        BDAS G 0 A_JumpIfNoAmmo("Fire1")
    DashBegin:
        BDAS B 0 ACS_NamedExecuteWithResult("core_flyplayer",0,2) // $WeapFixes
        BDAS H 0 ACS_NamedExecuteWithResult("core_freezeplayer",0,1) // $WeapFixes
        BDAS H 0 A_TakeInventory("BreakDashAmmo",3,TIF_NOTAKEINFINITE)
        BDAS N 1
        BDAS H 0 ACS_NamedExecuteWithResult("core_chargecolor",CLR_BREAKDASH,TCLR_NONE)
        BDAS OZG 1
        BDAS H 0 A_SpawnItemEx("BreakDashPuff")
        BDAS H 0 A_GiveInventory("BreakDashCharge", 3)
    DashLoop:
        BDAS F 0 A_PlaySoundEx("weapons/mmv/breakdashfire", "Weapon")
        BDAS F 0 A_GiveInventory("BreakDashSpeed", 1)
        BDAS F 0 A_SpawnItemEx("BreakDashFX", cos(pitch)*16+sin(-pitch)*12, 8, sin(-pitch)*16+32, momx, momy, momz, 0, SXF_ABSOLUTEMOMENTUM)
        BDAS F 0 A_SpawnItemEx("BreakDashSparkFX", 0, 0, 0, momx, momy, -2, 0, SXF_ABSOLUTEMOMENTUM)
        BDAS F 1 A_SpawnItemEx("BreakDashCheck", 0.75*WEPC_VBD_RADIUS*cos(pitch), 0, 0.75*WEPC_VBD_RADIUS*sin(-pitch)+(CallACS("core_getactorproperty", 0, APROP_Height, DATATYPE_INT)-WEPC_VBD_HALFHEIGHT*2)/2, cos(pitch), 0, sin(-pitch), 0, SXF_MULTIPLYSPEED)
        BDAS G 0 A_JumpIfInventory("BreakDashRecoil", 1, "Recoil")
        BDAS F 0 A_SpawnItemEx("BreakDashFX", cos(pitch)*16+sin(-pitch)*12, 8, sin(-pitch)*16+32, momx, momy, momz, 0, SXF_ABSOLUTEMOMENTUM)
        BDAS F 0 A_SpawnItemEx("BreakDashSparkFX", 0, 0, 0, momx, momy, -2, 0, SXF_ABSOLUTEMOMENTUM)
        BDAS F 1 A_SpawnItemEx("BreakDashCheck", 0.75*WEPC_VBD_RADIUS*cos(pitch), 0, 0.75*WEPC_VBD_RADIUS*sin(-pitch)+(CallACS("core_getactorproperty", 0, APROP_Height, DATATYPE_INT)-WEPC_VBD_HALFHEIGHT*2)/2, cos(pitch), 0, sin(-pitch), 0, SXF_MULTIPLYSPEED)
        BDAS G 0 A_JumpIfInventory("BreakDashRecoil", 1, "Recoil")
        BDAS F 0 A_SpawnItemEx("BreakDashFX", cos(pitch)*16+sin(-pitch)*12, 8, sin(-pitch)*16+32, momx, momy, momz, 0, SXF_ABSOLUTEMOMENTUM)
        BDAS F 0 A_SpawnItemEx("BreakDashSparkFX", 0, 0, 0, momx, momy, -2, 0, SXF_ABSOLUTEMOMENTUM)
        BDAS F 1 A_SpawnItemEx("BreakDashCheck", 0.75*WEPC_VBD_RADIUS*cos(pitch), 0, 0.75*WEPC_VBD_RADIUS*sin(-pitch)+(CallACS("core_getactorproperty", 0, APROP_Height, DATATYPE_INT)-WEPC_VBD_HALFHEIGHT*2)/2, cos(pitch), 0, sin(-pitch), 0, SXF_MULTIPLYSPEED)
        BDAS G 0 A_JumpIfInventory("BreakDashRecoil", 1, "Recoil")
        BDAS F 0 A_SpawnItemEx("BreakDashFX", cos(pitch)*16+sin(-pitch)*12, 8, sin(-pitch)*16+32, momx, momy, momz, 0, SXF_ABSOLUTEMOMENTUM)
        BDAS F 0 A_SpawnItemEx("BreakDashSparkFX", 0, 0, 0, momx, momy, -2, 0, SXF_ABSOLUTEMOMENTUM)
        BDAS F 1 A_SpawnItemEx("BreakDashCheck", 0.75*WEPC_VBD_RADIUS*cos(pitch), 0, 0.75*WEPC_VBD_RADIUS*sin(-pitch)+(CallACS("core_getactorproperty", 0, APROP_Height, DATATYPE_INT)-WEPC_VBD_HALFHEIGHT*2)/2, cos(pitch), 0, sin(-pitch), 0, SXF_MULTIPLYSPEED)
        BDAS G 0 A_JumpIfInventory("BreakDashRecoil", 1, "Recoil")
        BDAS G 0 A_TakeInventory("BreakDashCharge", 1)
        BDAS G 0 A_JumpIfInventory("BreakDashCharge", 1, "DashLoop")
        BDAS B 0 ACS_NamedExecuteWithResult("core_flyplayer",0,0) // $WeapFixes
        BDAS B 0 ACS_NamedExecuteWithResult("core_freezeplayer",0,0) // $WeapFixes
        BDAS B 0 A_ChangeVelocity(momx * 0.5, momy * 0.5, momz, CVF_REPLACE)
        BDAS S 3
        BDAS TZON 2
        BDAS H 2
        goto Ready+1
    Recoil:
        BDAS Z 1
        BDAS G 0 A_PlaySoundEx("weapons/mmv/breakdashimpact", "weapon")
        BDAS G 0 A_ChangeVelocity(-15*cos(pitch), 0, -8*sin(-pitch)+5, CVF_RELATIVE|CVF_REPLACE)
        BDAS G 0 A_TakeInventory("BreakDashCharge", 100)
        BDAS B 0 ACS_NamedExecuteWithResult("core_flyplayer",0,0) // $WeapFixes
        BDAS B 0 ACS_NamedExecuteWithResult("core_freezeplayer",0,0) // $WeapFixes
        BDAS ZZZONH 4
        BDAS B 0 A_JumpIfInventory("BreakDashHit", 1, "Hold3Ready")
        goto Ready+1
    NoAmmo:
        BDAS H 1 ACS_NamedExecuteAlways("core_noammo",0)
        BDAS H 0 //A_Refire
        goto Ready+1
    }
}

actor BreakDashHit : Once {}

actor BreakDashCharge : WeaponCharge
{
    inventory.amount 1
    inventory.maxamount 35
}

actor BreakDashRecoil : Powerup
{
    powerup.duration 10
}

actor BreakDashAmmo : Ammo
{
    inventory.amount 1
    inventory.maxamount 27
    +INVENTORY.IGNORESKILL
}

const int WEPC_VBD_HSPEED = 45;
const int WEPC_VBD_VSPEED = WEPC_VBD_HSPEED/2;

actor BreakDashSpeed : CustomInventory
{
    States
    {
    Pickup:
        TNT1 A 0 A_CheckFloor("Floor")
        TNT1 A 0 A_JumpIf(pitch < 180, "Down")
        TNT1 A 0 A_ChangeVelocity(WEPC_VBD_HSPEED*cos(pitch), 0, WEPC_VBD_VSPEED*sin(-pitch), CVF_RELATIVE|CVF_REPLACE)
        stop
    Down:
        TNT1 A 0 A_ChangeVelocity(WEPC_VBD_HSPEED*cos(pitch), 0, WEPC_VBD_HSPEED*sin(-pitch), CVF_RELATIVE|CVF_REPLACE)
        stop
    Floor:
        TNT1 A 0 A_ChangeVelocity(WEPC_VBD_HSPEED*cos(pitch), 0, (WEPC_VBD_VSPEED*sin(-pitch) > 0) * WEPC_VBD_VSPEED*sin(-pitch), CVF_RELATIVE|CVF_REPLACE)
        stop
    }
}

actor BreakDashCheck : BasicFastProjectile
{
    Obituary "$OB_BREAKDASH"
    Damagetype "BreakDash"
    height 20
    radius 24
    speed 64
    Damage (55)
    States
    {
    Spawn:
        TNT1 A 0
        stop
    XDeath:
        TNT1 A 0 A_GiveToTarget("BreakDashHit", 1)
    Death:
        TNT1 A 0 A_SpawnItemEx("PropBlowerEffect")
        TNT1 A 0 A_SpawnItemEx("BreakDashQuake")
        TNT1 A 0 A_GiveToTarget("BreakDashRecoil", 1)
        stop
    }
}

actor BreakDashQuake : BasicWatcher
{
	States
	{
	Spawn:
		TNT1 A 0
		TNT1 A 0 Radius_Quake(2, 4, 0, 2, 0)
		TNT1 A 1
		stop
	}
}

actor BreakDashProtect : PowerProtection
{
    powerup.duration 10
    damagefactor "BreakDash", 0.0
}

actor BreakDashFX : BasicGraphicEffect
{
    States
    {
    SpawnFrame:
        BDAS B 0
        BDAS B 1
        stop
    }
}

actor BreakDashSparkFX : BasicGraphicEffect
{
    -USESPAWNEVENTSCRIPT
    -NOINTERACTION
    States
    {
    Spawn:
        TNT1 A 0
        TNT1 A 1
        stop
    Death:
        TNT1 A 0 A_GiveInventory("MM8BDMProjSpawnFuncClient",1)
        TNT1 AAAAA 0 A_SpawnItemEx("BreakDashSpark", -momx*frandom(0, 1.0), momy*frandom(0, 1.0), 0, -frandom(8, 16), 0, frandom(4, 16), frandom(-20, 20), SXF_WEPFXCOLOR)
        stop
    }
}

actor BreakDashSpark : BreakDashFX
{
    States
    {
    SpawnFrame:
        BDAS EDCC 1
        stop
    }
}

actor BreakDashPuff : BreakDashFX
{
    -FORCEXYBILLBOARD
    States
    {
    SpawnFrame:
        BDAS P 0
        BDAS P 0 A_JumpIf(z-floorz > 0, "Air")
        stop
    Air:
        BDAS PQR 3
        stop
    }
}
