ACTOR RifleAmmo : Ammo
{
	Inventory.Amount 5
	Inventory.MaxAmount 20
	Ammo.BackpackAmount 5
	Ammo.BackpackMaxAmount 40
	Inventory.Icon RIFAB0
	Inventory.PickupMessage "Picked up some rifle cartridges."
	//Inventory.PickupSound "misc/i_pkup"

	States
	{
		Spawn:
			RIFA A -1
			Loop
	}
}

ACTOR RifleAmmoBox : RifleAmmo
{
	Inventory.PickupMessage "Picked up a box of rifle cartridges."
	Inventory.Amount 25

	States
	{
		Spawn:
			RIFP A -1
			Loop
	}
}

// ------------------------------------------------------------------


// rifle pickup that creates random weapon values
ACTOR RandomRiflePickup : PickupBase
{
	States
	{
		Spawn:
			TNT1 A 0
			TNT1 A 0 A_SetUserVar("user_isBDPickupItem", 1)	// mark thing as BorderDoom pickup item
			TNT1 A 0 A_SetUserVar("user_pickupType", ItemType_Rifle)	// set pickup type to weapon (Rifle)
			TNT1 A 0 ACS_NamedExecuteAlways("CreateRandomRifleData")
			TNT1 A 0 A_SetSpecial(ACS_ExecuteAlways,901,0,0,0,0)	// script for picking up the item
			goto Waiting
		Waiting:
			RIFL A -1
			stop
		// called by script. makes the pickup "useable" for a short amount of time so it can be picked up.
		IsUseable:
			RIFL A 0 A_ChangeFlag("USESPECIAL", TRUE)
			RIFL A 7
			RIFL A 0 A_ChangeFlag("USESPECIAL", FALSE)
			goto Waiting
	}
}

// Rifle pickup that uses pre-existing/pre-set weapon values
ACTOR FixedRiflePickup : RandomRiflePickup
{
	States
	{
		Spawn:
			TNT1 A 0
			TNT1 A 0 A_SetUserVar("user_isBDPickupItem", 1)	// mark thing as BorderDoom pickup item
			TNT1 A 0 A_SetUserVar("user_pickupType", ItemType_Rifle)	// set pickup type to weapon (Rifle)
			TNT1 A 0 A_SetSpecial(ACS_ExecuteAlways,901,0,0,0,0)	// script for picking up the item
			RIFL A -1
			stop
	}
}

ACTOR Rifle_Zoom_Helper : Inventory
{
	Inventory.Maxamount 1
}

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

ACTOR RealRifle : Chaingun
{
	Weapon.AmmoGive 0
	Weapon.BobSpeed 2.0
	+INVENTORY.UNDROPPABLE	// should only be dropped by script
	+Weapon.CHEATNOTWEAPON	// don't give this weapon when using cheats
	+Weapon.NOALERT	// to mute AltFire-Zoom
	+Weapon.Ammo_Optional
	// both these are needed so automatic reloading works even after using zoom.
	// by default zooming (AltFire) sets used ammo type to secondary ammo. since the used amount is set to 0
	// by default A_JumpIfNoAmmo will never jump. because of that we need to specify a secondary ammo type even
	// though it is never actually used.
	Weapon.AmmoUse2 1
	+Weapon.Alt_Ammo_Optional
	Decal BulletChip	// so this weapon also creates decals when fired at walls
	Tag "Sniper Rifle"

	Weapon.Kickback 150

	States
	{
		Spawn:
			RIFL A -1
			Loop
		Select:
			TNT1 A 0 ACS_NamedExecuteAlways("SetWeaponDamageMultiplier")
//			TNT1 A 0 ACS_NamedExecute("PrintEquippedWeaponInfo", 0, CallACS("GetWeaponIndex"))
			RIFG A 1 A_Raise
			TNT1 A 0 A_ZoomFactor(1.0)
			Loop
		Deselect:
			TNT1 A 0 A_ZoomFactor(1.0)
			TNT1 A 0 A_TakeInventory("Rifle_Zoom_Helper", 1)
			TNT1 A 0 A_ClearOverlays(2, 3)
			TNT1 A 0 A_SetCrosshair(0)	// switch to default crosshair
			RIFG A 1 A_Lower
			Loop
		Ready:
			TNT1 A 0 A_JumpIfInventory("Rifle_Zoom_Helper", 1, "ReadyZoomed")
			RIFG A 1 A_WeaponReady(WRF_ALLOWRELOAD)
			Goto ReadyEnd
			ReadyZoomed:
			RIFZ A 1 A_WeaponReady(WRF_ALLOWRELOAD|WRF_NOBOB)
			ReadyEnd:
//			TNT1 A 0 A_JumpIfInventory("Rifle_Zoom_Helper", 1, 2)
//			TNT1 A 0 A_ScaleVelocity(0.66)
			TNT1 A 0 A_JumpIfNoAmmo("Reload")	// auto-reload if player has more ammo
			Goto Ready
		AltFire:
			TNT1 A 0 A_JumpIfInventory("Rifle_Zoom_Helper", 1, "ZoomOut")
			Goto ZoomIn
		ZoomIn:
			TNT1 A 0 A_SetCrosshair(50)	// switch to empty crosshair
			TNT1 A 0 A_GiveInventory("Rifle_Zoom_Helper", 1)
			TNT1 A 0 A_ZoomFactor(3.0)
			// this manual transition is necessary because A_SetBlend only decreases alpha and does not increase
			RIFG A 1 A_SetBlend("00 00 00", 0.2, 1)
			RIFG A 1 A_SetBlend("00 00 00", 0.4, 1)
			RIFG A 1 A_SetBlend("00 00 00", 0.6, 1)
			RIFG A 1 A_SetBlend("00 00 00", 0.8, 1)
			// create border overlay to prevent see-through on the sides of the screen
			TNT1 A 0 A_Overlay(2, "ZoomBorderOverlay")
			TNT1 A 0 A_OverlayOffset(2, -239.0, 0.0)	// Y must be set to 0 (default is 32) because the offset is already set on the image itself
			TNT1 A 0 A_Overlay(3, "ZoomBorderOverlay")
			TNT1 A 0 A_OverlayOffset(3, 239.0, 0.0)
			RIFZ A 4 A_SetBlend("00 00 00", 1.0, 10)
			TNT1 A 0 A_Refire
			Goto Ready
		ZoomBorderOverlay:
			RIFZ C 1
			Loop
		ZoomOut:
			TNT1 A 0 A_TakeInventory("Rifle_Zoom_Helper", 1)
			TNT1 A 0 A_ZoomFactor(1.0)
			RIFZ A 1 A_SetBlend("00 00 00", 0.2, 1)
			RIFZ A 1 A_SetBlend("00 00 00", 0.4, 1)
			RIFZ A 1 A_SetBlend("00 00 00", 0.6, 1)
			RIFZ A 1 A_SetBlend("00 00 00", 0.8, 1)
			TNT1 A 0 A_ClearOverlays(2, 3)
			RIFG A 4 A_SetBlend("00 00 00", 1.0, 10)
			TNT1 A 0 A_SetCrosshair(0)	// switch to default crosshair
			TNT1 A 0 A_Refire
			Goto Ready
		// alt fire hold state: prevents from zooming in and out repeatedly when holding the key
		AltHold:
			TNT1 A 0 A_JumpIfInventory("Rifle_Zoom_Helper", 1, "AltHoldZoomed")
			RIFG A 1
			Goto AltHoldEnd
			AltHoldZoomed:
			RIFZ A 1
			AltHoldEnd:
			TNT1 A 0 A_Refire
			Goto Ready

		Fire:
			// "----" keeps the previous sprite. so no need for two seperate states (no zoom/zoom)
			"----" A 0 A_JumpIfNoAmmo("NoAmmo")
			goto DoFire
			NoAmmo:
			"----" A 0 A_JumpIfInventory("RifleAmmo", 1, "Reload")
			"----" A 0 A_PlaySound("player/noammo", CHAN_AUTO)
			NoAmmoHold:
			"----" A 1 A_WeaponReady(WRF_NOFIRE)
			"----" A 0 A_Refire("NoAmmoHold")
			goto Ready
			DoFire:
			//TNT1 A 0 A_Quake (4, 4, 0, 1)
			TNT1 A 0 A_AlertMonsters	// wouldn't happen otherwise because NOALERT is set
			TNT1 A 0 A_PlaySound("rifle/fire_med", CHAN_AUTO)
			// less precise when not zoomed in
			TNT1 A 0 A_JumpIfInventory("Rifle_Zoom_Helper", 1, "ZoomedInFire")
				//TNT1 A 0 A_FireBullets (CallACS("GetCurrentSpread")*4, CallACS("GetCurrentSpread")*2, CallACS("GetCurrentBulletcount"), CallACS("GetCurrentDamage"), "BulletPuff")
				TNT1 A 0 A_FireBullets ((CallACS("GetCurrentSpread")/65536.0)+0.666, (CallACS("GetCurrentSpread")/65536.0)+0.333, CallACS("GetCurrentBulletcount"), CallACS("GetCurrentDamage"), "BulletPuff", FBF_USEAMMO|FBF_NORANDOM)
				TNT1 A 0 ACS_NamedExecuteAlways("WeaponRecoil", 0, CallACS("GetCurrentRecoil"), 60, 10)
			Goto AccuracyCheckEnd
			ZoomedInFire:
				TNT1 A 0 A_FireBullets (CallACS("GetCurrentSpread")/65536.0, (CallACS("GetCurrentSpread")/65536.0)/2.0, CallACS("GetCurrentBulletcount"), CallACS("GetCurrentDamage"), "BulletPuff", FBF_USEAMMO|FBF_NORANDOM)
				TNT1 A 0 ACS_NamedExecuteAlways("WeaponRecoil", 0, CallACS("GetCurrentRecoil")/2, 80, 10)
				// if this was the last shot: zoom out
				TNT1 A 0 A_JumpIfNoAmmo("NoAmmoZoomOut")
				TNT1 A 0 A_Jump (256, "AccuracyCheckEnd")	// guaranteed jump
				NoAmmoZoomOut:
				TNT1 A 0 A_TakeInventory("Rifle_Zoom_Helper", 1)
				TNT1 A 0 A_ZoomFactor(1.0)
				TNT1 A 0 A_ClearOverlays(2, 3)
			AccuracyCheckEnd:

			TNT1 A 0 A_JumpIfInventory("Rifle_Zoom_Helper", 1, "ZoomedInFireEnd")
			RIFG A 1 Offset(0,64) A_GunFlash
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 1, "FireSpeed1")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 2, "FireSpeed2")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 3, "FireSpeed3")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 4, "FireSpeed4")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 5, "FireSpeed5")
			Goto Ready
			ZoomedInFireEnd:
			RIFZ A 1 A_GunFlash("FlashZoomed")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 1, "FireSpeed1Zoomed")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 2, "FireSpeed2Zoomed")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 3, "FireSpeed3Zoomed")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 4, "FireSpeed4Zoomed")
			TNT1 A 0 A_JumpIf(CallACS("GetCurrentFirerate") == 5, "FireSpeed5Zoomed")
			Goto Ready
		// fire speeds: very fast: -2, fast: -1, normal: +0, slow: +1, very slow: +2
		// FIXME should use A_SetTics with version 2.7.1 onward
		FireSpeed1:
			RIFG A 0	// wait for reload
			goto FireEnd
		FireSpeed2:
			RIFG A 10	// wait for reload
			goto FireEnd
		FireSpeed3:
			RIFG A 20	// wait for reload
			goto FireEnd
		FireSpeed4:
			RIFG A 30	// wait for reload
			goto FireEnd
		FireSpeed5:
			RIFG A 40	// wait for reload
			goto FireEnd
		FireEnd:
			RIFG A 1 Offset(0,62)
			RIFG A 1 Offset(0,60)
			RIFG A 1 Offset(0,58)
			RIFG A 1 Offset(0,56)
			RIFG A 1 Offset(0,52)
			RIFG A 1 Offset(0,50)
			RIFG A 1 Offset(0,48)
			RIFG A 1 Offset(0,46)
			RIFG A 1 Offset(0,44)
			RIFG A 1 Offset(0,42)
			RIFG A 1 Offset(0,40)
			RIFG A 1 Offset(0,38)
			RIFG A 1 Offset(0,36)
			RIFG A 1 Offset(0,34)
			RIFG A 0 A_Refire
			Goto Ready
		Flash:
			RIFF A 2 BRIGHT A_Light2
			TNT1 A 0 A_Light0
			Stop
		// ----------------
		FireSpeed1Zoomed:
			RIFZ B 0	// wait for reload
			goto FireEndZoomed
		FireSpeed2Zoomed:
			RIFZ B 10	// wait for reload
			goto FireEndZoomed
		FireSpeed3Zoomed:
			RIFZ B 20	// wait for reload
			goto FireEndZoomed
		FireSpeed4Zoomed:
			RIFZ B 30	// wait for reload
			goto FireEndZoomed
		FireSpeed5Zoomed:
			RIFZ B 40	// wait for reload
			goto FireEndZoomed
		FireEndZoomed:
			RIFZ B 14
			TNT1 A 0 A_Refire
			Goto Ready
		FlashZoomed:
			TNT1 A 2 BRIGHT A_Light1
			TNT1 A 2 BRIGHT A_Light2
			TNT1 A 0 A_Light0
			Stop

		ReloadWork:
			TNT1 A 0 A_TakeInventory("Rifle_Zoom_Helper", 1)
			RIFG A 0 A_ZoomFactor(1.0)
			TNT1 A 0 A_ClearOverlays(2, 3)
			TNT1 A 0 A_SetCrosshair(0)	// switch to default crosshair
			TNT1 A 0 A_PlaySound("rifle/reload_chamber_open", CHAN_WEAPON)
			RIFG A 1 Offset(0,40)
			RIFG A 1 Offset(0,48)
			RIFG A 1 Offset(0,56)
			RIFG A 1 Offset(0,64)
			RIFG A 1 Offset(0,72)
			RIFG A 1 Offset(0,80)
			RIFG A 1 Offset(0,88)
			RIFG A 1 Offset(0,96)
			TNT1 A 0 A_PlaySound("rifle/reload_emptyshells", CHAN_WEAPON)
			RIFG A 40	// wait for reload
			TNT1 A 0 A_PlaySound("rifle/reload_loaderin", CHAN_WEAPON)
			RIFG A 40	// wait for reload
			TNT1 A 0 A_PlaySound("rifle/reload_chamber_close", CHAN_WEAPON)
			RIFG A 0 A_Jump(256, "ReloadLoop")	// A_Jump jumps are virtual, "goto" is not
			//Goto ReloadLoop
		ReloadFinish:
			RIFG A 1 Offset(0,96)
			RIFG A 1 Offset(0,90)
			RIFG A 1 Offset(0,84)
			RIFG A 1 Offset(0,78)
			RIFG A 1 Offset(0,72)
			RIFG A 1 Offset(0,66)
			RIFG A 1 Offset(0,60)
			RIFG A 1 Offset(0,54)
			RIFG A 1 Offset(0,48)
			RIFG A 1 Offset(0,42)
			RIFG A 1 Offset(0,36)
			Goto Ready
	}
}


///////////////////////////////
// EQUIPPABLE RIFLES
///////////////////////////////
ACTOR RealRifle0 : RealRifle
{
	Weapon.SlotNumber 1
	Weapon.AmmoType "LoadedAmmoSlot0"
	Weapon.AmmoType2 "LoadedAmmoSlot0"	// needed for automatic reloading to work after zoom. see above.

	States
	{
		Reload:
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot0", CallACS("GetCurrentClipSize"), "Ready")	// ignore if fully loaded
			TNT1 A 0 A_JumpIfInventory("RifleAmmo", 1, "ReloadWork")	// do real reload
			Goto Ready
		ReloadLoop:
			TNT1 A 0 A_TakeInventory("RifleAmmo", 1)
			TNT1 A 0 A_GiveInventory("LoadedAmmoSlot0", 1)
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot0", CallACS("GetCurrentClipSize"), "ReloadFinish")
			TNT1 A 0 A_JumpIfInventory("RifleAmmo", 1, "ReloadLoop")
			Goto ReloadFinish
	}
}

ACTOR RealRifle1 : RealRifle
{
	Weapon.SlotNumber 2
	Weapon.AmmoType "LoadedAmmoSlot1"
	Weapon.AmmoType2 "LoadedAmmoSlot1"	// needed for automatic reloading to work after zoom. see above.

	States
	{
		Reload:
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot1", CallACS("GetCurrentClipSize"), "Ready")	// ignore if fully loaded
			TNT1 A 0 A_JumpIfInventory("RifleAmmo", 1, "ReloadWork")	// do real reload
			Goto Ready
		ReloadLoop:
			TNT1 A 0 A_TakeInventory("RifleAmmo", 1)
			TNT1 A 0 A_GiveInventory("LoadedAmmoSlot1", 1)
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot1", CallACS("GetCurrentClipSize"), "ReloadFinish")
			TNT1 A 0 A_JumpIfInventory("RifleAmmo", 1, "ReloadLoop")
			Goto ReloadFinish
	}
}

ACTOR RealRifle2 : RealRifle
{
	Weapon.SlotNumber 3
	Weapon.AmmoType "LoadedAmmoSlot2"
	Weapon.AmmoType2 "LoadedAmmoSlot2"	// needed for automatic reloading to work after zoom. see above.

	States
	{
		Reload:
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot2", CallACS("GetCurrentClipSize"), "Ready")	// ignore if fully loaded
			TNT1 A 0 A_JumpIfInventory("RifleAmmo", 1, "ReloadWork")	// do real reload
			Goto Ready
		ReloadLoop:
			TNT1 A 0 A_TakeInventory("RifleAmmo", 1)
			TNT1 A 0 A_GiveInventory("LoadedAmmoSlot2", 1)
			TNT1 A 0 A_JumpIfInventory("LoadedAmmoSlot2", CallACS("GetCurrentClipSize"), "ReloadFinish")
			TNT1 A 0 A_JumpIfInventory("RifleAmmo", 1, "ReloadLoop")
			Goto ReloadFinish
	}
}
