From 0c955ce797814d30104f2e6b4e1f841d6a3d2029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy?= <31370477+BarthPaleologue@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:01:57 +0200 Subject: [PATCH] abstract system model retreival 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 --- src/ts/cosmosJourneyer.ts | 16 ++-- .../missions/generateSightSeeingMissions.ts | 12 +-- .../sightseeing/missionAsteroidFieldNode.ts | 26 ++----- .../actions/sightseeing/missionFlyByNode.ts | 26 ++----- .../missionTerminatorLandingNode.ts | 27 ++----- src/ts/missions/sightSeeingMission.ts | 2 +- src/ts/society/powerplay.ts | 2 +- src/ts/society/spaceStationPlacement.ts | 7 +- src/ts/society/starSystemSociety.ts | 2 +- src/ts/spacestation/spacestationModel.ts | 9 +-- src/ts/starSystem/seededStarSystemModel.ts | 15 +++- src/ts/starSystem/starSystemController.ts | 8 +- src/ts/starSystem/starSystemView.ts | 32 +++----- src/ts/starmap/starMap.ts | 6 +- src/ts/starmap/starMapUI.ts | 9 +-- src/ts/starmap/starSector.ts | 16 +++- src/ts/starmap/stellarPathfinder.ts | 2 +- src/ts/tutorials/flightTutorial.ts | 10 ++- src/ts/ui/mainMenu.ts | 66 ++++++++++++---- .../ui/spaceStation/spaceStationMissions.ts | 12 +-- src/ts/ui/tutorial/tutorialsPanelContent.ts | 9 +-- src/ts/utils/getModelsFromSystemModel.ts | 10 +-- src/ts/utils/getNeighborStarSystems.ts | 2 +- src/ts/utils/orbitalObjectId.ts | 28 ++----- ...mSeed.ts => starSystemCoordinatesUtils.ts} | 22 +++++- src/ts/utils/systemSeed.ts | 76 ------------------- src/ts/utils/systemTarget.ts | 9 +-- tests/neighborStarSystems.unit.test.ts | 10 ++- 28 files changed, 191 insertions(+), 280 deletions(-) rename src/ts/utils/{getStarGalacticPositionFromSeed.ts => starSystemCoordinatesUtils.ts} (69%) delete mode 100644 src/ts/utils/systemSeed.ts diff --git a/src/ts/cosmosJourneyer.ts b/src/ts/cosmosJourneyer.ts index a75b981e0..1b7471f09 100644 --- a/src/ts/cosmosJourneyer.ts +++ b/src/ts/cosmosJourneyer.ts @@ -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, @@ -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); @@ -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(); diff --git a/src/ts/missions/generateSightSeeingMissions.ts b/src/ts/missions/generateSightSeeingMissions.ts index 7d41763e6..bf7a59a3a 100644 --- a/src/ts/missions/generateSightSeeingMissions.ts +++ b/src/ts/missions/generateSightSeeingMissions.ts @@ -1,4 +1,3 @@ -import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel"; import { getNeighborStarSystemCoordinates } from "../utils/getNeighborStarSystems"; import { SpaceStationModel } from "../spacestation/spacestationModel"; import { newSightSeeingMission } from "./sightSeeingMission"; @@ -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). @@ -21,9 +20,6 @@ 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[] = []; @@ -31,11 +27,7 @@ export function generateSightseeingMissions(spaceStationModel: SpaceStationModel 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( diff --git a/src/ts/missions/nodes/actions/sightseeing/missionAsteroidFieldNode.ts b/src/ts/missions/nodes/actions/sightseeing/missionAsteroidFieldNode.ts index 474afc916..028dc5017 100644 --- a/src/ts/missions/nodes/actions/sightseeing/missionAsteroidFieldNode.ts +++ b/src/ts/missions/nodes/actions/sightseeing/missionAsteroidFieldNode.ts @@ -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"; @@ -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); @@ -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, @@ -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); diff --git a/src/ts/missions/nodes/actions/sightseeing/missionFlyByNode.ts b/src/ts/missions/nodes/actions/sightseeing/missionFlyByNode.ts index dc4a1af78..8d50078ae 100644 --- a/src/ts/missions/nodes/actions/sightseeing/missionFlyByNode.ts +++ b/src/ts/missions/nodes/actions/sightseeing/missionFlyByNode.ts @@ -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"; @@ -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); @@ -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, @@ -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); diff --git a/src/ts/missions/nodes/actions/sightseeing/missionTerminatorLandingNode.ts b/src/ts/missions/nodes/actions/sightseeing/missionTerminatorLandingNode.ts index f45b82c2a..8f06373e3 100644 --- a/src/ts/missions/nodes/actions/sightseeing/missionTerminatorLandingNode.ts +++ b/src/ts/missions/nodes/actions/sightseeing/missionTerminatorLandingNode.ts @@ -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"; @@ -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); @@ -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, @@ -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); diff --git a/src/ts/missions/sightSeeingMission.ts b/src/ts/missions/sightSeeingMission.ts index 07bc45831..7be6117c7 100644 --- a/src/ts/missions/sightSeeingMission.ts +++ b/src/ts/missions/sightSeeingMission.ts @@ -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"; diff --git a/src/ts/society/powerplay.ts b/src/ts/society/powerplay.ts index 6dfc39006..5ba249e34 100644 --- a/src/ts/society/powerplay.ts +++ b/src/ts/society/powerplay.ts @@ -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); diff --git a/src/ts/society/spaceStationPlacement.ts b/src/ts/society/spaceStationPlacement.ts index 969cd5185..94ace49a5 100644 --- a/src/ts/society/spaceStationPlacement.ts +++ b/src/ts/society/spaceStationPlacement.ts @@ -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: diff --git a/src/ts/society/starSystemSociety.ts b/src/ts/society/starSystemSociety.ts index bd5afc9bd..49421ceb5 100644 --- a/src/ts/society/starSystemSociety.ts +++ b/src/ts/society/starSystemSociety.ts @@ -1,4 +1,4 @@ -import { getStarGalacticPosition } from "../utils/getStarGalacticPositionFromSeed"; +import { getStarGalacticPosition } from "../utils/starSystemCoordinatesUtils"; import { Settings } from "../settings"; import { StarSystemCoordinates } from "../starSystem/starSystemModel"; diff --git a/src/ts/spacestation/spacestationModel.ts b/src/ts/spacestation/spacestationModel.ts index a0d9458db..fe2a80f8b 100644 --- a/src/ts/spacestation/spacestationModel.ts +++ b/src/ts/spacestation/spacestationModel.ts @@ -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"; @@ -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); diff --git a/src/ts/starSystem/seededStarSystemModel.ts b/src/ts/starSystem/seededStarSystemModel.ts index 10add2b43..b4a96d13b 100644 --- a/src/ts/starSystem/seededStarSystemModel.ts +++ b/src/ts/starSystem/seededStarSystemModel.ts @@ -19,13 +19,13 @@ import { seededSquirrelNoise } from "squirrel-noise"; import { centeredRand, randRangeInt, uniformRandBool } from "extended-random"; import { Settings } from "../settings"; import { generateStarName } from "../utils/starNameGenerator"; -import { SystemSeed } from "../utils/systemSeed"; import { BodyType } from "../architecture/bodyType"; import { wheelOfFortune } from "../utils/random"; import { AnomalyType } from "../anomalies/anomalyType"; import { StarSystemCoordinates, StarSystemModel } from "./starSystemModel"; import { StarSector } from "../starmap/starSector"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; +import { hashVec3 } from "../utils/hashVec3"; const enum GenerationSteps { NAME, @@ -37,6 +37,13 @@ const enum GenerationSteps { GENERATE_ANOMALIES = 666 } +export type SystemSeed = { + starSectorX: number; + starSectorY: number; + starSectorZ: number; + index: number; +}; + export class SeededStarSystemModel implements StarSystemModel { readonly seed: SystemSeed; readonly rng: (step: number) => number; @@ -45,7 +52,11 @@ export class SeededStarSystemModel implements StarSystemModel { constructor(seed: SystemSeed) { this.seed = seed; - this.rng = seededSquirrelNoise(this.seed.hash); + + const cellRNG = seededSquirrelNoise(hashVec3(seed.starSectorX, seed.starSectorY, seed.starSectorZ)); + const hash = centeredRand(cellRNG, 1 + seed.index) * Settings.SEED_HALF_RANGE; + + this.rng = seededSquirrelNoise(hash); this.name = generateStarName(this.rng, GenerationSteps.NAME); } diff --git a/src/ts/starSystem/starSystemController.ts b/src/ts/starSystem/starSystemController.ts index 0d37ca1bd..66fccecac 100644 --- a/src/ts/starSystem/starSystemController.ts +++ b/src/ts/starSystem/starSystemController.ts @@ -25,12 +25,10 @@ import { SpaceStation } from "../spacestation/spaceStation"; import { TelluricPlanet } from "../planets/telluricPlanet/telluricPlanet"; import { GasPlanet } from "../planets/gasPlanet/gasPlanet"; import { Mandelbulb } from "../anomalies/mandelbulb/mandelbulb"; -import { SeededStarSystemModel } from "./seededStarSystemModel"; import { rotateAround, translate } from "../uberCore/transforms/basicTransform"; import { Star } from "../stellarObjects/star/star"; import { BlackHole } from "../stellarObjects/blackHole/blackHole"; import { NeutronStar } from "../stellarObjects/neutronStar/neutronStar"; -import { SystemSeed } from "../utils/systemSeed"; import { ChunkForge } from "../planets/telluricPlanet/terrain/chunks/chunkForge"; import { OrbitalObject, OrbitalObjectUtils } from "../architecture/orbitalObject"; import { CelestialBody } from "../architecture/celestialBody"; @@ -42,7 +40,7 @@ import { Anomaly } from "../anomalies/anomaly"; import { StarFieldBox } from "./starFieldBox"; import { StarSystemCoordinates, StarSystemModel } from "./starSystemModel"; import { Settings } from "../settings"; -import { getStarGalacticPosition } from "../utils/getStarGalacticPositionFromSeed"; +import { getStarGalacticPosition } from "../utils/starSystemCoordinatesUtils"; export class StarSystemController { readonly scene: UberScene; @@ -97,12 +95,12 @@ export class StarSystemController { private elapsedSeconds = 0; - constructor(model: StarSystemModel | SystemSeed, scene: UberScene) { + constructor(model: StarSystemModel, scene: UberScene) { this.scene = scene; this.starFieldBox = new StarFieldBox(scene); - this.model = model instanceof SystemSeed ? new SeededStarSystemModel(model) : model; + this.model = model; } public addSatellite(satellite: TelluricPlanet): void { diff --git a/src/ts/starSystem/starSystemView.ts b/src/ts/starSystem/starSystemView.ts index 18a22ba60..5641c4f4e 100644 --- a/src/ts/starSystem/starSystemView.ts +++ b/src/ts/starSystem/starSystemView.ts @@ -42,7 +42,6 @@ import { getForwardDirection, getRotationQuaternion, setRotationQuaternion, tran import { Observable } from "@babylonjs/core/Misc/observable"; import { NeutronStar } from "../stellarObjects/neutronStar/neutronStar"; import { View } from "../utils/view"; -import { SystemSeed } from "../utils/systemSeed"; import { SystemTarget } from "../utils/systemTarget"; import { StarSystemInputs } from "../inputs/starSystemInputs"; import { createNotification } from "../utils/notification"; @@ -67,7 +66,6 @@ import { Materials } from "../assets/materials"; import { SpaceStation } from "../spacestation/spaceStation"; import { ObjectTargetCursorType } from "../ui/objectTargetCursor"; import { SpaceStationLayer } from "../ui/spaceStation/spaceStationLayer"; -import { SeededStarSystemModel } from "./seededStarSystemModel"; import { placeSpaceStations } from "../society/spaceStationPlacement"; import { isSystemInHumanBubble } from "../society/starSystemSociety"; import { Player } from "../player/player"; @@ -80,7 +78,7 @@ import { getGlobalKeyboardLayoutMap } from "../utils/keyboardAPI"; import { MissionContext } from "../missions/missionContext"; import { Mission } from "../missions/mission"; import { StarSystemCoordinates, starSystemCoordinatesEquals } from "./starSystemModel"; -import { getSeedFromCoordinates } from "../utils/getStarGalacticPositionFromSeed"; +import { getSystemModelFromCoordinates } from "../utils/starSystemCoordinatesUtils"; /** * The star system view is the part of Cosmos Journeyer responsible to display the current star system, along with the @@ -285,9 +283,9 @@ export class StarSystemView implements View { }); const starSystemCoordinates = target.systemCoordinates; - const seed = getSeedFromCoordinates(starSystemCoordinates); - if (seed === null) throw new Error("No seed found for coordinates. Custom star systems are not supported for direct jumps yet."); - await this.loadStarSystemFromSeed(seed); + const systemModel = getSystemModelFromCoordinates(starSystemCoordinates); + const systemController = new StarSystemController(systemModel, this.scene); + await this.loadStarSystem(systemController, true); this.initStarSystem(); this.spaceshipControls?.spaceship.hyperSpaceTunnel.setEnabled(false); @@ -404,10 +402,6 @@ export class StarSystemView implements View { */ } - public async loadStarSystemFromSeed(seed: SystemSeed) { - await this.loadStarSystem(new StarSystemController(seed, this.scene), true); - } - /** * Dispose the previous star system and incrementally loads the new star system. All the assets are instantiated but the system still need to be initialized * @param starSystem the star system to be set @@ -498,7 +492,7 @@ export class StarSystemView implements View { } // Space stations - if (systemModel instanceof SeededStarSystemModel && isSystemInHumanBubble(systemModel.getCoordinates())) { + if (isSystemInHumanBubble(systemModel.getCoordinates())) { const spaceStationPlaces = placeSpaceStations(systemModel); for (const planetModel of spaceStationPlaces) { const planet = planets.find((planet) => planet.model.name === planetModel.name); @@ -556,16 +550,14 @@ export class StarSystemView implements View { starSystem.initPostProcesses(this.postProcessManager); - if (starSystem.model instanceof SeededStarSystemModel) { - getNeighborStarSystemCoordinates(starSystem.model.getCoordinates(), Math.min(Settings.PLAYER_JUMP_RANGE_LY, Settings.VISIBLE_NEIGHBORHOOD_MAX_RADIUS_LY)).forEach( - ([neighborCoordinates, position, distance]) => { - const systemTarget = this.getStarSystem().addSystemTarget(neighborCoordinates); - this.targetCursorLayer.addObject(systemTarget, ObjectTargetCursorType.STAR_SYSTEM, 0, 0); - } - ); - } + getNeighborStarSystemCoordinates(starSystem.model.getCoordinates(), Math.min(Settings.PLAYER_JUMP_RANGE_LY, Settings.VISIBLE_NEIGHBORHOOD_MAX_RADIUS_LY)).forEach( + ([neighborCoordinates, position, distance]) => { + const systemTarget = this.getStarSystem().addSystemTarget(neighborCoordinates); + this.targetCursorLayer.addObject(systemTarget, ObjectTargetCursorType.STAR_SYSTEM, 0, 0); + } + ); - if (this.player.currentItinerary.length >= 2 && starSystem.model instanceof SeededStarSystemModel) { + if (this.player.currentItinerary.length >= 2) { const targetCoordinates = this.player.currentItinerary[1]; if (starSystemCoordinatesEquals(starSystem.model.getCoordinates(), targetCoordinates)) { // the current system was the first destination of the itinerary, we can remove the system before from the itinerary diff --git a/src/ts/starmap/starMap.ts b/src/ts/starmap/starMap.ts index d2ce74ac4..eca3c4f5d 100644 --- a/src/ts/starmap/starMap.ts +++ b/src/ts/starmap/starMap.ts @@ -18,7 +18,6 @@ import starTexture from "../../asset/textures/starParticle.png"; import blackHoleTexture from "../../asset/textures/blackholeParticleSmall.png"; -import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel"; import { BuildData, StarSector, vector3ToString } from "./starSector"; import { StarMapUI } from "./starMapUI"; import { Scene } from "@babylonjs/core/scene"; @@ -56,7 +55,7 @@ import { CameraRadiusAnimation } from "../uberCore/transforms/animations/radius" import { Camera } from "@babylonjs/core/Cameras/camera"; import { StellarPathfinder } from "./stellarPathfinder"; import { createNotification } from "../utils/notification"; -import { getStarGalacticPosition } from "../utils/getStarGalacticPositionFromSeed"; +import { getStarGalacticPosition, getSystemModelFromCoordinates } from "../utils/starSystemCoordinatesUtils"; import { Player } from "../player/player"; import { Settings } from "../settings"; import { StarSystemCoordinates, starSystemCoordinatesEquals } from "../starSystem/starSystemModel"; @@ -443,8 +442,7 @@ export class StarMap implements View { } private createInstance(data: BuildData) { - const starSystemSeed = data.seed; - const starSystemModel = new SeededStarSystemModel(starSystemSeed); + const starSystemModel = getSystemModelFromCoordinates(data.coordinates); const starSystemCoordinates = starSystemModel.getCoordinates(); const starSeed = starSystemModel.getStellarObjectSeed(0); diff --git a/src/ts/starmap/starMapUI.ts b/src/ts/starmap/starMapUI.ts index 6142c98ad..74a64e8f4 100644 --- a/src/ts/starmap/starMapUI.ts +++ b/src/ts/starmap/starMapUI.ts @@ -20,12 +20,11 @@ import { Scene } from "@babylonjs/core/scene"; import { Matrix, Vector3 } from "@babylonjs/core/Maths/math.vector"; import i18n from "../i18n"; import { getStellarTypeString } from "../stellarObjects/common"; -import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel"; import { StarModel } from "../stellarObjects/star/starModel"; import { BlackHoleModel } from "../stellarObjects/blackHole/blackHoleModel"; import { NeutronStarModel } from "../stellarObjects/neutronStar/neutronStarModel"; import { BodyType } from "../architecture/bodyType"; -import { getSeedFromCoordinates, getStarGalacticPosition } from "../utils/getStarGalacticPositionFromSeed"; +import { getStarGalacticPosition, getSystemModelFromCoordinates } from "../utils/starSystemCoordinatesUtils"; import { factionToString } from "../society/factions"; import { isSystemInHumanBubble } from "../society/starSystemSociety"; import { getSpaceStationModels } from "../utils/getModelsFromSystemModel"; @@ -305,11 +304,7 @@ export class StarMapUI { setSelectedSystem(targetSystemCoordinates: StarSystemCoordinates, currentSystemCoordinates: StarSystemCoordinates | null) { const targetPosition = getStarGalacticPosition(targetSystemCoordinates); - const targetSystemSeed = getSeedFromCoordinates(targetSystemCoordinates); - if (targetSystemSeed === null) { - throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet."); - } - const targetSystemModel = new SeededStarSystemModel(targetSystemSeed); + const targetSystemModel = getSystemModelFromCoordinates(targetSystemCoordinates); if (currentSystemCoordinates !== null) { const currentCoordinates = getStarGalacticPosition(currentSystemCoordinates); diff --git a/src/ts/starmap/starSector.ts b/src/ts/starmap/starSector.ts index 7d975f6af..96676c6c2 100644 --- a/src/ts/starmap/starSector.ts +++ b/src/ts/starmap/starSector.ts @@ -22,7 +22,7 @@ import { UniverseDensity } from "../settings"; import { Matrix, Vector3 } from "@babylonjs/core/Maths/math.vector"; import { InstancedMesh } from "@babylonjs/core/Meshes/instancedMesh"; import { BoundingBox } from "@babylonjs/core/Culling/boundingBox"; -import { SystemSeed } from "../utils/systemSeed"; +import { StarSystemCoordinates } from "../starSystem/starSystemModel"; export function vector3ToString(v: Vector3): string { return `${v.x},${v.y},${v.z}`; @@ -30,7 +30,7 @@ export function vector3ToString(v: Vector3): string { export type BuildData = { name: string; - seed: SystemSeed; + coordinates: StarSystemCoordinates; sectorString: string; position: Vector3; }; @@ -78,10 +78,18 @@ export class StarSector { const sectorString = this.getKey(); const data: BuildData[] = []; for (let i = 0; i < this.nbStars; i++) { - const systemSeed = new SystemSeed(this.coordinates.x, this.coordinates.y, this.coordinates.z, i); + const localPosition = this.getLocalPositionOfStar(i); + const coordinates: StarSystemCoordinates = { + starSectorX: this.coordinates.x, + starSectorY: this.coordinates.y, + starSectorZ: this.coordinates.z, + localX: localPosition.x, + localY: localPosition.y, + localZ: localPosition.z + }; data.push({ name: `starInstance|${this.coordinates.x}|${this.coordinates.y}|${this.coordinates.z}|${i}`, - seed: systemSeed, + coordinates: coordinates, sectorString: sectorString, position: this.getPositionOfStar(i) }); diff --git a/src/ts/starmap/stellarPathfinder.ts b/src/ts/starmap/stellarPathfinder.ts index 16caab320..84663c446 100644 --- a/src/ts/starmap/stellarPathfinder.ts +++ b/src/ts/starmap/stellarPathfinder.ts @@ -15,7 +15,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import { getStarGalacticPosition } from "../utils/getStarGalacticPositionFromSeed"; +import { getStarGalacticPosition } from "../utils/starSystemCoordinatesUtils"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { getNeighborStarSystemCoordinates } from "../utils/getNeighborStarSystems"; import { PriorityQueue } from "../utils/priorityQueue"; diff --git a/src/ts/tutorials/flightTutorial.ts b/src/ts/tutorials/flightTutorial.ts index b98f8177e..2d1c64c08 100644 --- a/src/ts/tutorials/flightTutorial.ts +++ b/src/ts/tutorials/flightTutorial.ts @@ -29,8 +29,7 @@ import warpImageSrc from "../../asset/tutorials/flightTutorial/warp.webp"; import congratsImageSrc from "../../asset/tutorials/flightTutorial/congrats.webp"; import { SystemObjectType } from "../saveFile/universeCoordinates"; import { getGlobalKeyboardLayoutMap } from "../utils/keyboardAPI"; -import { getStarSystemCoordinatesFromSeed } from "../utils/getStarGalacticPositionFromSeed"; -import { SystemSeed } from "../utils/systemSeed"; +import { getStarSystemCoordinatesFromSeed } from "../utils/starSystemCoordinatesUtils"; export const FlightTutorial: Tutorial = { title: i18n.t("tutorials:flightTutorial:title"), @@ -38,7 +37,12 @@ export const FlightTutorial: Tutorial = { description: i18n.t("tutorials:flightTutorial:description"), universeObjectId: { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(0, 0, 1, 1)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 0, + starSectorY: 0, + starSectorZ: 1, + index: 1 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 1 }, diff --git a/src/ts/ui/mainMenu.ts b/src/ts/ui/mainMenu.ts index 84677c98f..238b504ef 100644 --- a/src/ts/ui/mainMenu.ts +++ b/src/ts/ui/mainMenu.ts @@ -9,7 +9,6 @@ import { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { TransformRotationAnimation } from "../uberCore/transforms/animations/rotation"; import { TransformTranslationAnimation } from "../uberCore/transforms/animations/translation"; import { Observable } from "@babylonjs/core/Misc/observable"; -import { SystemSeed } from "../utils/systemSeed"; import { parseSaveFileData, SaveFileData } from "../saveFile/saveFileData"; import packageInfo from "../../../package.json"; import { Settings } from "../settings"; @@ -19,7 +18,7 @@ import { Sounds } from "../assets/sounds"; import { PanelType, SidePanels } from "./sidePanels"; import { SystemObjectType, UniverseObjectId } from "../saveFile/universeCoordinates"; import { getObjectBySystemId } from "../utils/orbitalObjectId"; -import { getSeedFromCoordinates, getStarSystemCoordinatesFromSeed } from "../utils/getStarGalacticPositionFromSeed"; +import { getStarSystemCoordinatesFromSeed, getSystemModelFromCoordinates } from "../utils/starSystemCoordinatesUtils"; export class MainMenu { readonly scene: UberScene; @@ -55,42 +54,82 @@ export class MainMenu { const allowedIdentifiers: UniverseObjectId[] = [ { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(1, 1, 0, 7)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 1, + starSectorY: 1, + starSectorZ: 0, + index: 7 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 1 }, { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(0, 0, 0, 0)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 0, + starSectorY: 0, + starSectorZ: 0, + index: 0 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 1 }, { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(0, 0, 1, 4)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 0, + starSectorY: 0, + starSectorZ: 1, + index: 4 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 3 }, { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(0, 0, 1, 9)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 0, + starSectorY: 0, + starSectorZ: 1, + index: 9 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 0 }, { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(0, 0, 1, 1)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 0, + starSectorY: 0, + starSectorZ: 1, + index: 1 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 1 }, { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(1, 1, 0, 12)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 1, + starSectorY: 1, + starSectorZ: 0, + index: 12 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 0 }, { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(1, 1, 0, 5)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 1, + starSectorY: 1, + starSectorZ: 0, + index: 5 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 0 }, { - starSystemCoordinates: getStarSystemCoordinatesFromSeed(new SystemSeed(0, 0, 0, 17)), + starSystemCoordinates: getStarSystemCoordinatesFromSeed({ + starSectorX: 0, + starSectorY: 0, + starSectorZ: 0, + index: 17 + }), objectType: SystemObjectType.PLANETARY_MASS_OBJECT, objectIndex: 2 } @@ -99,11 +138,8 @@ export class MainMenu { const randomIndex = Math.floor(Math.random() * allowedIdentifiers.length); this.universeObjectId = allowedIdentifiers[randomIndex]; const coordinates = this.universeObjectId.starSystemCoordinates; - const seed = getSeedFromCoordinates(coordinates); - if (seed === null) { - throw new Error("No seed found for coordinates. Custom star systems are not supported in the main menu yet."); - } - this.starSystemController = new StarSystemController(seed, this.scene); + const systemModel = getSystemModelFromCoordinates(coordinates); + this.starSystemController = new StarSystemController(systemModel, this.scene); document.body.insertAdjacentHTML("beforeend", mainMenuHTML); diff --git a/src/ts/ui/spaceStation/spaceStationMissions.ts b/src/ts/ui/spaceStation/spaceStationMissions.ts index 8d2e418a8..4de725d10 100644 --- a/src/ts/ui/spaceStation/spaceStationMissions.ts +++ b/src/ts/ui/spaceStation/spaceStationMissions.ts @@ -1,6 +1,5 @@ import { SpaceStationModel } from "../../spacestation/spacestationModel"; import { getNeighborStarSystemCoordinates } from "../../utils/getNeighborStarSystems"; -import { SeededStarSystemModel } from "../../starSystem/seededStarSystemModel"; import { parseDistance } from "../../utils/parseToStrings"; import { Settings } from "../../settings"; import { uniformRandBool } from "extended-random"; @@ -8,7 +7,7 @@ import { getSpaceStationModels } from "../../utils/getModelsFromSystemModel"; import { generateSightseeingMissions } from "../../missions/generateSightSeeingMissions"; import { Player } from "../../player/player"; import { MissionContainer } from "./missionContainer"; -import { getSeedFromCoordinates } from "../../utils/getStarGalacticPositionFromSeed"; +import { getSystemModelFromCoordinates } from "../../utils/starSystemCoordinatesUtils"; /** * Generates all missions available at the given space station for the player. Missions are generated based on the current timestamp (hourly basis). @@ -20,18 +19,11 @@ export function generateMissionsDom(stationModel: SpaceStationModel, player: Pla const sightSeeingMissions = generateSightseeingMissions(stationModel, player, Date.now()); const starSystem = stationModel.starSystem; - if (!(starSystem instanceof SeededStarSystemModel)) { - throw new Error("Star system is not seeded, hence missions cannot be generated"); - } const neighborSystems = getNeighborStarSystemCoordinates(starSystem.getCoordinates(), 75); let neighborSpaceStations: [SpaceStationModel, number][] = []; neighborSystems.forEach(([coordinates, position, distance], index) => { - const seed = getSeedFromCoordinates(coordinates); - if (seed === null) { - throw new Error("Seed is null. Custom star systems are not supported yet."); - } - const systemModel = new SeededStarSystemModel(seed); + const systemModel = getSystemModelFromCoordinates(coordinates); const spaceStations = getSpaceStationModels(systemModel).map<[SpaceStationModel, number]>((stationModel) => { return [stationModel, distance]; }); diff --git a/src/ts/ui/tutorial/tutorialsPanelContent.ts b/src/ts/ui/tutorial/tutorialsPanelContent.ts index 574a8c807..be73f4210 100644 --- a/src/ts/ui/tutorial/tutorialsPanelContent.ts +++ b/src/ts/ui/tutorial/tutorialsPanelContent.ts @@ -8,7 +8,7 @@ import { Tutorial } from "../../tutorials/tutorial"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { SystemObjectType } from "../../saveFile/universeCoordinates"; import { OrbitalObject } from "../../architecture/orbitalObject"; -import { getSeedFromCoordinates } from "../../utils/getStarGalacticPositionFromSeed"; +import { getSystemModelFromCoordinates } from "../../utils/starSystemCoordinatesUtils"; export class TutorialsPanelContent { readonly htmlRoot: HTMLElement; @@ -44,11 +44,8 @@ export class TutorialsPanelContent { if (tutorial.universeObjectId !== undefined) { const engine = starSystemView.scene.getEngine(); engine.displayLoadingUI(); - const systemSeed = getSeedFromCoordinates(tutorial.universeObjectId.starSystemCoordinates); - if (systemSeed === null) { - throw new Error("No seed found for coordinates. Custom star systems are not supported in tutorials yet."); - } - await starSystemView.loadStarSystem(new StarSystemController(systemSeed, starSystemView.scene), true); + const systemModel = getSystemModelFromCoordinates(tutorial.universeObjectId.starSystemCoordinates); + await starSystemView.loadStarSystem(new StarSystemController(systemModel, starSystemView.scene), true); starSystemView.initStarSystem(); engine.hideLoadingUI(); diff --git a/src/ts/utils/getModelsFromSystemModel.ts b/src/ts/utils/getModelsFromSystemModel.ts index 39eb86ea3..fa9cdfeb1 100644 --- a/src/ts/utils/getModelsFromSystemModel.ts +++ b/src/ts/utils/getModelsFromSystemModel.ts @@ -1,7 +1,6 @@ import { placeSpaceStations } from "../society/spaceStationPlacement"; import { SpaceStationModel } from "../spacestation/spacestationModel"; import { getMoonSeeds, getSpaceStationSeed } from "../planets/common"; -import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel"; import { StarModel } from "../stellarObjects/star/starModel"; import { BlackHoleModel } from "../stellarObjects/blackHole/blackHoleModel"; import { BodyType } from "../architecture/bodyType"; @@ -13,15 +12,16 @@ import { JuliaSetModel } from "../anomalies/julia/juliaSetModel"; import { PlanetModel } from "../architecture/planet"; import { TelluricPlanetModel } from "../planets/telluricPlanet/telluricPlanetModel"; import { GasPlanetModel } from "../planets/gasPlanet/gasPlanetModel"; +import { StarSystemModel } from "../starSystem/starSystemModel"; -export function getSpaceStationModels(system: SeededStarSystemModel): SpaceStationModel[] { +export function getSpaceStationModels(system: StarSystemModel): SpaceStationModel[] { const spaceStationParents = placeSpaceStations(system); return spaceStationParents.map((planet) => { return new SpaceStationModel(getSpaceStationSeed(planet, 0), system, planet); }); } -export function getStellarObjectModels(system: SeededStarSystemModel): StellarObjectModel[] { +export function getStellarObjectModels(system: StarSystemModel): StellarObjectModel[] { const stellarObjectSeedAndTypes = system.getStellarObjects(); const stellarObjectModels: StellarObjectModel[] = []; for (let i = 0; i < stellarObjectSeedAndTypes.length; i++) { @@ -45,7 +45,7 @@ export function getStellarObjectModels(system: SeededStarSystemModel): StellarOb return stellarObjectModels; } -export function getAnomalyModels(system: SeededStarSystemModel): (MandelbulbModel | JuliaSetModel)[] { +export function getAnomalyModels(system: StarSystemModel): (MandelbulbModel | JuliaSetModel)[] { const anomalySeedAndTypes = system.getAnomalies(); const anomalyModels: (MandelbulbModel | JuliaSetModel)[] = []; const parentBodyModel = getStellarObjectModels(system)[0]; @@ -66,7 +66,7 @@ export function getAnomalyModels(system: SeededStarSystemModel): (MandelbulbMode return anomalyModels; } -export function getPlanetaryMassObjectModels(system: SeededStarSystemModel): PlanetModel[] { +export function getPlanetaryMassObjectModels(system: StarSystemModel): PlanetModel[] { const planetSeedAndTypes = system.getPlanets(); const planetModels: PlanetModel[] = []; const parentBodyModel = getStellarObjectModels(system)[0]; diff --git a/src/ts/utils/getNeighborStarSystems.ts b/src/ts/utils/getNeighborStarSystems.ts index ccb2e1365..96495bd71 100644 --- a/src/ts/utils/getNeighborStarSystems.ts +++ b/src/ts/utils/getNeighborStarSystems.ts @@ -1,4 +1,4 @@ -import { getStarGalacticPosition } from "./getStarGalacticPositionFromSeed"; +import { getStarGalacticPosition } from "./starSystemCoordinatesUtils"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { StarSector } from "../starmap/starSector"; import { StarSystemCoordinates, starSystemCoordinatesEquals } from "../starSystem/starSystemModel"; diff --git a/src/ts/utils/orbitalObjectId.ts b/src/ts/utils/orbitalObjectId.ts index 71f3a9728..50227178d 100644 --- a/src/ts/utils/orbitalObjectId.ts +++ b/src/ts/utils/orbitalObjectId.ts @@ -5,10 +5,9 @@ import { SystemObjectId, UniverseObjectId, SystemObjectType } from "../saveFile/ import { Planet } from "../architecture/planet"; import { Anomaly } from "../anomalies/anomaly"; import { SpaceStation } from "../spacestation/spaceStation"; -import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel"; import { getAnomalyModels, getPlanetaryMassObjectModels, getSpaceStationModels, getStellarObjectModels } from "./getModelsFromSystemModel"; import { SpaceStationModel } from "../spacestation/spacestationModel"; -import { getSeedFromCoordinates, getStarSystemCoordinatesFromSeed } from "./getStarGalacticPositionFromSeed"; +import { getSystemModelFromCoordinates } from "./starSystemCoordinatesUtils"; /** * Get the object ID of the given orbital object within the star system. @@ -40,13 +39,9 @@ export function getSystemObjectId(orbitalObject: OrbitalObject, starSystem: Star * @param starSystem The star system controller. */ export function getUniverseObjectId(orbitalObject: OrbitalObject, starSystem: StarSystemController): UniverseObjectId { - if (!(starSystem.model instanceof SeededStarSystemModel)) { - throw new Error("Star system is not a seeded star system model"); - } - return { ...getSystemObjectId(orbitalObject, starSystem), - starSystemCoordinates: getStarSystemCoordinatesFromSeed(starSystem.model.seed) + starSystemCoordinates: starSystem.model.getCoordinates() }; } @@ -77,21 +72,17 @@ export function getObjectBySystemId(systemObjectId: SystemObjectId, starSystem: export function getObjectModelByUniverseId(universeObjectId: UniverseObjectId): OrbitalObjectModel { const starSystemCoordinates = universeObjectId.starSystemCoordinates; - const starSystemSeed = getSeedFromCoordinates(starSystemCoordinates); - if (starSystemSeed === null) { - throw new Error("Could not find star system seed from coordinates. Custom star systems are not supported yet."); - } - const seededStarSystemModel = new SeededStarSystemModel(starSystemSeed); + const starSystemModel = getSystemModelFromCoordinates(starSystemCoordinates); switch (universeObjectId.objectType) { case SystemObjectType.STELLAR_OBJECT: - return getStellarObjectModels(seededStarSystemModel)[universeObjectId.objectIndex]; + return getStellarObjectModels(starSystemModel)[universeObjectId.objectIndex]; case SystemObjectType.PLANETARY_MASS_OBJECT: - return getPlanetaryMassObjectModels(seededStarSystemModel)[universeObjectId.objectIndex]; + return getPlanetaryMassObjectModels(starSystemModel)[universeObjectId.objectIndex]; case SystemObjectType.ANOMALY: - return getAnomalyModels(seededStarSystemModel)[universeObjectId.objectIndex]; + return getAnomalyModels(starSystemModel)[universeObjectId.objectIndex]; case SystemObjectType.SPACE_STATION: - return getSpaceStationModels(seededStarSystemModel)[universeObjectId.objectIndex]; + return getSpaceStationModels(starSystemModel)[universeObjectId.objectIndex]; default: throw new Error(`Unknown universe object type: ${universeObjectId.objectType}`); } @@ -99,9 +90,6 @@ export function getObjectModelByUniverseId(universeObjectId: UniverseObjectId): export function getUniverseIdForSpaceStationModel(spaceStationModel: SpaceStationModel): UniverseObjectId { const systemModel = spaceStationModel.starSystem; - if (!(systemModel instanceof SeededStarSystemModel)) { - throw new Error("Cannot handle non-seeded star system models yet"); - } const spaceStationModels = getSpaceStationModels(systemModel); const index = spaceStationModels.findIndex((model) => model.seed === spaceStationModel.seed); @@ -112,6 +100,6 @@ export function getUniverseIdForSpaceStationModel(spaceStationModel: SpaceStatio return { objectType: SystemObjectType.SPACE_STATION, objectIndex: index, - starSystemCoordinates: getStarSystemCoordinatesFromSeed(systemModel.seed) + starSystemCoordinates: systemModel.getCoordinates() }; } diff --git a/src/ts/utils/getStarGalacticPositionFromSeed.ts b/src/ts/utils/starSystemCoordinatesUtils.ts similarity index 69% rename from src/ts/utils/getStarGalacticPositionFromSeed.ts rename to src/ts/utils/starSystemCoordinatesUtils.ts index 08b9f780a..2cf0d678c 100644 --- a/src/ts/utils/getStarGalacticPositionFromSeed.ts +++ b/src/ts/utils/starSystemCoordinatesUtils.ts @@ -1,7 +1,7 @@ -import { SystemSeed } from "./systemSeed"; import { StarSector } from "../starmap/starSector"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; import { StarSystemCoordinates } from "../starSystem/starSystemModel"; +import { SeededStarSystemModel, SystemSeed } from "../starSystem/seededStarSystemModel"; export function getStarSystemCoordinatesFromSeed(systemSeed: SystemSeed): StarSystemCoordinates { const starSector = new StarSector(new Vector3(systemSeed.starSectorX, systemSeed.starSectorY, systemSeed.starSectorZ)); @@ -34,14 +34,28 @@ export function getStarGalacticPosition(coordinates: StarSystemCoordinates) { * @param coordinates The coordinates of the system. * @returns The seed of the system, or null if not found. */ -export function getSeedFromCoordinates(coordinates: StarSystemCoordinates) { +export function getSeedFromCoordinates(coordinates: StarSystemCoordinates): SystemSeed | null { const starSector = new StarSector(new Vector3(coordinates.starSectorX, coordinates.starSectorY, coordinates.starSectorZ)); const localPositions = starSector.getLocalPositionsOfStars(); - for (const localPosition of localPositions) { + for (let i = 0; i < localPositions.length; i++) { + const localPosition = localPositions[i]; if (localPosition.equals(new Vector3(coordinates.localX, coordinates.localY, coordinates.localZ))) { - return new SystemSeed(coordinates.starSectorX, coordinates.starSectorY, coordinates.starSectorZ, localPositions.indexOf(localPosition)); + return { + starSectorX: coordinates.starSectorX, + starSectorY: coordinates.starSectorY, + starSectorZ: coordinates.starSectorZ, + index: i + }; } } return null; } + +export function getSystemModelFromCoordinates(coordinates: StarSystemCoordinates) { + const seed = getSeedFromCoordinates(coordinates); + if (seed === null) { + throw new Error("No seed found for coordinates. Custom star systems are not supported in system targets yet."); + } + return new SeededStarSystemModel(seed); +} diff --git a/src/ts/utils/systemSeed.ts b/src/ts/utils/systemSeed.ts deleted file mode 100644 index 4a2c74275..000000000 --- a/src/ts/utils/systemSeed.ts +++ /dev/null @@ -1,76 +0,0 @@ -// This file is part of Cosmos Journeyer -// -// Copyright (C) 2024 Barthélemy Paléologue -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -import { seededSquirrelNoise } from "squirrel-noise"; -import { hashVec3 } from "./hashVec3"; -import { centeredRand } from "extended-random"; -import { Settings } from "../settings"; - -export type SystemSeedSerialized = { - starSectorX: number; - starSectorY: number; - starSectorZ: number; - index: number; -}; - -export function systemSeedSerializedEquals(a: SystemSeedSerialized, b: SystemSeedSerialized): boolean { - return a.starSectorX === b.starSectorX && a.starSectorY === b.starSectorY && a.starSectorZ === b.starSectorZ && a.index === b.index; -} - -export class SystemSeed { - readonly starSectorX: number; - readonly starSectorY: number; - readonly starSectorZ: number; - readonly index: number; - - readonly hash: number; - - constructor(starSectorX: number, starSectorY: number, starSectorZ: number, index: number) { - this.starSectorX = starSectorX; - this.starSectorY = starSectorY; - this.starSectorZ = starSectorZ; - this.index = index; - - if (!Number.isSafeInteger(this.starSectorX)) throw new Error("x coordinate of star sector is not a safe integer"); - if (!Number.isSafeInteger(this.starSectorY)) throw new Error("y coordinate of star sector is not a safe integer"); - if (!Number.isSafeInteger(this.starSectorZ)) throw new Error("z coordinate of star sector is not a safe integer"); - - const cellRNG = seededSquirrelNoise(hashVec3(starSectorX, starSectorY, starSectorZ)); - this.hash = centeredRand(cellRNG, 1 + index) * Settings.SEED_HALF_RANGE; - } - - equals(other: SystemSeed): boolean { - return this.starSectorX === other.starSectorX && this.starSectorY === other.starSectorY && this.starSectorZ === other.starSectorZ && this.index === other.index; - } - - toString(): string { - return `${this.starSectorX},${this.starSectorY},${this.starSectorZ},${this.index}`; - } - - serialize(): SystemSeedSerialized { - return { - starSectorX: this.starSectorX, - starSectorY: this.starSectorY, - starSectorZ: this.starSectorZ, - index: this.index - }; - } - - static Deserialize(data: SystemSeedSerialized): SystemSeed { - return new SystemSeed(data.starSectorX, data.starSectorY, data.starSectorZ, data.index); - } -} diff --git a/src/ts/utils/systemTarget.ts b/src/ts/utils/systemTarget.ts index ec93b8f5d..504edad92 100644 --- a/src/ts/utils/systemTarget.ts +++ b/src/ts/utils/systemTarget.ts @@ -3,10 +3,9 @@ import { TransformNode } from "@babylonjs/core/Meshes"; import { Transformable } from "../architecture/transformable"; import { HasBoundingSphere } from "../architecture/hasBoundingSphere"; import { TypedObject } from "../architecture/typedObject"; -import { SeededStarSystemModel } from "../starSystem/seededStarSystemModel"; import i18n from "../i18n"; import { StarSystemCoordinates } from "../starSystem/starSystemModel"; -import { getSeedFromCoordinates } from "./getStarGalacticPositionFromSeed"; +import { getSystemModelFromCoordinates } from "./starSystemCoordinatesUtils"; export class SystemTarget implements Transformable, HasBoundingSphere, TypedObject { readonly name: string; @@ -15,11 +14,7 @@ export class SystemTarget implements Transformable, HasBoundingSphere, TypedObje readonly systemCoordinates: StarSystemCoordinates; constructor(systemCoordinates: StarSystemCoordinates, scene: Scene) { - const seed = getSeedFromCoordinates(systemCoordinates); - if (seed === null) { - throw new Error("No seed found for coordinates. Custom star systems are not supported in system targets yet."); - } - const systemModel = new SeededStarSystemModel(seed); + const systemModel = getSystemModelFromCoordinates(systemCoordinates); this.name = systemModel.name; this.transform = new TransformNode(this.name, scene); this.systemCoordinates = systemCoordinates; diff --git a/tests/neighborStarSystems.unit.test.ts b/tests/neighborStarSystems.unit.test.ts index f8a12d825..da6762914 100644 --- a/tests/neighborStarSystems.unit.test.ts +++ b/tests/neighborStarSystems.unit.test.ts @@ -15,14 +15,18 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import { SystemSeed } from "../src/ts/utils/systemSeed"; import { getNeighborStarSystemCoordinates } from "../src/ts/utils/getNeighborStarSystems"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; -import { getStarSystemCoordinatesFromSeed } from "../src/ts/utils/getStarGalacticPositionFromSeed"; +import { getStarSystemCoordinatesFromSeed } from "../src/ts/utils/starSystemCoordinatesUtils"; import { starSystemCoordinatesEquals } from "../src/ts/starSystem/starSystemModel"; test("getNeighborStarSystemCoordinates", () => { - const systemCoordinates = getStarSystemCoordinatesFromSeed(new SystemSeed(0, 0, 0, 0)); + const systemCoordinates = getStarSystemCoordinatesFromSeed({ + starSectorX: 0, + starSectorY: 0, + starSectorZ: 0, + index: 0 + }); for (let i = 0; i < 10; i++) { const searchRadius = 5 * i;