bitburner-src/src/Faction/FactionHelpers.jsx

244 lines
8.5 KiB
React
Raw Normal View History

2019-04-14 11:08:10 +02:00
import React from "react";
import ReactDOM from "react-dom";
2021-09-17 08:04:44 +02:00
import { FactionRoot } from "./ui/FactionRoot";
2019-04-14 11:08:10 +02:00
import { Augmentations } from "../Augmentation/Augmentations";
import { PlayerOwnedAugmentation } from "../Augmentation/PlayerOwnedAugmentation";
import { AugmentationNames } from "../Augmentation/data/AugmentationNames";
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
import { CONSTANTS } from "../Constants";
import { Engine } from "../engine";
import { Faction } from "./Faction";
import { Factions } from "./Factions";
import { HackingMission, setInMission } from "../Missions";
import { Player } from "../Player";
import { Settings } from "../Settings/Settings";
2021-09-05 01:09:30 +02:00
import {
getHackingWorkRepGain,
getFactionSecurityWorkRepGain,
getFactionFieldWorkRepGain,
} from "../PersonObjects/formulas/reputation";
2021-08-15 08:14:07 +02:00
import { SourceFileFlags } from "../SourceFile/SourceFileFlags";
2019-04-14 11:08:10 +02:00
import { dialogBoxCreate } from "../../utils/DialogBox";
2021-09-13 00:03:07 +02:00
import { createPopup } from "../ui/React/createPopup";
import { InvitationPopup } from "./ui/InvitationPopup";
2019-04-14 11:08:10 +02:00
export function inviteToFaction(faction) {
2021-09-13 00:03:07 +02:00
Player.factionInvitations.push(faction.name);
faction.alreadyInvited = true;
if (!Settings.SuppressFactionInvites) {
const popupId = "faction-invitation";
createPopup(popupId, InvitationPopup, {
player: Player,
faction: faction,
popupId: popupId,
});
2021-09-05 01:09:30 +02:00
}
2019-04-14 11:08:10 +02:00
}
export function joinFaction(faction) {
2021-09-05 01:09:30 +02:00
if (faction.isMember) return;
faction.isMember = true;
Player.factions.push(faction.name);
const factionInfo = faction.getInfo();
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
//Determine what factions you are banned from now that you have joined this faction
for (const i in factionInfo.enemies) {
const enemy = factionInfo.enemies[i];
if (Factions[enemy] instanceof Faction) {
Factions[enemy].isBanned = true;
2019-04-14 11:08:10 +02:00
}
2021-09-05 01:09:30 +02:00
}
for (var i = 0; i < Player.factionInvitations.length; ++i) {
2021-09-09 05:47:34 +02:00
if (Player.factionInvitations[i] == faction.name || Factions[Player.factionInvitations[i]].isBanned) {
2021-09-05 01:09:30 +02:00
Player.factionInvitations.splice(i, 1);
i--;
}
2021-09-05 01:09:30 +02:00
}
2019-04-14 11:08:10 +02:00
}
export function startHackingMission(faction) {
2021-09-05 01:09:30 +02:00
const mission = new HackingMission(faction.playerReputation, faction);
setInMission(true, mission); //Sets inMission flag to true
mission.init();
2019-04-14 11:08:10 +02:00
}
//Displays the HTML content for a specific faction
2021-09-09 05:47:34 +02:00
export function displayFactionContent(factionName, initiallyOnAugmentationsPage = false) {
2021-09-05 01:09:30 +02:00
const faction = Factions[factionName];
if (faction == null) {
2021-09-09 05:47:34 +02:00
throw new Error(`Invalid factionName passed into displayFactionContent(): ${factionName}`);
2021-09-05 01:09:30 +02:00
}
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
if (!faction.isMember) {
2021-09-09 05:47:34 +02:00
throw new Error(`Not a member of this faction. Cannot display faction information`);
2021-09-05 01:09:30 +02:00
}
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
ReactDOM.render(
<FactionRoot
engine={Engine}
initiallyOnAugmentationsPage={initiallyOnAugmentationsPage}
faction={faction}
p={Player}
startHackingMissionFn={startHackingMission}
/>,
Engine.Display.content,
2021-09-05 01:09:30 +02:00
);
2019-04-14 11:08:10 +02:00
}
//Returns a boolean indicating whether the player has the prerequisites for the
//specified Augmentation
export function hasAugmentationPrereqs(aug) {
2021-09-05 01:09:30 +02:00
let hasPrereqs = true;
if (aug.prereqs && aug.prereqs.length > 0) {
for (let i = 0; i < aug.prereqs.length; ++i) {
const prereqAug = Augmentations[aug.prereqs[i]];
if (prereqAug == null) {
console.error(`Invalid prereq Augmentation ${aug.prereqs[i]}`);
continue;
}
if (prereqAug.owned === false) {
hasPrereqs = false;
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
// Check if the aug is purchased
for (let j = 0; j < Player.queuedAugmentations.length; ++j) {
if (Player.queuedAugmentations[j].name === prereqAug.name) {
hasPrereqs = true;
break;
}
2019-04-14 11:08:10 +02:00
}
2021-09-05 01:09:30 +02:00
}
2019-04-14 11:08:10 +02:00
}
2021-09-05 01:09:30 +02:00
}
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
return hasPrereqs;
2019-04-14 11:08:10 +02:00
}
2021-09-05 01:09:30 +02:00
export function purchaseAugmentation(aug, fac, sing = false) {
const factionInfo = fac.getInfo();
var hasPrereqs = hasAugmentationPrereqs(aug);
if (!hasPrereqs) {
2021-09-09 05:47:34 +02:00
var txt = "You must first purchase or install " + aug.prereqs.join(",") + " before you can " + "purchase this one.";
2021-09-05 01:09:30 +02:00
if (sing) {
return txt;
} else {
dialogBoxCreate(txt);
}
2021-09-09 05:47:34 +02:00
} else if (aug.baseCost !== 0 && Player.money.lt(aug.baseCost * factionInfo.augmentationPriceMult)) {
2021-09-05 01:09:30 +02:00
let txt = "You don't have enough money to purchase " + aug.name;
if (sing) {
return txt;
}
dialogBoxCreate(txt);
} else if (fac.playerReputation < aug.baseRepRequirement) {
2021-09-09 05:47:34 +02:00
let txt = "You don't have enough faction reputation to purchase " + aug.name;
2021-09-05 01:09:30 +02:00
if (sing) {
return txt;
}
dialogBoxCreate(txt);
2021-09-09 05:47:34 +02:00
} else if (aug.baseCost === 0 || Player.money.gte(aug.baseCost * factionInfo.augmentationPriceMult)) {
2021-09-05 01:09:30 +02:00
if (Player.firstAugPurchased === false) {
Player.firstAugPurchased = true;
document.getElementById("augmentations-tab").style.display = "list-item";
document.getElementById("character-menu-header").click();
document.getElementById("character-menu-header").click();
}
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
var queuedAugmentation = new PlayerOwnedAugmentation(aug.name);
if (aug.name == AugmentationNames.NeuroFluxGovernor) {
queuedAugmentation.level = getNextNeurofluxLevel();
}
Player.queuedAugmentations.push(queuedAugmentation);
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
Player.loseMoney(aug.baseCost * factionInfo.augmentationPriceMult);
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
// If you just purchased Neuroflux Governor, recalculate the cost
if (aug.name == AugmentationNames.NeuroFluxGovernor) {
var nextLevel = getNextNeurofluxLevel();
--nextLevel;
var mult = Math.pow(CONSTANTS.NeuroFluxGovernorLevelMult, nextLevel);
2021-09-09 05:47:34 +02:00
aug.baseRepRequirement = 500 * mult * BitNodeMultipliers.AugmentationRepCost;
2021-09-05 01:09:30 +02:00
aug.baseCost = 750e3 * mult * BitNodeMultipliers.AugmentationMoneyCost;
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
for (var i = 0; i < Player.queuedAugmentations.length - 1; ++i) {
2021-09-09 05:47:34 +02:00
aug.baseCost *= CONSTANTS.MultipleAugMultiplier * [1, 0.96, 0.94, 0.93][SourceFileFlags[11]];
2021-09-05 01:09:30 +02:00
}
}
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
for (var name in Augmentations) {
if (Augmentations.hasOwnProperty(name)) {
2021-09-09 05:47:34 +02:00
Augmentations[name].baseCost *= CONSTANTS.MultipleAugMultiplier * [1, 0.96, 0.94, 0.93][SourceFileFlags[11]];
2021-09-05 01:09:30 +02:00
}
}
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
if (sing) {
return "You purchased " + aug.name;
2019-04-14 11:08:10 +02:00
} else {
2021-09-05 01:09:30 +02:00
if (!Settings.SuppressBuyAugmentationConfirmation) {
dialogBoxCreate(
"You purchased " +
aug.name +
". It's enhancements will not take " +
"effect until they are installed. To install your augmentations, go to the " +
"'Augmentations' tab on the left-hand navigation menu. Purchasing additional " +
"augmentations will now be more expensive.",
);
}
2019-04-14 11:08:10 +02:00
}
2021-09-05 01:09:30 +02:00
// Force a rerender of the Augmentations page
displayFactionContent(fac.name, true);
} else {
dialogBoxCreate(
"Hmm, something went wrong when trying to purchase an Augmentation. " +
"Please report this to the game developer with an explanation of how to " +
"reproduce this.",
);
}
2019-04-14 11:08:10 +02:00
}
export function getNextNeurofluxLevel() {
2021-09-05 01:09:30 +02:00
// Get current Neuroflux level based on Player's augmentations
let currLevel = 0;
for (var i = 0; i < Player.augmentations.length; ++i) {
if (Player.augmentations[i].name === AugmentationNames.NeuroFluxGovernor) {
currLevel = Player.augmentations[i].level;
2019-04-14 11:08:10 +02:00
}
2021-09-05 01:09:30 +02:00
}
2019-04-14 11:08:10 +02:00
2021-09-05 01:09:30 +02:00
// Account for purchased but uninstalled Augmentations
for (var i = 0; i < Player.queuedAugmentations.length; ++i) {
2021-09-09 05:47:34 +02:00
if (Player.queuedAugmentations[i].name == AugmentationNames.NeuroFluxGovernor) {
2021-09-05 01:09:30 +02:00
++currLevel;
2019-04-14 11:08:10 +02:00
}
2021-09-05 01:09:30 +02:00
}
return currLevel + 1;
2019-04-14 11:08:10 +02:00
}
export function processPassiveFactionRepGain(numCycles) {
2021-09-05 01:09:30 +02:00
for (const name in Factions) {
if (name === Player.currentWorkFactionName) continue;
if (!Factions.hasOwnProperty(name)) continue;
const faction = Factions[name];
if (!faction.isMember) continue;
// No passive rep for special factions
const info = faction.getInfo();
if (!info.offersWork()) continue;
// No passive rep for gangs.
if (Player.getGangName() === name) continue;
// 0 favor = 1%/s
// 50 favor = 6%/s
// 100 favor = 11%/s
const favorMult = Math.min(0.1, faction.favor / 1000 + 0.01);
// Find the best of all possible favor gain, minimum 1 rep / 2 minute.
const hRep = getHackingWorkRepGain(Player, faction);
const sRep = getFactionSecurityWorkRepGain(Player, faction);
const fRep = getFactionFieldWorkRepGain(Player, faction);
2021-09-09 05:47:34 +02:00
const rate = Math.max(hRep * favorMult, sRep * favorMult, fRep * favorMult, 1 / 120);
2021-09-09 05:47:34 +02:00
faction.playerReputation += rate * numCycles * Player.faction_rep_mult * BitNodeMultipliers.FactionPassiveRepGain;
2021-09-05 01:09:30 +02:00
}
2019-04-14 11:08:10 +02:00
}