@aifivem/lib 0.6.0 → 0.8.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.
- package/package.json +3 -3
- package/src/client/action.ts +38 -38
- package/src/client/callback.ts +70 -70
- package/src/client/controls.ts +73 -73
- package/src/client/entity.ts +15 -15
- package/src/client/headshot.ts +26 -26
- package/src/client/index.ts +2 -5
- package/src/client/localPed.ts +169 -169
- package/src/client/location.ts +54 -54
- package/src/client/netEvent.ts +40 -40
- package/src/client/nuiScreen.ts +87 -87
- package/src/client/phone.ts +14 -0
- package/src/client/players.ts +34 -34
- package/src/client/raycast.ts +48 -48
- package/src/client/shot.ts +29 -29
- package/src/client/vehicleProps.ts +107 -107
- package/src/fivem/callbackNames.ts +12 -12
- package/src/fivem/locale.ts +57 -57
- package/src/server/callback.ts +112 -112
- package/src/server/characters.ts +12 -3
- package/src/server/dataFile.ts +81 -81
- package/src/server/illegal.ts +26 -0
- package/src/server/index.ts +2 -0
- package/src/server/mdt.ts +13 -0
- package/src/server/netEvent.ts +58 -58
- package/src/server/phone.ts +11 -5
- package/src/server/position.ts +20 -20
- package/src/server/rateLimit.ts +53 -53
- package/src/server/stress.ts +4 -1
- package/src/server/warrants.ts +37 -0
- package/src/shared/format.ts +26 -26
- package/src/shared/grid.ts +20 -20
- package/src/shared/index.ts +151 -151
- package/src/shared/lang.ts +39 -39
- package/src/shared/read.ts +60 -60
- package/src/shared/strict.ts +70 -70
- package/src/shared/vec.ts +19 -19
- package/src/shared/vehicleProps.ts +138 -138
- package/src/shared/wait.ts +27 -27
package/src/server/netEvent.ts
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { createLogger } from "../shared";
|
|
2
|
-
import {
|
|
3
|
-
ClientGate,
|
|
4
|
-
type ClientInputOptions,
|
|
5
|
-
type ParsedInputOptions,
|
|
6
|
-
} from "./gate";
|
|
7
|
-
|
|
8
|
-
const log = createLogger(GetCurrentResourceName());
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A net event a client sends: the only way an ai-* server listens to clients (never bare `onNet`). The source is
|
|
12
|
-
* read first, then the gate runs (real player, rate, guards, `args` parser, optional lock) and only then the
|
|
13
|
-
* handler. A handler that takes arguments gets them only through `args` (options come before it then), so
|
|
14
|
-
* unchecked client data never reaches it. A thrown error is logged, never sent back.
|
|
15
|
-
*
|
|
16
|
-
* onClientEvent("ai-shop:close", (source) => shop.close(source));
|
|
17
|
-
* onClientEvent(
|
|
18
|
-
* "ai-shop:buy",
|
|
19
|
-
* { args: ([item, count]) => (typeof item === "string" && isCount(count) ? [item, count] : null), guards: [hasCharacter()], lock: true },
|
|
20
|
-
* (source, item, count) => shop.buy(source, item, count),
|
|
21
|
-
* );
|
|
22
|
-
*/
|
|
23
|
-
export function onClientEvent(
|
|
24
|
-
name: string,
|
|
25
|
-
handler: (source: number) => unknown,
|
|
26
|
-
options?: ClientInputOptions,
|
|
27
|
-
): void;
|
|
28
|
-
export function onClientEvent<const A extends readonly unknown[]>(
|
|
29
|
-
name: string,
|
|
30
|
-
options: ParsedInputOptions<A>,
|
|
31
|
-
handler: (source: number, ...args: A) => unknown,
|
|
32
|
-
): void;
|
|
33
|
-
export function onClientEvent(
|
|
34
|
-
name: string,
|
|
35
|
-
first: ((source: number) => unknown) | ParsedInputOptions<readonly unknown[]>,
|
|
36
|
-
second?:
|
|
37
|
-
| ClientInputOptions
|
|
38
|
-
| ((source: number, ...args: readonly unknown[]) => unknown),
|
|
39
|
-
): void {
|
|
40
|
-
const [handler, options] =
|
|
41
|
-
typeof first === "function"
|
|
42
|
-
? [first, (second ?? {}) as ClientInputOptions]
|
|
43
|
-
: [
|
|
44
|
-
second as (source: number, ...args: readonly unknown[]) => unknown,
|
|
45
|
-
first,
|
|
46
|
-
];
|
|
47
|
-
const gate = new ClientGate(name, options, 10);
|
|
48
|
-
onNet(name, (...raw: unknown[]) => {
|
|
49
|
-
const source = Number(globalThis.source);
|
|
50
|
-
const args = gate.check(source, raw);
|
|
51
|
-
if (!args) return;
|
|
52
|
-
gate
|
|
53
|
-
.run(source, undefined, () => handler(source, ...args))
|
|
54
|
-
.catch((error: unknown) =>
|
|
55
|
-
log.error(`event ${name} failed for ${source}`, error),
|
|
56
|
-
);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
1
|
+
import { createLogger } from "../shared";
|
|
2
|
+
import {
|
|
3
|
+
ClientGate,
|
|
4
|
+
type ClientInputOptions,
|
|
5
|
+
type ParsedInputOptions,
|
|
6
|
+
} from "./gate";
|
|
7
|
+
|
|
8
|
+
const log = createLogger(GetCurrentResourceName());
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A net event a client sends: the only way an ai-* server listens to clients (never bare `onNet`). The source is
|
|
12
|
+
* read first, then the gate runs (real player, rate, guards, `args` parser, optional lock) and only then the
|
|
13
|
+
* handler. A handler that takes arguments gets them only through `args` (options come before it then), so
|
|
14
|
+
* unchecked client data never reaches it. A thrown error is logged, never sent back.
|
|
15
|
+
*
|
|
16
|
+
* onClientEvent("ai-shop:close", (source) => shop.close(source));
|
|
17
|
+
* onClientEvent(
|
|
18
|
+
* "ai-shop:buy",
|
|
19
|
+
* { args: ([item, count]) => (typeof item === "string" && isCount(count) ? [item, count] : null), guards: [hasCharacter()], lock: true },
|
|
20
|
+
* (source, item, count) => shop.buy(source, item, count),
|
|
21
|
+
* );
|
|
22
|
+
*/
|
|
23
|
+
export function onClientEvent(
|
|
24
|
+
name: string,
|
|
25
|
+
handler: (source: number) => unknown,
|
|
26
|
+
options?: ClientInputOptions,
|
|
27
|
+
): void;
|
|
28
|
+
export function onClientEvent<const A extends readonly unknown[]>(
|
|
29
|
+
name: string,
|
|
30
|
+
options: ParsedInputOptions<A>,
|
|
31
|
+
handler: (source: number, ...args: A) => unknown,
|
|
32
|
+
): void;
|
|
33
|
+
export function onClientEvent(
|
|
34
|
+
name: string,
|
|
35
|
+
first: ((source: number) => unknown) | ParsedInputOptions<readonly unknown[]>,
|
|
36
|
+
second?:
|
|
37
|
+
| ClientInputOptions
|
|
38
|
+
| ((source: number, ...args: readonly unknown[]) => unknown),
|
|
39
|
+
): void {
|
|
40
|
+
const [handler, options] =
|
|
41
|
+
typeof first === "function"
|
|
42
|
+
? [first, (second ?? {}) as ClientInputOptions]
|
|
43
|
+
: [
|
|
44
|
+
second as (source: number, ...args: readonly unknown[]) => unknown,
|
|
45
|
+
first,
|
|
46
|
+
];
|
|
47
|
+
const gate = new ClientGate(name, options, 10);
|
|
48
|
+
onNet(name, (...raw: unknown[]) => {
|
|
49
|
+
const source = Number(globalThis.source);
|
|
50
|
+
const args = gate.check(source, raw);
|
|
51
|
+
if (!args) return;
|
|
52
|
+
gate
|
|
53
|
+
.run(source, undefined, () => handler(source, ...args))
|
|
54
|
+
.catch((error: unknown) =>
|
|
55
|
+
log.error(`event ${name} failed for ${source}`, error),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
}
|
package/src/server/phone.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CORE,
|
|
3
|
-
type PhoneMail,
|
|
4
|
-
type PhoneNotification,
|
|
5
|
-
} from "@aifivem/shared";
|
|
1
|
+
import { CORE, type PhoneMail, type PhoneNotification } from "@aifivem/shared";
|
|
6
2
|
import { resourceExport } from "../fivem/exports";
|
|
7
3
|
|
|
8
4
|
const core = resourceExport(CORE.resource);
|
|
@@ -19,4 +15,14 @@ export const phone = {
|
|
|
19
15
|
core.SendPhoneMessage(from, to, text),
|
|
20
16
|
notify: (source: number, notification: PhoneNotification): boolean =>
|
|
21
17
|
core.PhoneNotify(source, notification),
|
|
18
|
+
/**
|
|
19
|
+
* `fn(source, phone)` when a player with a loaded character opens the phone (`phone`: the phone's resource name):
|
|
20
|
+
* once per opening, at most once per 10 s per player; nothing for a phone without an open signal.
|
|
21
|
+
*/
|
|
22
|
+
onUsed(fn: (source: number, phone: string) => void): void {
|
|
23
|
+
on(CORE.phoneUsedEvent, (source: unknown, phone: unknown) => {
|
|
24
|
+
if (typeof source === "number" && typeof phone === "string")
|
|
25
|
+
fn(source, phone);
|
|
26
|
+
});
|
|
27
|
+
},
|
|
22
28
|
};
|
package/src/server/position.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type { Vec3 } from "../shared/vec";
|
|
2
|
-
|
|
3
|
-
/** An entity's position (server copy of the owner's sync). */
|
|
4
|
-
export function entityCoords(entity: number): Vec3 {
|
|
5
|
-
const [x = 0, y = 0, z = 0] = GetEntityCoords(entity);
|
|
6
|
-
return { x, y, z };
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/** A player's ped position. */
|
|
10
|
-
export const coordsOf = (source: number): Vec3 =>
|
|
11
|
-
entityCoords(GetPlayerPed(String(source)));
|
|
12
|
-
|
|
1
|
+
import type { Vec3 } from "../shared/vec";
|
|
2
|
+
|
|
3
|
+
/** An entity's position (server copy of the owner's sync). */
|
|
4
|
+
export function entityCoords(entity: number): Vec3 {
|
|
5
|
+
const [x = 0, y = 0, z = 0] = GetEntityCoords(entity);
|
|
6
|
+
return { x, y, z };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** A player's ped position. */
|
|
10
|
+
export const coordsOf = (source: number): Vec3 =>
|
|
11
|
+
entityCoords(GetPlayerPed(String(source)));
|
|
12
|
+
|
|
13
13
|
/** The vehicle the player sits in (with `driver: true` only when in its driver seat), 0 on foot. */
|
|
14
14
|
export function vehicleOf(
|
|
15
15
|
source: number,
|
|
@@ -21,11 +21,11 @@ export function vehicleOf(
|
|
|
21
21
|
return GetPedInVehicleSeat(vehicle, -1) === ped ? vehicle : 0;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
/** The player's ped is within `metres` of a point. */
|
|
25
|
-
export function isNear(source: number, point: Vec3, metres: number): boolean {
|
|
26
|
-
const [x = 0, y = 0, z = 0] = GetEntityCoords(GetPlayerPed(String(source)));
|
|
27
|
-
const dx = x - point.x;
|
|
28
|
-
const dy = y - point.y;
|
|
29
|
-
const dz = z - point.z;
|
|
30
|
-
return dx * dx + dy * dy + dz * dz <= metres * metres;
|
|
31
|
-
}
|
|
24
|
+
/** The player's ped is within `metres` of a point. */
|
|
25
|
+
export function isNear(source: number, point: Vec3, metres: number): boolean {
|
|
26
|
+
const [x = 0, y = 0, z = 0] = GetEntityCoords(GetPlayerPed(String(source)));
|
|
27
|
+
const dx = x - point.x;
|
|
28
|
+
const dy = y - point.y;
|
|
29
|
+
const dz = z - point.z;
|
|
30
|
+
return dx * dx + dy * dy + dz * dz <= metres * metres;
|
|
31
|
+
}
|
package/src/server/rateLimit.ts
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* One in-flight action per player. `run` returns `busy` instead of starting a second copy.
|
|
3
|
-
* Call `release(source)` from playerDropped.
|
|
4
|
-
*/
|
|
5
|
-
export class SourceLock {
|
|
6
|
-
private readonly active = new Set<number>();
|
|
7
|
-
|
|
8
|
-
async run<T, B>(
|
|
9
|
-
source: number,
|
|
10
|
-
busy: B,
|
|
11
|
-
task: () => Promise<T>,
|
|
12
|
-
): Promise<T | B> {
|
|
13
|
-
if (this.active.has(source)) return busy;
|
|
14
|
-
this.active.add(source);
|
|
15
|
-
try {
|
|
16
|
-
return await task();
|
|
17
|
-
} finally {
|
|
18
|
-
this.active.delete(source);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
release(source: number): void {
|
|
23
|
-
this.active.delete(source);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** Per-player burst limit: at most `count` actions inside any `windowMs`. Call `release(source)` from playerDropped. */
|
|
28
|
-
export class RateLimit {
|
|
29
|
-
private readonly hits = new Map<number, number[]>();
|
|
30
|
-
|
|
31
|
-
constructor(
|
|
32
|
-
private readonly count: number,
|
|
33
|
-
private readonly windowMs: number,
|
|
34
|
-
) {}
|
|
35
|
-
|
|
36
|
-
take(source: number): boolean {
|
|
37
|
-
const now = GetGameTimer();
|
|
38
|
-
const recent = (this.hits.get(source) ?? []).filter(
|
|
39
|
-
(at) => now - at < this.windowMs,
|
|
40
|
-
);
|
|
41
|
-
if (recent.length >= this.count) {
|
|
42
|
-
this.hits.set(source, recent);
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
recent.push(now);
|
|
46
|
-
this.hits.set(source, recent);
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
release(source: number): void {
|
|
51
|
-
this.hits.delete(source);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* One in-flight action per player. `run` returns `busy` instead of starting a second copy.
|
|
3
|
+
* Call `release(source)` from playerDropped.
|
|
4
|
+
*/
|
|
5
|
+
export class SourceLock {
|
|
6
|
+
private readonly active = new Set<number>();
|
|
7
|
+
|
|
8
|
+
async run<T, B>(
|
|
9
|
+
source: number,
|
|
10
|
+
busy: B,
|
|
11
|
+
task: () => Promise<T>,
|
|
12
|
+
): Promise<T | B> {
|
|
13
|
+
if (this.active.has(source)) return busy;
|
|
14
|
+
this.active.add(source);
|
|
15
|
+
try {
|
|
16
|
+
return await task();
|
|
17
|
+
} finally {
|
|
18
|
+
this.active.delete(source);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
release(source: number): void {
|
|
23
|
+
this.active.delete(source);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Per-player burst limit: at most `count` actions inside any `windowMs`. Call `release(source)` from playerDropped. */
|
|
28
|
+
export class RateLimit {
|
|
29
|
+
private readonly hits = new Map<number, number[]>();
|
|
30
|
+
|
|
31
|
+
constructor(
|
|
32
|
+
private readonly count: number,
|
|
33
|
+
private readonly windowMs: number,
|
|
34
|
+
) {}
|
|
35
|
+
|
|
36
|
+
take(source: number): boolean {
|
|
37
|
+
const now = GetGameTimer();
|
|
38
|
+
const recent = (this.hits.get(source) ?? []).filter(
|
|
39
|
+
(at) => now - at < this.windowMs,
|
|
40
|
+
);
|
|
41
|
+
if (recent.length >= this.count) {
|
|
42
|
+
this.hits.set(source, recent);
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
recent.push(now);
|
|
46
|
+
this.hits.set(source, recent);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
release(source: number): void {
|
|
51
|
+
this.hits.delete(source);
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/server/stress.ts
CHANGED
|
@@ -15,11 +15,14 @@ export const stress = {
|
|
|
15
15
|
provider: (): string | null => core.GetStressSystem(),
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
/** Players' hunger and thirst
|
|
18
|
+
/** Players' hunger and thirst, kept by ai-core (character metadata, 0-100); any HUD only draws them. */
|
|
19
19
|
export const needs = {
|
|
20
20
|
/** Hunger and thirst to full (admin revive, hospital); false when the player has no character. */
|
|
21
21
|
refill: (source: number): boolean => core.RefillNeeds(source),
|
|
22
22
|
/** Adds hunger or thirst (0-100 scale; a negative amount takes it away), a food or drink's value. */
|
|
23
23
|
add: (source: number, need: "hunger" | "thirst", amount: number): boolean =>
|
|
24
24
|
core.AddNeed(source, need, amount),
|
|
25
|
+
/** Sets hunger or thirst to a value (0-100, clamped); false when the player has no character. */
|
|
26
|
+
set: (source: number, need: "hunger" | "thirst", value: number): boolean =>
|
|
27
|
+
core.SetNeed(source, need, value),
|
|
25
28
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE,
|
|
3
|
+
type Warrant,
|
|
4
|
+
type WarrantAdd,
|
|
5
|
+
type WarrantPage,
|
|
6
|
+
type WarrantResult,
|
|
7
|
+
type WarrantTarget,
|
|
8
|
+
} from "@aifivem/shared";
|
|
9
|
+
import { resourceExport } from "../fivem/exports";
|
|
10
|
+
|
|
11
|
+
const core = resourceExport(CORE.resource);
|
|
12
|
+
|
|
13
|
+
/** A callback export as a promise (no promise crosses a resource boundary). */
|
|
14
|
+
const ask = <T>(call: (done: (value: T) => void) => void): Promise<T> =>
|
|
15
|
+
new Promise((resolve) => call(resolve));
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* ai-core's own warrants (by character, loaded or not; logged on ai-core's `warrants` channel). Whether they count in
|
|
19
|
+
* `mdt.hasWarrant` is ai-core's panel setting `warrants` (`docs/compat/mdt.md`).
|
|
20
|
+
*/
|
|
21
|
+
export const warrants = {
|
|
22
|
+
/** Issues a warrant (`minutes` null or left out = until cleared): `{ ok, id }` or `{ ok: false, error }` (ai-core lang key). */
|
|
23
|
+
add: (warrant: WarrantAdd): Promise<WarrantResult> =>
|
|
24
|
+
ask((done) => core.AddWarrant(warrant, done)),
|
|
25
|
+
/** Clears one active warrant; false when no active warrant has that id. */
|
|
26
|
+
clear: (id: number, by?: number | string): Promise<boolean> =>
|
|
27
|
+
ask((done) => core.ClearWarrant(id, by, done)),
|
|
28
|
+
/** Clears every active warrant of a character; resolves how many. */
|
|
29
|
+
clearAll: (target: WarrantTarget, by?: number | string): Promise<number> =>
|
|
30
|
+
ask((done) => core.ClearWarrants(target, by, done)),
|
|
31
|
+
/** A character's active warrants, newest first; null when the target names no character. */
|
|
32
|
+
of: (target: WarrantTarget): Promise<Warrant[] | null> =>
|
|
33
|
+
ask((done) => core.GetWarrants(target, done)),
|
|
34
|
+
/** Every active warrant, newest first, 25 a page (`cursor` = the last page's `next`). */
|
|
35
|
+
page: (cursor?: number | null): Promise<WarrantPage> =>
|
|
36
|
+
ask((done) => core.ListWarrants(cursor, done)),
|
|
37
|
+
};
|
package/src/shared/format.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
/** Rounds to `decimals` places (default 0). */
|
|
2
|
-
export function round(value: number, decimals = 0): number {
|
|
3
|
-
const power = 10 ** decimals;
|
|
4
|
-
return Math.round(value * power) / power;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/** `1234567` as `1,234,567` (`separator` default `,`); keeps a sign and decimals. */
|
|
8
|
-
export function groupDigits(value: number | string, separator = ","): string {
|
|
9
|
-
const [whole = "", fraction] = String(value).split(".");
|
|
10
|
-
const grouped = whole.replace(/\B(?=(\d{3})+(?!\d))/g, separator);
|
|
11
|
-
return fraction === undefined ? grouped : `${grouped}.${fraction}`;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Text from another script made plain: HTML tags, GTA colour and input codes (`~r~`, `~INPUT_PICKUP~`) and ` `
|
|
16
|
-
* removed, runs of spaces folded.
|
|
17
|
-
*/
|
|
18
|
-
export function stripGameText(text: unknown): string {
|
|
19
|
-
if (text === null || text === undefined) return "";
|
|
20
|
-
return String(text)
|
|
21
|
-
.replace(/<[^>]*>/g, "")
|
|
22
|
-
.replace(/~[\w]+~/g, "")
|
|
23
|
-
.replace(/ /g, " ")
|
|
24
|
-
.replace(/\s\s+/g, " ")
|
|
25
|
-
.trim();
|
|
26
|
-
}
|
|
1
|
+
/** Rounds to `decimals` places (default 0). */
|
|
2
|
+
export function round(value: number, decimals = 0): number {
|
|
3
|
+
const power = 10 ** decimals;
|
|
4
|
+
return Math.round(value * power) / power;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** `1234567` as `1,234,567` (`separator` default `,`); keeps a sign and decimals. */
|
|
8
|
+
export function groupDigits(value: number | string, separator = ","): string {
|
|
9
|
+
const [whole = "", fraction] = String(value).split(".");
|
|
10
|
+
const grouped = whole.replace(/\B(?=(\d{3})+(?!\d))/g, separator);
|
|
11
|
+
return fraction === undefined ? grouped : `${grouped}.${fraction}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Text from another script made plain: HTML tags, GTA colour and input codes (`~r~`, `~INPUT_PICKUP~`) and ` `
|
|
16
|
+
* removed, runs of spaces folded.
|
|
17
|
+
*/
|
|
18
|
+
export function stripGameText(text: unknown): string {
|
|
19
|
+
if (text === null || text === undefined) return "";
|
|
20
|
+
return String(text)
|
|
21
|
+
.replace(/<[^>]*>/g, "")
|
|
22
|
+
.replace(/~[\w]+~/g, "")
|
|
23
|
+
.replace(/ /g, " ")
|
|
24
|
+
.replace(/\s\s+/g, " ")
|
|
25
|
+
.trim();
|
|
26
|
+
}
|
|
27
27
|
|
|
28
28
|
/** A weight in grams for players: "750 g", "1.5 kg", "1,250 kg" (one decimal under 100 kg). */
|
|
29
29
|
export function formatWeight(grams: number): string {
|
package/src/shared/grid.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
/** Grid cells for world streaming (lib `AreaStream` / `AreaFollower`): the same math on server and client. */
|
|
2
|
-
|
|
3
|
-
/** Cells are clamped to +-16,000 so `cellKey` stays a small integer (a fast Map key). */
|
|
4
|
-
const LIMIT = 16_000;
|
|
5
|
-
const OFFSET = 16_384;
|
|
6
|
-
const clampCell = (v: number): number => Math.max(-LIMIT, Math.min(LIMIT, v));
|
|
7
|
-
|
|
8
|
-
/** The integer cell of a point on a grid of `size` metres. */
|
|
9
|
-
export const gridCell = (
|
|
10
|
-
x: number,
|
|
11
|
-
y: number,
|
|
12
|
-
size: number,
|
|
13
|
-
): { cx: number; cy: number } => ({
|
|
14
|
-
cx: clampCell(Math.floor(x / size)),
|
|
15
|
-
cy: clampCell(Math.floor(y / size)),
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
/** One number for a cell (below 2^30). */
|
|
19
|
-
export const cellKey = (cx: number, cy: number): number =>
|
|
20
|
-
(clampCell(cx) + OFFSET) * 32_768 + (clampCell(cy) + OFFSET);
|
|
1
|
+
/** Grid cells for world streaming (lib `AreaStream` / `AreaFollower`): the same math on server and client. */
|
|
2
|
+
|
|
3
|
+
/** Cells are clamped to +-16,000 so `cellKey` stays a small integer (a fast Map key). */
|
|
4
|
+
const LIMIT = 16_000;
|
|
5
|
+
const OFFSET = 16_384;
|
|
6
|
+
const clampCell = (v: number): number => Math.max(-LIMIT, Math.min(LIMIT, v));
|
|
7
|
+
|
|
8
|
+
/** The integer cell of a point on a grid of `size` metres. */
|
|
9
|
+
export const gridCell = (
|
|
10
|
+
x: number,
|
|
11
|
+
y: number,
|
|
12
|
+
size: number,
|
|
13
|
+
): { cx: number; cy: number } => ({
|
|
14
|
+
cx: clampCell(Math.floor(x / size)),
|
|
15
|
+
cy: clampCell(Math.floor(y / size)),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/** One number for a cell (below 2^30). */
|
|
19
|
+
export const cellKey = (cx: number, cy: number): number =>
|
|
20
|
+
(clampCell(cx) + OFFSET) * 32_768 + (clampCell(cy) + OFFSET);
|