SimulationBuilder.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. FactorioEngineOrchestrator,
  3. MachineSimulator,
  4. InserterSimulator,
  5. FilterableInserterSimulator,
  6. Chest,
  7. Belt,
  8. } from "./simulator";
  9. import type { Recipe } from "../../scripts/factorio-dump/helpers/recipes.helper";
  10. import type { Machine } from "../../scripts/factorio-dump/process-data.models";
  11. import type { MachineSetup } from "./model";
  12. import data from "../assets/data/2.0/data.json"
  13. // --- Shared Fixtures ---
  14. export const StandardSetups = {
  15. assembler1: { machine: { crafting_speed: 1 } as Machine, machineModules: [], beacons: [], machineQualityLevel: 0 },
  16. assembler2: { machine: { name: "assembling-machine-2", crafting_speed: 10 } as Machine, machineModules: [], beacons: [], machineQualityLevel: 0 },
  17. empSpeed: { machine: { name: "electromagnetic-plant", crafting_speed: 6 } as Machine, machineModules: [], beacons: [], machineQualityLevel: 5 },
  18. fastAssembler: { machine: { crafting_speed: 60 } as Machine, machineModules: [], beacons: [], machineQualityLevel: 0 },
  19. instantAssembler: { machine: { crafting_speed: 30 } as Machine, machineModules: [], beacons: [], machineQualityLevel: 0 }
  20. };
  21. // --- The Builder ---
  22. export class SimulationBuilder {
  23. public orchestrator = new FactorioEngineOrchestrator();
  24. public machines: Record<string, MachineSimulator> = {};
  25. public chests: Record<string, Chest> = {};
  26. public belts: Record<string, Belt> = {};
  27. public inserters: Record<string, InserterSimulator | FilterableInserterSimulator> = {};
  28. addMachine(id: string, recipe: Recipe, setup: MachineSetup = StandardSetups.assembler1 ) {
  29. const machine = new MachineSimulator(setup, recipe, data.stackSizes);
  30. this.machines[id] = machine;
  31. this.orchestrator.registerMachine(machine);
  32. return this;
  33. }
  34. addChest(id: string, initialItem?: string) {
  35. this.chests[id] = initialItem ? new Chest(initialItem) : new Chest();
  36. return this;
  37. }
  38. addBelt(id: string, ...items: string[]) {
  39. this.belts[id] = new Belt(...items);
  40. return this;
  41. }
  42. addInserter(
  43. id: string,
  44. handSize: number,
  45. sourceId: string,
  46. targetId: string,
  47. filters?: string | string[],
  48. isDynamicFilter: boolean = false
  49. ) {
  50. const source = this.getEntity(sourceId);
  51. const target = this.getEntity(targetId);
  52. let inserter;
  53. if (Array.isArray(filters) || isDynamicFilter) {
  54. inserter = new FilterableInserterSimulator(handSize, source, target, Array.isArray(filters) ? filters : undefined);
  55. if (isDynamicFilter) inserter.useDynamicFilters = true;
  56. } else {
  57. inserter = new InserterSimulator(handSize, source, target, filters as string);
  58. }
  59. this.inserters[id] = inserter;
  60. this.orchestrator.registerInserter(inserter);
  61. return this;
  62. }
  63. bindToClock(inserterId: string, rowId: string) {
  64. const inserter = this.inserters[inserterId];
  65. if (!inserter) throw new Error(`Inserter ${inserterId} not found`);
  66. this.orchestrator.bindInserterToRow(inserter, rowId);
  67. return this;
  68. }
  69. private getEntity(id: string) {
  70. const entity = this.chests[id] || this.machines[id] || this.belts[id];
  71. if (!entity) throw new Error(`Entity '${id}' not found in builder registry. Declare it before using it in an inserter.`);
  72. return entity;
  73. }
  74. build() {
  75. return {
  76. orchestrator: this.orchestrator,
  77. machines: this.machines,
  78. chests: this.chests,
  79. belts: this.belts,
  80. inserters: this.inserters,
  81. };
  82. }
  83. }