bitburner-src/src/NetscriptFunctions/Gang.ts

309 lines
13 KiB
TypeScript
Raw Normal View History

2021-10-05 03:06:55 +02:00
import { INetscriptHelper } from "./INetscriptHelper";
import { IPlayer } from "../PersonObjects/IPlayer";
import { getRamCost } from "../Netscript/RamCostGenerator";
import { Gang } from "../Gang/Gang";
import { AllGangs } from "../Gang/AllGangs";
import { GangMemberTasks } from "../Gang/GangMemberTasks";
import { GangMemberUpgrades } from "../Gang/GangMemberUpgrades";
import { WorkerScript } from "../Netscript/WorkerScript";
import { GangMember } from "../Gang/GangMember";
import { GangMemberTask } from "../Gang/GangMemberTask";
import {
Gang as IGang,
GangGenInfo,
GangOtherInfo,
GangMemberInfo,
GangMemberAscension,
EquipmentStats,
GangTaskStats,
} from "../ScriptEditor/NetscriptDefinitions";
2021-10-05 03:06:55 +02:00
export function NetscriptGang(player: IPlayer, workerScript: WorkerScript, helper: INetscriptHelper): IGang {
2021-10-05 03:06:55 +02:00
const checkGangApiAccess = function (func: string): void {
const gang = player.gang;
if (gang === null) throw new Error("Must have joined gang");
const hasAccess = gang instanceof Gang;
if (!hasAccess) {
throw helper.makeRuntimeErrorMsg(`gang.${func}`, `You do not currently have a Gang`);
}
};
const getGangMember = function (func: string, name: string): GangMember {
const gang = player.gang;
if (gang === null) throw new Error("Must have joined gang");
for (const member of gang.members) if (member.name === name) return member;
throw helper.makeRuntimeErrorMsg(`gang.${func}`, `Invalid gang member: '${name}'`);
};
const getGangTask = function (func: string, name: string): GangMemberTask {
const task = GangMemberTasks[name];
if (!task) {
throw helper.makeRuntimeErrorMsg(`gang.${func}`, `Invalid task: '${name}'`);
}
return task;
};
return {
createGang: function (faction: string): boolean {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("createGang", getRamCost("gang", "createGang"));
// this list is copied from Faction/ui/Root.tsx
const GangNames = [
"Slum Snakes",
"Tetrads",
"The Syndicate",
"The Dark Army",
"Speakers for the Dead",
"NiteSec",
"The Black Hand",
];
if (!player.canAccessGang() || !GangNames.includes(faction)) return false;
if (player.inGang()) return false;
if (!player.factions.includes(faction)) return false;
const isHacking = faction === "NiteSec" || faction === "The Black Hand";
player.startGang(faction, isHacking);
return true;
},
inGang: function (): boolean {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("inGang", getRamCost("gang", "inGang"));
return player.inGang();
},
getMemberNames: function (): string[] {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getMemberNames", getRamCost("gang", "getMemberNames"));
checkGangApiAccess("getMemberNames");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
return gang.members.map((member) => member.name);
},
getGangInformation: function (): GangGenInfo {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getGangInformation", getRamCost("gang", "getGangInformation"));
checkGangApiAccess("getGangInformation");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
return {
faction: gang.facName,
isHacking: gang.isHackingGang,
moneyGainRate: gang.moneyGainRate,
power: gang.getPower(),
respect: gang.respect,
respectGainRate: gang.respectGainRate,
territory: gang.getTerritory(),
territoryClashChance: gang.territoryClashChance,
territoryWarfareEngaged: gang.territoryWarfareEngaged,
wantedLevel: gang.wanted,
wantedLevelGainRate: gang.wantedGainRate,
};
},
getOtherGangInformation: function (): GangOtherInfo {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getOtherGangInformation", getRamCost("gang", "getOtherGangInformation"));
checkGangApiAccess("getOtherGangInformation");
const cpy: any = {};
for (const gang in AllGangs) {
cpy[gang] = Object.assign({}, AllGangs[gang]);
}
return cpy;
},
getMemberInformation: function (name: any): GangMemberInfo {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getMemberInformation", getRamCost("gang", "getMemberInformation"));
checkGangApiAccess("getMemberInformation");
const member = getGangMember("getMemberInformation", name);
return {
name: member.name,
task: member.task,
earnedRespect: member.earnedRespect,
hack: member.hack,
str: member.str,
def: member.def,
dex: member.dex,
agi: member.agi,
cha: member.cha,
hack_exp: member.hack_exp,
str_exp: member.str_exp,
def_exp: member.def_exp,
dex_exp: member.dex_exp,
agi_exp: member.agi_exp,
cha_exp: member.cha_exp,
hack_mult: member.hack_mult,
str_mult: member.str_mult,
def_mult: member.def_mult,
dex_mult: member.dex_mult,
agi_mult: member.agi_mult,
cha_mult: member.cha_mult,
hack_asc_mult: member.calculateAscensionMult(member.hack_asc_points),
str_asc_mult: member.calculateAscensionMult(member.str_asc_points),
def_asc_mult: member.calculateAscensionMult(member.def_asc_points),
dex_asc_mult: member.calculateAscensionMult(member.dex_asc_points),
agi_asc_mult: member.calculateAscensionMult(member.agi_asc_points),
cha_asc_mult: member.calculateAscensionMult(member.cha_asc_points),
hack_asc_points: member.hack_asc_points,
str_asc_points: member.str_asc_points,
def_asc_points: member.def_asc_points,
dex_asc_points: member.dex_asc_points,
agi_asc_points: member.agi_asc_points,
cha_asc_points: member.cha_asc_points,
upgrades: member.upgrades.slice(),
augmentations: member.augmentations.slice(),
};
},
canRecruitMember: function (): boolean {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("canRecruitMember", getRamCost("gang", "canRecruitMember"));
checkGangApiAccess("canRecruitMember");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
return gang.canRecruitMember();
},
recruitMember: function (name: any): boolean {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("recruitMember", getRamCost("gang", "recruitMember"));
checkGangApiAccess("recruitMember");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
const recruited = gang.recruitMember(name);
if (recruited) {
2021-12-03 19:54:49 +01:00
workerScript.log("gang.recruitMember", () => `Successfully recruited Gang Member '${name}'`);
2021-10-05 03:06:55 +02:00
} else {
2021-12-03 19:54:49 +01:00
workerScript.log("gang.recruitMember", () => `Failed to recruit Gang Member '${name}'`);
2021-10-05 03:06:55 +02:00
}
return recruited;
},
getTaskNames: function (): string[] {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getTaskNames", getRamCost("gang", "getTaskNames"));
checkGangApiAccess("getTaskNames");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
const tasks = gang.getAllTaskNames();
tasks.unshift("Unassigned");
return tasks;
},
setMemberTask: function (memberName: any, taskName: any): boolean {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("setMemberTask", getRamCost("gang", "setMemberTask"));
checkGangApiAccess("setMemberTask");
const member = getGangMember("setMemberTask", memberName);
const success = member.assignToTask(taskName);
if (success) {
workerScript.log(
2021-12-03 19:54:49 +01:00
"gang.setMemberTask",
() => `Successfully assigned Gang Member '${memberName}' to '${taskName}' task`,
);
2021-10-05 03:06:55 +02:00
} else {
workerScript.log(
2021-12-03 19:54:49 +01:00
"gang.setMemberTask",
() => `Failed to assign Gang Member '${memberName}' to '${taskName}' task. '${memberName}' is now Unassigned`,
2021-10-05 03:06:55 +02:00
);
}
return success;
},
getTaskStats: function (taskName: any): GangTaskStats {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getTaskStats", getRamCost("gang", "getTaskStats"));
checkGangApiAccess("getTaskStats");
const task = getGangTask("getTaskStats", taskName);
const copy = Object.assign({}, task);
copy.territory = Object.assign({}, task.territory);
return copy;
},
getEquipmentNames: function (): string[] {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getEquipmentNames", getRamCost("gang", "getEquipmentNames"));
checkGangApiAccess("getEquipmentNames");
return Object.keys(GangMemberUpgrades);
},
getEquipmentCost: function (equipName: any): number {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getEquipmentCost", getRamCost("gang", "getEquipmentCost"));
checkGangApiAccess("getEquipmentCost");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
const upg = GangMemberUpgrades[equipName];
if (upg === null) return Infinity;
return gang.getUpgradeCost(upg);
},
getEquipmentType: function (equipName: any): string {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getEquipmentType", getRamCost("gang", "getEquipmentType"));
checkGangApiAccess("getEquipmentType");
const upg = GangMemberUpgrades[equipName];
if (upg == null) return "";
return upg.getType();
},
getEquipmentStats: function (equipName: any): EquipmentStats {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getEquipmentStats", getRamCost("gang", "getEquipmentStats"));
checkGangApiAccess("getEquipmentStats");
const equipment = GangMemberUpgrades[equipName];
if (!equipment) {
throw helper.makeRuntimeErrorMsg("getEquipmentStats", `Invalid equipment: ${equipName}`);
}
const typecheck: EquipmentStats = equipment.mults;
return Object.assign({}, typecheck) as any;
2021-10-05 03:06:55 +02:00
},
purchaseEquipment: function (memberName: any, equipName: any): boolean {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("purchaseEquipment", getRamCost("gang", "purchaseEquipment"));
checkGangApiAccess("purchaseEquipment");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
const member = getGangMember("purchaseEquipment", memberName);
const equipment = GangMemberUpgrades[equipName];
if (!equipment) return false;
const res = member.buyUpgrade(equipment, player, gang);
if (res) {
2021-12-03 19:54:49 +01:00
workerScript.log("gang.purchaseEquipment", () => `Purchased '${equipName}' for Gang member '${memberName}'`);
2021-10-05 03:06:55 +02:00
} else {
workerScript.log(
2021-12-03 19:54:49 +01:00
"gang.purchaseEquipment",
() => `Failed to purchase '${equipName}' for Gang member '${memberName}'`,
);
2021-10-05 03:06:55 +02:00
}
return res;
},
ascendMember: function (name: any): GangMemberAscension | undefined {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("ascendMember", getRamCost("gang", "ascendMember"));
checkGangApiAccess("ascendMember");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
const member = getGangMember("ascendMember", name);
if (!member.canAscend()) return;
return gang.ascendMember(member, workerScript);
},
setTerritoryWarfare: function (engage: any): void {
helper.updateDynamicRam("setTerritoryWarfare", getRamCost("gang", "setTerritoryWarfare"));
checkGangApiAccess("setTerritoryWarfare");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
if (engage) {
gang.territoryWarfareEngaged = true;
2021-12-03 19:54:49 +01:00
workerScript.log("gang.setTerritoryWarfare", () => "Engaging in Gang Territory Warfare");
2021-10-05 03:06:55 +02:00
} else {
gang.territoryWarfareEngaged = false;
2021-12-03 19:54:49 +01:00
workerScript.log("gang.setTerritoryWarfare", () => "Disengaging in Gang Territory Warfare");
2021-10-05 03:06:55 +02:00
}
},
getChanceToWinClash: function (otherGang: any): number {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getChanceToWinClash", getRamCost("gang", "getChanceToWinClash"));
checkGangApiAccess("getChanceToWinClash");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
if (AllGangs[otherGang] == null) {
throw helper.makeRuntimeErrorMsg(`gang.getChanceToWinClash`, `Invalid gang: ${otherGang}`);
}
const playerPower = AllGangs[gang.facName].power;
const otherPower = AllGangs[otherGang].power;
return playerPower / (otherPower + playerPower);
},
getBonusTime: function (): number {
2021-10-05 03:06:55 +02:00
helper.updateDynamicRam("getBonusTime", getRamCost("gang", "getBonusTime"));
checkGangApiAccess("getBonusTime");
const gang = player.gang;
if (gang === null) throw new Error("Should not be called without Gang");
return Math.round(gang.storedCycles / 5);
},
};
}