CORPORATION: Dramatically lower API RAM costs (#818)

This commit is contained in:
Jesse Clark 2023-09-27 14:38:30 -07:00 committed by GitHub
parent c5e2f65cb0
commit ad6f919d64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 79 deletions

@ -10,7 +10,8 @@ export type RamCostTree<API> = {
export const RamCostConstants = { export const RamCostConstants = {
Base: 1.6, Base: 1.6,
Dom: 25, Dom: 25,
Corporation: 1024, CorporationInfo: 10,
CorporationAction: 20,
Max: 1024, Max: 1024,
Hack: 0.1, Hack: 0.1,
HackAnalyze: 1, HackAnalyze: 1,
@ -354,64 +355,64 @@ const grafting = {
const corporation = { const corporation = {
hasCorporation: 0, // This one is free hasCorporation: 0, // This one is free
getConstants: 0, getConstants: 0,
getIndustryData: RamCostConstants.Corporation, getIndustryData: RamCostConstants.CorporationInfo,
getMaterialData: RamCostConstants.Corporation, getMaterialData: RamCostConstants.CorporationInfo,
issueNewShares: RamCostConstants.Corporation, issueNewShares: RamCostConstants.CorporationAction,
createCorporation: RamCostConstants.Corporation, createCorporation: RamCostConstants.CorporationAction,
hasUnlock: RamCostConstants.Corporation, hasUnlock: RamCostConstants.CorporationInfo,
getUnlockCost: RamCostConstants.Corporation, getUnlockCost: RamCostConstants.CorporationInfo,
getUpgradeLevel: RamCostConstants.Corporation, getUpgradeLevel: RamCostConstants.CorporationInfo,
getUpgradeLevelCost: RamCostConstants.Corporation, getUpgradeLevelCost: RamCostConstants.CorporationInfo,
getInvestmentOffer: RamCostConstants.Corporation, getInvestmentOffer: RamCostConstants.CorporationInfo,
acceptInvestmentOffer: RamCostConstants.Corporation, acceptInvestmentOffer: RamCostConstants.CorporationAction,
goPublic: RamCostConstants.Corporation, goPublic: RamCostConstants.CorporationAction,
bribe: RamCostConstants.Corporation, bribe: RamCostConstants.CorporationAction,
getCorporation: RamCostConstants.Corporation, getCorporation: RamCostConstants.CorporationInfo,
getDivision: RamCostConstants.Corporation, getDivision: RamCostConstants.CorporationInfo,
expandIndustry: RamCostConstants.Corporation, expandIndustry: RamCostConstants.CorporationAction,
expandCity: RamCostConstants.Corporation, expandCity: RamCostConstants.CorporationAction,
purchaseUnlock: RamCostConstants.Corporation, purchaseUnlock: RamCostConstants.CorporationAction,
levelUpgrade: RamCostConstants.Corporation, levelUpgrade: RamCostConstants.CorporationAction,
issueDividends: RamCostConstants.Corporation, issueDividends: RamCostConstants.CorporationAction,
buyBackShares: RamCostConstants.Corporation, buyBackShares: RamCostConstants.CorporationAction,
sellShares: RamCostConstants.Corporation, sellShares: RamCostConstants.CorporationAction,
getBonusTime: 0, getBonusTime: 0,
sellMaterial: RamCostConstants.Corporation, sellMaterial: RamCostConstants.CorporationAction,
sellProduct: RamCostConstants.Corporation, sellProduct: RamCostConstants.CorporationAction,
discontinueProduct: RamCostConstants.Corporation, discontinueProduct: RamCostConstants.CorporationAction,
setSmartSupply: RamCostConstants.Corporation, setSmartSupply: RamCostConstants.CorporationAction,
setSmartSupplyOption: RamCostConstants.Corporation, setSmartSupplyOption: RamCostConstants.CorporationAction,
buyMaterial: RamCostConstants.Corporation, buyMaterial: RamCostConstants.CorporationAction,
bulkPurchase: RamCostConstants.Corporation, bulkPurchase: RamCostConstants.CorporationAction,
getWarehouse: RamCostConstants.Corporation, getWarehouse: RamCostConstants.CorporationInfo,
getProduct: RamCostConstants.Corporation, getProduct: RamCostConstants.CorporationInfo,
getMaterial: RamCostConstants.Corporation, getMaterial: RamCostConstants.CorporationInfo,
setMaterialMarketTA1: RamCostConstants.Corporation, setMaterialMarketTA1: RamCostConstants.CorporationAction,
setMaterialMarketTA2: RamCostConstants.Corporation, setMaterialMarketTA2: RamCostConstants.CorporationAction,
setProductMarketTA1: RamCostConstants.Corporation, setProductMarketTA1: RamCostConstants.CorporationAction,
setProductMarketTA2: RamCostConstants.Corporation, setProductMarketTA2: RamCostConstants.CorporationAction,
exportMaterial: RamCostConstants.Corporation, exportMaterial: RamCostConstants.CorporationAction,
cancelExportMaterial: RamCostConstants.Corporation, cancelExportMaterial: RamCostConstants.CorporationAction,
purchaseWarehouse: RamCostConstants.Corporation, purchaseWarehouse: RamCostConstants.CorporationAction,
upgradeWarehouse: RamCostConstants.Corporation, upgradeWarehouse: RamCostConstants.CorporationAction,
makeProduct: RamCostConstants.Corporation, makeProduct: RamCostConstants.CorporationAction,
limitMaterialProduction: RamCostConstants.Corporation, limitMaterialProduction: RamCostConstants.CorporationAction,
limitProductProduction: RamCostConstants.Corporation, limitProductProduction: RamCostConstants.CorporationAction,
getUpgradeWarehouseCost: RamCostConstants.Corporation, getUpgradeWarehouseCost: RamCostConstants.CorporationInfo,
hasWarehouse: RamCostConstants.Corporation, hasWarehouse: RamCostConstants.CorporationInfo,
hireEmployee: RamCostConstants.Corporation, hireEmployee: RamCostConstants.CorporationAction,
upgradeOfficeSize: RamCostConstants.Corporation, upgradeOfficeSize: RamCostConstants.CorporationAction,
throwParty: RamCostConstants.Corporation, throwParty: RamCostConstants.CorporationAction,
buyTea: RamCostConstants.Corporation, buyTea: RamCostConstants.CorporationAction,
hireAdVert: RamCostConstants.Corporation, hireAdVert: RamCostConstants.CorporationAction,
research: RamCostConstants.Corporation, research: RamCostConstants.CorporationAction,
getOffice: RamCostConstants.Corporation, getOffice: RamCostConstants.CorporationInfo,
getHireAdVertCost: RamCostConstants.Corporation, getHireAdVertCost: RamCostConstants.CorporationInfo,
getHireAdVertCount: RamCostConstants.Corporation, getHireAdVertCount: RamCostConstants.CorporationInfo,
getResearchCost: RamCostConstants.Corporation, getResearchCost: RamCostConstants.CorporationInfo,
hasResearched: RamCostConstants.Corporation, hasResearched: RamCostConstants.CorporationInfo,
setAutoJobAssignment: RamCostConstants.Corporation, setAutoJobAssignment: RamCostConstants.CorporationAction,
getOfficeSizeUpgradeCost: RamCostConstants.Corporation, getOfficeSizeUpgradeCost: RamCostConstants.CorporationInfo,
} as const; } as const;
/** RamCosts guaranteed to match ns structure 1:1 (aside from args and enums). /** RamCosts guaranteed to match ns structure 1:1 (aside from args and enums).

@ -1,4 +1,5 @@
import { calculateRamUsage } from "../../../src/Script/RamCalculations"; import { calculateRamUsage } from "../../../src/Script/RamCalculations";
import { RamCosts } from "../../../src/Netscript/RamCostGenerator";
import { Script } from "../../../src/Script/Script"; import { Script } from "../../../src/Script/Script";
const BaseCost = 1.6; const BaseCost = 1.6;
@ -169,27 +170,6 @@ describe("Parsing NetScript code to work out static RAM costs", function () {
expectCost(calculated, HacknetCost); expectCost(calculated, HacknetCost);
}); });
it("One corporation function reaching max ram cap", async function () {
const code = `
export async function main(ns) {
ns.corporation.getCorporation();
}
`;
const calculated = calculateRamUsage(code, new Map()).cost;
expectCost(calculated, MaxCost);
});
it("Two corporation functions at max ram cap", async function () {
const code = `
export async function main(ns) {
ns.corporation.createCorporation("Noodle Bar");
ns.corporation.getCorporation();
}
`;
const calculated = calculateRamUsage(code, new Map()).cost;
expectCost(calculated, MaxCost);
});
it("Sleeve functions with an individual cost", async function () { it("Sleeve functions with an individual cost", async function () {
const code = ` const code = `
export async function main(ns) { export async function main(ns) {
@ -268,6 +248,27 @@ describe("Parsing NetScript code to work out static RAM costs", function () {
expectCost(calculated, HackCost + GrowCost); expectCost(calculated, HackCost + GrowCost);
}); });
it("Using every function in the API costs MaxCost", () => {
const lines: string[] = [];
for (const [key, val] of Object.entries(RamCosts)) {
if (typeof val === "object") {
const namespace = key;
for (const name of Object.keys(val)) {
lines.push(`ns.${namespace}.${name}()`);
}
} else {
lines.push(`ns.${key}()`);
}
}
const code = `
export async function main(ns) {
${lines.join("\n")};
}
`;
const calculated = calculateRamUsage(code, new Map()).cost;
expectCost(calculated, MaxCost);
});
// TODO: once we fix static parsing this should pass // TODO: once we fix static parsing this should pass
it.skip("Importing a function from a library that contains a class", async function () { it.skip("Importing a function from a library that contains a class", async function () {
const libCode = ` const libCode = `