-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFusionUtils.cs
747 lines (639 loc) · 28.2 KB
/
FusionUtils.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
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
using FusionLibrary.Extensions;
using GTA;
using GTA.Math;
using GTA.Native;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.RegularExpressions;
using static FusionLibrary.FusionEnums;
namespace FusionLibrary
{
public static class FusionUtils
{
/// <summary>
/// Checks if first tick has gone.
/// </summary>
public static bool FirstTick { get; internal set; } = true;
/// <summary>
/// Represents a pseudo-random number generator, which is an algorithm that produces a sequence of numbers that meet certain statistical requirements for randomness.
/// </summary>
public static Random Random = new Random(DateTime.Now.Millisecond);
/// <summary>
/// Serializes and deserializes an object, or an entire graph of connected objects, in binary format.
/// </summary>
public static BinaryFormatter BinaryFormatter { get; } = new BinaryFormatter();
private static int _padShakeStop;
/// <summary>
/// Gets or sets current <see cref="DateTime"/> of the game's world.
/// </summary>
public static DateTime CurrentTime
{
get => GetWorldTime();
set => SetWorldTime(value);
}
/// <summary>
/// Gets the <see cref="Ped"/> of the current <see cref="GTA.Player"/>.
/// </summary>
public static Ped PlayerPed => Game.Player.Character;
/// <summary>
/// Gets the current driven <see cref="Vehicle"/> by <see cref="GTA.Player"/>.
/// </summary>
public static Vehicle PlayerVehicle => PlayerPed.CurrentVehicle;
public static List<Vehicle> AllVehicles { get; internal set; }
public static int NumDlcVehicles { get; } = Function.Call<int>(Hash.GET_NUM_DLC_VEHICLES);
public static List<VehicleModelInfo> AllVehiclesModels { get; internal set; } = new List<VehicleModelInfo>();
/// <summary>
/// Toggles visibility state of game's GUI.
/// </summary>
public static bool HideGUI { get; set; } = false;
public static string HelpText { get; set; } = null;
public static string NotificationText { get; set; } = null;
public static string SubtitleText { get; set; } = null;
private static bool randomTrains = true;
/// <summary>
/// Toggles random generated trains on map.
/// </summary>
public static bool RandomTrains
{
get => randomTrains;
set
{
Function.Call(Hash.SET_RANDOM_TRAINS, value);
if (!value)
{
Function.Call(Hash.DELETE_ALL_TRAINS);
}
randomTrains = value;
}
}
public static void Log(string message)
{
File.AppendAllText($"./ScriptHookVDotNet.log", $"[{DateTime.Now:HH:mm:ss}] [DEBUG] {message}" + Environment.NewLine);
}
public static Hash GetDlcVehicleModel(int dlcIndex)
{
return Function.Call<Hash>(Hash.GET_DLC_VEHICLE_MODEL, dlcIndex);
}
/// <summary>
/// Requests the given <paramref name="model"/> in game.
/// </summary>
/// <param name="model">Instance of a <see cref="Model"/>.</param>
/// <param name="name">Name of the <paramref name="model"/>.</param>
/// <returns></returns>
public static Model LoadAndRequestModel(Model model, string name = default)
{
if (!model.IsInCdImage || !model.IsValid)
{
if (name == default)
{
throw new Exception(model + " not present!");
}
else
{
throw new Exception(name + " not present!");
}
}
model.Request();
while (!model.IsLoaded)
Script.Yield();
return model;
}
/// <summary>
/// Gets whether a bullet has impacted within the specified radius of a given <paramref name="position"/>.
/// </summary>
/// <param name="position">Instance of a <see cref="Vector3"/>.</param>
/// <param name="radius">Instance of a <see cref="float"/>.</param>
public static bool DidBulletHit(Vector3 position, float radius) => Function.Call<bool>(Hash.HAS_BULLET_IMPACTED_IN_AREA, position.X, position.Y, position.Z, radius);
/// <summary>
/// Gets whether a bullet is present within the specified radius of a given <paramref name="position"/>.
/// </summary>
/// <param name="position">Instance of a <see cref="Vector3"/>.</param>
/// <param name="radius">Instance of a <see cref="float"/>.</param>
public static bool IsBulletPresent(Vector3 position, float radius)
{
if (Function.Call<bool>(Hash.IS_BULLET_IN_AREA, position.X, position.Y, position.Z, radius, false))
{
return true;
}
else if (Function.Call<bool>(Hash.IS_BULLET_IN_AREA, position.X, position.Y, position.Z, radius, true))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Gets or sets whether ambient world traffic generators are active.
/// </summary>
public static bool IsTrafficAlive
{
get => _trafficAlive;
set => SetTrafficAlive(value);
}
private static bool _trafficAlive;
private static void SetTrafficAlive(bool state)
{
Function.Call(Hash.SET_CREATE_RANDOM_COPS, state);
Function.Call(Hash.SET_GARBAGE_TRUCKS, state);
if (state)
Function.Call(Hash.SET_ROADS_BACK_TO_ORIGINAL, -10000.0f, -10000.0f, -1000.0f, 10000.0f, 10000.0f, 1000.0f);
else
Function.Call(Hash.SET_ROADS_IN_AREA, -10000.0f, -10000.0f, -1000.0f, 10000.0f, 10000.0f, 1000.0f, false, false);
if (state)
Function.Call(Hash.SET_ALL_VEHICLE_GENERATORS_ACTIVE);
else
Function.Call(Hash.SET_ALL_VEHICLE_GENERATORS_ACTIVE_IN_AREA, -10000.0f, -10000.0f, -1000.0f, 10000.0f, 10000.0f, 1000.0f, false, false);
Function.Call(Hash.SET_DISTANT_CARS_ENABLED, state);
Function.Call(Hash.DISABLE_VEHICLE_DISTANTLIGHTS, !state);
_trafficAlive = state;
}
/// <summary>
/// Given a <paramref name="position"/> returns the nearest roadside point.
/// </summary>
/// <param name="position">Instance of a <see cref="Vector3"/>.</param>
/// <returns>Nearest roadside point</returns>
public static Vector3 GetPointOnRoadSide(Vector3 position)
{
OutputArgument ret = new OutputArgument();
Function.Call(Hash.GET_POSITION_BY_SIDE_OF_ROAD, position.X, position.Y, position.Z, -1, ret);
return ret.GetResult<Vector3>();
}
/// <summary>
/// Cleares game's world from every ped and vehicles. Except entities with <see cref="Decorator.DoNotDelete"/> == <see langword="true"/>.
/// </summary>
public static void ClearWorld()
{
Function.Call(Hash.DELETE_ALL_TRAINS);
Function.Call(Hash.CLEAR_AREA_OF_COPS, PlayerPed.Position.X, PlayerPed.Position.Y, PlayerPed.Position.Z, 1000f, 0);
Vehicle[] allVehicles = World.GetAllVehicles();
allVehicles.Where(x => x.NotNullAndExists() && !x.Decorator().DoNotDelete).ToList()
.ForEach(x => x?.DeleteCompletely());
Ped[] allPeds = World.GetAllPeds();
allPeds.Where(x => x.NotNullAndExists() && x != PlayerPed && (!PlayerVehicle.NotNullAndExists() || !PlayerVehicle.Passengers.Contains(x))).ToList()
.ForEach(x => x?.Delete());
}
/// <summary>
/// Given two <see cref="Vector3"/> points, returns the rotation between them.
/// </summary>
/// <param name="src">First <see cref="Vector3"/> point.</param>
/// <param name="dst">Second <see cref="Vector3"/> point.</param>
/// <param name="roll">Desired roll for output rotation.</param>
/// <returns>Rotation between the two points</returns>
public static Vector3 DirectionToRotation(Vector3 src, Vector3 dst, float roll)
{
Vector3 dir = src.GetDirectionTo(dst);
Vector3 rotval = Vector3.Zero;
rotval.Z = -(((float)Math.Atan2(dir.X, dir.Y)).ToDeg());
Vector3 rotpos = Vector3.Normalize(new Vector3(dir.Z, new Vector3(dir.X, dir.Y, 0.0f).Length(), 0.0f));
rotval.X = ((float)Math.Atan2(rotpos.X, rotpos.Y)).ToDeg();
rotval.Y = roll;
return rotval;
}
/// <summary>
/// Checks if pad is shaking.
/// </summary>
public static bool IsPadShaking => _padShakeStop >= Game.GameTime;
/// <summary>
/// Sets pad shake <paramref name="duration"/> and <paramref name="frequency"/>.
/// </summary>
/// <param name="duration"></param>
/// <param name="frequency"></param>
public static void SetPadShake(int duration, int frequency)
{
_padShakeStop = Game.GameTime + duration;
Function.Call(Hash.SET_CONTROL_SHAKE, 0, duration, frequency);
}
/// <summary>
/// Instantly stops pad shake.
/// </summary>
public static void StopPadShake()
{
_padShakeStop = 0;
Function.Call(Hash.STOP_CONTROL_SHAKE);
}
/// <summary>
/// Gets the current game's world <see cref="DateTime"/>.
/// </summary>
/// <returns></returns>
private static DateTime GetWorldTime()
{
try
{
int month = Function.Call<int>(Hash.GET_CLOCK_MONTH) + 1;
int year = Function.Call<int>(Hash.GET_CLOCK_YEAR);
int day = Function.Call<int>(Hash.GET_CLOCK_DAY_OF_MONTH);
int hour = Function.Call<int>(Hash.GET_CLOCK_HOURS);
int minute = Function.Call<int>(Hash.GET_CLOCK_MINUTES);
int second = Function.Call<int>(Hash.GET_CLOCK_SECONDS);
return new DateTime(year, month, day, hour, minute, second);
}
catch (Exception)
{
Function.Call(Hash.SET_CLOCK_DATE, 1985, 8, 21);
Function.Call(Hash.SET_CLOCK_TIME, 8, 0, 0);
return new DateTime(1985, 9, 21, 8, 0, 0);
}
}
/// <summary>
/// Sets the current game's world <see cref="DateTime"/>.
/// </summary>
/// <param name="time"></param>
private static void SetWorldTime(DateTime time)
{
Function.Call(Hash.SET_CLOCK_DATE, time.Day, time.Month - 1, time.Year);
Function.Call(Hash.SET_CLOCK_TIME, time.Hour, time.Minute, time.Second);
}
// https://code.google.com/archive/p/slimmath/
public static bool Decompose(Matrix matrix, out Vector3 scale, out Quaternion rotation, out Vector3 translation)
{
const float ZeroTolerance = 1e-6f;
translation = Vector3.Zero;
scale = Vector3.Zero;
//Source: Unknown
//References: http://www.gamedev.net/community/forums/topic.asp?topic_id=441695
//Get the translation.
translation.X = matrix.M41;
translation.Y = matrix.M42;
translation.Z = matrix.M43;
//Scaling is the length of the rows.
scale.X = (float)Math.Sqrt((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12) + (matrix.M13 * matrix.M13));
scale.Y = (float)Math.Sqrt((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22) + (matrix.M23 * matrix.M23));
scale.Z = (float)Math.Sqrt((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32) + (matrix.M33 * matrix.M33));
//If any of the scaling factors are zero, than the rotation matrix can not exist.
if (Math.Abs(scale.X) < ZeroTolerance ||
Math.Abs(scale.Y) < ZeroTolerance ||
Math.Abs(scale.Z) < ZeroTolerance)
{
rotation = Quaternion.Identity;
return false;
}
//The rotation is the left over matrix after dividing out the scaling.
Matrix rotationmatrix = new Matrix
{
M11 = matrix.M11 / scale.X,
M12 = matrix.M12 / scale.X,
M13 = matrix.M13 / scale.X,
M21 = matrix.M21 / scale.Y,
M22 = matrix.M22 / scale.Y,
M23 = matrix.M23 / scale.Y,
M31 = matrix.M31 / scale.Z,
M32 = matrix.M32 / scale.Z,
M33 = matrix.M33 / scale.Z,
M44 = 1f
};
rotation = Quaternion.RotationMatrix(rotationmatrix);
return true;
}
/// <summary>
/// Wraps <paramref name="x"/> between <paramref name="min"/> and <paramref name="max"/>.
/// </summary>
/// <param name="x">Original value</param>
/// <param name="min">Min value</param>
/// <param name="max">Max value</param>
/// <returns>Wrapped value</returns>
public static float Wrap(float x, float min, float max)
{
float delta = max - min;
x = (x + max) % delta;
if (x < 0)
{
x += delta;
}
return x + min;
}
/// <summary>
/// Linear interpolation between <paramref name="a"/> and <paramref name="b"/> by <paramref name="f"/> value.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="f"></param>
/// <returns>Interpolated value.</returns>
public static float Lerp(float a, float b, float f)
{
return a + (b - a) * f;
}
/// <summary>
/// Combination of <see cref="Lerp(float, float, float)"/> and <see cref="Wrap(float, float, float)"/>.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="f"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static float Lerp(float a, float b, float f, float min, float max)
{
return Wrap(Lerp(a, b, f), min, max);
}
/// <summary>
/// Linear interpolation between <paramref name="a"/> and <paramref name="b"/> by <paramref name="f"/> value.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="f"></param>
/// <returns>Interpolated value.</returns>
public static int Lerp(int a, int b, float f)
{
return (int)(a + (b - (float)a) * f);
}
/// <summary>
/// Linear interpolation between <paramref name="a"/> and <paramref name="b"/> by <paramref name="f"/> value.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="f"></param>
/// <returns>Interpolated value.</returns>
public static Vector3 Lerp(Vector3 a, Vector3 b, float f)
{
return new Vector3() { X = Lerp(a.X, b.X, f), Y = Lerp(a.Y, b.Y, f), Z = Lerp(a.Z, b.Z, f) };
}
/// <summary>
/// Combination of <see cref="Lerp(float, float, float)"/> and <see cref="Wrap(float, float, float)"/>.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="f"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static Vector3 Lerp(Vector3 a, Vector3 b, float f, float min, float max)
{
return new Vector3() { X = Lerp(a.X, b.X, f, min, max), Y = Lerp(a.Y, b.Y, f, min, max), Z = Lerp(a.Z, b.Z, f, min, max) };
}
/// <summary>
/// Returns the unit vector of the specified <paramref name="coordinate"/>.
/// </summary>
/// <param name="coordinate">Desired <see cref="Coordinate"/> of unit vector.</param>
/// <returns>Unit vector.</returns>
public static Vector3 GetUnitVector(Coordinate coordinate)
{
switch (coordinate)
{
case Coordinate.X:
return Vector3.UnitX;
case Coordinate.Y:
return Vector3.UnitY;
default:
return Vector3.UnitZ;
}
}
/// <summary>
/// List of wheels bones names.
/// </summary>
public static readonly string[] WheelsBonesNames = new string[10]
{
"wheel_lf",
"wheel_rf",
"wheel_lr",
"wheel_rr",
"wheel_lm1",
"wheel_rm1",
"wheel_lm2",
"wheel_rm2",
"wheel_lm3",
"wheel_rm3"
};
/// <summary>
/// Returns the <see cref="VehicleWheelBoneId"/> of the given wheel's <paramref name="name"/>.
/// </summary>
/// <param name="name">Wheel's name.</param>
/// <returns><see cref="VehicleWheelBoneId"/> of the wheel.</returns>
public static VehicleWheelBoneId ConvertWheelNameToID(string name)
{
switch (name)
{
case "wheel_lf":
return VehicleWheelBoneId.WheelLeftFront;
case "wheel_lr":
return VehicleWheelBoneId.WheelLeftRear;
case "wheel_rf":
return VehicleWheelBoneId.WheelRightFront;
case "wheel_rr":
return VehicleWheelBoneId.WheelRightRear;
case "wheel_lm1":
return VehicleWheelBoneId.WheelLeftMiddle1;
case "wheel_rm1":
return VehicleWheelBoneId.WheelRightMiddle1;
case "wheel_lm2":
return VehicleWheelBoneId.WheelLeftMiddle2;
case "wheel_rm2":
return VehicleWheelBoneId.WheelRightMiddle2;
case "wheel_lm3":
return VehicleWheelBoneId.WheelLeftMiddle3;
case "wheel_rm3":
return VehicleWheelBoneId.WheelRightMiddle3;
default:
return VehicleWheelBoneId.Invalid;
}
}
public static string ConvertWheelIDToName(VehicleWheelBoneId id)
{
switch (id)
{
case VehicleWheelBoneId.WheelLeftFront:
return "wheel_lf";
case VehicleWheelBoneId.WheelLeftRear:
return "wheel_lr";
case VehicleWheelBoneId.WheelRightFront:
return "wheel_rf";
case VehicleWheelBoneId.WheelRightRear:
return "wheel_rr";
case VehicleWheelBoneId.WheelLeftMiddle1:
return "wheel_lm1";
case VehicleWheelBoneId.WheelRightMiddle1:
return "wheel_rm1";
case VehicleWheelBoneId.WheelLeftMiddle2:
return "wheel_lm2";
case VehicleWheelBoneId.WheelRightMiddle2:
return "wheel_rm2";
case VehicleWheelBoneId.WheelLeftMiddle3:
return "wheel_lm3";
case VehicleWheelBoneId.WheelRightMiddle3:
return "wheel_rm3";
default:
return "";
}
}
/// <summary>
/// Spawns a train using <paramref name="variation"/> config and <paramref name="direction"/> at <paramref name="position"/>.
/// </summary>
/// <param name="variation">ID of the train configuration. Check trains.xml for available ones.</param>
/// <param name="position">Position of the train. Actually it will be used the nearest track to this position.</param>
/// <param name="direction">Direction of the train.</param>
/// <returns><see cref="Vehicle"/> instance of the head of the train.</returns>
public static Vehicle CreateMissionTrain(int variation, Vector3 position, bool direction)
{
return Function.Call<Vehicle>(Hash.CREATE_MISSION_TRAIN, variation, position.X, position.Y, position.Z, direction);
}
/// <summary>
/// Parses a <paramref name="raw"/> string trying to retrieve a correct <see cref="DateTime"/> representation.
/// </summary>
/// <param name="raw">Raw string</param>
/// <param name="currentTime">Original <see cref="DateTime"/>.</param>
/// <param name="inputType">Returns the <see cref="InputType"/>.</param>
/// <returns><see cref="DateTime"/> value; otherwise <c>null</c>.</returns>
public static DateTime? ParseFromRawString(string raw, DateTime currentTime, out InputType inputType)
{
try
{
if (raw.Length == 12)
{
string month = raw.Substring(0, 2);
string day = raw.Substring(2, 2);
string year = raw.Substring(4, 4);
string hour = raw.Substring(8, 2);
string minute = raw.Substring(10, 2);
inputType = InputType.Full;
return new DateTime(int.Parse(year), int.Parse(month), int.Parse(day), int.Parse(hour), int.Parse(minute), 0);
}
else if (raw.Length == 8)
{
string month = raw.Substring(0, 2);
string day = raw.Substring(2, 2);
string year = raw.Substring(4, 4);
inputType = InputType.Date;
return new DateTime(int.Parse(year), int.Parse(month), int.Parse(day), currentTime.Hour, currentTime.Minute, 0);
}
else if (raw.Length == 4)
{
string hour = raw.Substring(0, 2);
string minute = raw.Substring(2, 2);
inputType = InputType.Time;
return new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, int.Parse(hour), int.Parse(minute), 0);
}
inputType = InputType.Error;
return null;
}
catch (Exception)
{
inputType = InputType.Error;
return null;
}
}
/// <summary>
/// Returns the 2D squared distance between <paramref name="entity1"/> and <paramref name="entity2"/>.
/// </summary>
/// <param name="entity1">Instance of an entity.</param>
/// <param name="entity2">Instance of an entity.</param>
/// <returns>Distance in <c>float</c> between the entities.</returns>
public static float DistanceToSquared2D(Entity entity1, Entity entity2)
{
return entity1.Position.DistanceToSquared2D(entity2.Position);
}
/// <summary>
/// Draws a line.
/// </summary>
/// <param name="from">First point.</param>
/// <param name="to">Second point.</param>
/// <param name="color">Color of the line.</param>
public static void DrawLine(Vector3 from, Vector3 to, Color color)
{
Function.Call(Hash.DRAW_LINE, from.X, from.Y, from.Z, to.X, to.Y, to.Z, color.R, color.G, color.B, color.A);
}
/// <summary>
/// Gets the position on ground of the given <paramref name="position"/> with <paramref name="verticalOffset"/>.
/// </summary>
/// <param name="position">Point in the world.</param>
/// <param name="verticalOffset">Z offset.</param>
/// <returns>Point on ground.</returns>
public static Vector3 GetPositionOnGround(Vector3 position, float verticalOffset)
{
float result = -1;
unsafe
{
Function.Call(Hash.GET_GROUND_Z_FOR_3D_COORD, position.X, position.Y, position.Z, &result, false);
}
position.Z = result + verticalOffset;
return position;
}
/// <summary>
/// Gets the position of the waypoint, if any.
/// </summary>
/// <returns><see cref="Vector3"/> position of the waypoint. Returns <see cref="Vector3.Zero"/> if waypoint is not present.</returns>
public static Vector3 GetWaypointPosition()
{
if (!Game.IsWaypointActive)
{
return Vector3.Zero;
}
Vector3 position = World.WaypointPosition;
do
{
for (int z = 0; z < 1000; z += 100)
{
position = new Vector3(position.X, position.Y, z);
position.RequestCollision();
Script.Yield();
position.Z = World.GetGroundHeight(new Vector2(position.X, position.Y));
}
} while (position.Z == 0);
return position;
}
/// <summary>
/// Checks if current camera is in first person view.
/// </summary>
/// <returns><see langword="true"/> if FPV is enabled; otherwise <see langword="false"/>.</returns>
public static bool IsCameraInFirstPerson()
{
return Function.Call<bool>(Hash.IS_CINEMATIC_FIRST_PERSON_VEHICLE_INTERIOR_CAM_RENDERING) || Function.Call<bool>(Hash.IS_BONNET_CINEMATIC_CAM_RENDERING);
}
public static float RainLevel
{
get => Function.Call<float>(Hash.GET_RAIN_LEVEL);
set => Function.Call(Hash.SET_RAIN, value);
}
public static float WindSpeed
{
get => Function.Call<float>(Hash.GET_WIND_SPEED);
set => Function.Call(Hash.SET_WIND_SPEED, value);
}
public static float Magnitude(Vector3 vector3)
{
return Function.Call<float>(Hash.VMAG2, vector3.X, vector3.Z, vector3.Y);
}
/// <summary>
/// Checks if wheel at <paramref name="pos"/> of <paramref name="vehicle"/> is on rail tracks.
/// </summary>
/// <param name="pos"><see cref="Vector3"/> of the wheel.</param>
/// <param name="vehicle">Instance of a <see cref="Vector3"/>.</param>
/// <returns><see langword="true"/> wheel is on rail tracks; otherwise <see langword="false"/>.</returns>
internal static bool IsWheelOnTracks(Vector3 pos, Vehicle vehicle)
{
float diff = GetPositionOnGround(pos, -0.01f).Z - pos.Z;
RaycastResult ret = World.Raycast(pos, pos.GetSingleOffset(Coordinate.Z, diff), IntersectFlags.Map, vehicle);
// Tracks materials
List<MaterialHash> allowedSurfaces = new List<MaterialHash>
{
MaterialHash.MetalSolidRoadSurface,
MaterialHash.MetalSolidSmall,
MaterialHash.MetalSolidMedium,
MaterialHash.MetalSolidLarge,
MaterialHash.GravelTrainTrack
};
return allowedSurfaces.Contains(ret.MaterialHash);
}
public static DateTime RandomDate()
{
Random rand = new Random();
int second = rand.Next(0, 59);
int minute = rand.Next(0, 59);
int hour = rand.Next(0, 23);
int month = rand.Next(1, 12);
int year = rand.Next(1, 9999);
int day = rand.Next(1, DateTime.DaysInMonth(year, month));
return new DateTime(year, month, day, hour, minute, second);
}
public static string RemoveIllegalFileNameChars(string input, string replacement = "")
{
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
return r.Replace(input, replacement);
}
}
}