Repository navigation
Expand file tree
/
Copy pathBotRandomizer.cs
More file actions
701 lines (620 loc) · 25.4 KB
/
Copy pathBotRandomizer.cs
File metadata and controls
701 lines (620 loc) · 25.4 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
using System.Runtime.InteropServices;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;
namespace BotRandomizer;
public sealed class BotRandomizerPlugin : BasePlugin
{
private readonly CosmeticStateStore _states = new();
private readonly HashSet<int> _pendingRerolls = [];
private CosmeticCatalog? _catalog;
private CosmeticRoller? _roller;
private CosmeticApplicator? _applicator;
private WeaponItemViewStore? _weaponItemViews;
private bool _giveNamedItemHooked;
private bool _giveNamedItemErrorLogged;
public override string ModuleName => "BotRandomizer";
public override string ModuleVersion => "1.3.5";
public override string ModuleAuthor => "ed0ard, Misaka17032, unicbm & XBribo";
public override string ModuleDescription =>
"Stable per-bot knives, gloves, weapon skins, stickers, charms, agents and music kits";
public override void Load(bool hotReload)
{
LoadCatalog();
LoadAttributeWriter();
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);
RegisterEventHandler<EventRoundPrestart>(OnRoundPrestart, HookMode.Pre);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
RegisterEventHandler<EventRoundMvp>(OnRoundMvp, HookMode.Pre);
RegisterEventHandler<EventPlayerTeam>(OnPlayerTeam);
RegisterEventHandler<EventItemPickup>(OnItemPickup);
if (_weaponItemViews?.NativeAvailable == true)
{
VirtualFunctions.GiveNamedItemFunc.Hook(OnGiveNamedItemPre, HookMode.Pre);
_giveNamedItemHooked = true;
}
if (hotReload)
RestoreAllBots(CosmeticScope.All);
}
public override void Unload(bool hotReload)
{
if (_giveNamedItemHooked)
{
VirtualFunctions.GiveNamedItemFunc.Unhook(OnGiveNamedItemPre, HookMode.Pre);
_giveNamedItemHooked = false;
}
_weaponItemViews?.Dispose();
_weaponItemViews = null;
}
private void LoadCatalog()
{
try
{
var catalogPath = Path.Combine(ModuleDirectory, "cosmetic_catalog.json");
var placementPath = Path.Combine(ModuleDirectory, "charm_placements.json");
_catalog = CosmeticCatalog.Load(catalogPath);
var charmPlacements = CharmPlacementCatalog.Load(placementPath, _catalog);
_roller = new CosmeticRoller(_catalog, charmPlacements);
Logger.LogInformation(
"[BotRandomizer] Catalog loaded: {Weapons} weapons, {Paints} weapon paints, {Stickers} stickers, {Charms} charms; {CharmPositions} charm positions for {CharmWeapons} weapons",
_catalog.WeaponCount,
_catalog.WeaponPaintCount,
_catalog.StickerKits.Count,
_catalog.KeychainDefinitions.Count,
charmPlacements.PlacementCount,
charmPlacements.WeaponCount);
}
catch (Exception exception)
{
_catalog = null;
_roller = null;
Logger.LogError(
exception,
"[BotRandomizer] cosmetic_catalog.json or charm_placements.json is invalid; randomization disabled");
}
}
private void LoadAttributeWriter()
{
MemoryFunctionWithReturn<nint, string, float, int>? writer = null;
try
{
// SetOrAddAttributeValueByName
writer = new MemoryFunctionWithReturn<nint, string, float, int>(
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? "55 48 89 E5 41 57 41 56 41 55 49 89 FD 41 54 53 48 89 F3 48 83 EC ? F3 0F 11 85"
: "48 89 4C 24 ? 53 41 55 41 56");
}
catch (Exception exception)
{
Logger.LogError(
exception,
"[BotRandomizer] SetOrAddAttributeValueByName signature failed; economic cosmetics disabled");
}
_applicator = new CosmeticApplicator(writer, Logger);
MemoryFunctionWithReturn<nint, nint>? itemViewConstructor = null;
Action<nint>? itemViewDestructor = null;
if (writer is not null)
{
try
{
if (RuntimeInformation.ProcessArchitecture != Architecture.X64)
throw new PlatformNotSupportedException("CEconItemView destruction requires x64");
if (OperatingSystem.IsWindows())
{
// MSVC scalar deleting destructor: flags=0 destroys members without
// deleting the outer AllocHGlobal allocation.
var destructor = new MemoryFunctionWithReturn<nint, uint, nint>(
"48 89 5C 24 08 48 89 6C 24 10 48 89 74 24 18 48 89 7C 24 20 41 56 48 83 EC 20 48 8D 05 ? ? ? ? 8B EA 48 89 01 48 8B D9 83 79 78 00");
itemViewDestructor = handle => destructor.Invoke(handle, 0);
}
else if (OperatingSystem.IsLinux())
{
// Itanium ABI complete-object destructor (D1): void(this), no flags.
// Do not bind D0, which also calls operator delete on the outer storage.
var destructor = new MemoryFunctionVoid<nint>(
"55 48 8D 05 ? ? ? ? 48 89 E5 41 57 41 56 41 55 41 54 53 48 89 FB 48 83 EC ? 8B 57 78 48 89 07 85 D2");
itemViewDestructor = handle => destructor.Invoke(handle);
}
else
{
throw new PlatformNotSupportedException("CEconItemView destruction requires Windows or Linux");
}
// CEconItemViewConstructor
itemViewConstructor = new MemoryFunctionWithReturn<nint, nint>(
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? "55 48 8D 05 ? ? ? ? 66 0F EF C0 48 89 E5 41 57 41 56 4C 8D 35 ? ? ? ? 41 55 41 54 45 31 E4"
: "48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 57 41 54 41 55 41 56 41 57 48 83 EC ? 48 8B F9 48 8D 05");
}
catch (Exception exception)
{
itemViewConstructor = null;
itemViewDestructor = null;
Logger.LogError(
exception,
"[BotRandomizer] CEconItemView lifetime signatures failed; weapon cosmetics disabled");
}
}
_weaponItemViews = new WeaponItemViewStore(itemViewConstructor, itemViewDestructor, writer, Logger);
}
private void OnMapStart(string mapName)
{
_states.Reset();
_pendingRerolls.Clear();
_roller?.ResetMap();
// Keep constructed item-view storage alive across map transitions. Each view is
// fully overwritten before reuse and is freed only at slot teardown or unload.
_applicator?.Reset();
foreach (var agent in RandomizerAssets.CounterTerroristAgents)
Server.PrecacheModel(agent.ModelPath);
foreach (var agent in RandomizerAssets.TerroristAgents)
Server.PrecacheModel(agent.ModelPath);
}
private void OnClientDisconnect(int playerSlot)
{
_states.Remove(playerSlot);
_pendingRerolls.Remove(playerSlot);
_weaponItemViews?.ClearSlot(playerSlot);
_applicator?.ClearSlot(playerSlot);
}
private HookResult OnRoundPrestart(EventRoundPrestart @event, GameEventInfo info)
{
foreach (var player in Utilities.GetPlayers())
ConsumePendingReroll(player);
// Deferred one frame so this round's intro entities already exist. Rerolls are consumed
// first, so the intro publishes the loadout the bot will actually spawn with.
Server.NextFrame(ApplyIntroAgents);
return HookResult.Continue;
}
// Updates both teams only while the game is preparing the team intro
private void ApplyIntroAgents()
{
var gameRules = Utilities
.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules")
.FirstOrDefault()
?.GameRules;
if (gameRules is null || !gameRules.TeamIntroPeriod)
return;
ApplyIntroAgentsForTeam(
"team_intro_counterterrorist",
RandomizerAssets.CounterTerroristTeam,
gameRules.CTTeamIntroVariant);
ApplyIntroAgentsForTeam(
"team_intro_terrorist",
RandomizerAssets.TerroristTeam,
gameRules.TTeamIntroVariant);
}
// Matches bot controllers to active intro positions and publishes their agent items
private void ApplyIntroAgentsForTeam(string designerName, byte team, int introVariant)
{
var teamPlayers = Utilities.GetPlayers()
.Where(player => player.IsValid && player.TeamNum == team)
.OrderBy(player => player.Slot)
.ToList();
var bots = teamPlayers
.Where(player => player is { IsBot: true, IsHLTV: false })
.ToList();
if (bots.Count == 0)
return;
var previews = Utilities
.FindAllEntitiesByDesignerName<CCSGO_TeamPreviewCharacterPosition>(designerName)
.Where(preview => preview.IsValid && preview.Variant == introVariant)
.OrderBy(preview => preview.Ordinal)
.Take(teamPlayers.Count)
.ToList();
var unmatchedBots = new List<CCSPlayerController>(bots);
var unmatchedPreviews = new List<CCSGO_TeamPreviewCharacterPosition>(previews);
foreach (var bot in bots.Where(bot => bot.SteamID != 0))
{
var preview = unmatchedPreviews.FirstOrDefault(candidate => candidate.Xuid == bot.SteamID);
if (preview is null)
continue;
ApplyIntroAgent(preview, bot);
unmatchedBots.Remove(bot);
unmatchedPreviews.Remove(preview);
}
var botPreviews = unmatchedPreviews
.Where(preview => preview.Xuid == 0)
.OrderBy(preview => preview.Ordinal)
.ToList();
var pairCount = Math.Min(unmatchedBots.Count, botPreviews.Count);
for (var i = 0; i < pairCount; i++)
ApplyIntroAgent(botPreviews[i], unmatchedBots[i]);
}
private void ApplyIntroAgent(
CCSGO_TeamPreviewCharacterPosition preview,
CCSPlayerController bot)
{
var state = GetOrCreateState(bot);
if (state is null)
return;
// Publishes the very agent this bot's pawn is given at spawn, so the intro and the
// in-game model always match.
preview.AgentItem.ItemDefinitionIndex = state.Loadout.AgentDefIndex;
Utilities.SetStateChanged(
preview,
"CCSGO_TeamPreviewCharacterPosition",
"m_agentItem");
}
private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
ConsumePendingReroll(@event.Userid);
var state = GetOrCreateState(@event.Userid);
if (state is null)
return HookResult.Continue;
var slot = state.Slot;
var userId = state.UserId;
var generation = state.Generation;
Server.NextFrame(() =>
{
if (!TryResolveCurrentBot(slot, userId, generation, out var player, out var pawn, out var current))
return;
ApplyIdentity(player, pawn, current, CosmeticScope.Agent | CosmeticScope.MusicKit);
ApplyWearables(player, pawn, current);
ScheduleWearableRetry(slot, userId, generation, 0.10f);
ScheduleWearableRetry(slot, userId, generation, 0.25f);
});
return HookResult.Continue;
}
private HookResult OnGiveNamedItemPre(DynamicHook hook)
{
if (_catalog is null
|| _roller is null
|| _weaponItemViews is null)
{
return HookResult.Continue;
}
try
{
var itemServices = hook.GetParam<CCSPlayer_ItemServices>(0);
var designerName = hook.GetParam<string>(1);
if (string.IsNullOrWhiteSpace(designerName))
return HookResult.Continue;
var player = GetPlayerFromItemServices(itemServices);
if (player is null)
return HookResult.Continue;
var state = GetOrCreateState(player);
if (state is null || !_catalog.TryGetWeapon(designerName, out var weapon))
{
return HookResult.Continue;
}
var selection = _roller.GetOrCreateWeapon(state.Loadout, weapon.DefIndex);
if (selection is not null
&& _weaponItemViews.TryPrepare(
state,
weapon,
selection,
includeStickers: true,
includeCharms: true,
player.SteamID,
out var itemViewHandle))
{
hook.SetParam(3, itemViewHandle);
}
}
catch (Exception exception)
{
if (!_giveNamedItemErrorLogged)
{
_giveNamedItemErrorLogged = true;
Logger.LogError(exception, "[BotRandomizer] GiveNamedItem pre-hook failed");
}
}
return HookResult.Continue;
}
private HookResult OnItemPickup(EventItemPickup @event, GameEventInfo info)
{
if (_applicator is null)
return HookResult.Continue;
if (string.IsNullOrEmpty(@event.Item)
|| (!@event.Item.Contains("knife", StringComparison.Ordinal)
&& !@event.Item.Contains("bayonet", StringComparison.Ordinal)))
{
return HookResult.Continue;
}
var state = GetOrCreateState(@event.Userid);
if (state is null)
return HookResult.Continue;
ScheduleKnifeSync(state.Slot, state.UserId, state.Generation, nextFrame: true);
ScheduleKnifeSync(state.Slot, state.UserId, state.Generation, delay: 0.10f);
ScheduleKnifeSync(state.Slot, state.UserId, state.Generation, delay: 0.25f);
return HookResult.Continue;
}
private HookResult OnPlayerTeam(EventPlayerTeam @event, GameEventInfo info)
{
var player = @event.Userid;
if (player is not { IsValid: true, IsBot: true } || player.UserId is not int userId)
return HookResult.Continue;
if (@event.Disconnect)
{
OnClientDisconnect(player.Slot);
return HookResult.Continue;
}
if (_roller is null || !IsPlayableTeam(@event.Team))
{
_states.BumpGeneration(player.Slot);
return HookResult.Continue;
}
_states.Reroll(
player.Slot,
userId,
(byte)@event.Team,
preserveMusic: true,
music => _roller.RollLoadout((byte)@event.Team, music));
var slot = player.Slot;
AddTimer(
0.10f,
() => RestoreBot(
slot,
CosmeticScope.Agent | CosmeticScope.Knife | CosmeticScope.Gloves),
TimerFlags.STOP_ON_MAPCHANGE);
return HookResult.Continue;
}
private HookResult OnRoundMvp(EventRoundMvp @event, GameEventInfo info)
{
var state = GetOrCreateState(@event.Userid);
var player = @event.Userid;
if (state is null || player is null)
{
return HookResult.Continue;
}
ApplyMusicKit(player, state.Loadout.MusicKit, 0);
@event.Musickitid = state.Loadout.MusicKit;
@event.Musickitmvps = 0;
@event.Nomusic = 0;
return HookResult.Continue;
}
private SlotCosmeticState? GetOrCreateState(CCSPlayerController? player)
{
if (_roller is null
|| player is not { IsValid: true, IsBot: true, IsHLTV: false }
|| player.UserId is not int userId
|| !IsPlayableTeam(player.TeamNum))
{
return null;
}
var team = (byte)player.TeamNum;
return _states.GetOrCreate(
player.Slot,
userId,
team,
music => _roller.RollLoadout(team, music));
}
private void ApplyIdentity(
CCSPlayerController player,
CCSPlayerPawn pawn,
SlotCosmeticState state,
CosmeticScope scope)
{
if (_applicator is null)
return;
if ((scope & CosmeticScope.Agent) != 0)
{
_applicator.ApplyAgent(pawn, state.Loadout.AgentModel);
}
if ((scope & CosmeticScope.MusicKit) != 0)
{
ApplyMusicKit(player, state.Loadout.MusicKit, 0);
}
}
private void ApplyWearables(
CCSPlayerController player,
CCSPlayerPawn pawn,
SlotCosmeticState state,
CosmeticScope scope = CosmeticScope.Knife | CosmeticScope.Gloves)
{
if (_applicator is null
|| player is not { IsValid: true, IsBot: true }
|| !pawn.IsValid)
{
return;
}
if ((scope & CosmeticScope.Knife) != 0)
{
_applicator.ApplyKnife(player, pawn, state.Loadout.Knife);
}
if ((scope & CosmeticScope.Gloves) != 0)
{
if (_applicator.ApplyGloves(player, pawn, state.Loadout.Glove))
{
var generation = state.Generation;
var pawnHandle = pawn.Handle;
AddTimer(0.2f, () =>
{
if (TryResolveCurrentBot(
state.Slot,
state.UserId,
generation,
out _,
out var currentPawn,
out _)
&& currentPawn.Handle == pawnHandle)
{
_applicator.ShowGloves(currentPawn);
}
}, TimerFlags.STOP_ON_MAPCHANGE);
}
}
}
private void ScheduleWearableRetry(
int slot,
int userId,
long generation,
float delay,
CosmeticScope scope = CosmeticScope.Knife | CosmeticScope.Gloves)
{
AddTimer(delay, () =>
{
if (TryResolveCurrentBot(slot, userId, generation, out var player, out var pawn, out var state))
ApplyWearables(player, pawn, state, scope);
}, TimerFlags.STOP_ON_MAPCHANGE);
}
private void ScheduleKnifeSync(
int slot,
int userId,
long generation,
float? delay = null,
bool nextFrame = false)
{
void Callback()
{
if (_applicator is not null
&& TryResolveCurrentBot(slot, userId, generation, out _, out var pawn, out _))
{
_applicator.SyncPickedUpKnife(pawn);
}
}
if (nextFrame)
Server.NextFrame(Callback);
else if (delay is float seconds)
AddTimer(seconds, Callback, TimerFlags.STOP_ON_MAPCHANGE);
}
private bool TryResolveCurrentBot(
int slot,
int userId,
long generation,
out CCSPlayerController player,
out CCSPlayerPawn pawn,
out SlotCosmeticState state)
{
player = null!;
pawn = null!;
state = null!;
if (!_states.IsCurrent(slot, userId, generation))
return false;
var resolved = Utilities.GetPlayerFromSlot(slot);
if (resolved is not { IsValid: true, IsBot: true, IsHLTV: false }
|| resolved.UserId != userId
|| resolved.PlayerPawn?.Value is not { IsValid: true } resolvedPawn
|| !_states.TryGet(slot, out var resolvedState))
{
return false;
}
player = resolved;
pawn = resolvedPawn;
state = resolvedState;
return true;
}
private void RestoreBot(int slot, CosmeticScope scope)
{
var player = Utilities.GetPlayerFromSlot(slot);
if (_roller is null
|| player is not { IsValid: true, IsBot: true, IsHLTV: false }
|| player.UserId is not int userId
|| !IsPlayableTeam(player.TeamNum))
{
return;
}
var state = GetOrCreateState(player);
if (state is null)
return;
var generation = state.Generation;
Server.NextFrame(() =>
{
if (!TryResolveCurrentBot(slot, userId, generation, out var currentPlayer, out var pawn, out var current))
return;
ApplyIdentity(currentPlayer, pawn, current, scope);
var wearableScope = scope & (CosmeticScope.Knife | CosmeticScope.Gloves);
if (wearableScope != CosmeticScope.None)
{
ApplyWearables(currentPlayer, pawn, current, wearableScope);
ScheduleWearableRetry(slot, userId, generation, 0.10f, wearableScope);
ScheduleWearableRetry(slot, userId, generation, 0.25f, wearableScope);
}
});
}
private void RestoreAllBots(CosmeticScope scope)
{
foreach (var player in Utilities.GetPlayers())
{
if (player is { IsValid: true, IsBot: true, IsHLTV: false })
RestoreBot(player.Slot, scope);
}
}
[ConsoleCommand("br_reroll", "Queue new loadouts for the next safe spawn")]
public void OnRerollCommand(CCSPlayerController? player, CommandInfo command)
{
if (_roller is null)
{
command.ReplyToCommand("Cosmetic catalog is unavailable.");
return;
}
var target = command.ArgCount >= 2 ? command.GetArg(1) : "all";
int? targetSlot = null;
if (target != "all")
{
if (!int.TryParse(target, out var slot))
{
command.ReplyToCommand("Usage: br_reroll [all|bot slot]");
return;
}
targetSlot = slot;
}
var bots = Utilities.GetPlayers()
.Where(bot =>
bot is { IsValid: true, IsBot: true, IsHLTV: false }
&& bot.UserId is not null
&& IsPlayableTeam(bot.TeamNum)
&& (targetSlot is null || bot.Slot == targetSlot.Value))
.ToArray();
if (bots.Length == 0)
{
command.ReplyToCommand("No matching bot slots.");
return;
}
foreach (var bot in bots)
_pendingRerolls.Add(bot.Slot);
command.ReplyToCommand(
$"Queued {bots.Length} bot loadout(s) for the next safe spawn.");
}
private void ConsumePendingReroll(CCSPlayerController? player)
{
if (_roller is null
|| player is not { IsValid: true, IsBot: true, IsHLTV: false }
|| player.UserId is not int userId
|| !IsPlayableTeam(player.TeamNum)
|| !_pendingRerolls.Remove(player.Slot))
{
return;
}
var team = (byte)player.TeamNum;
_states.Reroll(
player.Slot,
userId,
team,
preserveMusic: false,
music => _roller.RollLoadout(team, music));
}
private static bool IsPlayableTeam(int team)
=> team is RandomizerAssets.TerroristTeam or RandomizerAssets.CounterTerroristTeam;
private static CCSPlayerController? GetPlayerFromItemServices(CCSPlayer_ItemServices itemServices)
{
var pawn = itemServices.Pawn.Value;
if (pawn is not { IsValid: true } || pawn.Controller.Value is not { IsValid: true } controller)
return null;
var player = new CCSPlayerController(controller.Handle);
return player is { IsValid: true, IsBot: true, IsHLTV: false } ? player : null;
}
private static void ApplyMusicKit(CCSPlayerController player, int kitId, int musicKitMvps)
{
var inventory = player.InventoryServices;
if (inventory is not null)
{
inventory.MusicID = checked((ushort)kitId);
Utilities.SetStateChanged(player, "CCSPlayerController", "m_pInventoryServices");
}
player.MusicKitID = kitId;
Utilities.SetStateChanged(player, "CCSPlayerController", "m_iMusicKitID");
player.MusicKitMVPs = musicKitMvps;
Utilities.SetStateChanged(player, "CCSPlayerController", "m_iMusicKitMVPs");
player.MvpNoMusic = false;
Utilities.SetStateChanged(player, "CCSPlayerController", "m_bMvpNoMusic");
}
}