bitburner-src/src/Hacknet/HacknetServer.ts

159 lines
4.6 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 { BaseServer } from "../Server/BaseServer";
import { RunningScript } from "../Script/RunningScript";
import { HacknetServerConstants } from "./data/Constants";
2021-09-05 01:09:30 +02:00
import {
calculateHashGainRate,
calculateLevelUpgradeCost,
calculateRamUpgradeCost,
calculateCoreUpgradeCost,
calculateCacheUpgradeCost,
} from "./formulas/HacknetServers";
2019-03-25 04:03:24 +01:00
2021-09-25 20:42:57 +02:00
import { createRandomIp } from "../utils/IPAddress";
2019-03-25 04:03:24 +01:00
2022-07-15 01:00:10 +02:00
import { Generic_fromJSON, Generic_toJSON, IReviverValue, Reviver } from "../utils/JSONReviver";
2021-11-16 05:49:33 +01:00
import { IPlayer } from "../PersonObjects/IPlayer";
2019-03-25 04:03:24 +01:00
interface IConstructorParams {
2021-09-05 01:09:30 +02:00
adminRights?: boolean;
hostname: string;
ip?: string;
isConnectedTo?: boolean;
maxRam?: number;
organizationName?: string;
2019-03-25 04:03:24 +01:00
}
export class HacknetServer extends BaseServer implements IHacknetNode {
2021-09-05 01:09:30 +02:00
// Cache level. Affects hash Capacity
cache = 1;
// Number of cores. Improves hash production
cores = 1;
2021-11-11 23:00:36 +01:00
2021-09-05 01:09:30 +02:00
// Number of hashes that can be stored by this Hacknet Server
hashCapacity = 0;
// Hashes produced per second
hashRate = 0;
// Similar to Node level. Improves hash production
level = 1;
// How long this HacknetServer has existed, in seconds
onlineTimeSeconds = 0;
// Total number of hashes earned by this server
totalHashesGenerated = 0;
2021-11-11 23:00:36 +01:00
// Flag indicating wehther this is a purchased server
purchasedByPlayer = true;
2021-09-09 05:47:34 +02:00
constructor(params: IConstructorParams = { hostname: "", ip: createRandomIp() }) {
2021-09-05 01:09:30 +02:00
super(params);
this.maxRam = 1;
this.updateHashCapacity();
}
calculateCacheUpgradeCost(levels: number): number {
return calculateCacheUpgradeCost(this.cache, levels);
}
calculateCoreUpgradeCost(levels: number, costMult: number): number {
return calculateCoreUpgradeCost(this.cores, levels, costMult);
}
calculateLevelUpgradeCost(levels: number, costMult: number): number {
return calculateLevelUpgradeCost(this.level, levels, costMult);
}
calculateRamUpgradeCost(levels: number, costMult: number): number {
return calculateRamUpgradeCost(this.maxRam, levels, costMult);
}
// Process this Hacknet Server in the game loop. Returns the number of hashes generated
process(numCycles = 1): number {
const seconds = (numCycles * CONSTANTS.MilliPerCycle) / 1000;
2021-10-04 03:33:48 +02:00
this.onlineTimeSeconds += seconds;
2021-09-05 01:09:30 +02:00
2021-10-04 03:33:48 +02:00
const hashes = this.hashRate * seconds;
this.totalHashesGenerated += hashes;
return hashes;
2021-09-05 01:09:30 +02:00
}
upgradeCache(levels: number): void {
2021-09-09 05:47:34 +02:00
this.cache = Math.min(HacknetServerConstants.MaxCache, Math.round(this.cache + levels));
2021-09-05 01:09:30 +02:00
this.updateHashCapacity();
}
upgradeCore(levels: number, prodMult: number): void {
2021-09-09 05:47:34 +02:00
this.cores = Math.min(HacknetServerConstants.MaxCores, Math.round(this.cores + levels));
2021-09-05 01:09:30 +02:00
this.updateHashRate(prodMult);
2021-11-11 23:00:36 +01:00
this.cpuCores = this.cores;
2021-09-05 01:09:30 +02:00
}
upgradeLevel(levels: number, prodMult: number): void {
2021-09-09 05:47:34 +02:00
this.level = Math.min(HacknetServerConstants.MaxLevel, Math.round(this.level + levels));
2021-09-05 01:09:30 +02:00
this.updateHashRate(prodMult);
}
upgradeRam(levels: number, prodMult: number): boolean {
for (let i = 0; i < levels; ++i) {
this.maxRam *= 2;
2019-03-25 04:03:24 +01:00
}
2021-09-09 05:47:34 +02:00
this.maxRam = Math.min(HacknetServerConstants.MaxRam, Math.round(this.maxRam));
2021-09-05 01:09:30 +02:00
this.updateHashRate(prodMult);
return true;
}
// Whenever a script is run, we must update this server's hash rate
runScript(script: RunningScript, prodMult?: number): void {
super.runScript(script);
if (prodMult != null && typeof prodMult === "number") {
this.updateHashRate(prodMult);
2019-03-25 04:03:24 +01:00
}
2021-09-05 01:09:30 +02:00
}
2021-11-16 05:49:33 +01:00
updateRamUsed(ram: number, player: IPlayer): void {
super.updateRamUsed(ram, player);
2022-07-15 00:43:33 +02:00
this.updateHashRate(player.mults.hacknet_node_money);
2021-11-16 05:49:33 +01:00
}
2021-09-05 01:09:30 +02:00
updateHashCapacity(): void {
this.hashCapacity = 32 * Math.pow(2, this.cache);
}
updateHashRate(prodMult: number): void {
2021-09-09 05:47:34 +02:00
this.hashRate = calculateHashGainRate(this.level, this.ramUsed, this.maxRam, this.cores, prodMult);
2021-09-05 01:09:30 +02: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,
);
2021-05-01 09:17:31 +02:00
}
2021-09-05 01:09:30 +02:00
}
// Serialize the current object to a JSON save state
2022-07-15 01:00:10 +02:00
toJSON(): IReviverValue {
2021-09-05 01:09:30 +02:00
return Generic_toJSON("HacknetServer", this);
}
// Initializes a HacknetServer Object from a JSON save state
2022-07-15 01:00:10 +02:00
static fromJSON(value: IReviverValue): HacknetServer {
2021-09-05 01:09:30 +02:00
return Generic_fromJSON(HacknetServer, value.data);
}
2019-03-25 04:03:24 +01:00
}
Reviver.constructors.HacknetServer = HacknetServer;