-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTwoPlayerMod.cs
612 lines (518 loc) · 20.2 KB
/
TwoPlayerMod.cs
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
using GTA;
using GTA.Math;
using GTA.Native;
using SharpDX.DirectInput;
using System;
using NativeUI;
using System.Windows.Forms;
using System.Collections.Generic;
using Benjamin94.Input;
using Benjamin94;
using SharpDX.XInput;
using System.Linq;
/// <summary>
/// The main Script, this will handle all logic
/// </summary>
class TwoPlayerMod : Script
{
// for ini keys
public static string ScriptName = "TwoPlayerMod";
private const string ToggleMenuKey = "ToggleMenuKey";
private const string CharacterHashKey = "CharacterHash";
private const string ControllerKey = "Controller";
private const string CustomCameraKey = "CustomCamera";
private const string BlipSpriteKey = "BlipSprite";
private const string BlipColorKey = "BlipColor";
private const string EnabledKey = "Enabled";
// Player 1
private Player player;
public static Ped player1;
// PlayerPeds
public static List<PlayerPed> playerPeds = new List<PlayerPed>();
// Menu
private UIMenu menu;
private MenuPool menuPool = new MenuPool();
// Settings
private Keys toggleMenuKey = Keys.F11;
// camera
public static bool customCamera = false;
private Camera camera;
// players
private readonly UserIndex[] userIndices = new UserIndex[] { UserIndex.Two, UserIndex.Three, UserIndex.Four };
public TwoPlayerMod()
{
ScriptName = Name;
player = Game.Player;
player1 = player.Character;
player1.Task.ClearAll();
LoadSettings();
SetupMenu();
KeyDown += TwoPlayerMod_KeyDown;
Tick += TwoPlayerMod_Tick;
}
/// <summary>
/// Loads the mod settings, if there is an error it will revert the file back to default
/// </summary>
private void LoadSettings()
{
try
{
toggleMenuKey = (Keys)new KeysConverter().ConvertFromString(Settings.GetValue(Name, ToggleMenuKey, Keys.F11.ToString()));
}
catch (Exception)
{
toggleMenuKey = Keys.F11;
UI.Notify("Failed to read '" + ToggleMenuKey + "', reverting to default " + ToggleMenuKey + " F11");
Settings.SetValue(Name, ToggleMenuKey, Keys.F11.ToString());
Settings.Save();
}
try
{
customCamera = bool.Parse(Settings.GetValue(Name, CustomCameraKey, "False"));
}
catch (Exception)
{
customCamera = false;
UI.Notify("Failed to read '" + CustomCameraKey + "', reverting to default " + CustomCameraKey + " False");
Settings.SetValue(Name, CustomCameraKey, "False");
Settings.Save();
}
}
/// <summary>
/// Determines if the mod is enabled or disabled
/// </summary>
/// <returns>true or false whether the mod is enabled</returns>
private bool Enabled()
{
return playerPeds.Count > 0;
}
/// <summary>
/// Sets up the NativeUI menu
/// </summary>
private void SetupMenu()
{
if (menuPool != null)
{
menuPool.ToList().ForEach(menu => { menu.Clear(); });
}
menu = new UIMenu("Two Player Mod", Enabled() ? "~g~Enabled" : "~r~Disabled");
menuPool.Add(menu);
UIMenuItem toggleItem = new UIMenuItem("Toggle mod", "Toggle Two Player mode");
toggleItem.Activated += ToggleMod_Activated;
menu.AddItem(toggleItem);
UIMenu allPlayersMenu = menuPool.AddSubMenu(menu, "Players");
menu.MenuItems.FirstOrDefault(item => { return item.Text.Equals("Players"); }).Description = "Configure players";
foreach (UserIndex player in userIndices)
{
bool check = bool.Parse(PlayerSettings.GetValue(player, EnabledKey, false.ToString()));
UIMenu playerMenu = menuPool.AddSubMenu(allPlayersMenu, "Player " + player);
UIMenuItem playerItem = allPlayersMenu.MenuItems.FirstOrDefault(item => { return item.Text.Equals("Player " + player); });
string controllerGuid = PlayerSettings.GetValue(player, ControllerKey, "");
playerItem.Description = "Configure player " + player;
if (!string.IsNullOrEmpty(controllerGuid))
{
playerItem.SetRightBadge(UIMenuItem.BadgeStyle.Star);
}
UIMenuCheckboxItem togglePlayerItem = new UIMenuCheckboxItem("Toggle player " + player, check, "Enables/disables this player");
togglePlayerItem.CheckboxEvent += (s, enabled) =>
{
PlayerSettings.SetValue(player, EnabledKey, enabled.ToString());
RefreshSubItems(togglePlayerItem, playerMenu, enabled);
};
playerMenu.AddItem(togglePlayerItem);
playerMenu.AddItem(ConstructSettingsListItem(player, "Character", "Select a character for player " + player, CharacterHashKey, PedHash.Trevor));
playerMenu.AddItem(ConstructSettingsListItem(player, "Blip sprite", "Select a blip sprite for player " + player, BlipSpriteKey, BlipSprite.Standard));
playerMenu.AddItem(ConstructSettingsListItem(player, "Blip color", "Select a blip color for player " + player, BlipColorKey, BlipColor.Green));
UIMenu controllerMenu = menuPool.AddSubMenu(playerMenu, "Assign controller");
playerMenu.MenuItems.FirstOrDefault(item => { return item.Text.Equals("Assign controller"); }).Description = "Assign controller to player " + player;
foreach (InputManager manager in InputManager.GetAvailableInputManagers())
{
UIMenuItem controllerItem = new UIMenuItem(manager.DeviceName, "Assign this controller to player " + player);
string guid = manager.DeviceGuid;
if (PlayerSettings.GetValue(player, ControllerKey, "").Equals(guid))
{
controllerItem.SetRightBadge(UIMenuItem.BadgeStyle.Star);
}
if (manager is DirectInputManager)
{
DirectInputManager directManager = (DirectInputManager)manager;
bool configured = DirectInputManager.IsConfigured(directManager.device, GetIniFile());
controllerItem.Enabled = configured;
if (!configured)
{
controllerItem.Description = "Please configure this controller first from the main menu";
}
}
controllerItem.Activated += (s, i) =>
{
if (i.Enabled)
{
PlayerSettings.SetValue(player, ControllerKey, guid);
controllerMenu.MenuItems.ForEach(item =>
{
if (item == controllerItem)
{
item.SetRightBadge(UIMenuItem.BadgeStyle.Star);
}
else
{
item.SetRightBadge(UIMenuItem.BadgeStyle.None);
}
});
}
};
controllerMenu.AddItem(controllerItem);
}
RefreshSubItems(togglePlayerItem, playerMenu, check);
}
UIMenuCheckboxItem cameraItem = new UIMenuCheckboxItem("GTA:SA style camera", customCamera, "Enables/disables the GTA:SA style camera");
cameraItem.CheckboxEvent += (s, i) =>
{
customCamera = !customCamera;
Settings.SetValue(Name, CustomCameraKey, customCamera.ToString());
Settings.Save();
};
menu.AddItem(cameraItem);
UIMenu controllersMenu = menuPool.AddSubMenu(menu, "Configure controllers");
menu.MenuItems.FirstOrDefault(item => { return item.Text.Equals("Configure controllers"); }).Description = "Configure controllers before assigning them to players";
foreach (Joystick stick in DirectInputManager.GetDevices())
{
UIMenuItem stickItem = new UIMenuItem(stick.Information.ProductName, "Configure " + stick.Information.ProductName);
controllersMenu.AddItem(stickItem);
stickItem.Activated += (s, i) =>
{
ControllerWizard wizard = new ControllerWizard(stick);
bool success = wizard.StartConfiguration(GetIniFile());
if (success)
{
UI.Notify("Controller successfully configured, you can now assign this controller");
}
else
{
UI.Notify("Controller configuration canceled, please configure your controller before playing");
}
SetupMenu();
};
}
menu.RefreshIndex();
}
/// <summary>
/// Refreshes all items in the UIMenu except the parentItem
/// </summary>
/// <param name="parentItem">The parent UIMenuItem to ignore</param>
/// <param name="menu">UIMenu</param>
/// <param name="enabled">Whether to enable or disable the sub items</param>
private void RefreshSubItems(UIMenuItem parentItem, UIMenu menu, bool enabled)
{
foreach (UIMenuItem item in menu.MenuItems)
{
if (!item.Equals(parentItem))
{
item.Enabled = enabled;
}
}
}
/// <summary>
/// Constructs a new UIMenuListItem with automatic handling of selected value
/// </summary>
/// <typeparam name="TEnum">The type of the enum</typeparam>
/// <param name="player">The </param>
/// <param name="text">The menu item text</param>
/// <param name="description">The menu item description</param>
/// <param name="settingsKey">The settings key</param>
/// <param name="defaultValue">The initial selected value</param>
/// <returns>A fully working UIMenuListItem with automatic saving</returns>
private UIMenuListItem ConstructSettingsListItem<TEnum>(UserIndex player, string text, string description, string settingsKey, TEnum defaultValue)
{
List<dynamic> items = GetDynamicEnumList<TEnum>();
TEnum value = PlayerSettings.GetEnumValue<TEnum>(player, settingsKey, defaultValue.ToString());
UIMenuListItem menuItem = new UIMenuListItem(text, items, items.IndexOf(value.ToString()), description);
menuItem.OnListChanged += (s, i) =>
{
if (s.Enabled)
{
TEnum selectedItem = Enum.Parse(typeof(TEnum), s.IndexToItem(s.Index));
PlayerSettings.SetValue(player, settingsKey, selectedItem.ToString());
}
};
return menuItem;
}
/// <summary>
/// This method will handle entering vehicles
/// </summary>
/// <param name="ped">Target Ped</param>
public static void HandleEnterVehicle(Ped ped)
{
Vehicle v = World.GetClosestVehicle(ped.Position, 15);
if (v == null)
{
return;
}
Ped driver = v.GetPedOnSeat(VehicleSeat.Driver);
if (driver != null && driver.IsPlayerPed())
{
ped.Task.EnterVehicle(v, VehicleSeat.Any);
}
else
{
if (driver != null)
{
driver.Delete();
}
ped.Task.EnterVehicle(v, VehicleSeat.Any);
}
}
/// <summary>
/// Helper method to get all values of an Enum for displaying in a menu
/// </summary>
/// <param name="type">Type of the Enum</param>
/// <returns>Sorted list of all possible given Enum values</returns>
private List<dynamic> GetDynamicEnumList<TEnum>()
{
List<dynamic> values = new List<dynamic>();
foreach (Enum pedHash in Enum.GetValues(typeof(TEnum)))
{
values.Add(pedHash.ToString());
}
values.Sort();
return values;
}
/// <summary>
/// Helper method to get the INI file
/// </summary>
/// <returns>"scripts//" + Name + ".ini"</returns>
private string GetIniFile()
{
return "scripts//" + Name + ".ini";
}
/// <summary>
/// Gets called by NativeUI when the user selects "Toggle mod" in the menu
/// </summary>
private void ToggleMod_Activated(UIMenu sender, UIMenuItem selectedItem)
{
if (!Enabled())
{
UI.ShowSubtitle("Like this mod? Please consider donating!", 10000);
SetupPlayerPeds();
if (playerPeds.Count == 0)
{
UI.Notify("Please assign a controller to at least one player");
UI.Notify("Also make sure you have configured at least one controller");
return;
}
SetupCamera();
}
else
{
Clean();
}
menu.Subtitle.Caption = Enabled() ? "~g~Enabled" : "~r~Disabled";
}
/// <summary>
/// Sets up all correctly configured PlayerPed
/// </summary>
private void SetupPlayerPeds()
{
foreach (UserIndex player in userIndices)
{
if (bool.Parse(PlayerSettings.GetValue(player, EnabledKey, false.ToString())))
{
string guid = PlayerSettings.GetValue(player, ControllerKey, "");
foreach (InputManager input in InputManager.GetAvailableInputManagers())
{
if (input.DeviceGuid.Equals(guid))
{
InputManager manager = input;
if (input is DirectInputManager)
{
manager = DirectInputManager.LoadConfig(((DirectInputManager)input).device, GetIniFile());
}
PedHash characterHash = PlayerSettings.GetEnumValue<PedHash>(player, CharacterHashKey, PedHash.Trevor.ToString());
BlipSprite blipSprite = PlayerSettings.GetEnumValue<BlipSprite>(player, BlipSpriteKey, BlipSprite.Standard.ToString());
BlipColor blipColor = PlayerSettings.GetEnumValue<BlipColor>(player, BlipColorKey, BlipColor.Green.ToString());
PlayerPed playerPed = new PlayerPed(player, characterHash, blipSprite, blipColor, player1, manager);
playerPeds.Add(playerPed);
break;
}
}
}
}
}
/// <summary>
/// This method will setup the camera system
/// </summary>
private void SetupCamera()
{
Function.Call(Hash.LOCK_MINIMAP_ANGLE, 0);
camera = World.CreateCamera(player1.GetOffsetInWorldCoords(new Vector3(0, 10, 10)), Vector3.Zero, GameplayCamera.FieldOfView);
}
/// <summary>
/// This method is overridden so we can cleanup the script
/// </summary>
protected override void Dispose(bool A_0)
{
Clean();
base.Dispose(A_0);
}
/// <summary>
/// Toggles the menu
/// </summary>
private void TwoPlayerMod_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == toggleMenuKey)
{
menu.Visible = !menu.Visible;
}
}
/// <summary>
/// Cleans up the script, deletes Player2 and cleans the InputManager system
/// </summary>
private void Clean()
{
Function.Call(Hash.UNLOCK_MINIMAP_ANGLE);
CleanCamera();
CleanUpPlayerPeds();
}
/// <summary>
/// Cleans up all PlayerPeds
/// </summary>
private void CleanUpPlayerPeds()
{
playerPeds.ForEach((playerPed => { playerPed.Clean(); }));
playerPeds.Clear();
if (player1 != null)
{
player1.Task.ClearAllImmediately();
}
}
/// <summary>
/// This method will return the center of given vectors
/// </summary>
/// <param name="vectors">Vector3 A</param>
/// <returns>The center Vector3 of the two given Vector3s</returns>
public Vector3 CenterOfVectors(params Vector3[] vectors)
{
Vector3 center = Vector3.Zero;
foreach (Vector3 vector in vectors)
{
center += vector;
}
return center / vectors.Length;
}
private bool resetWalking = false;
private void TwoPlayerMod_Tick(object sender, EventArgs e)
{
menuPool.ProcessMenus();
if (Enabled())
{
if (!Player1Available())
{
CleanCamera();
while (!Player1Available() || Function.Call<bool>(Hash.IS_SCREEN_FADING_IN))
{
Wait(1000);
}
playerPeds.ForEach(playerPed =>
{
playerPed.Respawn();
Wait(500);
});
SetupCamera();
}
playerPeds.ForEach((playerPed => { playerPed.Tick(); }));
UpdateCamera();
if (customCamera && player1.IsOnFoot)
{
if (Game.IsControlPressed(0, GTA.Control.Jump))
{
player1.Task.Climb();
}
Vector2 offset = Vector2.Zero;
if (Game.IsControlPressed(0, GTA.Control.MoveUpOnly))
{
offset.Y++;
}
if (Game.IsControlPressed(0, GTA.Control.MoveLeftOnly))
{
offset.X--;
}
if (Game.IsControlPressed(0, GTA.Control.MoveRightOnly))
{
offset.X++;
}
if (Game.IsControlPressed(0, GTA.Control.MoveDownOnly))
{
offset.Y--;
}
if (offset != Vector2.Zero)
{
if (Game.IsControlPressed(0, GTA.Control.Sprint))
{
// needed for running
offset *= 10;
}
Vector3 dest = Vector3.Zero;
dest = player1.Position - new Vector3(offset.X, offset.Y, 0);
player1.Task.RunTo(dest, true, -1);
resetWalking = true;
}
else if (resetWalking)
{
player1.Task.ClearAll();
resetWalking = false;
}
}
if (Game.IsControlJustReleased(0, GTA.Control.NextCamera))
{
customCamera = !customCamera;
}
// for letting player get in a vehicle
if (player1.IsOnFoot && Game.IsControlJustReleased(0, GTA.Control.VehicleExit))
{
HandleEnterVehicle(player1);
}
}
}
/// <summary>
/// Cleans the Camera system
/// </summary>
private void CleanCamera()
{
World.DestroyAllCameras();
World.RenderingCamera = null;
}
/// <summary>
/// Helper method to check if Player 1 is still normally available
/// </summary>
/// <returns>True when Player 1 is still normally available to play</returns>
private bool Player1Available()
{
return !player1.IsDead && !Function.Call<bool>(Hash.IS_PLAYER_BEING_ARRESTED, player, true) && !Function.Call<bool>(Hash.IS_PLAYER_BEING_ARRESTED, player, false) && !Function.Call<bool>(Hash.IS_SCREEN_FADING_OUT);
}
/// <summary>
/// This will update the camera
/// </summary>
private void UpdateCamera()
{
// if all in same vehicle
if (playerPeds.TrueForAll(p => { return p.Ped.CurrentVehicle != null && p.Ped.CurrentVehicle == player1.CurrentVehicle; }))
{
World.RenderingCamera = null;
}
else if (customCamera)
{
PlayerPed furthestPlayer = playerPeds.OrderByDescending(playerPed => { return player1.Position.DistanceTo(playerPed.Ped.Position); }).FirstOrDefault();
Vector3 center = CenterOfVectors(player1.Position, furthestPlayer.Ped.Position);
World.RenderingCamera = camera;
camera.PointAt(center);
float dist = furthestPlayer.Ped.Position.DistanceTo(player1.Position);
center.Y += 5f + (dist / 1.6f);
center.Z += 2f + (dist / 1.4f);
camera.Position = center;
}
else
{
World.RenderingCamera = null;
}
}
}