isaacscript-common 10.0.0 → 10.1.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.
Files changed (88) hide show
  1. package/dist/index.d.ts +78 -22
  2. package/dist/isaacscript-common.lua +203 -102
  3. package/dist/package.lua +2 -2
  4. package/dist/src/features/deployJSONRoom.d.ts +16 -6
  5. package/dist/src/features/deployJSONRoom.d.ts.map +1 -1
  6. package/dist/src/features/deployJSONRoom.lua +16 -6
  7. package/dist/src/features/saveDataManager/load.d.ts.map +1 -1
  8. package/dist/src/features/saveDataManager/merge.d.ts.map +1 -1
  9. package/dist/src/features/saveDataManager/saveDataManagerConstants.d.ts +1 -1
  10. package/dist/src/features/saveDataManager/saveDataManagerConstants.d.ts.map +1 -1
  11. package/dist/src/functions/bitSet128.d.ts +2 -1
  12. package/dist/src/functions/bitSet128.d.ts.map +1 -1
  13. package/dist/src/functions/bitSet128.lua +2 -2
  14. package/dist/src/functions/color.d.ts +2 -3
  15. package/dist/src/functions/color.d.ts.map +1 -1
  16. package/dist/src/functions/color.lua +2 -2
  17. package/dist/src/functions/curses.d.ts +1 -1
  18. package/dist/src/functions/curses.lua +1 -1
  19. package/dist/src/functions/deepCopy.d.ts +2 -1
  20. package/dist/src/functions/deepCopy.d.ts.map +1 -1
  21. package/dist/src/functions/deepCopy.lua +19 -25
  22. package/dist/src/functions/deepCopyTests.lua +13 -26
  23. package/dist/src/functions/doors.d.ts +9 -1
  24. package/dist/src/functions/doors.d.ts.map +1 -1
  25. package/dist/src/functions/doors.lua +8 -1
  26. package/dist/src/functions/kColor.d.ts +2 -3
  27. package/dist/src/functions/kColor.d.ts.map +1 -1
  28. package/dist/src/functions/kColor.lua +2 -2
  29. package/dist/src/functions/rng.d.ts +2 -2
  30. package/dist/src/functions/rng.d.ts.map +1 -1
  31. package/dist/src/functions/serialization.d.ts +11 -4
  32. package/dist/src/functions/serialization.d.ts.map +1 -1
  33. package/dist/src/functions/serialization.lua +15 -0
  34. package/dist/src/functions/table.d.ts +2 -2
  35. package/dist/src/functions/table.d.ts.map +1 -1
  36. package/dist/src/functions/table.lua +6 -5
  37. package/dist/src/functions/vector.d.ts +2 -1
  38. package/dist/src/functions/vector.d.ts.map +1 -1
  39. package/dist/src/functions/vector.lua +2 -2
  40. package/dist/src/functions/weighted.d.ts +6 -0
  41. package/dist/src/functions/weighted.d.ts.map +1 -0
  42. package/dist/src/functions/weighted.lua +35 -0
  43. package/dist/src/index.d.ts +2 -1
  44. package/dist/src/index.d.ts.map +1 -1
  45. package/dist/src/index.lua +8 -0
  46. package/dist/src/interfaces/SaveData.d.ts +3 -8
  47. package/dist/src/interfaces/SaveData.d.ts.map +1 -1
  48. package/dist/src/objects/isaacAPIClassTypeToBrand.d.ts +1 -1
  49. package/dist/src/objects/isaacAPIClassTypeToBrand.d.ts.map +1 -1
  50. package/dist/src/objects/isaacAPIClassTypeToBrand.lua +2 -2
  51. package/dist/src/objects/isaacAPIClassTypeToFunctions.d.ts +36 -8
  52. package/dist/src/objects/isaacAPIClassTypeToFunctions.d.ts.map +1 -1
  53. package/dist/src/objects/isaacAPIClassTypeToFunctions.lua +42 -7
  54. package/dist/src/types/WeightedArray.d.ts +4 -0
  55. package/dist/src/types/WeightedArray.d.ts.map +1 -0
  56. package/dist/src/types/{SerializedIsaacAPIClass.lua → WeightedArray.lua} +0 -0
  57. package/package.json +2 -2
  58. package/src/callbacks/customRevive.ts +1 -2
  59. package/src/features/deployJSONRoom.ts +16 -6
  60. package/src/features/saveDataManager/exports.ts +1 -1
  61. package/src/features/saveDataManager/load.ts +0 -2
  62. package/src/features/saveDataManager/merge.ts +0 -3
  63. package/src/features/saveDataManager/saveDataManagerConstants.ts +1 -1
  64. package/src/functions/bitSet128.ts +8 -2
  65. package/src/functions/color.ts +8 -2
  66. package/src/functions/curses.ts +1 -1
  67. package/src/functions/deepCopy.ts +25 -23
  68. package/src/functions/deepCopyTests.ts +19 -41
  69. package/src/functions/doors.ts +13 -1
  70. package/src/functions/initArray.ts +1 -1
  71. package/src/functions/kColor.ts +8 -2
  72. package/src/functions/rng.ts +2 -0
  73. package/src/functions/serialization.ts +72 -18
  74. package/src/functions/table.ts +8 -5
  75. package/src/functions/vector.ts +11 -3
  76. package/src/functions/weighted.ts +36 -0
  77. package/src/index.ts +2 -1
  78. package/src/interfaces/SaveData.ts +4 -3
  79. package/src/objects/isaacAPIClassTypeToBrand.ts +1 -1
  80. package/src/objects/isaacAPIClassTypeToFunctions.ts +58 -9
  81. package/src/types/WeightedArray.ts +2 -0
  82. package/dist/src/enums/private/CopyableIsaacAPIClassType.d.ts +0 -12
  83. package/dist/src/enums/private/CopyableIsaacAPIClassType.d.ts.map +0 -1
  84. package/dist/src/enums/private/CopyableIsaacAPIClassType.lua +0 -10
  85. package/dist/src/types/SerializedIsaacAPIClass.d.ts +0 -11
  86. package/dist/src/types/SerializedIsaacAPIClass.d.ts.map +0 -1
  87. package/src/enums/private/CopyableIsaacAPIClassType.ts +0 -11
  88. package/src/types/SerializedIsaacAPIClass.ts +0 -9
@@ -1,4 +1,4 @@
1
- import { isBoolean, isNumber, isString, isTable } from "./types";
1
+ import { isBoolean, isNumber, isString, isUserdata } from "./types";
2
2
 
3
3
  /**
4
4
  * In a `Map`, you can use the `clear` method to delete every element. However, in a `LuaMap`, the
@@ -10,13 +10,13 @@ export function clearTable(luaMap: LuaMap<AnyNotNil, unknown>): void {
10
10
  }
11
11
  }
12
12
 
13
- /** Helper function to copy specific values from a object to a table. */
14
- export function copyValuesToTable(
13
+ /** Helper function to copy specific values from a userdata object (e.g. `Vector`) to a table. */
14
+ export function copyUserdataValuesToTable(
15
15
  object: unknown,
16
16
  keys: string[],
17
17
  luaMap: LuaMap<string, unknown>,
18
18
  ): void {
19
- if (!isTable(object)) {
19
+ if (!isUserdata(object)) {
20
20
  error(
21
21
  `Failed to copy an object values to a table, since the object was of type: ${type(
22
22
  object,
@@ -24,8 +24,11 @@ export function copyValuesToTable(
24
24
  );
25
25
  }
26
26
 
27
+ // We can access values on userdata objects similar to a normal table.
28
+ const userdata = object as unknown as LuaMap<AnyNotNil, unknown>;
29
+
27
30
  for (const key of keys) {
28
- const value = object.get(key);
31
+ const value = userdata.get(key);
29
32
  luaMap.set(key, value);
30
33
  }
31
34
  }
@@ -1,14 +1,22 @@
1
- import { Direction } from "isaac-typescript-definitions";
1
+ import {
2
+ CopyableIsaacAPIClassType,
3
+ Direction,
4
+ } from "isaac-typescript-definitions";
2
5
  import { SerializationBrand } from "../enums/private/SerializationBrand";
3
6
  import { angleToDirection } from "./direction";
4
7
  import { isaacAPIClassEquals, isIsaacAPIClassOfType } from "./isaacAPIClass";
5
8
  import { getRandomFloat } from "./random";
6
9
  import { getRandomSeed, isRNG, newRNG } from "./rng";
7
- import { copyValuesToTable, getNumbersFromTable, tableHasKeys } from "./table";
10
+ import {
11
+ copyUserdataValuesToTable,
12
+ getNumbersFromTable,
13
+ tableHasKeys,
14
+ } from "./table";
8
15
  import { isTable } from "./types";
9
16
 
10
17
  export type SerializedVector = LuaMap<string, unknown> & {
11
18
  readonly __serializedVectorBrand: symbol;
19
+ readonly __kind: CopyableIsaacAPIClassType.VECTOR;
12
20
  };
13
21
 
14
22
  const OBJECT_NAME = "Vector";
@@ -104,7 +112,7 @@ export function serializeVector(vector: Vector): SerializedVector {
104
112
  }
105
113
 
106
114
  const vectorTable = new LuaMap<string, unknown>();
107
- copyValuesToTable(vector, KEYS, vectorTable);
115
+ copyUserdataValuesToTable(vector, KEYS, vectorTable);
108
116
  vectorTable.set(SerializationBrand.VECTOR, "");
109
117
  return vectorTable as SerializedVector;
110
118
  }
@@ -0,0 +1,36 @@
1
+ import { WeightedArray } from "../types/WeightedArray";
2
+ import { arrayToString, sumArray } from "./array";
3
+ import { getRandomFloat } from "./random";
4
+ import { getRandomSeed } from "./rng";
5
+
6
+ /** Get a random value from a `WeightedArray`. (The second element in the array is the weight.) */
7
+ export function getRandomFromWeightedArray<T>(
8
+ weightedArray: WeightedArray<T>,
9
+ seedOrRNG: Seed | RNG = getRandomSeed(),
10
+ ): T {
11
+ if (weightedArray.length === 0) {
12
+ error(
13
+ "Failed to get a random element from a weighted array since the provided array was empty.",
14
+ );
15
+ }
16
+
17
+ const weights = weightedArray.map((tuple) => tuple[1]);
18
+ const totalWeight = sumArray(weights);
19
+ const randomWeight = getRandomFloat(0, totalWeight, seedOrRNG);
20
+
21
+ let weightAccumulator = 0;
22
+ for (const tuple of weightedArray) {
23
+ const [element, weight] = tuple;
24
+
25
+ weightAccumulator += weight;
26
+ if (weightAccumulator >= randomWeight) {
27
+ return element;
28
+ }
29
+ }
30
+
31
+ error(
32
+ `Failed to get a random element from a weighted array: ${arrayToString(
33
+ weightedArray,
34
+ )}`,
35
+ );
36
+ }
package/src/index.ts CHANGED
@@ -149,6 +149,7 @@ export * from "./functions/types";
149
149
  export * from "./functions/ui";
150
150
  export * from "./functions/utils";
151
151
  export * from "./functions/vector";
152
+ export * from "./functions/weighted";
152
153
  export * from "./interfaces/ChargeBarSprites";
153
154
  export * from "./interfaces/Corner";
154
155
  export * from "./interfaces/CustomStageTSConfig";
@@ -174,5 +175,5 @@ export * from "./types/PickingUpItem";
174
175
  export * from "./types/PickupIndex";
175
176
  export * from "./types/PlayerIndex";
176
177
  export * from "./types/PossibleStatType";
177
- export * from "./types/SerializedIsaacAPIClass";
178
178
  export * from "./types/TSTLClass";
179
+ export * from "./types/WeightedArray";
@@ -1,5 +1,7 @@
1
1
  /* eslint-disable max-classes-per-file */
2
2
 
3
+ import { CopyableIsaacAPIClass } from "../objects/isaacAPIClassTypeToFunctions";
4
+
3
5
  /**
4
6
  * This is the format of the object that you give to the save data manager. It will contains all of
5
7
  * the variables for the particular mod feature.
@@ -45,7 +47,7 @@ type Serializable<T> =
45
47
  T extends SerializablePrimitive
46
48
  ? T
47
49
  : // Allow a specific subset of Isaac API classes that are copyable / serializable.
48
- T extends SerializableIsaacAPIClass
50
+ T extends CopyableIsaacAPIClass
49
51
  ? T
50
52
  : // Disallow all other Isaac API classes.
51
53
  T extends IsaacAPIClass
@@ -77,7 +79,7 @@ type SerializableInsideArrayOrMap<T> =
77
79
  T extends SerializablePrimitive
78
80
  ? T
79
81
  : // Allow a specific subset of Isaac API classes that are copyable / serializable.
80
- T extends SerializableIsaacAPIClass
82
+ T extends CopyableIsaacAPIClass
81
83
  ? T
82
84
  : // Disallow all other Isaac API classes.
83
85
  T extends IsaacAPIClass
@@ -125,7 +127,6 @@ type SerializableReadonlyMap<K, V> = ReadonlyMap<
125
127
  type SerializableSet<T> = Set<SerializableInsideArrayOrMap<T>>;
126
128
  type SerializableReadonlySet<T> = ReadonlySet<SerializableInsideArrayOrMap<T>>;
127
129
  type SerializableObject<T> = { [K in keyof T]: Serializable<T[K]> };
128
- type SerializableIsaacAPIClass = BitSet128 | Color | KColor | RNG | Vector;
129
130
 
130
131
  // eslint-disable-next-line @typescript-eslint/ban-types
131
132
  type HasMethods<T> = {} extends {
@@ -1,4 +1,4 @@
1
- import { CopyableIsaacAPIClassType } from "../enums/private/CopyableIsaacAPIClassType";
1
+ import { CopyableIsaacAPIClassType } from "isaac-typescript-definitions";
2
2
  import { SerializationBrand } from "../enums/private/SerializationBrand";
3
3
 
4
4
  export const ISAAC_API_CLASS_TYPE_TO_BRAND: {
@@ -1,72 +1,121 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
- import { CopyableIsaacAPIClassType } from "../enums/private/CopyableIsaacAPIClassType";
1
+ import { CopyableIsaacAPIClassType } from "isaac-typescript-definitions";
4
2
  import {
5
3
  copyBitSet128,
6
4
  deserializeBitSet128,
5
+ isBitSet128,
7
6
  isSerializedBitSet128,
8
7
  serializeBitSet128,
8
+ SerializedBitSet128,
9
9
  } from "../functions/bitSet128";
10
10
  import {
11
11
  copyColor,
12
12
  deserializeColor,
13
+ isColor,
13
14
  isSerializedColor,
14
15
  serializeColor,
16
+ SerializedColor,
15
17
  } from "../functions/color";
16
18
  import {
17
19
  copyKColor,
18
20
  deserializeKColor,
21
+ isKColor,
19
22
  isSerializedKColor,
23
+ SerializedKColor,
20
24
  serializeKColor,
21
25
  } from "../functions/kColor";
22
26
  import {
23
27
  copyRNG,
24
28
  deserializeRNG,
29
+ isRNG,
25
30
  isSerializedRNG,
31
+ SerializedRNG,
26
32
  serializeRNG,
27
33
  } from "../functions/rng";
28
34
  import {
29
35
  copyVector,
30
36
  deserializeVector,
31
37
  isSerializedVector,
38
+ isVector,
39
+ SerializedVector,
32
40
  serializeVector,
33
41
  } from "../functions/vector";
34
42
 
35
- interface IsaacAPIClassTypeFunctions {
36
- isSerialized: (object: unknown) => object is unknown;
37
- copy: (object: any) => any;
38
- serialize: (object: any) => any;
39
- deserialize: (object: any) => any;
43
+ /** A type representing an Isaac API class that can be safely copied or serialized. */
44
+ export type CopyableIsaacAPIClass = BitSet128 | Color | KColor | RNG | Vector;
45
+
46
+ /**
47
+ * A type representing an Isaac API class like `Color` or `RNG` that has been written to the
48
+ * "save#.dat" file. It is used by the save data manager when reading and writing to that file.
49
+ *
50
+ * For the list of supported classes, see the `CopyableIsaacAPIClassType` enum.
51
+ */
52
+ export type SerializedIsaacAPIClass =
53
+ | SerializedBitSet128
54
+ | SerializedColor
55
+ | SerializedKColor
56
+ | SerializedRNG
57
+ | SerializedVector;
58
+
59
+ export interface IsaacAPIClassTypeToType {
60
+ [CopyableIsaacAPIClassType.BIT_SET_128]: BitSet128;
61
+ [CopyableIsaacAPIClassType.COLOR]: Color;
62
+ [CopyableIsaacAPIClassType.K_COLOR]: KColor;
63
+ [CopyableIsaacAPIClassType.RNG]: RNG;
64
+ [CopyableIsaacAPIClassType.VECTOR]: Vector;
65
+ }
66
+
67
+ export interface IsaacAPIClassTypeToSerializedType {
68
+ [CopyableIsaacAPIClassType.BIT_SET_128]: SerializedBitSet128;
69
+ [CopyableIsaacAPIClassType.COLOR]: SerializedColor;
70
+ [CopyableIsaacAPIClassType.K_COLOR]: SerializedKColor;
71
+ [CopyableIsaacAPIClassType.RNG]: SerializedRNG;
72
+ [CopyableIsaacAPIClassType.VECTOR]: SerializedVector;
73
+ }
74
+
75
+ export interface IsaacAPIClassTypeFunctions<T, SerializedT> {
76
+ is: (object: unknown) => object is T;
77
+ isSerialized: (object: unknown) => object is SerializedT;
78
+ copy: (object: T) => T;
79
+ serialize: (object: T) => SerializedT;
80
+ deserialize: (object: SerializedT) => T;
40
81
  }
41
82
 
42
83
  export const ISAAC_API_CLASS_TYPE_TO_FUNCTIONS: {
43
- readonly [key in CopyableIsaacAPIClassType]: IsaacAPIClassTypeFunctions;
84
+ readonly [key in CopyableIsaacAPIClassType]: IsaacAPIClassTypeFunctions<
85
+ IsaacAPIClassTypeToType[key],
86
+ IsaacAPIClassTypeToSerializedType[key]
87
+ >;
44
88
  } = {
45
89
  [CopyableIsaacAPIClassType.BIT_SET_128]: {
90
+ is: isBitSet128,
46
91
  isSerialized: isSerializedBitSet128,
47
92
  copy: copyBitSet128,
48
93
  serialize: serializeBitSet128,
49
94
  deserialize: deserializeBitSet128,
50
95
  },
51
96
  [CopyableIsaacAPIClassType.COLOR]: {
97
+ is: isColor,
52
98
  isSerialized: isSerializedColor,
53
99
  copy: copyColor,
54
100
  serialize: serializeColor,
55
101
  deserialize: deserializeColor,
56
102
  },
57
103
  [CopyableIsaacAPIClassType.K_COLOR]: {
104
+ is: isKColor,
58
105
  isSerialized: isSerializedKColor,
59
106
  copy: copyKColor,
60
107
  serialize: serializeKColor,
61
108
  deserialize: deserializeKColor,
62
109
  },
63
110
  [CopyableIsaacAPIClassType.RNG]: {
111
+ is: isRNG,
64
112
  isSerialized: isSerializedRNG,
65
113
  copy: copyRNG,
66
114
  serialize: serializeRNG,
67
115
  deserialize: deserializeRNG,
68
116
  },
69
117
  [CopyableIsaacAPIClassType.VECTOR]: {
118
+ is: isVector,
70
119
  isSerialized: isSerializedVector,
71
120
  copy: copyVector,
72
121
  serialize: serializeVector,
@@ -0,0 +1,2 @@
1
+ /** An array where each element is paired with a number indicating that elements 'weight'. */
2
+ export type WeightedArray<T> = Array<[T, float]>;
@@ -1,12 +0,0 @@
1
- /**
2
- * - This must match the JSDoc comments for `deepCopy` and `merge`.
3
- * - This must match the `SerializableIsaacAPIClass` type union.
4
- */
5
- export declare enum CopyableIsaacAPIClassType {
6
- BIT_SET_128 = "BitSet128",
7
- COLOR = "Color",
8
- K_COLOR = "KColor",
9
- RNG = "RNG",
10
- VECTOR = "Vector"
11
- }
12
- //# sourceMappingURL=CopyableIsaacAPIClassType.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CopyableIsaacAPIClassType.d.ts","sourceRoot":"","sources":["../../../../src/enums/private/CopyableIsaacAPIClassType.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,oBAAY,yBAAyB;IACnC,WAAW,cAAc;IACzB,KAAK,UAAU;IACf,OAAO,WAAW;IAClB,GAAG,QAAQ;IACX,MAAM,WAAW;CAClB"}
@@ -1,10 +0,0 @@
1
- local ____exports = {}
2
- --- - This must match the JSDoc comments for `deepCopy` and `merge`.
3
- -- - This must match the `SerializableIsaacAPIClass` type union.
4
- ____exports.CopyableIsaacAPIClassType = {}
5
- ____exports.CopyableIsaacAPIClassType.BIT_SET_128 = "BitSet128"
6
- ____exports.CopyableIsaacAPIClassType.COLOR = "Color"
7
- ____exports.CopyableIsaacAPIClassType.K_COLOR = "KColor"
8
- ____exports.CopyableIsaacAPIClassType.RNG = "RNG"
9
- ____exports.CopyableIsaacAPIClassType.VECTOR = "Vector"
10
- return ____exports
@@ -1,11 +0,0 @@
1
- /// <reference types="@typescript-to-lua/language-extensions" />
2
- /**
3
- * This is a type representing an Isaac API class like `Color` or `RNG` that has been written to the
4
- * "save#.dat" file. It is used by the save data manager when reading and writing to that file.
5
- *
6
- * For the list of supported classes, see the `CopyableIsaacAPIClassType` enum.
7
- */
8
- export declare type SerializedIsaacAPIClass = LuaMap<string, unknown> & {
9
- readonly __serializedIsaacAPIClassBrand: symbol;
10
- };
11
- //# sourceMappingURL=SerializedIsaacAPIClass.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SerializedIsaacAPIClass.d.ts","sourceRoot":"","sources":["../../../src/types/SerializedIsaacAPIClass.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;AACH,oBAAY,uBAAuB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAC9D,QAAQ,CAAC,8BAA8B,EAAE,MAAM,CAAC;CACjD,CAAC"}
@@ -1,11 +0,0 @@
1
- /**
2
- * - This must match the JSDoc comments for `deepCopy` and `merge`.
3
- * - This must match the `SerializableIsaacAPIClass` type union.
4
- */
5
- export enum CopyableIsaacAPIClassType {
6
- BIT_SET_128 = "BitSet128",
7
- COLOR = "Color",
8
- K_COLOR = "KColor",
9
- RNG = "RNG",
10
- VECTOR = "Vector",
11
- }
@@ -1,9 +0,0 @@
1
- /**
2
- * This is a type representing an Isaac API class like `Color` or `RNG` that has been written to the
3
- * "save#.dat" file. It is used by the save data manager when reading and writing to that file.
4
- *
5
- * For the list of supported classes, see the `CopyableIsaacAPIClassType` enum.
6
- */
7
- export type SerializedIsaacAPIClass = LuaMap<string, unknown> & {
8
- readonly __serializedIsaacAPIClassBrand: symbol;
9
- };