Skip to content

Commit

Permalink
implement equality at mission node level
Browse files Browse the repository at this point in the history
this allows to check equality between 2 mission trees.

States are not taken into account. It the same tasks are required the 2 missions are equal
  • Loading branch information
BarthPaleologue committed Oct 9, 2024
1 parent df789fd commit 5f45c91
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 19 deletions.
16 changes: 6 additions & 10 deletions src/ts/missions/mission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export type MissionSerialized = {
reward: number;
};

export class Mission implements Mission {
private readonly tree: MissionNode;
export class Mission {
readonly tree: MissionNode;

private readonly reward: number;
readonly reward: number;

private readonly missionGiver: SpaceStationModel;
readonly missionGiver: SpaceStationModel;

private readonly missionType: MissionType;
readonly missionType: MissionType;

constructor(tree: MissionNode, reward: number, missionGiver: SpaceStationModel, missionType: MissionType) {
this.tree = tree;
Expand All @@ -41,11 +41,7 @@ export class Mission implements Mission {
}

equals(other: Mission): boolean {
return false;
}

getMissionGiver(): SpaceStationModel {
return this.missionGiver;
return this.tree.equals(other.tree);
}

getReward(): number {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MissionNode, MissionNodeSerialized, MissionNodeType } from "../../missionNode";
import { MissionContext } from "../../../missionContext";
import { UniverseObjectId } from "../../../../saveFile/universeCoordinates";
import { UniverseObjectId, universeObjectIdEquals } from "../../../../saveFile/universeCoordinates";
import { SeededStarSystemModel } from "../../../../starSystem/seededStarSystemModel";
import { SystemSeed } from "../../../../utils/systemSeed";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
Expand Down Expand Up @@ -45,6 +45,11 @@ export class MissionAsteroidFieldNode implements MissionNode {
return this.state === AsteroidFieldMissionState.CLOSE_ENOUGH;
}

equals(other: MissionNode): boolean {
if (!(other instanceof MissionAsteroidFieldNode)) return false;
return universeObjectIdEquals(this.objectId, other.objectId);
}

updateState(context: MissionContext) {
if (this.isCompleted()) return;

Expand Down Expand Up @@ -103,7 +108,7 @@ export class MissionAsteroidFieldNode implements MissionNode {
return i18n.t("missions:sightseeing:describeAsteroidFieldTrek", {
objectName: objectModel.name,
systemName: systemModel.name,
distance: distance > 0 ? parseDistance(distance * Settings.LIGHT_YEAR): i18n.t("missions:common:here")
distance: distance > 0 ? parseDistance(distance * Settings.LIGHT_YEAR) : i18n.t("missions:common:here")
});
}

Expand Down
9 changes: 7 additions & 2 deletions src/ts/missions/nodes/actions/sightseeing/missionFlyByNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MissionNode, MissionNodeSerialized, MissionNodeType } from "../../missionNode";
import { MissionContext } from "../../../missionContext";
import { UniverseObjectId } from "../../../../saveFile/universeCoordinates";
import { UniverseObjectId, universeObjectIdEquals } from "../../../../saveFile/universeCoordinates";
import { SeededStarSystemModel } from "../../../../starSystem/seededStarSystemModel";
import { SystemSeed } from "../../../../utils/systemSeed";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
Expand Down Expand Up @@ -44,6 +44,11 @@ export class MissionFlyByNode implements MissionNode {
return this.state === FlyByState.CLOSE_ENOUGH;
}

equals(other: MissionNode): boolean {
if (!(other instanceof MissionFlyByNode)) return false;
return universeObjectIdEquals(this.objectId, other.objectId);
}

updateState(context: MissionContext) {
if (this.isCompleted()) return;

Expand Down Expand Up @@ -82,7 +87,7 @@ export class MissionFlyByNode implements MissionNode {
return i18n.t("missions:sightseeing:describeFlyBy", {
objectType: objectModel.typeName.toLowerCase(),
systemName: systemModel.name,
distance: distance > 0 ? parseDistance(distance * Settings.LIGHT_YEAR): i18n.t("missions:common:here")
distance: distance > 0 ? parseDistance(distance * Settings.LIGHT_YEAR) : i18n.t("missions:common:here")
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MissionNode, MissionNodeSerialized, MissionNodeType } from "../../missionNode";
import { MissionContext } from "../../../missionContext";
import { UniverseObjectId } from "../../../../saveFile/universeCoordinates";
import { UniverseObjectId, universeObjectIdEquals } from "../../../../saveFile/universeCoordinates";
import { SeededStarSystemModel } from "../../../../starSystem/seededStarSystemModel";
import { SystemSeed } from "../../../../utils/systemSeed";
import { Vector3 } from "@babylonjs/core/Maths/math.vector";
Expand Down Expand Up @@ -47,6 +47,11 @@ export class MissionTerminatorLandingNode implements MissionNode {
return this.state === LandMissionState.LANDED;
}

equals(other: MissionNode): boolean {
if (!(other instanceof MissionTerminatorLandingNode)) return false;
return universeObjectIdEquals(this.objectId, other.objectId);
}

updateState(context: MissionContext) {
if (this.isCompleted()) return;

Expand Down Expand Up @@ -111,7 +116,7 @@ export class MissionTerminatorLandingNode implements MissionNode {
return i18n.t("missions:sightseeing:describeTerminatorLanding", {
objectName: objectModel.name,
systemName: systemModel.name,
distance: distance > 0 ? parseDistance(distance * Settings.LIGHT_YEAR): i18n.t("missions:common:here")
distance: distance > 0 ? parseDistance(distance * Settings.LIGHT_YEAR) : i18n.t("missions:common:here")
});
}

Expand Down
11 changes: 10 additions & 1 deletion src/ts/missions/nodes/logic/missionAndNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import i18n from "../../../i18n";
export type MissionAndNodeSerialized = MissionNodeSerialized;

export class MissionAndNode implements MissionNode {
public children: MissionNode[];
readonly children: MissionNode[];

private hasCompletedLock = false;

Expand All @@ -18,6 +18,15 @@ export class MissionAndNode implements MissionNode {
return this.hasCompletedLock;
}

equals(other: MissionNode): boolean {
if (!(other instanceof MissionAndNode)) return false;
if (this.children.length !== other.children.length) return false;
for (let i = 0; i < this.children.length; i++) {
if (!this.children[i].equals(other.children[i])) return false;
}
return true;
}

updateState(context: MissionContext) {
if (this.hasCompletedLock) return;
this.children.forEach((child) => child.updateState(context));
Expand Down
11 changes: 10 additions & 1 deletion src/ts/missions/nodes/logic/missionOrNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SystemSeed } from "../../../utils/systemSeed";
export type MissionOrNodeSerialized = MissionNodeSerialized;

export class MissionOrNode implements MissionNode {
public children: MissionNode[];
readonly children: MissionNode[];

private hasCompletedLock = false;

Expand All @@ -18,6 +18,15 @@ export class MissionOrNode implements MissionNode {
return this.hasCompletedLock;
}

equals(other: MissionNode): boolean {
if (!(other instanceof MissionOrNode)) return false;
if (this.children.length !== other.children.length) return false;
for (let i = 0; i < this.children.length; i++) {
if (!this.children[i].equals(other.children[i])) return false;
}
return true;
}

updateState(context: MissionContext) {
if (this.hasCompletedLock) return;
this.children.forEach((child) => child.updateState(context));
Expand Down
9 changes: 9 additions & 0 deletions src/ts/missions/nodes/logic/missionSequenceNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export class MissionSequenceNode implements MissionNode {
return this.hasCompletedLock;
}

equals(other: MissionNode): boolean {
if (!(other instanceof MissionSequenceNode)) return false;
if (this.children.length !== other.children.length) return false;
for (let i = 0; i < this.children.length; i++) {
if (!this.children[i].equals(other.children[i])) return false;
}
return true;
}

updateState(context: MissionContext) {
if (this.hasCompletedLock) return;
if (this.activeChildIndex >= this.children.length) return;
Expand Down
11 changes: 10 additions & 1 deletion src/ts/missions/nodes/logic/missionXorNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SystemSeed } from "../../../utils/systemSeed";
export type MissionXorNodeSerialized = MissionNodeSerialized;

export class MissionXorNode implements MissionNode {
public children: MissionNode[];
readonly children: MissionNode[];

private hasCompletedLock = false;

Expand All @@ -17,6 +17,15 @@ export class MissionXorNode implements MissionNode {
return this.hasCompletedLock;
}

equals(other: MissionNode): boolean {
if (!(other instanceof MissionXorNode)) return false;
if (this.children.length !== other.children.length) return false;
for (let i = 0; i < this.children.length; i++) {
if (!this.children[i].equals(other.children[i])) return false;
}
return true;
}

updateState(context: MissionContext) {
if (this.hasCompletedLock) return;
this.children.forEach((child) => child.updateState(context));
Expand Down
2 changes: 2 additions & 0 deletions src/ts/missions/nodes/missionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface MissionNode {

updateState(context: MissionContext): void;

equals(other: MissionNode): boolean;

describe(originSeed: SystemSeed): string;

describeNextTask(context: MissionContext): Promise<string>;
Expand Down

0 comments on commit 5f45c91

Please sign in to comment.