bitburner-src/src/Hacknet/HacknetServer.ts

147 lines
4.4 KiB
TypeScript
Raw Normal View History

2019-03-25 04:03:24 +01:00
/**
* Hacknet Servers - Reworked Hacknet Node mechanic for BitNode-9
*/
import { CONSTANTS } from "../Constants";
import { IHacknetNode } from "./IHacknetNode";
2019-03-25 04:03:24 +01:00
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
import { BaseServer } from "../Server/BaseServer";
import { RunningScript } from "../Script/RunningScript";
import { HacknetServerConstants } from "./data/Constants";
import {
calculateHashGainRate,
calculateLevelUpgradeCost,
calculateRamUpgradeCost,
calculateCoreUpgradeCost,
calculateCacheUpgradeCost,
} from "./formulas/HacknetServers";
2019-03-25 04:03:24 +01:00
import { createRandomIp } from "../../utils/IPAddress";
import {
Generic_fromJSON,
Generic_toJSON,
2021-04-30 05:52:56 +02:00
Reviver,
} from "../../utils/JSONReviver";
2019-03-25 04:03:24 +01:00
interface IConstructorParams {
adminRights?: boolean;
hostname: string;
ip?: string;
isConnectedTo?: boolean;
maxRam?: number;
organizationName?: string;
}
export class HacknetServer extends BaseServer implements IHacknetNode {
// Initializes a HacknetServer Object from a JSON save state
static fromJSON(value: any): HacknetServer {
return Generic_fromJSON(HacknetServer, value.data);
}
// Cache level. Affects hash Capacity
2021-04-30 05:52:56 +02:00
cache = 1;
2019-03-25 04:03:24 +01:00
// Number of cores. Improves hash production
2021-04-30 05:52:56 +02:00
cores = 1;
2019-03-25 04:03:24 +01:00
// Number of hashes that can be stored by this Hacknet Server
2021-04-30 05:52:56 +02:00
hashCapacity = 0;
2019-03-25 04:03:24 +01:00
// Hashes produced per second
2021-04-30 05:52:56 +02:00
hashRate = 0;
2019-03-25 04:03:24 +01:00
// Similar to Node level. Improves hash production
2021-04-30 05:52:56 +02:00
level = 1;
2019-03-25 04:03:24 +01:00
// How long this HacknetServer has existed, in seconds
2021-04-30 05:52:56 +02:00
onlineTimeSeconds = 0;
2019-03-25 04:03:24 +01:00
// Total number of hashes earned by this server
2021-04-30 05:52:56 +02:00
totalHashesGenerated = 0;
2019-03-25 04:03:24 +01:00
constructor(params: IConstructorParams={ hostname: "", ip: createRandomIp() }) {
super(params);
this.maxRam = 1;
this.updateHashCapacity();
}
calculateCacheUpgradeCost(levels: number): number {
return calculateCacheUpgradeCost(this.cache, levels);
2019-03-25 04:03:24 +01:00
}
calculateCoreUpgradeCost(levels: number, costMult: number): number {
return calculateCoreUpgradeCost(this.cores, levels, costMult);
2019-03-25 04:03:24 +01:00
}
calculateLevelUpgradeCost(levels: number, costMult: number): number {
return calculateLevelUpgradeCost(this.level, levels, costMult);
2019-03-25 04:03:24 +01:00
}
calculateRamUpgradeCost(levels: number, costMult: number): number {
return calculateRamUpgradeCost(this.maxRam, levels, costMult);
2019-03-25 04:03:24 +01:00
}
2019-05-18 00:51:28 +02:00
// Process this Hacknet Server in the game loop. Returns the number of hashes generated
2021-04-30 05:52:56 +02:00
process(numCycles=1): number {
2019-03-25 04:03:24 +01:00
const seconds = numCycles * CONSTANTS.MilliPerCycle / 1000;
return this.hashRate * seconds;
}
upgradeCache(levels: number): void {
this.cache = Math.min(HacknetServerConstants.MaxCache, Math.round(this.cache + levels));
2019-03-25 04:03:24 +01:00
this.updateHashCapacity();
}
upgradeCore(levels: number, prodMult: number): void {
this.cores = Math.min(HacknetServerConstants.MaxCores, Math.round(this.cores + levels));
this.updateHashRate(prodMult);
2019-03-25 04:03:24 +01:00
}
upgradeLevel(levels: number, prodMult: number): void {
this.level = Math.min(HacknetServerConstants.MaxLevel, Math.round(this.level + levels));
this.updateHashRate(prodMult);
2019-03-25 04:03:24 +01:00
}
upgradeRam(levels: number, prodMult: number): boolean {
for (let i = 0; i < levels; ++i) {
2019-03-25 04:03:24 +01:00
this.maxRam *= 2;
}
this.maxRam = Math.min(HacknetServerConstants.MaxRam, Math.round(this.maxRam));
this.updateHashRate(prodMult);
2019-03-25 04:03:24 +01:00
return true;
}
2019-05-18 00:51:28 +02:00
// Whenever a script is run, we must update this server's hash rate
runScript(script: RunningScript, prodMult?: number): void {
2019-03-25 04:03:24 +01:00
super.runScript(script);
if (prodMult != null && typeof prodMult === "number") {
this.updateHashRate(prodMult);
2019-03-25 04:03:24 +01:00
}
}
updateHashCapacity(): void {
this.hashCapacity = 32 * Math.pow(2, this.cache);
2019-03-25 04:03:24 +01:00
}
updateHashRate(prodMult: number): void {
this.hashRate = calculateHashGainRate(this.level, this.ramUsed, this.maxRam, this.cores, prodMult)
2019-03-25 04:03:24 +01:00
if (isNaN(this.hashRate)) {
this.hashRate = 0;
console.error(`Error calculating Hacknet Server hash production. This is a bug. Please report to game dev`, false);
2019-03-25 04:03:24 +01:00
}
}
// Serialize the current object to a JSON save state
toJSON(): any {
return Generic_toJSON("HacknetServer", this);
}
}
Reviver.constructors.HacknetServer = HacknetServer;