Skip to content

Commit

Permalink
feat: Add a simulation for calculating the power used by a ship system.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Mar 4, 2023
1 parent 44a9894 commit 8630ae6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
75 changes: 75 additions & 0 deletions server/src/systems/PowerDrawSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {Entity, System} from "../utils/ecs";

/**
* There's a subtle distinction between powerDraw and requestedPower
* - powerDraw is how much power the system is currently pulling based
* on it's current workload.
* - requestedPower is an artificial limit placed by the crew that keeps
* the power draw at or below that limit.
*/

export class PowerDrawSystem extends System {
test(entity: Entity) {
return !!entity.components.power && !!entity.components.isShipSystem;
}
update(entity: Entity) {
const systemType = entity.components.isShipSystem;
const power = entity.components.power;
const efficiency = entity.components.efficiency?.efficiency || 1;
const efficiencyMultiple = 1 / efficiency;
if (!systemType?.type || !power) return;
const {maxSafePower, requiredPower} = power;
let powerDraw = 0;
switch (systemType.type) {
case "warpEngines": {
if (!entity.components.isWarpEngines) return;
const {currentWarpFactor, warpFactorCount} =
entity.components.isWarpEngines;
if (currentWarpFactor === 0) break;
const warpEngineUse = currentWarpFactor / warpFactorCount;
powerDraw =
(maxSafePower - requiredPower) * warpEngineUse + requiredPower;
break;
}
case "impulseEngines": {
if (!entity.components.isImpulseEngines) return;
const {cruisingSpeed, targetSpeed} = entity.components.isImpulseEngines;
if (targetSpeed === 0) break;
const impulseEngineUse = targetSpeed / cruisingSpeed;
powerDraw =
(maxSafePower - requiredPower) * impulseEngineUse + requiredPower;
break;
}
case "thrusters": {
if (!entity.components.isThrusters) return;
const {direction, rotationDelta, thrusting} =
entity.components.isThrusters;
if (!thrusting) break;
const directionOutput = Math.hypot(
direction.x,
direction.y,
direction.z
);
const rotationOutput = Math.hypot(
rotationDelta.x,
rotationDelta.y,
rotationDelta.z
);
const totalOutput = directionOutput + rotationOutput;
powerDraw =
(maxSafePower - requiredPower) * totalOutput + requiredPower;
break;
}
case "generic":
powerDraw = power.requestedPower;
break;
default:
return;
}

// Limit the power draw to the requested power, so we never go over it.
entity.updateComponent("power", {
powerDraw: Math.min(power.requestedPower, powerDraw * efficiencyMultiple),
});
}
}
2 changes: 2 additions & 0 deletions server/src/systems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {FilterInventorySystem} from "./FilterInventorySystem";
import {ReactorHeatSystem} from "./ReactorHeatSystem";
import {HeatToCoolantSystem} from "./HeatToCoolantSystem";
import {HeatDispersionSystem} from "./HeatDispersionSystem";
import {PowerDrawSystem} from "./PowerDrawSystem";

const systems = [
FilterInventorySystem,
Expand All @@ -32,6 +33,7 @@ const systems = [
TimerSystem,
ReactorFuelSystem,
ReactorHeatSystem,
PowerDrawSystem,
AutoRotateSystem,
AutoThrustSystem,
ThrusterSystem,
Expand Down

0 comments on commit 8630ae6

Please sign in to comment.