Skip to content

Commit

Permalink
feat(colandreas)!: please wait samp-node callNative return retval
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Feb 11, 2024
1 parent 61493ef commit fb668d9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 167 deletions.
1 change: 1 addition & 0 deletions packages/colandreas/src/definitions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const MAX_CA_OBJECTS = 50000;
export const WATER_OBJECT = 20000;
export const FLOAT_INFINITY = 0x7f800000;
export const MAX_MULTICAST_SIZE = 99;
12 changes: 6 additions & 6 deletions packages/colandreas/src/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ function isInWater(instance: Player | Vehicle) {
if (!pos) return false;
const { x, y, z } = pos;

const ret = rayCastMultiLine(x, y, z + 1000.0, x, y, z - 1000.0, 10);
const {
collisions,
z: retZ,
modelIds,
} = rayCastMultiLine(x, y, z + 1000.0, x, y, z - 1000.0, 10);

if (!ret) return false;

const { collisions, z: retZ, modelIds } = ret;
if (collisions <= 0) return false;

let depth = FLOAT_INFINITY;
let instanceDepth = FLOAT_INFINITY;

if (collisions <= 0) return false;

for (let i = 0, j = 0; i < collisions; i++) {
if (modelIds[i] !== WATER_OBJECT) continue;

Expand Down
217 changes: 56 additions & 161 deletions packages/colandreas/src/natives/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MAX_MULTICAST_SIZE } from "../definitions";

export function init() {
return Boolean(samp.callNative("CA_Init", ""));
}
Expand Down Expand Up @@ -34,20 +36,7 @@ export function rayCastLine(
endY: number,
endZ: number
) {
const result = samp.callNative(
"CA_RayCastLine",
"ffffff",
startX,
startY,
startZ,
endX,
endY,
endZ
) as number;

if (result === 0) return null;

const [x, y, z] = samp.callNative(
const [x, y, z, result] = samp.callNative(
"CA_RayCastLine",
"ffffffFFF",
startX,
Expand All @@ -57,6 +46,9 @@ export function rayCastLine(
endY,
endZ
) as number[];

if (!result) return null;

return { result, x, y, z };
}

Expand All @@ -68,20 +60,7 @@ export function rayCastLineID(
endY: number,
endZ: number
) {
const result = samp.callNative(
"CA_RayCastLineID",
"ffffff",
startX,
startY,
startZ,
endX,
endY,
endZ
) as number;

if (result === 0) return null;

const [x, y, z] = samp.callNative(
const [x, y, z, result] = samp.callNative(
"CA_RayCastLineID",
"ffffffFFF",
startX,
Expand All @@ -91,6 +70,9 @@ export function rayCastLineID(
endY,
endZ
) as number[];

if (!result) return null;

return { result, x, y, z };
}

Expand All @@ -103,21 +85,7 @@ export function rayCastLineExtraID(
endY: number,
endZ: number
) {
const result = samp.callNative(
"CA_RayCastLineExtraID",
"iffffff",
type,
startX,
startY,
startZ,
endX,
endY,
endZ
) as number;

if (result === 0) return null;

const [x, y, z] = samp.callNative(
const [x, y, z, result] = samp.callNative(
"CA_RayCastLineExtraID",
"iffffffFFF",
type,
Expand All @@ -128,6 +96,9 @@ export function rayCastLineExtraID(
endY,
endZ
) as number[];

if (!result) return null;

return { result, x, y, z };
}

Expand All @@ -138,43 +109,29 @@ export function rayCastMultiLine(
endX: number,
endY: number,
endZ: number,
size: number
size = MAX_MULTICAST_SIZE
) {
const collisions = samp.callNative(
"CA_RayCastMultiLine",
"ffffff",
startX,
startY,
startZ,
endX,
endY,
endZ
) as number;

const [x, y, z, dist, modelIds] = samp.callNative(
const [x, y, z, dist, modelIds, result] = samp.callNative(
"CA_RayCastMultiLine",
"ffffffAiAiAiAiAii",
"ffffffVVVVAi",
startX,
startY,
startZ,
endX,
endY,
endZ,
size,
size,
size,
size,
size,
size
) as number[][];
) as [number[], number[], number[], number[], number[], number];

const len = result > 0 ? result : 0;

return {
collisions,
x,
y,
z,
dist,
modelIds,
collisions: result,
x: x.slice(0, len),
y: y.slice(0, len),
z: z.slice(0, len),
dist: dist.slice(0, len),
modelIds: modelIds.slice(0, len),
};
}

Expand All @@ -186,20 +143,7 @@ export function rayCastLineAngle(
endY: number,
endZ: number
) {
const result = samp.callNative(
"CA_RayCastLineAngle",
"ffffff",
startX,
startY,
startZ,
endX,
endY,
endZ
) as number;

if (result === 0) return null;

const [x, y, z, rx, ry, rz] = samp.callNative(
const [x, y, z, rx, ry, rz, result] = samp.callNative(
"CA_RayCastLineAngle",
"ffffffFFFFFF",
startX,
Expand All @@ -210,6 +154,8 @@ export function rayCastLineAngle(
endZ
) as number[];

if (!result) return null;

return { result, x, y, z, rx, ry, rz };
}

Expand All @@ -221,20 +167,7 @@ export function rayCastReflectionVector(
endY: number,
endZ: number
) {
const result = samp.callNative(
"CA_RayCastReflectionVector",
"ffffff",
startX,
startY,
startZ,
endX,
endY,
endZ
) as number;

if (result === 0) return null;

const [x, y, z, nx, ny, nz] = samp.callNative(
const [x, y, z, nx, ny, nz, result] = samp.callNative(
"CA_RayCastReflectionVector",
"ffffffFFFFFF",
startX,
Expand All @@ -245,6 +178,8 @@ export function rayCastReflectionVector(
endZ
) as number[];

if (!result) return null;

return { result, x, y, z, nx, ny, nz };
}

Expand All @@ -256,20 +191,7 @@ export function rayCastLineNormal(
endY: number,
endZ: number
) {
const result = samp.callNative(
"CA_RayCastLineNormal",
"ffffff",
startX,
startY,
startZ,
endX,
endY,
endZ
) as number;

if (result === 0) return null;

const [x, y, z, nx, ny, nz] = samp.callNative(
const [x, y, z, nx, ny, nz, result] = samp.callNative(
"CA_RayCastLineNormal",
"ffffffFFFFFF",
startX,
Expand All @@ -280,6 +202,8 @@ export function rayCastLineNormal(
endZ
) as number[];

if (!result) return null;

return { result, x, y, z, nx, ny, nz };
}

Expand Down Expand Up @@ -321,34 +245,26 @@ export function quatToEuler(x: number, y: number, z: number, w: number) {
}

export function getModelBoundingSphere(modelId: number) {
const result = Boolean(
samp.callNative("CA_GetModelBoundingSphere", "i", modelId)
);

if (!result) return null;

const [offX, offY, offZ, radius] = samp.callNative(
const [offX, offY, offZ, radius, result] = samp.callNative(
"CA_GetModelBoundingSphere",
"iFFFF",
modelId
) as number[];

if (!result) return null;

return { offX, offY, offZ, radius };
}

export function getModelBoundingBox(modelId: number) {
const result = Boolean(
samp.callNative("CA_GetModelBoundingBox", "i", modelId)
);

if (!result) return null;

const [minX, minY, minZ, maxX, maxY, maxZ] = samp.callNative(
const [minX, minY, minZ, maxX, maxY, maxZ, result] = samp.callNative(
"CA_GetModelBoundingBox",
"iFFFFFF",
modelId
) as number[];

if (!result) return null;

return { minX, minY, minZ, maxX, maxY, maxZ };
}

Expand All @@ -360,20 +276,7 @@ export function rayCastLineEx(
endY: number,
endZ: number
) {
const result = samp.callNative(
"CA_RayCastLineEx",
"ffffff",
startX,
startY,
startZ,
endX,
endY,
endZ
);

if (result === 0) return null;

const [x, y, z, rx, ry, rz, rw, cx, cy, cz] = samp.callNative(
const [x, y, z, rx, ry, rz, rw, cx, cy, cz, result] = samp.callNative(
"CA_RayCastLineEx",
"ffffffFFFFFFFFFF",
startX,
Expand All @@ -384,6 +287,8 @@ export function rayCastLineEx(
endZ
) as number[];

if (!result) return null;

return { x, y, z, rx, ry, rz, rw, cx, cy, cz };
}

Expand All @@ -395,29 +300,19 @@ export function rayCastLineAngleEx(
endY: number,
endZ: number
) {
const result = samp.callNative(
"CA_RayCastLineAngleEx",
"ffffff",
startX,
startY,
startZ,
endX,
endY,
endZ
);
const [x, y, z, rx, ry, rz, ocx, ocy, ocz, orx, ory, orz, result] =
samp.callNative(
"CA_RayCastLineAngleEx",
"ffffffFFFFFFFFFFFF",
startX,
startY,
startZ,
endX,
endY,
endZ
) as number[];

if (result === 0) return null;

const [x, y, z, rx, ry, rz, ocx, ocy, ocz, orx, ory, orz] = samp.callNative(
"CA_RayCastLineAngleEx",
"ffffffFFFFFFFFFFFF",
startX,
startY,
startZ,
endX,
endY,
endZ
) as number[];
if (!result) return null;

return { x, y, z, rx, ry, rz, ocx, ocy, ocz, orx, ory, orz };
}
Expand Down

0 comments on commit fb668d9

Please sign in to comment.