mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 07:33:12 +09:00
Add player position tracking
This commit is contained in:
1 parent
57f89de746
commit
047d3c22f5
8 files changed
+850
-16
No files matched your search
@@ -1,6 +1,15 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
import { TileDataEvent, ChartData, ChartTagDataEvent, ChartTagData, RecipeDataEvent, RecipeData } from "./messages";
|
||||
import {
|
||||
TileDataEvent,
|
||||
ChartData,
|
||||
ChartTagDataEvent,
|
||||
ChartTagData,
|
||||
RecipeDataEvent,
|
||||
RecipeData,
|
||||
PlayerPositionEvent,
|
||||
PlayerData,
|
||||
} from "./messages";
|
||||
|
||||
interface TileDataIpc {
|
||||
type: "chart";
|
||||
@@ -32,6 +41,14 @@ interface RecipeDataIpc {
|
||||
icon?: { type?: string, name?: string, quality?: string }
|
||||
}
|
||||
|
||||
interface PlayerPositionIpc {
|
||||
player_name: string;
|
||||
surface: string;
|
||||
x: number;
|
||||
y: number;
|
||||
sec: number;
|
||||
}
|
||||
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
pendingTileUpdates = new Map<string, Set<[number, number, [number, number, number, number]]>>();
|
||||
|
||||
@@ -60,6 +77,13 @@ export class InstancePlugin extends BaseInstancePlugin {
|
||||
`Error handling recipe data from Lua:\n${err}`
|
||||
));
|
||||
});
|
||||
|
||||
// Listen for player position data from Lua module
|
||||
this.instance.server.on("ipc-minimap:player_position", (data: PlayerPositionIpc) => {
|
||||
this.handlePlayerPositionFromLua(data).catch(err => this.logger.error(
|
||||
`Error handling player position data from Lua:\n${err}`
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
async handleTileDataFromLua(data: TileDataIpc) {
|
||||
@@ -139,6 +163,33 @@ export class InstancePlugin extends BaseInstancePlugin {
|
||||
}
|
||||
}
|
||||
|
||||
async handlePlayerPositionFromLua(data: PlayerPositionIpc) {
|
||||
try {
|
||||
if (!data) {
|
||||
this.logger.error("Invalid player position data received");
|
||||
return;
|
||||
}
|
||||
|
||||
const playerData: PlayerData = {
|
||||
player_name: data.player_name,
|
||||
surface: data.surface,
|
||||
x: data.x,
|
||||
y: data.y,
|
||||
sec: data.sec,
|
||||
};
|
||||
|
||||
const event = new PlayerPositionEvent(
|
||||
this.instance.config.get("instance.id"),
|
||||
playerData
|
||||
);
|
||||
this.logger.info(`MM: got Lua pos for ${data.player_name} ` +
|
||||
`(${data.x},${data.y}) – forwarding`);
|
||||
this.instance.sendTo("controller", event);
|
||||
} catch (err: unknown) {
|
||||
this.logger.error(`Failed to process player position data from Lua: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
async onStart() {
|
||||
// Initialize the Lua module when the instance starts
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user