Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a simulation for calculating the power used by a ship system. #577

Merged
merged 2 commits into from
Mar 5, 2023
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
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";
import {PowerGridSystem} from "./PowerGridSystem";

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