@aifivem/lib 0.0.0-stage → 0.7.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.
Files changed (162) hide show
  1. package/package.json +34 -4
  2. package/src/client/action.ts +38 -0
  3. package/src/client/adminWorld.ts +95 -0
  4. package/src/client/appearance.ts +29 -0
  5. package/src/client/areaFollower.ts +41 -0
  6. package/src/client/blip.ts +79 -0
  7. package/src/client/cache.ts +66 -0
  8. package/src/client/callback.ts +70 -0
  9. package/src/client/controls.ts +73 -0
  10. package/src/client/entities.ts +83 -0
  11. package/src/client/entity.ts +26 -0
  12. package/src/client/fuel.ts +18 -0
  13. package/src/client/gestures.ts +18 -0
  14. package/src/client/headshot.ts +26 -0
  15. package/src/client/hud.ts +24 -0
  16. package/src/client/index.ts +131 -0
  17. package/src/client/inventory.ts +117 -0
  18. package/src/client/keyHints.ts +13 -0
  19. package/src/client/keybind.ts +163 -0
  20. package/src/client/localPed.ts +169 -0
  21. package/src/client/location.ts +54 -0
  22. package/src/client/mdt.ts +10 -0
  23. package/src/client/medical.ts +28 -0
  24. package/src/client/membership.ts +41 -0
  25. package/src/client/menu/dialogs.ts +77 -0
  26. package/src/client/menu/minigames.ts +79 -0
  27. package/src/client/menu/requests.ts +65 -0
  28. package/src/client/modelScan/def.ts +29 -0
  29. package/src/client/modelScan/objectIndex.ts +41 -0
  30. package/src/client/modelScan/scan.ts +69 -0
  31. package/src/client/modelScan/scanner.ts +164 -0
  32. package/src/client/netEvent.ts +40 -0
  33. package/src/client/notify.ts +14 -0
  34. package/src/client/nuiScreen.ts +87 -0
  35. package/src/client/player/anim.ts +98 -0
  36. package/src/client/player/fade.ts +17 -0
  37. package/src/client/player/player.ts +80 -0
  38. package/src/client/player/teleport.ts +87 -0
  39. package/src/client/playerData.ts +83 -0
  40. package/src/client/players.ts +44 -0
  41. package/src/client/points/interact.ts +144 -0
  42. package/src/client/points/modelHost.ts +117 -0
  43. package/src/client/points/modelPoint.ts +181 -0
  44. package/src/client/points/point.ts +159 -0
  45. package/src/client/points/points.ts +170 -0
  46. package/src/client/points/targets/adapter.ts +40 -0
  47. package/src/client/points/targets/interactTarget.ts +45 -0
  48. package/src/client/points/targets/optionShape.ts +22 -0
  49. package/src/client/points/targets/oxTarget.ts +42 -0
  50. package/src/client/points/targets/qbTarget.ts +71 -0
  51. package/src/client/points/targets/targets.ts +20 -0
  52. package/src/client/preview.ts +51 -0
  53. package/src/client/progress.ts +132 -0
  54. package/src/client/prompt.ts +26 -0
  55. package/src/client/prop.ts +79 -0
  56. package/src/client/radial.ts +16 -0
  57. package/src/client/raycast.ts +48 -0
  58. package/src/client/scriptCamera.ts +167 -0
  59. package/src/client/seatbelt.ts +18 -0
  60. package/src/client/shot.ts +29 -0
  61. package/src/client/streaming.ts +91 -0
  62. package/src/client/tickLoop.ts +18 -0
  63. package/src/client/vehicleKeys.ts +14 -0
  64. package/src/client/vehicleProps.ts +107 -0
  65. package/src/client/vehicles.ts +46 -0
  66. package/src/client/waypoint.ts +10 -0
  67. package/src/client/world.ts +10 -0
  68. package/src/client/zoneEngine.ts +174 -0
  69. package/src/client/zones.ts +161 -0
  70. package/src/fivem/byResource.ts +146 -0
  71. package/src/fivem/callbackNames.ts +12 -0
  72. package/src/fivem/exports.ts +70 -0
  73. package/src/fivem/index.ts +13 -0
  74. package/src/fivem/locale.ts +57 -0
  75. package/src/fivem/playerData.ts +13 -0
  76. package/src/fivem/scheduler/every.ts +101 -0
  77. package/src/fivem/scheduler/group.ts +97 -0
  78. package/src/fivem/scheduler/scheduler.ts +77 -0
  79. package/src/fivem/state/entityState.ts +75 -0
  80. package/src/fivem/state/globalState.ts +80 -0
  81. package/src/fivem/state/mirror.ts +47 -0
  82. package/src/fivem/state/playerState.ts +60 -0
  83. package/src/server/admin/descriptions.ts +57 -0
  84. package/src/server/admin/edit.ts +127 -0
  85. package/src/server/admin/jsonSchema.ts +156 -0
  86. package/src/server/admin/panel.ts +167 -0
  87. package/src/server/admin/panelFiles.ts +128 -0
  88. package/src/server/admin/patch.ts +46 -0
  89. package/src/server/admin/validate.ts +147 -0
  90. package/src/server/appearance.ts +28 -0
  91. package/src/server/areaStream/areaStream.ts +141 -0
  92. package/src/server/areaStream/followers.ts +154 -0
  93. package/src/server/areaStream/grid.ts +74 -0
  94. package/src/server/areaStream/options.ts +16 -0
  95. package/src/server/banking.ts +15 -0
  96. package/src/server/bans.ts +47 -0
  97. package/src/server/billing.ts +20 -0
  98. package/src/server/buckets.ts +12 -0
  99. package/src/server/callback.ts +112 -0
  100. package/src/server/characters.ts +129 -0
  101. package/src/server/command/addCommand.ts +83 -0
  102. package/src/server/command/command.ts +116 -0
  103. package/src/server/command/commandSet.ts +74 -0
  104. package/src/server/dataFile.ts +81 -0
  105. package/src/server/database.ts +117 -0
  106. package/src/server/definitions.ts +65 -0
  107. package/src/server/dispatch.ts +13 -0
  108. package/src/server/doors.ts +15 -0
  109. package/src/server/ems.ts +15 -0
  110. package/src/server/fuel.ts +15 -0
  111. package/src/server/garages.ts +34 -0
  112. package/src/server/gate.ts +148 -0
  113. package/src/server/index.ts +165 -0
  114. package/src/server/inventory.ts +106 -0
  115. package/src/server/jobs.ts +42 -0
  116. package/src/server/log.ts +21 -0
  117. package/src/server/mdt.ts +12 -0
  118. package/src/server/medical.ts +16 -0
  119. package/src/server/minigameFor.ts +65 -0
  120. package/src/server/money.ts +70 -0
  121. package/src/server/netEntity.ts +19 -0
  122. package/src/server/netEvent.ts +58 -0
  123. package/src/server/notify.ts +17 -0
  124. package/src/server/online/mirror.ts +176 -0
  125. package/src/server/online/online.ts +190 -0
  126. package/src/server/pending.ts +70 -0
  127. package/src/server/phone.ts +22 -0
  128. package/src/server/players.ts +197 -0
  129. package/src/server/position.ts +31 -0
  130. package/src/server/primeTime.ts +7 -0
  131. package/src/server/prison.ts +15 -0
  132. package/src/server/progressFor.ts +62 -0
  133. package/src/server/proximity.ts +98 -0
  134. package/src/server/rateLimit.ts +53 -0
  135. package/src/server/state.ts +45 -0
  136. package/src/server/stress.ts +25 -0
  137. package/src/server/vehicle/nearby.ts +66 -0
  138. package/src/server/vehicle/plate.ts +14 -0
  139. package/src/server/vehicle/spawn.ts +125 -0
  140. package/src/server/vehicle/vehicleType.ts +50 -0
  141. package/src/server/vehicleKeys.ts +23 -0
  142. package/src/server/watchers.ts +46 -0
  143. package/src/server/world.ts +16 -0
  144. package/src/shared/access.ts +94 -0
  145. package/src/shared/action.ts +28 -0
  146. package/src/shared/blipStyle.ts +26 -0
  147. package/src/shared/clock.ts +22 -0
  148. package/src/shared/format.ts +36 -0
  149. package/src/shared/grid.ts +20 -0
  150. package/src/shared/index.ts +179 -0
  151. package/src/shared/lang.ts +39 -0
  152. package/src/shared/placePed.ts +96 -0
  153. package/src/shared/random.ts +11 -0
  154. package/src/shared/read.ts +60 -0
  155. package/src/shared/records.ts +148 -0
  156. package/src/shared/spec.ts +20 -0
  157. package/src/shared/strict.ts +70 -0
  158. package/src/shared/vec.ts +48 -0
  159. package/src/shared/vehicleProps.ts +138 -0
  160. package/src/shared/wait.ts +27 -0
  161. package/src/shared/zone.ts +144 -0
  162. package/README.md +0 -3
@@ -0,0 +1,144 @@
1
+ import { CORE } from "@aifivem/shared";
2
+ import { isResourceStarted } from "../../fivem/exports";
3
+ import { GlobalKey } from "../../fivem/state/globalState";
4
+ import { stateString } from "../../fivem/state/mirror";
5
+ import { createLogger } from "../../shared";
6
+ import type { Target, TargetAdapter } from "./targets/adapter";
7
+ import { TARGETS } from "./targets/targets";
8
+
9
+ /** What `Points` and model points share: the interaction style, the target in use, one action at a time. */
10
+
11
+ export type Style = "prompt" | "target";
12
+ export const log = createLogger(GetCurrentResourceName());
13
+
14
+ const NAMES = new Set(TARGETS.map((t) => t.resource));
15
+ /**
16
+ * Targets that read started but answered "No such export": while clients load, a resource reads started before its
17
+ * scripts ran (and a provided name reads started without answering). They are skipped until a target starts.
18
+ */
19
+ const notReady = new Set<string>();
20
+ const changeHandlers: Array<() => void> = [];
21
+ let changeQueued = false;
22
+ /** Tells `onTargetChange` listeners once, after the current call (the start / stop event or the failed call). */
23
+ function changed(): void {
24
+ if (changeQueued) return;
25
+ changeQueued = true;
26
+ setTimeout(() => {
27
+ changeQueued = false;
28
+ for (const fn of changeHandlers) fn();
29
+ }, 0);
30
+ }
31
+ for (const event of ["onClientResourceStart", "onClientResourceStop"])
32
+ on(event, (resource: string) => {
33
+ if (!NAMES.has(resource)) return;
34
+ if (event === "onClientResourceStart") notReady.clear();
35
+ changed();
36
+ });
37
+
38
+ /** Runs `fn` when the target in use may have changed (a target started, stopped or turned out not ready). */
39
+ export const onTargetChange = (fn: () => void) => void changeHandlers.push(fn);
40
+
41
+ /** The target options go on: the first running one in `TARGETS` order that is ready, null when none. */
42
+ export function activeTarget(): Target | null {
43
+ for (const target of TARGETS)
44
+ if (!notReady.has(target.resource) && isResourceStarted(target.resource))
45
+ return target;
46
+ return null;
47
+ }
48
+ export const targetRunning = (): boolean => activeTarget() !== null;
49
+
50
+ /** One call on a target; null when it failed (no exports yet: the target is skipped until a target starts). */
51
+ function useTarget<T>(
52
+ target: Target,
53
+ call: (adapter: TargetAdapter) => T,
54
+ ): { value: T } | null {
55
+ try {
56
+ return { value: call(target.adapter) };
57
+ } catch (error) {
58
+ if (String(error).includes("No such export")) {
59
+ notReady.add(target.resource);
60
+ changed();
61
+ } else log.error(`${target.resource} target call failed`, error);
62
+ return null;
63
+ }
64
+ }
65
+
66
+ /** An option put on a target, with the call that takes it off again. */
67
+ export interface Registration {
68
+ target: Target;
69
+ remove(adapter: TargetAdapter): void;
70
+ }
71
+
72
+ /** Puts an option on a target; null when the target could not take it (the point stays a prompt meanwhile). */
73
+ export function register<T>(
74
+ target: Target,
75
+ add: (adapter: TargetAdapter) => T,
76
+ remove: (adapter: TargetAdapter, value: T) => void,
77
+ ): Registration | null {
78
+ const added = useTarget(target, add);
79
+ return added && { target, remove: (a) => remove(a, added.value) };
80
+ }
81
+
82
+ /** Takes an option off its target again (nothing to do when that target stopped: its options went with it). */
83
+ export function unregister(registration: Registration | null): void {
84
+ if (registration && isResourceStarted(registration.target.resource))
85
+ useTarget(registration.target, registration.remove);
86
+ }
87
+
88
+ const interaction = new GlobalKey(CORE.interactionStateKey, stateString);
89
+ /** Prompt mode, or target mode when the owner picked it and a target script runs. */
90
+ export const style = (): Style =>
91
+ interaction.get() === "target" && targetRunning() ? "target" : "prompt";
92
+ export const onStyleChange = (fn: () => void) => interaction.onChange(fn);
93
+
94
+ /** The style one point uses: the owner's, unless the point asks for its own (`promptOnly`, `preferTarget`). */
95
+ export function pointStyle(
96
+ options: { promptOnly?: boolean; preferTarget?: boolean },
97
+ group: Style,
98
+ ): Style {
99
+ if (options.promptOnly) return "prompt";
100
+ if (options.preferTarget && targetRunning()) return "target";
101
+ return group;
102
+ }
103
+
104
+ let busy = false;
105
+
106
+ /** Runs a point's action unless another one of this resource still runs; a returned promise blocks until it settles. */
107
+ export function runPress(allowed: () => boolean, action: () => unknown): void {
108
+ if (busy || !allowed()) return;
109
+ let result: unknown;
110
+ try {
111
+ result = action();
112
+ } catch (error) {
113
+ return log.error("point onPressed failed", error);
114
+ }
115
+ if (!(result instanceof Promise)) return;
116
+ busy = true;
117
+ result
118
+ .catch((error: unknown) => log.error("point onPressed failed", error))
119
+ .finally(() => (busy = false));
120
+ }
121
+
122
+ /** A label or a label function, read safely. */
123
+ export function readLabel(label: string | (() => string)): string {
124
+ if (typeof label === "string") return label;
125
+ try {
126
+ return label();
127
+ } catch (error) {
128
+ log.error("point label failed", error);
129
+ return "";
130
+ }
131
+ }
132
+
133
+ /** A `canShow`, read safely (absent = allowed). */
134
+ export function readAllowed(
135
+ canShow: ((entity: number) => boolean) | undefined,
136
+ entity = 0,
137
+ ): boolean {
138
+ try {
139
+ return canShow?.(entity) ?? true;
140
+ } catch (error) {
141
+ log.error("point canShow failed", error);
142
+ return false;
143
+ }
144
+ }
@@ -0,0 +1,117 @@
1
+ import { LIB_UI } from "@aifivem/shared";
2
+ import { callExport, isResourceStarted } from "../../fivem/exports";
3
+ import type { ModelScanDef } from "../modelScan/def";
4
+ import { ModelScanner } from "../modelScan/scanner";
5
+
6
+ /**
7
+ * Prompt-mode model points are scanned by ai-lib for every resource, like zones (`zones.ts`): FiveM enters a
8
+ * resource's JS every frame while it has any timer, so one scan for all costs one resource's frame time instead of
9
+ * every resource's. ai-lib tells the owner which object is nearest when it changes; label, `canShow` and the action
10
+ * stay here. A resource scans its own points only while ai-lib does not run and hands them back when it starts.
11
+ */
12
+ const RESOURCE = GetCurrentResourceName();
13
+ const HOST = LIB_UI.resource;
14
+
15
+ class Entry {
16
+ entity = 0;
17
+ constructor(
18
+ readonly key: string,
19
+ readonly def: ModelScanDef,
20
+ readonly onNearest: (entity: number) => void,
21
+ ) {}
22
+ }
23
+
24
+ const entries = new Map<string, Entry>();
25
+ const local = new ModelScanner();
26
+ let hosted = false;
27
+ /** ai-lib reads started but its exports are not there yet (client loading): wait for its start event. */
28
+ let waiting = false;
29
+
30
+ function setNearest(entry: Entry, entity: number): void {
31
+ if (entry.entity === entity) return;
32
+ entry.entity = entity;
33
+ entry.onNearest(entity);
34
+ }
35
+
36
+ function addLocal(entry: Entry): void {
37
+ local.add(
38
+ entry.key,
39
+ entry.def,
40
+ (entity) => setNearest(entry, entity),
41
+ entry.entity,
42
+ );
43
+ }
44
+
45
+ /** Hands one point to ai-lib; false while it cannot take it (not started, or its exports not there yet). */
46
+ function addHosted(entry: Entry): boolean {
47
+ try {
48
+ callExport(HOST, "AddModelPoint", entry.key, entry.def, entry.entity);
49
+ return true;
50
+ } catch (error) {
51
+ if (!String(error).includes("No such export")) throw error;
52
+ waiting = true;
53
+ return false;
54
+ }
55
+ }
56
+
57
+ /** ai-lib takes every point over; the local scan stops. */
58
+ function handOver(): void {
59
+ if (RESOURCE === HOST || waiting || !isResourceStarted(HOST)) return;
60
+ for (const entry of entries.values())
61
+ if (!addHosted(entry)) return takeBack();
62
+ hosted = true;
63
+ for (const key of entries.keys()) local.remove(key);
64
+ }
65
+
66
+ /** ai-lib is gone: this resource scans its points again, from the state ai-lib last reported. */
67
+ function takeBack(): void {
68
+ hosted = false;
69
+ for (const entry of entries.values()) addLocal(entry);
70
+ }
71
+
72
+ if (RESOURCE !== HOST) {
73
+ on(
74
+ LIB_UI.modelPointEvent,
75
+ (owner: unknown, key: unknown, entity: unknown) => {
76
+ if (owner !== RESOURCE || typeof key !== "string" || !hosted) return;
77
+ const entry = entries.get(key);
78
+ if (entry)
79
+ setNearest(entry, Number.isInteger(entity) ? (entity as number) : 0);
80
+ },
81
+ );
82
+ on("onClientResourceStart", (resource: string) => {
83
+ if (resource !== HOST) return;
84
+ waiting = false;
85
+ handOver();
86
+ });
87
+ on("onClientResourceStop", (resource: string) => {
88
+ if (resource === HOST && hosted) takeBack();
89
+ });
90
+ }
91
+
92
+ /** Starts scanning for a model point (`key` unique in this resource); `onNearest` gets each change (0 = none near). */
93
+ export function scanModelPoint(
94
+ key: string,
95
+ def: ModelScanDef,
96
+ onNearest: (entity: number) => void,
97
+ ): void {
98
+ stopModelPoint(key);
99
+ const entry = new Entry(key, def, onNearest);
100
+ entries.set(key, entry);
101
+ if (!hosted) {
102
+ handOver();
103
+ if (!hosted) return addLocal(entry);
104
+ }
105
+ if (!addHosted(entry)) takeBack();
106
+ }
107
+
108
+ /** Stops scanning for a model point (no last `onNearest`). */
109
+ export function stopModelPoint(key: string): void {
110
+ if (!entries.delete(key)) return;
111
+ if (!hosted) return local.remove(key);
112
+ try {
113
+ callExport(HOST, "RemoveModelPoint", key);
114
+ } catch {
115
+ // ai-lib stopped meanwhile: its points went with it.
116
+ }
117
+ }
@@ -0,0 +1,181 @@
1
+ import { every } from "../../fivem/scheduler/every";
2
+ import {
3
+ activeTarget,
4
+ pointStyle,
5
+ readAllowed,
6
+ readLabel,
7
+ register,
8
+ runPress,
9
+ unregister,
10
+ type Registration,
11
+ type Style,
12
+ } from "./interact";
13
+ import { scanModelPoint, stopModelPoint } from "./modelHost";
14
+ import { hidePrompt, showPrompt } from "../prompt";
15
+ import type { TargetOption } from "./targets/adapter";
16
+
17
+ export interface ModelPointOptions {
18
+ /** World objects this point is on (ATMs, vending machines, dumpsters). */
19
+ models: Array<string | number>;
20
+ /** The models are vehicles (boats, trucks): the prompt scan looks at nearby vehicles instead of objects. */
21
+ vehicles?: boolean;
22
+ label: string | (() => string);
23
+ icon?: string;
24
+ /** How close to the object, in metres (default 1.5). */
25
+ radius?: number;
26
+ /** Object points are skipped while the player sits in a vehicle (no scan, no prompt) unless this is true. */
27
+ inVehicle?: boolean;
28
+ /** A target option whenever a target resource runs, whatever the owner picked (seats); else the prompt. */
29
+ preferTarget?: boolean;
30
+ /** Gets the object in question (0 when unknown). */
31
+ canShow?: (entity: number) => boolean;
32
+ /** Gets the object used. A returned promise blocks this resource's other points until it settles. */
33
+ onPressed: (entity: number) => unknown;
34
+ }
35
+
36
+ /** A point on every vehicle (`Points.addVehicle`): the target's global vehicle option, or a prompt at the nearest one. */
37
+ export type VehiclePointOptions = Omit<
38
+ ModelPointOptions,
39
+ "models" | "vehicles"
40
+ >;
41
+
42
+ const RESOURCE = GetCurrentResourceName();
43
+ /** Prompt-mode points with an object near the player: `canShow` and the label are read again while they stay near. */
44
+ const near = new Set<ModelPoint>();
45
+ const recheck = every(
46
+ 500,
47
+ () => {
48
+ for (const point of near) point.sync();
49
+ },
50
+ { active: false, name: "modelPrompts" },
51
+ );
52
+
53
+ /**
54
+ * A point on every world object of some models: a target option on the models in target mode, otherwise a prompt
55
+ * while the player is near one of them. The scan for the nearest object runs in ai-lib for every resource
56
+ * (`modelHost.ts`); this resource only reads `canShow` and the label while one is near.
57
+ */
58
+ export class ModelPoint {
59
+ private readonly hashes: number[];
60
+ private entity = 0;
61
+ private shown: string | null = null;
62
+ private registration: Registration | null = null;
63
+ private scanning = false;
64
+ /** Every networked vehicle, whatever its model (`Points.addVehicle`). */
65
+ private readonly global: boolean;
66
+
67
+ constructor(
68
+ readonly promptId: string,
69
+ readonly options: ModelPointOptions,
70
+ ) {
71
+ this.hashes = options.models.map((m) =>
72
+ typeof m === "number" ? m : GetHashKey(m),
73
+ );
74
+ this.global = options.vehicles === true && options.models.length === 0;
75
+ }
76
+
77
+ attach(mode: Style): void {
78
+ const target = activeTarget();
79
+ if (target && pointStyle(this.options, mode) === "target") {
80
+ const option = this.targetOption();
81
+ const { models } = this.options;
82
+ this.registration = this.global
83
+ ? register(
84
+ target,
85
+ (a) => a.addGlobalVehicle(option),
86
+ (a) => a.removeGlobalVehicle(option),
87
+ )
88
+ : register(
89
+ target,
90
+ (a) => a.addModels(models, option),
91
+ (a) => a.removeModels(models, option),
92
+ );
93
+ // Not ready yet (client still loading): a prompt until the target starts and restyles every point.
94
+ if (this.registration) return;
95
+ }
96
+ this.scanning = true;
97
+ scanModelPoint(
98
+ this.promptId,
99
+ {
100
+ models: this.hashes,
101
+ vehicles: this.options.vehicles === true,
102
+ radius: this.options.radius ?? 1.5,
103
+ inVehicle: this.options.inVehicle === true,
104
+ },
105
+ (entity) => this.nearest(entity),
106
+ );
107
+ }
108
+
109
+ detach(): void {
110
+ unregister(this.registration);
111
+ this.registration = null;
112
+ if (this.scanning) stopModelPoint(this.promptId);
113
+ this.scanning = false;
114
+ this.nearest(0);
115
+ }
116
+
117
+ /** Puts the point on the current style; a prompt that stays a prompt keeps its scan (no flicker on a target restart). */
118
+ restyle(mode: Style): void {
119
+ const wantsTarget =
120
+ activeTarget() !== null && pointStyle(this.options, mode) === "target";
121
+ if (this.scanning && !wantsTarget) return;
122
+ this.detach();
123
+ this.attach(mode);
124
+ }
125
+
126
+ /** `refresh()`: a prompt reads `canShow` and the label again, a target option with a label function is put on again. */
127
+ refresh(mode: Style): void {
128
+ if (this.scanning) return this.sync();
129
+ if (this.registration && typeof this.options.label === "function") {
130
+ this.detach();
131
+ this.attach(mode);
132
+ }
133
+ }
134
+
135
+ press(): void {
136
+ if (this.entity) this.run(this.entity);
137
+ }
138
+
139
+ /** The prompt follows the nearest object, its `canShow` and the current label. */
140
+ sync(): void {
141
+ const entity = this.entity;
142
+ this.show(
143
+ entity && readAllowed(this.options.canShow, entity)
144
+ ? readLabel(this.options.label)
145
+ : null,
146
+ );
147
+ }
148
+
149
+ private nearest(entity: number): void {
150
+ this.entity = entity;
151
+ if (entity) near.add(this);
152
+ else near.delete(this);
153
+ recheck.set(near.size > 0);
154
+ this.sync();
155
+ }
156
+
157
+ private targetOption(): TargetOption {
158
+ return {
159
+ name: `${RESOURCE}:${this.promptId}`,
160
+ label: readLabel(this.options.label),
161
+ icon: this.options.icon,
162
+ distance: this.options.radius ?? 1.5,
163
+ canInteract: (entity) => readAllowed(this.options.canShow, entity),
164
+ onSelect: (entity) => this.run(entity),
165
+ };
166
+ }
167
+
168
+ private run(entity: number): void {
169
+ runPress(
170
+ () => readAllowed(this.options.canShow, entity),
171
+ () => this.options.onPressed(entity),
172
+ );
173
+ }
174
+
175
+ private show(text: string | null): void {
176
+ if (text === this.shown) return;
177
+ this.shown = text;
178
+ if (text !== null) showPrompt(this.promptId, text);
179
+ else hidePrompt(this.promptId);
180
+ }
181
+ }
@@ -0,0 +1,159 @@
1
+ import {
2
+ activeTarget,
3
+ pointStyle,
4
+ readAllowed,
5
+ readLabel,
6
+ register,
7
+ runPress,
8
+ unregister,
9
+ type Registration,
10
+ type Style,
11
+ } from "./interact";
12
+ import type { BlipOptions } from "../blip";
13
+ import type { LocalPed, LocalPedOptions } from "../localPed";
14
+ import { hidePrompt, showPrompt } from "../prompt";
15
+ import type { PromptAnchor } from "@aifivem/shared";
16
+ import type { TargetOption } from "./targets/adapter";
17
+
18
+ interface Vec3 {
19
+ x: number;
20
+ y: number;
21
+ z: number;
22
+ }
23
+
24
+ export interface PointOptions {
25
+ coords: Vec3;
26
+ /** The prompt text or target option label (already translated); a function is read again on enter and `refresh()` (text that follows a state). */
27
+ label: string | (() => string);
28
+ /** Font Awesome name of the target option (`fa-solid fa-store` or `store`). */
29
+ icon?: string;
30
+ /** How close the player must be, in metres (default 2). */
31
+ radius?: number;
32
+ /** A cylinder instead of a ball: metres up and down from `coords` (rooms, stations, floors above). */
33
+ height?: number;
34
+ /** Target mode: size of the spot players aim at, in metres (default 1; a room-sized point wants more). */
35
+ targetRadius?: number;
36
+ /** Always a prompt, whatever the owner picked: actions done from a vehicle (fuel pumps, car washes, drive-ins). */
37
+ promptOnly?: boolean;
38
+ /** A target option whenever a target resource runs, whatever the owner picked (seats); else the prompt. */
39
+ preferTarget?: boolean;
40
+ /** As a prompt: drawn over `coords` (or the point / entity spots a function gives when it shows) in the world instead of the right-hand list (doors). */
41
+ world?: boolean | (() => PromptAnchor);
42
+ /** Zone check interval in ms (default 500). */
43
+ interval?: number;
44
+ /** Whether the point is offered now (job, duty, in a vehicle...). Prompts re-check it on `refresh()`. */
45
+ canShow?: () => boolean;
46
+ /** The player used the point. A returned promise blocks this resource's other points until it settles. */
47
+ onPressed: () => unknown;
48
+ /** Inside the radius / left it, whatever the interaction style (markers, previews). */
49
+ onEnter?: () => void;
50
+ onExit?: () => void;
51
+ /** A ped standing at the point (at `coords` unless it has its own); targets go on the ped. */
52
+ ped?: Omit<LocalPedOptions, "coords" | "onSpawn" | "onDespawn"> & {
53
+ coords?: LocalPedOptions["coords"];
54
+ };
55
+ /** A map blip at the point (or at its own `coords`); its `label` is the map name (the place's name, not the prompt text). */
56
+ blip?: Omit<BlipOptions, "coords"> & { coords?: Vec3 };
57
+ }
58
+
59
+ const RESOURCE = GetCurrentResourceName();
60
+
61
+ export class Point {
62
+ inside = false;
63
+ ped: LocalPed | null = null;
64
+ /** The target option while the point is on a target (a spot, or its ped). */
65
+ registration: Registration | null = null;
66
+ /** The prompt text on screen, null while hidden. */
67
+ shown: string | null = null;
68
+ /** The label the target option was registered with. */
69
+ targetLabel: string | null = null;
70
+
71
+ constructor(
72
+ readonly promptId: string,
73
+ readonly options: PointOptions,
74
+ ) {}
75
+
76
+ text(): string {
77
+ return readLabel(this.options.label);
78
+ }
79
+
80
+ allowed(): boolean {
81
+ return readAllowed(this.options.canShow);
82
+ }
83
+
84
+ press(): void {
85
+ runPress(() => this.allowed(), this.options.onPressed);
86
+ }
87
+
88
+ /** The prompt follows inside + canShow + the current label (prompt mode only). */
89
+ sync(group: Style): void {
90
+ const text =
91
+ this.style(group) === "prompt" && this.inside && this.allowed()
92
+ ? this.text()
93
+ : null;
94
+ if (text === this.shown) return;
95
+ this.shown = text;
96
+ if (text === null) return hidePrompt(this.promptId);
97
+ const { world } = this.options;
98
+ showPrompt(
99
+ this.promptId,
100
+ text,
101
+ typeof world === "function"
102
+ ? world()
103
+ : world
104
+ ? this.options.coords
105
+ : undefined,
106
+ );
107
+ }
108
+
109
+ style(group: Style): Style {
110
+ const mode = pointStyle(this.options, group);
111
+ // A target that cannot take an option at a bare point (interact from JavaScript) leaves such points prompts.
112
+ return mode === "target" &&
113
+ !this.options.ped &&
114
+ activeTarget()?.adapter.coords === false
115
+ ? "prompt"
116
+ : mode;
117
+ }
118
+
119
+ targetOption(): TargetOption {
120
+ this.targetLabel = this.text();
121
+ return {
122
+ name: `${RESOURCE}:${this.promptId}`,
123
+ label: this.targetLabel,
124
+ icon: this.options.icon,
125
+ distance: this.options.radius ?? 2,
126
+ canInteract: () => this.allowed(),
127
+ onSelect: () => this.press(),
128
+ };
129
+ }
130
+
131
+ /** Puts the target option on (a spot, or its ped) while the point is in target mode. */
132
+ attach(mode: Style): void {
133
+ const target = activeTarget();
134
+ if (!target || this.registration || this.style(mode) !== "target") return;
135
+ const option = this.targetOption();
136
+ if (this.ped) {
137
+ const ped = this.ped.entity;
138
+ if (ped)
139
+ this.registration = register(
140
+ target,
141
+ (a) => a.addLocalEntity(ped, option),
142
+ (a) => a.removeLocalEntity(ped, option),
143
+ );
144
+ return;
145
+ }
146
+ // Far points get their target zone too: ai-target only scans zones near the player.
147
+ const { coords, targetRadius } = this.options;
148
+ this.registration = register(
149
+ target,
150
+ (a) => a.addSphere(coords, targetRadius ?? 1, option),
151
+ (a, handle) => a.removeZone(handle, option),
152
+ );
153
+ }
154
+
155
+ detach(): void {
156
+ unregister(this.registration);
157
+ this.registration = null;
158
+ }
159
+ }