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

@ -1,4 +1,5 @@
import { calculateRamUsage } from "../../../src/Script/RamCalculations";
import { RamCosts } from "../../../src/Netscript/RamCostGenerator";
import { Script } from "../../../src/Script/Script";
const BaseCost = 1.6;
@ -169,27 +170,6 @@ describe("Parsing NetScript code to work out static RAM costs", function () {
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 () {
const code = `
export async function main(ns) {
@ -268,6 +248,27 @@ describe("Parsing NetScript code to work out static RAM costs", function () {
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
it.skip("Importing a function from a library that contains a class", async function () {
const libCode = `