forked from kamukrass/Bitburner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorp.js
452 lines (421 loc) · 19.2 KB
/
corp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/** @param {NS} ns **/
export async function main(ns) {
ns.disableLog("ALL");
const stockValuePort = ns.getPortHandle(4);
let stockValue = stockValuePort.peek() == "NULL PORT DATA" ? 0 : stockValuePort.peek();
let player = ns.getPlayer();
while (!ns.corporation.hasCorporation()) {
stockValue = stockValuePort.peek() == "NULL PORT DATA" ? 0 : stockValuePort.peek();
player = ns.getPlayer();
if (player.money + stockValue * 0.5 > 150e9) {
ns.corporation.createCorporation("MyCorp");
} else {
ns.print("Not enough money to start a corporation, waiting until $150,000,000,000");
await ns.sleep(60000);
}
}
var corp = ns.corporation.getCorporation();
if (corp.divisions.length < 1) {
// initial Company setup
ns.corporation.expandIndustry("Tobacco", "Tobacco");
corp = ns.corporation.getCorporation();
initialCorpUpgrade(ns);
initCities(ns, corp.divisions[0]);
}
while (true) {
corp = ns.corporation.getCorporation();
for (const division of corp.divisions.reverse()) {
expandCities(ns, division);
upgradeWarehouses(ns, division);
upgradeCorp(ns);
hireEmployees(ns, division);
if (ns.corporation.getDivision(division).type === "Tobacco") {
newProduct(ns, division);
}
doResearch(ns, division);
}
if (corp.divisions.length < 2 && corp.numShares == corp.totalShares) {
if (ns.corporation.getDivision(corp.divisions[0]).products.length > 2) {
await trickInvest(ns, corp.divisions[0]);
}
}
await ns.sleep(5000);
}
}
/** @param {NS} ns **/
function hireEmployees(ns, division, productCity = "Sector-12") {
var employees = ns.corporation.getOffice(division, productCity).numEmployees;
while (ns.corporation.getCorporation().funds > (cities.length * ns.corporation.getOfficeSizeUpgradeCost(division, productCity, 3))) {
// upgrade all cities + 3 employees if sufficient funds
ns.print(division + " Upgrade office size");
for (const city of cities) {
ns.corporation.upgradeOfficeSize(division, city, 3);
for (var i = 0; i < 3; i++) {
ns.corporation.hireEmployee(division, city);
}
}
}
if (ns.corporation.getOffice(division, productCity).numEmployees >= employees) {
// set jobs after hiring people just in case we hire lots of people at once and setting jobs is slow
for (const city of cities) {
employees = ns.corporation.getOffice(division, city).numEmployees;
if (ns.corporation.hasResearched(division, "Market-TA.II")) {
// TODO: Simplify here. ProductCity config can always be used
if (city == productCity) {
try {
ns.corporation.setAutoJobAssignment(division, city, "Operations", Math.ceil(employees / 5));
ns.corporation.setAutoJobAssignment(division, city, "Engineer", Math.ceil(employees / 5));
ns.corporation.setAutoJobAssignment(division, city, "Business", Math.ceil(employees / 5));
ns.corporation.setAutoJobAssignment(division, city, "Management", Math.ceil(employees / 10));
var remainingEmployees = employees - (3 * Math.ceil(employees / 5) + Math.ceil(employees / 10));
ns.corporation.setAutoJobAssignment(division, city, "Training", Math.ceil(remainingEmployees));
} catch (e) {}
}
else {
try{
ns.corporation.setAutoJobAssignment(division, city, "Operations", Math.floor(employees / 10));
ns.corporation.setAutoJobAssignment(division, city, "Engineer", 1);
ns.corporation.setAutoJobAssignment(division, city, "Business", Math.floor(employees / 5));
ns.corporation.setAutoJobAssignment(division, city, "Management", Math.ceil(employees / 100));
ns.corporation.setAutoJobAssignment(division, city, "Research & Development", Math.ceil(employees / 2));
var remainingEmployees = employees - (Math.floor(employees / 5) + Math.floor(employees / 10) + 1 + Math.ceil(employees / 100) + Math.ceil(employees / 2));
ns.corporation.setAutoJobAssignment(division, city, "Training", Math.floor(remainingEmployees));
} catch (e) {}
}
}
else {
if (city == productCity) {
try {
ns.corporation.setAutoJobAssignment(division, city, "Operations", Math.floor((employees - 2) / 2));
ns.corporation.setAutoJobAssignment(division, city, "Engineer", Math.ceil((employees - 2) / 2));
ns.corporation.setAutoJobAssignment(division, city, "Management", 2);
} catch (e) {}
}
else {
try {
ns.corporation.setAutoJobAssignment(division, city, "Operations", 1);
ns.corporation.setAutoJobAssignment(division, city, "Engineer", 1);
ns.corporation.setAutoJobAssignment(division, city, "Research & Development", (employees - 2));
} catch (e) {}
}
}
}
}
}
/** @param {NS} ns **/
function expandCities(ns, division) {
for (const city of cities) {
// check this city has a warehouse
if (!ns.corporation.hasWarehouse(division, city) && ns.corporation.getConstants().officeInitialCost < ns.corporation.getCorporation().funds) {
ns.print(division + " Expanding to city " + city);
ns.corporation.expandCity(division, city);
} else {
continue;
}
}
}
/** @param {NS} ns **/
function upgradeWarehouses(ns, division) {
for (const city of cities) {
// check this city has been expanded in to
if (!ns.corporation.getDivision(division).cities.includes(city)) {
continue;
}
// check this city has a warehouse and purchase if possible
if (!ns.corporation.hasWarehouse(division, city)) {
if (ns.corporation.getConstants().warehouseInitialCost < ns.corporation.getCorporation().funds) {
ns.print(division + " Purchasing a warehouse in " + city);
ns.corporation.purchaseWarehouse(division, city);
} else {
// no warehouse and can't afford one
continue;
}
}
// check if warehouses are near max capacity and upgrade if needed
var cityWarehouse = ns.corporation.getWarehouse(division, city);
if (cityWarehouse.sizeUsed > 0.9 * cityWarehouse.size) {
if (ns.corporation.getCorporation().funds > ns.corporation.getUpgradeWarehouseCost(division, city)) {
ns.print(division + " Upgrade warehouse in " + city);
ns.corporation.upgradeWarehouse(division, city);
}
}
}
if (ns.corporation.getUpgradeLevel("Wilson Analytics") > 20) {
// Upgrade AdVert.Inc after a certain amount of Wilson Analytivs upgrades are available
if (ns.corporation.getCorporation().funds > (4 * ns.corporation.getHireAdVertCost(division))) {
ns.print(division + " Hire AdVert");
ns.corporation.hireAdVert(division);
}
}
}
/** @param {NS} ns **/
function upgradeCorp(ns) {
for (const upgrade of upgradeList) {
// purchase upgrades based on available funds and priority; see upgradeList
if (ns.corporation.getCorporation().funds > (upgrade.prio * ns.corporation.getUpgradeLevelCost(upgrade.name))) {
// those two upgrades ony make sense later once we can afford a bunch of them and already have some base marketing from DreamSense
if ((upgrade.name != "ABC SalesBots" && upgrade.name != "Wilson Analytics") || (ns.corporation.getUpgradeLevel("DreamSense") > 20)) {
ns.print("Upgrade " + upgrade.name + " to " + (ns.corporation.getUpgradeLevel(upgrade.name) + 1));
ns.corporation.levelUpgrade(upgrade.name);
}
}
}
if (!ns.corporation.hasUnlock("Shady Accounting") && ns.corporation.getUnlockCost("Shady Accounting") * 2 < ns.corporation.getCorporation().funds) {
ns.print("Unlock Shady Accounting")
ns.corporation.purchaseUnlock("Shady Accounting");
}
else if (!ns.corporation.hasUnlock("Government Partnership") && ns.corporation.getUnlockCost("Government Partnership") * 2 < ns.corporation.getCorporation().funds) {
ns.print("Unlock Government Partnership")
ns.corporation.purchaseUnlock("Government Partnership");
}
}
/** @param {NS} ns **/
async function trickInvest(ns, division, productCity = "Sector-12") {
ns.print("Prepare to trick investors")
for (var product of ns.corporation.getDivision(division).products) {
// stop selling products
ns.corporation.sellProduct(division, productCity, product, "0", "MP", true);
}
for (const city of cities) {
// put all employees into production to produce as fast as possible
const employees = ns.corporation.getOffice(division, city).numEmployees;
ns.corporation.setAutoJobAssignment(division, city, "Engineer", 0);
ns.corporation.setAutoJobAssignment(division, city, "Management", 0);
ns.corporation.setAutoJobAssignment(division, city, "Research & Development", 0);
ns.corporation.setAutoJobAssignment(division, city, "Operations", employees - 2); // workaround for bug
ns.corporation.setAutoJobAssignment(division, city, "Operations", employees - 1); // workaround for bug
ns.corporation.setAutoJobAssignment(division, city, "Operations", employees);
}
ns.print("Wait for warehouses to fill up")
//ns.print("Warehouse usage: " + refWarehouse.sizeUsed + " of " + refWarehouse.size);
let allWarehousesFull = false;
while (!allWarehousesFull) {
allWarehousesFull = true;
for (const city of cities) {
if (ns.corporation.getWarehouse(division, city).sizeUsed <= (0.98 * ns.corporation.getWarehouse(division, city).size)) {
allWarehousesFull = false;
break;
}
}
await ns.sleep(5000);
}
ns.print("Warehouses are full, start selling");
var initialInvestFunds = ns.corporation.getInvestmentOffer().funds;
ns.print("Initial investmant offer: " + ns.formatNumber(initialInvestFunds));
for (const city of cities) {
// put all employees into business to sell as much as possible
const employees = ns.corporation.getOffice(division, city).numEmployees;
ns.corporation.setAutoJobAssignment(division, city, "Operations", 0);
ns.corporation.setAutoJobAssignment(division, city, "Business", employees - 2); // workaround for bug
ns.corporation.setAutoJobAssignment(division, city, "Business", employees - 1); // workaround for bug
ns.corporation.setAutoJobAssignment(division, city, "Business", employees);
}
for (var product of ns.corporation.getDivision(division).products) {
// sell products again
ns.corporation.sellProduct(division, productCity, product, "MAX", "MP", true);
}
while (ns.corporation.getInvestmentOffer().funds < (4 * initialInvestFunds)) {
// wait until the stored products are sold, which should lead to huge investment offers
await ns.sleep(200);
}
ns.print("Investment offer for 10% shares: " + ns.formatNumber(ns.corporation.getInvestmentOffer().funds));
ns.print("Funds before public: " + ns.formatNumber(ns.corporation.getCorporation().funds));
ns.corporation.goPublic(800e6);
ns.print("Funds after public: " + ns.formatNumber(ns.corporation.getCorporation().funds));
for (const city of cities) {
// set employees back to normal operation
const employees = ns.corporation.getOffice(division, city).numEmployees;
ns.corporation.setAutoJobAssignment(division, city, "Business", 0);
if (city == productCity) {
ns.corporation.setAutoJobAssignment(division, city, "Operations", 1);
ns.corporation.setAutoJobAssignment(division, city, "Engineer", (employees - 2));
ns.corporation.setAutoJobAssignment(division, city, "Management", 1);
}
else {
ns.corporation.setAutoJobAssignment(division, city, "Operations", 1);
ns.corporation.setAutoJobAssignment(division, city, "Research & Development", (employees - 1));
}
}
// with gained money, expand to the most profitable division
ns.corporation.expandIndustry("Healthcare", "Healthcare");
initCities(ns, ns.corporation.getCorporation().divisions[1]);
}
/** @param {NS} ns **/
function doResearch(ns, division) {
const laboratory = "Hi-Tech R&D Laboratory"
const marketTAI = "Market-TA.I";
const marketTAII = "Market-TA.II";
if (!ns.corporation.hasResearched(division, laboratory)) {
// always research labaratory first
if (division.research > ns.corporation.getResearchCost(division, laboratory)) {
ns.print(division + " Research " + laboratory);
ns.corporation.research(division, laboratory);
}
}
else if (!ns.corporation.hasResearched(division, marketTAII)) {
// always research Market-TA.I plus .II first and in one step
var researchCost = ns.corporation.getResearchCost(division, marketTAI) + ns.corporation.getResearchCost(division, marketTAII);
if (division.research > researchCost * 1.1) {
ns.print(division + " Research " + marketTAI);
ns.corporation.research(division, marketTAI);
ns.print(division + " Research " + marketTAII);
ns.corporation.research(division, marketTAII);
for (var product of ns.corporation.getDivision(division).products) {
ns.corporation.setProductMarketTA1(division, product, true);
ns.corporation.setProductMarketTA2(division, product, true);
}
}
return;
}
else {
for (const researchObject of researchList) {
// research other upgrades based on available funds and priority; see researchList
if (!ns.corporation.hasResearched(division, researchObject.name)) {
if (division.research > (researchObject.prio * ns.corporation.getResearchCost(division, researchObject.name))) {
ns.print(division + " Research " + researchObject.name);
ns.corporation.research(division, researchObject.name);
}
}
}
}
}
/** @param {NS} ns **/
function newProduct(ns, division, productCity = "Sector-12") {
//ns.print("Products: " + ns.corporation.getDivision(division).products);
var productNumbers = [];
for (var product of ns.corporation.getDivision(division).products) {
if (ns.corporation.getProduct(division, productCity, product).developmentProgress < 100) {
ns.print(division + " Product development progress: " + ns.corporation.getProduct(division, productCity, product).developmentProgress.toFixed(1) + "%");
return false;
}
else {
productNumbers.push(product.charAt(product.length - 1));
// initial sell value if nothing is defined yet is 0
if (ns.corporation.getProduct(division, productCity, product).sCost == 0) {
ns.print(division + " Start selling product " + product);
ns.corporation.sellProduct(division, productCity, product, "MAX", "MP", true);
if (ns.corporation.hasResearched(division, "Market-TA.II")) {
ns.corporation.setProductMarketTA1(division, product, true);
ns.corporation.setProductMarketTA2(division, product, true);
}
}
}
}
var numProducts = 3;
// amount of products which can be sold in parallel is 3; can be upgraded
if (ns.corporation.hasResearched(division, "uPgrade: Capacity.I")) {
numProducts++;
if (ns.corporation.hasResearched(division, "uPgrade: Capacity.II")) {
numProducts++;
}
}
if (productNumbers.length >= numProducts) {
// discontinue the oldest product if over max amount of products
ns.print(division + " Discontinue product " + ns.corporation.getDivision(division).products[0]);
ns.corporation.discontinueProduct(division, ns.corporation.getDivision(division).products[0]);
}
// get the product number of the latest product and increase it by 1 for the mext product. Product names must be unique.
var newProductNumber = 0;
if (productNumbers.length > 0) {
newProductNumber = parseInt(productNumbers[productNumbers.length - 1]) + 1;
// cap product numbers to one digit and restart at 0 if > 9.
if (newProductNumber > 9) {
newProductNumber = 0;
}
}
const newProductName = "Product-" + newProductNumber;
var productInvest = 1e9;
if (ns.corporation.getCorporation().funds < (2 * productInvest)) {
if (ns.corporation.getCorporation().funds <= 0) {
ns.print("WARN negative funds, cannot start new product development " + ns.formatNumber(ns.corporation.getCorporation().funds));
return;
// productInvest = 0; // product development with 0 funds not possible if corp has negative funds
}
else {
productInvest = Math.floor(ns.corporation.getCorporation().funds / 2);
}
}
ns.print("Start new product development " + newProductName);
ns.corporation.makeProduct(division, productCity, newProductName, productInvest, productInvest);
}
/** @param {NS} ns **/
function initCities(ns, division, productCity = "Sector-12") {
for (const city of cities) {
ns.print("Expand " + division + " to City " + city);
if (!ns.corporation.getDivision(division).cities.includes(city)) {
ns.corporation.expandCity(division, city);
ns.corporation.purchaseWarehouse(division, city);
}
//ns.corporation.setSmartSupply(division, city, true); // does not work anymore, bug?
if (city != productCity) {
// setup employees
for (let i = 0; i < 3; i++) {
ns.corporation.hireEmployee(division, city);
}
ns.corporation.setAutoJobAssignment(division, city, "Research & Development", 3);
}
else {
const warehouseUpgrades = 3;
// get a bigger warehouse in the product city. we can produce and sell more here
for (let i = 0; i < warehouseUpgrades; i++) {
ns.corporation.upgradeWarehouse(division, city);
}
// get more employees in the main product development city
const newEmployees = 9;
ns.corporation.upgradeOfficeSize(division, productCity, newEmployees);
for (let i = 0; i < newEmployees + 3; i++) {
ns.corporation.hireEmployee(division, productCity);
}
ns.corporation.setAutoJobAssignment(division, productCity, "Operations", 4);
ns.corporation.setAutoJobAssignment(division, productCity, "Engineer", 6);
ns.corporation.setAutoJobAssignment(division, productCity, "Management", 2);
}
const warehouseUpgrades = 3;
for (let i = 0; i < warehouseUpgrades; i++) {
ns.corporation.upgradeWarehouse(division, city);
}
}
ns.corporation.makeProduct(division, productCity, "Product-0", "1e9", "1e9");
}
/** @param {NS} ns **/
function initialCorpUpgrade(ns) {
ns.print("unlock upgrades");
ns.corporation.purchaseUnlock("Smart Supply");
ns.corporation.levelUpgrade("Smart Storage");
ns.corporation.levelUpgrade("Smart Storage");
ns.corporation.levelUpgrade("Smart Storage");
ns.corporation.levelUpgrade("Smart Storage");
ns.corporation.levelUpgrade("DreamSense");
// upgrade employee stats
ns.corporation.levelUpgrade("Nuoptimal Nootropic Injector Implants");
ns.corporation.levelUpgrade("Speech Processor Implants");
ns.corporation.levelUpgrade("Neural Accelerators");
ns.corporation.levelUpgrade("FocusWires");
}
const cities = ["Sector-12", "Aevum", "Volhaven", "Chongqing", "New Tokyo", "Ishima"];
const upgradeList = [
// lower priority value -> upgrade faster
{ prio: 2, name: "Project Insight", },
{ prio: 2, name: "DreamSense" },
{ prio: 4, name: "ABC SalesBots" },
{ prio: 4, name: "Smart Factories" },
{ prio: 4, name: "Smart Storage" },
{ prio: 8, name: "Neural Accelerators" },
{ prio: 8, name: "Nuoptimal Nootropic Injector Implants" },
{ prio: 8, name: "FocusWires" },
{ prio: 8, name: "Speech Processor Implants" },
{ prio: 8, name: "Wilson Analytics" },
];
const researchList = [
// lower priority value -> upgrade faster
{ prio: 10, name: "Overclock" },
{ prio: 10, name: "uPgrade: Fulcrum" },
{ prio: 3, name: "uPgrade: Capacity.I" },
{ prio: 4, name: "uPgrade: Capacity.II" },
{ prio: 10, name: "Self-Correcting Assemblers" },
{ prio: 21, name: "Drones" },
{ prio: 4, name: "Drones - Assembly" },
{ prio: 10, name: "Drones - Transport" },
{ prio: 26, name: "Automatic Drug Administration" },
{ prio: 10, name: "CPH4 Injections" },
];