Skip to content

Commit

Permalink
testing navmesh
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGruber committed Sep 19, 2024
1 parent 37aa7f3 commit ba3f752
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"fix-signatures": "npx ts-node ./scripts/fixSignatures.ts && npm run prettier_sources",
"gen-doc": "npm run updt-doc-config && npx typedoc",
"updt-doc-config": "npx ts-node ./scripts/updateTypeDoc.ts",
"start": "node --experimental-require-module ./scripts/h1z1-server-demo-2016.js",
"start": "node --no-warnings --experimental-require-module ./scripts/h1z1-server-demo-2016.js",
"start-dev": "npm run build && npm start",
"start-echo": "npm run build && npm run build-benchs && node --inspect ./benchmarks/out/echo/echo-server-start.js",
"lint": "npx eslint src",
Expand Down
9 changes: 9 additions & 0 deletions src/servers/ZoneServer2016/handlers/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ const dev: any = {
server._npcs[characterId] = zombie;
const e = new EntityFromJs(EntityType.Zombie, zombie);
server.aiManager.add_entity(e);
setInterval(() => {
const p = server.navigator.testNavMesh(
zombie.state.position,
client.character.state.position
);
if (p.length) {
p.forEach((v) => zombie.goTo(v));
}
}, 1000);
},
abilities: function (
server: ZoneServer2016,
Expand Down
9 changes: 9 additions & 0 deletions src/servers/ZoneServer2016/zoneserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ import { RewardManager } from "./managers/rewardmanager";
import { DynamicAppearance } from "types/zonedata";
import { AiManager, EntityFromJs, EntityType } from "h1emu-ai";

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / eslint

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / interfaces-up-to-date

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / prettier

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / linux-local (20.12.2)

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / linux-local (20.x)

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / mongo-linux (20.12.2)

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / linux-local (21.x)

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / npm-pack-linux (20.x)

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / mongo-linux (20.x)

Cannot find module 'h1emu-ai' or its corresponding type declarations.

Check failure on line 251 in src/servers/ZoneServer2016/zoneserver.ts

View workflow job for this annotation

GitHub Actions / win-local (20.x)

Cannot find module 'h1emu-ai' or its corresponding type declarations.
import { setInterval } from "node:timers";
import { readFileSync } from "node:fs";
import { Navig } from "../../utils/recast";

const spawnLocations2 = require("../../../data/2016/zoneData/Z1_gridSpawns.json"),
deprecatedDoors = require("../../../data/2016/sampleData/deprecatedDoors.json"),
Expand Down Expand Up @@ -463,6 +465,7 @@ export class ZoneServer2016 extends EventEmitter {
crowbarHitRewardChance!: number;
crowbarHitDamage!: number;
/* */
navigator: Navig;

constructor(
serverPort: number,
Expand Down Expand Up @@ -495,6 +498,7 @@ export class ZoneServer2016 extends EventEmitter {
this.commandHandler = new CommandHandler();
this.playTimeManager = new PlayTimeManager();
this.aiManager = new AiManager();
this.navigator = new Navig();
/* CONFIG MANAGER MUST BE INSTANTIATED LAST ! */
this.configManager = new ConfigManager(this, process.env.CONFIG_PATH);
this.enableWorldSaves =
Expand Down Expand Up @@ -1599,6 +1603,11 @@ export class ZoneServer2016 extends EventEmitter {
}

private async setupServer() {
const z1_nav = new Uint8Array(
readFileSync(__dirname + "/../../../data/2016/navData/z1.bin")
);
await this.navigator.loadNav(z1_nav);

this.weatherManager.init();
this.playTimeManager.init(this);
this.initModelsDataSource();
Expand Down
45 changes: 45 additions & 0 deletions src/utils/recast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// ======================================================================
//
// GNU GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
// copyright (C) 2020 - 2021 Quentin Gruber
// copyright (C) 2021 - 2024 H1emu community
//
// https://github.com/QuentinGruber/h1z1-server
// https://www.npmjs.com/package/h1z1-server
//
// Based on https://github.com/psemu/soe-network
// ======================================================================

import { init as initRecast, NavMesh } from "recast-navigation";
import { NavMeshQuery } from "recast-navigation";
import { importNavMesh } from "recast-navigation";

export class Navig {
navmesh!: NavMesh;
constructor() {}
async loadNav(navData: Uint8Array) {
await initRecast();
const { navMesh } = importNavMesh(navData);
this.navmesh = navMesh;
}
testNavMesh(a: Float32Array, b: Float32Array): Float32Array[] {
console.time("calculating path");
const navMeshQuery = new NavMeshQuery(this.navmesh);

const start = { x: a[0], y: a[1], z: a[2] };
const end = { x: b[0], y: b[1], z: b[2] };
const { success, error, path } = navMeshQuery.computePath(start, end);
console.log(success);
console.log(error);
console.log(path);
console.timeEnd("calculating path");
const pathNodes: Float32Array[] = [];
if (path) {
path.forEach((v) => {
pathNodes.push(new Float32Array([v.x, v.y, v.z]));
});
}
return pathNodes;
}
}

0 comments on commit ba3f752

Please sign in to comment.