Skip to content

Commit

Permalink
factory v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Gagnon committed Jun 2, 2024
1 parent 76ce2f9 commit f0b9b2d
Show file tree
Hide file tree
Showing 16 changed files with 732 additions and 43 deletions.
6 changes: 3 additions & 3 deletions electron/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ async function pushSaveDataToSteamCloud(saveData, currentPlayerId) {
*
* Instead of implementing it, the old code (encoding in base64) is used here for backward compatibility.
*/
const content = Buffer.from(saveData).toString("base64");
log.debug(`saveData: ${saveData.length} bytes`);
log.debug(`Base64 string of saveData: ${content.length} bytes`);
const content = saveData.toString("base64");
log.debug(`Uncompressed: ${saveData.length} bytes`);
log.debug(`Compressed: ${content.length} bytes`);
log.debug(`Saving to Steam Cloud as ${steamSaveName}`);

try {
Expand Down
6 changes: 2 additions & 4 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,10 @@ export const CONSTANTS: {

// Also update doc/source/changelog.rst
LatestUpdate: `
## v2.6.2 dev - Last update 2 June 2024
## v2.6.2 dev - Last update 22 May 2024
See 2.6.1 changelog at https://github.com/bitburner-official/bitburner-src/blob/v2.6.1/src/Documentation/doc/changelog.md
### HOTFIX (changes also added to 2.6.1 post release)
- Fixed an issue with invalid format on steam cloud save (@catloversg)
No changes yet since 2.6.1 release
`,
};
26 changes: 26 additions & 0 deletions src/Factory/Factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Bot, Entity, EntityType, Item } from "@nsdefs";
import { factory } from "./Helper";

export interface Factory {
bits: number;
entities: Entity[];
}

export const distance = (a: Entity, b: Entity) => Math.abs(a.x - b.x) + Math.abs(a.y - b.y);

export const adjacent = (a: Entity, b: Entity) => distance(a, b) === 1;

export const findBot = (name: string) =>
factory.entities.find((e): e is Bot => e.type === EntityType.Bot && e.name === name);

export const findEntityAt = <T extends Entity>(x: number, y: number, type?: EntityType): T | undefined =>
factory.entities.find((e): e is T => (!type || e.type === type) && e.x === x && e.y === y);

export const bitsMap: Record<Item, number> = {
[Item.BasicR]: 1,
[Item.BasicG]: 1,
[Item.BasicB]: 1,
[Item.ComplexR]: 4,
[Item.ComplexG]: 4,
[Item.ComplexB]: 4,
};
127 changes: 127 additions & 0 deletions src/Factory/Helper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { EntityType, Item } from "@nsdefs";
import { Factory } from "./Factory";

export const factorySize = 12;

const defaultFactory: Factory = {
bits: 0,
entities: [
{
type: EntityType.Dispenser,
dispensing: Item.BasicR,
x: Math.floor(factorySize / 4),
y: 0,
cooldown: 10000,
cooldownUntil: 0,
inventory: [Item.BasicR],
maxInventory: 1,
},
{
type: EntityType.Dispenser,
dispensing: Item.BasicG,
x: Math.floor(factorySize / 2),
y: 0,
cooldown: 10000,
cooldownUntil: 0,
inventory: [Item.BasicG],
maxInventory: 1,
},
{
type: EntityType.Dispenser,
dispensing: Item.BasicB,
x: Math.floor((factorySize * 3) / 4),
y: 0,
cooldown: 10000,
cooldownUntil: 0,
inventory: [Item.BasicB],
maxInventory: 1,
},
{
type: EntityType.Dock,
x: Math.floor(factorySize / 4),
y: Math.floor(factorySize - 1),
potentialRequest: [Item.BasicR, Item.BasicG, Item.BasicB],
potentialRequestCount: 1,
currentRequest: [Item.BasicR],
inventory: [],
maxInventory: 1,
},
{
type: EntityType.Dock,
x: Math.floor(factorySize / 2),
y: Math.floor(factorySize - 1),
potentialRequest: [Item.BasicR, Item.BasicG, Item.BasicB],
potentialRequestCount: 1,
currentRequest: [Item.BasicG],
inventory: [],
maxInventory: 1,
},
{
type: EntityType.Dock,
x: Math.floor((factorySize * 3) / 4),
y: Math.floor(factorySize - 1),
potentialRequest: [Item.BasicR, Item.BasicG, Item.BasicB],
potentialRequestCount: 1,
currentRequest: [Item.BasicB],
inventory: [],
maxInventory: 1,
},
{
type: EntityType.Dock,
x: 0,
y: Math.floor(factorySize - 1),
potentialRequest: [Item.ComplexR],
potentialRequestCount: 1,
currentRequest: [Item.ComplexR],
inventory: [],
maxInventory: 1,
},
{
type: EntityType.Bot,
x: Math.floor(factorySize / 2),
y: Math.floor(factorySize / 2),
inventory: [],
energy: 16,
name: "alice",
maxInventory: 1,
},
{
type: EntityType.Crafter,
x: 2,
y: 2,
inventory: [],
maxInventory: 3,
recipe: {
input: [Item.BasicR],
output: [Item.ComplexR],
},
},
{
type: EntityType.Chest,
inventory: [Item.BasicR],
maxInventory: 3,
x: Math.floor(factorySize / 2) + 1,
y: Math.floor(factorySize / 2),
},
],
};

export const factory: Factory = defaultFactory;

export const loadFactory = (save: string) => {
if (!save) return;
// const savedFactory = JSON.parse(save);
// Object.assign(factory, savedFactory);
};

export const NewBot = (name: string, x: number, y: number) => {
factory.entities.push({
type: EntityType.Bot,
x,
y,
inventory: [],
energy: 16,
name,
maxInventory: 1,
});
};
1 change: 1 addition & 0 deletions src/Factory/formulas/formulas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const botPrice = (currentBots: number): number => Math.pow(2, currentBots + 3);
150 changes: 150 additions & 0 deletions src/Factory/ui/EntityIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import React from "react";
import SmartToyIcon from "@mui/icons-material/SmartToy";
import { styled } from "@mui/system";
import { Dispenser, Entity, EntityType, Item } from "@nsdefs";

import MoveToInboxIcon from "@mui/icons-material/MoveToInbox";
import OutboxIcon from "@mui/icons-material/Outbox";
import CheckBoxOutlineBlankIcon from "@mui/icons-material/CheckBoxOutlineBlank";
import PrecisionManufacturingIcon from "@mui/icons-material/PrecisionManufacturing";
import { Tooltip, Typography } from "@mui/material";

export const cellSize = 48;

const defaultIconStyle = {
width: cellSize + "px",
height: cellSize + "px",
color: "white",
};

const colorRed = "red";
const colorBlue = "#1E90FF";
const colorGreen = "#7CFC00";

const itemColorMap: Record<Item, string> = {
[Item.BasicR]: colorRed,
[Item.BasicB]: colorBlue,
[Item.BasicG]: colorGreen,
[Item.ComplexR]: colorRed,
[Item.ComplexB]: colorBlue,
[Item.ComplexG]: colorGreen,
};

const BotIcon = styled(SmartToyIcon)(defaultIconStyle);
const DispenserIcon = styled(OutboxIcon)((props: { dispenser: Dispenser; col: string }) => ({
...defaultIconStyle,
color: new Date().getTime() > props.dispenser.cooldownUntil ? props.col : "gray",
}));

const DockIcon = styled(MoveToInboxIcon)({
...defaultIconStyle,
});

const CrafterIcon = styled(PrecisionManufacturingIcon)({
...defaultIconStyle,
});

const ChestIcon = styled(CheckBoxOutlineBlankIcon)({
...defaultIconStyle,
});

interface ITooltipContentProps {
entity: Entity;
content: React.ReactElement;
}
const TooltipContent = ({ entity, content }: ITooltipContentProps): React.ReactElement => {
return (
<>
<Typography>{entity.type}</Typography>
{content}
</>
);
};

const TooltipInventory = ({ entity }: { entity: Entity }): React.ReactElement => {
return (
<Typography component="span">
{entity.inventory.map((item) => (
<span key={item} style={{ color: itemColorMap[item] }}>
{item}
</span>
))}
</Typography>
);
};

export const EntityIcon = ({ entity }: { entity: Entity }): React.ReactElement => {
switch (entity.type) {
case EntityType.Chest: {
return (
<Tooltip title={<TooltipContent entity={entity} content={<TooltipInventory entity={entity} />} />}>
<ChestIcon />
</Tooltip>
);
}
case EntityType.Bot: {
return (
<Tooltip
title={
<Typography>
{entity.name}
<br /> <TooltipInventory entity={entity} />
</Typography>
}
>
<BotIcon />
</Tooltip>
);
}
case EntityType.Dispenser: {
return (
<Tooltip
title={
<>
<TooltipContent entity={entity} content={<Typography>{`dispensing: ${entity.dispensing}`}</Typography>} />
<TooltipInventory entity={entity} />
</>
}
>
<DispenserIcon dispenser={entity} col={itemColorMap[entity.dispensing]} />
</Tooltip>
);
break;
}
case EntityType.Dock: {
return (
<Tooltip
title={
<TooltipContent
entity={entity}
content={<Typography>{`requesting: ${entity.currentRequest}`}</Typography>}
/>
}
>
<DockIcon sx={{ color: itemColorMap[entity.currentRequest[0] ?? Item.BasicR] }} />
</Tooltip>
);
}
case EntityType.Crafter: {
return (
<Tooltip
title={
<TooltipContent
entity={entity}
content={
<>
<Typography>{`input: ${entity.recipe.input}, output: ${entity.recipe.output}`}</Typography>
<TooltipInventory entity={entity} />
</>
}
/>
}
>
<CrafterIcon />
</Tooltip>
);
break;
}
}
return <></>;
};
Loading

0 comments on commit f0b9b2d

Please sign in to comment.