2018-12-07 11:54:26 +01:00
|
|
|
/**
|
|
|
|
* Initialization and manipulation of the Factions object, which stores data
|
|
|
|
* about all Factions in the game
|
|
|
|
*/
|
|
|
|
import { Faction } from "./Faction";
|
|
|
|
import { FactionInfos } from "./FactionInfo";
|
|
|
|
|
2021-09-25 20:42:57 +02:00
|
|
|
import { Reviver } from "../utils/JSONReviver";
|
2023-06-16 23:52:42 +02:00
|
|
|
import { getRecordValues } from "../Types/Record";
|
|
|
|
import { Augmentations, initCircadianModulator } from "../Augmentation/Augmentations";
|
2018-12-07 11:54:26 +01:00
|
|
|
|
2022-10-03 18:12:16 +02:00
|
|
|
export let Factions: Record<string, Faction> = {};
|
2018-12-07 11:54:26 +01:00
|
|
|
|
|
|
|
export function loadFactions(saveString: string): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
Factions = JSON.parse(saveString, Reviver);
|
2021-09-25 23:29:51 +02:00
|
|
|
// safety check for when we load older save file that don't have newer factions
|
|
|
|
for (const faction of Object.keys(Factions)) {
|
|
|
|
try {
|
|
|
|
Factions[faction].getInfo();
|
|
|
|
} catch (err) {
|
|
|
|
console.error("deleting " + faction);
|
|
|
|
delete Factions[faction];
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 11:54:26 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:45:50 +02:00
|
|
|
function AddToFactions(faction: Faction): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
const name: string = faction.name;
|
|
|
|
Factions[name] = faction;
|
2018-12-07 11:54:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function factionExists(name: string): boolean {
|
2023-05-05 09:55:59 +02:00
|
|
|
return Object.hasOwn(Factions, name);
|
2018-12-07 11:54:26 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function initFactions(): void {
|
2022-01-16 01:45:03 +01:00
|
|
|
for (const name of Object.keys(FactionInfos)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
resetFaction(new Faction(name));
|
|
|
|
}
|
2018-12-07 11:54:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Resets a faction during (re-)initialization. Saves the favor in the new
|
|
|
|
//Faction object and deletes the old Faction Object from "Factions". Then
|
|
|
|
//reinserts the new Faction object
|
2021-10-15 00:45:50 +02:00
|
|
|
function resetFaction(newFactionObject: Faction): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
const factionName: string = newFactionObject.name;
|
|
|
|
if (factionExists(factionName)) {
|
|
|
|
newFactionObject.favor = Factions[factionName].favor;
|
|
|
|
delete Factions[factionName];
|
|
|
|
}
|
|
|
|
AddToFactions(newFactionObject);
|
2023-06-16 23:52:42 +02:00
|
|
|
// All factions are added, this is a good place to add augs back to factions.
|
|
|
|
initCircadianModulator();
|
|
|
|
for (const aug of getRecordValues(Augmentations)) {
|
|
|
|
for (const factionName of aug.factions) {
|
|
|
|
const faction = Factions[factionName];
|
|
|
|
if (!faction) {
|
|
|
|
console.error(`Faction ${factionName} did not exist while adding augs to factions`);
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-20 13:31:45 +02:00
|
|
|
faction.augmentations.add(aug.name);
|
2023-06-16 23:52:42 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 11:54:26 +01:00
|
|
|
}
|