Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ <h4 style="color: #00d4ff; margin-bottom: 0.5rem;">How to Move:</h4>
<h4>Inventory</h4>
<ul id="inventoryList"></ul>
</div>

<div id="routeCardPanel"
style="display:none; margin-top: 1rem; background: rgba(0, 212, 255, 0.08); border: 1px solid #2a3f66; border-radius: 6px; padding: 0.6rem;">
<h4 style="margin-bottom: 0.5rem;">Route Card</h4>
<div style="max-height: 260px; overflow: auto;">
<table style="width:100%; border-collapse: collapse; font-size: 0.8rem;">
<thead>
<tr>
<th style="text-align:left; border-bottom: 1px solid #4a5f85; padding: 0.25rem;">#</th>
<th style="text-align:left; border-bottom: 1px solid #4a5f85; padding: 0.25rem;">From</th>
<th style="text-align:left; border-bottom: 1px solid #4a5f85; padding: 0.25rem;">To</th>
<th style="text-align:left; border-bottom: 1px solid #4a5f85; padding: 0.25rem;">Direction</th>
<th style="text-align:left; border-bottom: 1px solid #4a5f85; padding: 0.25rem;">Distance</th>
<th style="text-align:left; border-bottom: 1px solid #4a5f85; padding: 0.25rem;">Total</th>
</tr>
</thead>
<tbody id="routeCardBody"></tbody>
</table>
</div>
</div>
</div>
</main>
</div>
Expand Down
46 changes: 46 additions & 0 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,59 @@ class PwshLeafmapGame {
const durMin = Math.round(data.duration / 60);
pathDisplay.textContent = `${distKm}km • ~${durMin}min • ${data.travelMode}`;
}

this.renderRouteCard(data?.routeCard || []);
}

hidePathInfo() {
const pathPanel = document.getElementById('pathInfoPanel');
if (pathPanel) {
pathPanel.style.display = 'none';
}

const routeCardPanel = document.getElementById('routeCardPanel');
if (routeCardPanel) {
routeCardPanel.style.display = 'none';
}
}

renderRouteCard(routeCardRows) {
const panel = document.getElementById('routeCardPanel');
const body = document.getElementById('routeCardBody');

if (!panel || !body) {
return;
}

body.innerHTML = '';

if (!Array.isArray(routeCardRows) || routeCardRows.length === 0) {
panel.style.display = 'none';
return;
}

routeCardRows.forEach(row => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${row.serial ?? ''}</td>
<td>${row.from ?? ''}</td>
<td>${row.to ?? ''}</td>
<td>${row.directionText ?? ''}</td>
<td>${this.formatDistance(row.segmentDistance)}</td>
<td>${this.formatDistance(row.cumulativeDistance)}</td>
`;
body.appendChild(tr);
});

panel.style.display = 'block';
}

formatDistance(distanceMeters) {
const meters = Number(distanceMeters) || 0;
if (meters < 1000) {
return `${Math.round(meters)} m`;
}
return `${(meters / 1000).toFixed(2)} km`;
}

async autoLoadGameData() {
Expand Down
7 changes: 5 additions & 2 deletions js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class GameMap {
distance: path.distance,
duration: path.duration,
travelMode: path.travelMode,
pathType: path.type
pathType: path.type,
routeCard: path.routeCard || []
});
}

Expand Down Expand Up @@ -497,7 +498,9 @@ class GameMap {
this.game.updatePathInfo({
distance: pathData.distance,
duration: pathData.duration,
type: pathData.type
type: pathData.type,
travelMode: pathData.travelMode,
routeCard: pathData.routeCard || []
});
}

Expand Down
156 changes: 153 additions & 3 deletions js/pathfinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class PathfindingManager {
const profile = travelMode === 'foot' ? 'foot' : 'driving';

// Build OSRM URL
const url = `${this.osrmUrl}/${profile}/${start.lng},${start.lat};${destination.lng},${destination.lat}?overview=full&geometries=geojson`;
const url = `${this.osrmUrl}/${profile}/${start.lng},${start.lat};${destination.lng},${destination.lat}?overview=full&geometries=geojson&steps=true`;

console.log('Fetching OSRM route:', url);

Expand All @@ -83,7 +83,8 @@ class PathfindingManager {
distance: route.distance, // meters
duration: route.duration, // seconds
type: 'road',
travelMode: travelMode
travelMode: travelMode,
routeCard: this.normalizeOSRMRouteToRouteCard(route, start, destination, travelMode, 'road')
};

// Cache the route
Expand All @@ -106,7 +107,8 @@ class PathfindingManager {
distance: distance,
duration: duration,
type: 'direct',
travelMode: travelMode
travelMode: travelMode,
routeCard: this.createDirectRouteCard(start, destination, distance, travelMode, 'direct')
};

console.log(`Direct path: ${path.distance}m, ${path.duration}s`);
Expand Down Expand Up @@ -242,6 +244,154 @@ class PathfindingManager {
this.routeCache.clear();
console.log('Route cache cleared');
}

/**
* Normalize OSRM legs/steps into reusable route-card rows
*/
normalizeOSRMRouteToRouteCard(route, start, destination, travelMode, pathType) {
if (!route || !route.legs || route.legs.length === 0) {
return this.createDirectRouteCard(start, destination, start.distanceTo(destination), travelMode, pathType);
}

const rows = [];
let serial = 1;
let cumulativeDistance = 0;
let previousLabel = this.formatCoordinateLabel(start);

route.legs.forEach((leg, legIndex) => {
const steps = Array.isArray(leg.steps) ? leg.steps : [];
steps.forEach((step, stepIndex) => {
const segmentDistance = Number(step.distance) || 0;
cumulativeDistance += segmentDistance;

const currentLabel = this.getStepLabel(step, legIndex, stepIndex);
const nextStep = steps[stepIndex + 1];
const isLastStep = legIndex === route.legs.length - 1 && stepIndex === steps.length - 1;
const toLabel = isLastStep
? this.formatCoordinateLabel(destination)
: (nextStep ? this.getStepLabel(nextStep, legIndex, stepIndex + 1) : currentLabel);

rows.push({
serial: serial++,
from: previousLabel,
to: toLabel,
directionText: this.getStepDirectionText(step, toLabel),
segmentDistance: segmentDistance,
cumulativeDistance: cumulativeDistance,
travelMode: travelMode,
pathType: pathType,
roadName: step.name || '',
maneuverType: step.maneuver?.type || '',
modifier: step.maneuver?.modifier || ''
});

previousLabel = currentLabel;
});
});

if (rows.length === 0) {
return this.createDirectRouteCard(start, destination, start.distanceTo(destination), travelMode, pathType);
}

return rows;
}

/**
* Build a synthetic route-card row for direct-line travel
*/
createDirectRouteCard(start, destination, distance, travelMode, pathType) {
const bearing = this.getBearing(start, destination);
const cardinal = this.bearingToCardinal(bearing);

return [{
serial: 1,
from: this.formatCoordinateLabel(start),
to: this.formatCoordinateLabel(destination),
directionText: `Head ${cardinal} to destination`,
segmentDistance: distance,
cumulativeDistance: distance,
travelMode: travelMode,
pathType: pathType,
roadName: '',
maneuverType: 'direct',
modifier: cardinal
}];
}

getStepLabel(step, legIndex, stepIndex) {
if (step && step.name && step.name.trim().length > 0) {
return step.name.trim();
}

return `Segment ${legIndex + 1}.${stepIndex + 1}`;
}

getStepDirectionText(step, toLabel) {
const maneuver = step?.maneuver || {};
const type = maneuver.type || 'continue';
const modifier = maneuver.modifier || '';
const roadName = (step?.name || '').trim();

if (type === 'roundabout') {
const exitText = maneuver.exit ? ` (exit ${maneuver.exit})` : '';
return `Enter roundabout${exitText}${roadName ? ` onto ${roadName}` : ''}`;
}

if (type === 'arrive') {
return `Arrive at ${toLabel}`;
}

if (modifier) {
return `${this.capitalize(modifier)} ${type}${roadName ? ` on ${roadName}` : ''}`;
}

return `${this.capitalize(type)}${roadName ? ` on ${roadName}` : ''}`;
}

formatCoordinateLabel(latLng) {
if (!latLng || typeof latLng.lat !== 'number' || typeof latLng.lng !== 'number') {
return 'Unknown';
}

return `[${latLng.lat.toFixed(5)}, ${latLng.lng.toFixed(5)}]`;
}

getBearing(start, destination) {
const startLat = this.toRadians(start.lat);
const startLng = this.toRadians(start.lng);
const endLat = this.toRadians(destination.lat);
const endLng = this.toRadians(destination.lng);

const y = Math.sin(endLng - startLng) * Math.cos(endLat);
const x =
Math.cos(startLat) * Math.sin(endLat) -
Math.sin(startLat) * Math.cos(endLat) * Math.cos(endLng - startLng);

const bearing = (this.toDegrees(Math.atan2(y, x)) + 360) % 360;
return bearing;
}

bearingToCardinal(bearing) {
const directions = ['north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'northwest'];
const index = Math.round(bearing / 45) % 8;
return directions[index];
}

toRadians(degrees) {
return degrees * (Math.PI / 180);
}

toDegrees(radians) {
return radians * (180 / Math.PI);
}

capitalize(value) {
if (!value || typeof value !== 'string') {
return '';
}

return value.charAt(0).toUpperCase() + value.slice(1);
}
}

// Export for use in other modules
Expand Down