Skip to content

Commit 75da20f

Browse files
committed
print the tracers on the map when running the command
1 parent 5b24262 commit 75da20f

File tree

1 file changed

+69
-30
lines changed

1 file changed

+69
-30
lines changed

src/structures/rustPlusMap.ts

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ import * as path from 'path';
2525

2626
import { guildInstanceManager as gim, localeManager as lm } from '../../index';
2727
import { RustPlusInstance } from '../managers/rustPlusManager';
28-
import { getGridPos, getMonumentsInfo, getDistance, Point } from '../utils/map';
28+
import * as map from '../utils/map';
2929
import * as constants from '../utils/constants';
3030
import { GuildInstance } from '../managers/guildInstanceManager';
31+
import { Tracers } from './rustPlusMapMarkers';
3132

3233
const MARKERS_PATH = path.join(__dirname, '..', 'resources/images/markers/');
34+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
3335
const NOTE_MARKERS_PATH = path.join(__dirname, '..', 'resources/images/note_markers/');
3436

37+
const TRACER_LINE_WIDTH = 2;
38+
3539
const MARKER_ICON_MAP: Record<rp.AppMarkerType, string> = {
3640
[rp.AppMarkerType.Undefined]: '',
3741
[rp.AppMarkerType.Player]: 'player',
@@ -45,6 +49,15 @@ const MARKER_ICON_MAP: Record<rp.AppMarkerType, string> = {
4549
[rp.AppMarkerType.TravellingVendor]: 'travelling_vendor'
4650
}
4751

52+
const TRACER_COLOR_MAP: Record<keyof Tracers, string> = {
53+
players: "#9CDB2E",
54+
ch47s: "#E6194B",
55+
oilRigCh47s: "#4363D8",
56+
cargoShips: "#F58231",
57+
patrolHelicopters: "#911EB4",
58+
travellingVendors: "#FFE119"
59+
};
60+
4861
export class RustPlusMap {
4962
public rpInstance: RustPlusInstance;
5063
public appMap: rp.AppMap;
@@ -176,7 +189,7 @@ export class RustPlusMap {
176189
const pixelX = x * (mapWidthInPixels / mapSize) + oceanMargin;
177190
const pixelY = height - (y * (mapHeightInPixels / mapSize) + oceanMargin);
178191

179-
const label = getGridPos(x, y, mapSize) as string;
192+
const label = map.getGridPos(x, y, mapSize) as string;
180193
ctx.fillText(label, pixelX + labelPadding, pixelY + labelPadding);
181194
}
182195
}
@@ -188,12 +201,9 @@ export class RustPlusMap {
188201

189202
const gInstance = gim.getGuildInstance(this.rpInstance.guildId) as GuildInstance;
190203
const language = gInstance.generalSettings.language;
191-
const width = this.appMap.width;
192-
const height = this.appMap.height;
193-
const oceanMargin = this.appMap.oceanMargin;
194204
const monuments = this.appMap.monuments;
195205

196-
const monumentsInfo = getMonumentsInfo().monuments;
206+
const monumentsInfo = map.getMonumentsInfo().monuments;
197207
const monumentsWithoutText = [
198208
'DungeonBase', 'train_tunnel_display_name', 'train_tunnel_link_display_name'
199209
];
@@ -210,41 +220,32 @@ export class RustPlusMap {
210220
const sizeOfMonumentIcons = 40;
211221

212222
for (const monument of monuments) {
213-
const x = monument.x * ((width - 2 * oceanMargin) / mapSize) + oceanMargin;
214-
const n = height - 2 * oceanMargin;
215-
const y = height - (monument.y * (n / mapSize) + oceanMargin);
223+
const point = this.getMapPoint(monument.x, monument.y, mapSize)
216224

217225
if (monumentsWithoutText.includes(monument.token)) {
218226
if (monument.token === 'train_tunnel_display_name') {
219-
ctx.drawImage(trainTunnel, x, y, sizeOfMonumentIcons, sizeOfMonumentIcons);
227+
ctx.drawImage(trainTunnel, point.x, point.y, sizeOfMonumentIcons, sizeOfMonumentIcons);
220228
}
221229
else if (monument.token === 'train_tunnel_link_display_name') {
222-
ctx.drawImage(trainTunnelLink, x, y, sizeOfMonumentIcons, sizeOfMonumentIcons);
230+
ctx.drawImage(trainTunnelLink, point.x, point.y, sizeOfMonumentIcons, sizeOfMonumentIcons);
223231
}
224232
}
225233
else {
226234
const name = Object.keys(monumentsInfo).includes(monument.token) ?
227235
lm.getIntl(language, `monumentName-${monument.token}`) : monument.token;
228-
ctx.fillText(name, x, y);
236+
ctx.fillText(name, point.x, point.y);
229237
}
230238
}
231239
}
232240

233-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
234241
private async drawMarkers(ctx: SKRSContext2D) {
235242
const rpMapMarkers = this.rpInstance.rpMapMarkers;
236243
if (!rpMapMarkers) return;
237244
const mapSize = this.rpInstance.rpInfo?.appInfo.mapSize;
238245
if (!mapSize) return;
239246

240-
const width = this.appMap.width;
241-
const height = this.appMap.height;
242-
const oceanMargin = this.appMap.oceanMargin;
243-
244247
for (const marker of this.rpInstance.rpMapMarkers!.appMapMarkers.markers) {
245-
const x = marker.x * ((width - 2 * oceanMargin) / mapSize) + oceanMargin;
246-
const n = height - 2 * oceanMargin;
247-
const y = height - (marker.y * (n / mapSize) + oceanMargin);
248+
const point = this.getMapPoint(marker.x, marker.y, mapSize)
248249

249250
let iconPath = MARKER_ICON_MAP[marker.type as rp.AppMarkerType];
250251
if (marker.type === rp.AppMarkerType.VendingMachine) {
@@ -259,7 +260,7 @@ export class RustPlusMap {
259260
const radians = Math.abs(marker.rotation - 360) * (Math.PI / 180);
260261

261262
ctx.save();
262-
ctx.translate(x, y);
263+
ctx.translate(point.x, point.y);
263264
ctx.rotate(radians);
264265
ctx.drawImage(markerIcon, -iconWidth / 2, -iconHeight / 2, iconWidth, iconHeight);
265266
ctx.restore();
@@ -269,21 +270,47 @@ export class RustPlusMap {
269270
}
270271

271272
private async drawTracers(ctx: SKRSContext2D) {
272-
// TODO! TBD
273+
const rpMapMarkers = this.rpInstance.rpMapMarkers;
274+
if (!rpMapMarkers) return;
275+
const mapSize = this.rpInstance.rpInfo?.appInfo.mapSize;
276+
if (!mapSize) return;
277+
278+
const tracers = this.rpInstance.rpMapMarkers!.tracers;
279+
280+
for (const [category, entityMap] of Object.entries(tracers)) {
281+
for (const [, points] of entityMap as Map<string, map.Point[]>) {
282+
const color = TRACER_COLOR_MAP[category as keyof Tracers];
283+
if (points.length < 2) continue;
284+
285+
ctx.beginPath();
286+
ctx.strokeStyle = color;
287+
ctx.lineWidth = TRACER_LINE_WIDTH;
288+
289+
const firstPoint = this.getMapPoint(points[0].x, points[0].y, mapSize);
290+
ctx.moveTo(firstPoint.x, firstPoint.y);
291+
292+
for (let i = 1; i < points.length; i++) {
293+
const point = this.getMapPoint(points[i].x, points[i].y, mapSize);
294+
ctx.lineTo(point.x, point.y);
295+
}
296+
297+
ctx.stroke();
298+
}
299+
}
273300
}
274301

275-
public getMonumentPointIfInside(point: Point, monumentName: string, radius: number): Point | null {
302+
public getMonumentPointIfInside(point: map.Point, monumentName: string, radius: number): map.Point | null {
276303
const monument = this.appMap.monuments.find(e => monumentName === e.token &&
277-
getDistance(point.x, point.y, e.x, e.y) <= radius);
304+
map.getDistance(point.x, point.y, e.x, e.y) <= radius);
278305

279306
return monument ? { x: monument.x, y: monument.y } : null;
280307
}
281308

282-
public getClosestMonument(point: Point): rp.AppMap_Monument {
309+
public getClosestMonument(point: map.Point): rp.AppMap_Monument {
283310
const validMonuments = this.appMap.monuments.filter(e => !this.invalidClosestMonuments.includes(e.token));
284311
return validMonuments.reduce((closest, monument) => {
285-
const currentDistance = getDistance(point.x, point.y, monument.x, monument.y);
286-
const closestDistance = getDistance(point.x, point.y, closest.x, closest.y);
312+
const currentDistance = map.getDistance(point.x, point.y, monument.x, monument.y);
313+
const closestDistance = map.getDistance(point.x, point.y, closest.x, closest.y);
287314
return currentDistance < closestDistance ? monument : closest;
288315
});
289316
}
@@ -294,17 +321,29 @@ export class RustPlusMap {
294321
).length;
295322
}
296323

297-
public getClosestHarbor(point: Point): rp.AppMap_Monument | null {
324+
public getClosestHarbor(point: map.Point): rp.AppMap_Monument | null {
298325
const harbors = this.appMap.monuments.filter(m =>
299326
this.harborMonuments.includes(m.token)
300327
);
301328

302329
if (harbors.length === 0) return null;
303330

304331
return harbors.reduce((closest, harbor) => {
305-
const currentDistance = getDistance(point.x, point.y, harbor.x, harbor.y);
306-
const closestDistance = getDistance(point.x, point.y, closest.x, closest.y);
332+
const currentDistance = map.getDistance(point.x, point.y, harbor.x, harbor.y);
333+
const closestDistance = map.getDistance(point.x, point.y, closest.x, closest.y);
307334
return currentDistance < closestDistance ? harbor : closest;
308335
});
309336
}
337+
338+
public getMapPoint(x: number, y: number, mapSize: number): map.Point {
339+
const width = this.appMap.width;
340+
const height = this.appMap.height;
341+
const oceanMargin = this.appMap.oceanMargin;
342+
343+
const mapX = x * ((width - 2 * oceanMargin) / mapSize) + oceanMargin;
344+
const n = height - 2 * oceanMargin;
345+
const mapY = height - (y * (n / mapSize) + oceanMargin);
346+
347+
return { x: mapX, y: mapY };
348+
}
310349
}

0 commit comments

Comments
 (0)