1
0
Fork 0
polaris-task-force/src/lib/shipping.ts
Z8MB1E c64c9da0da feat(map): road-network routing for shipments with transport modes
Add pure routing, placement, point, and style libs plus map-system tests. Shipments gain a map relationship and routePath snapshot; ground vehicles route over the road network via Dijkstra while air and sea travel straight-line. Vehicle types declare a ground/air/sea transportMode and transit math works in meters.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-16 23:14:01 -04:00

84 lines
2.7 KiB
TypeScript

/**
* Calculates the transit time in milliseconds for a shipment.
* @param distance - Route length in meters (straight-line or road route)
* @param vehicleSpeed - Vehicle speed in km/h (from maxSpeedOnRoad, maxAirspeed, etc.)
*/
export function calculateTransitTime(
distance: number,
vehicleSpeed: number,
): number {
const km = distance / 1000;
const hours = km / vehicleSpeed;
return hours * 60 * 60 * 1000;
}
/**
* Calculates the total fuel cost for a shipment.
* @param distance - Route length in meters
* @param consumptionRate - Fuel consumed per unit of distance (from vehicle's fuelConsumptionRate)
*/
export function calculateFuelCost(
distance: number,
consumptionRate: number,
): number {
return Math.ceil(distance * consumptionRate);
}
/**
* Calculates the fuel consumed per game tick.
* @param totalFuelCost - Total fuel for the trip
* @param totalTicks - Total number of ticks for the trip
*/
export function calculateFuelPerTick(
totalFuelCost: number,
totalTicks: number,
): number {
if (totalTicks <= 0) return totalFuelCost;
return totalFuelCost / totalTicks;
}
/**
* Calculates the number of game ticks for a given duration.
* @param durationMs - Duration in milliseconds
* @param tickIntervalMinutes - Game tick interval in minutes
*/
export function calculateTicksForDuration(
durationMs: number,
tickIntervalMinutes: number,
): number {
const tickMs = tickIntervalMinutes * 60 * 1000;
return Math.ceil(durationMs / tickMs);
}
/**
* Gets the effective speed of a vehicle for its transport mode: ground
* prefers road speed then off-road, air uses airspeed, sea uses knots.
* Falls back through the remaining speeds and finally a 50 km/h default.
*/
export function getVehicleEffectiveSpeed(vehicle: {
transportMode?: "ground" | "air" | "sea" | null;
combat?: {
maxSpeedOnRoad?: number | null;
maxSpeedOffRoad?: number | null;
maxAirspeed?: number | null;
maxKnots?: number | null;
} | null;
} & {
maxSpeedOnRoad?: number | null;
maxSpeedOffRoad?: number | null;
maxAirspeed?: number | null;
maxKnots?: number | null;
}): number {
const combat = vehicle.combat ?? vehicle;
const mode = vehicle.transportMode ?? "ground";
const byMode: Record<string, (number | null | undefined)[]> = {
ground: [combat.maxSpeedOnRoad, combat.maxSpeedOffRoad, combat.maxAirspeed, combat.maxKnots],
air: [combat.maxAirspeed, combat.maxSpeedOnRoad, combat.maxSpeedOffRoad, combat.maxKnots],
sea: [combat.maxKnots, combat.maxSpeedOffRoad, combat.maxSpeedOnRoad, combat.maxAirspeed],
};
const speeds = byMode[mode] ?? byMode.ground;
for (const speed of speeds) {
if (speed != null && speed > 0) return speed;
}
return 50; // default fallback speed
}