Skip to content

Commit

Permalink
abstract system model retreival
Browse files Browse the repository at this point in the history
As with the previous commit the aim to use seeds only when absolutely necessary

This commit removes almost all checks with SeededStarSystemModel by a getter function that returns the correct model, be it seeded or custom
  • Loading branch information
BarthPaleologue committed Oct 10, 2024
1 parent d7adf79 commit 0c955ce
Show file tree
Hide file tree
Showing 28 changed files with 191 additions and 280 deletions.
16 changes: 6 additions & 10 deletions src/ts/cosmosJourneyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ import { TutorialLayer } from "./ui/tutorial/tutorialLayer";
import { FlightTutorial } from "./tutorials/flightTutorial";
import { SidePanels } from "./ui/sidePanels";
import { Settings } from "./settings";
import { SeededStarSystemModel } from "./starSystem/seededStarSystemModel";
import { Player } from "./player/player";
import { getObjectBySystemId, getUniverseObjectId } from "./utils/orbitalObjectId";
import { StarSystemCoordinates } from "./starSystem/starSystemModel";
import { getSeedFromCoordinates } from "./utils/getStarGalacticPositionFromSeed";
import { getSystemModelFromCoordinates } from "./utils/starSystemCoordinatesUtils";
import { StarSystemController } from "./starSystem/starSystemController";

const enum EngineState {
UNINITIALIZED,
Expand Down Expand Up @@ -133,9 +133,7 @@ export class CosmosJourneyer {

this.starSystemView.onInitStarSystem.add(() => {
const starSystemModel = this.starSystemView.getStarSystem().model;
if (starSystemModel instanceof SeededStarSystemModel) {
this.starMap.setCurrentStarSystem(starSystemModel.getCoordinates());
}
this.starMap.setCurrentStarSystem(starSystemModel.getCoordinates());
});

this.pauseMenu = new PauseMenu(this.sidePanels);
Expand Down Expand Up @@ -437,11 +435,9 @@ export class CosmosJourneyer {

const universeObjectId = universeCoordinates.universeObjectId;

const seed = getSeedFromCoordinates(universeObjectId.starSystemCoordinates);
if (seed === null) {
throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet.");
}
await this.starSystemView.loadStarSystemFromSeed(seed);
const systemModel = getSystemModelFromCoordinates(universeObjectId.starSystemCoordinates);
const systemController = new StarSystemController(systemModel, this.starSystemView.scene);
await this.starSystemView.loadStarSystem(systemController, true);

if (this.state === EngineState.UNINITIALIZED) await this.init(true);
else this.starSystemView.initStarSystem();
Expand Down
12 changes: 2 additions & 10 deletions src/ts/missions/generateSightSeeingMissions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel";
import { getNeighborStarSystemCoordinates } from "../utils/getNeighborStarSystems";
import { SpaceStationModel } from "../spacestation/spacestationModel";
import { newSightSeeingMission } from "./sightSeeingMission";
Expand All @@ -9,7 +8,7 @@ import { Player } from "../player/player";
import { getPlanetaryMassObjectModels } from "../utils/getModelsFromSystemModel";
import { TelluricPlanetModel } from "../planets/telluricPlanet/telluricPlanetModel";
import { Mission, MissionType } from "./mission";
import { getSeedFromCoordinates } from "../utils/getStarGalacticPositionFromSeed";
import { getSystemModelFromCoordinates } from "../utils/starSystemCoordinatesUtils";

/**
* Generates sightseeing missions available at the given space station for the player. Missions are generated based on the current timestamp (hourly basis).
Expand All @@ -21,21 +20,14 @@ export function generateSightseeingMissions(spaceStationModel: SpaceStationModel
const currentHour = Math.floor(timestampMillis / 1000 / 60 / 60);

const starSystem = spaceStationModel.starSystem;
if (!(starSystem instanceof SeededStarSystemModel)) {
throw new Error("Star system is not seeded, hence missions cannot be generated");
}

const anomalyFlyByMissions: Mission[] = [];
const neutronStarFlyByMissions: Mission[] = [];
const blackHoleFlyByMissions: Mission[] = [];

const neighborSystems = getNeighborStarSystemCoordinates(starSystem.getCoordinates(), 75);
neighborSystems.forEach(([systemCoordinates, coordinates, distance]) => {
const systemSeed = getSeedFromCoordinates(systemCoordinates);
if (systemSeed === null) {
throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet.");
}
const systemModel = new SeededStarSystemModel(systemSeed);
const systemModel = getSystemModelFromCoordinates(systemCoordinates);
systemModel.getAnomalies().forEach((_, anomalyIndex) => {
if (!uniformRandBool(1.0 / (1.0 + 0.4 * distance), systemModel.rng, 6254 + anomalyIndex + currentHour)) return;
anomalyFlyByMissions.push(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { MissionNode, MissionNodeSerialized, MissionNodeType } from "../../missionNode";
import { MissionContext } from "../../../missionContext";
import { UniverseObjectId, universeObjectIdEquals } from "../../../../saveFile/universeCoordinates";
import { SeededStarSystemModel } from "../../../../starSystem/seededStarSystemModel";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { clamp } from "../../../../utils/math";
import { getObjectBySystemId, getObjectModelByUniverseId } from "../../../../utils/orbitalObjectId";
import { getStarGalacticPosition, getSeedFromCoordinates } from "../../../../utils/getStarGalacticPositionFromSeed";
import { getStarGalacticPosition, getSystemModelFromCoordinates } from "../../../../utils/starSystemCoordinatesUtils";
import i18n from "../../../../i18n";
import { parseDistance } from "../../../../utils/parseToStrings";
import { Settings } from "../../../../settings";
Expand Down Expand Up @@ -62,12 +61,11 @@ export class MissionAsteroidFieldNode implements MissionNode {

const currentSystem = context.currentSystem;
const currentSystemModel = currentSystem.model;
if (currentSystemModel instanceof SeededStarSystemModel) {
// Skip if the current system is not the one we are looking for
if (!starSystemCoordinatesEquals(currentSystemModel.getCoordinates(), this.targetSystemCoordinates)) {
this.state = AsteroidFieldMissionState.NOT_IN_SYSTEM;
return;
}

// Skip if the current system is not the one we are looking for
if (!starSystemCoordinatesEquals(currentSystemModel.getCoordinates(), this.targetSystemCoordinates)) {
this.state = AsteroidFieldMissionState.NOT_IN_SYSTEM;
return;
}

const targetObject = getObjectBySystemId(this.objectId, currentSystem);
Expand Down Expand Up @@ -111,11 +109,7 @@ export class MissionAsteroidFieldNode implements MissionNode {
describe(originSystemCoordinates: StarSystemCoordinates): string {
const distance = Vector3.Distance(getStarGalacticPosition(originSystemCoordinates), getStarGalacticPosition(this.targetSystemCoordinates));
const objectModel = getObjectModelByUniverseId(this.objectId);
const systemSeed = getSeedFromCoordinates(this.targetSystemCoordinates);
if (systemSeed === null) {
throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet.");
}
const systemModel = new SeededStarSystemModel(systemSeed);
const systemModel = getSystemModelFromCoordinates(this.targetSystemCoordinates);
return i18n.t("missions:sightseeing:describeAsteroidFieldTrek", {
objectName: objectModel.name,
systemName: systemModel.name,
Expand All @@ -128,11 +122,7 @@ export class MissionAsteroidFieldNode implements MissionNode {
return i18n.t("missions:asteroidField:missionCompleted");
}

const systemSeed = getSeedFromCoordinates(this.targetSystemCoordinates);
if (systemSeed === null) {
throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet.");
}
const targetSystemModel = new SeededStarSystemModel(systemSeed);
const targetSystemModel = getSystemModelFromCoordinates(this.targetSystemCoordinates);
const currentSystemModel = context.currentSystem.model;

const targetSystemPosition = getStarGalacticPosition(this.targetSystemCoordinates);
Expand Down
26 changes: 8 additions & 18 deletions src/ts/missions/nodes/actions/sightseeing/missionFlyByNode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { MissionNode, MissionNodeSerialized, MissionNodeType } from "../../missionNode";
import { MissionContext } from "../../../missionContext";
import { UniverseObjectId, universeObjectIdEquals } from "../../../../saveFile/universeCoordinates";
import { SeededStarSystemModel } from "../../../../starSystem/seededStarSystemModel";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { getObjectBySystemId, getObjectModelByUniverseId } from "../../../../utils/orbitalObjectId";
import { getStarGalacticPosition, getSeedFromCoordinates } from "../../../../utils/getStarGalacticPositionFromSeed";
import { getStarGalacticPosition, getSystemModelFromCoordinates } from "../../../../utils/starSystemCoordinatesUtils";
import { parseDistance } from "../../../../utils/parseToStrings";
import { Settings } from "../../../../settings";
import i18n from "../../../../i18n";
Expand Down Expand Up @@ -61,12 +60,11 @@ export class MissionFlyByNode implements MissionNode {

const currentSystem = context.currentSystem;
const currentSystemModel = currentSystem.model;
if (currentSystemModel instanceof SeededStarSystemModel) {
// Skip if the current system is not the one we are looking for
if (!starSystemCoordinatesEquals(currentSystemModel.getCoordinates(), this.targetSystemCoordinates)) {
this.state = FlyByState.NOT_IN_SYSTEM;
return;
}

// Skip if the current system is not the one we are looking for
if (!starSystemCoordinatesEquals(currentSystemModel.getCoordinates(), this.targetSystemCoordinates)) {
this.state = FlyByState.NOT_IN_SYSTEM;
return;
}

const targetObject = getObjectBySystemId(this.objectId, currentSystem);
Expand All @@ -90,11 +88,7 @@ export class MissionFlyByNode implements MissionNode {
describe(originSystemCoordinates: StarSystemCoordinates): string {
const distance = Vector3.Distance(getStarGalacticPosition(originSystemCoordinates), getStarGalacticPosition(this.targetSystemCoordinates));
const objectModel = getObjectModelByUniverseId(this.objectId);
const systemSeed = getSeedFromCoordinates(this.targetSystemCoordinates);
if (systemSeed === null) {
throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet.");
}
const systemModel = new SeededStarSystemModel(systemSeed);
const systemModel = getSystemModelFromCoordinates(this.targetSystemCoordinates);
return i18n.t("missions:sightseeing:describeFlyBy", {
objectType: objectModel.typeName.toLowerCase(),
systemName: systemModel.name,
Expand All @@ -107,11 +101,7 @@ export class MissionFlyByNode implements MissionNode {
return i18n.t("missions:flyBy:missionCompleted");
}

const systemSeed = getSeedFromCoordinates(this.targetSystemCoordinates);
if (systemSeed === null) {
throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet.");
}
const targetSystemModel = new SeededStarSystemModel(systemSeed);
const targetSystemModel = getSystemModelFromCoordinates(this.targetSystemCoordinates);
const currentSystemModel = context.currentSystem.model;

const targetSystemPosition = getStarGalacticPosition(this.targetSystemCoordinates);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { MissionNode, MissionNodeSerialized, MissionNodeType } from "../../missionNode";
import { MissionContext } from "../../../missionContext";
import { UniverseObjectId, universeObjectIdEquals } from "../../../../saveFile/universeCoordinates";
import { SeededStarSystemModel } from "../../../../starSystem/seededStarSystemModel";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { PhysicsRaycastResult } from "@babylonjs/core/Physics/physicsRaycastResult";
import { CollisionMask, Settings } from "../../../../settings";
import { getObjectBySystemId, getObjectModelByUniverseId } from "../../../../utils/orbitalObjectId";
import { getSeedFromCoordinates, getStarGalacticPosition } from "../../../../utils/getStarGalacticPositionFromSeed";
import { getStarGalacticPosition, getSystemModelFromCoordinates } from "../../../../utils/starSystemCoordinatesUtils";
import i18n from "../../../../i18n";
import { parseDistance } from "../../../../utils/parseToStrings";
import { getGlobalKeyboardLayoutMap } from "../../../../utils/keyboardAPI";
Expand Down Expand Up @@ -64,12 +63,11 @@ export class MissionTerminatorLandingNode implements MissionNode {

const currentSystem = context.currentSystem;
const currentSystemModel = currentSystem.model;
if (currentSystemModel instanceof SeededStarSystemModel) {
// Skip if the current system is not the one we are looking for
if (!starSystemCoordinatesEquals(currentSystemModel.getCoordinates(), this.targetSystemCoordinates)) {
this.state = LandMissionState.NOT_IN_SYSTEM;
return;
}

// Skip if the current system is not the one we are looking for
if (!starSystemCoordinatesEquals(currentSystemModel.getCoordinates(), this.targetSystemCoordinates)) {
this.state = LandMissionState.NOT_IN_SYSTEM;
return;
}

const targetObject = getObjectBySystemId(this.objectId, currentSystem);
Expand Down Expand Up @@ -119,11 +117,7 @@ export class MissionTerminatorLandingNode implements MissionNode {
describe(originSystemCoordinates: StarSystemCoordinates): string {
const distance = Vector3.Distance(getStarGalacticPosition(originSystemCoordinates), getStarGalacticPosition(this.targetSystemCoordinates));
const objectModel = getObjectModelByUniverseId(this.objectId);
const systemSeed = getSeedFromCoordinates(this.targetSystemCoordinates);
if (systemSeed === null) {
throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet.");
}
const systemModel = new SeededStarSystemModel(systemSeed);
const systemModel = getSystemModelFromCoordinates(this.targetSystemCoordinates);
return i18n.t("missions:sightseeing:describeTerminatorLanding", {
objectName: objectModel.name,
systemName: systemModel.name,
Expand All @@ -136,12 +130,7 @@ export class MissionTerminatorLandingNode implements MissionNode {
return i18n.t("missions:terminatorLanding:missionCompleted");
}

const systemSeed = getSeedFromCoordinates(this.targetSystemCoordinates);
if (systemSeed === null) {
throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet.");
}

const targetSystemModel = new SeededStarSystemModel(systemSeed);
const targetSystemModel = getSystemModelFromCoordinates(this.targetSystemCoordinates);
const currentSystemModel = context.currentSystem.model;

const targetSystemPosition = getStarGalacticPosition(this.targetSystemCoordinates);
Expand Down
2 changes: 1 addition & 1 deletion src/ts/missions/sightSeeingMission.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Mission, MissionType } from "./mission";
import { SpaceStationModel } from "../spacestation/spacestationModel";
import { SystemObjectType, UniverseObjectId } from "../saveFile/universeCoordinates";
import { getStarGalacticPosition } from "../utils/getStarGalacticPositionFromSeed";
import { getStarGalacticPosition } from "../utils/starSystemCoordinatesUtils";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { MissionNode } from "./nodes/missionNode";
import { MissionFlyByNode } from "./nodes/actions/sightseeing/missionFlyByNode";
Expand Down
2 changes: 1 addition & 1 deletion src/ts/society/powerplay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { seededSquirrelNoise } from "squirrel-noise";
import { Settings } from "../settings";
import { makeNoise3D } from "fast-simplex-noise";
import { getStarGalacticPosition } from "../utils/getStarGalacticPositionFromSeed";
import { getStarGalacticPosition } from "../utils/starSystemCoordinatesUtils";
import { StarSystemCoordinates } from "../starSystem/starSystemModel";

const materialistSpiritualistRng = seededSquirrelNoise(Settings.POWER_PLAY_SEED);
Expand Down
7 changes: 6 additions & 1 deletion src/ts/society/spaceStationPlacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import { getMoonSeeds } from "../planets/common";
import { PlanetModel } from "../architecture/planet";
import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel";
import { NeutronStarModel } from "../stellarObjects/neutronStar/neutronStarModel";
import { StarSystemModel } from "../starSystem/starSystemModel";

/**
* Analyzes the given star system to return the indices of the orbital objects that are space stations.
* @param systemModel
*/
export function placeSpaceStations(systemModel: SeededStarSystemModel): PlanetModel[] {
export function placeSpaceStations(systemModel: StarSystemModel): PlanetModel[] {
if (!(systemModel instanceof SeededStarSystemModel)) {
throw new Error("Only seeded star systems are supported for space station placement.");
}

const stellarObjectModels = systemModel.getStellarObjects().map(([bodyType, seed]) => {
switch (bodyType) {
case BodyType.STAR:
Expand Down
2 changes: 1 addition & 1 deletion src/ts/society/starSystemSociety.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getStarGalacticPosition } from "../utils/getStarGalacticPositionFromSeed";
import { getStarGalacticPosition } from "../utils/starSystemCoordinatesUtils";
import { Settings } from "../settings";
import { StarSystemCoordinates } from "../starSystem/starSystemModel";

Expand Down
9 changes: 1 addition & 8 deletions src/ts/spacestation/spacestationModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { generateSpaceStationName } from "../utils/spaceStationNameGenerator";
import { StarSystemModel } from "../starSystem/starSystemModel";
import { Faction } from "../society/factions";
import { getPowerPlayData } from "../society/powerplay";
import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel";
import { NeutronStarModel } from "../stellarObjects/neutronStar/neutronStarModel";
import { BodyType } from "../architecture/bodyType";
import { BlackHoleModel } from "../stellarObjects/blackHole/blackHoleModel";
Expand Down Expand Up @@ -113,13 +112,7 @@ export class SpaceStationModel implements OrbitalObjectModel {
axialTilt: 2 * this.rng(GenerationSteps.AXIAL_TILT) * Math.PI
};

const powerplayData =
this.starSystem instanceof SeededStarSystemModel
? getPowerPlayData(this.starSystem.getCoordinates())
: {
materialistSpiritualist: 0.5,
capitalistCommunist: 0.5
};
const powerplayData = getPowerPlayData(this.starSystem.getCoordinates());

const isMaterialist = uniformRandBool(powerplayData.materialistSpiritualist, this.rng, 249);
const isCapitalist = uniformRandBool(powerplayData.capitalistCommunist, this.rng, 498);
Expand Down
Loading

0 comments on commit 0c955ce

Please sign in to comment.