View difference between Paste ID: 0jE249Ee and Hc7YwdZC
SHOW: | | - or go back to the newest paste.
1
2
--[[-------------------------------------------------------------------
3
	Modified Lightsaber:
4
		Runs on the intuitive wOS Lightsaber Base
5
			Powered by
6
						  _ _ _    ___  ____  
7
				__      _(_) | |_ / _ \/ ___| 
8
				\ \ /\ / / | | __| | | \___ \ 
9
				 \ V  V /| | | |_| |_| |___) |
10
				  \_/\_/ |_|_|\__|\___/|____/ 
11
											  
12
 _____         _                 _             _           
13
|_   _|__  ___| |__  _ __   ___ | | ___   __ _(_) ___  ___ 
14
  | |/ _ \/ __| '_ \| '_ \ / _ \| |/ _ \ / _` | |/ _ \/ __|
15
  | |  __/ (__| | | | | | | (_) | | (_) | (_| | |  __/\__ \
16
  |_|\___|\___|_| |_|_| |_|\___/|_|\___/ \__, |_|\___||___/
17
                                         |___/             
18
----------------------------- Copyright 2017, David "King David" Wiltos ]]--[[
19
							  
20
	Lua Developer: King David
21
	Contact: http://steamcommunity.com/groups/wiltostech
22
		
23
-- Copyright 2017, David "King David" Wiltos ]]--
24
25
AddCSLuaFile()
26
27
SWEP.Base = "weapon_lightsaber"
28
SWEP.WeaponSynched = true
29
30
SWEP.PrintName = "Training Saber"
31
SWEP.Author = "Robotboy655 + King David"
32
SWEP.Category = "wiltOS Technologies"
33
SWEP.Contact = ""
34
SWEP.Purpose = "To train in the ways of lightsaber combat."
35
SWEP.Instructions = "Harden your drive."
36
SWEP.RenderGroup = RENDERGROUP_BOTH
37
38
SWEP.Slot = 0
39
SWEP.SlotPos = 4
40
41
SWEP.Spawnable = true
42
SWEP.DrawAmmo = false
43
SWEP.DrawCrosshair = false
44
SWEP.AutoSwitchTo = false
45
SWEP.AutoSwitchFrom = false
46
SWEP.DrawWeaponInfoBox = false
47
48
SWEP.ViewModel = "models/weapons/v_crowbar.mdl"
49
SWEP.WorldModel = "models/sgg/starwars/weapons/w_anakin_ep2_saber_hilt.mdl"
50
SWEP.ViewModelFOV = 55
51
52
SWEP.Primary.ClipSize = -1
53
SWEP.Primary.DefaultClip = -1
54
SWEP.Primary.Automatic = false
55
SWEP.Primary.Ammo = "none"
56
57
SWEP.Secondary.ClipSize = -1
58
SWEP.Secondary.DefaultClip = -1
59
SWEP.Secondary.Automatic = true
60
SWEP.Secondary.Ammo = "none"
61
62
SWEP.HoldType = "bs-arrogant-balanced"
63
64
SWEP.IsLightsaber = true
65
SWEP.ForceSelectEnabled = false
66
67
function SWEP:PrimaryAttack()
68
	if ( !IsValid( self.Owner ) ) then return end
69
70
	self:SetNextAttack( 1 )
71
72
	if ( !self.Owner:IsNPC() && self:GetEnabled() ) then
73
		self.Owner:AnimResetGestureSlot( GESTURE_SLOT_CUSTOM )
74
		self.Owner:SetAnimation( PLAYER_ATTACK1 )
75
	end
76
end
77
78
function SWEP:GetTargetHoldType()
79
80
	return "bs-arrogant-balanced"
81
	
82
end
83
84
if SERVER then return end
85
86
local rb655_lightsaber_hud_blur = CreateClientConVar( "rb655_lightsaber_hud_blur", "0" )
87
88
local grad = Material( "gui/gradient_up" )
89
local matBlurScreen = Material( "pp/blurscreen" )
90
matBlurScreen:SetFloat( "$blur", 3 )
91
matBlurScreen:Recompute()
92
93
local function DrawHUDBox( x, y, w, h, b )
94
95
	x = math.floor( x )
96
	y = math.floor( y )
97
	w = math.floor( w )
98
	h = math.floor( h )
99
100
	surface.SetMaterial( matBlurScreen )
101
	surface.SetDrawColor( 255, 255, 255, 255 )
102
103
	if ( rb655_lightsaber_hud_blur:GetBool() ) then
104
		render.SetScissorRect( x, y, w + x, h + y, true )
105
			for i = 0.33, 1, 0.33 do
106
				matBlurScreen:SetFloat( "$blur", 5 * i )
107
				matBlurScreen:Recompute()
108
				render.UpdateScreenEffectTexture()
109
				surface.DrawTexturedRect( 0, 0, ScrW(), ScrH() )
110
			end
111
		render.SetScissorRect( 0, 0, 0, 0, false )
112
	else
113
		draw.NoTexture()
114
		surface.SetDrawColor( Color( 0, 0, 0, 128 ) )
115
		surface.DrawTexturedRect( x, y, w, h )
116
	end
117
118
	surface.SetDrawColor( Color( 0, 0, 0, 128 ) )
119
	surface.DrawRect( x, y, w, h )
120
121
	if ( b ) then
122
		surface.SetMaterial( grad )
123
		surface.SetDrawColor( Color( 0, 128, 255, 4 ) )
124
		surface.DrawTexturedRect( x, y, w, h )
125
	end
126
127
end
128
129
130
local ForceBar = 100
131
function SWEP:DrawHUD()
132
	if ( !IsValid( self.Owner ) or self.Owner:GetViewEntity() != self.Owner or self.Owner:InVehicle() ) then return end
133
134
	-----------------------------------
135
136
	local icon = 52
137
	local gap = 5
138
139
	local bar = 4
140
	local bar2 = 16
141
142
	if ( self.ForceSelectEnabled ) then
143
		icon = 128
144
		bar = 8
145
		bar2 = 24
146
	end
147
148
	local ForcePowers = self:GetActiveForcePowers()
149
150
	if ( #ForcePowers < 1 ) then self:DrawHUDTargetSelection() return end
151
152
	----------------------------------- Force Bar -----------------------------------
153
154
	ForceBar = math.min( 100, Lerp( 0.1, ForceBar, math.floor( self:GetForce() ) ) )
155
156
	local w = #ForcePowers * icon + ( #ForcePowers - 1 ) * gap
157
	local h = bar2
158
	local x = math.floor( ScrW() / 2 - w / 2 )
159
	local y = ScrH() - gap - bar2
160
161
	DrawHUDBox( x, y, w, h )
162
163
	local barW = math.ceil( w * ( ForceBar / 100 ) )
164
	if ( self:GetForce() <= 1 && barW <= 1 ) then barW = 0 end
165
	draw.RoundedBox( 0, x, y, barW, h, Color( 0, 128, 255, 255 ) )
166
167
	draw.SimpleText( math.floor( self:GetForce() ) .. "%", "SelectedForceHUD", x + w / 2, y + h / 2, Color( 255, 255, 255 ), 1, 1 )
168
169
	----------------------------------- Force Icons -----------------------------------
170
171
	local y = y - icon - gap
172
	local h = icon
173
174
	for id, t in pairs( ForcePowers ) do
175
		local x = x + ( id - 1 ) * ( h + gap )
176
		local x2 = math.floor( x + icon / 2 )
177
178
		DrawHUDBox( x, y, h, h, self:GetForceType() == id )
179
		draw.SimpleText( t.icon or "", "SelectedForceType", x2, math.floor( y + icon / 2 ), Color( 255, 255, 255 ), 1, 1 )
180
		if ( self.ForceSelectEnabled ) then
181
			draw.SimpleText( ( input.LookupBinding( "slot" .. id ) or "<NOT BOUND>" ):upper(), "SelectedForceHUD", x + gap, y + gap, Color( 255, 255, 255 ) )
182
		end
183
		if ( self:GetForceType() == id ) then
184
			local y = y + ( icon - bar )
185
			surface.SetDrawColor( 0, 128, 255, 255 )
186
			draw.NoTexture()
187
			surface.DrawPoly( {
188
				{ x = x2 - bar, y = y },
189
				{ x = x2, y = y - bar },
190
				{ x = x2 + bar, y = y }
191
			} )
192
			draw.RoundedBox( 0, x, y, h, bar, Color( 0, 128, 255, 255 ) )
193
		end
194
	end
195
196
	----------------------------------- Force Description -----------------------------------
197
198
	local selectedForcePower = self:GetActiveForcePowerType( self:GetForceType() )
199
200
	if ( selectedForcePower && self.ForceSelectEnabled ) then
201
202
		surface.SetFont( "SelectedForceHUD" )
203
		local tW, tH = surface.GetTextSize( selectedForcePower.description or "" )
204
205
		--[[local x = x + w + gap
206
		local y = y]]
207
		local x = ScrW() / 2 + gap-- - tW / 2
208
		local y = y - tH - gap * 3
209
210
		DrawHUDBox( x, y, tW + gap * 2, tH + gap * 2 )
211
212
		for id, txt in pairs( string.Explode( "\n", selectedForcePower.description or "" ) ) do
213
			draw.SimpleText( txt, "SelectedForceHUD", x + gap, y + ( id - 1 ) * ScreenScale( 6 ) + gap, Color( 255, 255, 255 ) )
214
		end
215
216
	end
217
218
	----------------------------------- Force Label -----------------------------------
219
220
	if ( !self.ForceSelectEnabled ) then
221
		surface.SetFont( "SelectedForceHUD" )
222
		local txt = "Press " .. ( input.LookupBinding( "impulse 100" ) or "<NOT BOUND>" ):upper() .. " to toggle Force selection"
223
		local tW, tH = surface.GetTextSize( txt )
224
225
		local x = x + w / 2
226
		local y = y - tH - gap
227
228
		DrawHUDBox( x - tW / 2 - 5, y, tW + 10, tH )
229
		draw.SimpleText( txt, "SelectedForceHUD", x, y, Color( 255, 255, 255 ), 1 )
230
231
232
		if ( self:GetIncorrectPlayerModel() != 0 ) then
233
			local txt = "Server is missing the player model or missing owner! Send a screenshot of this error to the mod page!\nPlayer model: " .. self.Owner:GetModel() .. " - Error Code: " .. self:GetIncorrectPlayerModel()
234
			local tW, tH = surface.GetTextSize( txt )
235
236
			y = y - tH - gap
237
238
			DrawHUDBox( x - tW / 2 - 5, y, tW + 10, tH )
239
			for id, str in pairs( string.Explode( "\n", txt ) ) do
240
				local _, lineH = surface.GetTextSize( str )
241
				draw.SimpleText( str, "SelectedForceHUD", x, y + ( id - 1 ) * lineH, Color( 255, 200, 200 ), 1 )
242
			end
243
		end
244
	end
245
246
	if ( selectedForcePower && self.ForceSelectEnabled ) then
247
		surface.SetFont( "SelectedForceType" )
248
		local txt = selectedForcePower.name or ""
249
		local tW2, tH2 = surface.GetTextSize( txt )
250
251
		local x = x + w / 2 - tW2 - gap * 2 --+ w / 2
252
		local y = y + gap - tH2 - gap * 2
253
254
		DrawHUDBox( x, y, tW2 + 10, tH2 )
255
		draw.SimpleText( txt, "SelectedForceType", x + gap, y, Color( 255, 255, 255 ) )
256
	end
257
258
	----------------------------------- Force Target -----------------------------------
259
260
	self:DrawHUDTargetSelection()
261
262
end
263
264
function SWEP:PlayWeaponSound( snd )
265
	if ( CLIENT ) then return end
266
	if ( IsValid( self:GetOwner() ) && IsValid( self:GetOwner():GetActiveWeapon() ) && self:GetOwner():GetActiveWeapon() != self ) then return end
267
	if ( !IsValid( self.Owner ) ) then return self:EmitSound( snd ) end
268
	self.Owner:EmitSound( snd )
269
end
270
271
function SWEP:SelectTargets( num )
272
	local t = {}
273
	local dist = 512
274
275
	--[[local tr = util.TraceLine( {
276
		start = self.Owner:GetShootPos(),
277
		endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * dist,
278
		filter = self.Owner
279
	} )]]
280
281
	local p = {}
282
	for id, ply in pairs( ents.GetAll() ) do
283
		if ( !ply:GetModel() or ply:GetModel() == "" or ply == self.Owner or ply:Health() < 1 ) then continue end
284
		if ( string.StartWith( ply:GetModel() or "", "models/gibs/" ) ) then continue end
285
		if ( string.find( ply:GetModel() or "", "chunk" ) ) then continue end
286
		if ( string.find( ply:GetModel() or "", "_shard" ) ) then continue end
287
		if ( string.find( ply:GetModel() or "", "_splinters" ) ) then continue end
288
289
		local tr = util.TraceLine( {
290
			start = self.Owner:GetShootPos(),
291
			endpos = ply.GetShootPos && ply:GetShootPos() or ply:GetPos(),
292
			filter = self.Owner,
293
		} )
294
295
		if ( tr.Entity != ply && IsValid( tr.Entity ) or tr.Entity == game.GetWorld() ) then continue end
296
297
		local pos1 = self.Owner:GetPos() + self.Owner:GetAimVector() * dist
298
		local pos2 = ply:GetPos()
299
		local dot = self.Owner:GetAimVector():Dot( ( self.Owner:GetPos() - pos2 ):GetNormalized() )
300
301
		if ( pos1:Distance( pos2 ) <= dist && ply:EntIndex() > 0 && ply:GetModel() && ply:GetModel() != "" ) then
302
			table.insert( p, { ply = ply, dist = tr.HitPos:Distance( pos2 ), dot = dot, score = -dot + ( ( dist - pos1:Distance( pos2 ) ) / dist ) * 50 } )
303
		end
304
	end
305
306
	for id, ply in SortedPairsByMemberValue( p, "dist" ) do
307
		table.insert( t, ply.ply )
308
		if ( #t >= num ) then return t end
309
	end
310
311-
end
311+
312
end
313
314
----------------------------------- Initialize -----------------------------------
315
316
function SWEP:LoadToolValues( ply )
317
	local maxLen = ply:GetInfoNum( "rb655_lightsaber_bladel", 42 )
318
	local bldWidth = ply:GetInfoNum( "rb655_lightsaber_bladew", 2 )
319
	local Vector = ply:GetInfoNum( "rb655_lightsaber_red", 255 ), ply:GetInfoNum( "rb655_lightsaber_green", 0 ), ply:GetInfoNum( "rb655_lightsaber_blue", 0 )
320
	local WorldModel = ply:GetInfoNum ( "rb655_lightsaber_model", "models/training/training.mdl" )
321
322
	self:SetMaxLength( maxLen )
323
	self:SetBladeWidth( bldWidth )
324
	self:SetCrystalColor( Vector( ply:GetInfo( "rb655_lightsaber_red" ), ply:GetInfo( "rb655_lightsaber_green" ), ply:GetInfo( "rb655_lightsaber_blue" ) ) )
325
	self:SetDarkInner( ply:GetInfo( "rb655_lightsaber_dark" ) == "1" )
326
	self:SetWorldModel( ply:GetInfo( "rb655_lightsaber_model" ) )
327
	self:SetModel( self:GetWorldModel() )
328
	self.WorldModel = self:GetWorldModel()
329
	--self:PhysicsInit( SOLID_VPHYSICS )
330
331
	self.LoopSound = ply:GetInfo( "rb655_lightsaber_humsound" )
332
	self.SwingSound = ply:GetInfo( "rb655_lightsaber_swingsound" )
333
	self:SetOnSound( ply:GetInfo( "rb655_lightsaber_onsound" ) )
334
	self:SetOffSound( ply:GetInfo( "rb655_lightsaber_offsound" ) )
335
	--self:SetEnabled( ply:GetInfo( "rb655_lightsaber_starton" ) )
336
337
	self.WeaponSynched = true
338
339
	-- Start it if we spawned it using the spawnmenu!
340
	if ( !IsValid( self.Owner ) || self.Owner:IsPlayer() ) then
341
		-- Gotta wait a tick so we don't play double sounds!
342
		timer.Simple( 0, function() if ( !IsValid( self ) ) then return end self:SetEnabled( false ) end )
343
	end
344
end
345
346
hook.Add( "PlayerSpawnedSWEP", "rb655_lightsaber_swep_sync", function( ply, wep )
347
	if ( !rb655_IsLightsaber( wep ) ) then return end
348
349
	wep:LoadToolValues( ply )
350
end )