Benny/gamemodes/benny/entities/weapons/benny/shared.lua

292 lines
7.9 KiB
Lua

-- The benny weapon handles the weapon pickups you find throughout the game.
SWEP.Base = "weapon_base"
SWEP.PrintName = "Benny Weapon Handler"
SWEP.ViewModel = "models/weapons/c_pistol.mdl"
SWEP.ViewModelFOV = 10
SWEP.WorldModel = "models/weapons/w_pistol.mdl"
SWEP.Primary.ClipSize = 0
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"
SWEP.Secondary.ClipSize = 0
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = "none"
AddCSLuaFile( "sh_statregen.lua" )
include ( "sh_statregen.lua" )
AddCSLuaFile( "sh_firing.lua" )
include ( "sh_firing.lua" )
AddCSLuaFile( "sh_inv.lua" )
include ( "sh_inv.lua" )
AddCSLuaFile( "sh_holdtypes.lua" )
include ( "sh_holdtypes.lua" )
AddCSLuaFile( "sh_reload.lua" )
include ( "sh_reload.lua" )
AddCSLuaFile( "cl_wm.lua" )
if CLIENT then
include ( "cl_wm.lua" )
end
function SWEP:SetupDataTables()
self:NetworkVar( "Float", 0, "Aim" )
self:NetworkVar( "Float", 1, "Delay1" )
self:NetworkVar( "Float", 2, "Delay2" )
self:NetworkVar( "Float", 3, "GrenadeDownStart" )
self:NetworkVar( "Float", 4, "Wep1_Spread" )
self:NetworkVar( "Float", 5, "Wep2_Spread" )
self:NetworkVar( "Float", 6, "Wep1_ShotTime" )
self:NetworkVar( "Float", 7, "Wep2_ShotTime" )
self:NetworkVar( "Float", 8, "Wep1_Holstering" )
self:NetworkVar( "Float", 9, "Wep2_Holstering" )
self:NetworkVar( "Float", 10, "Wep1_Reloading" )
self:NetworkVar( "Float", 11, "Wep2_Reloading" )
self:NetworkVar( "String", 0, "Wep1" )
self:NetworkVar( "String", 1, "Wep2" )
self:NetworkVar( "String", 2, "Wep1_Clip" )
self:NetworkVar( "String", 3, "Wep2_Clip" )
self:NetworkVar( "Int", 0, "Wep1_Burst" )
self:NetworkVar( "Int", 1, "Wep2_Burst" )
self:NetworkVar( "Int", 2, "Wep1_Firemode" )
self:NetworkVar( "Int", 3, "Wep2_Firemode" )
self:NetworkVar( "Int", 4, "Wep1_ReloadType" )
self:NetworkVar( "Int", 5, "Wep2_ReloadType" )
self:NetworkVar( "Bool", 0, "UserAim" )
self:NetworkVar( "Bool", 1, "GrenadeDown" )
self:SetWep1_Firemode( 1 )
self:SetWep2_Firemode( 1 )
self:SetWep1_Holstering( -1 )
self:SetWep2_Holstering( -1 )
self:SetWep1_Reloading( -1 )
self:SetWep2_Reloading( -1 )
end
-- BENNY shit
function SWEP:BTable( alt )
return self:GetOwner():INV_Get()[ alt and self:GetWep2() or self:GetWep1() ]
end
function SWEP:BClass( alt )
local ta = self:BTable( alt )
if ta then
return WEAPONS[ ta.Class ]
else
return false
end
end
function SWEP:B_Ammo( hand, value )
local p = self:GetOwner()
local inv = p:INV_Get()
self:D_SetClip( hand, value )
assert( self:D_GetMagID( hand ) != "", "There is no magazine loaded!" )
inv[ self:D_GetMagID( hand ) ].Ammo = value
end
function SWEP:B_Firemode( alt )
return self:BClass( alt ).Firemodes[ self:D_GetFiremode( alt ) ]
end
function SWEP:B_FiremodeName( alt )
local mode = self:B_Firemode( alt ).Mode
if mode == 1 then
return "SEMI"
elseif mode == math.huge then
return "AUTO"
else
return mode .. "RND"
end
end
hook.Add( "PlayerButtonDown", "Benny_PlayerButtonDown_TempForAim", function( ply, button )
local wep = ply:BennyCheck()
if wep then
if button == KEY_F then
if tobool(ply:GetInfoNum("benny_wep_toggleaim", 1)) then
wep:SetUserAim( !wep:GetUserAim() )
else
wep:SetUserAim( true )
end
end
local dual = wep:C_DualCheck()
if button == KEY_R then
if dual then wep:Reload( true ) else wep:Reload( false ) end
end
if button == KEY_T then
if dual then wep:Reload( false ) else wep:Reload( true ) end
end
end
end)
hook.Add( "PlayerButtonUp", "Benny_PlayerButtonUp_TempForAim", function( ply, button )
local wep = ply:BennyCheck()
if wep then
if button == KEY_F then
if !tobool(ply:GetInfoNum("benny_wep_toggleaim", 0)) then
wep:SetUserAim( false )
end
end
end
end)
function SWEP:BStartHolster( hand )
if self:D_GetHolstering( hand ) == -1 then
B_Sound( self, "Common.Holster" )
-- print( "Holstering the " .. (hand and "LEFT" or "RIGHT") )
self:D_SetHolstering( hand, 0 )
self:D_SetReloading( hand, -1 )
self:D_SetReloadType( hand, 0 )
end
end
function SWEP:BThinkHolster( hand )
if self:D_GetHolstering( hand ) >= 0 then
self:D_SetHolstering( hand, math.Approach( self:D_GetHolstering( hand ), 1, FrameTime() / 0.35 ) )
end
if self:D_GetHolstering( hand ) == 1 then
self:D_SetHolstering( hand, -1 )
self:D_SetReloading( hand, -1 )
self:D_SetReloadType( hand, 0 )
self:BHolster( hand )
local p = self:GetOwner()
local req = self:D_GetReqID( hand )
local inv = p:INV_Get()
if req != "" and inv[req] then
self:BDeploy( hand, req )
end
end
end
function SWEP:Think()
local p = self:GetOwner()
local inv = p:INV_Get()
local wep1 = self:BTable( false )
local wep1c = self:BClass( false )
local wep2 = self:BTable( true )
local wep2c = self:BClass( true )
if self:D_GetReqID( false ) != "" and self:D_GetReqID( true ) != "" and self:D_GetReqID( false ) == self:D_GetReqID( true ) then
self:D_SetReqID( false, "" )
self:D_SetReqID( true, "" )
if CLIENT then chat.AddText( "Same weapons on ReqID, both holstered" ) end
end
for i=1, 2 do
local hand = i==2
if self:D_GetReqID( hand ) != "" and !inv[self:D_GetReqID( hand )] then
self:D_SetReqID( hand, "" )
end
local req = self:D_GetReqID( hand )
local req_o = self:D_GetReqID( !hand )
local curr = self:D_GetID( hand )
local curr_o = self:D_GetID( !hand )
if req != curr then
if curr != "" then
-- require holster first
self:BStartHolster( hand )
else
local otherhasthis = curr_o == req
if req != "" then
if otherhasthis then
self:BStartHolster( !hand )
else
self:BDeploy( hand, req )
end
else
self:BStartHolster( hand )
end
end
end
self:BThinkHolster( hand )
do -- Reload logic
if self:D_GetReloading( hand ) != -1 then
local rlt = self:D_GetReloadType( hand )
-- TODO: Unshitify this.
if RealTime() >= self:D_GetReloading( hand ) + (rlt == 1 and self:GetStat( hand, "Reload_MagIn" ) or rlt == 2 and self:GetStat( hand, "Reload_MagOut" )) then
if rlt == 1 then
if SERVER or (CLIENT and IsFirstTimePredicted() ) then
self:Reload_MagIn( hand, self:D_GetMagID( hand ), inv )
end
elseif rlt == 2 then
end
self:D_SetReloading( hand, -1 )
self:D_SetReloadType( hand, 0 )
-- Do reload stuff.
end
end
end
end
self:SetAim( math.Approach( self:GetAim(), self:GetUserAim() and 1 or 0, FrameTime()/0.2 ) )
for i=1, 2 do
local hand = i==2
if !self:C_AttackDown( hand ) then
self:D_SetBurst( hand, 0 )
end
end
for i=1, 2 do
local hand = i==2
local wep, wepc = self:BTable( hand ), self:BClass( hand )
if wepc and wepc.Features == "firearm" and self:D_GetDelay( hand ) < CurTime()-0.01 then
local mweh = math.Remap( CurTime(), self:D_GetShotTime( hand ), self:D_GetShotTime( hand ) + self:GetStat( hand, "SpreadDecay_RampTime" ), 0, 1 )
mweh = math.Clamp( mweh, 0, 1 )
local decayfinal = Lerp( math.ease.InExpo( mweh ), self:GetStat( hand, "SpreadDecay_Start" ), self:GetStat( hand, "SpreadDecay_End" ) )
self:D_SetSpread( hand, math.Approach( self:D_GetSpread( hand ), 0, decayfinal * FrameTime() ) )
end
end
local ht = "normal"
if self:GetUserAim() and self:D_GetHolstering( false ) < 0 then
if self:BClass( false ) then
if self:BClass( true ) then
ht = "duel"
else
ht = self:BClass( false ).HoldType or "revolver"
end
end
end
if ht == "normal" and self:GetHoldType() != "normal" then
self:TPHolster( false )
elseif ht != "normal" and self:GetHoldType() == "normal" then
self:TPDraw( false )
end
for i=1, 2 do
local hand = i==2
if self:BClass( hand ) then
if self:BClass( hand ).Custom_Think then
self:BClass( hand ).Custom_Think( self, self:BTable( hand ) )
end
end
end
self:SetWeaponHoldType(ht)
self:SetHoldType(ht)
return true
end
function SWEP:Deploy()
return true
end
function SWEP:Holster()
return true
end