-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTABGPlayer.cs
More file actions
561 lines (449 loc) · 21.6 KB
/
TABGPlayer.cs
File metadata and controls
561 lines (449 loc) · 21.6 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
using Landfall.Network;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Epic.OnlineServices.AntiCheatCommon;
namespace CitrusLib
{
//do stuff to players
public static partial class Citrus
{
/// <summary>
/// teleports a living player. if the player is dead, they are respawned.
/// </summary>
/// <param name="player">The player to teleport</param>
/// <param name="pos">the position to teleport the player</param>
/// <param name="keepLoot">whether to retain the player's loot or remove it.</param>
public static void Teleport(TABGPlayerServer player, Vector3 pos, bool keepLoot = true)
{
Teleport(player, pos, player.PlayerRotation, keepLoot);
}
/// <summary>
/// teleports a living player. if the player is dead, they are respawned.
/// </summary>
/// <param name="player">The player to teleport</param>
/// <param name="pos">the position to teleport the player</param>
/// <param name="rot">the rotation to have the player face</param>
/// <param name="keepLoot">whether to retain the player's loot or remove it.</param>
public static void Teleport(TABGPlayerServer player, Vector3 pos, Vector2 rot, bool keepLoot = true)
{
byte[] array = new byte[(int)(24)];
using (MemoryStream memoryStream = new MemoryStream(array))
{
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
{
binaryWriter.Write((byte)1);
binaryWriter.Write(player.PlayerIndex);
binaryWriter.Write(player.PlayerIndex);
binaryWriter.Write(player.Health);
binaryWriter.Write(pos.x);
binaryWriter.Write(pos.y);
binaryWriter.Write(pos.z);
binaryWriter.Write((float)rot.x);
binaryWriter.Write(byte.MaxValue);
//RespawnEntityCommand.Run(World, player, pos, player.PlayerIndex);
}
}
LootPack lp = LootPack.CopyPlayerLoot(player.Loot);
if (player.IsDead)
{
Respawn(player, pos, false);
}
else
{
player.ClearLoot();
player.ClearEquipment();
World.SendMessageToClients(EventCode.PlayerRespawn, array, player.PlayerIndex, true, false);
}
if (keepLoot)
{
lp.GiveTo(player);
}
}
/// <summary>
/// respawns the player using the vanilla method. Also works as a means to teleport players, reviving them if they are dead.
///
/// use waituntill ready if the player JUST DIED and you want them to respawn as soon as reasonably possible (after the player slow-motion-effect wears off)
/// </summary>
/// <param name="player"></param>
/// <param name="pos"></param>
/// <param name="waitUntilReady"></param>
public static void Respawn(TABGPlayerServer player, Vector3 pos, bool waitUntilReady=false)
{
if (!waitUntilReady)
{
RespawnEntityCommand.Run(World, player, pos);
return;
}
WaitThen(8,delegate()
{
RespawnEntityCommand.Run(World, player, pos);
});
}
/// <summary>
/// kills a player. probably doesnt send them to the respawn minigame, and hopefully doesnt kick the player either
/// </summary>
/// <param name="player">The player to kill</param>
/// <param name="killer">Optional. allows a player to be awarded the kill</param>
public static void KillPlayer(TABGPlayerServer player, TABGPlayerServer killer = null)
{
if (player == null)
{
Citrus.log.LogError("Cant Find victim player that is dead");
return;
}
player.Kill();
player.TakeDamage(200f);
if (killer != null)
{
killer.AddKill();
byte groupIndex = killer.GroupIndex;
Citrus.World.GameRoomReference.CurrentGameKills.AddKillForTeam(groupIndex);
}
byte killid = killer == null ? byte.MaxValue : killer.PlayerIndex;
byte[] bytes = new UnicodeEncoding().GetBytes(player.PlayerName);
byte[] buffer = new byte[5 + bytes.Length + 4];
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
{
binaryWriter.Write(player.PlayerIndex);
binaryWriter.Write(killid);
binaryWriter.Write(byte.MaxValue);
binaryWriter.Write((byte)bytes.Length);
binaryWriter.Write(bytes);
if (killer != null)
{
binaryWriter.Write(killer.WeaponType);
}
else
{
binaryWriter.Write(int.MaxValue);
}
binaryWriter.Write(false);
}
}
Citrus.World.SendMessageToClients(EventCode.PlayerDead, buffer, byte.MaxValue, true, false);
}
/// <summary>
/// removes a player's loot and gives them items
/// </summary>
/// <param name="player">The player to affect</param>
/// <param name="items">an array of item ids. use the CitrusLib Item!</param>
/// <param name="quantities">an array of quantities for each item</param>
public static void SetLoot(TABGPlayerServer player, int[] items, int[] quantities)
{
SetLoot(player, items.ToList(), quantities.ToList());
}
/// <summary>
/// removes a player's loot and gives them items
/// </summary>
/// <param name="player">The player to affect</param>
/// <param name="items">a list of item ids. use the CitrusLib Item!</param>
/// <param name="quantities">a list of quantities for each item</param>
public static void SetLoot(TABGPlayerServer player, List<int> items, List<int> quantities)
{
LootPack lp = new LootPack();
for(int i =0; i<items.Count();i++)
{
lp.AddLoot(items[i], quantities[i]);
}
SetLoot(player, lp);
}
/// <summary>
/// Sets a players loot.
/// </summary>
/// <param name="player">The Player to effect</param>
/// <param name="loot">The LootPack to give to the player.</param>
public static void SetLoot(TABGPlayerServer player, LootPack loot)
{
//do something to remove the player's loot, such as teleport with no keeploot
Teleport(player, player.PlayerPosition, player.PlayerRotation, false);
loot.GiveTo(player);
}
/// <summary>
/// GIVES one stack of an item to a player
/// </summary>
/// <param name="player">The player to affect</param>
/// <param name="item">The item id. use the CitLib Item enumerator!</param>
/// <param name="quantity">Amount of the item to give</param>
public static void GiveLoot(TABGPlayerServer player, int item, int quantity = 1)
{
LootPack lp = new LootPack();
lp.AddLoot(item, quantity);
lp.GiveTo(player);
//GivePickUpCommand.Run(null, Citrus.World, player.PlayerIndex, weps, quants.ToArray());
}
/// <summary>
/// Gives a LootPack of items to a player
/// </summary>
/// <param name="player">the player to affect</param>
/// <param name="lp">The CitLib LootPack</param>
public static void GiveLoot(TABGPlayerServer player, LootPack lp)
{
lp.GiveTo(player);
}
/// <summary>
/// Kicks a player, because the vanilla kick doesnt work under certain conditions.
/// </summary>
/// <param name="player">The player to kick</param>
/// <param name="reason">a KickReason. the player sees this as a number when they are kicked.</param>
/// <param name="logReason">A message as to why they are kicked. only printed to the console</param>
public static void Kick(TABGPlayerServer player, KickReason reason = KickReason.Invalid, string logReason = "")
{
if (player == null)
{
Citrus.log.LogError("tried kicking null player?");
}
if (logReason != "")
{
Citrus.log.Log(string.Format("kicking player {0}, Ind:{1}, Epic:{2}, for reason: {3}", player.PlayerName, player.PlayerIndex, player.EpicUserName, logReason));
}
Citrus.World.SendMessageToClients(EventCode.KickPlayerMessage, new byte[] { (byte)Mathf.Clamp((int)reason+100,byte.MinValue, byte.MaxValue)}, player.PlayerIndex, true, false);
//world.HandlePlayerLeave(player);
//PlayerLeaveCommand.Run(world, player, true);
//PlayerLeaveCommand.RemovePlayer(player, gameRoomReference);
byte b = player.PlayerIndex;
//int id = ((UnityTransportServer)(world.Server)).
Citrus.World.SendMessageToClients(EventCode.PlayerLeft, new byte[]
{
player.PlayerIndex,
1
}, byte.MaxValue, true, false);
Citrus.World.GameRoomReference.RemovePlayer(player);
Citrus.World.GameRoomReference.CurrentGameMode.HandlePlayerLeave(player);
Citrus.World.GameRoomReference.CheckGameState();
Citrus.World.Server.DisconnectPlayer(b);
//this all works well enough so far but
Citrus.log.Log("kicklist enqueue " + b);
Citrus.kicklist.Enqueue(b);
Citrus.World.Server.Update();//maybe???????
//Network.Disconnect();
/*Citruslib.CitrusLib.World.WaitThenDoAction(2f, delegate
{
//world.SendMessageToClients(EventCode.KickPlayer, Array.Empty<byte>(), b, true, false);
world.Server.DisconnectPlayer(b);
});*/
}
//creates a login buffer with a new teamindex for changing teams. for carefully lying to clients...
//using geardatas allows altering a player's gear, relative to others only
static byte[] LoginData(TABGPlayerServer ply, byte newGroupIndex, int[] gearDatas=null)
{
byte[] bytes = Encoding.UTF8.GetBytes(ply.PlayerName);
byte[] buffer = new byte[15 + bytes.Length + 4 * ply.GearData.Length];
using (MemoryStream memoryStream2 = new MemoryStream(buffer))
{
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream2))
{
binaryWriter.Write(ply.PlayerIndex);
binaryWriter.Write(newGroupIndex);
binaryWriter.Write(bytes.Length);//name length
binaryWriter.Write(bytes);//name bytes
if (gearDatas != null)
{
binaryWriter.Write(gearDatas.Length); //gear length
for (int j = 0; j < gearDatas.Length; j++)
{
binaryWriter.Write(gearDatas[j]);//individual gear ints
}
}
else
{
binaryWriter.Write(ply.GearData.Length); //gear length
for (int j = 0; j < ply.GearData.Length; j++)
{
binaryWriter.Write(ply.GearData[j]);//individual gear ints
}
}
binaryWriter.Write(false); //i forgor but i dont think it matters
binaryWriter.Write((int)0); //player color. unused in battleroyale
}
}
return buffer;
}
static void RebuildStandings()
{
GameStats stats = Citrus.World.GameRoomReference.CurrentGameStats;
stats.Reset();
foreach (TABGPlayerServer player in Citrus.World.GameRoomReference.Players)
{
stats.AddPlayerToTeam(player.GroupIndex, player, true);
}
foreach (TeamStanding team in stats.GetAllTeams())
{
team.InitNumberOfLives(2147483647);
}
}
/// <summary>
/// tries to set a player to the sepecified team.
/// beacause a clients team cannot ever change, this uses some wildly convoluted "relative" team id switches of non-client peers to create the illusion of a team change.
/// On the server-side (here!), the players are eventually put on the same team.
/// the function, as is, sometimes doesnt work because of lag and stuff. Generally its a good idea NOT to spam this function.
/// </summary>
/// <param name="p"></param>
/// <param name="groupIndex"></param>
public static void SetTeam(TABGPlayerServer p, byte groupIndex)
{
PlayerRef player = Citrus.players.Find(pl => pl.player == p);
if (player == null)
{
Citrus.log.LogError("player ref not found...");
return;
}
PlayerTeam teamto = Citrus.teams.Find(t => t.groupIndex == groupIndex);
if (teamto == null)
{
Citrus.log.Log("team not found");
return;
}
SetTeam(player, teamto); //oh
}
/// <summary>
/// tries to set a player's team. this function uses Citlib PlayerRefs and PlayerTeam objects, which might not be easier for you to use than vanilla ids and bytes in the function above.
/// </summary>
/// <param name="player"></param>
/// <param name="team"></param>
public static void SetTeam(PlayerRef player, PlayerTeam team)
{
//players = players.Distinct().ToList();
foreach (PlayerRef pref in Citrus.players)
{
Citrus.log.Log(pref.player.PlayerIndex + " : " + pref.player.PlayerName);
}
if (team != null)
{
if (team.players.Contains(player))
{
Citrus.log.Log("already on team " + team.groupIndex + ", returning");
return; //already on team, shouldnt waste bandwidth
}
}
//remove existing alliances
PlayerTeam currentTeam = Citrus.teams.Find(t => t.players.Contains(player));
if (currentTeam != null)
{
currentTeam.players = currentTeam.players.Distinct().ToList();
currentTeam.players.Remove(player);
}
//add new* alliances
if (team != null)
{
team.players = team.players.Distinct().ToList();
team.players.Add(player);
player.player.UpdateGroupIndex(team.groupIndex); //hellish consequences
}
else
{
Citrus.log.Log("setting null team can have issues if the players original GI is a still-existing team");
player.player.UpdateGroupIndex(player.Get<byte>("originalTeam"));
}
//might not work....
foreach (PlayerRef pl in Citrus.players)
{
if (pl == player) continue;
SetAlly(player, pl, player.player.GroupIndex == pl.player.GroupIndex);
}
RebuildStandings();
}
//if onTeam is true, sets players to be allies with one another. otherwise, sets them to NOT be allies with one another...
static void SetAlly(PlayerRef player, PlayerRef ally, bool onTeam)
{
byte plIndex = player.player.PlayerIndex;
byte index = ally.player.PlayerIndex;
Citrus.log.Log(string.Format("Setting player {0} and {1} to be {2}!", plIndex, index, onTeam ? "friends" : "enemies"));
byte pgi = player.Get<byte>("originalTeam");
byte agi = ally.Get<byte>("originalTeam");
int t = onTeam ? 0 : 1;
//player leaves ally's game
Citrus.World.SendMessageToClients(EventCode.PlayerLeft, new byte[] { plIndex, (byte)1 }, index, true);
//ally leaves player's game
Citrus.World.SendMessageToClients(EventCode.PlayerLeft, new byte[] { index, (byte)1 }, plIndex, true);
//player joins ally's game with ally's GI or GI+1 if enemy
Citrus.World.SendMessageToClients(EventCode.Login, LoginData(player.player, (byte)((int)agi + t)), index, true);
//ally joins player's game with player's groupindex, or GI+1 if enemy
Citrus.World.SendMessageToClients(EventCode.Login, LoginData(ally.player, (byte)((int)pgi + t)), plIndex, true);
}
/// <summary>
/// makes the player throw a parrot at their feet with the desired message. Only the player throwing the parrot sees the message.
/// </summary>
/// <param name="p">The player to through the parrot</param>
/// <param name="message">the message</param>
public static void SelfParrot(TABGPlayerServer p, string message)
{
p.UpdateRotation(new Vector2(90, 90));
byte[] bytes = Encoding.Unicode.GetBytes(message);
byte[] array = new byte[2 + bytes.Length];
using (MemoryStream memoryStream = new MemoryStream(array))
{
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
{
binaryWriter.Write(p.PlayerIndex);
binaryWriter.Write((byte)bytes.Length);
binaryWriter.Write(bytes);
}
}
//might work
int num = array.Length - 1;
byte[] array2 = new byte[num];
Array.Copy(array, 1, array2, 0, num);
byte[] buffer = new byte[22 + array2.Length];
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
{
binaryWriter.Write(p.PlayerIndex);
binaryWriter.Write(p.PlayerPosition.x);
binaryWriter.Write(p.PlayerPosition.y);
binaryWriter.Write(p.PlayerPosition.z);
binaryWriter.Write(p.PlayerRotation.x);
binaryWriter.Write(p.PlayerRotation.y);
binaryWriter.Write(array2);
}
}
Citrus.World.SendMessageToClients(EventCode.ThrowChatMessage, buffer, p.PlayerIndex, true, false);
}
/// <summary>
/// Sets a player's gear. the client doesnt see the change, and should probably be alive during such a change.
/// </summary>
/// <param name="player"></param>
/// <param name="gearData"></param>
public static void SetGear(TABGPlayerServer player, int[] gearData)
{
//if i remember correctly...
int ind = player.PlayerIndex;
//player who's gear is changing leaves game for everyone else
Citrus.World.SendMessageToClients(EventCode.PlayerLeft, new byte[] { player.PlayerIndex, (byte)1 }, AllExcept(ind), true);
//it's possible a delay here is needed / extremely useful, but a long delay will hide the player for longer and possibly cause deadlier bugs
//player "joins back" with new gear. teeny tiny issue where the name over their head wont be there anymore... maybe i can fix that
Citrus.World.SendMessageToClients(EventCode.Login, LoginData(player, player.GroupIndex, gearData), AllExcept(ind), true);
}
/// <summary>
/// yknow how team members are meant to have their names over their heads?
/// changing teams and gear mid match causes issue with that.
/// run this code AFTER calling set team or set gear.
/// this is seperate from those function because you might be calling set team/gear many times and dont need redundant uses of this function.
/// it also only needs to be called on players who have had people *join* their team...
/// </summary>
public static void FixTeams()
{
//essentially i just need to change the gamestate to "waiting for players"
//and then set back to the current gamestate... which means that it cant return to the truck gamestate afaik
//and a couple other special states
//World.SendMessageToClients(EventCode.GameStateChanged, );
//SendGameStateChanged
}
/// <summary>
/// returns a list of all player IDs except the specified one. for use with sendmessage
/// </summary>
/// <param name="i">the index to ignore. doesnt nessesarily have to be a player who is actually connected to the server</param>
/// <returns></returns>
public static byte[] AllExcept(int i)
{
return players.Where((p) => p.player.PlayerIndex != i).ToList().ConvertAll<byte>((p) => p.player.PlayerIndex).ToArray();
}
}
}