Skip to content

Commit

Permalink
vWhatever.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Gagnon committed Jun 6, 2024
1 parent f0b9b2d commit c6c2b3b
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 180 deletions.
31 changes: 30 additions & 1 deletion src/Factory/Factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { Bot, Entity, EntityType, Item } from "@nsdefs";
import {
Bot,
Entity,
EntityType,
ContainerEntity,
Item,
Dispenser,
Dock,
Crafter,
Chest,
Wall,
BaseEntity,
EntityID,
} from "@nsdefs";
import { factory } from "./Helper";

export interface Factory {
Expand All @@ -24,3 +37,19 @@ export const bitsMap: Record<Item, number> = {
[Item.ComplexG]: 4,
[Item.ComplexB]: 4,
};

export const isEntityContainer = (entity: BaseEntity): entity is ContainerEntity => "inventory" in entity;

export const isEntityBot = (e: Entity): e is Bot => e.type === EntityType.Bot;
export const isEntityDispenser = (e: Entity): e is Dispenser => e.type === EntityType.Dispenser;
export const isEntityDock = (e: Entity): e is Dock => e.type === EntityType.Dock;
export const isEntityCrafter = (e: Entity): e is Crafter => e.type === EntityType.Crafter;
export const isEntityChest = (e: Entity): e is Chest => e.type === EntityType.Chest;
export const isEntityWall = (e: Entity): e is Wall => e.type === EntityType.Wall;

export const findEntity = (id: EntityID, type?: EntityType): Entity | undefined =>
factory.entities.find(
typeof id === "string"
? (e) => e.name === id && (!type || type === e.type)
: (e) => e.x === id[0] && e.y === id[1] && (!type || type === e.type),
);
193 changes: 88 additions & 105 deletions src/Factory/Helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,115 +5,11 @@ 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),
},
],
entities: [],
};

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,
Expand All @@ -125,3 +21,90 @@ export const NewBot = (name: string, x: number, y: number) => {
maxInventory: 1,
});
};

export const NewChest = (name: string, x: number, y: number) => {
factory.entities.push({
name,
type: EntityType.Chest,
inventory: [],
maxInventory: 1,
x,
y,
});
};

export const NewCrafter = (name: string, x: number, y: number) => {
factory.entities.push({
name,
type: EntityType.Crafter,
x,
y,
inventory: [],
maxInventory: 2,
recipe: {
input: [Item.BasicR, Item.BasicR],
output: [Item.ComplexR],
},
});
};

export const NewDispenser = (name: string, x: number, y: number, dispensing: Item) => {
factory.entities.push({
name,
type: EntityType.Dispenser,
x,
y,
dispensing,
cooldown: 10000,
cooldownUntil: 0,
inventory: [dispensing],
maxInventory: 1,
});
};

export const NewDock = (name: string, x: number, y: number) => {
const potential = [Item.BasicR, Item.BasicG, Item.BasicB];
factory.entities.push({
name,
type: EntityType.Dock,
x,
y,
potentialRequest: [Item.BasicR, Item.BasicG, Item.BasicB],
potentialRequestCount: 1,
currentRequest: [potential[Math.floor(Math.random() * potential.length)]],
inventory: [],
maxInventory: 1,
});
};

export const NewWall = (name: string, x: number, y: number) => {
factory.entities.push({
name,
type: EntityType.Wall,
x,
y,
});
};

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

export const resetFactory = () => {
factory.bits = 0;
factory.entities = [];
Object.assign(factory, defaultFactory);
NewBot("alice", Math.floor(factorySize / 2), Math.floor(factorySize / 2));
NewDispenser("disp0", Math.floor(factorySize / 4), 0, Item.BasicR);
NewDispenser("disp1", Math.floor(factorySize / 2), 0, Item.BasicG);
NewDispenser("disp2", Math.floor((factorySize * 3) / 4), 0, Item.BasicB);

NewDock("dock0", Math.floor(factorySize / 4), Math.floor(factorySize - 1));
NewDock("dock1", Math.floor(factorySize / 2), Math.floor(factorySize - 1));
NewDock("dock2", Math.floor((factorySize * 3) / 4), Math.floor(factorySize - 1));
NewWall("wall0", 2, 2);
};

resetFactory();
34 changes: 34 additions & 0 deletions src/Factory/formulas/formulas.ts
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
import { EntityType } from "@nsdefs";

export type FactoryFormulaParams = [number, number, number, number];

export const inventoryScale: Record<EntityType, FactoryFormulaParams> = {
[EntityType.Bot]: [8, 0.5, 2, 0],
[EntityType.Dispenser]: [4, 1, 5, 0],
[EntityType.Dock]: [Infinity, Infinity, Infinity, Infinity],
[EntityType.Crafter]: [Infinity, 1, -1, 4095],
[EntityType.Chest]: [1.2, 10, 0, 63],
[EntityType.Wall]: [Infinity, Infinity, Infinity, Infinity],
};

// a^(b*X+c)+d
const exp = (p: FactoryFormulaParams, x: number): number => Math.floor(Math.pow(p[0], p[1] * x + p[2]) + p[3]);

export const upgradeMaxInventoryCost = (type: EntityType, currentMaxInventory: number): number =>
exp(inventoryScale[type], currentMaxInventory);

export const botPrice = (currentBots: number): number => Math.pow(2, currentBots + 3);

/**
glitches:
1 - moving dock & dispensers
2 - energy consumption
3 - ugrade degradation
4 - power hinderance (speed & transfer)
5 - dexterity hinderance (craft & build)
6 - dock complexity
7 - random walls
*/
23 changes: 13 additions & 10 deletions src/Factory/ui/EntityIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ 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 BlockIcon from "@mui/icons-material/Block";
import { Tooltip, Typography } from "@mui/material";
import { isEntityContainer } from "../Factory";

export const cellSize = 48;

Expand All @@ -30,23 +32,18 @@ const itemColorMap: Record<Item, string> = {
[Item.ComplexG]: colorGreen,
};

const WallIcon = styled(BlockIcon)(defaultIconStyle);
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 DockIcon = styled(MoveToInboxIcon)(defaultIconStyle);

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

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

interface ITooltipContentProps {
entity: Entity;
Expand All @@ -55,13 +52,16 @@ interface ITooltipContentProps {
const TooltipContent = ({ entity, content }: ITooltipContentProps): React.ReactElement => {
return (
<>
<Typography>{entity.type}</Typography>
<Typography>
{entity.name} ({entity.type})
</Typography>
{content}
</>
);
};

const TooltipInventory = ({ entity }: { entity: Entity }): React.ReactElement => {
if (!isEntityContainer(entity)) return <></>;
return (
<Typography component="span">
{entity.inventory.map((item) => (
Expand All @@ -75,6 +75,9 @@ const TooltipInventory = ({ entity }: { entity: Entity }): React.ReactElement =>

export const EntityIcon = ({ entity }: { entity: Entity }): React.ReactElement => {
switch (entity.type) {
case EntityType.Wall: {
return <WallIcon />;
}
case EntityType.Chest: {
return (
<Tooltip title={<TooltipContent entity={entity} content={<TooltipInventory entity={entity} />} />}>
Expand Down
Loading

0 comments on commit c6c2b3b

Please sign in to comment.