screeps-clockwork 0.12.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -1,6 +1,8 @@
1
1
  import 'fastestsmallesttextencoderdecoder-encodeinto/EncoderDecoderTogether.min.js';
2
2
  import { ClockworkCostMatrix, DirectionOrder } from './wasm/screeps_clockwork';
3
+ import { type PortalPair } from './utils/packedArrays';
3
4
  export { ClockworkCostMatrix, DirectionOrder };
5
+ export type { PortalPair };
4
6
  export * from './wrappers/astarDistanceMap';
5
7
  export * from './wrappers/bfsDistanceMap';
6
8
  export * from './wrappers/dijkstraDistanceMap';
@@ -13,12 +15,47 @@ export * from './wrappers/multiroomMonoFlowField';
13
15
  export * from './wrappers/path';
14
16
  export type { ClockworkDistanceMap } from './wrappers/distanceMap';
15
17
  export type { ClockworkMultiroomDistanceMap } from './wrappers/multiroomDistanceMap';
18
+ export interface ClockworkInitializeOptions {
19
+ verbose?: boolean;
20
+ portals?: readonly PortalPair[];
21
+ portalDistanceCacheRoomLimit?: number;
22
+ }
23
+ export interface PortalIndexDebugInfo {
24
+ cachedDistanceMaps: number;
25
+ maxCachedDistanceMaps: number;
26
+ cachedPortals: number;
27
+ portalRoomSummaries: number;
28
+ totalSizeBytes: number;
29
+ totalSize: string;
30
+ }
16
31
  /**
17
- * The `initialize` function should be called in your main loop before
18
- * using any other screeps-clockwork functions. Depending on available
19
- * CPU, it may not load the WASM module completely in the first tick,
20
- * but it will pick back up where it left off if the script times out.
32
+ * The `initialize` function initializes the WASM module and applies global
33
+ * setup options. Call it in your main loop before using any other
34
+ * screeps-clockwork functions. Depending on available CPU, it may not load the
35
+ * WASM module completely in the first tick, but it will pick back up where it
36
+ * left off if the script times out.
21
37
  *
22
- * @param verbose - If true, will log the state of the WASM module as it loads.
38
+ * It is safe to call this function repeatedly, but setup options such as
39
+ * portals and portal cache sizing are applied only once, after WASM
40
+ * initialization has completed. Use `setPortals` when you intentionally need
41
+ * to reconfigure portals later.
42
+ *
43
+ * @param options - Pass `true` or `{ verbose: true }` to log WASM loading;
44
+ * provide portals and cache sizing to apply them once after WASM initialization.
45
+ */
46
+ export declare function initialize(options?: boolean | ClockworkInitializeOptions): void;
47
+ /**
48
+ * Configure the portal pairs used by portal-aware pathfinding functions.
49
+ * Each pair is installed bidirectionally.
50
+ */
51
+ export declare function setPortals(portals: readonly PortalPair[]): void;
52
+ /**
53
+ * Remove all configured portal pairs while preserving the initialize-time cache limit.
54
+ */
55
+ export declare function clearPortals(): void;
56
+ /**
57
+ * Return debug counters for the configured portal index and its nearest-portal cache.
58
+ * `totalSize` is an approximate in-memory size formatted for logs; `totalSizeBytes`
59
+ * preserves the raw byte estimate.
23
60
  */
24
- export declare function initialize(verbose?: boolean): void;
61
+ export declare function debugPortalIndex(): PortalIndexDebugInfo;
@@ -1,6 +1,9 @@
1
- export interface PackedDestination {
1
+ interface PackedDestination {
2
2
  pos: RoomPosition;
3
3
  range: number;
4
4
  }
5
+ export type PortalPair = readonly [RoomPosition, RoomPosition];
5
6
  export declare function packPositions(positions: RoomPosition[]): Uint32Array;
6
7
  export declare function packDestinations(destinations: PackedDestination[] | undefined): Uint32Array | undefined;
8
+ export declare function packPortalPairs(portals: readonly PortalPair[]): Uint32Array;
9
+ export {};
@@ -1,3 +1,8 @@
1
+ export function clear_portals(): void;
2
+ /**
3
+ * @returns {any}
4
+ */
5
+ export function debug_portal_index(): any;
1
6
  /**
2
7
  * Exports the global range calculation between two positions.
3
8
  * @param {number} packed_pos_1
@@ -24,6 +29,17 @@ export function get_terrain_cost_matrix(room_name: number, plain_cost?: number |
24
29
  * @returns {SearchResult}
25
30
  */
26
31
  export function js_astar_multiroom_distance_map(start_packed: Uint32Array, get_cost_matrix: Function, max_rooms: number, max_ops: number, max_path_cost: number, any_of_destinations?: Uint32Array | null, all_of_destinations?: Uint32Array | null): SearchResult;
32
+ /**
33
+ * @param {Uint32Array} start_packed
34
+ * @param {Function} get_cost_matrix
35
+ * @param {number} max_rooms
36
+ * @param {number} max_ops
37
+ * @param {number} max_path_cost
38
+ * @param {Uint32Array | null} [any_of_destinations]
39
+ * @param {Uint32Array | null} [all_of_destinations]
40
+ * @returns {SearchResult}
41
+ */
42
+ export function js_astar_portal_multiroom_distance_map(start_packed: Uint32Array, get_cost_matrix: Function, max_rooms: number, max_ops: number, max_path_cost: number, any_of_destinations?: Uint32Array | null, all_of_destinations?: Uint32Array | null): SearchResult;
27
43
  /**
28
44
  * WASM wrapper for the BFS multiroom distance map function.
29
45
  *
@@ -49,6 +65,17 @@ export function js_astar_multiroom_distance_map(start_packed: Uint32Array, get_c
49
65
  * @returns {SearchResult}
50
66
  */
51
67
  export function js_bfs_multiroom_distance_map(start_packed: Uint32Array, get_cost_matrix: Function, max_ops: number, max_rooms: number, max_path_cost: number, any_of_destinations?: Uint32Array | null, all_of_destinations?: Uint32Array | null): SearchResult;
68
+ /**
69
+ * @param {Uint32Array} start_packed
70
+ * @param {Function} get_cost_matrix
71
+ * @param {number} max_ops
72
+ * @param {number} max_rooms
73
+ * @param {number} max_path_cost
74
+ * @param {Uint32Array | null} [any_of_destinations]
75
+ * @param {Uint32Array | null} [all_of_destinations]
76
+ * @returns {SearchResult}
77
+ */
78
+ export function js_bfs_portal_multiroom_distance_map(start_packed: Uint32Array, get_cost_matrix: Function, max_ops: number, max_rooms: number, max_path_cost: number, any_of_destinations?: Uint32Array | null, all_of_destinations?: Uint32Array | null): SearchResult;
52
79
  /**
53
80
  * @param {Uint32Array} start_packed
54
81
  * @param {Function} get_cost_matrix
@@ -60,6 +87,17 @@ export function js_bfs_multiroom_distance_map(start_packed: Uint32Array, get_cos
60
87
  * @returns {SearchResult}
61
88
  */
62
89
  export function js_dijkstra_multiroom_distance_map(start_packed: Uint32Array, get_cost_matrix: Function, max_ops: number, max_rooms: number, max_path_cost: number, any_of_destinations?: Uint32Array | null, all_of_destinations?: Uint32Array | null): SearchResult;
90
+ /**
91
+ * @param {Uint32Array} start_packed
92
+ * @param {Function} get_cost_matrix
93
+ * @param {number} max_ops
94
+ * @param {number} max_rooms
95
+ * @param {number} max_path_cost
96
+ * @param {Uint32Array | null} [any_of_destinations]
97
+ * @param {Uint32Array | null} [all_of_destinations]
98
+ * @returns {SearchResult}
99
+ */
100
+ export function js_dijkstra_portal_multiroom_distance_map(start_packed: Uint32Array, get_cost_matrix: Function, max_ops: number, max_rooms: number, max_path_cost: number, any_of_destinations?: Uint32Array | null, all_of_destinations?: Uint32Array | null): SearchResult;
63
101
  /**
64
102
  * @param {number} start
65
103
  * @param {MultiroomDistanceMap} distance_map
@@ -67,18 +105,37 @@ export function js_dijkstra_multiroom_distance_map(start_packed: Uint32Array, ge
67
105
  * @returns {Path}
68
106
  */
69
107
  export function js_path_to_multiroom_distance_map_origin(start: number, distance_map: MultiroomDistanceMap, direction_order: DirectionOrder): Path;
108
+ /**
109
+ * @param {number} start
110
+ * @param {MultiroomDistanceMap} distance_map
111
+ * @param {DirectionOrder} direction_order
112
+ * @returns {Path}
113
+ */
114
+ export function js_path_to_multiroom_distance_map_origin_with_portals(start: number, distance_map: MultiroomDistanceMap, direction_order: DirectionOrder): Path;
70
115
  /**
71
116
  * @param {number} start
72
117
  * @param {MultiroomFlowField} flow_field
73
118
  * @returns {Path}
74
119
  */
75
120
  export function js_path_to_multiroom_flow_field_origin(start: number, flow_field: MultiroomFlowField): Path;
121
+ /**
122
+ * @param {number} start
123
+ * @param {MultiroomFlowField} flow_field
124
+ * @returns {Path}
125
+ */
126
+ export function js_path_to_multiroom_flow_field_origin_with_portals(start: number, flow_field: MultiroomFlowField): Path;
76
127
  /**
77
128
  * @param {number} start
78
129
  * @param {MultiroomMonoFlowField} flow_field
79
130
  * @returns {Path}
80
131
  */
81
132
  export function js_path_to_multiroom_mono_flow_field_origin(start: number, flow_field: MultiroomMonoFlowField): Path;
133
+ /**
134
+ * @param {number} start
135
+ * @param {MultiroomMonoFlowField} flow_field
136
+ * @returns {Path}
137
+ */
138
+ export function js_path_to_multiroom_mono_flow_field_origin_with_portals(start: number, flow_field: MultiroomMonoFlowField): Path;
82
139
  /**
83
140
  * Creates a flow field for the given distance map.
84
141
  * @param {MultiroomDistanceMap} distance_map
@@ -93,6 +150,26 @@ export function multiroomFlowField(distance_map: MultiroomDistanceMap, direction
93
150
  * @returns {MultiroomMonoFlowField}
94
151
  */
95
152
  export function multiroomMonoFlowField(distance_map: MultiroomDistanceMap, direction_order: DirectionOrder): MultiroomMonoFlowField;
153
+ /**
154
+ * @param {MultiroomDistanceMap} distance_map
155
+ * @param {DirectionOrder} direction_order
156
+ * @returns {MultiroomFlowField}
157
+ */
158
+ export function multiroomPortalFlowField(distance_map: MultiroomDistanceMap, direction_order: DirectionOrder): MultiroomFlowField;
159
+ /**
160
+ * @param {MultiroomDistanceMap} distance_map
161
+ * @param {DirectionOrder} direction_order
162
+ * @returns {MultiroomMonoFlowField}
163
+ */
164
+ export function multiroomPortalMonoFlowField(distance_map: MultiroomDistanceMap, direction_order: DirectionOrder): MultiroomMonoFlowField;
165
+ /**
166
+ * @param {number} room_limit
167
+ */
168
+ export function set_portal_distance_cache_room_limit(room_limit: number): void;
169
+ /**
170
+ * @param {Uint32Array} packed_pairs
171
+ */
172
+ export function set_portals(packed_pairs: Uint32Array): void;
96
173
  /**
97
174
  * @returns {string}
98
175
  */
@@ -33,3 +33,39 @@ export declare function astarMultiroomDistanceMap(start: RoomPosition[], { costM
33
33
  foundTargets: RoomPosition[];
34
34
  ops: number;
35
35
  };
36
+ /**
37
+ * Create a portal-aware distance map for the given start positions, using A* to
38
+ * optimize the search toward the given destinations.
39
+ *
40
+ * Portals configured with `initialize({ portals })` or `setPortals` are treated
41
+ * like exits: stepping onto a portal entrance lands on its paired exit.
42
+ *
43
+ * This calculates a distance map across multiple rooms, with a few configurable limits:
44
+ * - `maxOps`: The maximum number of pathfinding operations to perform.
45
+ * - `maxRooms`: The maximum number of rooms to explore.
46
+ * - `maxPathCost`: Don't explore tiles with a greater path cost than this.
47
+ *
48
+ * At least one of these limits or destination lists must be set.
49
+ *
50
+ * @param start - The starting positions.
51
+ * @param options - The options for the distance map.
52
+ * @returns A multi-room distance map.
53
+ */
54
+ export declare function astarPortalMultiroomDistanceMap(start: RoomPosition[], { costMatrixCallback, maxRooms, maxOps, maxPathCost, anyOfDestinations, allOfDestinations }: {
55
+ costMatrixCallback: (room: string) => ClockworkCostMatrix | undefined;
56
+ maxRooms?: number;
57
+ maxOps?: number;
58
+ maxPathCost?: number;
59
+ anyOfDestinations?: {
60
+ pos: RoomPosition;
61
+ range: number;
62
+ }[];
63
+ allOfDestinations?: {
64
+ pos: RoomPosition;
65
+ range: number;
66
+ }[];
67
+ }): {
68
+ distanceMap: import("./multiroomDistanceMap").ClockworkMultiroomDistanceMap;
69
+ foundTargets: RoomPosition[];
70
+ ops: number;
71
+ };
@@ -33,3 +33,40 @@ export declare function bfsMultiroomDistanceMap(start: RoomPosition[], { costMat
33
33
  foundTargets: RoomPosition[];
34
34
  ops: number;
35
35
  };
36
+ /**
37
+ * Create a portal-aware distance map for the given start positions, using a breadth-first search.
38
+ * This does not factor in terrain costs (treating anything less than 255 in the cost
39
+ * matrix as passable), so it's faster but less useful than Dijkstra's algorithm.
40
+ *
41
+ * Portals configured with `initialize({ portals })` or `setPortals` are treated
42
+ * like exits: stepping onto a portal entrance lands on its paired exit.
43
+ *
44
+ * This calculates a distance map across multiple rooms, with a few configurable limits:
45
+ * - `maxOps`: The maximum number of pathfinding operations to perform.
46
+ * - `maxRooms`: The maximum number of rooms to explore.
47
+ * - `maxPathCost`: Don't explore tiles with a greater path cost than this.
48
+ *
49
+ * At least one of these limits or destination lists must be set.
50
+ *
51
+ * @param start - The starting positions.
52
+ * @param options - The options for the distance map.
53
+ * @returns A multi-room distance map.
54
+ */
55
+ export declare function bfsPortalMultiroomDistanceMap(start: RoomPosition[], { costMatrixCallback, maxOps, maxRooms, maxPathCost, anyOfDestinations, allOfDestinations }: {
56
+ costMatrixCallback: (room: string) => ClockworkCostMatrix | undefined;
57
+ maxOps?: number;
58
+ maxRooms?: number;
59
+ maxPathCost?: number;
60
+ anyOfDestinations?: {
61
+ pos: RoomPosition;
62
+ range: number;
63
+ }[];
64
+ allOfDestinations?: {
65
+ pos: RoomPosition;
66
+ range: number;
67
+ }[];
68
+ }): {
69
+ distanceMap: import("./multiroomDistanceMap").ClockworkMultiroomDistanceMap;
70
+ foundTargets: RoomPosition[];
71
+ ops: number;
72
+ };
@@ -32,3 +32,39 @@ export declare function dijkstraMultiroomDistanceMap(start: RoomPosition[], { co
32
32
  foundTargets: RoomPosition[];
33
33
  ops: number;
34
34
  };
35
+ /**
36
+ * Create a portal-aware distance map for the given start positions, using Dijkstra's
37
+ * algorithm to factor in terrain costs (0-255, where 255 is impassable).
38
+ *
39
+ * Portals configured with `initialize({ portals })` or `setPortals` are treated
40
+ * like exits: stepping onto a portal entrance lands on its paired exit.
41
+ *
42
+ * This calculates a distance map across multiple rooms, with a few configurable limits:
43
+ * - `maxOps`: The maximum number of pathfinding operations to perform.
44
+ * - `maxRooms`: The maximum number of rooms to explore.
45
+ * - `maxPathCost`: Don't explore tiles with a greater path cost than this.
46
+ *
47
+ * At least one of these limits or destination lists must be set.
48
+ *
49
+ * @param start - The starting positions.
50
+ * @param options - The options for the distance map.
51
+ * @returns A multi-room distance map.
52
+ */
53
+ export declare function dijkstraPortalMultiroomDistanceMap(start: RoomPosition[], { costMatrixCallback, maxOps, maxRooms, maxPathCost, anyOfDestinations, allOfDestinations }: {
54
+ costMatrixCallback: (room: string) => ClockworkCostMatrix | undefined;
55
+ maxOps?: number;
56
+ maxRooms?: number;
57
+ maxPathCost?: number;
58
+ anyOfDestinations?: {
59
+ pos: RoomPosition;
60
+ range: number;
61
+ }[];
62
+ allOfDestinations?: {
63
+ pos: RoomPosition;
64
+ range: number;
65
+ }[];
66
+ }): {
67
+ distanceMap: import("./multiroomDistanceMap").ClockworkMultiroomDistanceMap;
68
+ foundTargets: RoomPosition[];
69
+ ops: number;
70
+ };
@@ -38,14 +38,29 @@ export declare class ClockworkMultiroomDistanceMap {
38
38
  * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal steps when multiple neighbors are equally close.
39
39
  */
40
40
  pathToOrigin(start: RoomPosition, options?: DirectionOrderOptions): ClockworkPath;
41
+ /**
42
+ * Portal-aware path to the origin from a given position.
43
+ * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal steps when multiple neighbors are equally close.
44
+ */
45
+ pathToOriginWithPortals(start: RoomPosition, options?: DirectionOrderOptions): ClockworkPath;
41
46
  /**
42
47
  * Flow field for this distance map.
43
48
  * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal directions when multiple neighbors are equally close.
44
49
  */
45
50
  toFlowField(options?: DirectionOrderOptions): ClockworkMultiroomFlowField;
51
+ /**
52
+ * Portal-aware flow field for this distance map.
53
+ * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal directions when multiple neighbors are equally close.
54
+ */
55
+ toFlowFieldWithPortals(options?: DirectionOrderOptions): ClockworkMultiroomFlowField;
46
56
  /**
47
57
  * Mono-directional flow field for this distance map.
48
58
  * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal directions when multiple neighbors are equally close.
49
59
  */
50
60
  toMonoFlowField(options?: DirectionOrderOptions): ClockworkMultiroomMonoFlowField;
61
+ /**
62
+ * Portal-aware monodirectional flow field for this distance map.
63
+ * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal directions when multiple neighbors are equally close.
64
+ */
65
+ toMonoFlowFieldWithPortals(options?: DirectionOrderOptions): ClockworkMultiroomMonoFlowField;
51
66
  }
@@ -43,4 +43,8 @@ export declare class ClockworkMultiroomFlowField {
43
43
  * Find a path from a given position to the origin of the flow field.
44
44
  */
45
45
  pathToOrigin(start: RoomPosition): ClockworkPath;
46
+ /**
47
+ * Find a portal-aware path from a given position to the origin of the flow field.
48
+ */
49
+ pathToOriginWithPortals(start: RoomPosition): ClockworkPath;
46
50
  }
@@ -31,4 +31,8 @@ export declare class ClockworkMultiroomMonoFlowField {
31
31
  * Find a path from a given position to the origin of the flow field.
32
32
  */
33
33
  pathToOrigin(start: RoomPosition): ClockworkPath;
34
+ /**
35
+ * Find a portal-aware path from a given position to the origin of the flow field.
36
+ */
37
+ pathToOriginWithPortals(start: RoomPosition): ClockworkPath;
34
38
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screeps-clockwork",
3
- "version": "0.12.0",
3
+ "version": "0.13.1",
4
4
  "description": "A WASM movement library for Screeps",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -21,15 +21,15 @@
21
21
  "typecheck:ts:test": "tsc -p tsconfig.test.json --noEmit",
22
22
  "typecheck:rust": "cargo check --target wasm32-unknown-unknown --all-features",
23
23
  "check:wasm-types": "run-s check:wasm-types:present check:wasm-types:src check:wasm-types:bench",
24
- "check:wasm-types:present": "test -f src/wasm/screeps_clockwork.d.ts && test -f src/wasm/screeps_clockwork_bg.wasm.d.ts && test -f benches/wasm/pkg/screeps_clockwork.d.ts && test -f benches/wasm/pkg/screeps_clockwork_bg.wasm.d.ts",
25
- "check:wasm-types:src": "npm run build:lib && git diff --exit-code -- src/wasm/screeps_clockwork.d.ts src/wasm/screeps_clockwork_bg.wasm.d.ts",
26
- "check:wasm-types:bench": "npm run bench:wasm:build && git diff --exit-code -- benches/wasm/pkg/screeps_clockwork.d.ts benches/wasm/pkg/screeps_clockwork_bg.wasm.d.ts",
24
+ "check:wasm-types:present": "test -f src/wasm/screeps_clockwork.d.ts && test -f benches/wasm/pkg/screeps_clockwork.d.ts",
25
+ "check:wasm-types:src": "npm run build:lib && git diff --exit-code -- src/wasm/screeps_clockwork.d.ts",
26
+ "check:wasm-types:bench": "npm run bench:wasm:build && git diff --exit-code -- benches/wasm/pkg/screeps_clockwork.d.ts",
27
27
  "build": "run-s build:lib build:src",
28
- "build:lib": "wasm-pack build --target web --out-dir src/wasm --config build.rustflags=[\\'-Ctarget-feature=-reference-types\\',\\'-Ctarget-feature=-multivalue\\',\\'-Ctarget-feature=-sign-ext\\'] -Z build-std=std,panic_abort && printf '*\\n!*.d.ts\\n' > src/wasm/.gitignore",
28
+ "build:lib": "wasm-pack build --target web --out-dir src/wasm --config build.rustflags=[\\'-Ctarget-feature=-reference-types\\',\\'-Ctarget-feature=-multivalue\\',\\'-Ctarget-feature=-sign-ext\\'] -Z build-std=std,panic_abort && printf '*\\n!screeps_clockwork.d.ts\\n' > src/wasm/.gitignore",
29
29
  "build:src": "rollup -c",
30
30
  "build:docs": "typedoc",
31
31
  "bench:rust": "cargo bench --features bench --bench distance_map",
32
- "bench:wasm:build": "wasm-pack build --target nodejs --out-dir benches/wasm/pkg --config \"build.rustflags=['-Ctarget-feature=-reference-types','-Ctarget-feature=-multivalue','-Ctarget-feature=-sign-ext']\" -Z build-std=std,panic_abort && printf '*\\n!*.d.ts\\n' > benches/wasm/pkg/.gitignore && cp benches/wasm/pkg/screeps_clockwork.js benches/wasm/pkg/screeps_clockwork.cjs",
32
+ "bench:wasm:build": "wasm-pack build --target nodejs --out-dir benches/wasm/pkg --config \"build.rustflags=['-Ctarget-feature=-reference-types','-Ctarget-feature=-multivalue','-Ctarget-feature=-sign-ext']\" -Z build-std=std,panic_abort && printf '*\\n!screeps_clockwork.d.ts\\n' > benches/wasm/pkg/.gitignore && cp benches/wasm/pkg/screeps_clockwork.js benches/wasm/pkg/screeps_clockwork.cjs",
33
33
  "bench:wasm:run": "node --no-liftoff --no-wasm-lazy-compilation --wasm-wrapper-tiering-budget=0 ./node_modules/vitest/vitest.mjs bench benches/wasm",
34
34
  "bench:wasm": "run-s bench:wasm:build bench:wasm:run",
35
35
  "bench:wasm:save": "run-s bench:wasm:build bench:wasm:save:run",