-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
executable file
·890 lines (733 loc) · 19.5 KB
/
init.lua
File metadata and controls
executable file
·890 lines (733 loc) · 19.5 KB
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
---@type Plugin
local mode = ...
mode.name = 'Duels'
mode.author = 'jdb'
mode.description = 'A multi 1v1 arena similar to source mods.'
mode.defaultConfig = {
advertisement = '',
weaponWeights = {1,1,1,1,2,2,2,2,3,3,4,4,5}
}
local json = require 'main.json'
local elo = mode:require('elo')
local botProfiles = mode:require('botProfiles')
local duelLocations = mode:require('locations')
local profilesFile = 'duels-elo.json'
local profiles = {}
local profilesDirty = false
local defaultElo = 1500
local testingLocation = 0
local currentDuels = {}
local canShoot = false
local function saveProfiles ()
local f = io.open(profilesFile, 'w')
if f then
f:write(json.encode(profiles))
f:close()
mode:print('Profiles saved')
end
end
local duelWeapons = {
{
name = 'AK-47',
id = 1,
extraMags = 3
},
{
name = 'M-16',
id = 3,
extraMags = 3
},
{
name = 'MP5',
id = 7,
extraMags = 6
},
{
name = 'Uzi',
id = 9,
extraMags = 6
},
{
name = '9mm',
id = 11,
extraMags = 6
}
}
-- Global text chat rather than human talking
function mode.hooks.PlayerChat (ply, message)
if message:gsub('%W',''):lower():find('nigge') then
message = "i'm an idiot"
end
local str = string.format('<%s> %s', ply.name, message)
chat.announceWrap(str)
if log then
log('[Chat][G] %s (%s): %s', ply.name, dashPhoneNumber(ply.phoneNumber), message)
end
return hook.override
end
local specCamSpawnPoint = Vector(2536, 41, 1000)
function mode.hooks.PostPlayerCreate ()
if server.state == 1 then
events.createSound(37, specCamSpawnPoint)
end
end
function mode.hooks.PostPlayerDelete ()
if server.state == STATE_PREGAME then
events.createSound(29, specCamSpawnPoint)
end
end
-- Disable top 10 list, we have a custom one
function mode.hooks.EventMessage (type, message, speakerID)
if type == 0 and speakerID == -1 then
if message == 'Top 10 list' then
return hook.override
end
for i = 1, 10 do
if message:startsWith(i .. '. ') then
return hook.override
end
end
end
end
function mode.hooks.PhysicsBullets ()
local doGC = false
for _, bul in pairs(bullets.getAll()) do
local duel = bul.player.data.duel
if duel then
local loc = duel.location
if not isVectorInCuboid(bul.pos, loc.bounds[1], loc.bounds[2]) then
bul.time = -1
doGC = true
end
end
end
if doGC then
physics.garbageCollectBullets()
end
end
function mode.hooks.Physics ()
if server.state == STATE_GAME then
for _, man in pairs(humans.getAll()) do
if not canShoot then
man.inputFlags = bit32.band(man.inputFlags, bit32.bnot(1))
end
if man.isAlive and not man.isBleeding then
if man.leftArmHP < 100 then man.leftArmHP = man.leftArmHP + 1 end
if man.rightArmHP < 100 then man.rightArmHP = man.rightArmHP + 1 end
if man.leftLegHP < 100 then man.leftLegHP = man.leftLegHP + 1 end
if man.rightLegHP < 100 then man.rightLegHP = man.rightLegHP + 1 end
end
end
end
end
function mode.onEnable ()
local f = io.open(profilesFile, 'r')
if f then
local data = json.decode(f:read('*all'))
profiles = data
f:close()
mode:print('Profiles loaded')
end
server.type = TYPE_ROUND
server:reset()
end
function mode.hooks.PostResetGame ()
if profilesDirty then
profilesDirty = false
saveProfiles()
end
server.state = STATE_PREGAME
server.time = 5 * server.TPS
for _, ply in ipairs(players.getAll()) do
ply.data.duel = nil
end
currentDuels = {}
canShoot = false
-- Play a C chord : )
events.createSound(29, specCamSpawnPoint, 1, 1.49831)
events.createSound(29, specCamSpawnPoint, 1, 1.25992)
events.createSound(29, specCamSpawnPoint, 1, 1)
-- Disable random spread
for _, type in pairs(itemTypes.getAll()) do
if type.isGun then
type.bulletSpread = 0.0
end
end
end
local function getProfile (ply)
if ply.isBot then
return nil
end
-- Profile keys are strings so JSON doesn't see it is an array
local phone = tostring(ply.phoneNumber)
if not profiles[phone] then
profiles[phone] = {
elo = defaultElo
}
end
profiles[phone].name = ply.name
return profiles[phone]
end
local function getElo (ply)
if ply.isBot then
return defaultElo
end
return getProfile(ply).elo
end
local function setElo (ply, eloRating)
local profile = getProfile(ply)
if not profile then return end
profile.elo = eloRating
if profile.timesUpdated then
profile.timesUpdated = profile.timesUpdated + 1
else
profile.timesUpdated = 1
end
if ply.isActive then
ply.money = eloRating
ply:updateFinance()
end
end
--[[
Decided based on the distribution of all players' ratings
1200
1258 D-
1317 D
1375 D+
1433 C-
1492 C
1550 C+
1608 B-
1667 B
1725 B+
1788 A-
1842 A
1900 A+
1958
]]
local function getEloRank (eloRating)
if eloRating < 1258 then return 'D-' end
if eloRating < 1317 then return 'D' end
if eloRating < 1375 then return 'D+' end
if eloRating < 1433 then return 'C-' end
if eloRating < 1492 then return 'C' end
if eloRating < 1550 then return 'C+' end
if eloRating < 1608 then return 'B-' end
if eloRating < 1667 then return 'B' end
if eloRating < 1725 then return 'B+' end
if eloRating < 1788 then return 'A-' end
if eloRating < 1842 then return 'A' end
return 'A+'
end
do
local oldName
mode.hooks.EventUpdatePlayer = function (ply)
if ply.isBot then
oldName = ply.name
ply.name = '[Bot] ' .. ply.name
return
end
local profile = getProfile(ply)
if profile then
ply.money = profile.elo
ply:updateFinance()
if profile.timesUpdated and profile.timesUpdated >= 10 then
oldName = ply.name
ply.name = string.format('[%s] %s', getEloRank(profile.elo), ply.name)
end
end
end
mode.hooks.PostEventUpdatePlayer = function (ply)
if oldName then
ply.name = oldName
oldName = nil
end
end
end
local function isSomeoneReady (plys, desired)
for _, ply in pairs(plys) do
if ply.isReady == desired then
return true
end
end
return false
end
local function playSoundAll (plys, type, volume, pitch)
for _, ply in pairs(plys) do
local man = ply.human
if man and man.isAlive then
events.createSound(type, man.pos, volume, pitch)
end
end
end
local function duelSpawn (ply, spawn, shirtColor, wep, objectsTable)
ply.model = 0 --civilian
ply.suitColor = shirtColor
ply.tieColor = 0 --none
local pos = vecRandBetween(spawn[2], spawn[3])
local rot = orientations[spawn[1]]
local man = humans.create(pos, rot, ply)
table.insert(objectsTable, man)
if not hook.run('EventUpdatePlayer', ply) then
ply:update()
hook.run('PostEventUpdatePlayer', ply)
end
if ply.isBot then
ply.botDestination = spawn[4]
end
local gun = items.create(itemTypes[wep.id], pos, rot)
if gun then
table.insert(objectsTable, gun)
man:mountItem(gun, 0)
local loadedMag = items.create(itemTypes[wep.id + 1], pos, rot)
if loadedMag then
table.insert(objectsTable, loadedMag)
gun:mountItem(loadedMag, 0)
end
for _ = 1, wep.extraMags do
local mag = items.create(itemTypes[wep.id + 1], pos, rot)
if not mag then break end
table.insert(objectsTable, mag)
for slot = 3, 5 do
if man:mountItem(mag, slot) then
break
end
end
end
end
local bandage = items.create(itemTypes[14], pos, rot)
if bandage then
table.insert(objectsTable, bandage)
man:mountItem(bandage, 6)
end
end
local function sortCompare (a, b)
return a.money > b.money
end
local function chooseWeapon (a, b)
local weights = mode.config.weaponWeights
local weapons = {}
for _, ply in ipairs({a, b}) do
if not ply.isBot then
local profile = getProfile(ply)
local weapon
if profile then
local name = profile.gun
if name then
for _, wep in ipairs(duelWeapons) do
if wep.name == name then
weapon = wep
end
end
end
end
if not weapon then
local index = weights[math.random(#weights)]
weapon = duelWeapons[index]
end
table.insert(weapons, weapon)
end
end
if #weapons then
return weapons[math.random(#weapons)]
end
return duelWeapons[weights[math.random(#weights)]]
end
local function getContestants (plys)
local contestants = {}
for _, ply in pairs(plys) do
if ply.isReady then
table.insert(contestants, ply)
end
end
table.sort(contestants, sortCompare)
if (#contestants % 2) ~= 0 then
local bot = players.createBot()
if bot then
bot.gender = math.random(0, 1)
local identities = botProfiles[bot.gender]
local index = math.random(#identities)
bot.name = identities[index]
bot.head = index % 5
bot.hairColor = index % 8
bot.skinColor = index % 6
bot.hair = index % 9
bot.eyeColor = index % 8
bot.phoneNumber = 4201337
table.insert(contestants, math.random(#contestants + 1), bot)
end
end
return contestants
end
local function matchPlayers (plys)
local contestants = getContestants(plys)
local remaining = #contestants
local location = 0
if testingLocation > 0 then
location = testingLocation - 1
else
table.shuffle(duelLocations)
end
mode:print('Matching players...')
while remaining > 1 do
location = location + 1
if location > #duelLocations then
chat.announce('** Ran out of arenas somehow. Sorry!')
break
end
remaining = remaining - 2
local a = table.remove(contestants, 1)
local b = table.remove(contestants, 1)
if not a or not b then
chat.announce('** Failed to get a bot for some reason. Sorry!')
break
end
a.team = 0
b.team = 1
local duel = {
a = a,
b = b,
location = duelLocations[location],
finishTime = nil,
objects = {}
}
mode:print(string.format(' -> %s is facing %s at %s', a.name, b.name, duel.location.name))
table.insert(currentDuels, duel)
a.data.duel = duel
b.data.duel = duel
local shirtColor = math.random(1, 5)
local wep = chooseWeapon(a, b)
duelSpawn(a, duel.location.spawnA, shirtColor, wep, duel.objects)
duelSpawn(b, duel.location.spawnB, shirtColor, wep, duel.objects)
end
end
local lastReadyUpWarning = 0
local function logicPregame (plys)
for _, ply in pairs(plys) do
if ply.team ~= 17 then
ply.team = 17
if not hook.run('EventUpdatePlayer', ply) then
ply:update()
hook.run('PostEventUpdatePlayer', ply)
end
end
end
local now = os.realClock()
if now - lastReadyUpWarning > 4 then
lastReadyUpWarning = now
for _, ply in pairs(plys) do
if not ply.isReady then
ply:sendMessage('** Ready up if you want to play!')
end
end
end
if not isSomeoneReady(plys, true) then
server.time = 10 * server.TPS
return
end
if isSomeoneReady(plys, false) then
server.time = server.time - 1
if server.time > 0 then return end
end
server.state = STATE_GAME
server.time = 95 * server.TPS
matchPlayers(plys)
end
local function announceTopFive ()
local candidates = {}
for _, profile in pairs(profiles) do
table.insert(candidates, profile)
end
table.sort(candidates, function (a, b)
return a.elo > b.elo
end)
local strs = {}
for i = 1, 5 do
local cnd = candidates[i]
if cnd then
table.insert(strs, string.format('%s (%i)', cnd.name, cnd.elo))
end
end
chat.announceWrap('** Top 5 players: ' .. table.concat(strs, ', '))
end
local function getEloBoard (candidates)
local board = {
title = 'Top Elo',
headers = {
'Rank',
'Elo',
'Player'
},
rows = {}
}
table.sort(candidates, function (a, b)
return a[2].elo > b[2].elo
end)
for i = 1, 20 do
local cnd = candidates[i]
if not cnd then
break
end
table.insert(board.rows, {
tostring(i),
tostring(cnd[2].elo),
{
name = cnd[2].name,
phoneNumber = tonumber(cnd[1])
}
})
end
return board
end
local function getTimesPlayedBoard (candidates)
local board = {
title = 'Top Play Time',
headers = {
'Rank',
'Games Played',
'Player'
},
rows = {}
}
table.sort(candidates, function (a, b)
return (a[2].timesUpdated or 0) > (b[2].timesUpdated or 0)
end)
for i = 1, 20 do
local cnd = candidates[i]
if not cnd then
break
end
table.insert(board.rows, {
tostring(i),
tostring(cnd[2].timesUpdated or 0),
{
name = cnd[2].name,
phoneNumber = tonumber(cnd[1])
}
})
end
return board
end
function mode.hooks.BuildBoards (boards)
local candidates = {}
for phoneString, profile in pairs(profiles) do
table.insert(candidates, { phoneString, profile })
end
table.insert(boards, getEloBoard(candidates))
table.insert(boards, getTimesPlayedBoard(candidates))
end
local function outOfBoundsLogic (plys)
local t = server.time
for _, ply in ipairs(plys) do
local man = ply.human
if man and man.isAlive then
local pos = man.pos
local duel = ply.data.duel
if duel then
local loc = duel.location
if pos.x ~= 0 or pos.y ~= 0 or pos.z ~= 0 then
for i = 0, 15 do
local body = man:getRigidBody(i)
-- Nicely bounce people back inside their arenas
if not isVectorInCuboid(body.pos, loc.bounds[1], loc.bounds[2]) then
local v = 0.03
local minX, maxX = lowHigh(loc.bounds[1].x, loc.bounds[2].x)
if body.pos.x <= minX then man:addVelocity(Vector(v, 0, 0)) end
if body.pos.x >= maxX then man:addVelocity(Vector(-v, 0, 0)) end
local minY, maxY = lowHigh(loc.bounds[1].y, loc.bounds[2].y)
if body.pos.y <= minY then man:addVelocity(Vector(0, v, 0)) end
if body.pos.y >= maxY then man:addVelocity(Vector(0, -v, 0)) end
local minZ, maxZ = lowHigh(loc.bounds[1].z, loc.bounds[2].z)
if body.pos.z <= minZ then man:addVelocity(Vector(0, 0, v)) end
if body.pos.z >= maxZ then man:addVelocity(Vector(0, 0, -v)) end
if duel.finishTime then break end
local manData = man.data
if manData.duelsOOBTimer and t > manData.duelsOOBTimer then break end
manData.duelsOOBTimer = t - server.TPS
if manData.duelsTimesOOB == nil then
manData.duelsTimesOOB = 1
else
manData.duelsTimesOOB = manData.duelsTimesOOB + 1
end
if manData.duelsTimesOOB > 10 then
man.isAlive = false
break
end
local remaining = 10 - manData.duelsTimesOOB
man:speak(string.format('Out of bounds! %i warning%s left.', remaining, remaining == 1 and '' or 's'), 0)
mode:print(ply.name..' out of bounds at '..loc.name..' @ '..pos.x..' '..pos.y..' '..pos.z)
break
end
end
end
end
end
end
end
local function winDuel (winner, loser)
local winElo, loseElo = elo.calculate(getElo(winner), getElo(loser), 1, 0)
local winMan = winner.human
local loseMan = loser.human
if winMan then
events.createSound(38, winMan:getRigidBody(3).pos)
end
local reason = 'defeated'
if isActive(loser) and isActive(loseMan) then
if loseMan.isAlive then
reason = 'knocked out'
else
reason = 'killed'
end
end
local numOngoing = 0
for _, duel in pairs(currentDuels) do
if not duel.finishTime then
numOngoing = numOngoing + 1
end
end
if not winner.isBot and not loser.isBot then
setElo(winner, winElo)
setElo(loser, loseElo)
profilesDirty = true
chat.announceWrap(string.format('** %s (%i) %s %s (%i)! %i duel%s left.', winner.name, winElo, reason, loser.name, loseElo, numOngoing, numOngoing == 1 and '' or 's'))
else
chat.announceWrap(string.format('** %s %s %s! %i duel%s left.', winner.name, reason, loser.name, numOngoing, numOngoing == 1 and '' or 's'))
end
mode:print(string.format(' -x %s %s %s', winner.name, reason, loser.name))
end
local function whisperInfo (plys)
for _, ply in ipairs(plys) do
local man = ply.human
if man and not ply.isBot then
local duel = ply.data.duel
if duel then
local opponent
if duel.a == ply then
opponent = duel.b
else
opponent = duel.a
end
if not opponent.isBot then
man:speak(string.format("Facing %s (%i) at %s", opponent.name, getElo(opponent), duel.location.name), 0)
else
man:speak(string.format("Practicing against %s at %s", opponent.name, duel.location.name), 0)
end
end
end
end
end
local function logicGame (plys)
server.time = server.time - 1
if server.time == 94 * server.TPS then
playSoundAll(plys, 29, 1, 1)
end
if server.time == 93 * server.TPS then
playSoundAll(plys, 29, 1, 1)
end
if server.time == 92 * server.TPS then
playSoundAll(plys, 29, 1, 1)
end
if server.time == 91 * server.TPS then
playSoundAll(plys, 32, 1, 1.63710223425)
canShoot = true
end
if server.time == (95 * server.TPS) - 20 then
whisperInfo(plys)
end
outOfBoundsLogic(plys)
local numOngoing = 0
local now = os.realClock()
for i = #currentDuels, 1, -1 do
local duel = currentDuels[i]
if not duel.finishTime then
numOngoing = numOngoing + 1
local a = duel.a
local b = duel.b
local aMan = a.human
local bMan = b.human
if not isActive(a) or not aMan or not aMan.isAlive or aMan.bloodLevel < 20 then
duel.finishTime = now
winDuel(b, a)
elseif not isActive(b) or not bMan or not bMan.isAlive or bMan.bloodLevel < 20 then
duel.finishTime = now
winDuel(a, b)
end
elseif now - duel.finishTime > 2.5 then
table.remove(currentDuels, i)
-- Delete the duel's humans and items
for _, obj in pairs(duel.objects) do
if isActive(obj) then
obj:remove()
end
end
for _, ply in ipairs({ duel.a, duel.b }) do
if isActive(ply) then
ply:update()
end
end
end
end
if numOngoing == 0 and server.time > 2 * server.TPS then
server.time = 2 * server.TPS
end
if server.time < 1 then
mode:print('Game ended')
if numOngoing > 0 then
chat.announce '** Out of time!'
end
server.state = STATE_RESTARTING
server.time = 4 * server.TPS
return
end
end
local function logicRestarting ()
server.time = server.time - 1
if server.time == 3 * server.TPS then
announceTopFive()
end
if server.time == 2 * server.TPS and mode.config.advertisement ~= '' then
chat.announce('** ' .. mode.config.advertisement)
end
if server.time < 1 then
server:reset()
end
end
mode.hooks.LogicRound = function ()
local state = server.state
local plys = players.getAll()
if state == STATE_PREGAME then
logicPregame(plys)
elseif state == STATE_GAME then
logicGame(plys)
elseif state == STATE_RESTARTING then
logicRestarting()
else
server.state = STATE_RESTARTING
end
return hook.override
end
mode.commands['/gun'] = {
info = 'Select your preferred gun.',
usage = '<name>',
canCall = function (ply) return not ply.isConsole end,
call = function (ply, _, args)
assert(#args >= 1, 'usage')
local allowedNames = {}
local gunName = args[1]:lower()
local foundWeapon
for _, weapon in ipairs(duelWeapons) do
table.insert(allowedNames, weapon.name)
if weapon.name:lower() == gunName then
foundWeapon = weapon
end
end
if not foundWeapon then
error('Allowed guns: ' .. table.concat(allowedNames, ', '))
end
local profile = getProfile(ply)
assert(profile, 'No profile?')
profile.gun = foundWeapon.name
profilesDirty = true
ply:sendMessage('Preferred gun set to ' .. foundWeapon.name)
end
}