screeps-clockwork 0.11.0 → 0.13.0

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.
package/dist/index.js CHANGED
@@ -852,6 +852,16 @@ Object.freeze({
852
852
  Wall: 1, "1": "Wall",
853
853
  Swamp: 2, "2": "Swamp",
854
854
  });
855
+ function clear_portals() {
856
+ wasm.clear_portals();
857
+ }
858
+ /**
859
+ * @returns {any}
860
+ */
861
+ function debug_portal_index() {
862
+ const ret = wasm.debug_portal_index();
863
+ return takeObject(ret);
864
+ }
855
865
  /**
856
866
  * Exports the global range calculation between two positions.
857
867
  * @param {number} packed_pos_1
@@ -898,6 +908,31 @@ function js_astar_multiroom_distance_map(start_packed, get_cost_matrix, max_room
898
908
  heap[stack_pointer++] = undefined;
899
909
  }
900
910
  }
911
+ /**
912
+ * @param {Uint32Array} start_packed
913
+ * @param {Function} get_cost_matrix
914
+ * @param {number} max_rooms
915
+ * @param {number} max_ops
916
+ * @param {number} max_path_cost
917
+ * @param {Uint32Array | null} [any_of_destinations]
918
+ * @param {Uint32Array | null} [all_of_destinations]
919
+ * @returns {SearchResult}
920
+ */
921
+ function js_astar_portal_multiroom_distance_map(start_packed, get_cost_matrix, max_rooms, max_ops, max_path_cost, any_of_destinations, all_of_destinations) {
922
+ try {
923
+ const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
924
+ const len0 = WASM_VECTOR_LEN;
925
+ var ptr1 = isLikeNone(any_of_destinations) ? 0 : passArray32ToWasm0(any_of_destinations, wasm.__wbindgen_malloc);
926
+ var len1 = WASM_VECTOR_LEN;
927
+ var ptr2 = isLikeNone(all_of_destinations) ? 0 : passArray32ToWasm0(all_of_destinations, wasm.__wbindgen_malloc);
928
+ var len2 = WASM_VECTOR_LEN;
929
+ const ret = wasm.js_astar_portal_multiroom_distance_map(ptr0, len0, addBorrowedObject(get_cost_matrix), max_rooms, max_ops, max_path_cost, ptr1, len1, ptr2, len2);
930
+ return SearchResult.__wrap(ret);
931
+ }
932
+ finally {
933
+ heap[stack_pointer++] = undefined;
934
+ }
935
+ }
901
936
  /**
902
937
  * WASM wrapper for the BFS multiroom distance map function.
903
938
  *
@@ -937,6 +972,31 @@ function js_bfs_multiroom_distance_map(start_packed, get_cost_matrix, max_ops, m
937
972
  heap[stack_pointer++] = undefined;
938
973
  }
939
974
  }
975
+ /**
976
+ * @param {Uint32Array} start_packed
977
+ * @param {Function} get_cost_matrix
978
+ * @param {number} max_ops
979
+ * @param {number} max_rooms
980
+ * @param {number} max_path_cost
981
+ * @param {Uint32Array | null} [any_of_destinations]
982
+ * @param {Uint32Array | null} [all_of_destinations]
983
+ * @returns {SearchResult}
984
+ */
985
+ function js_bfs_portal_multiroom_distance_map(start_packed, get_cost_matrix, max_ops, max_rooms, max_path_cost, any_of_destinations, all_of_destinations) {
986
+ try {
987
+ const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
988
+ const len0 = WASM_VECTOR_LEN;
989
+ var ptr1 = isLikeNone(any_of_destinations) ? 0 : passArray32ToWasm0(any_of_destinations, wasm.__wbindgen_malloc);
990
+ var len1 = WASM_VECTOR_LEN;
991
+ var ptr2 = isLikeNone(all_of_destinations) ? 0 : passArray32ToWasm0(all_of_destinations, wasm.__wbindgen_malloc);
992
+ var len2 = WASM_VECTOR_LEN;
993
+ const ret = wasm.js_bfs_portal_multiroom_distance_map(ptr0, len0, addBorrowedObject(get_cost_matrix), max_ops, max_rooms, max_path_cost, ptr1, len1, ptr2, len2);
994
+ return SearchResult.__wrap(ret);
995
+ }
996
+ finally {
997
+ heap[stack_pointer++] = undefined;
998
+ }
999
+ }
940
1000
  /**
941
1001
  * @param {Uint32Array} start_packed
942
1002
  * @param {Function} get_cost_matrix
@@ -962,6 +1022,31 @@ function js_dijkstra_multiroom_distance_map(start_packed, get_cost_matrix, max_o
962
1022
  heap[stack_pointer++] = undefined;
963
1023
  }
964
1024
  }
1025
+ /**
1026
+ * @param {Uint32Array} start_packed
1027
+ * @param {Function} get_cost_matrix
1028
+ * @param {number} max_ops
1029
+ * @param {number} max_rooms
1030
+ * @param {number} max_path_cost
1031
+ * @param {Uint32Array | null} [any_of_destinations]
1032
+ * @param {Uint32Array | null} [all_of_destinations]
1033
+ * @returns {SearchResult}
1034
+ */
1035
+ function js_dijkstra_portal_multiroom_distance_map(start_packed, get_cost_matrix, max_ops, max_rooms, max_path_cost, any_of_destinations, all_of_destinations) {
1036
+ try {
1037
+ const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
1038
+ const len0 = WASM_VECTOR_LEN;
1039
+ var ptr1 = isLikeNone(any_of_destinations) ? 0 : passArray32ToWasm0(any_of_destinations, wasm.__wbindgen_malloc);
1040
+ var len1 = WASM_VECTOR_LEN;
1041
+ var ptr2 = isLikeNone(all_of_destinations) ? 0 : passArray32ToWasm0(all_of_destinations, wasm.__wbindgen_malloc);
1042
+ var len2 = WASM_VECTOR_LEN;
1043
+ const ret = wasm.js_dijkstra_portal_multiroom_distance_map(ptr0, len0, addBorrowedObject(get_cost_matrix), max_ops, max_rooms, max_path_cost, ptr1, len1, ptr2, len2);
1044
+ return SearchResult.__wrap(ret);
1045
+ }
1046
+ finally {
1047
+ heap[stack_pointer++] = undefined;
1048
+ }
1049
+ }
965
1050
  /**
966
1051
  * @param {number} start
967
1052
  * @param {MultiroomDistanceMap} distance_map
@@ -985,6 +1070,29 @@ function js_path_to_multiroom_distance_map_origin(start, distance_map, direction
985
1070
  wasm.__wbindgen_add_to_stack_pointer(16);
986
1071
  }
987
1072
  }
1073
+ /**
1074
+ * @param {number} start
1075
+ * @param {MultiroomDistanceMap} distance_map
1076
+ * @param {DirectionOrder} direction_order
1077
+ * @returns {Path}
1078
+ */
1079
+ function js_path_to_multiroom_distance_map_origin_with_portals(start, distance_map, direction_order) {
1080
+ try {
1081
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1082
+ _assertClass(distance_map, MultiroomDistanceMap);
1083
+ wasm.js_path_to_multiroom_distance_map_origin_with_portals(retptr, start, distance_map.__wbg_ptr, direction_order);
1084
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1085
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1086
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
1087
+ if (r2) {
1088
+ throw takeObject(r1);
1089
+ }
1090
+ return Path.__wrap(r0);
1091
+ }
1092
+ finally {
1093
+ wasm.__wbindgen_add_to_stack_pointer(16);
1094
+ }
1095
+ }
988
1096
  /**
989
1097
  * @param {number} start
990
1098
  * @param {MultiroomFlowField} flow_field
@@ -1007,6 +1115,28 @@ function js_path_to_multiroom_flow_field_origin(start, flow_field) {
1007
1115
  wasm.__wbindgen_add_to_stack_pointer(16);
1008
1116
  }
1009
1117
  }
1118
+ /**
1119
+ * @param {number} start
1120
+ * @param {MultiroomFlowField} flow_field
1121
+ * @returns {Path}
1122
+ */
1123
+ function js_path_to_multiroom_flow_field_origin_with_portals(start, flow_field) {
1124
+ try {
1125
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1126
+ _assertClass(flow_field, MultiroomFlowField);
1127
+ wasm.js_path_to_multiroom_flow_field_origin_with_portals(retptr, start, flow_field.__wbg_ptr);
1128
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1129
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1130
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
1131
+ if (r2) {
1132
+ throw takeObject(r1);
1133
+ }
1134
+ return Path.__wrap(r0);
1135
+ }
1136
+ finally {
1137
+ wasm.__wbindgen_add_to_stack_pointer(16);
1138
+ }
1139
+ }
1010
1140
  /**
1011
1141
  * @param {number} start
1012
1142
  * @param {MultiroomMonoFlowField} flow_field
@@ -1029,6 +1159,28 @@ function js_path_to_multiroom_mono_flow_field_origin(start, flow_field) {
1029
1159
  wasm.__wbindgen_add_to_stack_pointer(16);
1030
1160
  }
1031
1161
  }
1162
+ /**
1163
+ * @param {number} start
1164
+ * @param {MultiroomMonoFlowField} flow_field
1165
+ * @returns {Path}
1166
+ */
1167
+ function js_path_to_multiroom_mono_flow_field_origin_with_portals(start, flow_field) {
1168
+ try {
1169
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1170
+ _assertClass(flow_field, MultiroomMonoFlowField);
1171
+ wasm.js_path_to_multiroom_mono_flow_field_origin_with_portals(retptr, start, flow_field.__wbg_ptr);
1172
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1173
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1174
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
1175
+ if (r2) {
1176
+ throw takeObject(r1);
1177
+ }
1178
+ return Path.__wrap(r0);
1179
+ }
1180
+ finally {
1181
+ wasm.__wbindgen_add_to_stack_pointer(16);
1182
+ }
1183
+ }
1032
1184
  /**
1033
1185
  * Creates a flow field for the given distance map.
1034
1186
  * @param {MultiroomDistanceMap} distance_map
@@ -1051,6 +1203,40 @@ function multiroomMonoFlowField(distance_map, direction_order) {
1051
1203
  const ret = wasm.multiroomMonoFlowField(distance_map.__wbg_ptr, direction_order);
1052
1204
  return MultiroomMonoFlowField.__wrap(ret);
1053
1205
  }
1206
+ /**
1207
+ * @param {MultiroomDistanceMap} distance_map
1208
+ * @param {DirectionOrder} direction_order
1209
+ * @returns {MultiroomFlowField}
1210
+ */
1211
+ function multiroomPortalFlowField(distance_map, direction_order) {
1212
+ _assertClass(distance_map, MultiroomDistanceMap);
1213
+ const ret = wasm.multiroomPortalFlowField(distance_map.__wbg_ptr, direction_order);
1214
+ return MultiroomFlowField.__wrap(ret);
1215
+ }
1216
+ /**
1217
+ * @param {MultiroomDistanceMap} distance_map
1218
+ * @param {DirectionOrder} direction_order
1219
+ * @returns {MultiroomMonoFlowField}
1220
+ */
1221
+ function multiroomPortalMonoFlowField(distance_map, direction_order) {
1222
+ _assertClass(distance_map, MultiroomDistanceMap);
1223
+ const ret = wasm.multiroomPortalMonoFlowField(distance_map.__wbg_ptr, direction_order);
1224
+ return MultiroomMonoFlowField.__wrap(ret);
1225
+ }
1226
+ /**
1227
+ * @param {number} room_limit
1228
+ */
1229
+ function set_portal_distance_cache_room_limit(room_limit) {
1230
+ wasm.set_portal_distance_cache_room_limit(room_limit);
1231
+ }
1232
+ /**
1233
+ * @param {Uint32Array} packed_pairs
1234
+ */
1235
+ function set_portals(packed_pairs) {
1236
+ const ptr0 = passArray32ToWasm0(packed_pairs, wasm.__wbindgen_malloc);
1237
+ const len0 = WASM_VECTOR_LEN;
1238
+ wasm.set_portals(ptr0, len0);
1239
+ }
1054
1240
  /**
1055
1241
  * @returns {string}
1056
1242
  */
@@ -1074,9 +1260,12 @@ function version() {
1074
1260
  function __wbg_get_imports() {
1075
1261
  const import0 = {
1076
1262
  __proto__: null,
1077
- __wbg___wbindgen_is_null_2042690d351e14f0: function (arg0) {
1078
- const ret = getObject(arg0) === null;
1079
- return ret;
1263
+ __wbg___wbindgen_debug_string_edece8177ad01481: function (arg0, arg1) {
1264
+ const ret = debugString(getObject(arg1));
1265
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1266
+ const len1 = WASM_VECTOR_LEN;
1267
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
1268
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
1080
1269
  },
1081
1270
  __wbg___wbindgen_is_undefined_35bb9f4c7fd651d5: function (arg0) {
1082
1271
  const ret = getObject(arg0) === undefined;
@@ -1100,11 +1289,7 @@ function __wbg_get_imports() {
1100
1289
  return addHeapObject(ret);
1101
1290
  }, arguments);
1102
1291
  },
1103
- __wbg_ceil_2f02237b685bdce4: function (arg0) {
1104
- const ret = Math.ceil(arg0);
1105
- return ret;
1106
- },
1107
- __wbg_clockworkcostmatrix_get_pointer_c0709a574ef183e2: function (arg0) {
1292
+ __wbg_clockworkcostmatrix_get_pointer_20f0153b006cac1d: function (arg0) {
1108
1293
  const ret = clockworkcostmatrix_get_pointer(takeObject(arg0));
1109
1294
  return ret;
1110
1295
  },
@@ -1128,37 +1313,10 @@ function __wbg_get_imports() {
1128
1313
  const ret = getObject(arg0).getRawBuffer(getObject(arg1));
1129
1314
  return addHeapObject(ret);
1130
1315
  },
1131
- __wbg_getRoomTerrain_ff542df2ea2015b1: function () {
1132
- return handleError(function (arg0) {
1133
- const ret = Game.map.getRoomTerrain(getObject(arg0));
1134
- return addHeapObject(ret);
1135
- }, arguments);
1136
- },
1137
- __wbg_get_8f8e5734959cd5fc: function (arg0, arg1, arg2) {
1138
- const ret = getObject(arg0).get(arg1, arg2);
1139
- return ret;
1140
- },
1141
- __wbg_get_unchecked_1dfe6d05ad91d9b7: function (arg0, arg1) {
1142
- const ret = getObject(arg0)[arg1 >>> 0];
1143
- return addHeapObject(ret);
1144
- },
1145
- __wbg_get_value_3083233b2ad9f36e: function (arg0, arg1) {
1146
- const ret = getObject(arg0)[getObject(arg1)];
1316
+ __wbg_new_02d162bc6cf02f60: function () {
1317
+ const ret = new Object();
1147
1318
  return addHeapObject(ret);
1148
1319
  },
1149
- __wbg_length_2591a0f4f659a55c: function (arg0) {
1150
- const ret = getObject(arg0).length;
1151
- return ret;
1152
- },
1153
- __wbg_log_525543f2526b398f: function (arg0, arg1) {
1154
- console.log(getStringFromWasm0(arg0, arg1));
1155
- },
1156
- __wbg_lookFor_0ddc4d9f22e9bfe5: function () {
1157
- return handleError(function (arg0, arg1) {
1158
- const ret = getObject(arg0).lookFor(__wbindgen_enum_Look[arg1]);
1159
- return isLikeNone(ret) ? 0 : addHeapObject(ret);
1160
- }, arguments);
1161
- },
1162
1320
  __wbg_new_1f236d63ba0c4784: function (arg0, arg1) {
1163
1321
  const ret = new Error(getStringFromWasm0(arg0, arg1));
1164
1322
  return addHeapObject(ret);
@@ -1173,9 +1331,11 @@ function __wbg_get_imports() {
1173
1331
  return addHeapObject(ret);
1174
1332
  }, arguments);
1175
1333
  },
1176
- __wbg_rooms_c5dbffde521939da: function () {
1177
- const ret = Game.rooms;
1178
- return addHeapObject(ret);
1334
+ __wbg_set_a0e911be3da02782: function () {
1335
+ return handleError(function (arg0, arg1, arg2) {
1336
+ const ret = Reflect.set(getObject(arg0), getObject(arg1), getObject(arg2));
1337
+ return ret;
1338
+ }, arguments);
1179
1339
  },
1180
1340
  __wbg_set_packed_cc769b1ab2aa7e8e: function (arg0, arg1) {
1181
1341
  getObject(arg0).__packedPos = arg1 >>> 0;
@@ -1191,10 +1351,6 @@ function __wbg_get_imports() {
1191
1351
  const ret = RoomPosition.prototype;
1192
1352
  return addHeapObject(ret);
1193
1353
  },
1194
- __wbg_structure_type_dbc211ed19dd8423: function (arg0) {
1195
- const ret = getObject(arg0).structureType;
1196
- return (__wbindgen_enum_StructureType.indexOf(ret) + 1 || 22) - 1;
1197
- },
1198
1354
  __wbindgen_cast_0000000000000001: function (arg0) {
1199
1355
  // Cast intrinsic for `F64 -> Externref`.
1200
1356
  const ret = arg0;
@@ -1219,8 +1375,6 @@ function __wbg_get_imports() {
1219
1375
  "./screeps_clockwork_bg.js": import0,
1220
1376
  };
1221
1377
  }
1222
- const __wbindgen_enum_Look = ["creep", "energy", "resource", "source", "mineral", "structure", "flag", "constructionSite", "nuke", "terrain", "tombstone", "powerCreep", "deposit", "ruin", "scoreContainer", "scoreCollector", "symbolContainer", "symbolDecoder", "reactor"];
1223
- const __wbindgen_enum_StructureType = ["spawn", "extension", "road", "constructedWall", "rampart", "keeperLair", "portal", "controller", "link", "storage", "tower", "observer", "powerBank", "powerSpawn", "extractor", "lab", "terminal", "container", "nuker", "factory", "invaderCore"];
1224
1378
  const ClockworkCostMatrixFinalization = (typeof FinalizationRegistry === 'undefined')
1225
1379
  ? { register: () => { }, unregister: () => { } }
1226
1380
  : new FinalizationRegistry(ptr => wasm.__wbg_clockworkcostmatrix_free(ptr, 1));
@@ -1245,9 +1399,6 @@ const MultiroomMonoFlowFieldFinalization = (typeof FinalizationRegistry === 'und
1245
1399
  const PathFinalization = (typeof FinalizationRegistry === 'undefined')
1246
1400
  ? { register: () => { }, unregister: () => { } }
1247
1401
  : new FinalizationRegistry(ptr => wasm.__wbg_path_free(ptr, 1));
1248
- (typeof FinalizationRegistry === 'undefined')
1249
- ? { register: () => { }, unregister: () => { } }
1250
- : new FinalizationRegistry(ptr => wasm.__wbg_pathfatigue_free(ptr, 1));
1251
1402
  (typeof FinalizationRegistry === 'undefined')
1252
1403
  ? { register: () => { }, unregister: () => { } }
1253
1404
  : new FinalizationRegistry(ptr => wasm.__wbg_searchgoal_free(ptr, 1));
@@ -1273,6 +1424,74 @@ function addBorrowedObject(obj) {
1273
1424
  heap[--stack_pointer] = obj;
1274
1425
  return stack_pointer;
1275
1426
  }
1427
+ function debugString(val) {
1428
+ // primitive types
1429
+ const type = typeof val;
1430
+ if (type == 'number' || type == 'boolean' || val == null) {
1431
+ return `${val}`;
1432
+ }
1433
+ if (type == 'string') {
1434
+ return `"${val}"`;
1435
+ }
1436
+ if (type == 'symbol') {
1437
+ const description = val.description;
1438
+ if (description == null) {
1439
+ return 'Symbol';
1440
+ }
1441
+ else {
1442
+ return `Symbol(${description})`;
1443
+ }
1444
+ }
1445
+ if (type == 'function') {
1446
+ const name = val.name;
1447
+ if (typeof name == 'string' && name.length > 0) {
1448
+ return `Function(${name})`;
1449
+ }
1450
+ else {
1451
+ return 'Function';
1452
+ }
1453
+ }
1454
+ // objects
1455
+ if (Array.isArray(val)) {
1456
+ const length = val.length;
1457
+ let debug = '[';
1458
+ if (length > 0) {
1459
+ debug += debugString(val[0]);
1460
+ }
1461
+ for (let i = 1; i < length; i++) {
1462
+ debug += ', ' + debugString(val[i]);
1463
+ }
1464
+ debug += ']';
1465
+ return debug;
1466
+ }
1467
+ // Test for built-in
1468
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
1469
+ let className;
1470
+ if (builtInMatches && builtInMatches.length > 1) {
1471
+ className = builtInMatches[1];
1472
+ }
1473
+ else {
1474
+ // Failed to match the standard '[object ClassName]'
1475
+ return toString.call(val);
1476
+ }
1477
+ if (className == 'Object') {
1478
+ // we're a user defined class or Object
1479
+ // JSON.stringify avoids problems with cycles, and is generally much
1480
+ // easier than looping through ownProperties of `val`.
1481
+ try {
1482
+ return 'Object(' + JSON.stringify(val) + ')';
1483
+ }
1484
+ catch (_) {
1485
+ return 'Object';
1486
+ }
1487
+ }
1488
+ // errors
1489
+ if (val instanceof Error) {
1490
+ return `${val.name}: ${val.message}\n${val.stack}`;
1491
+ }
1492
+ // TODO we could test for more things here, like `Set`s and `Map`s.
1493
+ return className;
1494
+ }
1276
1495
  function dropObject(idx) {
1277
1496
  if (idx < 1028)
1278
1497
  return;
@@ -1451,6 +1670,35 @@ function initSync(module) {
1451
1670
  return __wbg_finalize_init(instance);
1452
1671
  }
1453
1672
 
1673
+ function packPositions(positions) {
1674
+ const packed = new Uint32Array(positions.length);
1675
+ for (let i = 0; i < positions.length; i++) {
1676
+ packed[i] = positions[i].__packedPos;
1677
+ }
1678
+ return packed;
1679
+ }
1680
+ function packDestinations(destinations) {
1681
+ if (!destinations) {
1682
+ return undefined;
1683
+ }
1684
+ const packed = new Uint32Array(destinations.length * 2);
1685
+ for (let i = 0; i < destinations.length; i++) {
1686
+ const offset = i * 2;
1687
+ packed[offset] = destinations[i].pos.__packedPos;
1688
+ packed[offset + 1] = destinations[i].range;
1689
+ }
1690
+ return packed;
1691
+ }
1692
+ function packPortalPairs(portals) {
1693
+ const packed = new Uint32Array(portals.length * 2);
1694
+ for (let i = 0; i < portals.length; i++) {
1695
+ const offset = i * 2;
1696
+ packed[offset] = portals[i][0].__packedPos;
1697
+ packed[offset + 1] = portals[i][1].__packedPos;
1698
+ }
1699
+ return packed;
1700
+ }
1701
+
1454
1702
  const MAX_USIZE = 0xffffffff;
1455
1703
 
1456
1704
  function fromPacked(packedPos) {
@@ -1472,6 +1720,15 @@ function fromPackedRoomName(packedRoomName) {
1472
1720
  const result = (x_coord >= 0 ? `E${x_coord}` : `W${~x_coord}`) + (y_coord >= 0 ? `S${y_coord}` : `N${~y_coord}`);
1473
1721
  return result;
1474
1722
  }
1723
+ const roomNameCache = new Map();
1724
+ function fromPackedRoomNameCached(packedRoomName) {
1725
+ let roomName = roomNameCache.get(packedRoomName);
1726
+ if (roomName === undefined) {
1727
+ roomName = fromPackedRoomName(packedRoomName);
1728
+ roomNameCache.set(packedRoomName, roomName);
1729
+ }
1730
+ return roomName;
1731
+ }
1475
1732
  function packRoomName(room) {
1476
1733
  // Handle sim room case
1477
1734
  if (room === 'sim') {
@@ -1492,30 +1749,47 @@ function packRoomName(room) {
1492
1749
  return (x << 8) | y;
1493
1750
  }
1494
1751
 
1752
+ function assertNotFreed(handle, label) {
1753
+ if (!handle) {
1754
+ throw new Error(`${label} has been freed`);
1755
+ }
1756
+ return handle;
1757
+ }
1758
+ function freeHandle(handle) {
1759
+ handle === null || handle === void 0 ? void 0 : handle.free();
1760
+ return undefined;
1761
+ }
1762
+
1495
1763
  /**
1496
1764
  * A distance map for a single room.
1497
1765
  */
1498
1766
  class ClockworkDistanceMap {
1499
- constructor(_map) {
1500
- this._map = _map;
1767
+ constructor(map) {
1768
+ this._map = map;
1769
+ }
1770
+ /**
1771
+ * Frees the underlying WASM distance map allocation.
1772
+ */
1773
+ free() {
1774
+ this._map = freeHandle(this._map);
1501
1775
  }
1502
1776
  /**
1503
1777
  * Gets the distance value at a given position.
1504
1778
  */
1505
1779
  get(x, y) {
1506
- return this._map.get(x, y);
1780
+ return assertNotFreed(this._map, 'ClockworkDistanceMap').get(x, y);
1507
1781
  }
1508
1782
  /**
1509
1783
  * Sets the distance value at a given position.
1510
1784
  */
1511
1785
  set(x, y, value) {
1512
- this._map.set(x, y, value);
1786
+ assertNotFreed(this._map, 'ClockworkDistanceMap').set(x, y, value);
1513
1787
  }
1514
1788
  /**
1515
1789
  * Converts the distance map into a flat array of distances.
1516
1790
  */
1517
1791
  toArray() {
1518
- return this._map.toArray();
1792
+ return assertNotFreed(this._map, 'ClockworkDistanceMap').toArray();
1519
1793
  }
1520
1794
  }
1521
1795
 
@@ -1523,38 +1797,44 @@ class ClockworkDistanceMap {
1523
1797
  * A flow field for a single room that stores multiple directions per tile.
1524
1798
  */
1525
1799
  class ClockworkFlowField {
1526
- constructor(_flowField) {
1527
- this._flowField = _flowField;
1800
+ constructor(flowField) {
1801
+ this._flowField = flowField;
1802
+ }
1803
+ /**
1804
+ * Frees the underlying WASM flow field allocation.
1805
+ */
1806
+ free() {
1807
+ this._flowField = freeHandle(this._flowField);
1528
1808
  }
1529
1809
  /**
1530
1810
  * Get the internal value for a given coordinate.
1531
1811
  */
1532
1812
  get(x, y) {
1533
- return this._flowField.get(x, y);
1813
+ return assertNotFreed(this._flowField, 'ClockworkFlowField').get(x, y);
1534
1814
  }
1535
1815
  /**
1536
1816
  * Set the internal value for a given coordinate.
1537
1817
  */
1538
1818
  set(x, y, value) {
1539
- this._flowField.set(x, y, value);
1819
+ assertNotFreed(this._flowField, 'ClockworkFlowField').set(x, y, value);
1540
1820
  }
1541
1821
  /**
1542
1822
  * Get the list of valid directions for a given coordinate.
1543
1823
  */
1544
1824
  getDirections(x, y) {
1545
- return this._flowField.getDirections(x, y);
1825
+ return assertNotFreed(this._flowField, 'ClockworkFlowField').getDirections(x, y);
1546
1826
  }
1547
1827
  /**
1548
1828
  * Set the list of valid directions for a given coordinate.
1549
1829
  */
1550
1830
  setDirections(x, y, directions) {
1551
- this._flowField.setDirections(x, y, directions);
1831
+ assertNotFreed(this._flowField, 'ClockworkFlowField').setDirections(x, y, directions);
1552
1832
  }
1553
1833
  /**
1554
1834
  * Add a direction to the list of valid directions for a given coordinate.
1555
1835
  */
1556
1836
  addDirection(x, y, direction) {
1557
- this._flowField.addDirection(x, y, direction);
1837
+ assertNotFreed(this._flowField, 'ClockworkFlowField').addDirection(x, y, direction);
1558
1838
  }
1559
1839
  }
1560
1840
 
@@ -1566,6 +1846,12 @@ class ClockworkPath {
1566
1846
  constructor(path) {
1567
1847
  this.path = path;
1568
1848
  }
1849
+ /**
1850
+ * Frees the underlying WASM path allocation.
1851
+ */
1852
+ free() {
1853
+ this.path = freeHandle(this.path);
1854
+ }
1569
1855
  /**
1570
1856
  * Iterate over the path.
1571
1857
  *
@@ -1617,7 +1903,7 @@ class ClockworkPath {
1617
1903
  * Get the position at a given index.
1618
1904
  */
1619
1905
  get(index) {
1620
- const packedPos = this.path.get(index);
1906
+ const packedPos = assertNotFreed(this.path, 'ClockworkPath').get(index);
1621
1907
  if (packedPos === undefined) {
1622
1908
  throw new Error('Index out of bounds');
1623
1909
  }
@@ -1627,20 +1913,20 @@ class ClockworkPath {
1627
1913
  * Get the length of the path.
1628
1914
  */
1629
1915
  get length() {
1630
- return this.path.len();
1916
+ return assertNotFreed(this.path, 'ClockworkPath').len();
1631
1917
  }
1632
1918
  /**
1633
1919
  * Given a current position, find the index of the next position in the path.
1634
1920
  */
1635
1921
  findNextIndex(pos) {
1636
- return this.path.find_next_index(pos.__packedPos);
1922
+ return assertNotFreed(this.path, 'ClockworkPath').find_next_index(pos.__packedPos);
1637
1923
  }
1638
1924
  /**
1639
1925
  * Convert the path to an array of positions.
1640
1926
  */
1641
1927
  toArray() {
1642
1928
  const result = [];
1643
- for (const packedPos of this.path.to_array()) {
1929
+ for (const packedPos of assertNotFreed(this.path, 'ClockworkPath').to_array()) {
1644
1930
  result.push(fromPacked(packedPos));
1645
1931
  }
1646
1932
  return result;
@@ -1650,7 +1936,7 @@ class ClockworkPath {
1650
1936
  */
1651
1937
  toArrayReversed() {
1652
1938
  const result = [];
1653
- for (const packedPos of this.path.to_array_reversed()) {
1939
+ for (const packedPos of assertNotFreed(this.path, 'ClockworkPath').to_array_reversed()) {
1654
1940
  result.push(fromPacked(packedPos));
1655
1941
  }
1656
1942
  return result;
@@ -1661,45 +1947,51 @@ class ClockworkPath {
1661
1947
  * A flow field that spans multiple rooms, storing multiple directions per tile.
1662
1948
  */
1663
1949
  class ClockworkMultiroomFlowField {
1664
- constructor(_flowField) {
1665
- this._flowField = _flowField;
1950
+ constructor(flowField) {
1951
+ this._flowField = flowField;
1952
+ }
1953
+ /**
1954
+ * Frees the underlying WASM multiroom flow field allocation.
1955
+ */
1956
+ free() {
1957
+ this._flowField = freeHandle(this._flowField);
1666
1958
  }
1667
1959
  /**
1668
1960
  * Get the flow field value at a given position.
1669
1961
  */
1670
1962
  get(pos) {
1671
- return this._flowField.get(pos.__packedPos);
1963
+ return assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField').get(pos.__packedPos);
1672
1964
  }
1673
1965
  /**
1674
1966
  * Set the flow field value at a given position.
1675
1967
  */
1676
1968
  set(pos, value) {
1677
- this._flowField.set(pos.__packedPos, value);
1969
+ assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField').set(pos.__packedPos, value);
1678
1970
  }
1679
1971
  /**
1680
1972
  * Get the list of valid directions at a given position.
1681
1973
  */
1682
1974
  getDirections(pos) {
1683
- return this._flowField.getDirections(pos.__packedPos);
1975
+ return assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField').getDirections(pos.__packedPos);
1684
1976
  }
1685
1977
  /**
1686
1978
  * Set the list of valid directions at a given position.
1687
1979
  */
1688
1980
  setDirections(pos, directions) {
1689
- this._flowField.setDirections(pos.__packedPos, directions);
1981
+ assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField').setDirections(pos.__packedPos, directions);
1690
1982
  }
1691
1983
  /**
1692
1984
  * Add a direction to the list of valid directions at a given position.
1693
1985
  */
1694
1986
  addDirection(pos, direction) {
1695
- this._flowField.addDirection(pos.__packedPos, direction);
1987
+ assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField').addDirection(pos.__packedPos, direction);
1696
1988
  }
1697
1989
  /**
1698
1990
  * Get the list of rooms in the flow field.
1699
1991
  */
1700
1992
  getRooms() {
1701
1993
  const rooms = [];
1702
- for (const packedRoomName of this._flowField.getRooms()) {
1994
+ for (const packedRoomName of assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField').getRooms()) {
1703
1995
  rooms.push(fromPackedRoomName(packedRoomName));
1704
1996
  }
1705
1997
  return rooms;
@@ -1708,14 +2000,20 @@ class ClockworkMultiroomFlowField {
1708
2000
  * Get the flow field for a given room.
1709
2001
  */
1710
2002
  getRoom(roomName) {
1711
- const flowField = this._flowField.getRoom(packRoomName(roomName));
2003
+ const flowField = assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField').getRoom(packRoomName(roomName));
1712
2004
  return flowField ? new ClockworkFlowField(flowField) : null;
1713
2005
  }
1714
2006
  /**
1715
2007
  * Find a path from a given position to the origin of the flow field.
1716
2008
  */
1717
2009
  pathToOrigin(start) {
1718
- return new ClockworkPath(js_path_to_multiroom_flow_field_origin(start.__packedPos, this._flowField));
2010
+ return new ClockworkPath(js_path_to_multiroom_flow_field_origin(start.__packedPos, assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField')));
2011
+ }
2012
+ /**
2013
+ * Find a portal-aware path from a given position to the origin of the flow field.
2014
+ */
2015
+ pathToOriginWithPortals(start) {
2016
+ return new ClockworkPath(js_path_to_multiroom_flow_field_origin_with_portals(start.__packedPos, assertNotFreed(this._flowField, 'ClockworkMultiroomFlowField')));
1719
2017
  }
1720
2018
  }
1721
2019
 
@@ -1723,20 +2021,26 @@ class ClockworkMultiroomFlowField {
1723
2021
  * A flow field for a single room that stores one direction per tile.
1724
2022
  */
1725
2023
  class ClockworkMonoFlowField {
1726
- constructor(_flowField) {
1727
- this._flowField = _flowField;
2024
+ constructor(flowField) {
2025
+ this._flowField = flowField;
2026
+ }
2027
+ /**
2028
+ * Frees the underlying WASM flow field allocation.
2029
+ */
2030
+ free() {
2031
+ this._flowField = freeHandle(this._flowField);
1728
2032
  }
1729
2033
  /**
1730
2034
  * Get the direction for a given coordinate.
1731
2035
  */
1732
2036
  get(x, y) {
1733
- return this._flowField.get(x, y);
2037
+ return assertNotFreed(this._flowField, 'ClockworkMonoFlowField').get(x, y);
1734
2038
  }
1735
2039
  /**
1736
2040
  * Set the direction for a given coordinate.
1737
2041
  */
1738
2042
  set(x, y, value) {
1739
- this._flowField.set(x, y, value);
2043
+ assertNotFreed(this._flowField, 'ClockworkMonoFlowField').set(x, y, value);
1740
2044
  }
1741
2045
  }
1742
2046
 
@@ -1744,28 +2048,34 @@ class ClockworkMonoFlowField {
1744
2048
  * A flow field that spans multiple rooms, storing a single direction per tile.
1745
2049
  */
1746
2050
  class ClockworkMultiroomMonoFlowField {
1747
- constructor(_flowField) {
1748
- this._flowField = _flowField;
2051
+ constructor(flowField) {
2052
+ this._flowField = flowField;
2053
+ }
2054
+ /**
2055
+ * Frees the underlying WASM multiroom flow field allocation.
2056
+ */
2057
+ free() {
2058
+ this._flowField = freeHandle(this._flowField);
1749
2059
  }
1750
2060
  /**
1751
2061
  * Get the direction at a given position.
1752
2062
  */
1753
2063
  get(pos) {
1754
2064
  var _a;
1755
- return (_a = this._flowField.get(pos.__packedPos)) !== null && _a !== void 0 ? _a : null;
2065
+ return (_a = assertNotFreed(this._flowField, 'ClockworkMultiroomMonoFlowField').get(pos.__packedPos)) !== null && _a !== void 0 ? _a : null;
1756
2066
  }
1757
2067
  /**
1758
2068
  * Set the direction at a given position.
1759
2069
  */
1760
2070
  set(pos, direction) {
1761
- this._flowField.set(pos.__packedPos, direction !== null && direction !== void 0 ? direction : undefined);
2071
+ assertNotFreed(this._flowField, 'ClockworkMultiroomMonoFlowField').set(pos.__packedPos, direction !== null && direction !== void 0 ? direction : undefined);
1762
2072
  }
1763
2073
  /**
1764
2074
  * Get the list of rooms in the flow field.
1765
2075
  */
1766
2076
  getRooms() {
1767
2077
  const rooms = [];
1768
- for (const packedRoomName of this._flowField.getRooms()) {
2078
+ for (const packedRoomName of assertNotFreed(this._flowField, 'ClockworkMultiroomMonoFlowField').getRooms()) {
1769
2079
  rooms.push(fromPackedRoomName(packedRoomName));
1770
2080
  }
1771
2081
  return rooms;
@@ -1774,14 +2084,20 @@ class ClockworkMultiroomMonoFlowField {
1774
2084
  * Get the flow field for a given room.
1775
2085
  */
1776
2086
  getRoom(roomName) {
1777
- const flowField = this._flowField.getRoom(packRoomName(roomName));
2087
+ const flowField = assertNotFreed(this._flowField, 'ClockworkMultiroomMonoFlowField').getRoom(packRoomName(roomName));
1778
2088
  return flowField ? new ClockworkMonoFlowField(flowField) : null;
1779
2089
  }
1780
2090
  /**
1781
2091
  * Find a path from a given position to the origin of the flow field.
1782
2092
  */
1783
2093
  pathToOrigin(start) {
1784
- return new ClockworkPath(js_path_to_multiroom_mono_flow_field_origin(start.__packedPos, this._flowField));
2094
+ return new ClockworkPath(js_path_to_multiroom_mono_flow_field_origin(start.__packedPos, assertNotFreed(this._flowField, 'ClockworkMultiroomMonoFlowField')));
2095
+ }
2096
+ /**
2097
+ * Find a portal-aware path from a given position to the origin of the flow field.
2098
+ */
2099
+ pathToOriginWithPortals(start) {
2100
+ return new ClockworkPath(js_path_to_multiroom_mono_flow_field_origin_with_portals(start.__packedPos, assertNotFreed(this._flowField, 'ClockworkMultiroomMonoFlowField')));
1785
2101
  }
1786
2102
  }
1787
2103
 
@@ -1791,33 +2107,39 @@ const DEFAULT_DIRECTION_ORDER = DirectionOrder.CardinalFirst;
1791
2107
  * like `bfsMultiroomDistanceMap` rather than created directly.
1792
2108
  */
1793
2109
  class ClockworkMultiroomDistanceMap {
1794
- constructor(_map) {
1795
- this._map = _map;
2110
+ constructor(map) {
2111
+ this._map = map;
2112
+ }
2113
+ /**
2114
+ * Frees the underlying WASM multiroom distance map allocation.
2115
+ */
2116
+ free() {
2117
+ this._map = freeHandle(this._map);
1796
2118
  }
1797
2119
  /**
1798
2120
  * Get the stored value for a given position.
1799
2121
  */
1800
2122
  get(pos) {
1801
- return this._map.get(pos.__packedPos);
2123
+ return assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap').get(pos.__packedPos);
1802
2124
  }
1803
2125
  /**
1804
2126
  * Set the stored value for a given position.
1805
2127
  */
1806
2128
  set(pos, value) {
1807
- this._map.set(pos.__packedPos, value);
2129
+ assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap').set(pos.__packedPos, value);
1808
2130
  }
1809
2131
  /**
1810
2132
  * Get the DistanceMap for a given room.
1811
2133
  */
1812
2134
  getRoom(room) {
1813
- const map = this._map.get_room(packRoomName(room));
2135
+ const map = assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap').get_room(packRoomName(room));
1814
2136
  return map ? new ClockworkDistanceMap(map) : undefined;
1815
2137
  }
1816
2138
  /**
1817
2139
  * List all the rooms covered by this distance map.
1818
2140
  */
1819
2141
  getRooms() {
1820
- return [...this._map.get_rooms()].map(room => fromPackedRoomName(room));
2142
+ return [...assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap').get_rooms()].map(room => fromPackedRoomName(room));
1821
2143
  }
1822
2144
  /**
1823
2145
  * Path to the origin from a given position.
@@ -1825,7 +2147,15 @@ class ClockworkMultiroomDistanceMap {
1825
2147
  */
1826
2148
  pathToOrigin(start, options = {}) {
1827
2149
  var _a;
1828
- return new ClockworkPath(js_path_to_multiroom_distance_map_origin(start.__packedPos, this._map, (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
2150
+ return new ClockworkPath(js_path_to_multiroom_distance_map_origin(start.__packedPos, assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap'), (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
2151
+ }
2152
+ /**
2153
+ * Portal-aware path to the origin from a given position.
2154
+ * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal steps when multiple neighbors are equally close.
2155
+ */
2156
+ pathToOriginWithPortals(start, options = {}) {
2157
+ var _a;
2158
+ return new ClockworkPath(js_path_to_multiroom_distance_map_origin_with_portals(start.__packedPos, assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap'), (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
1829
2159
  }
1830
2160
  /**
1831
2161
  * Flow field for this distance map.
@@ -1833,7 +2163,15 @@ class ClockworkMultiroomDistanceMap {
1833
2163
  */
1834
2164
  toFlowField(options = {}) {
1835
2165
  var _a;
1836
- return new ClockworkMultiroomFlowField(multiroomFlowField(this._map, (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
2166
+ return new ClockworkMultiroomFlowField(multiroomFlowField(assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap'), (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
2167
+ }
2168
+ /**
2169
+ * Portal-aware flow field for this distance map.
2170
+ * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal directions when multiple neighbors are equally close.
2171
+ */
2172
+ toFlowFieldWithPortals(options = {}) {
2173
+ var _a;
2174
+ return new ClockworkMultiroomFlowField(multiroomPortalFlowField(assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap'), (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
1837
2175
  }
1838
2176
  /**
1839
2177
  * Mono-directional flow field for this distance map.
@@ -1841,22 +2179,35 @@ class ClockworkMultiroomDistanceMap {
1841
2179
  */
1842
2180
  toMonoFlowField(options = {}) {
1843
2181
  var _a;
1844
- return new ClockworkMultiroomMonoFlowField(multiroomMonoFlowField(this._map, (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
2182
+ return new ClockworkMultiroomMonoFlowField(multiroomMonoFlowField(assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap'), (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
2183
+ }
2184
+ /**
2185
+ * Portal-aware monodirectional flow field for this distance map.
2186
+ * Pass `DirectionOrder.DiagonalFirst` to prefer diagonal directions when multiple neighbors are equally close.
2187
+ */
2188
+ toMonoFlowFieldWithPortals(options = {}) {
2189
+ var _a;
2190
+ return new ClockworkMultiroomMonoFlowField(multiroomPortalMonoFlowField(assertNotFreed(this._map, 'ClockworkMultiroomDistanceMap'), (_a = options.directionOrder) !== null && _a !== void 0 ? _a : DEFAULT_DIRECTION_ORDER));
1845
2191
  }
1846
2192
  }
1847
2193
 
1848
2194
  function fromPackedSearchResult(result) {
1849
- const foundTargets = result.found_targets.reduce((acc, pos) => {
1850
- acc.push(fromPacked(pos));
1851
- return acc;
1852
- }, []);
1853
- const distanceMap = new ClockworkMultiroomDistanceMap(result.distance_map);
1854
- const ops = result.ops;
1855
- return {
1856
- distanceMap,
1857
- foundTargets,
1858
- ops
1859
- };
2195
+ try {
2196
+ const foundTargets = result.found_targets.reduce((acc, pos) => {
2197
+ acc.push(fromPacked(pos));
2198
+ return acc;
2199
+ }, []);
2200
+ const distanceMap = new ClockworkMultiroomDistanceMap(result.distance_map);
2201
+ const ops = result.ops;
2202
+ return {
2203
+ distanceMap,
2204
+ foundTargets,
2205
+ ops
2206
+ };
2207
+ }
2208
+ finally {
2209
+ result.free();
2210
+ }
1860
2211
  }
1861
2212
 
1862
2213
  /**
@@ -1879,18 +2230,34 @@ function astarMultiroomDistanceMap(start, { costMatrixCallback, maxRooms = MAX_U
1879
2230
  if ([maxRooms, maxOps, maxPathCost].every(n => n === MAX_USIZE) && !anyOfDestinations && !allOfDestinations) {
1880
2231
  throw new Error('At least one of maxRooms, maxOps, maxPathCost, anyOfDestinations, or allOfDestinations must be set');
1881
2232
  }
1882
- const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
1883
- const result = js_astar_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomName(room)), maxRooms, maxOps, maxPathCost, anyOfDestinations
1884
- ? new Uint32Array(anyOfDestinations.reduce((acc, { pos, range }) => {
1885
- acc.push(pos.__packedPos, range);
1886
- return acc;
1887
- }, []))
1888
- : undefined, allOfDestinations
1889
- ? new Uint32Array(allOfDestinations.reduce((acc, { pos, range }) => {
1890
- acc.push(pos.__packedPos, range);
1891
- return acc;
1892
- }, []))
1893
- : undefined);
2233
+ const startPacked = packPositions(start);
2234
+ const result = js_astar_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomNameCached(room)), maxRooms, maxOps, maxPathCost, packDestinations(anyOfDestinations), packDestinations(allOfDestinations));
2235
+ return fromPackedSearchResult(result);
2236
+ }
2237
+ /**
2238
+ * Create a portal-aware distance map for the given start positions, using A* to
2239
+ * optimize the search toward the given destinations.
2240
+ *
2241
+ * Portals configured with `initialize({ portals })` or `setPortals` are treated
2242
+ * like exits: stepping onto a portal entrance lands on its paired exit.
2243
+ *
2244
+ * This calculates a distance map across multiple rooms, with a few configurable limits:
2245
+ * - `maxOps`: The maximum number of pathfinding operations to perform.
2246
+ * - `maxRooms`: The maximum number of rooms to explore.
2247
+ * - `maxPathCost`: Don't explore tiles with a greater path cost than this.
2248
+ *
2249
+ * At least one of these limits or destination lists must be set.
2250
+ *
2251
+ * @param start - The starting positions.
2252
+ * @param options - The options for the distance map.
2253
+ * @returns A multi-room distance map.
2254
+ */
2255
+ function astarPortalMultiroomDistanceMap(start, { costMatrixCallback, maxRooms = MAX_USIZE, maxOps = MAX_USIZE, maxPathCost = MAX_USIZE, anyOfDestinations, allOfDestinations }) {
2256
+ if ([maxRooms, maxOps, maxPathCost].every(n => n === MAX_USIZE) && !anyOfDestinations && !allOfDestinations) {
2257
+ throw new Error('At least one of maxRooms, maxOps, maxPathCost, anyOfDestinations, or allOfDestinations must be set');
2258
+ }
2259
+ const startPacked = packPositions(start);
2260
+ const result = js_astar_portal_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomNameCached(room)), maxRooms, maxOps, maxPathCost, packDestinations(anyOfDestinations), packDestinations(allOfDestinations));
1894
2261
  return fromPackedSearchResult(result);
1895
2262
  }
1896
2263
 
@@ -1912,20 +2279,37 @@ function astarMultiroomDistanceMap(start, { costMatrixCallback, maxRooms = MAX_U
1912
2279
  */
1913
2280
  function bfsMultiroomDistanceMap(start, { costMatrixCallback, maxOps = MAX_USIZE, maxRooms = MAX_USIZE, maxPathCost = MAX_USIZE, anyOfDestinations, allOfDestinations }) {
1914
2281
  if ([maxOps, maxRooms, maxPathCost].every(n => n === MAX_USIZE) && !anyOfDestinations && !allOfDestinations) {
1915
- throw new Error('At least one of maxOps, maxRooms, maxRoomDistance, maxPathCost, anyOfDestinations, or allOfDestinations must be set');
2282
+ throw new Error('At least one of maxOps, maxRooms, maxPathCost, anyOfDestinations, or allOfDestinations must be set');
1916
2283
  }
1917
- const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
1918
- const result = js_bfs_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomName(room)), maxOps, maxRooms, maxPathCost, anyOfDestinations
1919
- ? new Uint32Array(anyOfDestinations.reduce((acc, { pos, range }) => {
1920
- acc.push(pos.__packedPos, range);
1921
- return acc;
1922
- }, []))
1923
- : undefined, allOfDestinations
1924
- ? new Uint32Array(allOfDestinations.reduce((acc, { pos, range }) => {
1925
- acc.push(pos.__packedPos, range);
1926
- return acc;
1927
- }, []))
1928
- : undefined);
2284
+ const startPacked = packPositions(start);
2285
+ const result = js_bfs_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomNameCached(room)), maxOps, maxRooms, maxPathCost, packDestinations(anyOfDestinations), packDestinations(allOfDestinations));
2286
+ return fromPackedSearchResult(result);
2287
+ }
2288
+ /**
2289
+ * Create a portal-aware distance map for the given start positions, using a breadth-first search.
2290
+ * This does not factor in terrain costs (treating anything less than 255 in the cost
2291
+ * matrix as passable), so it's faster but less useful than Dijkstra's algorithm.
2292
+ *
2293
+ * Portals configured with `initialize({ portals })` or `setPortals` are treated
2294
+ * like exits: stepping onto a portal entrance lands on its paired exit.
2295
+ *
2296
+ * This calculates a distance map across multiple rooms, with a few configurable limits:
2297
+ * - `maxOps`: The maximum number of pathfinding operations to perform.
2298
+ * - `maxRooms`: The maximum number of rooms to explore.
2299
+ * - `maxPathCost`: Don't explore tiles with a greater path cost than this.
2300
+ *
2301
+ * At least one of these limits or destination lists must be set.
2302
+ *
2303
+ * @param start - The starting positions.
2304
+ * @param options - The options for the distance map.
2305
+ * @returns A multi-room distance map.
2306
+ */
2307
+ function bfsPortalMultiroomDistanceMap(start, { costMatrixCallback, maxOps = MAX_USIZE, maxRooms = MAX_USIZE, maxPathCost = MAX_USIZE, anyOfDestinations, allOfDestinations }) {
2308
+ if ([maxOps, maxRooms, maxPathCost].every(n => n === MAX_USIZE) && !anyOfDestinations && !allOfDestinations) {
2309
+ throw new Error('At least one of maxOps, maxRooms, maxPathCost, anyOfDestinations, or allOfDestinations must be set');
2310
+ }
2311
+ const startPacked = packPositions(start);
2312
+ const result = js_bfs_portal_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomNameCached(room)), maxOps, maxRooms, maxPathCost, packDestinations(anyOfDestinations), packDestinations(allOfDestinations));
1929
2313
  return fromPackedSearchResult(result);
1930
2314
  }
1931
2315
 
@@ -1948,18 +2332,34 @@ function dijkstraMultiroomDistanceMap(start, { costMatrixCallback, maxOps = MAX_
1948
2332
  if ([maxOps, maxRooms, maxPathCost].every(n => n === MAX_USIZE) && !anyOfDestinations && !allOfDestinations) {
1949
2333
  throw new Error('At least one of maxOps, maxRooms, maxPathCost, anyOfDestinations, or allOfDestinations must be set');
1950
2334
  }
1951
- const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
1952
- const result = js_dijkstra_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomName(room)), maxOps, maxRooms, maxPathCost, anyOfDestinations
1953
- ? new Uint32Array(anyOfDestinations.reduce((acc, { pos, range }) => {
1954
- acc.push(pos.__packedPos, range);
1955
- return acc;
1956
- }, []))
1957
- : undefined, allOfDestinations
1958
- ? new Uint32Array(allOfDestinations.reduce((acc, { pos, range }) => {
1959
- acc.push(pos.__packedPos, range);
1960
- return acc;
1961
- }, []))
1962
- : undefined);
2335
+ const startPacked = packPositions(start);
2336
+ const result = js_dijkstra_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomNameCached(room)), maxOps, maxRooms, maxPathCost, packDestinations(anyOfDestinations), packDestinations(allOfDestinations));
2337
+ return fromPackedSearchResult(result);
2338
+ }
2339
+ /**
2340
+ * Create a portal-aware distance map for the given start positions, using Dijkstra's
2341
+ * algorithm to factor in terrain costs (0-255, where 255 is impassable).
2342
+ *
2343
+ * Portals configured with `initialize({ portals })` or `setPortals` are treated
2344
+ * like exits: stepping onto a portal entrance lands on its paired exit.
2345
+ *
2346
+ * This calculates a distance map across multiple rooms, with a few configurable limits:
2347
+ * - `maxOps`: The maximum number of pathfinding operations to perform.
2348
+ * - `maxRooms`: The maximum number of rooms to explore.
2349
+ * - `maxPathCost`: Don't explore tiles with a greater path cost than this.
2350
+ *
2351
+ * At least one of these limits or destination lists must be set.
2352
+ *
2353
+ * @param start - The starting positions.
2354
+ * @param options - The options for the distance map.
2355
+ * @returns A multi-room distance map.
2356
+ */
2357
+ function dijkstraPortalMultiroomDistanceMap(start, { costMatrixCallback, maxOps = MAX_USIZE, maxRooms = MAX_USIZE, maxPathCost = MAX_USIZE, anyOfDestinations, allOfDestinations }) {
2358
+ if ([maxOps, maxRooms, maxPathCost].every(n => n === MAX_USIZE) && !anyOfDestinations && !allOfDestinations) {
2359
+ throw new Error('At least one of maxOps, maxRooms, maxPathCost, anyOfDestinations, or allOfDestinations must be set');
2360
+ }
2361
+ const startPacked = packPositions(start);
2362
+ const result = js_dijkstra_portal_multiroom_distance_map(startPacked, (room) => costMatrixCallback(fromPackedRoomNameCached(room)), maxOps, maxRooms, maxPathCost, packDestinations(anyOfDestinations), packDestinations(allOfDestinations));
1963
2363
  return fromPackedSearchResult(result);
1964
2364
  }
1965
2365
 
@@ -1985,15 +2385,31 @@ let wasm_bytes;
1985
2385
  let wasm_module;
1986
2386
  let wasm_instance;
1987
2387
  let initialized = false;
2388
+ let initializeOptionsApplied = false;
2389
+ let pendingInitializeOptions;
2390
+ let configuredPortalList;
2391
+ let configuredPortalDistanceCacheRoomLimit;
1988
2392
  /**
1989
- * The `initialize` function should be called in your main loop before
1990
- * using any other screeps-clockwork functions. Depending on available
1991
- * CPU, it may not load the WASM module completely in the first tick,
1992
- * but it will pick back up where it left off if the script times out.
2393
+ * The `initialize` function initializes the WASM module and applies global
2394
+ * setup options. Call it in your main loop before using any other
2395
+ * screeps-clockwork functions. Depending on available CPU, it may not load the
2396
+ * WASM module completely in the first tick, but it will pick back up where it
2397
+ * left off if the script times out.
2398
+ *
2399
+ * It is safe to call this function repeatedly, but setup options such as
2400
+ * portals and portal cache sizing are applied only once, after WASM
2401
+ * initialization has completed. Use `setPortals` when you intentionally need
2402
+ * to reconfigure portals later.
1993
2403
  *
1994
- * @param verbose - If true, will log the state of the WASM module as it loads.
2404
+ * @param options - Pass `true` or `{ verbose: true }` to log WASM loading;
2405
+ * provide portals and cache sizing to apply them once after WASM initialization.
1995
2406
  */
1996
- function initialize(verbose = false) {
2407
+ function initialize(options = false) {
2408
+ var _a;
2409
+ const verbose = typeof options === 'boolean' ? options : ((_a = options.verbose) !== null && _a !== void 0 ? _a : false);
2410
+ if (typeof options !== 'boolean') {
2411
+ recordInitializeOptions(options);
2412
+ }
1997
2413
  // need to freshly override the fake console object each tick
1998
2414
  console.error = console_error;
1999
2415
  const start = Game.cpu.getUsed();
@@ -2012,6 +2428,77 @@ function initialize(verbose = false) {
2012
2428
  console.logUnsafe(`[clockwork] version ${version()} initialized with ${(Game.cpu.getUsed() - start).toFixed(2)} CPU`);
2013
2429
  }
2014
2430
  initialized = true;
2431
+ applyInitializeOptions();
2432
+ }
2433
+ function recordInitializeOptions(options) {
2434
+ var _a, _b;
2435
+ if (options.portalDistanceCacheRoomLimit === undefined && options.portals === undefined) {
2436
+ return;
2437
+ }
2438
+ pendingInitializeOptions = {
2439
+ portals: (_a = options.portals) !== null && _a !== void 0 ? _a : pendingInitializeOptions === null || pendingInitializeOptions === void 0 ? void 0 : pendingInitializeOptions.portals,
2440
+ portalDistanceCacheRoomLimit: (_b = options.portalDistanceCacheRoomLimit) !== null && _b !== void 0 ? _b : pendingInitializeOptions === null || pendingInitializeOptions === void 0 ? void 0 : pendingInitializeOptions.portalDistanceCacheRoomLimit
2441
+ };
2442
+ }
2443
+ function applyInitializeOptions() {
2444
+ if (!initialized || initializeOptionsApplied || !pendingInitializeOptions) {
2445
+ return;
2446
+ }
2447
+ const options = pendingInitializeOptions;
2448
+ if (options.portalDistanceCacheRoomLimit !== undefined || options.portals !== undefined) {
2449
+ if (options.portalDistanceCacheRoomLimit !== undefined &&
2450
+ options.portalDistanceCacheRoomLimit !== configuredPortalDistanceCacheRoomLimit) {
2451
+ applyPortalDistanceCacheRoomLimit(options.portalDistanceCacheRoomLimit);
2452
+ }
2453
+ if (options.portals && options.portals !== configuredPortalList) {
2454
+ setPortals(options.portals);
2455
+ }
2456
+ initializeOptionsApplied = true;
2457
+ }
2458
+ }
2459
+ /**
2460
+ * Configure the portal pairs used by portal-aware pathfinding functions.
2461
+ * Each pair is installed bidirectionally.
2462
+ */
2463
+ function setPortals(portals) {
2464
+ set_portals(packPortalPairs(portals));
2465
+ configuredPortalList = portals;
2466
+ }
2467
+ function applyPortalDistanceCacheRoomLimit(roomLimit) {
2468
+ if (!Number.isFinite(roomLimit) || roomLimit < 0 || Math.floor(roomLimit) !== roomLimit) {
2469
+ throw new Error('Portal distance cache room limit must be a non-negative integer');
2470
+ }
2471
+ set_portal_distance_cache_room_limit(roomLimit);
2472
+ configuredPortalDistanceCacheRoomLimit = roomLimit;
2473
+ }
2474
+ /**
2475
+ * Remove all configured portal pairs while preserving the initialize-time cache limit.
2476
+ */
2477
+ function clearPortals() {
2478
+ clear_portals();
2479
+ configuredPortalList = undefined;
2480
+ }
2481
+ /**
2482
+ * Return debug counters for the configured portal index and its nearest-portal cache.
2483
+ * `totalSize` is an approximate in-memory size formatted for logs; `totalSizeBytes`
2484
+ * preserves the raw byte estimate.
2485
+ */
2486
+ function debugPortalIndex() {
2487
+ const info = debug_portal_index();
2488
+ return Object.assign(Object.assign({}, info), { totalSize: formatBytes(info.totalSizeBytes) });
2489
+ }
2490
+ function formatBytes(bytes) {
2491
+ const units = ['B', 'KiB', 'MiB', 'GiB'];
2492
+ let value = bytes;
2493
+ let unitIndex = 0;
2494
+ while (value >= 1024 && unitIndex < units.length - 1) {
2495
+ value /= 1024;
2496
+ unitIndex++;
2497
+ }
2498
+ if (unitIndex === 0) {
2499
+ return `${bytes} B`;
2500
+ }
2501
+ return `${value.toFixed(2)} ${units[unitIndex]}`;
2015
2502
  }
2016
2503
  // This provides the function `console.error` that wasm_bindgen sometimes expects to exist,
2017
2504
  // especially with type checks in debug mode. An alternative is to have this be `function () {}`
@@ -2019,8 +2506,8 @@ function initialize(verbose = false) {
2019
2506
  // information that wasm_bindgen only passes here.
2020
2507
  //
2021
2508
  // There is nothing special about this function and it may also be used by any JS/Rust code as a convenience.
2022
- function console_error() {
2023
- const processedArgs = Array.from(arguments)
2509
+ function console_error(...args) {
2510
+ const processedArgs = args
2024
2511
  .map(arg => {
2025
2512
  if (arg instanceof Error) {
2026
2513
  // On this version of Node, the `stack` property of errors contains
@@ -2043,9 +2530,15 @@ exports.ClockworkMultiroomMonoFlowField = ClockworkMultiroomMonoFlowField;
2043
2530
  exports.ClockworkPath = ClockworkPath;
2044
2531
  exports.DirectionOrder = DirectionOrder;
2045
2532
  exports.astarMultiroomDistanceMap = astarMultiroomDistanceMap;
2533
+ exports.astarPortalMultiroomDistanceMap = astarPortalMultiroomDistanceMap;
2046
2534
  exports.bfsMultiroomDistanceMap = bfsMultiroomDistanceMap;
2535
+ exports.bfsPortalMultiroomDistanceMap = bfsPortalMultiroomDistanceMap;
2536
+ exports.clearPortals = clearPortals;
2537
+ exports.debugPortalIndex = debugPortalIndex;
2047
2538
  exports.dijkstraMultiroomDistanceMap = dijkstraMultiroomDistanceMap;
2539
+ exports.dijkstraPortalMultiroomDistanceMap = dijkstraPortalMultiroomDistanceMap;
2048
2540
  exports.getRange = getRange;
2049
2541
  exports.getTerrainCostMatrix = getTerrainCostMatrix;
2050
2542
  exports.initialize = initialize;
2543
+ exports.setPortals = setPortals;
2051
2544
  //# sourceMappingURL=index.js.map