-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitmarkers.txt
631 lines (495 loc) · 19.5 KB
/
hitmarkers.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
--@name Hitmarkers
--@author legokidlogan
--@shared
if SERVER then
local headDist = 10
local headDistSqr = headDist ^ 2
local connectedPlyCount = 0
local connectedPlys = {}
local connectedPlys2 = {}
hook.add( "PlayerSay", "LKL_Hitmarkers_PlayerSay", function( ply, msg )
local LS = string.explode( " ", msg )
if LS[1] ~= "/hit" then return end
local command = LS[2]
if command == "sounds" or command == "sound" or command == "s" then
if not connectedPlys[ply] then return end
net.start( "LKL_Hitmarkers_ToggleSounds" )
net.send( ply )
end
if ply ~= owner() then return end
if command == "players" or command == "users" or command == "p" or command == "u" then
local plyCount = table.count( connectedPlys )
plyCount = plyCount > 0 and plyCount
local out = plyCount and {
Color( 255, 255, 255 ), "[Hitmarkers] The players currently connected are ",
} or {
Color( 255, 255, 255 ), "[Hitmarkers] ",
Color( 255, 150, 150 ), "No one is connected!"
}
if plyCount then
local indBase = 2
for cPly, plyData in pairs( connectedPlys ) do
plyData = plyData or {
"An unkown player",
Color( 152, 212, 255 ),
}
rawset( out, indBase + 1, rawget( plyData, 2 ) )
rawset( out, indBase + 2, rawget( plyData, 1 ) )
rawset( out, indBase + 3, Color( 255, 255, 255 ) )
rawset( out, indBase + 4, ", " )
indBase = indBase + 4
end
indBase = ( plyCount - 1 ) * 4 + 2
rawset( out, indBase + 4, "." )
if plyCount > 1 then
rawset( out, indBase, plyCount == 2 and " and " or ", and " )
end
end
print( unpack( out ) )
else
print(
Color( 255, 255, 255 ), "[Hitmarkers] ",
Color( 255, 0, 0 ), "That is not a valid command! ",
Color( 255, 255, 255 ), "The available commands are ",
Color( 255, 255, 0 ), "users",
Color( 255, 255, 255 ),"."
)
end
return ""
end )
net.receive( "LKL_Hitmarkers_HUDChange", function( _, ply )
local valid = isValid( ply )
local state = net.readBool()
local plyName = valid and ply:getName() or "An unkown player"
local plyColor = valid and team.getColor( ply:getTeam() ) or Color( 152, 212, 255 )
local stateStr = state and " has connected." or " has disconnected."
local stateColor = state and Color( 150, 255, 150 ) or Color( 255, 150, 150 )
if valid then
local val = state and { plyName, plyColor } or nil
rawset( connectedPlys, ply, val )
if val then
table.insert( connectedPlys2, ply )
else
table.removeByValue( connectedPlys2, ply )
end
else
for i = #connectedPlys2, 1, -1 do
if not isValid( rawget( connectedPlys2, i ) ) then
table.remove( connectedPlys2, i )
end
end
if state then
if ply then
local plyData = rawget( connectedPlys, ply )
if plyData then
plyName = rawget( plyData, 1 )
plyColor = rawget( plyData, 2 )
else
rawset( connectedPlys, ply, { plyName, plyColor } )
end
end
else
if ply then
local plyData = rawget( connectedPlys, ply )
if plyData then
plyName = rawget( plyData, 1 )
plyColor = rawget( plyData, 2 )
end
end
for i = 1, #connectedPlys do
if not isValid( rawget( connectedPlys, i ) ) then
rawset( connectedPlys, i, nil )
end
end
end
end
connectedPlyCount = #connectedPlys2
print(
Color( 255, 255, 255 ), "[Hitmarkers] ",
plyColor, plyName,
stateColor, stateStr
)
end )
hook.add( "EntityTakeDamage", "LKL_HitMarkers_EntityTakeDamage", function( ent, attacker, inflictor, amount, type, pos )
if not isValid( ent ) or not ent:isPlayer() or ent == attacker or not connectedPlys[attacker] then return end
--print( "dmg", ent, amount )
attacker.hitmarkerPoints = attacker.hitmarkerPoints or {}
attacker.hitmarkerPoints[ent] = pos
end )
hook.add( "PlayerHurt", "LKL_Hitmarkers_PlayerHurt", function( ply, attacker, newHealth, damage )
if not connectedPlys[attacker] then return end
--print( "hurt", ply, damage )
attacker.hitmarkerPoints = attacker.hitmarkerPoints or {}
local pos = attacker.hitmarkerPoints[ply] or ply:getAttachment( ply:lookupAttachment( "chest" ) ) or ply:getPos() + Vector( 0, 0, ply:obbSize().z * 2 / 3 )
local headShot = false
local headInd = ply:lookupAttachment( "eyes" )
if headInd ~= 0 then
local headPos = ply:getAttachment( headInd )
headShot = headPos:getDistanceSqr( pos ) <= headDistSqr
end
net.start( "LKL_Hitmarkers_Hit" )
net.writeEntity( ply )
net.writeVector( pos )
net.writeFloat( damage )
net.writeBool( headShot )
net.send( attacker )
attacker.hitmarkerPoints[ply] = nil
end )
hook.add( "PlayerDeath", "LKL_Hitmarkers_PlayerDeath", function( ply )
if not connectedPlys[ply] then return end
net.start( "LKL_Hitmarkers_Die" )
net.send( ply )
end )
hook.add( "PlayerDeath", "LKL_Hitmarkers_PlayerKill", function( ply, _, attacker )
if not connectedPlys[attacker] then return end
net.start( "LKL_Hitmarkers_Kill" )
net.writeEntity( ply )
net.send( attacker )
end )
hook.add( "PlayerSpawn", "LKL_Hitmarkers_PlayerSpawn", function( ply )
if not connectedPlys[ply] then return end
net.start( "LKL_Hitmarkers_Spawn" )
net.send( ply )
end )
else
local streakLength = 5
local fadeTime = 3
local fadeTimeMini = 2.5
local miniHitVelMin = 1.5
local miniHitVelMax = 3
local miniHitIntertia = 0.93
local miniHitGravity = 0.03
local updateInterval = 0.01
local roundDecimals = 1
local doSounds = true
local soundTbl = {
Hit = {
Path = "sound/buttons/lightswitch2.wav",
Bass = false,
Pitch = function() return math.rand( 0.9, 1.1 ) end,
Volume = 1.5,
},
Headshot = {
Path = "sound/buttons/button16.wav",
Bass = false,
Pitch = function() return math.rand( 0.95, 1.1 ) end,
Volume = 1,
},
Kill = {
Path = "sound/buttons/combine_button1.wav",
Bass = false,
Pitch = 1,
Volume = 1.5,
},
Template = {
Path = "",
Bass = false,
Pitch = 1,
Volume = 1.5,
},
}
local infoFontSize = 25
local infoColorBase = Color( 255, 255, 255, 255 )
local infoColorHighlight = Color( 255, 255, 0, 255 )
local infoColorPositive = Color( 150, 255, 150, 255 )
local infoColorPositiveHighlight = Color( 0, 255, 0, 255 )
local infoColorNegative = Color( 255, 150, 150, 255 )
local infoColorNegativeHighlight = Color( 255, 0, 0, 255 )
local markerColor = Color( 255, 0, 0, 255 )
local markerColorMini = Color( 255, 100, 0, 255 )
--local markerFontSize = 5000
local markerFontSize = 30
local markerFontSizeMini = 30
--local markerFontData = { 400, true, false, false, false, false, false }
--local markerFontDataMini = { 400, true, false, false, false, false, false }
-----------------------------------------------------------------------------
local streaking = false
local fullTotal = 0
local fullDPS = 0
local lifeTotal = 0
local lifeDPS = 0
local streakTotal = 0
local streakDPS = 0
local hitCountMini = 0
local fullTimeStart
local lifeTimeStart
local streakTimeStart
local scrW
local scrH
local hitSound
local hitScores = {}
local hitFonts = {}
local hitColors = {}
local hitTimes = {}
local hitPoints = {}
local hitPointsMini = {}
local infoTextLengths = {}
local infoTextValues = {}
local infoTexts = {
"Total damage: ",
"Total DPS: ",
"Lifetime damage: ",
"Lifetime DPS: ",
"Streak damage: ",
"Streak DPS: ",
}
for i, text in ipairs( infoTexts ) do
infoTextLengths[i] = text:len() * infoFontSize / 2
end
local permissions = {
"render.hud",
"bass.loadFile",
"bass.play2D",
}
setupPermissionRequest( permissions, "See how much damage you're doing", true )
local permissionSatisfied
local infoFont = render.createFont( "Roboto Mono", infoFontSize, 400, true, false, false, false, false, false )
local markerFont = render.createFont( "Roboto Mono", markerFontSize, 500, true, false, false, false, false, false )
local markerFontMini = render.createFont( "Roboto Mono", markerFontSizeMini, 500, true, false, false, false, false, false )
local function doSound( soundType )
local snd = soundTbl[soundType]
if not snd then return end
local path = snd.Path
local bassObj = snd.Bass
if not bassObj then
if not permissionSatisfied then return end
bass.loadFile( path, "mono", function( s )
bassObj = s
snd.Bass = bassObj
end )
if not bassObj then return end
end
local pitch = snd.Pitch
local volume = snd.Volume
if type( pitch ) == "function" then
pitch = pitch()
end
if type( volume ) == "function" then
volume = volume()
end
bassObj:setPitch( pitch )
bassObj:setVolume( volume )
bassObj:play()
end
local function updateInfoValues()
infoTextValues = {
fullTotal,
fullDPS,
lifeTotal,
lifeDPS,
streakTotal,
streakDPS,
}
end
local function clearStreak()
streakTotal = 0
streakDPS = 0
streakTimeStart = nil
for ply, score in pairs( hitScores ) do
hitScores[ply] = nil
hitColors[ply] = nil
hitTimes[ply] = nil
hitPoints[ply] = nil
end
updateInfoValues()
end
local function clearLife()
lifeTotal = 0
lifeDPS = 0
lifeTimeStart = nil
updateInfoValues()
end
local function checkPermissions()
local isSatisfied = true
for i, permission in pairs( permissions ) do
if not hasPermission( permission ) then
isSatisfied = false
break
end
end
permissionSatisfied = isSatisfied
end
checkPermissions()
local function HUDChange( state )
checkPermissions()
if permissionSatisfied then
net.start( "LKL_Hitmarkers_HUDChange" )
net.writeBool( state )
net.send()
elseif state then
sendPermissionRequest()
end
end
hook.add( "hudconnected", "LKL_Hitmarkers_HUDConnect", function()
HUDChange( true )
end )
hook.add( "huddisconnected", "LKL_Hitmarkers_HUDConnect", function()
HUDChange( false )
end )
hook.add( "permissionrequest", "LKL_Hitmarkers_PermissionRequest", function()
checkPermissions()
if not permissionSatisfied then
enableHud( player(), false )
end
end )
hook.add( "drawhud", "LKL_HitMarkers_DrawHud", function()
if not permissionSatisfied then return end
if not scrW then
scrW, scrH = render.getGameResolution()
end
render.setFont( infoFont )
render.setColor( infoColorBase )
for i, text in ipairs( infoTexts ) do
if i == 5 then
render.setColor( ( streaking and infoColorPositive ) or infoColorNegative )
end
render.drawSimpleText( 0, ( i - 1 ) * infoFontSize, text, -1, 3 )
end
render.setColor( infoColorHighlight )
for i, length in ipairs( infoTextLengths ) do
if i == 5 then
render.setColor( ( streaking and infoColorPositiveHighlight ) or infoColorNegativeHighlight )
end
render.drawSimpleText( length, ( i - 1 ) * infoFontSize, tostring( infoTextValues[i] ), -1, 3 )
end
render.setFont( markerFontMini )
for i = 1, hitCountMini do
local hitMini = hitPointsMini[i]
local screenPos = hitMini.Pos:toScreen()
local xPos = screenPos.x
local yPos = screenPos.y
render.setColor( hitMini.Color )
render.drawSimpleText( xPos, yPos, hitMini.Text, 1, 1 )
end
render.setFont( markerFont )
for ply, score in pairs( hitScores ) do
local screenPos = hitPoints[ply]:toScreen()
local xPos = screenPos.x
local yPos = screenPos.y
render.setColor( hitColors[ply] )
--render.setFont( hitFonts[ply] )
render.drawSimpleText( xPos, yPos, tostring( score ), 1, 1 )
end
end )
if render.isHUDActive() then
HUDChange( true )
end
net.receive( "LKL_Hitmarkers_ForceDisconnect", function()
enableHud( player(), false )
end )
net.receive( "LKL_Hitmarkers_Hit", function()
local ply = net.readEntity()
local pos = net.readVector()
local dmg = math.round( net.readFloat(), roundDecimals )
local headShot = net.readBool()
if not streaking then
clearStreak()
streaking = true
end
hitScores[ply] = ( hitScores[ply] or 0 ) + dmg
hitColors[ply] = markerColor:clone()
hitTimes[ply] = timer.realtime()
hitPoints[ply] = pos
fullTotal = fullTotal + dmg
lifeTotal = lifeTotal + dmg
streakTotal = streakTotal + dmg
fullTimeStart = fullTimeStart or timer.realtime()
lifeTimeStart = lifeTimeStart or timer.realtime()
streakTimeStart = streakTimeStart or timer.realtime()
local hitVelMini = Vector( math.rand( -1, 1 ), math.rand( -1, 1 ), math.rand( -1, 1 ) )
hitVelMini:normalize()
hitCountMini = hitCountMini + 1
hitPointsMini[hitCountMini] = {
Pos = pos,
Text = headShot and ">" .. dmg .. "<" or dmg .. "",
Vel = hitVelMini * math.rand( miniHitVelMin, miniHitVelMax ),
Color = markerColorMini:clone(),
Time = timer.realtime(),
}
updateInfoValues()
if doSounds and permissionSatisfied then
doSound( headShot and "Headshot" or "Hit" )
end
timer.remove( "LKL_Hitmarkers_StreakTimeout" )
timer.create( "LKL_Hitmarkers_StreakTimeout", streakLength, 1, function()
streaking = false
end )
end )
net.receive( "LKL_Hitmarkers_Die", function()
clearLife()
streaking = false
timer.remove( "LKL_Hitmarkers_StreakTimeout" )
end )
net.receive( "LKL_Hitmarkers_Spawn", function()
lifeTimeStart = timer.realtime()
end )
net.receive( "LKL_Hitmarkers_ToggleSounds", function()
doSounds = not doSounds
end )
net.receive( "LKL_Hitmarkers_Kill", function()
local ply = net.readEntity()
if doSounds and permissionSatisfied then
doSound( "Kill" )
end
end )
timer.create( "LKL_Hitmarkers_UpdatePoints", updateInterval, 0, function()
if not permissionSatisfied or not render.isHUDActive() then return end
local curTime = timer.realtime()
for ply, time in pairs( hitTimes ) do
local alpha = 255 * ( 1 - ( curTime - time ) / fadeTime )
if alpha < 0 then
--[[
local oldFont = hitFonts[ply]
if oldFont then
oldFont:remove()
end
--]]
hitScores[ply] = nil
hitColors[ply] = nil
hitTimes[ply] = nil
hitPoints[ply] = nil
--hitFonts[ply] = nil
else
--[[
local oldFont = hitFonts[ply]
if oldFont then
oldFont:remove()
end
--]]
hitColors[ply]:setA( alpha )
--hitFonts[ply] = render.createFont( markerFont, markerFontSize / math.max( eyePos():getDistance( hitPoints[ply] ), 1 ), unpack( markerFontData ) )
end
end
for i = hitCountMini, 1, -1 do
local hitMini = hitPointsMini[i]
local alpha = 255 * ( 1 - ( curTime - hitMini.Time ) / fadeTimeMini )
if alpha < 0 then
table.remove( hitPointsMini, i )
hitCountMini = hitCountMini - 1
else
hitMini.Color:setA( alpha )
hitMini.Pos = hitMini.Pos + hitMini.Vel
hitMini.Vel = hitMini.Vel * miniHitIntertia + Vector( 0, 0, - miniHitGravity )
end
end
if fullTimeStart then
local timeDiff = curTime - fullTimeStart
if timeDiff > 0 then
fullDPS = math.round( fullTotal / timeDiff, roundDecimals )
end
end
if lifeTimeStart then
local timeDiff = curTime - lifeTimeStart
if timeDiff > 0 then
lifeDPS = math.round( lifeTotal / timeDiff, roundDecimals )
end
end
if streaking and streakTimeStart then
local timeDiff = curTime - streakTimeStart
if timeDiff > 0 then
streakDPS = math.round( streakTotal / timeDiff, roundDecimals )
end
end
updateInfoValues()
end )
end