@aifivem/lib 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ import { isRecord } from "./record";
2
+
3
+ /**
4
+ * A vehicle's damage beyond engine and body health, as plain data: read and restored by the server on FiveM for GTAV
5
+ * Enhanced (lib server `enhanced`). Stored JSON carries `v: 1`.
6
+ */
7
+ export interface VehicleDamage {
8
+ v: 1;
9
+ /** Smashed windows: 0 front left, 1 front right, 2 rear left, 3 rear right, 4 middle left, 5 middle right, 6 windscreen, 7 rear windscreen. */
10
+ windows: number[];
11
+ /** Burst tyres by synced wheel index, `true` when it runs on the rim. */
12
+ tyres: Array<[wheel: number, onRim: boolean]>;
13
+ /** Broken off wheels by synced wheel index. */
14
+ wheelsOff: number[];
15
+ /** Broken lights, 0-21 (`SET_VEHICLE_LIGHT_BROKEN`: headlights 0-1, taillights 2-3, indicators 4-7, ...). */
16
+ lights: number[];
17
+ /** Front and rear bumper: 0 intact, 1 hanging loose, 2 broken off. */
18
+ bumpers: [front: number, rear: number];
19
+ /** Petrol tank health, 1000 undamaged. */
20
+ tank: number;
21
+ }
22
+
23
+ /** Highest index kept of each part: the server reader walks the same ranges. */
24
+ export const VEHICLE_DAMAGE_MAX = {
25
+ window: 7,
26
+ light: 21,
27
+ /** Synced wheel index (a vehicle has at most 10 wheels). */
28
+ wheel: 15,
29
+ } as const;
30
+ const MAX_WHEEL = VEHICLE_DAMAGE_MAX.wheel;
31
+
32
+ const indexes = (v: unknown, max: number): number[] | null =>
33
+ Array.isArray(v) &&
34
+ v.length <= max + 1 &&
35
+ v.every((x) => Number.isInteger(x) && x >= 0 && x <= max)
36
+ ? [...new Set(v as number[])]
37
+ : null;
38
+
39
+ /** A bumper state (0 intact, 1 loose, 2 off), null when it is not one. */
40
+ export const readBumperState = (v: unknown): number | null =>
41
+ v === 0 || v === 1 || v === 2 ? v : null;
42
+
43
+ /** Stored damage, checked; null when the shape is not ours. */
44
+ export function toVehicleDamage(v: unknown): VehicleDamage | null {
45
+ if (!isRecord(v) || v.v !== 1) return null;
46
+ const windows = indexes(v.windows, VEHICLE_DAMAGE_MAX.window);
47
+ const wheelsOff = indexes(v.wheelsOff, MAX_WHEEL);
48
+ const lights = indexes(v.lights, VEHICLE_DAMAGE_MAX.light);
49
+ const front = Array.isArray(v.bumpers) ? readBumperState(v.bumpers[0]) : null;
50
+ const rear = Array.isArray(v.bumpers) ? readBumperState(v.bumpers[1]) : null;
51
+ if (
52
+ !windows ||
53
+ !wheelsOff ||
54
+ !lights ||
55
+ front === null ||
56
+ rear === null ||
57
+ typeof v.tank !== "number" ||
58
+ !Number.isFinite(v.tank) ||
59
+ !Array.isArray(v.tyres) ||
60
+ v.tyres.length > MAX_WHEEL + 1
61
+ )
62
+ return null;
63
+ const tyres: Array<[number, boolean]> = [];
64
+ for (const t of v.tyres) {
65
+ if (
66
+ !Array.isArray(t) ||
67
+ !Number.isInteger(t[0]) ||
68
+ t[0] < 0 ||
69
+ t[0] > MAX_WHEEL ||
70
+ typeof t[1] !== "boolean"
71
+ )
72
+ return null;
73
+ tyres.push([t[0] as number, t[1]]);
74
+ }
75
+ return {
76
+ v: 1,
77
+ windows,
78
+ tyres,
79
+ wheelsOff,
80
+ lights,
81
+ bumpers: [front, rear],
82
+ tank: Math.min(1000, Math.max(-1000, v.tank)),
83
+ };
84
+ }
@@ -1,3 +1,4 @@
1
+ import { isRecord } from "./record";
1
2
  /**
2
3
  * A vehicle's look and tuning as plain data (colours, mods, extras, livery, wheels, neon, tint, plate), read from and
3
4
  * written to an entity. Stored JSON carries `v: 1`.
@@ -26,8 +27,6 @@ export interface VehicleProps {
26
27
  bodyHealth: number;
27
28
  }
28
29
 
29
- const isObj = (v: unknown): v is Record<string, unknown> =>
30
- typeof v === "object" && v !== null && !Array.isArray(v);
31
30
  const nums = (v: unknown, n: number): number[] | null =>
32
31
  Array.isArray(v) && v.length === n && v.every((x) => typeof x === "number")
33
32
  ? v
@@ -36,7 +35,7 @@ const nums = (v: unknown, n: number): number[] | null =>
36
35
  /** Stored or received props, checked; null when the shape is not ours. */
37
36
  export function toVehicleProps(v: unknown): VehicleProps | null {
38
37
  if (
39
- !isObj(v) ||
38
+ !isRecord(v) ||
40
39
  v.v !== 1 ||
41
40
  typeof v.model !== "number" ||
42
41
  typeof v.plate !== "string"
@@ -51,8 +50,8 @@ export function toVehicleProps(v: unknown): VehicleProps | null {
51
50
  !extraColors ||
52
51
  !neonColor ||
53
52
  !tyreSmoke ||
54
- !isObj(v.mods) ||
55
- !isObj(v.extras) ||
53
+ !isRecord(v.mods) ||
54
+ !isRecord(v.extras) ||
56
55
  !Array.isArray(v.neon)
57
56
  )
58
57
  return null;
@@ -124,7 +123,7 @@ const colorsOf = (v: unknown): [number, number] | undefined => {
124
123
 
125
124
  /** A received setup, checked field by field (wrong fields drop); null when it is not an object. */
126
125
  export function toVehicleSetup(v: unknown): VehicleSetup | null {
127
- if (!isObj(v)) return null;
126
+ if (!isRecord(v)) return null;
128
127
  return {
129
128
  maxed: v.maxed === true || undefined,
130
129
  colors: colorsOf(v.colors),