2019-03-25 04:03:24 +01:00
|
|
|
/**
|
|
|
|
* Hacknet Node Class
|
|
|
|
*
|
|
|
|
* Hacknet Nodes are specialized machines that passively earn the player money over time.
|
|
|
|
* They can be upgraded to increase their production
|
|
|
|
*/
|
|
|
|
import { IHacknetNode } from "./IHacknetNode";
|
|
|
|
|
|
|
|
import { CONSTANTS } from "../Constants";
|
|
|
|
|
2021-03-31 06:45:21 +02:00
|
|
|
import {
|
|
|
|
calculateMoneyGainRate,
|
|
|
|
calculateLevelUpgradeCost,
|
|
|
|
calculateCoreUpgradeCost,
|
|
|
|
calculateRamUpgradeCost,
|
|
|
|
} from "./formulas/HacknetNodes";
|
|
|
|
import { HacknetNodeConstants } from "./data/Constants";
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
import { dialogBoxCreate } from "../../utils/DialogBox";
|
|
|
|
import { Generic_fromJSON,
|
|
|
|
Generic_toJSON,
|
|
|
|
Reviver } from "../../utils/JSONReviver";
|
|
|
|
|
|
|
|
export class HacknetNode implements IHacknetNode {
|
|
|
|
|
|
|
|
// Node's number of cores
|
2021-04-30 05:52:56 +02:00
|
|
|
cores = 1;
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
// Node's Level
|
2021-04-30 05:52:56 +02:00
|
|
|
level = 1;
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
// Node's production per second
|
2021-04-30 05:52:56 +02:00
|
|
|
moneyGainRatePerSecond = 0;
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
// Identifier for Node. Includes the full "name" (hacknet-node-N)
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
// How long this Node has existed, in seconds
|
2021-04-30 05:52:56 +02:00
|
|
|
onlineTimeSeconds = 0;
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
// Node's RAM (GB)
|
2021-04-30 05:52:56 +02:00
|
|
|
ram = 1;
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
// Total money earned by this Node
|
2021-04-30 05:52:56 +02:00
|
|
|
totalMoneyGenerated = 0;
|
2019-03-25 04:03:24 +01:00
|
|
|
|
2021-04-30 05:52:56 +02:00
|
|
|
constructor(name="", prodMult=1) {
|
2019-03-25 04:03:24 +01:00
|
|
|
this.name = name;
|
2019-05-05 06:03:40 +02:00
|
|
|
|
|
|
|
this.updateMoneyGainRate(prodMult);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the cost to upgrade this Node's number of cores
|
2021-04-30 05:52:56 +02:00
|
|
|
calculateCoreUpgradeCost(levels=1, costMult: number): number {
|
2021-03-31 06:45:21 +02:00
|
|
|
return calculateCoreUpgradeCost(this.cores, levels, costMult);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the cost to upgrade this Node's level
|
2021-04-30 05:52:56 +02:00
|
|
|
calculateLevelUpgradeCost(levels=1, costMult: number): number {
|
2021-03-31 06:45:21 +02:00
|
|
|
return calculateLevelUpgradeCost(this.level, levels, costMult);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the cost to upgrade this Node's RAM
|
2021-04-30 05:52:56 +02:00
|
|
|
calculateRamUpgradeCost(levels=1, costMult: number): number {
|
2021-03-31 06:45:21 +02:00
|
|
|
return calculateRamUpgradeCost(this.ram, levels, costMult);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process this Hacknet Node in the game loop.
|
|
|
|
// Returns the amount of money 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;
|
|
|
|
let gain = this.moneyGainRatePerSecond * seconds;
|
|
|
|
if (isNaN(gain)) {
|
|
|
|
console.error(`Hacknet Node ${this.name} calculated earnings of NaN`);
|
|
|
|
gain = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.totalMoneyGenerated += gain;
|
|
|
|
this.onlineTimeSeconds += seconds;
|
|
|
|
|
|
|
|
return gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade this Node's number of cores, if possible
|
|
|
|
// Returns a boolean indicating whether new cores were successfully bought
|
2021-04-30 05:52:56 +02:00
|
|
|
upgradeCore(levels=1, prodMult: number): void {
|
2021-03-31 06:45:21 +02:00
|
|
|
this.cores = Math.min(HacknetNodeConstants.MaxCores, Math.round(this.cores + levels));
|
2019-05-05 06:03:40 +02:00
|
|
|
this.updateMoneyGainRate(prodMult);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade this Node's level, if possible
|
|
|
|
// Returns a boolean indicating whether the level was successfully updated
|
2021-04-30 05:52:56 +02:00
|
|
|
upgradeLevel(levels=1, prodMult: number): void {
|
2021-03-31 06:45:21 +02:00
|
|
|
this.level = Math.min(HacknetNodeConstants.MaxLevel, Math.round(this.level + levels));
|
2019-05-05 06:03:40 +02:00
|
|
|
this.updateMoneyGainRate(prodMult);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade this Node's RAM, if possible
|
|
|
|
// Returns a boolean indicating whether the RAM was successfully upgraded
|
2021-04-30 05:52:56 +02:00
|
|
|
upgradeRam(levels=1, prodMult: number): void {
|
2019-05-05 06:03:40 +02:00
|
|
|
for (let i = 0; i < levels; ++i) {
|
2019-03-25 04:03:24 +01:00
|
|
|
this.ram *= 2; // Ram is always doubled
|
|
|
|
}
|
|
|
|
this.ram = Math.round(this.ram); // Handle any floating point precision issues
|
2019-05-05 06:03:40 +02:00
|
|
|
this.updateMoneyGainRate(prodMult);
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re-calculate this Node's production and update the moneyGainRatePerSecond prop
|
2019-05-05 06:03:40 +02:00
|
|
|
updateMoneyGainRate(prodMult: number): void {
|
2021-03-31 06:45:21 +02:00
|
|
|
this.moneyGainRatePerSecond = calculateMoneyGainRate(this.level, this.ram, this.cores, prodMult);
|
2019-03-25 04:03:24 +01:00
|
|
|
if (isNaN(this.moneyGainRatePerSecond)) {
|
|
|
|
this.moneyGainRatePerSecond = 0;
|
|
|
|
dialogBoxCreate("Error in calculating Hacknet Node production. Please report to game developer", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the current object to a JSON save state.
|
|
|
|
*/
|
|
|
|
toJSON(): any {
|
|
|
|
return Generic_toJSON("HacknetNode", this);
|
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiatizes a HacknetNode object from a JSON save state.
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
static fromJSON(value: any): HacknetNode {
|
|
|
|
return Generic_fromJSON(HacknetNode, value.data);
|
|
|
|
}
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.HacknetNode = HacknetNode;
|