forked from clusterio/gridworld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.ts
More file actions
1031 lines (932 loc) · 30.2 KB
/
controller.ts
File metadata and controls
1031 lines (932 loc) · 30.2 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from "fs/promises";
import path from "path";
import * as lib from "@clusterio/lib";
import { BaseControllerPlugin, type InstanceInfo } from "@clusterio/controller";
import * as messages from "./messages";
type TileRecord = {
x: number;
y: number;
instanceId: number;
saveName: string;
createdAtMs: number;
};
type ParsedMapSettings = {
mapGenSettings: Record<string, any>;
mapSettings: Record<string, any>;
seed?: number;
};
type EdgeTargetSpec = {
instanceId: number;
origin: [number, number];
surface: string;
direction: number;
ready: boolean;
};
type UniversalEdgesController = {
edgeDatastore?: Map<string, any>;
handleSetEdgeConfigRequest?: (request: { edge: any }) => Promise<void> | void;
};
// Universal edges uses 16-direction values where 0=east, 4=south, 8=west, 12=north.
const EDGE_DIRECTIONS = {
north: 0,
east: 4,
south: 8,
west: 12,
} as const;
const NEIGHBOR_DELTAS = [
{ dx: 0, dy: -1 },
{ dx: 1, dy: 0 },
{ dx: 0, dy: 1 },
{ dx: -1, dy: 0 },
];
const ACTIVE_INSTANCE_STATUSES = new Set<lib.InstanceStatus>([
"starting",
"running",
"stopping",
"creating_save",
"exporting_data",
]);
function tileKey(x: number, y: number) {
return `${x},${y}`;
}
function edgeKey(a: TileRecord, b: TileRecord) {
const aKey = tileKey(a.x, a.y);
const bKey = tileKey(b.x, b.y);
return aKey < bKey ? `gridworld:${aKey}:${bKey}` : `gridworld:${bKey}:${aKey}`;
}
function deepClone<T>(value: T): T {
return JSON.parse(JSON.stringify(value)) as T;
}
async function loadTiles(
config: lib.ControllerConfig,
logger: lib.Logger,
): Promise<Map<string, TileRecord>> {
const filePath = path.resolve(config.get("controller.database_directory"), "gridworld_tiles.json");
logger.verbose(`Loading ${filePath}`);
try {
const content = await fs.readFile(filePath, "utf8");
if (!content.trim()) {
return new Map();
}
const parsed = JSON.parse(content) as TileRecord[];
const map = new Map<string, TileRecord>();
const saveNamePrefix = config.get("gridworld.save_name_prefix");
for (const entry of parsed) {
if (!entry || typeof entry !== "object") {
continue;
}
const e = entry as any;
if (!Number.isFinite(e.x) || !Number.isFinite(e.y) || !Number.isFinite(e.instanceId)) {
continue;
}
const x = e.x as number;
const y = e.y as number;
const instanceId = e.instanceId as number;
const saveName = typeof e.saveName === "string"
? e.saveName
: `${saveNamePrefix}_${x}_${y}.zip`;
const createdAtMs = Number.isFinite(e.createdAtMs)
? e.createdAtMs as number
: Date.now();
map.set(tileKey(x, y), {
x,
y,
instanceId,
saveName,
createdAtMs,
});
}
return map;
} catch (err: any) {
if (err.code === "ENOENT") {
logger.verbose("Creating new gridworld tile database");
return new Map();
}
throw err;
}
}
async function saveTiles(
config: lib.ControllerConfig,
tiles: Map<string, TileRecord>,
logger: lib.Logger,
) {
const filePath = path.resolve(config.get("controller.database_directory"), "gridworld_tiles.json");
logger.verbose(`writing ${filePath}`);
await lib.safeOutputFile(filePath, JSON.stringify([...tiles.values()], null, "\t"));
}
export class ControllerPlugin extends BaseControllerPlugin {
private tiles = new Map<string, TileRecord>();
private tilesByInstance = new Map<number, TileRecord>();
private pendingTiles = new Map<string, Promise<TileRecord>>();
private pendingStarts = new Map<number, Promise<void>>();
private hostAssignIndex = 0;
private storageDirty = false;
private parsedMapSettings: ParsedMapSettings | null = null;
private mapExchangeError: string | null = null;
private stateUpdatedAtMs = Date.now();
private stateBroadcastQueued = false;
async init() {
this.controller.handle(messages.GridworldStateRequest, this.handleGridworldStateRequest.bind(this));
this.controller.handle(messages.GridworldCreateRequest, this.handleGridworldCreateRequest.bind(this));
this.controller.handle(messages.GridworldDeleteRequest, this.handleGridworldDeleteRequest.bind(this));
this.controller.subscriptions.handle(messages.GridworldStateUpdate, this.handleGridworldStateSubscription.bind(this));
this.tiles = await loadTiles(this.controller.config, this.logger);
this.rebuildTileIndex();
this.loadMapExchangeString();
await this.syncTileInstanceConfigs();
await this.ensureEdgesForKnownTiles();
}
async onSaveData() {
if (this.storageDirty) {
await saveTiles(this.controller.config, this.tiles, this.logger);
this.storageDirty = false;
}
}
async onPlayerEvent(instance: InstanceInfo, event: lib.PlayerEvent) {
if (event.type !== "join") {
return;
}
const tile = this.tilesByInstance.get(instance.id);
if (!tile) {
return;
}
try {
await this.ensureNeighbors(tile);
await this.configureSpawnPosition(tile);
} catch (err: any) {
this.logger.warn(
`Failed handling player join for tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
}
}
async onControllerConfigFieldChanged(field: string) {
if (field === "gridworld.map_exchange_string") {
this.loadMapExchangeString();
this.markStateDirty();
}
if (field === "gridworld.tile_size" || field === "gridworld.surface_name") {
await this.syncTileInstanceConfigs();
await this.ensureEdgesForKnownTiles();
await this.configureSpawnPositionsForKnownTiles();
if (field === "gridworld.tile_size") {
this.markStateDirty();
}
}
if (field === "gridworld.initial_tile_x" || field === "gridworld.initial_tile_y") {
this.markStateDirty();
}
}
private rebuildTileIndex() {
this.tilesByInstance.clear();
for (const [key, tile] of [...this.tiles.entries()]) {
if (!this.controller.instances.has(tile.instanceId)) {
this.logger.warn(`Dropping tile ${key} for missing instance ${tile.instanceId}`);
this.tiles.delete(key);
this.storageDirty = true;
continue;
}
this.tilesByInstance.set(tile.instanceId, tile);
}
}
private async syncTileInstanceConfigs() {
const tileSize = this.controller.config.get("gridworld.tile_size");
const surfaceName = this.controller.config.get("gridworld.surface_name");
for (const tile of this.tiles.values()) {
const instance = this.controller.instances.get(tile.instanceId);
if (!instance) {
continue;
}
let updated = false;
if (instance.config.get("gridworld.tile_x") !== tile.x) {
instance.config.set("gridworld.tile_x", tile.x, "controller");
updated = true;
}
if (instance.config.get("gridworld.tile_y") !== tile.y) {
instance.config.set("gridworld.tile_y", tile.y, "controller");
updated = true;
}
if (instance.config.get("gridworld.tile_size") !== tileSize) {
instance.config.set("gridworld.tile_size", tileSize, "controller");
updated = true;
}
if (instance.config.get("gridworld.surface_name") !== surfaceName) {
instance.config.set("gridworld.surface_name", surfaceName, "controller");
updated = true;
}
if (updated) {
try {
await this.controller.instanceConfigUpdated(instance);
} catch (err: any) {
this.logger.warn(
`Failed updating config for tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
}
}
}
}
private setInstanceConfigForTile(instanceConfig: lib.InstanceConfig, x: number, y: number) {
instanceConfig.set("gridworld.tile_x", x, "controller");
instanceConfig.set("gridworld.tile_y", y, "controller");
instanceConfig.set("gridworld.tile_size", this.controller.config.get("gridworld.tile_size"), "controller");
instanceConfig.set("gridworld.surface_name", this.controller.config.get("gridworld.surface_name"), "controller");
}
private loadMapExchangeString() {
const exchangeString = this.controller.config.get("gridworld.map_exchange_string");
if (!exchangeString || !exchangeString.trim()) {
this.parsedMapSettings = null;
this.mapExchangeError = "Map exchange string is not configured.";
return;
}
try {
const parsed = lib.readMapExchangeString(exchangeString);
this.parsedMapSettings = {
mapGenSettings: parsed.map_gen_settings,
mapSettings: parsed.map_settings,
seed: parsed.map_gen_settings.seed,
};
this.mapExchangeError = null;
} catch (err: any) {
this.parsedMapSettings = null;
this.mapExchangeError = err.message ?? String(err);
this.logger.error(`Failed to parse map exchange string: ${this.mapExchangeError}`);
}
}
private getUniversalEdgesController(): UniversalEdgesController | null {
const plugin = this.controller.plugins.get("universal_edges") as UniversalEdgesController | undefined;
if (!plugin) {
this.logger.warn("Universal edges plugin not loaded; gridworld edges will not be created.");
return null;
}
return plugin;
}
private async ensureTile(x: number, y: number, reason: string): Promise<TileRecord> {
const key = tileKey(x, y);
const existing = this.tiles.get(key);
if (existing) {
return existing;
}
const pending = this.pendingTiles.get(key);
if (pending) {
return await pending;
}
const creation = this.createTile(x, y, reason).finally(() => {
this.pendingTiles.delete(key);
});
this.pendingTiles.set(key, creation);
return await creation;
}
private async createTile(x: number, y: number, reason: string): Promise<TileRecord> {
const mapSettings = this.buildMapSettingsForTile(x, y);
if (!mapSettings) {
throw new lib.ResponseError(
`Cannot create tile ${x},${y}: ${this.mapExchangeError ?? "map exchange string is not configured."}`,
);
}
const hostId = this.getHostIdForTile();
if (hostId === undefined) {
throw new lib.ResponseError(`Cannot create tile ${x},${y}: no hosts connected.`);
}
const instanceName = `gridworld_${x}_${y}`;
const saveName = `${this.controller.config.get("gridworld.save_name_prefix")}_${x}_${y}.zip`;
const instanceConfig = new lib.InstanceConfig("controller");
instanceConfig.set("instance.name", instanceName, "controller");
instanceConfig.set("instance.auto_start", false, "controller");
this.setInstanceConfigForTile(instanceConfig, x, y);
await this.controller.instanceCreate(instanceConfig);
const instanceId = instanceConfig.get("instance.id");
const tile: TileRecord = {
x,
y,
instanceId,
saveName,
createdAtMs: Date.now(),
};
try {
const assigned = await this.assignAndSetupInstance(tile, hostId);
if (!assigned) {
throw new lib.ResponseError(
`Failed assigning instance ${instanceId} for tile ${x},${y}`,
);
}
await this.ensureInstanceStarted(tile, reason, mapSettings);
await this.ensureEdgesForTile(tile);
} catch (err: any) {
this.logger.error(
`Tile creation failed for ${x},${y} (instance ${instanceId}): ${err?.message ?? err}`,
);
await this.cleanupFailedTileCreation(tile);
throw err;
}
this.tiles.set(tileKey(x, y), tile);
this.tilesByInstance.set(instanceId, tile);
this.storageDirty = true;
this.markStateDirty();
this.logger.info(`Created tile ${x},${y} for instance ${instanceId} (${reason})`);
return tile;
}
private getHostIdForTile(): number | undefined {
const hostIds = [...this.controller.wsServer.hostConnections.keys()];
if (!hostIds.length) {
this.logger.warn("No hosts connected; cannot assign new gridworld instances.");
return undefined;
}
hostIds.sort((a, b) => a - b);
const hostId = hostIds[this.hostAssignIndex % hostIds.length];
this.hostAssignIndex = (this.hostAssignIndex + 1) % hostIds.length;
return hostId;
}
private async assignAndSetupInstance(tile: TileRecord, hostId?: number): Promise<boolean> {
const resolvedHostId = hostId ?? this.getHostIdForTile();
if (resolvedHostId === undefined) {
return false;
}
try {
await this.controller.instanceAssign(tile.instanceId, resolvedHostId);
return true;
} catch (err: any) {
this.logger.error(`Failed to assign instance ${tile.instanceId}: ${err?.message ?? err}`);
return false;
}
}
private buildMapSettingsForTile(x: number, y: number): ParsedMapSettings | null {
if (!this.parsedMapSettings) {
return null;
}
const mapGenSettings = deepClone(this.parsedMapSettings.mapGenSettings);
const mapSettings = deepClone(this.parsedMapSettings.mapSettings);
const initialX = this.controller.config.get("gridworld.initial_tile_x");
const initialY = this.controller.config.get("gridworld.initial_tile_y");
const tileSize = this.controller.config.get("gridworld.tile_size");
const offsetX = initialX * tileSize;
const offsetY = initialY * tileSize;
if (mapGenSettings.area_to_generate_at_start) {
const area = mapGenSettings.area_to_generate_at_start;
if (area.left_top) {
area.left_top.x += offsetX;
area.left_top.y += offsetY;
}
if (area.right_bottom) {
area.right_bottom.x += offsetX;
area.right_bottom.y += offsetY;
}
}
if (Array.isArray(mapGenSettings.starting_points)) {
mapGenSettings.starting_points = mapGenSettings.starting_points.map((point: { x: number; y: number }) => ({
x: point.x + offsetX,
y: point.y + offsetY,
}));
} else {
mapGenSettings.starting_points = [{ x: offsetX, y: offsetY }];
}
const seed = typeof mapGenSettings.seed === "number"
? mapGenSettings.seed
: this.parsedMapSettings.seed;
return {
mapGenSettings,
mapSettings,
seed,
};
}
private async ensureNeighbors(tile: TileRecord) {
for (const delta of NEIGHBOR_DELTAS) {
const neighbor = await this.ensureTile(tile.x + delta.dx, tile.y + delta.dy, "neighbor");
await this.ensureInstanceStarted(neighbor, "neighbor");
await this.ensureEdgeBetween(tile, neighbor);
}
}
private async ensureEdgesForKnownTiles() {
for (const tile of this.tiles.values()) {
await this.ensureEdgesForTile(tile);
}
}
private async ensureEdgesForTile(tile: TileRecord) {
for (const delta of NEIGHBOR_DELTAS) {
const neighbor = this.tiles.get(tileKey(tile.x + delta.dx, tile.y + delta.dy));
if (neighbor) {
await this.ensureEdgeBetween(tile, neighbor);
}
}
}
private async ensureEdgeBetween(tile: TileRecord, neighbor: TileRecord) {
const ue = this.getUniversalEdgesController();
if (!ue?.handleSetEdgeConfigRequest) {
return;
}
const edgeId = edgeKey(tile, neighbor);
const existingEdge = ue.edgeDatastore?.get(edgeId);
const [sourceTile, targetTile] = this.orderTiles(tile, neighbor);
const sourceSpec = this.buildEdgeTargetSpec(sourceTile, targetTile);
const targetSpec = this.buildEdgeTargetSpec(targetTile, sourceTile);
const tileSize = this.controller.config.get("gridworld.tile_size");
const desiredEdge = {
id: edgeId,
updatedAtMs: Date.now(),
isDeleted: false,
source: sourceSpec,
target: targetSpec,
length: tileSize,
link_destinations: existingEdge?.link_destinations ?? {},
};
if (existingEdge && !existingEdge.isDeleted) {
if (!this.edgeConfigNeedsUpdate(existingEdge, desiredEdge)) {
return;
}
await ue.handleSetEdgeConfigRequest({
edge: {
...existingEdge,
...desiredEdge,
},
});
return;
}
await ue.handleSetEdgeConfigRequest({
edge: {
...existingEdge,
...desiredEdge,
active: existingEdge?.active ?? true,
},
});
}
private edgeTargetSpecEquals(a: any, b: any) {
if (!a || !b) {
return false;
}
if (a.instanceId !== b.instanceId) {
return false;
}
if (a.surface !== b.surface) {
return false;
}
if (a.direction !== b.direction) {
return false;
}
if (Boolean(a.ready) !== Boolean(b.ready)) {
return false;
}
const aOrigin = Array.isArray(a.origin) ? a.origin : null;
const bOrigin = Array.isArray(b.origin) ? b.origin : null;
if (!aOrigin || !bOrigin || aOrigin.length < 2 || bOrigin.length < 2) {
return false;
}
return aOrigin[0] === bOrigin[0] && aOrigin[1] === bOrigin[1];
}
private edgeConfigNeedsUpdate(existingEdge: any, desiredEdge: any) {
if (!existingEdge || !desiredEdge) {
return true;
}
if (existingEdge.length !== desiredEdge.length) {
return true;
}
if (!this.edgeTargetSpecEquals(existingEdge.source, desiredEdge.source)) {
return true;
}
if (!this.edgeTargetSpecEquals(existingEdge.target, desiredEdge.target)) {
return true;
}
return false;
}
private orderTiles(a: TileRecord, b: TileRecord): [TileRecord, TileRecord] {
if (a.x !== b.x) {
return a.x < b.x ? [a, b] : [b, a];
}
if (a.y !== b.y) {
return a.y < b.y ? [a, b] : [b, a];
}
return [a, b];
}
private buildEdgeTargetSpec(tile: TileRecord, neighbor: TileRecord): EdgeTargetSpec {
const bounds = this.tileBounds(tile.x, tile.y);
const surface = this.controller.config.get("gridworld.surface_name");
const side = this.getSide(tile, neighbor);
const { origin, direction } = this.edgeSideSpec(bounds, side);
return {
instanceId: tile.instanceId,
origin: [origin[0], origin[1]],
surface,
direction,
ready: true,
};
}
private getSide(tile: TileRecord, neighbor: TileRecord): "north" | "east" | "south" | "west" {
if (neighbor.x === tile.x && neighbor.y === tile.y - 1) {
return "north";
}
if (neighbor.x === tile.x && neighbor.y === tile.y + 1) {
return "south";
}
if (neighbor.x === tile.x + 1 && neighbor.y === tile.y) {
return "east";
}
return "west";
}
private tileBounds(x: number, y: number) {
const tileSize = this.controller.config.get("gridworld.tile_size");
const half = tileSize / 2;
const centerX = x * tileSize;
const centerY = y * tileSize;
return {
minX: centerX - half,
maxX: centerX + half,
minY: centerY - half,
maxY: centerY + half,
};
}
private edgeSideSpec(bounds: { minX: number; maxX: number; minY: number; maxY: number }, side: "north" | "east" | "south" | "west") {
switch (side) {
case "north":
return {
origin: [bounds.minX, bounds.minY] as [number, number],
direction: EDGE_DIRECTIONS.north,
};
case "south":
return {
origin: [bounds.maxX, bounds.maxY] as [number, number],
direction: EDGE_DIRECTIONS.south,
};
case "east":
return {
origin: [bounds.maxX, bounds.minY] as [number, number],
direction: EDGE_DIRECTIONS.east,
};
case "west":
default:
return {
origin: [bounds.minX, bounds.maxY] as [number, number],
direction: EDGE_DIRECTIONS.west,
};
}
}
private async ensureInstanceStarted(tile: TileRecord, reason: string, mapSettings?: ParsedMapSettings) {
const pending = this.pendingStarts.get(tile.instanceId);
if (pending) {
await pending;
return;
}
const startPromise = this.startInstanceIfNeeded(tile, reason, mapSettings).finally(() => {
this.pendingStarts.delete(tile.instanceId);
});
this.pendingStarts.set(tile.instanceId, startPromise);
await startPromise;
}
private async startInstanceIfNeeded(tile: TileRecord, reason: string, mapSettingsOverride?: ParsedMapSettings) {
const instance = this.controller.instances.get(tile.instanceId);
if (!instance) {
this.logger.warn(`Missing instance ${tile.instanceId} for tile ${tile.x},${tile.y}`);
return;
}
if (instance.status === "running" || instance.status === "starting" || instance.status === "creating_save") {
return;
}
if (instance.config.get("instance.assigned_host") === null) {
const assigned = await this.assignAndSetupInstance(tile);
const updated = this.controller.instances.get(tile.instanceId);
if (!assigned || updated?.config.get("instance.assigned_host") === null) {
this.logger.warn(
`Cannot start tile ${tile.x},${tile.y}: instance ${tile.instanceId} is not assigned to a host.`,
);
return;
}
}
const mapSettings = mapSettingsOverride ?? this.buildMapSettingsForTile(tile.x, tile.y);
if (!mapSettings) {
this.logger.error(`Skipping save creation for tile ${tile.x},${tile.y}: ${this.mapExchangeError}`);
return;
}
const hasSave = [...this.controller.saves.values()].some(save =>
save.instanceId === tile.instanceId && save.name === tile.saveName && !save.isDeleted
);
if (!hasSave) {
try {
await this.controller.sendTo(
{ instanceId: tile.instanceId },
new lib.InstanceCreateSaveRequest(
tile.saveName,
mapSettings.seed,
mapSettings.mapGenSettings,
mapSettings.mapSettings,
),
);
} catch (err: any) {
this.logger.error(
`Failed creating save for tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
return;
}
}
try {
await this.controller.sendTo(
{ instanceId: tile.instanceId },
new lib.InstanceStartRequest(tile.saveName),
);
this.logger.info(`Started tile ${tile.x},${tile.y} (${reason})`);
} catch (err: any) {
this.logger.error(
`Failed starting instance ${tile.instanceId} for tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
return;
}
try {
await this.configureFreeplayIntro(tile);
} catch (err: any) {
this.logger.warn(
`Failed configuring freeplay intro for tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
}
try {
await this.configureSpawnPosition(tile);
} catch (err: any) {
this.logger.warn(
`Failed configuring spawn for tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
}
}
private async cleanupFailedTileCreation(tile: TileRecord) {
try {
const edgeCleanupTiles: TileRecord[] = [tile];
for (const delta of NEIGHBOR_DELTAS) {
const neighbor = this.tiles.get(tileKey(tile.x + delta.dx, tile.y + delta.dy));
if (neighbor) {
edgeCleanupTiles.push(neighbor);
}
}
await this.removeEdgesForTiles(edgeCleanupTiles);
} catch (err: any) {
this.logger.warn(
`Failed removing edges for aborted tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
}
await this.stopInstanceBeforeDelete(tile.instanceId, `aborted tile ${tile.x},${tile.y}`);
try {
await this.controller.instanceDelete(tile.instanceId);
} catch (err: any) {
this.logger.error(
`Failed deleting aborted tile instance ${tile.instanceId} (${tile.x},${tile.y}): ${err?.message ?? err}`,
);
}
}
private async configureFreeplayIntro(tile: TileRecord) {
const commands = [
`/c remote.call("freeplay", "set_disable_crashsite", true)`,
`/c remote.call("freeplay", "set_skip_intro", true)`,
];
for (const command of commands) {
await this.controller.sendTo(
{ instanceId: tile.instanceId },
new lib.InstanceSendRconRequest(command),
);
}
}
private async configureSpawnPosition(tile: TileRecord) {
const tileSize = this.controller.config.get("gridworld.tile_size");
const surface = this.controller.config.get("gridworld.surface_name");
const escapedSurface = lib.escapeString(surface);
const x = tile.x * tileSize;
const y = tile.y * tileSize;
const command = `/c local force=game.forces.player; local surface=game.surfaces["${escapedSurface}"]; if force and surface then force.set_spawn_position({x=${x}, y=${y}}, surface) end`;
await this.controller.sendTo(
{ instanceId: tile.instanceId },
new lib.InstanceSendRconRequest(command),
);
}
private async configureSpawnPositionsForKnownTiles() {
for (const tile of this.tiles.values()) {
const instance = this.controller.instances.get(tile.instanceId);
if (!instance || instance.status !== "running") {
continue;
}
try {
await this.configureSpawnPosition(tile);
} catch (err: any) {
this.logger.warn(
`Failed configuring spawn for tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
}
}
}
private async stopInstanceBeforeDelete(instanceId: number, context?: string) {
const instance = this.controller.instances.get(instanceId);
if (!instance) {
return;
}
if (!ACTIVE_INSTANCE_STATUSES.has(instance.status)) {
return;
}
try {
await this.controller.sendTo(
{ instanceId },
new lib.InstanceStopRequest(),
);
} catch (err: any) {
const contextSuffix = context ? ` (${context})` : "";
this.logger.error(
`Failed stopping instance ${instanceId}${contextSuffix}: ${err?.message ?? err}`,
);
return;
}
const deadline = Date.now() + 5000;
while (Date.now() < deadline) {
const updated = this.controller.instances.get(instanceId);
if (!updated || !ACTIVE_INSTANCE_STATUSES.has(updated.status)) {
break;
}
/* eslint-disable-next-line no-await-in-loop */
await new Promise(resolve => setTimeout(resolve, 250));
}
}
private getStateValue(): messages.GridworldStateValue {
return {
id: "state",
updatedAtMs: this.stateUpdatedAtMs,
isDeleted: false,
...this.getState(),
};
}
private markStateDirty() {
const now = Date.now();
this.stateUpdatedAtMs = now > this.stateUpdatedAtMs ? now : this.stateUpdatedAtMs + 1;
if (this.stateBroadcastQueued) {
return;
}
this.stateBroadcastQueued = true;
setImmediate(() => {
this.stateBroadcastQueued = false;
this.controller.subscriptions.broadcast(new messages.GridworldStateUpdate([
this.getStateValue(),
]));
});
}
private async handleGridworldStateSubscription(request: lib.SubscriptionRequest) {
if (this.stateUpdatedAtMs <= request.lastRequestTimeMs) {
return null;
}
return new messages.GridworldStateUpdate([
this.getStateValue(),
]);
}
private getState(): messages.GridworldStateResponse {
const tiles = [...this.tiles.values()].map(tile => ({
x: tile.x,
y: tile.y,
instanceId: tile.instanceId,
saveName: tile.saveName,
createdAtMs: tile.createdAtMs,
}));
tiles.sort((a, b) => (a.y - b.y) || (a.x - b.x));
return {
tiles,
tileSize: this.controller.config.get("gridworld.tile_size"),
initialTile: {
x: this.controller.config.get("gridworld.initial_tile_x"),
y: this.controller.config.get("gridworld.initial_tile_y"),
},
mapExchangeError: this.mapExchangeError,
};
}
private async handleGridworldStateRequest(_request: messages.GridworldStateRequest) {
return this.getState();
}
private async handleGridworldCreateRequest(_request: messages.GridworldCreateRequest) {
await this.resetGridworld(true);
return this.getState();
}
private async handleGridworldDeleteRequest(_request: messages.GridworldDeleteRequest) {
await this.resetGridworld(false);
return this.getState();
}
private async resetGridworld(createInitial: boolean) {
if (this.pendingTiles.size) {
await Promise.allSettled([...this.pendingTiles.values()]);
this.pendingTiles.clear();
}
const tiles = [...this.tiles.values()];
await this.removeEdgesForTiles(tiles);
const deletedInstanceIds = new Set<number>();
for (const tile of tiles) {
await this.stopInstanceBeforeDelete(tile.instanceId, `gridworld tile ${tile.x},${tile.y}`);
try {
await this.controller.instanceDelete(tile.instanceId);
deletedInstanceIds.add(tile.instanceId);
} catch (err: any) {
this.logger.error(
`Failed deleting instance ${tile.instanceId} for tile ${tile.x},${tile.y}: ${err?.message ?? err}`,
);
}
}
const namePrefix = "gridworld_";
for (const instance of this.controller.instances.values()) {
if (deletedInstanceIds.has(instance.id)) {
continue;
}
const name = instance.config.get("instance.name");
if (!name.startsWith(namePrefix) && name !== "pathworld") {
continue;
}
await this.stopInstanceBeforeDelete(instance.id, name);
try {
await this.controller.instanceDelete(instance.id);
} catch (err: any) {
this.logger.error(
`Failed deleting instance ${instance.id} (${name}): ${err?.message ?? err}`,
);
}
}
this.tiles.clear();
this.tilesByInstance.clear();
this.storageDirty = true;
if (createInitial) {
const x = this.controller.config.get("gridworld.initial_tile_x");
const y = this.controller.config.get("gridworld.initial_tile_y");
await this.createPathworldInstance();
await this.ensureTile(x, y, "reset");
}
this.markStateDirty();
}
private async createPathworldInstance() {
const initialX = this.controller.config.get("gridworld.initial_tile_x");
const initialY = this.controller.config.get("gridworld.initial_tile_y");
const mapSettings = this.buildMapSettingsForTile(initialX, initialY);
if (!mapSettings) {
this.logger.error(`Cannot create pathworld instance: ${this.mapExchangeError ?? "map exchange string is not configured."}`);
return;
}
const hostId = this.getHostIdForTile();
if (hostId === undefined) {
this.logger.error("Cannot create pathworld instance: no hosts connected.");
return;
}
const instanceName = "pathworld";
const saveName = "pathworld.zip";
const instanceConfig = new lib.InstanceConfig("controller");
instanceConfig.set("instance.name", instanceName, "controller");
instanceConfig.set("instance.auto_start", true, "controller");
// instanceConfig.set("factorio.settings", { public: false, lan: false }, "controller");
await this.controller.instanceCreate(instanceConfig);
const instanceId = instanceConfig.get("instance.id");
try {
await this.controller.instanceAssign(instanceId, hostId);
} catch (err: any) {
this.logger.error(`Failed to assign pathworld instance ${instanceId}: ${err?.message ?? err}`);
try {
await this.controller.instanceDelete(instanceId);
} catch { /* ignore */ }
return;
}
const hasSave = [...this.controller.saves.values()].some(save =>
save.instanceId === instanceId && save.name === saveName && !save.isDeleted
);
if (!hasSave) {
try {
await this.controller.sendTo(
{ instanceId },
new lib.InstanceCreateSaveRequest(
saveName,
mapSettings.seed,
mapSettings.mapGenSettings,
mapSettings.mapSettings,
),
);
} catch (err: any) {
this.logger.error(`Failed creating save for pathworld: ${err?.message ?? err}`);
return;
}
}
try {
await this.controller.sendTo(
{ instanceId },
new lib.InstanceStartRequest(saveName),
);
this.logger.info(`Started pathworld instance ${instanceId}`);
} catch (err: any) {
this.logger.error(`Failed starting pathworld instance ${instanceId}: ${err?.message ?? err}`);
}
}
private async removeEdgesForTiles(tiles: TileRecord[]) {